Example #1
0
/* 
   orders incoming blocks of data into dedispersed sub-bands 
*/
void dedisperse_data(FILE *input, FILE *output) /*includefile*/
{ 
  char message[80];
  float *buff[2], *dedisp, realtime, nextbaseline, *offset, *tmpblk;
  int readnext=0,isamp,bnum,nsamp,i,j,s,c,b,indx,ns[2],soffset,ddidx;
  int ic,ixnb,ixnc,*ishift,maxshift,nsblk,nsmax,cpb,d,spb,x,nsout,nxb;
  int *ignore;

  /* calculate table of shift values given the filterbank and DM */
  ishift=dmshift(fch1,foff,nchans,nbands,userdm,refrf,tsamp,frequency_table);
  maxshift=ishift[nchans-1];

  /* set the buffer size based on the maximum shift */
  nsblk=256*nchans; nsout=32*nchans;
  /*nsblk=256*nchans; nsout=32768*nchans;*/
  nsmax=maxshift*nifs*nchans;
  if (nsmax>nsblk) nsblk=nsmax;
  nxb=nifs*nbands;

  /* define the buffers and initialize counters */
  dedisp =(float *) malloc(nxb*nsout*sizeof(float));
  offset =(float *) malloc(nxb*sizeof(float));
  tmpblk =(float *) malloc(nsout*sizeof(float));
  buff[0]=(float *) malloc(nsblk*sizeof(float));
  buff[1]=(float *) malloc(nsblk*sizeof(float));
  for (i=0;i<nxb;i++) offset[i]=0.0;
  d=bnum=isamp=0;
  ic=nchans*nifs;
  nextbaseline=realtime=0.0;

  /* zero any channels that are in the ignored list of channels */
  if (file_exists(ignfile)) {
    ignore=ignored_channels(ignfile,nchans);
  } else {
    ignore=(int *) malloc(nchans*sizeof(int));
    for (i=0;i<nchans;i++) ignore[i]=0;
  }

  /* number of channels per band to dedisperse (cpb) must be an integer */
  cpb=nchans/nbands; 
  if ((cpb*nbands) != nchans) error_message("silly sub-band selection!");

  /* main loop - keep going until no more data comes in */
  while (1) {

    /* read in the buffer to be processed if not done so already */
    if (!readnext) {
      sprintf(message,"time:%.1fs:DM:%.1fpc/cc",realtime,refdm);
      update_log(message);
      if ((ns[bnum]=read_block(input,nbits,buff[bnum],nsblk))<=0) {
	if (isamp)write_dedisp(dedisp,isamp,nifs,nbands,offset,output);
	return;
      }
    }

    /* number of samples in this buffer */
    nsamp=ns[bnum]/ic;

    /* flag to signify whether next buffer has been read in (1=yes;0=no) */
    readnext=0;

    /* dedispersing loop over all samples in this buffer */
    for (s=0; s<nsamp; s++) {
      soffset=isamp*nxb;
      /* loop over the IFs */
      for (i=0; i<nifs; i++) {
	/* number of channels to skip within this IF */
	ixnc=i*nchans; 
	ixnb=i*nbands;
	for (b=0; b<nbands; b++) {
	  /* calculate index of this sample */
	  ddidx=soffset+ixnb+b;
	  /* clear array element for storing dedispersed subband */
	  dedisp[ddidx]=0.0; 
	  /* loop over the channels in this subband */
	  for (c=b*cpb;c<(b+1)*cpb;c++) {
	    /* proceed only if selected channel # is not in ignore list */
	    if (!ignore[c]) 
	    {
	      /* calculate index of sample to be added */
	      indx=(s+ishift[c])*ic+ixnc+c;
	      /* required sample will be in either this buffer or the next */
	      if (indx<ns[bnum]) {
	        dedisp[ddidx]+=buff[bnum][indx];
	      } else {
	        if (!readnext) {
		  if ((ns[!bnum]=read_block(input,nbits,buff[!bnum],nsblk))<=0) {
		    if (isamp) {
		      write_dedisp(dedisp,isamp,nifs,nbands,offset,output);
		    }
		    return;
		  }
		  sprintf(message,"time:%.1fs:DM:%.1fpc/cc",realtime,refdm);
		  update_log(message);
		  readnext=1;
	        }
	        dedisp[ddidx]+=buff[!bnum][indx-ns[bnum]];
	      }
	    }
	  }
	} /* end of loop over subbands */
      } /* end of loop over IFs */
      /* update number of samples dedispersed and elapsed time */
      isamp++; realtime+=tsamp;
      if (isamp==nsout) {
	if (baseline) {
	  for (i=0;i<nifs;i++) {
	    ixnb=i*nbands;
	    for (b=0;b<nbands;b++) {
	      for (j=0;j<nsout;j++) tmpblk[j]=dedisp[j*nxb+ixnb+b];
	      offset[ixnb+b]=nrselect(nsout/2,nsout,tmpblk-1);
	    }
	  }
	}
	write_dedisp(dedisp,nsout,nifs,nbands,offset,output);
	isamp=0;
      }
    } /* end of loop over samples */
    /* switch to next buffer */
    bnum=!bnum;
  } /* end of main loop */
}
Example #2
0
/* 
   orders incoming blocks of data into dedispersed sub-bands 
 */
