Exemplo n.º 1
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);
}
Exemplo n.º 2
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);
}
Exemplo n.º 3
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;
      }
    }
Exemplo n.º 4
0
main(int argc, char **argv) 
{
  float tsec,fmhz,peak=1.0,sum,sumsq,sigma,sig2,mean,meansq,chi2,diff;
  float *pcopy,n;
  int hh,mm,i,j,row,dummy,mbins=64,obins,first=1,idx,nprof=1,display=0,rc2=0,flux=0;
  unsigned long *indx;
  char line[240], hash, gry[10], message[80];

  strcpy(gry,"   ,:o*@$#");
  input=stdin;

  if (argc > 1) {
    print_version(argv[0],argv[1]);
    if (help_required(argv[1])) {
      profile_help();
      exit(0);
    }
    i=1;
    while (i<argc) {
      if (file_exists(argv[i])) {
	input=open_file(argv[i],"r");
      } else if (strings_equal(argv[i],"-frequency")) {
	display=1;
      } else if (strings_equal(argv[i],"-chi2")) {
	rc2=1;
      } else if (strings_equal(argv[i],"-sum")) {
	flux=1;
      } else if (strings_equal(argv[i],"-p")) {
	peak=atof(argv[++i]);
      } else {
	sprintf(message,"command-line argument %s not recognized...",argv[i]);
	error_message(message);
      }
      i++;
    }
  }

  fgets(line,sizeof(line),input);
  sscanf(line,"%c %lf %lf %lf %ld %lf %lf %d",&hash,
	 &mjdobs,&tstart,&period,&np,&fch1,&refdm,&nbins);
  if (nbins > 0) {
    while (!feof(input)) {
      profile=(float *) malloc(sizeof(float)*nbins);
      for (i=0; i<nbins; i++) {
	fscanf(input,"%d %f",&dummy,&profile[i]);
      }
      if (rc2 || flux) {
	pcopy=(float *) malloc(sizeof(float)*nbins);
	for (i=0;i<nbins;i++) pcopy[i]=profile[i];
	indx=(unsigned long *) malloc(sizeof(unsigned long)*nbins);
	indexx((unsigned long)nbins,pcopy,indx);
	if (flux) {
	  n=sum=sumsq=0.0;
	  for (i=0;i<40;i++) {
	    sum+=profile[i];
	    sumsq+=profile[i]*profile[i];
	    n+=1.0;
	  }
	  mean=sum/n;
	  meansq=sumsq/n;
	  sigma=sqrt(meansq-mean*mean)/sqrt(n-1.0);
	  n=sum=sumsq=0.0;
	  for (i=43;i<52;i++) {
	    sum+=profile[i];
	    sumsq+=profile[i]*profile[i];
	    n+=1.0;
	  }
	  printf("%f %f\n",sum/n,sigma);
	  exit(0);
	}
	n=sum=sumsq=0.0;
	for (i=0;i<nbins/2;i++) {
	  j=indx[i];
	  sum+=profile[j];
	  sumsq+=profile[j]*profile[j];
	  n+=1.0;
	}
	mean=sum/n;
	meansq=sumsq/n;
	sigma=sqrt(meansq-mean*mean);
	sig2=sigma*sigma;
	sumsq=0.0;
	for (i=0;i<nbins;i++) {
	  diff=profile[i]-mean;
	  sumsq+=diff*diff;
	}
	//chi2=sumsq/sig2/(float)(nbins-1);
	chi2=sumsq/(float)(nbins-1);
	printf("%f %f\n",period,chi2);
	exit(0);
      }
      if (nbins>mbins) {
	add_channels(profile,nbins,nbins/mbins);
	nbins=mbins;
      }
      pmin=vmin(profile,nbins);
      pmax=vmax(profile,nbins)*peak;
      prng=pmax-pmin;
      for (i=0; i<nbins; i++) profile[i]=19.0*(profile[i]-pmin)/prng+1.0;
      for (row=20; row>=1; row--) {
	for (i=0; i<nbins; i++) {
	  if (profile[i]>=(float) row) 
	    printf("#");
	  else
	    printf(" ");
	}
	printf("\n");
      }
      free(profile);
      nbins=0;
      fgets(line,sizeof(line),input);
      fgets(line,sizeof(line),input);
      sscanf(line,"%c %lf %lf %lf %ld %lf %lf %d",&hash,
	 &mjdobs,&tstart,&period,&np,&fch1,&refdm,&nbins);
      if (nbins<=0) break;
    }
  } else {
    while (!feof(input)) {
      sscanf(line,"#START %d %f %f",&nbins,&tsec,&fmhz);
      if (nbins <=0) break;
      profile=(float *) malloc(sizeof(float)*nbins);
      for (i=0; i<nbins; i++) fscanf(input,"%d %f",&dummy,&profile[i]);
      fgets(line,sizeof(line),input);
      fgets(line,sizeof(line),input);
      if (nbins>mbins) {
	obins=nbins;
	add_channels(profile,nbins,nbins/mbins);
	nbins=mbins;
      }
      if (first) {
	pmin=vmin(profile,nbins);
	pmax=vmax(profile,nbins)*peak;
	prng=pmax-pmin;
	/*first=0;*/
      }
      if (display) {
	printf("|%04d|%11.6f|",nprof,fmhz);
      } else {
	hh=(int) tsec/3600.0;
	tsec-=(float) hh*3600.0;
	mm=(int) tsec/60.0;
	tsec-=(float) mm*60.0;
	printf("|%04d|%02d:%02d:%02d|",nprof,hh,mm,(int)tsec);
      }
      for (i=0; i<nbins; i++) {
	idx=(int) (9.0*(profile[i]-pmin)/prng);
	if (idx<0) idx=0;
	if (idx>9) idx=9;
	putchar(gry[idx]);
      }
      puts("|");
      free(profile);
      fgets(line,sizeof(line),input);
      nbins=0;
      nprof++;
    }
  }
}
Exemplo n.º 5
0
main (int argc, char **argv)
{
  int i=1, j, k, nfiles=0, *numbt, schans=0, nbytes, *nchan;
  FILE *input[32];
  char *block;
  double *stamp, *frch1, *froff, *frmhz;
  output=stdout;
  /* print help if necessary */
  if (argc<=1 || help_required(argv[1])) {
    /*splice_help();*/
    exit(0);
  } else {
    print_version(argv[0],argv[1]);
  }

  /* open up files */
  while (i<argc) {
    if (file_exists(argv[i])) {
      input[nfiles]=open_file(argv[i],"rb");
      nfiles++;
    } else if (strings_equal(argv[i],"-o")) {
      output=open_file(argv[++i],"wb");
    }
    i++;
  }

  /* read in headers and check time stamps */
  stamp = (double *) malloc(nfiles*sizeof(double));
  frch1 = (double *) malloc(nfiles*sizeof(double));
  froff = (double *) malloc(nfiles*sizeof(double));
  numbt = (int *) malloc(nfiles*sizeof(int));
  nchan = (int *) malloc(nfiles*sizeof(int));
  for (i=0; i<nfiles; i++) {
    if (read_header(input[i])) {
      stamp[i]=tstart;
      frch1[i]=fch1;
      froff[i]=foff;
      numbt[i]=nbits;
      nchan[i]=nchans;
      schans+=nchans;
    } else {
      error_message("problem reading header parameters");
    }
    if (data_type != 1) 
      error_message("input data are not in filterbank format!");
    if (stamp[i] != stamp[0]) 
      error_message("start times in input files are not identical!");
    if (numbt[i] != numbt[0])
      error_message("number of bits per sample in input files not identical!");
    if (i>0) {
      if (frch1[i] > frch1[i-1]) 
	error_message("input files not ordered in descending frequency!");
    }

  }


  send_string("HEADER_START");
  send_int("machine_id",machine_id);
  send_int("telescope_id",telescope_id);
  send_int("data_type",1);

  send_string("FREQUENCY_START");
  send_int("nchans",schans);
  frmhz = (double *) malloc(sizeof(double)*schans);
  k=0;
  for (i=0; i<nfiles; i++) {
    for (j=0; j<nchans; j++) {
      frmhz[k]=frch1[i]+j*froff[i];
      send_double("fchannel",frmhz[k++]);
    }
  }
  send_string("FREQUENCY_END");
  

  if (!strings_equal(source_name,"")) {
    send_string("source_name");
    send_string(source_name);
  }
  send_coords(src_raj,src_dej,az_start,za_start);
  send_int("nbits",nbits);
  send_double("tstart",tstart);
  send_double("tsamp",tsamp);
  send_int("nifs",nifs);
  send_string("HEADER_END");

  nbytes = nchans*nbits/8;
  block = (char *) malloc(nbytes);
  while (1) {
    for (i=0; i<nfiles; i++) {
      if (feof(input[i])) exit(0);
      fread(block,nbytes,1,input[i]);
      fwrite(block,nbytes,1,output);
    }
  }
}
Exemplo n.º 6
0
main(int argc, char **argv) 
{
  char message[80];
  int i;
  float fmhz=430.0, bw=7.68, nch=128.0, dm=50.0, pms=150.0,
        wms=5.0, tms=0.08, tsc=0.0, mdm=0.0, nrm=100.0;
  float xdm, wef, snr, tdm, tdd, snrmax=0.0;
  FILE *input;
  if (argc > 1) {
    print_version(argv[0],argv[1]);
    if (help_required(argv[1])) {
      /*snrdm_help();*/
      exit(0);
    }
    i=1;
    while (i<argc) {
      if (file_exists(argv[i])) {
	input=open_file(argv[i],"r");
      } else if (strings_equal(argv[i],"-f")) {
        fmhz=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-b")) {
        bw=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-c")) {
        nch=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-d")) {
        dm=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-p")) {
        pms=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-w")) {
        wms=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-t")) {
        tms=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-s")) {
        tsc=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-m")) {
        mdm=atof(argv[++i]);
      } else if (strings_equal(argv[i],"-n")) {
        nrm=atof(argv[++i]);
      } else {
	sprintf(message,"command-line argument %s not recognized...",argv[i]);
	error_message(message);
      }
      i++;
    }
  }
  if (mdm == 0.0) mdm=2.0*dm;
  tdm=(float) dmdelay(fmhz-bw/nch,fmhz,dm)*1000.0;
  for (xdm=0.0;xdm<mdm;xdm+=0.1) {
    tdd=(float) dmdelay(fmhz-bw/2.0,fmhz+bw/2.0,abs(dm-xdm))*1000.0;
    wef=sqrt(wms*wms+tsc*tsc+tms*tms+tdm*tdm+tdd*tdd);
    snr=sqrt((pms-wef)/wef);
    snrmax=(snr>snrmax)?snr:snrmax;
  }
  printf("#Signal-to-noise/DM curve assuming following parameters:\n");
  fprintf(stderr,"#Pulse Period %.3f ms\n",pms);
  fprintf(stderr,"#Intrinsic Width %.3f ms\n",wms);
  fprintf(stderr,"#Scattering Time %.3f ms\n",tsc);
  fprintf(stderr,"#True DM %.3f pc/cc\n",dm);
  fprintf(stderr,"#Centre Frequency %.3f MHz\n",fmhz);
  fprintf(stderr,"#Bandwidth %.3f MHz\n",bw);
  fprintf(stderr,"#Number of Channels %d\n",nch);
  printf("#START\n");
  for (xdm=0.0;xdm<mdm;xdm+=0.1) {
    tdd=(float) dmdelay(fmhz-bw/2.0,fmhz+bw/2.0,abs(dm-xdm))*1000.0;
    wef=sqrt(wms*wms+tsc*tsc+tms*tms+tdm*tdm+tdd*tdd);
    if (wef>pms) 
      snr=0.0;
    else
      snr=nrm*sqrt((pms-wef)/wef)/snrmax;
    printf("%.2f %f\n",xdm,snr);
  }
  printf("#STOP\n");
  printf("#DONE\n");
}
Exemplo n.º 7
0
main (int argc, char *argv[]) 
{
  float clip_level, *blk, sum, mean, blksum, blksum2, blkmean, blkmean2;
  unsigned char *cblk;
  unsigned short *sblk;
  float blksigma, dmean, dsigma;
  int blksize, nread, nsblk, i, headersize, c, s, offset, obits;
  long int isamp=0, seed=0, nclip=0;

  if (argc<2 || help_required(argv[1])) {
    //blanker_help();
    exit(0);
  }
  print_version(argv[0],argv[1]);
  if (!file_exists(argv[1]))
    error_message("input file does not exist!");

  strcpy(inpfile,argv[1]);
  input=open_file(inpfile,"r");
  strcpy(outfile,"stdout");
  output=stdout;
  nsblk=1024;
  clip_level=6.0;
  obits=32;

  i=2;
  while (i<argc) {
    if (strings_equal(argv[i],"-c"))       clip_level=atof(argv[++i]);
    if (strings_equal(argv[i],"-s"))       nsblk=atof(argv[++i]);
    if (strings_equal(argv[i],"-b"))       obits=atoi(argv[++i]);
    i++;
  }

  if ((headersize=read_header(input))) {

    if (nifs>1) error_message("blanker can only handle nifs=1");

    send_string("HEADER_START");
    if (!strings_equal(source_name,"")) {
      send_string("source_name");
      send_string(source_name);
    }
    send_int("telescope_id",telescope_id); 
    send_int("machine_id",machine_id);
    send_coords(src_raj,src_dej,az_start,za_start);
    send_int("data_type",data_type);
    send_double("fch1",fch1);
    send_double("foff",foff);
    send_int("barycentric",barycentric);
    send_int("nchans",nchans);
    send_int("nbits",obits);  
    send_double ("tstart",tstart); 
    send_double("tsamp",tsamp);
    send_int("nifs",nifs);
    send_string("HEADER_END");

    
    blksize=nchans*nsblk;
    blk = (float *) malloc(blksize*sizeof(float));
    cblk = (unsigned char *) malloc(blksize*sizeof(unsigned char));
    sblk = (unsigned short *) malloc(blksize*sizeof(unsigned short));
    
    while (nread=read_block(input,nbits,blk,blksize)) {

      /* calculate mean and sigma of the whole block */
      blksum=blksum2=0.0;
      for (i=0; i<nread; i++) {
	blksum+=blk[i];
	blksum2+=blk[i]*blk[i];
      }
      blkmean=blksum/(float)nread;
      blkmean2=blksum2/(float)nread;
      blksigma=sqrt(blkmean2-blkmean*blkmean);
      /* expected mean and sigma of a dedispersed sample in this block */
      dmean=blkmean*(float)nchans;
      dsigma=blksigma*sqrt((float)nchans);

      /* now work through each sample in the block */
      nsblk=nread/nchans;
      offset=0;
      for (s=0; s<nsblk; s++) {
	isamp++;
	sum=0.0;
	for (c=0; c<nchans; c++) sum+=blk[offset+c];
	if (abs(sum-dmean)>clip_level*dsigma) {
	  nclip++;
	  for (c=0; c<nchans; c++) 
	    blk[offset+c]=gasdev(&seed)*blksigma+blkmean;
	}
	offset+=nchans;
      }

      /* write out the clipped data (32 bit!) */
      if (obits == 32) 
	fwrite(blk,sizeof(float),nread,output);
      } else if (obits == 8) {
	for (i=0;i<nread;i++) cblk[i]=blk[i];
	fwrite(cblk,sizeof(unsigned char),nread,output);
      } else if (obits == 16) {
	for (i=0;i<nread;i++) sblk[i]=blk[i];
	fwrite(sblk,sizeof(unsigned short),nread,output);
      } else {
	error_message("only 8, 16 or 32 bit output modes supported");
      }
    }
Exemplo n.º 8
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);
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
0
/*
char *telescope_name(int telescope_id);
char *data_category(int data_type);
char *backend_name(int machine_id);
void angle_split(double angle,int *dd,int *mm,double *ss);
void cal(double djm,int *year,int *month,int *day);
long numsamples(FILE *filename,int headersize,int nbits,int nifs,int nchans);
*/
int main(int argc,char *argv[]){

  FILE *fil;
  int i;
  char key_name[N]="";
  int key_length;
  int size_int=sizeof(int),size_double=sizeof(double);
  int nsamples=0;
  long numsamps;
  int size_header=0;
  long size_data;
  int dd,mm;
  double ss;
  int year,month,day;
  double total_obs;
  double low_channel,high_channel,total_bandwidth;
  char unit[N];
  int index_chan=0;

  if(help_required(argv[1])){
	  help_header();
	  exit(-1);
  }

//Default value of key parameters

//  rawdatafile="";
//  source_name="uknown";
  src_raj=0;
  src_dej=0;
  telescope_id=11; //Telescope="uknown"
  machine_id=11; //Datataking Machine="uknown"
  data_type=11; //Data file="uknown"
  barycentric=0; //Barycentric="No"
  pulsarcentric=0; //pulsarcentric="Yes"
  az_start=0;
  za_start=0;
  tstart=0;
  tsamp=0;
  nbits=0;
  nsamples=0;
  total_obs=0;
  ibeam=0;
  nbeams=0;
  nifs=0;
  low_channel=0;
  high_channel=0;
  foff=0;
  nchans=0;
  total_bandwidth=0;
  refdm=0;
  size_data=0;
  size_header=0; 

//END

  if((fil=fopen(argv[1],"rb")) == NULL){
    fprintf(stderr,"Error:Cannot open file!\n");
    exit(-1);
  }
//read HEADER_START of *.fil
  if(fread(&key_length,size_int,1,fil) < 1){
    fprintf(stderr,"Error read key_length\n");
    exit(-1);
  }
  
  size_header=size_header+size_int*1;

  if((fgets(key_name,key_length+1,fil)) == NULL){
    fprintf(stderr,"Error:Failed to read key_name\n");
    exit(-1);
  }

  size_header=size_header+key_length*1;
  
  if(strcmp(key_name,"HEADER_START")){
    fprintf(stderr,"Error:Failed to read header\n");
    exit(-1);
  }

//read HEADER of *.fil 
  while(1){

    if(fread(&key_length,size_int,1,fil) < 1){  
      fprintf(stderr,"Error read key_length\n");
      exit(-1);
    }

    size_header=size_header+size_int*1;

    if((fgets(key_name,key_length+1,fil)) == NULL){
      fprintf(stderr,"Error:Failed to read key_name\n");
      exit(-1);
    }

    size_header=size_header+key_length*1;
//find key_parameters
    if(strcmp(key_name,"telescope_id") == 0){
      if(fread(&telescope_id,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read telescope_id\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"FREQUENCY_START") == 0){
      index_chan=0;
    }
    else if(strcmp(key_name,"FREQUENCY_END") == 0){
      fch1=0;
      foff=0;
    }
    else if(strcmp(key_name,"fchannel") == 0){
      if(fread(&frequency_table[index_chan++],size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read fchannel\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"machine_id") == 0){
      if(fread(&machine_id,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read telescope_id\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"data_type") == 0){
      if(fread(&data_type,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read data_type\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nchans") == 0){
      if(fread(&nchans,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nchans\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nbits") == 0){
      if(fread(&nbits,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nbits\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nifs") == 0){
      if(fread(&nifs,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nifs\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"scan_number") == 0){
      if(fread(&scan_number,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read scan_number\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"barycentric") == 0){
      if(fread(&barycentric,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read barycentric\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"pulsarcentric") == 0){
      if(fread(&pulsarcentric,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read pulsarcentric\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"tstart") == 0){
      if(fread(&tstart,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read tstart\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"mjdobs") == 0){
      if(fread(&mjdobs,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read mjdobs\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"tsamp") == 0){
      if(fread(&tsamp,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read tsamp\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"fch1") == 0){
      if(fread(&fch1,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read fch1\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"foff") == 0){
      if(fread(&foff,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read foff\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"refdm") == 0){
      if(fread(&refdm,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read refdm\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"az_start") == 0){
      if(fread(&az_start,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read az_start\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"za_start") == 0){
      if(fread(&za_start,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read za_start\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"src_raj") == 0){
      if(fread(&src_raj,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read src_raj\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"src_dej") == 0){
      if(fread(&src_dej,size_double,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read src_dej\n");
        exit(-1);
      }
      size_header=size_header+size_double*1;
    }
    else if(strcmp(key_name,"ibeam") == 0){
      if(fread(&ibeam,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read ibeam\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nbeams") == 0){
      if(fread(&nbeams,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nbeams\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"nsamples") == 0){
      if(fread(&nsamples,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read nsamples\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
    }
    else if(strcmp(key_name,"rawdatafile") == 0){
      if(fread(&key_length,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read rawdatafile_length\n");
        exit(-1);
      }  
      size_header=size_header+size_int*1;    
      if(fgets(rawdatafile,key_length+1,fil) ==NULL){
        fprintf(stderr,"Error:Failed to read rawdatafile\n");
      }
      size_header=size_header+key_length*1;
    }
    else if(strcmp(key_name,"source_name") == 0){
      if(fread(&key_length,size_int,1,fil) < 1){
        fprintf(stderr,"Error:Failed to read source_name_length\n");
        exit(-1);
      }
      size_header=size_header+size_int*1;
      if(fgets(source_name,key_length+1,fil) ==NULL){
        fprintf(stderr,"Error:Failed to read source_name\n");
      }
      size_header=size_header+key_length*1;
    }
    else if(strcmp(key_name,"HEADER_END") == 0) break;
    else{
      fprintf(stderr,"Error:Failed to recognize key parameter\n");
    }
  }
//Data file
  if(strcmp(rawdatafile,"") != 0)
    printf("%-38s : %s\n","Data file",rawdatafile);
  else
    printf("%-38s : %s\n","Data file",argv[1]);
//Source name
  printf("%-38s : %s\n","Source Name",source_name);
//Source RA (J2000)
  angle_split(src_raj,&dd,&mm,&ss);
  printf("%-38s : %d:%d:%.1lf\n","Source RA (J2000)",dd,mm,ss);
//Source DEC (J2000)
  angle_split(src_dej,&dd,&mm,&ss);
  printf("%-38s : %d:%d:%.1lf\n","Source DEC (J2000)",dd,mm,ss);
//Telescope
  printf("%-38s : %s\n","Telescope",telescope_name(telescope_id));
//Datataking Machine
  printf("%-38s : %s\n","Datataking Machine",backend_name(machine_id));
//Data type
  printf("%-38s : %s\n","Data type",data_category(data_type));
//Barycentric
  if(barycentric==1)
    printf("%-38s : %s\n","Barycentric","Yes");
  else
    printf("%-38s : %s\n","Barycentric","No");
//Pulsarcentric
  if(pulsarcentric==1)
    printf("%-38s : %s\n","Pulsarcentric","Yes");
  else
    printf("%-38s : %s\n","Pulsarcentric","No");
//Telescope azimuth angle (degrees)
    printf("%-38s : %lf\n","Telescope azimuth angle (degrees)",az_start);
//Telescope zenith angle (degrees)
    printf("%-38s : %lf\n","Telescope zenith angle (degrees)",za_start);
//Time stamp of first sample (MJD)
    printf("%-38s : %.12lf\n","Time stamp of first sample (MJD)",tstart);
//Gregorian date
  cal(tstart,&year,&month,&day);
  printf("%-38s : %d/%.2d/%.2d\n","Gregorian date",year,month,day);
//Sample time (us)
    printf("%-38s : %lf\n","Sample time (us)",tsamp*1.0e6);
//Number of bits per sample
    printf("%-38s : %d\n","Number of bits per sample",nbits);
//Number of samples //Warning:nifs*nchans*nsamples should be even;
  if(nsamples == 0){
    numsamps=numsamples(fil,size_header,nbits,nifs,nchans);
    size_data=(long)nifs*nchans*numsamps*nbits/8;
    total_obs=numsamps*tsamp;
    printf("%-38s : %ld\n","Number of samples",numsamps);
  }
  else{
    printf("%-38s : %d\n","Number of samples",nsamples);
    size_data=nifs*nchans*nsamples*nbits/8;
    total_obs=nsamples*tsamp;
  }
//Observation length
  strcpy(unit,"(s)");
  if(total_obs>60.0){
    total_obs/=60.0;
    strcpy(unit,"(m)");
    if(total_obs>60.0){
      total_obs/=60.0;
      strcpy(unit,"(h)");
      if(total_obs>24.0){
        total_obs/=24.0;
        strcpy(unit,"(d)");
      }
    }
  }
  printf("%s %-19s : %lf\n","Observation length",unit,total_obs);
//Beam number
  printf("%-38s : %d\n","Beam number",ibeam);
//Number of beams
  printf("%-38s : %d\n","Number of beams",nbeams);
//Number of IFs
  printf("%-38s : %d\n","Number of IFs",nifs);
//frequency table
  if(index_chan > 0){
    printf("%-38s : ","Frequency");
    for(i=0;i<index_chan;i++){
      printf("%lf ",frequency_table[i]);
    }
  }
  else{
//Low channel (MHz)
    if(foff < 0)
      low_channel=fch1+(nchans-1)*foff;
    else if(foff > 0)
      low_channel=fch1;
    printf("%-38s : %lf\n","Low channel (MHz)",low_channel);
//High channel (MHz)
    if(foff > 0)
      high_channel=fch1+(nchans-1)*foff;
    else if(foff < 0)
      high_channel=fch1;
    printf("%-38s : %lf\n","High channel (MHz)",high_channel);
//Central freq (MHz)
    printf("%-38s : %lf\n","Central freq (MHz)",(high_channel+low_channel)/2.);
//Channel bandwidth (MHz)
    printf("%-38s : %lf\n","Channel bandwidth (MHz)",foff);  
//Number of channels
    printf("%-38s : %d\n","Number of channels",nchans);
//Total Bandwidth (MHz)
    total_bandwidth=fabs(foff)*nchans;
    printf("%-38s : %lf\n","Total Bandwidth (MHz)",total_bandwidth);
  }
//Reference dispersion measure (cm-3 pc)
  printf("%-38s : %lf\n","Reference dispersion measure (cm-3 pc)",refdm);
//Data size (bytes)
  printf("%-38s : %ld\n","Data size (bytes)",size_data);
//Header size (bytes)
  printf("%-38s : %d\n","Header size (bytes)",size_header);


  fclose(fil);

return 0;
}