Ejemplo n.º 1
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;
      }
    }
Ejemplo n.º 2
0
/**
 * baseline method using exponential smoothing, smoothing factor set at 0.02 default.
 * ndat is the size of the timeseries
 * dat is the timeseries
 * overloaded method takes threshold which will cut out spikes above specified 
 * threshold in sigma's.
**/
void
find_baseline( int ndat, float * dat,float smooth_nsamp) {
	find_baseline(ndat,dat,smooth_nsamp,std::numeric_limits<float>::max());
}