void dedisperse_data(FILE *input, FILE *output) /*includefile*/
{ 
	char message[80];
	float *buff[2], *dedisp, realtime, nextbaseline, *offset, *tmpblk;
	int readnext=0,isamp,bnum,nsamp,i,j,s,c,b,indx,ns[2],soffset,ddidx;
	int ic,ixnb,ixnc,*ishift,maxshift,nsblk,nsmax,cpb,d,spb,x,nsout,nxb;
	int *ignore;
	unsigned long long totalsamp;

	totalsamp = 0;

	/* calculate table of shift values given the filterbank and DM */
	ishift=dmshift(fch1,foff,nchans,nbands,userdm,refrf,tsamp,frequency_table);
	maxshift=ishift[nchans-1];

	/* set the buffer size based on the maximum shift */
	nsblk=256*nchans; nsout=32*nchans;  
	/*nsblk=256*nchans; nsout=32768*nchans;*/
	nsmax=maxshift*nifs*nchans;
	if (nsmax>nsblk) nsblk=nsmax;
	nxb=nifs*nbands;

	/* define the buffers and initialize counters */
	dedisp =(float *) malloc(nxb*nsout*sizeof(float));
	offset =(float *) malloc(nxb*sizeof(float));
	tmpblk =(float *) malloc(nsout*sizeof(float));
	buff[0]=(float *) malloc(nsblk*sizeof(float));
	buff[1]=(float *) malloc(nsblk*sizeof(float));
	for (i=0;i<nxb;i++) offset[i]=0.0;
	d=bnum=isamp=0;
	ic=nchans*nifs;
	nextbaseline=realtime=0.0;

	/* zero any channels that are in the ignored list of channels */
	if (file_exists(ignfile)) {
		ignore=ignored_channels(ignfile,nchans);
	} else {
		ignore=(int *) malloc(nchans*sizeof(int));
		for (i=0;i<nchans;i++) ignore[i]=0;
	}

	/* number of channels per band to dedisperse (cpb) must be an integer */
	cpb=nchans/nbands; 
	if ((cpb*nbands) != nchans) error_message("silly sub-band selection!");

	/* main loop - keep going until no more data comes in */
	while (1) {

		/* read in the buffer to be processed if not done so already */
		if (!readnext) {
			sprintf(message,"time:%.1fs:DM:%.1fpc/cc",realtime,refdm);
			update_log(message);
			if ((ns[bnum] = read_block (input, nbits, buff[bnum], nsblk)) <= 0){
				/* There were no more samples to read */
				if (isamp)write_dedisp(dedisp,isamp,nifs,nbands,offset,output);
				return;
			} else {
				/* We managed to read some samples!
				 * There may be some things we want to do to the data as we read it in
				 * These are done here if they need to be done.
				 */

				if(ns[bnum]%nchans){
					/* If we get here then ns[bnum]/nchans is not integer!
					 * This is really bad, as we assume that we read in an
					 * integer number of samples.
					 */
					fprintf(stderr,"ERROR: Could not read an integer number of samples (nread/nchans is not integer).\nPerhaps the file is incomplete or the stream was terminated.\nUnfortunately dedispersion cannot continue.\n");
					exit(2);
				}

 				/* Do we want to clip the input channel data
				 * Note that we must do this BEFORE zerodming
                                 */
                                if(doInputClip){
                                        inputClip(buff[bnum],ns[bnum]/nchans,inputClipLow,inputClipHigh,doInputClip); /* It is important that ns[bnum]/nchans is an integer! */
                                }


				/* Do we want to do the 'zero dm' subtraction?
				 */
				if(doSubZero){
					subzero(buff[bnum],ns[bnum]/nchans); /* It is important that ns[bnum]/nchans is an integer! */
				}

				if(doBaselineFile){
					reverseBaselineNormalise(buff[bnum],totalsamp,ns[bnum]/nchans,nchans);
				}

			}

		}

		/* number of samples in this buffer */
		nsamp=ns[bnum]/ic;

		/* flag to signify whether next buffer has been read in (1=yes;0=no) */
		readnext=0;

		/* dedispersing loop over all samples in this buffer */
		for (s=0; s<nsamp; s++) {
			soffset=isamp*nxb;
			/* loop over the IFs */
			for (i=0; i<nifs; i++) {
				/* number of channels to skip within this IF */
				ixnc=i*nchans; 
				ixnb=i*nbands;
				for (b=0; b<nbands; b++) {
					/* calculate index of this sample */
					ddidx=soffset+ixnb+b;
					/* clear array element for storing dedispersed subband */
					dedisp[ddidx]=0.0; 
					/* loop over the channels in this subband */
					for (c=b*cpb;c<(b+1)*cpb;c++) {
						/* proceed only if selected channel # is not in ignore list */
						if (!ignore[c]) 
						{
							/* calculate index of sample to be added */
							indx=(s+ishift[c])*ic+ixnc+c;
							/* required sample will be in either this buffer or the next */
							if (indx<ns[bnum]) {
								dedisp[ddidx]+=buff[bnum][indx];
							} else {
								if (!readnext) {
									if ((ns[!bnum]=read_block(input,nbits,buff[!bnum],nsblk))<=0) {
									/* There are no more samples to read, nread <= 0 */
										if (isamp) {
											write_dedisp(dedisp,isamp,nifs,nbands,offset,output);
										}
										return;
									} else {
										/* We read some samples, as nread > 0.
										 * There may be some things we want to do to the data as we read it in
										 * These are done here if they need to be done.
										 */

										if(ns[!bnum]%nchans){
											/* If we get here then ns[bnum]/nchans is not integer!
											 * This is really bad, as we assume that we read in an
											 * integer number of samples.
											 */
											fprintf(stderr,"ERROR: Could not read an integer number of samples (nread/nchans is not integer).\nPerhaps the file is incomplete or the stream was terminated.\nUnfortunately dedispersion cannot continue.\n");
											exit(2);
										}

                                                                                /* Do we want to clip the input channel data
                                                                                 */
                                                                                if(doInputClip){
                                                                                        inputClip(buff[!bnum],ns[!bnum]/nchans,inputClipLow,inputClipHigh,doInputClip);
											/* It is important that ns[!bnum]/nchans is an integer! */
                                                                                }

										/* Do we want to do the 'zero dm' subtraction?
										 */
										if(doSubZero){
											subzero(buff[!bnum],ns[!bnum]/nchans); /* It is important that ns[!bnum]/nchans is an integer! */
										}
										if(doBaselineFile){
											reverseBaselineNormalise(buff[!bnum],totalsamp,ns[!bnum]/nchans,nchans);
										}



									}
									sprintf(message,"time:%.1fs:DM:%.1fpc/cc",realtime,refdm);
									update_log(message);
									readnext=1;
								}
								dedisp[ddidx]+=buff[!bnum][indx-ns[bnum]];
							}
						}
					}
				} /* end of loop over subbands */
			} /* end of loop over IFs */
			/* update number of samples dedispersed and elapsed time */
			isamp++; realtime+=tsamp;
			totalsamp++;
			if (isamp==nsout) {
				if (baseline) {
					for (i=0;i<nifs;i++) {
						ixnb=i*nbands;
						for (b=0;b<nbands;b++) {
							for (j=0;j<nsout;j++) tmpblk[j]=dedisp[j*nxb+ixnb+b];
							offset[ixnb+b]=nrselect(nsout/2,nsout,tmpblk-1);
							//fprintf(stderr,"%d %f\n",ixnb+b,offset[ixnb+b]);
						}
					}
				}
				write_dedisp(dedisp,nsout,nifs,nbands,offset,output);
				isamp=0;
			}
		} /* end of loop over samples */
		/* switch to next buffer */
		bnum=!bnum;
	} /* end of main loop */
}