Example #1
0
main(int argc, char *argv[])
{
  FILE *fileptr;
  int i,numsamps,nfft,nspc,headersize,binmin;
  float *data, *amplitude, ar, ai, arl, ail, a1, a2;
  double frequency, pmax=9.99999;

  fileptr=open_file(argv[1],"rb");

  if (!(headersize=read_header(fileptr))) {
    error_message("could not read header parameters!");
    exit(1);
  }

  numsamps=nsamples(argv[1],headersize,nbits,nifs,nchans);
  nfft=(int)pow(2.0,(double)np2(numsamps));
  nspc=nfft/2;
  data=(float *) malloc(sizeof(float)*numsamps);
  
  printf("#Reading in %d samples from file: %s...\n",numsamps,argv[1]);
  if ((read_block(fileptr,nbits,data,numsamps)) != numsamps) {
    error_message("error reading data");
  }

  printf("#Performing 2^%d point FFT...\n",np2(numsamps));
  realft(data-1,(unsigned long) nfft,1);

  printf("#Forming amplitude spectrum...\n");
  amplitude=(float *) malloc(nspc*sizeof(float));
  binmin=fbin(tsamp,nspc,1,1.0/pmax);
  ar=ai=arl=ail=a1=a2=0.0;
  for (i=0;i<nspc;i++) {
    if (i<binmin) 
      amplitude[i]=0.0;
    else {
      ar=data[2*i];
      ai=data[2*i+1];
      a1=ar*ar+ai*ai;
      a2=0.5*((ar-arl)*(ar-arl)+(ai-ail)*(ai-ail));
      amplitude[i]=(a1>a2)?a1:a2;
      arl=ar;
      ail=ai;
    }
    /*printf("%f %f\n",ffreq(tsamp,nspc,1,i+1),amplitude[i]);*/
  }
  free(data);

}
Example #2
0
int CSCPatch::get_size()  // setting the size of Amtx.
{
  int ns = nsamples();
  if(femesh_order_ == 1) {  // available size: 3, 4, 6, 9, 16
    if(ns < 4)
      return 3;
    else if(ns < 6)
      return 4;
    else if(ns < 9)
      return 6;
    else if(ns < 16)
      return 9;
    else
      return 16;
  }
  else {  // available size: 6, 9, 16
    if(ns < 9)
      return 6;
    else if(ns < 16)
      return 9;
    else
      return 16;
  }
}
Example #3
0
main(int argc, char *argv[]) 
{
	FILE *fileptr[50];
	FILE *output;
    float buffer[4096];  //max channels 4096
    float spectra_sum[128];
    float scale_factor;
	char filename[80],*telescope,*backend,*datatype,message[80],unit[16], outfile[120];
	int i,j, k, year,month,day,check,rah,ram,ded,dem, nfiles, opened=0;
	double ras,des,frac,tobs;
	char sra[6],sde[6],decsign;
	int raw,uth,utm,uts;
	long long numsamps,datasize,headersize;
    long long mindatasize=0;
	int writeobsdbline;

	/* get these from first file on command line, apply to output file */
	int output_nchans=0, output_nbits=0, output_nifs=0;
	long long output_datasize=0;
    long long output_headersize=0;
    int output_ptr;
	char quantval;
    
    double mean=0.0, sigma=0.0;
    
    

    
  /* work out how many files are on the command line */
  i=1;
  nfiles=0;
  while(file_exists(argv[i]) && (i < argc)) {
        //printf("opening %s\n", argv[i]);
  	    nfiles++;
        i++;
  }
  if (!nfiles) {
	  error_message("no input files supplied on command line!");
	  exit(1);
  }

  if (nfiles > 50) {
      error_message("too many input files supplied on command line (max 50)!");
	  exit(1);
  }


  for(i = 0; i<nfiles; i++) {
			  fileptr[i]=open_file(argv[i+1],"rb");
  }


  if (argc>nfiles) {
	 i=nfiles+1;
	 while (i<argc) {
		if (strings_equal(argv[i],"-o")) {
		
		  /* get and open file for output */
		  strcpy(outfile,argv[++i]);
		  if(file_exists(outfile)) {
			  sprintf(message,"output file (%s) exists!",argv[i]);
			  error_message(message);
			  exit(1);
		  }
		  output=fopen(outfile,"wb");
		  opened=1;
		} else {
			/* unknown argument passed down - stop! */
			sprintf(message,"unknown argument (%s) passed to filterbank.",argv[i]);
			error_message(message);
			exit(1);
		}
		i++;
	  }
  }

  if (!opened) {
	  error_message("must have an output file (-o <output>)!");
	  exit(1);
  }


	pulsarcentric=barycentric=0;
	writeobsdbline=0;
	


    
  for(i = 0; i<nfiles; i++) {
		headersize=read_header(fileptr[i]);		
	    rewind(fileptr[i]);
		datasize=sizeof_file(argv[i+1])-headersize;
	    numsamps=nsamples(argv[i+1],headersize,nbits,nifs,nchans);
		if(output_datasize == 0 || datasize < output_datasize) {
			output_datasize = datasize;
		    output_headersize = headersize;
			output_ptr = i;
		}
		if(output_nchans == 0) {
		  output_nchans = nchans; 
		  output_nbits = nbits; 
		  output_nifs = nifs;		
		}
        if(nchans != output_nchans || nbits != output_nbits || nifs != output_nifs) {
			sprintf(message,"channel/if/bit mismatch, exiting...");
			error_message(message);
			exit(1);
        }
        
        //printf("file: %s headersize: %d nbits: %d nifs: %d nchans: %d\n", argv[i+1], headersize, nbits, nifs, nchans); 
  }


  if (output_nbits != 32) {
	  error_message("This routine only works on 32 bit (float) filterbank data!");
	  exit(1);
  }
  
 
  printf("minimum data size is: %d\n", output_datasize);
  
  printf("will dump: %d\n", (output_datasize / (long long) (output_nifs * output_nchans)));
  
  headersize=read_header(fileptr[output_ptr]);
  rewind(fileptr[output_ptr]);

  printf("header size lead file: %d\n", headersize); 
  fread(buffer, sizeof(char), headersize, fileptr[output_ptr]);
  fwrite(buffer, sizeof(char), headersize, output);
  rewind(fileptr[output_ptr]);

 /* bump past header for all input files */
   for(i = 0; i<nfiles; i++) {
		headersize=read_header(fileptr[i]);	
	}
  


 /* rewind and bump past header for all input files */
   for(i = 0; i<nfiles; i++) {
		rewind( fileptr[i] );
		headersize=read_header(fileptr[i]);	
	}

scale_factor = output_datasize / ((float) (output_nifs * output_nchans));

/* outer loop, read through all spectra, quantize to 8 bits, write to file */

  for (j = 0; j < (output_datasize / (long long) (output_nifs * output_nchans)); j++){
  
	 /* read n spectra (1 spectra x n files), sum */
	 for(i=0;i<128;i++) spectra_sum[i] = 0.0;
	 for(i = 0; i<nfiles; i++) {
	     fread(&buffer, sizeof(float), (output_nifs * output_nchans), fileptr[i]); 		

         for(k=0; k<128; k++) spectra_sum[k] = (spectra_sum[k] + (buffer[k] / scale_factor) );
	  }
	  fwrite(&spectra_sum, sizeof(float), 128, output); 		        

   }



/*
	if (argc>1) {
		print_version(argv[0],argv[1]);
		if (help_required(argv[1])) {
			header_help();
			exit(0);
		} else if (file_exists(argv[1])) {
			strcpy(filename,argv[1]);
			fileptr=open_file(filename,"rb");
		} else if (!file_exists(argv[1]) && (strncmp(argv[1],"-",1) !=0)) {
			sprintf(message,"Data file: %s not found...\n",argv[1]);
			error_message(message);
			exit(1);
		}
	}
*/

}
Example #4
0
void wapp2fb(FILE *input, FILE *output) /* includefile */
{
  FILE *bptr, *fpou, *alfa[2];
  int pixel[2];
  double pra, pdec;
  double bw, bandwidth, scale, power, hweight, tsamp_us, crate, lmst;
  double *lag, *sum, *acf, *window, jan1, days, epoch, ras,des,rahr,dede; 
  float zerolag,*block,smin,smax;
  int doit,i,j,k,two_nlags,nlags,stat,rec_size,idump,swap_bytes,ifnum,opened;
  int filesize,headersize,beam,utsecs,iymdf[4],rah,ram,ded,dem;
  unsigned char *cblock, zuc;
  unsigned short *sblock, zus; 
  unsigned int zul;
  char message[80], outfile[80];
  void *dump;
  static float realtime=0.0;

#ifdef FFTW
  fftw_plan fftplan;
#endif
  /* establish whether we need to swap bytes (WAPP is little endian) */
  swap_bytes=big_endian();

  /* initialise correlator parameters used below */
  nlags=nchans;  
  two_nlags=2*nlags;  
  bandwidth=foff*nlags;
  tsamp_us=tsamp*1.0e6;
  if (bandwidth<0.0) bandwidth *= -1.0;

#ifdef FFTW
  acf = fftw_malloc(sizeof(double) * two_nlags);
  lag = fftw_malloc(sizeof(double) * two_nlags);
  /* set up fftw table and acf array when computing power spectra */
  fftplan=fftw_plan_r2r_1d(two_nlags,acf,lag,FFTW_R2HC,FFTW_PATIENT);
#endif
#ifndef FFTW
  /* set up acf array when computing power spectra */
  acf = (double *) malloc(two_nlags * sizeof(double));
  lag = (double *) malloc(two_nlags * sizeof(double));
#endif

  if (compute_spectra) {
    /* ranges for scaling spectra */
    smin=0.0;smax=3.0;
  } else {
    /* ranges for scaling correlation functions */
    smin=-0.5;smax=1.0;
  }

  /* set up the weights for windowing of ACF to monimize FFT leakage */
  if (hanning) {
    /* Hanning window */
    hweight=0.50;
  } else if (hamming) {
    /* Hamming window */
    hweight=0.54;
  } else {
    /* no window (default) */
    hweight=1.00;
  }

  /* define the smoothing window to be applied base on the above weight */
  window = (double *) malloc(nlags * sizeof(double));
  for (j=0; j<nlags; j++) window[j]=(hweight+(1.0-hweight)*cos(PI*j/nlags));

  /* work out number of IFs to loop over */
  if (sumifs && (nifs>1)) {
    smin*=2.0;
    smax*=2.0;
    ifnum=2;
  } else {
    sumifs=0;
    ifnum=nifs;
  }

  /* calculate required record size for reading - i.e. number of bytes/dump */
  rec_size = nifs*nlags*(nbits/8);
  dump = malloc(rec_size); /* pointer to the correlator dump */

  /* correlator data rate */
  crate = 1.0/(tsamp_us-WAPP_DEAD_TIME); 
  /* scale factor to normalize correlation functions */
  if (bandwidth < 50.0) 
    bw=50.0; /* correct scaling for narrow-band use */
  else
    bw=bandwidth;

  scale = crate/bw;
  if (wapp_level==9) scale/=16.0; /* 9-level sampling */
  if (wapp_sum) scale/=2.0;  /* summed IFs (search mode) */
  scale*=pow(2.0,(double)wapp_lagtrunc); /* needed for truncation modes */

  /* now define a number of working arrays to store lags and spectra */
  block = (float *) malloc(nlags * sizeof(float));
  cblock = (unsigned char *) malloc(nlags * sizeof(unsigned char));
  sblock = (unsigned short *) malloc(nlags * sizeof(unsigned short));

  /* if the file is ALFA data --- do the demultiplexing to two files */
  if (wapp_isalfa) {

    angle_split(src_raj,&rah,&ram,&ras);
    rahr=(double)rah+(double)ram/60.0+(double)ras/3600.0;
    angle_split(src_dej,&ded,&dem,&des);
    if (ded>0)
      dede=(double)ded+(double)dem/60.0+(double)des/3600.0;
    else
      dede=(double)ded-(double)dem/60.0-(double)des/3600.0;
    /* calculate local sidereal time in hours */
    lmst=slaGmst(tstart)*12.0/4.0/atan(1.0)-4.4502051459439667;
    if (lmst<0.0) lmst+=24.0;
    slaDjcal(5,tstart,iymdf,&stat);
    slaCaldj(iymdf[0],1,1,&jan1,&stat);
    days=tstart-jan1+1.0;
    epoch=(double)iymdf[0]+days/365.25;
    utsecs=86400*(tstart-floor(tstart));

    pixel[0]=(wapp_number-1)*2;
    pixel[1]=pixel[0]+1;
    puts("opening output files for demultiplexed ALFA data...");
    for (i=0; i<2; i++) {
      if (alfa_raj[pixel[i]] == 0.0) {
      alfa_position(rahr,dede,lmst,epoch,alfa_ang,0.0,0.0,pixel[i],&pra,&pdec);
      src_raj=h2hms(pra);
      src_dej=deg2dms(pdec);
      } else {
      src_raj=h2hms(alfa_raj[pixel[i]]);
      src_dej=deg2dms(alfa_dej[pixel[i]]);
      }
      sprintf(outfile,"%s_%.0f_%05d_%04d_%s_%d.fil",
	    project,floor(tstart),utsecs,
	    scan_number,source_name,pixel[i]);
      alfa[i]=open_file(outfile,"wb");
      puts(outfile);
      filterbank_header(alfa[i]);
    }
    beam=0;
  }

  if (headerfile) {
    /* write output ASCII header file */
    fpou=open_file("head","w");  
    fprintf(fpou,"Original WAPP file: %s\n",inpfile);
    fprintf(fpou,"Sample time (us): %f\n",tsamp_us);
    fprintf(fpou,"Observation time (s): %f\n",wapp_obstime);
    fprintf(fpou,"Time stamp (MJD): %18.12f\n",tstart);
    fprintf(fpou,"Number of samples/record: %d\n",512);
    fprintf(fpou,"Center freq (MHz): %f\n",fch1+(float)nlags*foff/2.0);
    fprintf(fpou,"Channel band (kHz): %f\n",bandwidth*1000.0/nlags);
    fprintf(fpou,"Number of channels/record: %d\n",nlags);
    fprintf(fpou,"Nifs: %d\n",ifnum);
    fprintf(fpou,"RA (J2000): %f\n",src_raj);
    fprintf(fpou,"DEC (J2000):  %f\n",src_dej);
    fprintf(fpou,"Gal l: %.4f\n",srcl);
    fprintf(fpou,"Gal b: %.4f\n",srcb); 
    fprintf(fpou,"Name: %s\n",source_name);
    fprintf(fpou,"Lagformat: %d\n",wapp_lagformat);
    fprintf(fpou,"Sum: %d\n",wapp_sum);
    fprintf(fpou,"Level: %d\n",wapp_level);
    fprintf(fpou,"AZ at start: %f\n",az_start);
    fprintf(fpou,"ZA at start: %f\n",za_start);
    fprintf(fpou,"AST at start: %f\n",ast0);
    fprintf(fpou,"LST at start: %f\n",lst0);
    fprintf(fpou,"Project ID: %s\n",project);
    fprintf(fpou,"Observers: %s\n",culprits);
    filesize=sizeof_file(inpfile);
    fprintf(fpou,"File size (bytes): %d\n",filesize);
    headersize=wapp_header_size+wapp_incfile_length;
    fprintf(fpou,"Data size (bytes): %d\n",filesize-headersize);
    fprintf(fpou,"Number of samples: %d\n",nsamples(inpfile,headersize,nbits,nifs,nchans));
    fclose(fpou);
  }

  /* initialise various counters and flags */
  opened=idump=i=j=0; 

  /* main loop reading data from infile until no more left to read */
  while( (stat=read(wapp_file,dump,rec_size)) == rec_size) {

    /* calculate elapsed time and determine whether we process this record */
    realtime += (float) tsamp;
    if ( (doit=process(realtime,start_time,final_time)) == -1) break;

    if (doit) {

      /* set ALFA beam output if necessary */
      if (wapp_isalfa) {
	output=alfa[beam];       /* set output file for this loop */
	beam=!(beam);            /* flip file for next iteration */
      }

      /* clear zerolag and blocksum arrays */
      zerolag=0.0;
      for (j=0; j<nlags; j++) block[j]=0.0;

      /* loop over the IFs */
      for (i=0; i<ifnum; i++) {
	if (ifstream[i]=='Y') {
	  if (zerolagdump) {
	    /* only interested in the zero lag term for each IF */
	    switch (nbits) {
	    case 8:
	      zuc = *(((unsigned char *)dump)+i*nlags);
	      zerolag+=zuc;
	      break;
	    case 16:
	      zus = *(((unsigned short *)dump)+i*nlags);
	      if (swap_bytes) swap_short(&zus); 
	      zerolag+=zus;
	      break;
	    case 32:
	      zul = *(((unsigned int *)dump)+i*nlags);
	      if (swap_bytes) swap_int(&zul); 
	      zerolag+=zul;
	      break;
	    }
	    /* write out the data checking IF number for summed mode */
	    if ( (sumifs && (i==1)) || (!sumifs) ) {
	      if (obits==32) {
		if (swapout) swap_float(&zerolag);
		fwrite(&zerolag,sizeof(float),1,output);
	      } else {
		sprintf(message,"cannot write %d bits in zerolag mode",obits);
		error_message(message);
	      }
	    }
	  } else {
	    /* fill lag array with scaled CFs */
	    for (j=0; j<nlags; j++) {
	      switch (nbits) {
	      case 8:
		zuc = *(((unsigned char *)dump)+j+i*nlags);
		lag[j] = scale * (double) zuc - 1.0;
		break;
	      case 16:
		zus = *(((unsigned short *)dump)+j+i*nlags);
		if (swap_bytes) swap_short(&zus);
		lag[j] = scale * (double) zus - 1.0;
		break;
	      case 32:
		zul = *(((unsigned int  *)dump)+j+i*nlags);
		if (swap_bytes) swap_int(&zul);
		lag[j] = scale * (double) zul - 1.0;
		break;
	      }
	    }
	    /* calculate power and correct for finite level quantization */
	    power = inv_cerf(lag[0]);
	    power = 0.1872721836/power/power;
	    if (i<2) {
	      if (do_vanvleck) { 
		if (wapp_level==3) {
		  /* apply standard 3-level van vleck correction */
		  vanvleck3lev(lag,nlags);
		} else if (wapp_level==9) {
		  /* apply 9-level van vleck correction */
		  vanvleck9lev(lag,nlags);
		}
	      }
	    }

	    if (compute_spectra) {
	      /* form windowed even ACF in array */
	      for(j=1; j<nlags; j++) {
		acf[j]=window[j]*lag[j]*power;
		acf[two_nlags-j]=acf[j];
	      }   
	      acf[nlags]=0.0;
	      acf[0]=lag[0]*power; 
	      /* FFT the ACF (which is real and even) -> real and even FFT */
#ifdef FFTW
	      fftw_execute(fftplan);
#endif
#ifndef FFTW
	      rfft(two_nlags,acf,lag);
#endif
	      /* if the band needs to be flipped --- do it here */
	      if (wapp_flip) {
		/* use acf as temporary array */
		for (j=0;j<nlags;j++) acf[j]=lag[j]; 
		k=nlags-1;
		for (j=0;j<nlags;j++) {
		  lag[k]=acf[j];
		  k--;
		}
	      }
  	      /* add lags to block array */
	      for (j=0; j<nlags; j++) block[j]+=lag[j];
	    } else {			
	      /* just copy correlation functions into block */
	      for (j=0; j<nlags; j++) block[j]=lag[j];	
	    }
	    /* write out data block checking IF number for summed mode */
	    if ( (sumifs && (i==1)) || (!sumifs) ) {
	      if (obits==32) {
		if (swapout) for (j=0; j<nlags; j++) swap_float(&block[j]);
		fwrite(block,sizeof(float),nlags,output);
	      } else if (obits==16) {
		float2short(block,nlags,smin,smax,sblock);
		if (swapout) for (j=0; j<nlags; j++) swap_short(&sblock[j]);
		fwrite(sblock,sizeof(unsigned short),nlags,output);
	      } else if (obits==8) {
		float2char(block,nlags,smin,smax,cblock);
		fwrite(cblock,sizeof(unsigned char),nlags,output);
	      } else if (obits==4) {
		float2four(block,nlags,smin,smax,cblock);
		fwrite(cblock,sizeof(unsigned char),nlags/2,output);
	      } else {
		sprintf(message,"cannot write %d bits in wapp2fb",obits);
		error_message(message);
	      }
	    }
	  } /* end of zerolagdump if */

	  if (!sumifs) { /* reset block and zerolag if not summing */
	    zerolag=0.0;
	    for (j=0; j<nlags; j++) block[j]=0.0;
	  }
	}          /* end of IFstream if */
      }            /* end of loop over IFs */
    }              /* end of processing if */

    /* increment dump counter and update logfile every 512 dumps */
    idump++;
    if (idump%512 == 0) {
      if (!opened) {
	/* open up logfile */
	open_log("filterbank.monitor");
	opened=1;
      }
      sprintf(message,"time:%.1fs",realtime);
      update_log(message);
    }
  }                /* end of main read loop*/

    
  /* job done - free up remaining arrays */
  free(dump);free(block);free(sblock);free(cblock);free(window);
#ifdef FFTW
  fftw_destroy_plan(fftplan);
  fftw_free(acf); fftw_free(lag);
#endif
#ifndef FFTW
  free(acf); free(lag);
#endif
}
Example #5
0
main(int argc, char** argv)
{
  int dum_int, iread, iw, indx, ITStart, namelen, HdrSize,
    NTOT, NSkip, NPtsToRead, SkiByte, log2NTOT, NDM,
    NOPFiles, MAX, NTSampInRead, i, NSampInRead, BytePerFrame,
    NByteInRead, ITOffset, NBitChan, *ibrev, NRead, IFiles, FOld,
    FNew, FSwitch, KeepTrack, NTReadOld, NTReadNew, NReadOld,
    NReadNew, OPFileSize, IOffset, SkipByte, oldper, newper ,NT_Files;

  long long TotalTrack;

  float  FMin, FMax, FCen, *Inbuf, *Outbuf, timecount;

  char  *filename, *filefull, *parfile;

  FILE  *fpar, *Fout[8192], *Fin;

  if (argc <= 1) 
    tree_help();
  else 
    tree_parms(argc, argv);

  filename   = (char *) calloc(120, sizeof(char));
  filefull   = (char *) calloc(120, sizeof(char));
  parfile    = (char *) calloc(120, sizeof(char));

  ITStart = 0; NT_Files = 0;

  /* To read a sample header from first time file */

  strcpy(filename, unfname);
  namelen    = strlen(filename);

  printf("First filename   : %s\n", filename);
  if ((fpar     = fopen(filename, "rb")) == NULL) {
    printf("ERROR opening file %s.\n", filename);
    exit(0);
  }
  HdrSize  = read_header(fpar);
  fclose(fpar);

  FMin     = fch1;
  FMax     = FMin + (float)(nchans - 1) * foff;
  FCen     = (FMin + FMax) / 2.0;

  printf("No. of frequency channels        : %d\n",nchans);
  printf("Beginning radio frequency        : %f MHz\n", FMin - (foff / 2.0));
  printf("Ending radio frequency           : %f MHz\n", FMax + (foff / 2.0));
  printf("Centre frq. of the whole band    : %f MHz\n", FCen);
  printf("\n");
  printf("input sampling interval       : %f sec\n", tsamp);

  NTOT = 0;
  strcpy(filename, unfname);
  namelen    = strlen(filename);
  if ((fpar       = fopen(filename, "rb")) == NULL) {
    printf("ERROR opening %s.\n", filename);
  }

  HdrSize    = read_header(fpar);
  fclose(fpar);

  NT_Files = (int)nsamples(filename, HdrSize, nbits, nifs, nchans);

  NTOT       += NT_Files;
  printf("File %5d   : %s with %d samples\n", i, filename, NT_Files);

  NSkip      = (int)(TSkip / tsamp);
  if (NSkip > NT_Files) {
    printf("Initial Skip-length longer than the first file!\n");
    exit(0);
  }

  NPtsToRead = (int)(TRead / tsamp);
  if (NPtsToRead==0) {
    TRead=NT_Files*tsamp;
    NPtsToRead=NT_Files;
  }
  if (NPtsToRead > NTOT) NPtsToRead = NTOT;
  SkipByte   = (NSkip * nbits * nchans / 8);

  printf("\n");
  printf("This data set contains %d number of samples\n", NTOT);
  printf("Number of bits per sample          : %d\n", nbits);
  printf("Time length to skip at the begin.  : %f\n", TSkip);
  printf("Bytes to skip at the beginning     : %d\n", SkipByte);
  printf("No. of time samples to skip        : %d\n", NSkip);
  printf("Time length to read after skipping : %f\n", TRead);
  printf("Samples to read after skipping     : %d\n", NPtsToRead);

  NTOT  -= NSkip;
  printf("Time samples after initial skip    : %d\n", NTOT);

  if (NPtsToRead > NTOT) {
    NPtsToRead  = NTOT;
    printf("Too many samples to read. Truncated to %d samples\n", NTOT);
  } else
    NTOT  = NPtsToRead;

  log2NTOT = (int)(log((double)NTOT) / log((double)2.0));
  NTOT     = (1 << log2NTOT);
  printf("After truncating to lower 2^n      : %d\n", NTOT);

  if (DMMin < 0) DMMin = 0;
  if (DMMax >= nchans) DMMax = (nchans - 1);
  if ((DMMin == 0) && (DMMax == 0)) {
    DMMin = 0; DMMax = (nchans - 1);
  } else if ((DMMin != 0) && (DMMax == 0)) 
    DMMax = (nchans - 1);
  printf("\n");
  printf("Minimum DM index = %d   Maximum = %d\n", DMMin, DMMax);
  DMMinv=(tsamp/8.3e3)*(double)(DMMin)*pow((FMax+FMin)/2.,3.)/fabs(FMax-FMin);
  DMMaxv=(tsamp/8.3e3)*(double)(DMMax)*pow((FMax+FMin)/2.,3.)/fabs(FMax-FMin);
  printf("Minimum DM value = %f   Maximum = %f\n", DMMinv,DMMaxv);
  NDM        = (DMMax - DMMin + 1);

  NOPFiles   = NDM;
  OPFileSize = sizeof(float) * NTOT;

  printf("\n");
  printf("Total number of output files    : %d\n", NOPFiles);
  printf("Size of each output file        : %d bytes\n", OPFileSize);

  for (i=0; i<NOPFiles; i++) {
    strcpy(filefull,unfname);
    sprintf(&filefull[strlen(filefull)-4], ".DM%.4d.tim", (i+DMMin));
    if((Fout[i] = fopen(filefull,"wb")) == NULL) {
      printf("ERROR opening output file %s.\n", filefull);
      exit(0);
    } else {
      nbands=1;
      nobits=32;
      refdm=(tsamp/8.3e3)*(double)(i+DMMin)*pow((FMax+FMin)/2.,3.)/
	fabs(FMax-FMin);
      output=Fout[i];
      dedisperse_header();
    }
  }
  printf("\n");

  /*
  strcpy(parfile, "\0"); strcpy(parfile, unfname);
  strncat(parfile, ".par", 4);
  fpar = fopen(parfile, "w");
  fprintf(fpar, "%s\n", unfname);
  fprintf(fpar, "%d  %d  %10.8f \n ", NTOT, nchans, tsamp);
  fprintf(fpar, "%d  %d  %d  %d\n", DMMin, DMMax, NDM, NOPFiles);
  fprintf(fpar, "%d\n", nchans);
  fprintf(fpar, "%f %f\n", FMin, FMax);
  fclose(fpar);
  */

  MAX          = (1 << 12);
  if (MAX > NTOT) MAX = NTOT;

  NTSampInRead = (MAX + (2 * nchans));
  NSampInRead  = (nchans * NTSampInRead);
  BytePerFrame = (nchans * nbits / 8);
  NByteInRead  = (NTSampInRead * BytePerFrame);

  ITOffset     = 2 * nchans;
  IOffset      = (-ITOffset * BytePerFrame);

  NBitChan = (int)(log((double)nchans) / 0.6931471);

  printf("Time samples to read in one read      : %d\n", NTSampInRead);
  printf("Bytes to read in one read             : %d bytes\n", NByteInRead);
  printf("Offset counter for each read          : %d time samples\n", IOffset);
  printf("\n");
  printf("No of bits to represent all channels  : %d\n", NBitChan);

  Inbuf    = (float *) calloc(NSampInRead, sizeof(float));
  Outbuf   = (float *) calloc(NSampInRead, sizeof(float));
  ibrev    = (int *)   calloc(nchans, sizeof(int));

  for (i=0; i<nchans; i++) {
    ibrev[i] = bitrev(i, NBitChan);
  }

  NRead    = (int)(NTOT / MAX);
  printf("Total number of reads in the run       : %d\n", NRead);

  IFiles   = 0;
  FOld     = 0;
  FNew     = 0;
  FSwitch  = 1;

  strcpy(filename, unfname);  namelen    = strlen(filename);
  printf("First filename   : %s\n", filename);
  if ((Fin = fopen(filename, "rb")) == NULL) {
    printf("ERROR opening file %s.\n", filename);
    exit(0);
  }
  HdrSize  = read_header(Fin);
  printf("Going into the main loop\n");

  fseek(Fin, SkipByte, SEEK_CUR);
  KeepTrack = (NT_Files - NSkip);
  TotalTrack = 0;

  newper   = 0.0;
  oldper   = newper;
  printf("\n\n");
  for (iread=0; iread<NRead; iread++) {

    newper = 100.0 * (((float)iread / (float)NRead));
    if (newper > oldper) {
      timecount = ((float)iread * NTSampInRead * tsamp);
      printf("\rProcessed : %3d%%    Current file : %s    Time from beg : %8.2f sec", 
	     (int)newper, filename, timecount); fflush(stdout);
      oldper = newper;
    }

    KeepTrack  -= NTSampInRead;
    TotalTrack += NTSampInRead;

    if (KeepTrack > 0) {
      read_block(Fin, nbits, Inbuf, NSampInRead);
      fseek(Fin, IOffset, SEEK_CUR);
      KeepTrack  += ITOffset;
      TotalTrack -= ITOffset;
    }

    if (KeepTrack > 0) FSwitch = 1;

    memset(&Outbuf[0], 0, (sizeof(float) * NSampInRead));

    AxisSwap(Inbuf, Outbuf, nchans, NTSampInRead);

    if (FlipSwitch == 0)  {
      printf("BEFORE..."); fflush(stdout);
      FlipBand(Outbuf, nchans, NTSampInRead);
      printf("AFTER!\n");
    }

    taylor_flt(Outbuf, NSampInRead, nchans);

    for (iw=DMMin; iw<=DMMax; iw++) {
      indx  = (ibrev[iw] * NTSampInRead);
      fwrite(&Outbuf[indx], sizeof(float), MAX, Fout[iw-DMMin]);
    }
  }

  newper    = 100.0 * ((float)iread / NRead);
  timecount = ((float)iread * NTSampInRead * tsamp);
  printf("\rProcessed : %3d%%    Current file : %s    Time from beg : %8.2f sec", 
	 (int)newper, filename, timecount); fflush(stdout);
  printf("\n\n");

  for (i=0; i<NOPFiles; i++) fclose(Fout[i]);

  exit(0);
}
Example #6
0
main(int argc, char *argv[]) 
{
	FILE *fileptr;
	char filename[80],*telescope,*backend,*datatype,message[80],unit[16];
	int i,j,year,month,day,check,rah,ram,ded,dem;
	double ras,des,frac,tobs;
	char sra[6],sde[6],decsign;
	int raw,uth,utm,uts;
	long long numsamps,datasize,headersize;

	int writeobsdbline;

	fileptr=stdin;
	strcpy(filename,"stdin");
	strcpy(rawdatafile,"stdin");
	pulsarcentric=barycentric=0;

	writeobsdbline=0;

	if (argc>1) {
		print_version(argv[0],argv[1]);
		if (help_required(argv[1])) {
			header_help();
			exit(0);
		} else if (file_exists(argv[1])) {
			strcpy(filename,argv[1]);
			fileptr=open_file(filename,"rb");
		} else if (!file_exists(argv[1]) && (strncmp(argv[1],"-",1) !=0)) {
			sprintf(message,"Data file: %s not found...\n",argv[1]);
			error_message(message);
			exit(1);
		}
	}


	if (!(headersize=read_header(fileptr))) {
		rewind(fileptr);
		if ((raw=typeof_inputdata(fileptr,filename))) {
			data_type=0;
			switch (raw) {
				case 1:
					headersize=32768;
					break;
				case 5:
					headersize=32768;
					break;
				case 3:
					headersize=wapp_header_size+wapp_incfile_length;
					break;
				default:
					break;
			}
		} else {
			error_message("could not read header parameters!");
			exit(1);
		}
	}

	/* attempt to find number of bytes of data and number of samples */
	if (!strings_equal(filename,"stdin")) {
		datasize=sizeof_file(filename)-headersize;
		numsamps=nsamples(filename,headersize,nbits,nifs,nchans);
	} else if (!strings_equal(rawdatafile,"stdin")) {
		datasize=sizeof_file(rawdatafile)-headersize;
		numsamps=nsamples(rawdatafile,headersize,nbits,nifs,nchans);
	} else {
		datasize=numsamps=0;
	}

	telescope=telescope_name(telescope_id);
	backend=backend_name(machine_id);
	datatype=data_category(data_type);

	if (argc>2) {
		check=1;
		i=2;
	} else if ((argc>1) && strings_equal(filename,"stdin")) {
		check=1;
		i=1;
	} else {
		check=0;
	}

	angle_split(src_raj,&rah,&ram,&ras);
	if (ras<10.0) {
		sprintf(sra,"0%.1f",ras);
	} else {
		sprintf(sra,"%.1f",ras);
	}

	angle_split(src_dej,&ded,&dem,&des);
	if (src_dej > 0.0) 
		decsign = '+';
	else 
		decsign = '-';
	if (des<10.0) {
		sprintf(sde,"0%.1f",des);
	} else {
		sprintf(sde,"%.1f",des);
	}

	cal(tstart,&year,&month,&day);

	if (check) {
		/* check command-line parameters */ 
		while (i<argc) {
			if (strings_equal(argv[i],"-telescope")) {
				puts(telescope);
			} else if (strings_equal(argv[i],"-obsdb")) {
				writeobsdbline=1;
			} else if (strings_equal(argv[i],"-machine")) {
				puts(backend);
			} else if (strings_equal(argv[i],"-source_name")) {
				puts(source_name);
			} else if (strings_equal(argv[i],"-scan_number")) {
				puti(scan_number);
			} else if (strings_equal(argv[i],"-datatype")) {
				puts(datatype);
			} else if (strings_equal(argv[i],"-frame")) {
				if (pulsarcentric) 
					puts("pulsarcentric");
				else if (barycentric) 
					puts("barycentric");
				else 
					puts("topocentric");
			} else if (strings_equal(argv[i],"-barycentric")) {
				puti(barycentric);
			} else if (strings_equal(argv[i],"-pulsarcentric")) {
				puti(pulsarcentric);
			} else if (strings_equal(argv[i],"-data_type")) {
				puti(data_type);
			} else if (strings_equal(argv[i],"-headersize")) {
				printf("%d\n",headersize);
			} else if (strings_equal(argv[i],"-datasize")) {
				printf("%lld\n",datasize);
			} else if (strings_equal(argv[i],"-nsamples")) {
				printf("%lld\n",numsamps);
			} else if (strings_equal(argv[i],"-tobs")) {
				printf("%f\n",(double)numsamps*tsamp);
			} else if (strings_equal(argv[i],"-az_start")) {
				printf("%f\n",az_start);
			} else if (strings_equal(argv[i],"-za_start")) {
				printf("%f\n",za_start);
			} else if (strings_equal(argv[i],"-fch1")) {
				printf("%.3f\n",fch1);
			} else if (strings_equal(argv[i],"-bandwidth")) {
				printf("%.3f\n",fabs(foff)*(double)nchans);
			} else if (strings_equal(argv[i],"-fmid")) {
				printf("%.3f\n",fch1+foff*nchans/2);
			} else if (strings_equal(argv[i],"-foff")) {
				printf("%f\n",foff);
			} else if (strings_equal(argv[i],"-refdm")||strings_equal(argv[i],"-dm")) {
				printf("%f\n",refdm);
			} else if (strings_equal(argv[i],"-nchans")) {
				printf("%d\n",nchans);
			} else if (strings_equal(argv[i],"-tstart")) {
				printf("%.12f\n",tstart);
			} else if (strings_equal(argv[i],"-frequencies")) {
				for (j=0; j<nchans; j++) printf("%f\n",frequency_table[j]);
			} else if (strings_equal(argv[i],"-mjd")) {
				printf("%d\n",(int)floor(tstart));
			} else if (strings_equal(argv[i],"-date")) {
				printf("%4d/%02d/%02d\n",year,month,day);
			} else if (strings_equal(argv[i],"-utstart")) {
				frac=tstart-floor(tstart);
				uth=(int) floor(24.0*frac);
				frac-=(double)uth/24.0;
				utm=(int) floor(1440.0*frac);
				frac-=(double)utm/1440.0;
				uts=(int) floor(86400.0*frac);
				printf("%02d:%02d:%02d\n",uth,utm,uts);
			} else if (strings_equal(argv[i],"-tsamp")) {
				printf("%.5f\n",tsamp*1.0e6);
			} else if (strings_equal(argv[i],"-nbits")) {
				printf("%d\n",nbits);
			} else if (strings_equal(argv[i],"-nifs")) {
				printf("%d\n",nifs);
			} else if (strings_equal(argv[i],"-src_raj")) {
				printf("%02d:%02d:%s\n",rah,ram,sra);
			} else if (strings_equal(argv[i],"-src_dej")) {
				printf("%c%02d:%02d:%s\n",decsign,abs(ded),dem,sde);
			}else if (strings_equal(argv[i],"-ra_deg")) {
				printf("%f\n",rah*15+ram/4.0+ras/240.0);
			} else if (strings_equal(argv[i],"-dec_deg")) {
				printf("%c%f\n",decsign,abs(ded)+dem/60.0+des/3600.0);
			} else {
				header_help();
				sprintf(message,"unknown argument (%s) passed to header",argv[i]);
				error_message(message);
			}
			i++;
		}
		/* if we are doing a obs line do this... otherwise continue normaly
		 * MK 2006, for the MM survey bookkeeping.
		 */


		if(writeobsdbline){
			printf("%s ",source_name);
                        printf("%3.3lf %3.3lf ",gal_l,gal_b);
			printf("%s ",filename);
                        printf("%5.3lf %6.6lf ",header_tobs,tstart);
			printf("%6.3lf %6.3lf ",src_raj,src_dej);
                        printf("%5.3lf %3.3lf ",raw_fch1,raw_foff);
			printf("%d %d %5.3lf",nbeams,nchans,tsamp*1000);
			printf("\n");

		}

		exit(0);
	}


	/* no command-line flags were specified - display full output */

	printf("Data file                        : %s\n",filename);
	printf("Header size (bytes)              : %d\n",headersize);
	if (datasize) 
		printf("Data size (bytes)                : %lld\n",datasize);
	if (pulsarcentric) 
		printf("Data type                        : %s (pulsarcentric)\n",datatype);
	else if (barycentric) 
		printf("Data type                        : %s (barycentric)\n",datatype);
	else
		printf("Data type                        : %s (topocentric)\n",datatype);

	printf("Telescope                        : %s\n",telescope);
	printf("Datataking Machine               : %s\n",backend);


	if (!strings_equal(source_name,"")) 
		printf("Source Name                      : %s\n",source_name);
	if (src_raj != 0.0) 
		printf("Source RA (J2000)                : %02d:%02d:%s\n",rah,ram,sra);
	if (src_dej != 0.0)
		printf("Source DEC (J2000)               : %c%02d:%02d:%s\n",
				decsign,abs(ded),dem,sde);
	if ((az_start != 0.0) && (az_start != -1.0))
		printf("Start AZ (deg)                   : %f\n",az_start);
	if ((za_start != 0.0) && (za_start != -1.0))
		printf("Start ZA (deg)                   : %f\n",za_start);

	switch (data_type) {
		case 0:
		case 1:
			if ((fch1==0.0) && (foff==0.0)) {
				printf("Highest frequency channel (MHz)  : %f\n",
						frequency_table[0]);
				printf("Lowest frequency channel  (MHz)  : %f\n",
						frequency_table[nchans-1]);
			} else {
				printf("Frequency of channel 1 (MHz)     : %f\n",fch1);
				printf("Channel bandwidth      (MHz)     : %f\n",foff); 
				printf("Number of channels               : %d\n",nchans);
				printf("Number of beams                  : %d\n",nbeams);
				printf("Beam number                      : %d\n",ibeam); 
			}
			break;
		case 2:
			nchans=1;
			printf("Reference DM (pc/cc)             : %f\n",refdm);
			printf("Reference frequency    (MHz)     : %f\n",fch1);
			break;
		case 3:
			if (refdm > 0.0)
				printf("Reference DM (pc/cc)             : %f\n",refdm);
			printf("Frequency of channel 1 (MHz)     : %f\n",fch1);
			printf("Channel bandwidth      (MHz)     : %f\n",foff); 
			printf("Number of channels               : %d\n",nchans);
			printf("Number of phase bins             : %d\n",nbins);
			printf("Folding period  (s)              : %.12f\n",period);
			break;
		case 6:
			printf("Reference DM (pc/cc)             : %f\n",refdm);
			printf("Frequency of channel 1 (MHz)     : %f\n",fch1);
			printf("Channel bandwidth      (MHz)     : %f\n",foff); 
			printf("Number of channels               : %d\n",nchans);
			break;
	}

	printf("Time stamp of first sample (MJD) : %.12f\n",tstart);
	printf("Gregorian date (YYYY/MM/DD)      : %4d/%02d/%02d\n",year,month,day);

	if (data_type != 3) 
		printf("Sample time (us)                 : %.5f\n",tsamp*1.0e6); 

	if (datasize && data_type != 3) {
		printf("Number of samples                : %lld\n",numsamps);
		tobs=(double)numsamps*tsamp;
		strcpy(unit,"(seconds)   ");
		if (tobs>60.0) {
			tobs/=60.0;
			strcpy(unit,"(minutes)   ");
			if (tobs>60.0) {
				tobs/=60.0;
				strcpy(unit,"(hours)     ");
				if (tobs>24.0) {
					tobs/=24.0;
					strcpy(unit,"(days)      ");
				}
			}
		}
		printf("Observation length %s  : %.1f\n",unit,tobs);
	}
	printf("Number of bits per sample        : %d\n",nbits);
	printf("Number of IFs                    : %d\n",nifs);
	exit(0);
}
Example #7
0
int main (int argc, char *argv[]) 
{
  int ntimglobal=0;  // number of time samples in original
  int ngulp_original=0;       // number of time samples to look at at once
  int nskipstart=0;       // number skipped at start
  int nrejects; //ZAPPER
  int zapswitch = 0; //ZAPPER  
  double tsamp_orig=0;

  //gsearch setup & defaults
  float Gsigmacut=6.0;
  float delta, tstart;
  vector<Gpulse> * Giant = new vector<Gpulse>[MAXFILES];
  bool Gsearched=false;

  int i,ntim,headersize[MAXFILES],noff=0,gulp;
  float *time_series[MAXFILES],sum=0.0,sumsq=0.0,mean,meansq,sigma;
  int MAXMARKERS = 1024;
  int nfiles = 0;
  FILE *inputfile[MAXFILES];
  char filename[MAXFILES][256];
  int spectra=0;
  int powerspectra=0;
  double dmoffirstfile;
  char *killfile;
  bool dokill=false;
  bool ssigned=true;
  bool fsigned=false;
  int topfold=-1;
  int topgiant=-1;
  int toppeak=-1; //?!? sarah added this 
  bool askdevice=false;
  char devicename[200];

  if (argc<2 || help_required(argv[1])) {
      helpmenu();
//    fprintf(stderr,"Usage: giant filenames\n\t(e.g.>>  giant *.tim)\n\n\t-s  N\tskip N samples\n\t-n  N\tread N samples\n\t-S read spectra instead of amplitudes\n-i interpret signed chars as unsigned\n\t-z make a zap list of bad time samples\n");
      exit(0);
  }
  print_version(argv[0],argv[1]);
  i=1;
  while (i<argc) {
    if (file_exists(argv[i]))          {
      inputfile[nfiles]=open_file(argv[i],"r");
      strcpy(filename[nfiles],argv[i]);
      nfiles++;
    }
    if (strings_equal(argv[i],"-s"))       sscanf(argv[++i],"%d",&nskipstart);
    if (strings_equal(argv[i],"-S"))       spectra=1;
    if (strings_equal(argv[i],"-i"))       ssigned=false;
    if (strings_equal(argv[i],"-f"))      fsigned=true;
    if (strings_equal(argv[i],"-n"))       sscanf(argv[++i],"%d",&ngulp_original);
    if (strings_equal(argv[i],"-c"))       sscanf(argv[++i],"%f",&Gsigmacut);
    if (strings_equal(argv[i],"-z"))       zapswitch=1;
    if (strings_equal(argv[i],"-g"))       {askdevice=true;sscanf(argv[++i],"%s",&devicename);}
    if (strings_equal(argv[i],"-k"))       {killfile=(char*)malloc(strlen(argv[++i])+1); strcpy(killfile,argv[i]);dokill=true;}
    if (nfiles>MAXFILES) error_message("too many open files");
    i++;
  }


  int ntimglobal_smallest=0, nsamp;
  for (i=0; i<nfiles; i++) {

    if (spectra){
      int npf; 
      double rate;
      time_series[i]=Creadspec(filename[i],&npf,&rate);
      tsamp = 1.0/(rate);
      //normalise(npf,time_series[i]);
      nsamp = ntimglobal = ntimglobal_smallest = npf;
    }
    else
    {
    if ((headersize[i]=read_header(inputfile[i]))) {
	    if (! fsigned){
		    if (isign > 0) {
			    ssigned=false;
			    fprintf(stderr,"using signed header variable to set UNSIGNED\n");
		    }
		    if (isign < 0) {
			    ssigned=true;
			    fprintf(stderr,"using signed header variable to set SIGNED\n");
		    }
	    }
      if (i==0) dmoffirstfile = refdm;
      if (nbits!=8 && nbits!=32)
	    error_message("giant currently only works for 8- or 32-bit data");

      nsamp = nsamples(filename[i],headersize[i],nbits,nifs,nchans);
      if (i == 0) {
	ntimglobal_smallest=nsamp;
      } else {
	ntimglobal= nsamp;
	if (ntimglobal < ntimglobal_smallest) ntimglobal_smallest = ntimglobal;
      }
      
      // Space for data (time_series)
      time_series[i]=(float *) malloc((nsamp+2)*sizeof(float));
      if (time_series[i]==NULL){
	fprintf(stderr,"Error mallocing %d floats of %d size\n",nsamp,
		sizeof(float));
	exit(-1);
      }
      tsamp_orig = tsamp;
      
      // Skip data
      fprintf(stderr,"Skipping %d bytes\n",nskipstart*nbits/8);
      fseek(inputfile[i],nskipstart*nbits/8,SEEK_CUR);
      
    } // each file
    } // spectra or not
  }  // for (i...)
  puti(ntimglobal_smallest);
  if (ngulp_original==0) ngulp_original=ntimglobal_smallest;


// ****** SAM'S ZAP SWITCH ******
// Sam Bates 2009
// Integrated into new giant by SBS
// Switch to make a .killtchan file for time samples > 3.5 sigma
// SARAHZAP tag means addition was added later by Sarah
// ******************************
  int ngulp=ngulp_original;
//  int nrejects_max=ngulp_original/100;
  int * mown = new int[ngulp_original];
  int nstart=0;
  if (zapswitch){
    float dummy;
    int NActuallyRead;
    char *buffer;
    buffer = new char[ngulp*nbits/8];
    for (i=0; i<nfiles; i++){
      NActuallyRead = fread(buffer,nbits/8,ngulp,inputfile[i]);
      if (nbits==32){
	memcpy(time_series[i],buffer,sizeof(float)*ngulp);
      } else {
	for (int j=0;j<NActuallyRead;j++){
	  if (ssigned) time_series[i][j]=(float)buffer[j];
	  if (!ssigned) time_series[i][j]=(float)((unsigned char)buffer[j]);
	}
      }
      puti(ngulp);
      find_baseline(ngulp,time_series[i],10.0/tsamp,5.0);
      mowlawn(ngulp,time_series[i],5,256);
    }
    printf("%f\n",dummy);
    printf("Bad time samples found...\n");
    exit(0);
  }


  int pgpID;
  if (askdevice){
      pgpID = cpgbeg(0,devicename,1,1);
  } else {
      pgpID = cpgbeg(0,"/xs",1,1);
  }
  cpgsch(0.5);
  cpgtext(0.6,0.0,"Press 'h' over the main window for help and full options list.");
  cpgsch(1.0);
  /* create the dialog */
  dialog * d = new dialog();

  /* add the "action" buttons */
  int QUIT         = d->addbutton(0.02,0.95,"Quit");
  int POWER        = d->addbutton(0.07,0.85,"POWER");
  int SMHRM        = d->addbutton(0.075,0.80,"SMHRM");
  int FFT          = d->addbutton(0.02,0.85,"FFT");
  int PLOT         = d->addbutton(0.02,0.80,"Plot");
  int NEXT         = d->addbutton(0.02,0.75,"Next");
  int ZAPPEAK      = d->addbutton(0.075,0.75,"ZapPeak");
  int RESET        = d->addbutton(0.02,0.70,"Reset");
  int GLOBALRESET  = d->addbutton(0.02,0.65,"Global Reset");
  int HALVEPLOT    = d->addbutton(0.02,0.60,"Halve Plot");
  int BASELINE     = d->addbutton(0.02,0.50,"Baseline");
  int ZAPCOMMON    = d->addbutton(0.02,0.45,"Zap Common");
  int SUBTRACTMEAN = d->addbutton(0.02,0.40,"ZAP Mean");
  int BSCRUNCH     = d->addbutton(0.02,0.35,"Bscrunch");
  int NORMALISE    = d->addbutton(0.02,0.30,"Normalise"); 
  int HISTOGRAM    = d->addbutton(0.02,0.25,"Histogram"); 
  int GSEARCH      = d->addbutton(0.02,0.20,"Find Giants");
  int MOWLAWN      = d->addbutton(0.08,0.70,"LAWN");
  int SEEFIL       = d->addbutton(0.02,0.15,"View Band");
  int FWRITE       = d->addbutton(0.02,0.05,"Write File");
 

  /* add the plot regions */
  d->addplotregion(0.2,0.99,0.98,0.99);
  float deltay = 0.9/(float)nfiles;
  for (i=0; i<nfiles; i++) 
      d->addplotregion(0.2,0.99,0.95-deltay*(float)(i+1),0.95-deltay*(float)i);

  d->draw();

  float x,y;
  char ans;
  int button=-1; int plotno=-1;
  int NPIXELS = 1024;
  float * xaxis = new float[NPIXELS];
  float * ymaxes = new float[NPIXELS];
  float * ymins = new float[NPIXELS];

  int scrunch=1;
  int nmarkers=0;
  int * markers= new int[MAXMARKERS];
  int nfileptr=nskipstart;
  int nplot=ngulp_original;
  nstart=0;  //COMMENTED IN ZAPPER VERSION: MAY CAUSE CONFLICTS IN THIS VER.
  ngulp=ngulp_original;  //COMMENTED IN ZAPPER VERSION: MAY CAUSE CONFLICTS IN THIS VER.
  double trialperiod;
  int doperiod=-1;
  double xperiod;

  bool zoneplot=false;
  int ngates=0;
  float xgate=0.0;



  button=NEXT;
  if (spectra) button = PLOT;
  while (button!=QUIT){
    // Plot the zone
    // Entire file is white
    if (button!=NEXT)button=d->manage(&x,&y,&ans,&plotno);
    if (ans=='h'){
	buttonexplain();
	continue;
    }
//    printf("manage x %f y %f plotno %d\n",x,y,plotno);
    if (button==BASELINE) {
	for (i=0; i<nfiles; i++){
	    find_baseline(ngulp,time_series[i],10.0/tsamp,5.0);
	}
	button = PLOT;
	zoneplot=false;
        plotno = -1;
    }
    if (button==FWRITE) {
      // reread first header and close it. Sets globals.
      fclose(inputfile[0]);
      inputfile[0]=open_file(argv[1],"r");
      headersize[0]=read_header(inputfile[0]);
      output = open_file("giant.tim","w");
      nobits=32;
      nbands=1;
      dedisperse_header();
      fprintf(stderr,"Opened file, writing data\n");
      fwrite(time_series[0],sizeof(float),ngulp,output);
      fclose(output);
      button = -1;
      zoneplot=false;
      plotno =-1;
    }
    if (button==BSCRUNCH) {
	for (i=0; i<nfiles; i++){
	    bscrunch(ngulp,time_series[i]);
	}
	tsamp*=2;
	scrunch*=2;
      	ngulp/=2;
	nplot/=2;
	button = PLOT;
	zoneplot=false;
	Gsearched=false;
        plotno = -1;
    }
    if (button==FFT) {
	for (i=0; i<nfiles; i++){
	  ngulp = ngulp_original;
	  find_fft(&ngulp,time_series[i]);
	// Zap DC spike
	  time_series[i][0]=0.0;
	  time_series[i][1]=0.0;
	}
	spectra = 1;
	nplot = ngulp;
	button = PLOT;
	Gsearched=false;
        plotno = -1;
    }
    if (button==POWER) {
	for (i=0; i<nfiles; i++){
	  find_formspec(ngulp,time_series[i]);
	}
	ngulp/=2;
	powerspectra = 1;
	nplot = ngulp;
	button = PLOT;
        plotno = -1;
    }
    if (button==SMHRM) {
        nfiles = 6;
	for (i=1; i<nfiles; i++){
	  time_series[i]=(float *) malloc((ngulp+2)*sizeof(float));
	  if (time_series[i]==NULL){
	    fprintf(stderr,"Error allocating memory\n");
	    exit(-1);
	  }
	}
	for (i=1;i<nfiles;i++) memcpy(time_series[i],time_series[0],
				      (ngulp+2)*sizeof(float));
	d->nplotregion=1;
        float deltay = 0.9/(float)nfiles;
        for (i=0; i<nfiles; i++) 
          d->addplotregion(0.2,0.99,0.95-deltay*(float)(i+1),
			   0.95-deltay*(float)i);
	cpgeras();
	d->draw();

	float * workspace = new float[ngulp];

	// Set up space for data, now actually sumhrm
	int one=1;
			newoldsumhrm_(&time_series[0][1],workspace,&ngulp,&one,
	//		newoldsumhrm_(&time_series[0][0],workspace,&ngulp,&one,
		   time_series[1],time_series[2],time_series[3],
		   time_series[4],time_series[5]);
		/*	newnewsumhrm_(time_series[0],&ngulp,&one,
		   time_series[1],time_series[2],time_series[3],
		   time_series[4],time_series[5]);*/
		for (int iff=2;iff<6;iff++){
		  for (int i=0;i<ngulp;i++){
		    time_series[iff][i]/=sqrt(pow(2.0,(float)(iff-1)));
		  }
		}
	delete [] workspace;
	button = PLOT;
        plotno = -1;
    }
    if (button==NORMALISE) {
	for (i=0; i<nfiles; i++){
	  normalise(ngulp,time_series[i],5.0);
	}
	button = PLOT;
	Gsearched=false;
        plotno = -1;
    }
    if (button==HISTOGRAM) {
      float pdfs[nfiles][MAXSIGMA];
      //  create pdfs for each beam
      for (int i=0;i<nfiles;i++)
	formpdf(pdfs[i],MAXSIGMA,ngulp,time_series[i]);

      for (int i=0;i<nfiles;i++){
	for (int j=0;j<MAXSIGMA; j++){
	  fprintf(stderr, "pdfs[%d][%2d]=%8.0f %f \%\n", i, j+1, pdfs[i][j], 100*pdfs[i][j]/ngulp);
	}
      }
        button = PLOT;
        plotno = -1;
    }
    if (button==HALVEPLOT) {
	nplot/=2;
	button = PLOT;
	zoneplot=true;
	Gsearched=false;
        plotno = -1;
    }
    if (button==GLOBALRESET) {
      plotno = -1;
      nstart = 0;
      scrunch=1;
      tsamp = tsamp_orig;
      nplot=ngulp_original;
      ngulp=ngulp_original;
      button=PLOT;
      // Skip to end of skipped data
      for (i=0; i<nfiles; i++){
	fseek(inputfile[i],-(nfileptr-nskipstart)*nbits/8,SEEK_CUR);
	Giant[i].clear();
      }
      nfileptr=nskipstart;
      zoneplot=false;
      Gsearched=false;
      doperiod=-1;
      button=NEXT;
    }
    if (button==SUBTRACTMEAN && nfiles>1) {
      plotno = -1;
      nstart = 0;
      nplot=ngulp_original;
      ngulp=ngulp_original;
      button=PLOT;
      // Skip to end of skipped data
	for (int jj=0;jj<ngulp;jj++){
	  float sum;
	  sum=0.0;
	  for (i=1;i<nfiles;i++){
	    sum+=time_series[i][jj];
	  }
	  time_series[0][jj]-=sum/(float(nfiles-1));
	}
	Gsearched=false;
    }
    if (button==ZAPCOMMON && nfiles>1) {
      plotno = -1;
      nstart = 0;
      nplot=ngulp_original;
      ngulp=ngulp_original;
      button=PLOT;
      float pdfs[nfiles][MAXSIGMA];
      //  create pdfs for each beam
      for (int i=0;i<nfiles;i++)
	formpdf(pdfs[i],MAXSIGMA,ngulp,time_series[i]);
      //  for each point in each beam, mask if improbable
      float thresh = 3.0;
      int nbeammax = 5;
      zap_improbables(pdfs,time_series,nfiles,ngulp,MAXSIGMA,thresh,nbeammax);
      // Skip to end of skipped data
      //for (int jj=0;jj<ngulp;jj++){
      //  float sum;
      //  sum=0.0;
      //  for (i=1;i<nfiles;i++){
      //    sum+=time_series[i][jj];
      //  }
      //  time_series[0][jj]-=sum/(float(nfiles-1));
      //}
      //Gsearched=false;
    }
    if (button==NEXT) {
      ngulp=ngulp_original;
      nstart=0;
      nplot=ngulp_original;
      // Read the data
      int NActuallyRead;
      //      unsigned char *buffer;
      char *buffer;
      buffer = new char[ngulp*nbits/8];
      //buffer = new char[ngulp*nbits/8];
      for (i=0; i<nfiles; i++) {
//	NActuallyRead = fread(time_series[i],sizeof(float),ngulp,inputfile[i]);
	NActuallyRead = fread(buffer,nbits/8,ngulp,inputfile[i]);
	if (nbits==32){
	  memcpy(time_series[i],buffer,sizeof(float)*ngulp);
	} else {
	    for (int j=0;j<NActuallyRead;j++){
	      if (ssigned) time_series[i][j]=(float)buffer[j];
	      if (!ssigned) time_series[i][j]=(float)((unsigned char)buffer[j]);
	    }
	}
	
	puti(ngulp);
	if (NActuallyRead!=ngulp){
	  fprintf(stderr,"Could not read %d floats from file\n",ngulp);
	  ngulp = NActuallyRead;
	}
	if(nfiles==1){
	  // Add fake pulsar here....
	  //	  for (int ii=0;ii<ngulp;ii++) time_series[i][ii]+= 10.0*pow(sin(float(ii*2.0*M_PI/60.0)),250.0);
	}
	//normalise(ngulp,time_series[i]);
      }
      nfileptr+=ngulp;
      button = PLOT;
      plotno= -1;
      zoneplot=true;
    }
    if (button==RESET) {
      button = plotno = -1;
      nstart=0;
      nplot=ngulp;
      button=PLOT;
      zoneplot=true;
      Gsearched=false;
      if (ans=='p'){
	doperiod=-1;
      }
    }
    if (plotno>0){
/*      if (ans=='p'){  // hit p on a plot to type in a period
	d->plotregions[plotno].reset();
	//plot the thing;
	fprintf(stderr,"Please enter a period in seconds: ");
	cin>>trialperiod;
	xperiod = x;
	doperiod=plotno;
	button=PLOT;
	}*/
      if (ans=='p'){  // hit p on a plot to type in a period
	d->plotregions[plotno].reset();
	//plot the thing;
	fprintf(stderr,"Please enter a period in seconds: ");
	cin>>trialperiod;
	xperiod = (double)x;
	doperiod=plotno;
	button=PLOT;
      }
      if (ans=='m'){  // subtract 0.0000005 seconds from period
	d->plotregions[plotno].reset();
	trialperiod-=0.0000005;
	fprintf(stderr,"Trial period is now %lf\n",trialperiod);
	doperiod=plotno;
	button=PLOT;
      }
      if (ans=='/'){  // add 0.0000005 seconds to period
	d->plotregions[plotno].reset();
	trialperiod+=0.0000005;
	fprintf(stderr,"Trial period is now %lf\n",trialperiod);
	doperiod=plotno;
	button=PLOT;
      }
      if (ans==','){  // subtract 0.000005 seconds from period
	d->plotregions[plotno].reset();
	trialperiod-=0.000005;
	fprintf(stderr,"Trial period is now %lf\n",trialperiod);
	doperiod=plotno;
	button=PLOT;
      }
      if (ans=='.'){  // add 0.000005 seconds to period
	d->plotregions[plotno].reset();
	trialperiod+=0.000005;
	fprintf(stderr,"Trial period is now %lf\n",trialperiod);
	doperiod=plotno;
	button=PLOT;
      }
      if (ans=='<'){  // subtract 0.001 seconds from period
	d->plotregions[plotno].reset();
	trialperiod-=0.001;
	fprintf(stderr,"Trial period is now %lf\n",trialperiod);
	doperiod=plotno;
	button=PLOT;
      }
      if (ans=='>'){  // add 0.001 seconds to period
	d->plotregions[plotno].reset();
	trialperiod+=0.001;
	fprintf(stderr,"Trial period is now %lf\n",trialperiod);
	doperiod=plotno;
	button=PLOT;
      }
      if (ans=='X'){  // right click two points on a plot to calculate and plot a period
	d->plotregions[plotno].reset();
	cpgsci(3);
	cpgmove(x,-1000);
	cpgdraw(x,1000);
	if (ngates==0){
	  xgate=x;
	  ngates++;
	} else {
	  min_means_min(&x,&xgate);
	  printf("Period from %f to %f is %f\n",x,xgate,xgate-x);  
	  doperiod=plotno;
	  xperiod = (double)x;
	  trialperiod=(double)(xgate-x);
	  ngates=0;
	  button=PLOT;
	}
      }
      if (ans=='D'){
	markers[nmarkers]=(int)(x/NPIXELS)*nplot+nstart+nfileptr-ngulp;
	nmarkers++;
	zoneplot=true;
      }
      if (ans=='A'){
	d->plotregions[plotno].reset();
	cpgsci(2);
	cpgmove(x,-1000);
	cpgdraw(x,1000);
	if (ngates==0){
	  xgate=x;
	  ngates++;
	} else {
	  min_means_min(&x,&xgate);
//	  printf("x %f xgate %f tstart %f\n",x,xgate,tstart);
	  nstart=(int)((x-tstart)/delta)+nstart;
	  nplot=(int)((xgate-x)/delta);
	  //if (nplot<NPIXELS) nplot=NPIXELS;
	  ngates=0;
	  button=PLOT;
	  zoneplot=true;
//	  printf("nplot %d nstart %d\n",nplot,nstart);
	}
      }
      if (ans=='z'){
	if (NPIXELS>nplot) {
	  nstart+=(int)x;
	}else
	nstart=(int)(x/(float)NPIXELS*nplot)+nstart;
	printf("nstart %d\n",nstart);
	nplot/=4;
	printf("nplot %d\n",nplot);
	nstart-=nplot/2;
	printf("nstart %d\n",nstart);
	//if (nplot<NPIXELS){nplot=NPIXELS;}
	button=PLOT;
	zoneplot=true;
      }
    }
Example #8
0
main (int argc, char *argv[])
{
  int i, nc, headersize, headerless=0;
  char string[80];

  /* set up default global variables */
  obits=headerless=naddc=naddt=nsamp=0;
  input=stdin;
  strcpy(inpfile,"stdin");
  output=stdout;
  strcpy(outfile,"stdout");

  if (argc > 1) {
    /* check command-line parameters */ 
    print_version(argv[0],argv[1]);
    i=1;
    while (i<argc) {
      if (strings_equal(argv[i],"-c")) {
	i++;
	naddc=atoi(argv[i]);
      } else if (strings_equal(argv[i],"-t")) {
	i++;
	naddt=atoi(argv[i]);
      } else if (strings_equal(argv[i],"-o")) {
	/* get and open file for output */
	output=fopen(argv[++i],"wb");
      } else if (strings_equal(argv[i],"-T")) {
	i++;
	nsamp=atoi(argv[i]);
      } else if (strings_equal(argv[i],"-n")) {
	i++;
	obits=atoi(argv[i]);
      } else if (strings_equal(argv[i],"-headerless")) {
	headerless=1;
      } else if (help_required(argv[1])) {
	decimate_help();
	exit(0);
      } else if (file_exists(argv[i])) {
	strcpy(inpfile,argv[i]);
	input=open_file(inpfile,"rb");
      } else {
	decimate_help();
	sprintf(string,"unknown argument (%s) passed to decimate",argv[i]);
	error_message(string);
      }
      i++;
    }
  }

  /* read in the header to establish what the input data are... */
  if ((headersize=read_header(input))) {
    if ( (nsamp > 0) && !strings_equal(inpfile,"stdin") ) {
      naddt=nsamples(inpfile,headersize,nbits,nifs,nchans)/nsamp;
      if (naddt%2) naddt--;
    }
    switch (data_type) {
    case 1:
      break;
    case 2:
      nchans=1;
      break;
    default:
      error_message("input data to decimate is not in filterbank format");
      break;
    }
    /* check number of time samples to add */
    if (naddt <= 1) naddt=1;
    /*if (naddt%2) error_message("time decimation must be a power of 2");*/
    /* check number of frequency channels to add (integer multiple) */
    if (naddc<1) naddc=nchans;
    nc=nchans/naddc;
    if ( (nc*naddc) != nchans ) 
      error_message("nchans must be integer multiple of decimation factor");
    if (obits == 0) obits=nbits;
    if (obits==1) error_message("output of 1-bit data will result in vastly reduced S/N!\nselect a higher output bit size with the -n option");
    /* all ok - broadcast the new header */
    if (!headerless) decimate_header();
  } else {
    error_message("input data file is of unknown origin!!!");
  }
  
  /* finally decimate and output the data */
  decimate_data(input,output);

  exit(0);
}
Example #9
0
int main(int argc, char *argv[]) {

  /* Cmd line */
  static struct option long_opts[] = {
	{"frontend",   1, NULL, 'F'},
	{"offset",   0, NULL, 'o'},
	{"srcname",   1, NULL, 's'},
  };
  
    int ii;
    double dtmp;
    char filename[256], *adr;
    struct psrfits pf;
    int c1,c2;
    double c3;
	int opt, opti;
    int scan_id, beam_id;
	bool have_Stokes = false;
	bool have_chan_offset = false;
	bool have_frontend = false; char frontend[3];
	bool have_srcname = false; char srcname[24];

	while ((opt=getopt_long(argc,argv,"ob:t:j:i:F:s:p:P:F:CuU:SAqh",long_opts,&opti))!=-1) {
	  switch (opt) {
	  case 'F':
		have_frontend = true;
		strncpy(frontend, optarg, 3);
		break;
	  case 'o':
		have_chan_offset = true;
		break;
	  case 's':
		have_srcname = true;
		strncpy(srcname, optarg, 24);
		break;
	  case 'S':
		have_Stokes = true;
		break;
	  case 'h':
	  default:
		usage();
		exit(0);
		break;
	  }

	}
    
    // Only set the basefilename and not "filename"
    // Also, fptr will be set by psrfits_create_searchmode()

    FILE *pfi;

    strcpy(filename, argv[optind]);
    printf("Filename = %s\n", filename);

    // Read filterbank                                                                              
    if((pfi=fopen(filename,"r"))==NULL) {
      printf("Cannot open file %s\n", filename);
      exit(-2);
    } else {
      printf("Opening file %s\n", filename);
    }

    /* try to read the header */
    long long header_size, numsamps;
    header iheader;
    if (!(header_size=read_header(pfi,&iheader))) {
      printf("Error reading header\n");
      exit(-2);
    }
    //printf("header_size = %lld\n", header_size);

    // Get the number of samples                                                                              
    numsamps=nsamples(filename, header_size, &iheader);
    //printf("numsamps = %lld  ra=%lf  dec=%lf\n", numsamps, iheader.src_raj, iheader.src_dej);
    //fflush(stdout);
    
    pf.filenum = 0;           // This is the crucial one to set to initialize things
    pf.rows_per_file = 4000;  // Need to set this based on PSRFITS_MAXFILELEN

    // Now set values for our hdrinfo structure
    pf.hdr.scanlen = 3600; // in sec
    strcpy(pf.hdr.observer, "Observer");
    strcpy(pf.hdr.telescope, "Effelsberg");
    strcpy(pf.hdr.obs_mode, "SEARCH");
    strcpy(pf.hdr.backend, "PFFTS");
    strncpy(pf.hdr.source, iheader.source_name, 24);
    strcpy(pf.hdr.frontend, "LBand");
    strcpy(pf.hdr.project_id, "HTRUN");
    strcpy(pf.hdr.date_obs, "2010-01-01T05:15:30.000");
    strcpy(pf.hdr.poln_type, "LIN");
    strcpy(pf.hdr.poln_order, "IQUV");
    strcpy(pf.hdr.track_mode, "TRACK");
    strcpy(pf.hdr.cal_mode, "OFF");
    strcpy(pf.hdr.feed_mode, "FA");
    //printf("filename = %s\n", filename); fflush(stdout);
    if(strstr(filename,"/") != NULL) { adr=(char *)strrchr(filename,'/')+1; sprintf(pf.filename,"%s",adr); }
    else strncpy(pf.filename, filename, 80);
    //printf("filename = %s\n", pf.filename); fflush(stdout);
    sscanf(pf.filename, "%d_%d_%d_%dbit.fil", &scan_id, &c1, &beam_id, &c2);
      
    pf.hdr.dt = iheader.tsamp;
    pf.hdr.fctr = iheader.fch1+iheader.foff*iheader.nchans/2. - iheader.foff/2.;
	if (have_chan_offset)
	  pf.hdr.fctr += iheader.foff/2.;
	
    pf.hdr.BW = iheader.nchans * iheader.foff;
    angle_split(iheader.src_raj, &c1, &c2, &c3);
    sprintf(pf.hdr.ra_str, "%2.2d:%2.2d:%07.4lf", c1, c2, c3);
    
    angle_split(iheader.src_dej, &c1, &c2, &c3);
    sprintf(pf.hdr.dec_str, "%2.2d:%2.2d:%07.4lf", c1, c2, c3);
    pf.hdr.azimuth = iheader.az_start;
    pf.hdr.zenith_ang = iheader.za_start;
    pf.hdr.beam_FWHM = 0.25;
    pf.hdr.start_lst = 0.0;
    pf.hdr.start_sec = 0.0;
    pf.hdr.start_day = 55000;
    pf.hdr.scan_number = scan_id;
    pf.hdr.rcvr_polns = 2;
    pf.hdr.summed_polns = 0;
    pf.hdr.offset_subint = 0;
    pf.hdr.nchan = iheader.nchans;
    pf.hdr.orig_nchan = pf.hdr.nchan;
    pf.hdr.orig_df = pf.hdr.df = pf.hdr.BW / pf.hdr.nchan;
    pf.hdr.nbits = iheader.nbits;
    pf.hdr.npol = iheader.nifs;
	pf.hdr.onlyI = 0;
	if (pf.hdr.npol==1) pf.hdr.onlyI = 1;
    pf.hdr.chan_dm = 0.0;
    pf.hdr.fd_hand = 1;
    pf.hdr.fd_sang = 0;
    pf.hdr.fd_xyph = 0;
    pf.hdr.be_phase = 1;
    pf.hdr.ibeam = beam_id;
    pf.hdr.nsblk = 2048;

    pf.hdr.MJD_epoch = iheader.tstart;  // Note the "L" for long double
    pf.hdr.ds_time_fact = 1;
    pf.hdr.ds_freq_fact = 1;
    //sprintf(pf.basefilename, "%s_%04d_%02d", pf.hdr.backend, pf.hdr.scan_number, pf.hdr.ibeam);

    //psrfits_create(&pf);


    // Now set values for our subint structure
    pf.sub.tsubint = pf.hdr.nsblk * pf.hdr.dt;
    pf.tot_rows = 0.0;
    pf.sub.offs = (pf.tot_rows + 0.5) * pf.sub.tsubint;
    pf.sub.lst = pf.hdr.start_lst;
    pf.sub.ra = pf.hdr.ra2000;
    pf.sub.dec = pf.hdr.dec2000;
    // GD slaEqgal(pf.hdr.ra2000*DEGTORAD, pf.hdr.dec2000*DEGTORAD, 
    //&pf.sub.glon, &pf.sub.glat);
    //pf.sub.glon *= RADTODEG;
    //pf.sub.glat *= RADTODEG;
    pf.sub.feed_ang = 0.0;
    pf.sub.pos_ang = 0.0;
    pf.sub.par_ang = 0.0;
    pf.sub.tel_az = pf.hdr.azimuth;
    pf.sub.tel_zen = pf.hdr.zenith_ang;
    pf.sub.bytes_per_subint = (pf.hdr.nbits * pf.hdr.nchan * 
                               pf.hdr.npol * pf.hdr.nsblk) / 8;
	
    if (pf.hdr.nbits==32) pf.sub.FITS_typecode = TFLOAT;
	else pf.sub.FITS_typecode = TBYTE;  // 11 = byte

	//
	if (have_frontend) {
	  if (strncmp(frontend,"S36",3)==0) {
		strcpy(pf.hdr.backend, "PSRIX");
		strcpy(pf.hdr.frontend, "S36");
		strcpy(pf.hdr.poln_type, "CIRC");
		strcpy(pf.hdr.poln_order, "AABBCRCI");
		pf.hdr.fd_hand = -1; // To conform PA and Stokes V to PSR/IEEE 
	  }
	  else if (strncmp(frontend,"S60",3)==0) {
		strcpy(pf.hdr.backend, "PSRIX");
		strcpy(pf.hdr.frontend, "S60");
		strcpy(pf.hdr.poln_type, "CIRC");
		strcpy(pf.hdr.poln_order, "AABBCRCI");		
	  }
	  else if (strncmp(frontend,"S110",3)==0) {
		strcpy(pf.hdr.backend, "PSRIX");
		strcpy(pf.hdr.frontend, "S110");
		strcpy(pf.hdr.poln_type, "CIRC");
		strcpy(pf.hdr.poln_order, "AABBCRCI");
		pf.hdr.be_phase = -1; // To conform Stokes V to PSR/IEEE
	  }
	  else if (strncmp(frontend,"S45",3)==0) {
		strcpy(pf.hdr.backend, "PSRIX");
		strcpy(pf.hdr.frontend, "S45");
		strcpy(pf.hdr.poln_type, "LIN");
		strcpy(pf.hdr.poln_order, "AABBCRCI");
		pf.hdr.be_phase = -1; // To conform Stokes V to PSR/IEEE
	  }
	  else {printf("Frontend %s not recognised\n", frontend);}
	}
	if (have_srcname) strncpy(pf.hdr.source, srcname, 24);

	if (have_Stokes) strcpy(pf.hdr.poln_order, "IQUV");
	
	if (strncmp(pf.hdr.backend, "PFFTS",5)==0)
	  sprintf(pf.basefilename, "%s_%04d_%02d", pf.hdr.backend, pf.hdr.scan_number, pf.hdr.ibeam);
	else
	  sprintf(pf.basefilename, "%s_%s_%5d_%d", pf.hdr.backend, pf.hdr.source, (int) pf.hdr.MJD_epoch, (int) pf.hdr.fctr);
	psrfits_create(&pf);
	
    // Create and initialize the subint arrays
    pf.sub.dat_freqs = (float *)malloc(sizeof(float) * pf.hdr.nchan);
    pf.sub.dat_weights = (float *)malloc(sizeof(float) * pf.hdr.nchan);
    dtmp = pf.hdr.fctr - 0.5 * pf.hdr.BW + 0.5 * pf.hdr.df;
    for (ii = 0 ; ii < pf.hdr.nchan ; ii++) {
        pf.sub.dat_freqs[ii] = dtmp + ii * pf.hdr.df;
        pf.sub.dat_weights[ii] = 1.0;
    }
    pf.sub.dat_offsets = (float *)malloc(sizeof(float) * pf.hdr.nchan * pf.hdr.npol);
    pf.sub.dat_scales = (float *)malloc(sizeof(float) * pf.hdr.nchan * pf.hdr.npol);
    for (ii = 0 ; ii < pf.hdr.nchan * pf.hdr.npol ; ii++) {
        pf.sub.dat_offsets[ii] = 0.0;
        pf.sub.dat_scales[ii] = 1.0;
    }
 

    // This is what you would update for each time sample (likely just
    // adjusting the pointer to point to your data)
    pf.sub.rawdata = (unsigned char *)malloc(pf.sub.bytes_per_subint);
   
    // Here is the real data-writing loop
    do {
      // Update the pf.sub entries here for each subint
      // as well as the pf.sub.data pointer
      
      fread(pf.sub.rawdata, pf.sub.bytes_per_subint, 1, pfi);
      pf.sub.offs = (pf.tot_rows + 0.5) * pf.sub.tsubint;
      
      psrfits_write_subint(&pf);
    } while (pf.T < pf.hdr.scanlen && !feof (pfi) && !pf.status);
    
    // Close the last file and cleanup
    fits_close_file(pf.fptr, &(pf.status));
    free(pf.sub.dat_freqs);
    free(pf.sub.dat_weights);
    free(pf.sub.dat_offsets);
    free(pf.sub.dat_scales);
    free(pf.sub.rawdata);

    printf("Done.  Wrote %d subints (%f sec) in %d files.  status = %d\n", 
           pf.tot_rows, pf.T, pf.filenum, pf.status);

    exit(0);
}
Example #10
0
int main (int argc, char *argv[])
{
  /* local variables */
//  char string[180];
  int i,useroutput=0,nfiles=0,fileidx,sigproc,scan_number,subscan=0;
  int numsamps=0;
  long long int totnumsamps=0;
  unsigned char * rawdata;
  unsigned short int * unpacked; //, * times;
  int nbytesraw;
  int ibyte,j,k;
  unsigned char abyte;
  unsigned short int ** times;
  unsigned short int * dmzero;
  int nread;
  float DM_trial;
  int ndm=0;
  double ti = 40.0;
  double tol = 1.25;
  float total_MBytes = 0;
  float start_DM=0.0, end_DM;
  int counts;
  int dmlogfile=0;
  int readsamp = 0;
  int nreadsamp = 0;
  int skip = 0;
  int nskip = 0;
  int ntoload;
  int ntodedisp;
  int maxdelay = 0;
  int appendable = 0;
  int ngulp; //max number of samples at a time
  int gulping = 0;
  int ngulpsize;
  int nsampleft; 
  char * killfile;
  int killing=0;
  bool doGsearch = 0,doMultibeam=0;
  int Gwidtol = 30;
  float Gthresh = 6;
  int Gscrnch = 256;
  int Goffset = 0;
  float Girrel = 3;
  char *Gfilename;
  Gfilename = (char *) malloc(13);
  strcpy(Gfilename,"GResults.txt");
  float flo,fhi;

  if(sizeof(LONG64BIT) != 8 ){
	  fprintf(stderr,"ERROR: sofware has been compiled with LONG64BIT as a datatype of %d bytes, needs to be 8\n",sizeof(LONG64BIT));
  }

  /* check number of command-line arguments and print help if necessary */
  if (argc<2) {
    inline_dedisperse_all_help();
    exit(0);
  }
  
  /* print help if necessary */
  if (strcmp(argv[1],"-h")==0) {
    inline_dedisperse_all_help();
    exit(0);
  }
  
  /* set up default globals */
  userbins=usrdm=asciipol=stream=clipping=swapout=headerless=0;
  sumifs=wapp_inv=wapp_off=barycentric=0;
  nobits=32;
  ascii=1;
  fftshift=1;
  profnum1 = 0;
  profnum2 = 1000;
  nbands=baseline=1;
  clipvalue=refrf=userdm=fcorrect=0.0;
  refdm=-1.0;
  output=NULL;
  randomise=false;
  zerodm=false;
  strcpy(ignfile,"");

  // **************************************
  //          PARSE COMMAND LINE
  // **************************************
  i = 1;
  while (i<argc) {
    if (fopen(argv[i],"rb")!=NULL){
      strcpy(inpfile,argv[i]);
      input=fopen(inpfile,"rb");
      if (input==NULL){
        fprintf(stderr,"Error opening file %s\n",inpfile);
        exit(-1);
      }
      nfiles++;
    }
    else if (!strcmp(argv[i],"-d")) {
      /* get DM from the user */
      start_DM=atof(argv[++i]);
      end_DM=atof(argv[++i]);
      usrdm=1;
    }
    else if (!strcmp(argv[i],"-l")) {
      /* Create a log file of the DMs */
      dmlogfile=1;
    }
    else if (!strcmp(argv[i],"--randomise")) {
      /* randomise channels */
      randomise=true;
    }
    else if (!strcmp(argv[i],"--zerodm")) {
      /* randomise channels */
      zerodm=true;
    }

   else if (!strcmp(argv[i],"-i")) {
      /* set intrinsic width */
      ti=atof(argv[++i]);
    }
    else if (!strcmp(argv[i],"-tol")) {
      /* set tolerance level (e.g. 25% = 1.25)*/
      tol=atof(argv[++i]);
    }
    else if (!strcmp(argv[i],"-v")) {
      verbose=1;
    }
    else if (!strcmp(argv[i],"-n")) {
      /* read only X samples */
      ntodedisp=atoi(argv[++i]);
      readsamp=1;
    }
    else if (!strcmp(argv[i],"-s")) {
      /* skip first X samples */
      nskip=atoi(argv[++i]);
      skip=1;
    }
    else if (!strcmp(argv[i],"-m")) {
      /* sub-band */
      nbands=atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-g")) {
      ngulpsize=atoi(argv[++i]);
      gulping=1;
    }
    else if (!strcmp(argv[i],"-a")) {
      appendable=1;
    }
    else if (!strcmp(argv[i],"-k")) {
      killing = 1;
      killfile = (char *) malloc(strlen(argv[++i])+1);
      strcpy(killfile,argv[i]);
    } else if (!strcmp(argv[i],"-or")) {
	output_rotate=atoi(argv[++i]);
    } else if (!strcmp(argv[i],"-os")) {
	output_subtract=atoi(argv[++i]);
    } else if (!strcmp(argv[i],"-G")) {
      doGsearch = 1;
      fprintf(stderr,"Will perform giant pulse search\n");
    }
    else if (!strcmp(argv[i],"-wid")) {
      Gwidtol = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-sig")) {
      Gthresh = atof(argv[++i]);
    }
    else if (!strcmp(argv[i],"-dec")) {
      Gscrnch = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-cut")){
      Girrel = atof(argv[++i]);
    }
    else if (!strcmp(argv[i],"-file")){
      Gfilename = (char *) malloc(strlen(argv[++i])+1);
      strcpy(Gfilename,argv[i]);
    }
    else if (!strcmp(argv[i],"-mb")){
	doMultibeam = 1;
    }
    else {
      /* unknown argument passed down - stop! */
      inline_dedisperse_all_help();
      fprintf(stderr,"unknown argument (%s) passed to %s\n\n",argv[i],argv[0]);
      exit(1);
    }
    i++;
  }


  // **************************************
  //            ERROR TESTING
  // **************************************
  if (doGsearch && nbands>1){
    fprintf(stderr,"Can't do gsearch while running in subband mode!\n");
    exit(-1);
  }

  if (!useroutput) {
    /* no output file selected, use standard output */
    output=stdout;
    strcpy(outfile,"stdout");
  }

  if (!nfiles) {
    fprintf(stderr,"File not supplied on command line; please supply filename here: ");
    strcpy(inpfile,"stdin");
    input=fopen(inpfile,"rb");
    if (input==NULL){
      fprintf(stderr,"Error opening file %s\n",inpfile);
      exit(-1);
    }
    nfiles=1;
  }
  fileidx=1;
  if (nfiles>1) {
    fprintf(stderr,"Multi file mode not supported yet!\n");
    exit(-1);
  }
  char tmp[180];
  char *tmp2;
  strcpy(tmp,inpfile); // These few lines strip the input file's
  tmp2 = basename(tmp);// name from it's directory tag.
  strcpy(outfile_root,tmp2);


  // **************************************
  //         VALUE PRECALCULATION
  // **************************************  
  /* read in the header to establish what the input data are... */
  sigproc=read_header(input);
  if (!sigproc) {
    fprintf(stderr,"Not sigproc data\n");
    exit(-1);
  }
  if (foff > 0.0) {
    fprintf(stderr,"dedisperse can't handle low->high frequency ordering!");
    exit(1);
  }

  // But in case low->high frequency ordering ever gets implemented...
  if (foff < 0){
    flo = (float)fch1 + (nchans*(float)foff);
    fhi = (float)fch1;
  } else {
    flo = (float)fch1;
    fhi = (float)fch1 + (nchans*(float)foff);
  }

  if (fileidx == 1) {
    /* this is filterbank data */
    if (output!=stdout) output=fopen(outfile,"wb");
    if (output==NULL){
      fprintf(stderr,"Error opening file %s\n",output);
      exit(-1);
    }
  }
  
  numsamps = nsamples(inpfile,sigproc,nbits,nifs,nchans);	/* get numsamps */
  totnumsamps = nsamples(inpfile,sigproc,nbits,nifs,nchans);
  if (usrdm) maxdelay = DM_shift(end_DM,nchans,tsamp,fch1,foff);
  cout << "maxdelay = " << maxdelay << endl;
  
  if (readsamp) numsamps=ntodedisp+maxdelay;
  
  if (gulping) {
    if (ngulpsize>numsamps) ngulpsize = numsamps-maxdelay;
    ntodedisp=ngulpsize;
  } else {
    ntodedisp=numsamps-maxdelay;
    ngulpsize=ntodedisp;
  }

  ntoload = ntodedisp + maxdelay; 
  Goffset = (int)(ntoload-maxdelay);
  
  nbytesraw = nchans * ntoload * nbits/8;
  if ((rawdata = (unsigned char *) malloc(nbytesraw))==NULL){
      fprintf(stderr,"Error allocating %d bytes of RAM for raw data\n",nbytesraw);
      exit(-1);
  }
  // skip either 0 or nskip samples
  fseek(input, nskip*nchans*nbits/8, SEEK_CUR);
  nsampleft-=nskip;
  // some values used for unpacking
  int sampperbyte = (int)(8/nbits);
  int andvalue = (int)pow(2,nbits)-1;


  // **************************************
  //      SET UP DMTABLE AND SUBBANDS
  // **************************************  
  float * DMtable;
  // a hack for less files when subbanding... M.Keith
  if (nbands==1)
    getDMtable(start_DM,end_DM, tsamp*1e6, ti, foff, (fch1+(nchans/2-0.5)*foff)/1000,nchans/nbands, tol, &ndm, DMtable);
  else
    getDMtable(0.0,end_DM, tsamp*1e6, ti, foff, (fch1+(nchans/2-0.5)*foff)/1000,nchans/nbands, tol, &ndm, DMtable);

  // If Gsearch is being run but start_DM is non-zero, add 0.0 to DM table.
  if (doGsearch && start_DM != 0.0){
      float *tempDMtable = new float[ndm+1];
      tempDMtable[0] = 0.0;
#pragma omp parallel for private(i)
      for (int i=0;i<ndm;i++){
	  tempDMtable[i+1] = DMtable[i];
      }
      delete DMtable;
      DMtable = tempDMtable;
      ndm++;
  }
  
  fprintf(stderr,"%d subbands from %d chans\n",nbands,nchans);

  if(nchans % nbands){
	fprintf(stderr,"invalid number of subbands selected!\n");
	exit(1);
  }

  int nwholegulps = (numsamps - maxdelay)/ngulpsize;
  int nleft = numsamps - ngulpsize * nwholegulps;
  int ngulps = nwholegulps + 1;


  //  if (!debird){
    unpacked = (unsigned short int *) malloc(nchans*ntoload*
					     sizeof(unsigned short int));
    if (unpacked==NULL) {
      fprintf(stderr,"Failed to allocate space for unpacked %d bytes\n",
	      (int)(nchans*ntoload*sizeof(unsigned short int)));
      exit(-2);
    }
    if (zerodm){
	    dmzero = (unsigned short int *) 
		    malloc(sizeof(unsigned short int)*ntodedisp); 
    }
    times = (unsigned short int **) 
      malloc(sizeof(unsigned short int*)*nbands); 
    for(int band=0; band < nbands; band++){
	times[band] = (unsigned short int *)
		      malloc(sizeof(unsigned short int)*ntodedisp);
    }
    if (times==NULL){
      fprintf(stderr,"Error allocating times array\n");
      exit(-1);
    }
    //  } // (!debird)

  int * killdata = new int[nchans];
  if (killing) int loaded = load_killdata(killdata,nchans,killfile);
  else
    {
      killing=1;
      for (int i=0;i<nchans;i++) killdata[i]=1; // ie don't kill anything.
    }



  int prerotate=0;
  if (zerodm){
	  while(nchans*(pow(2,nbits)-1)*(float)(pow(2,prerotate)) < 32768)
		  prerotate++;

	  max_chan_val=(pow(2,nbits)-1)*(float)(pow(2,prerotate));

	  printf("Using 'ZERODM' RFI reduction method\n");
	  printf("Multiplying input by %d to increase dynamic range for zerodm removal\n",(int)(pow(2,prerotate)));
  }


  int rotate = 0;
  while(pow(2,prerotate)*nchans*(pow(2,nbits)-1)/(float)nbands/(float)(pow(2,rotate)) > 255)
    rotate++;
  
  rotate-=output_rotate;

  if (output_rotate){
	  printf("Warning: Modifying scale factor by %d, some clipping may occur!\n",(int)(pow(2,output_rotate)));
  }
  printf("Dividing output by %d to scale to 1 byte per sample per subband\n",(int)(pow(2,rotate)));

  if(randomise){
	  sprintf(outfile_root,"%s_RAND",outfile_root);
	  printf("WARNING: Randomising channel order! Data will not be astrophysical.\n");
  }

  // Set up gpulse control variables
  GPulseState Gholder(ndm); // Giant pulse state to hold trans-DM detections
  int Gndet;                // Integer number of detections in this beam
  int *Gresults;            // Integer array of results: contained as cand1.start.bin cand1.end.bin cand2.start.bin cand2.end.bin...... etc)
  //Gresults = new int[totnumsamps*ndm]; // This declaration might not be necessary

  if (doGsearch){
      FILE *Gresultsfile;
      Gresultsfile = fopen(Gfilename,"w");
      if (Gresultsfile==NULL){
	  fprintf(stderr,"Error opening giant search results output file %s.\nWILL NOT RUN GIANT SEARCH.\n\n",Gresultsfile);
	  doGsearch = false;
      } else {
	  //HERE open Gfile and print out a short "header". Gfilename is the name of the file.
	  // header needs to contain:        //      infilename, nsamp, tsamp, ctr. frequency, bandwidth, RA, DEC, UTC  time/date of obs, snr limit, Ndms, DM search range.
	  fprintf(Gresultsfile,"# %s %lld %g %g %g %g %g %g %.3f %d %.2f %.2f\n",outfile_root,totnumsamps,tsamp,fch1 + 0.5*(nchans*foff),fabs(foff*nchans),(src_raj),(src_dej),(src_dej),Gthresh,ndm,start_DM,end_DM);
      }
      fclose(Gresultsfile);
  }


  // Start of main loop
  for (int igulp=0; igulp<ngulps;igulp++){

//rewind a few samples if not at first gulp
    if (igulp!=0){
	    fprintf(stderr,"Skipping back %d bytes\n",maxdelay*nbits*nchans/8);
	    int ret = fseek(input,-1*maxdelay*nbits*nchans/8,SEEK_CUR);
	    if(ret){
		    fprintf(stderr,"Could not skip backwards!\n");
		    exit(1);
	    }
    }
    if (igulp==nwholegulps) {
      ntoload = nleft;
      nbytesraw = ntoload*nbits*nchans/8;
      ntodedisp = nleft - maxdelay;
    }

//read gulp from file
    fprintf(stderr,"Gulp %d Loading %d samples, i. e. %d bytes of Raw Data\n",igulp, ntoload, nbytesraw);
    nread = fread(rawdata,nbytesraw,1,input);
    
    if (nread != 1){
      fprintf(stderr, "Failed to read %d nread = %d \n", nbytesraw, nread);
      exit(-1);
    }
  
    /* Unpack it if dedispersing */
    
    
    if (1){
      if (verbose) fprintf(stderr,"Reordering data\n");
      // all time samples for a given freq channel in order in RAM

      for (ibyte=0;ibyte<nchans/sampperbyte;ibyte++){
#pragma omp parallel for private (abyte,k,j)
        for (j=0;j<ntoload;j++){
          abyte = rawdata[ibyte+j*nchans/sampperbyte];
	  for (k=0;k<8;k+=nbits)
            unpacked[j+((ibyte*8+k)/(int)nbits)*ntoload]=(unsigned short int)((abyte>>k)&andvalue) << prerotate;
	  }
      }
      
      
      if (1==0){ // Old way.
      if (killing){
	for (int i=0;i<nchans;i++){
	  if (!killdata[i]){
	    cout << i << " " ;
#pragma omp parallel for private(j)	  
	    for (int j=0;j<ntoload;j++)
	      unpacked[j+i*ntoload]=0;
	  }
	}
      }
      }
      
      cout << endl; // flush the buffer
      
      /* for each DM dedisperse it */
      /* Start with zero DM */
      if (!usrdm) end_DM=1e3;
      if (dmlogfile && igulp==0){
	sprintf(dmlogfilename,"%s.dmlog",outfile_root);
	dmlogfileptr=fopen(dmlogfilename,"w");
	if (dmlogfileptr==NULL) {
	  fprintf(stderr,"Error opening file %s\n",dmlogfilename);
	  exit(-3);
	}
      }
      if (igulp==0) appendable=0; else appendable=1;


      if (zerodm){
	    do_dedispersion(&dmzero, unpacked, 1, ntoload, ntoload, 0, killdata);
	    do_zerodm(dmzero, unpacked, ntodedisp, ntoload, killdata);
      }

      for (int idm=0;idm<ndm;idm++)
	{
	  //DM_trial = get_DM(idm,nchans,tsamp,fch1,foff);
	  DM_trial = DMtable[idm];
	  if ((DM_trial>=start_DM && DM_trial<=end_DM) || (doGsearch && DM_trial==0)){
	    if (verbose) fprintf(stderr,"DM trial #%d at  DM=%f ",idm,DM_trial);
	    if (dmlogfile && igulp==0) fprintf(dmlogfileptr,"%f %07.2f\n",DM_trial,
				   DM_trial);
	    // Name output file, dmlog file
	    if(nbands==1)sprintf(outfile,"%s.%07.2f.tim",outfile_root,DM_trial);
	    else sprintf(outfile,"%s.%07.2f.sub",outfile_root,DM_trial);
	    // open it
	    if (appendable){
		outfileptr=fopen(outfile,"a");
		if (outfileptr==NULL) {
		    fprintf(stderr,"Error opening file %s\n",outfile);
		    exit(-3);
		}
	    }
	      if (!appendable){
	      outfileptr=fopen(outfile,"w");
	      if (outfileptr==NULL) {
		  fprintf(stderr,"Error opening file %s\n",outfile);
		  exit(-3);
	      }
	      // create a log of the 'sub' dms to dedisperse at
	      // These DMs are chosen fairly arbitraraly, rather than
	      // based on any ridgid mathematics.
	      if(nbands > 1){
		float* DMtable_sub;
		int ndm_sub;
		float sub_start_DM=0;
		float sub_end_DM=end_DM;
		if (idm > 0){
		  sub_start_DM =  (DMtable[idm]+ DMtable[idm-1])/2.0;
		}
		if (idm < ndm-1){
		  sub_end_DM =  (DMtable[idm]+ DMtable[idm+1])/2.0;
		}
		// make the dm table for the sub-banded data...
		getDMtable(sub_start_DM,sub_end_DM, tsamp*1e6, ti, foff, (fch1+(nchans/2-0.5)*foff)/1000,
			   nchans, tol, &ndm_sub, DMtable_sub);
		char subdm_filename[180];
		sprintf(subdm_filename,"%s.valid_dms",outfile);
		FILE* subdm_fileptr = fopen(subdm_filename,"w");
		if(subdm_fileptr == NULL){
		  printf("Error: could not open file %s for writing\n",subdm_filename);
		  exit(2);
		}
		for(int ix = 0; ix < ndm_sub; ix++){
		  double sub_dm_trial = DMtable_sub[ix];
		  fprintf(subdm_fileptr,"%f\n",sub_dm_trial);
		}
		delete[] DMtable_sub;
		fclose(subdm_fileptr);
	      }
	    }
	    output=outfileptr;
	    // write header variables into globals
	    // dedisperse_header() uses "userdm" not refdm, so make sure to set that. MJK-19042010
	    refdm = DM_trial;
	    userdm = refdm;
	    nobits = 8;
	    // write header
	    if (!appendable) dedisperse_header();
	    // do the dedispersion
	    do_dedispersion(times, unpacked, nbands, ntodedisp, ntoload, DM_trial, killdata);

	    // Do the Gsearch for this DM trial
	    if (doGsearch){
		int runningmeanval = (int)(2.0/tsamp); //will smooth over two seconds.
//		sprintf"%f %07.2f\n",DM_trial, //FORMAT DM HERE?
		float *temptimes = new float[ntodedisp];
		if (runningmeanval>ntodedisp){
		    fprintf(stderr,"***Giants warning: running mean longer than data; will not remove baseline\n");
		    Gholder.searchforgiants(idm,ntodedisp,(int)(Goffset*igulp),times[0],Gthresh,Gwidtol,Gscrnch,DM_trial,1);
		} else if (temptimes == NULL) {
		    fprintf(stderr,"***Giants warning: Couldn't allocate %d floats; will not remove baseline\n",ntodedisp);
		    Gholder.searchforgiants(idm,ntodedisp,(int)(Goffset*igulp),times[0],Gthresh,Gwidtol,Gscrnch,DM_trial,1);
		} else {
		    removebaseline(times[0],temptimes,ntodedisp,runningmeanval,3.0);
		    Gholder.searchforgiants(idm,ntodedisp,(int)(Goffset*igulp),temptimes,Gthresh,Gwidtol,Gscrnch,DM_trial,1);
		}
		delete[] temptimes;
	    }


	    // write data
	    if (1==1){
#if SIGNED
		char lotsofbytes[ntodedisp];
#else
		unsigned char lotsofbytes[ntodedisp];
#endif
		for (int d=0;d<ntodedisp;d++){
		    for(int iband=0; iband<nbands; iband++){
			unsigned short int twobytes = times[iband][d]>>rotate;
			if (output_subtract){
				twobytes-=output_subtract;
				if (twobytes > 32768){
					twobytes=0;
				}
			}
			if (output_rotate && twobytes > 255){
				twobytes=255;
			}
			//		unsigned char onebyte = twobytes;
#if SIGNED
			// here we subtract 128 to make signed since numbers were scaled
			// to be between 0 -> 256
			lotsofbytes[d]=(twobytes-128);
#else
			lotsofbytes[d]=(twobytes);
#endif
		    }
		}
		fwrite(lotsofbytes,ntodedisp,1,outfileptr);
	    }
	    // close file
	    fclose(outfileptr);
	    if (verbose) fprintf(stderr,"%d bytes to file %s\n", ntodedisp*nbands, outfile);
	    total_MBytes+= (ntodedisp*nbands)/1.0e6;
	  } // if DM is in range
	} // DM for loop
      if (verbose) fprintf(stderr,"Wrote a total of %6.1f MB to disk\n",total_MBytes);
      /* close log files if on last input file */
      if (dmlogfile && igulp==0) fclose(dmlogfileptr);
    } //if (!debird)
    
    // After gulp's done, pump out the Gsearch results for that gulp
    if (doGsearch){

	if (verbose) fprintf(stderr,"Completeing Gsearch results for this gulp\n");
	string UTroot = outfile_root;
	int pos = UTroot.find(".fil");
	if( pos != string::npos)
	    UTroot.replace(pos, 4, "");
	
	Gresults = Gholder.givetimes(&Gndet,tsamp,flo,fhi,Girrel,&UTroot[0],ibeam,Gfilename);

	Gholder.selfdestruct();
//	for (i=0;i<ndm;i++) Gholder.DMtrials[i].erase(Gholder.DMtrials[i].begin(),Gholder.DMtrials[i].end());
//      fprintf(stderr,"GRESULTS:\n");
//      for (int i=0;i<Gndet;i+=2){
//	  fprintf (stderr,"Detection %d: %d\t%d\n",i,Gresults[i],Gresults[i+1]);
//      }
	if (verbose) fprintf(stderr,"Completed Gsearch results for this gulp\n");
    }

  } //end per-gulp loop
Example #11
0
main (int argc, char *argv[])
{
  /* local variables */
  double pfactor,newmjd=0.0;
  float sefd;
  int i,opened_input=0,opened_output=0,headersize=0;
  char string[80];

  /* set up default globals */
  baseline=ascii=multiple=1;
  npuls=binary=totalpower=accumulate=0;
  time_offset=acceleration=skip_time=read_time=0.0;
  asciipol=psrfits=stream=headerless=npulses=0;
  phase_start=folding_period=dump_time=tsamp_user=0.0;
  phase_finish=pfactor=1.0;
  jyfactor=sefd=userbase=0.0;
  nbins=0; /* this will get set in the folding routine if not set by user */
  strcpy(polyco_file,"");

  /* check the command line parameters */
  i=1;
  while (i<argc) {
    print_version(argv[0],argv[1]);
    if (strings_equal(argv[i],"-o")) {
      /* get and open file for output */
      i++;
      strcpy(outfile,argv[i]);
      output=fopen(outfile,"wb");
      opened_output=1;
    } else if (strings_equal(argv[i],"-m")) {
      multiple=atoi(argv[++i]);
    } else if (strings_equal(argv[i],"-p")) {
      /* get folding period */
      i++;
      if (file_exists(argv[i])) {
	strcpy(polyco_file,argv[i]);
	folding_period=-1.0;
      } else {
	folding_period=atof(argv[i]);
      }
    } else if (strings_equal(argv[i],"-dt")) {
      /* add a time offset in seconds to tstart */
      time_offset=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-mjd")) {
      /* change the start time completely! */
      newmjd=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-sk")) {
      /* skip the first skip_time seconds before folding */
      skip_time=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-re")) {
      /* read and fold only read_time seconds of data */
      read_time=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-a")) {
      /* get acceleration for folding */
      acceleration=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-d")) {
      /* get dumptime or number of pulses for subintegrations */
      i++;
      if (strcspn(".",argv[i])) {
	npulses=atoi(argv[i]);
      } else {
	dump_time=atof(argv[i]);
      }
    } else if (strings_equal(argv[i],"-t")) {
      /* get user-supplied sampling time */
      i++;
      tsamp_user=atof(argv[i]);
    } else if (strings_equal(argv[i],"-j")) {
      /* get user-supplied Jansky calibration factor */
      jyfactor=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-s")) {
      /* get user-supplied SEFD */
      sefd=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-b")) {
      /* get user-supplied baseline */
      baseline=0;
      userbase=atof(argv[++i]);
    } else if (strings_equal(argv[i],"-f")) {
      /* get period multiplication factor */
      i++;
      pfactor=atof(argv[i]);
    } else if (strings_equal(argv[i],"-l")) {
      /* get leading phase of pulse */
      i++;
      phase_start=atof(argv[i]);
      if ( (phase_start < 0.0) || (phase_start > 1.0) ) 
	error_message("start pulse phase out of range!");
    } else if (strings_equal(argv[i],"-r")) {
      /* get trailing phase of pulse */
      i++;
      phase_finish=atof(argv[i]);
      if ( (phase_finish < 0.0) || (phase_finish > 1.0) ) 
	error_message("final pulse phase out of range!");
    } else if (strings_equal(argv[i],"-n")) {
      /* get number of bins */
      i++;
      nbins=atoi(argv[i]);
    } else if (strings_equal(argv[i],"-ascii")) {
      /* write data as ASCII numbers */
      ascii=1;
    } else if (strings_equal(argv[i],"-totalpower")) {
      /* sum polarizations 1+2 before writing */
      totalpower=1;
    } else if (strings_equal(argv[i],"-epn")) {
      /* write data in EPN format */
      ascii=0;
    } else if (strings_equal(argv[i],"-bin")) {
      /* write data in SIGPROC binary format */
      binary=1;
    } else if (strings_equal(argv[i],"-acc")) {
      /* write out accumulated pulse profiles in subints */
      accumulate=1;
    } else if (strings_equal(argv[i],"-asciipol")) {
      /* write data as ASCII numbers for Jim's polarization code */
      asciipol=1;
    } else if (strings_equal(argv[i],"-psrfits")) {
      /* write data in PSRFITS format */
      ascii=0;
      psrfits=1;
#ifndef PSRFITS
      error_message("-psrfits option not supported in this compilation...\nConsult the SIGPROC manual for further information about PSRFITS.");
#endif
    } else if (strings_equal(argv[i],"-stream")) {
      /* write data as ASCII streams */
      stream=1;
    } else if (strings_equal(argv[i],"-sub")) {
      /* shorthand for -nobaseline -stream -d x */
      stream=1;
      baseline=0;
      i++;
      if (strcspn(".",argv[i])) {
	npulses=atoi(argv[i]);
      } else {
	dump_time=atof(argv[i]);
      }
    } else if (strings_equal(argv[i],"-nobaseline")) {
      /* processing correlation functions so don't subtract baseline */
      baseline=0;
    } else if (file_exists(argv[i])) {
      /* get and open file for input */
      strcpy(inpfile,argv[i]);
      input=open_file(inpfile,"rb");
      opened_input=1;
    } else if (help_required(argv[i])) {
      fold_help();
      exit(0);
    } else {
	/* unknown argument passed down - stop! */
	fold_help();
	sprintf(string,"unknown argument (%s) passed to %s",argv[i],argv[0]);
	error_message(string);
    }
    i++;
  }

  /* get appropriate calibration factor from SEFD and baseline */
  if (sefd != 0.0 && userbase != 0.0) jyfactor=sefd/userbase;

  /* multiply folding period by user-supplied factor */
  if (folding_period != -1.0) folding_period*=pfactor;

  /* check start and end phase of pulse */
  if (phase_start >= phase_finish) 
    error_message("silly pulse phases selected!");

  /* check npulses versus dump_time */
  if (npulses < 0) error_message("npulses < 0!");
  if ((npulses > 0) && (dump_time > 0.0)) 
    error_message("can't have npulses AND dumptime defined!");

  /* check for folding period still set to zero - if so, look for polyco.dat */
  if (folding_period == 0.0) {
    strcpy(polyco_file,"polyco.dat");
    if (file_exists(polyco_file)) {
      folding_period=-1.0;
    } else {
      error_message("folding period not specified and no polyco.dat found!");
    }
  }

  if (!opened_input) {
    /* no input file selected, use standard input */
    input=stdin;
    strcpy(inpfile,"stdin");
  }

  /* read in the header parameters from the input stream */
  if (!(headersize=read_header(input))) 
    error_message("could not read header parameters!");

  if (acceleration != 0.0) {
    tobs=tsamp*(double)nsamples(inpfile,headersize,nbits,nifs,nchans);
    if (tobs <= 0.0) error_message("could not get sensible observation time");
  }

  /* override the header */
  if (newmjd!=0.0) tstart=newmjd;

  if (!opened_output) {
    /* no output file selected, use standard output */
    output=stdout;
    strcpy(outfile,"stdout");
  }

  /* open the raw data file and establish its origin and header pars */
  switch(data_type) {
  case 1: 
  case 2:
  case 6:
    open_log("fold.monitor");
    folded_profiles=fold_data();
    break;
  default:
    error_message("input data is of unknown origin!!!");
  }

  if ((npulses==0.0) && (dump_time==0.0))
     write_profiles(folded_profiles,nbins,nchans,nifs,output);
  if (stream) fprintf(output,"#DONE\n");

  /* all done, update and close logfile */
  update_log("finished");
  close_log();
  i=0;
#ifdef PSRFITS
  if (psrfits) fits_close_file(fits,&i);
#endif
  exit(0);
}
Example #12
0
main(int argc, char *argv[]) 
{
	FILE *fileptr, *outfile;
	char filename[1024],*telescope,*backend,*datatype,message[80],unit[16];
	int i,j,year,month,day,check,rah,ram,ded,dem;
	double ras,des,frac,tobs;
	char sra[6],sde[6],decsign;
	int raw,uth,utm,uts;
	long long numsamps,datasize,headersize;
	double readsec,skipsec;

	readsec=1;
	skipsec=0;


	fileptr=stdin;
	outfile=stdout;
	strcpy(filename,"stdin");
	strcpy(rawdatafile,"stdin");
	pulsarcentric=barycentric=0;


	if (argc>1) {
		print_version(argv[0],argv[1]);
		if (help_required(argv[1])) {
			chop_fil_help();
			exit(0);
		} else if (file_exists(argv[1])) {
			strcpy(filename,argv[1]);
			fileptr=open_file(filename,"rb");
		} else if (!file_exists(argv[1]) && (strncmp(argv[1],"-",1) !=0)) {
			sprintf(message,"Data file: %s not found...\n",argv[1]);
			error_message(message);
			exit(1);
		}
	}


	if (!(headersize=read_header(fileptr))) {
		rewind(fileptr);
		if ((raw=typeof_inputdata(fileptr,filename))) {
			data_type=0;
			switch (raw) {
				case 1:
					headersize=32768;
					break;
				case 5:
					headersize=32768;
					break;
				case 3:
					headersize=wapp_header_size+wapp_incfile_length;
					break;
				default:
					break;
			}
		} else {
			error_message("could not read header parameters!");
			exit(1);
		}
	}

	/* attempt to find number of bytes of data and number of samples */
	if (!strings_equal(filename,"stdin")) {
		datasize=sizeof_file(filename)-headersize;
		numsamps=nsamples(filename,headersize,nbits,nifs,nchans);
	} else if (!strings_equal(rawdatafile,"stdin")) {
		datasize=sizeof_file(rawdatafile)-headersize;
		numsamps=nsamples(rawdatafile,headersize,nbits,nifs,nchans);
	} else {
		datasize=numsamps=0;
	}

	telescope=telescope_name(telescope_id);
	backend=backend_name(machine_id);
	datatype=data_category(data_type);

	if (argc>2) {
		check=1;
		i=2;
	} else if ((argc>1) && strings_equal(filename,"stdin")) {
		check=1;
		i=1;
	} else {
		check=0;
	}
	char force_read=0;

	for (i = 0; i < argc ; i++){
		if (strcmp(argv[i],"-s")==0){
			skipsec=atof(argv[++i]);
		}
		if (strcmp(argv[i],"-r")==0){
			readsec=atof(argv[++i]);
		}
		if (strcmp(argv[i],"-f")==0){
		   force_read=1;
		}

	}

	rewind(fileptr);

	char* block_array;
	unsigned long long int count;
	unsigned long long int update_count;
	unsigned long long int bytes_per_sample=(unsigned long long int)(nchans*nbits)/8;
	unsigned long long int bytes_to_read=bytes_per_sample * (unsigned long long int)(readsec / tsamp +0.5);
	unsigned long long int bytes_to_skip=bytes_per_sample * (unsigned long long int)(skipsec / tsamp + 0.5);
	unsigned long long int blocksize = bytes_per_sample;
	unsigned long long int numblocks = bytes_to_read / blocksize;
	unsigned long long int update_size = (unsigned long long int) (10.0 * (bytes_per_sample/tsamp));
	fprintf(stderr,"Bytes per sample = %lld\n",bytes_per_sample);
	fprintf(stderr,"Bytes to read    = %lld\n",bytes_to_read);
	fprintf(stderr,"Bytes to skip    = %lld\n",bytes_to_skip);


	fprintf(stderr,"\n\n==============\n");
	fprintf(stderr,"Copying header (%d bytes)\n",headersize);

	block_array = (char*)malloc(headersize);
	count = fread(block_array,1,headersize,fileptr);
	if ( count != headersize ){
		fprintf(stderr,"Error! Could not read header %d/%d\n",count,headersize);
		exit(1);
	}
	count = fwrite(block_array,1,headersize,outfile);
	if ( count != headersize ){
		fprintf(stderr,"Error! Could not write header\n");
		exit(1);
	}
	free(block_array);

	fprintf(stderr,"Skipping data...\n");
	count = 0;
	update_count = update_size;
	while ( count < bytes_to_skip ) { 
		fseek(fileptr,blocksize,SEEK_CUR);
		count += blocksize;
		update_count += blocksize;
		if ( update_count >= update_size ){
			update_count=0;
			fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
			fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);
		}
	}
	fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
	fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);


	fprintf(stderr,"\n\n");
	block_array = (char*) malloc(blocksize);
	
	fprintf(stderr,"Copying data...\n");
	count = 0;
	update_count = update_size;
	while ( count < bytes_to_read ) { 
		int read = fread(block_array,1,blocksize,fileptr);
		if ( read < 1 ) {
		   if (force_read){
			  fclose(fileptr);
			  fileptr = fopen("/dev/urandom","r");
			  fprintf(stderr,"\rPast end of file, reading from /dev/urandom\n");
			  continue;
		   } else {
			fprintf(stderr,"Error! Could not read enough data\n");
			exit(2);
		   }
		}
		read = fwrite(block_array,1,read,outfile);
		if ( read < 1 ) {
			fprintf(stderr,"Error! Could not write enough data\n");
			exit(2);
		}
		count += read;
		update_count += read;
		if ( update_count >= update_size ){
			update_count=0;
			fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
			fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);
		}
	}
	fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
	fprintf(stderr,"\t% 8.1f s",tsamp*count/(float)bytes_per_sample);

	fprintf(stderr,"\nDone\n");

	free(block_array);

	exit(0);
}