Example #1
0
void readhd_(char filename[80], float *rdm, float *rtsmp, char ra[12], char dec[12], float *fref, double *mjdstart, char srcname[80], char telname[80])
{
  int i;
  int rah,ram,ded,dem;
  double ras,des;
  char decsign,sra[6],sde[6];
  /* first fix filename */
  for (i=0; i<strlen(filename); i++) {
    if (filename[i]==' ') {
      filename[i]='\0';
      break;
    }
  }

  /* now open up the file and read in the header */
  refdm=tsamp=0.0;
  timfile=open_file(filename,"rb");
  read_header(timfile);
  strcpy(telname,telescope_name(telescope_id));
  *fref=(float)fch1;
  *mjdstart=tstart;
  *rtsmp=(float) tsamp;
  *rdm=(float) refdm;
  angle_split(src_raj,&rah,&ram,&ras);
  strcpy(srcname,source_name);
  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);
  }
  sprintf(ra,"%02d:%02d:%s",rah,ram,sra);
  sprintf(dec,"%c%02d:%02d:%s",decsign,abs(ded),dem,sde);
  if (nbits != 32 && nbits != 8 && nbits != 16) {
    fprintf(stderr,"cannot read %d bit data..\n");
    exit(1);
  }
}
Example #2
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 #3
0
void machine2prf(FILE *input, FILE *output) /* includefile */
{
  int i,b,c,dayno,day=0,month=0,year=0,subscan=0,scanno=0,nprds,nchans0;
  int *chtab,*ignore;
  unsigned long *iprofile;
  char  *telname,*catref,*bibref,*paflag,*timflag,*srcname;
  double dm,rm,*chanfreq,pra,pde,df;
  double phase,psrfreq,phase0;
  float *profile,opos,offset=0.0,scale=1.0,rms=0.0;
  struct EPN epn;

  if (data_type == 3) {
    /* SIGPROC binary profile format */
    wapp_off=0;
    srcname=source_name;
    fmid=fch1+fabs(foff)/2.0+foff*((float)nchans)/2.0;
    foff=fabs(foff);
    profile = (float *) malloc(sizeof(float)*nifs*nchans*nbins);
    fread(profile,sizeof(float),nifs*nchans*nbins,input);
    mjdobs=floor(tstart);
    tstart=(tstart-mjdobs)*86400.0;
    phase=0.0;
    nprds=npuls;
    dm=0.0;
  } else {
    /* set up header variables and read in data depending on the machine */
    switch (machine_id) {
    case 1:
      /* PSPM data */
      wapp_off=0;
      fmid=pspm_timing.freq; 
      foff=0.062;
      df=7.68;
      nbins=pspm_timing.num_phase_bins;
      nchans=pspm_timing.num_chans;
      period=pspm_timing.psr_period;
      read_aoscan(pspm_timing.scan_num, &dayno, &year, &subscan);
      dm = pspm_timing.psr_dm;
      chtab=pspm_chans(nchans);
      profile=pspm_prof(input,nbins,nchans,chtab);
      nifs=1;
      nprds=pspm_timing.num_periods;
      srcname=pspm_timing.psr_name+3;
      pra=pspm_timing.psmon_ra;
      pde=pspm_timing.psmon_dec;
      break;
    case 2:
      /* WAPP data */
      fmid=wapp->cent_freq;
      if(fcorrect>0.) fmid = fcorrect;
      foff=-(1-2*wapp_inv)*wapp->bandwidth/wapp->num_lags;
      df=wapp->bandwidth;
      nbins=wapp->nbins;
      nchans=wapp->num_lags;
      nifs=wapp->nifs;
      period=1.0/wapp->psr_f0[0];
      dm = wapp->psr_dm;
      nprds=1;
      pra=wapp->src_ra;
      pde=wapp->src_dec;
/*      if(profnum2 > 1000) profnum2 = wapp_ndumps; */
      profile=wapp_prof(nbins,nchans,nifs,profnum1,profnum2);
      srcname=wapp->src_name;
/*      tstart += profnum1*wapp->dumptime; */
      if((fabs(period-0.040))>0.001) { /* not a cal scan */
	phcalc(mjdobs,tstart,&phase0,&psrfreq,wapp->rphase,wapp->psr_f0,
	       wapp->poly_tmid,wapp->coeff,wapp->num_coeffs);
	tstart += wapp->dumptime/2.;
      /* Actually midpoint, not start, of combined dumps */
        tstart += (profnum1+profnum2)/2.*wapp->dumptime;
        phcalc(mjdobs,tstart,&phase,&psrfreq,wapp->rphase,wapp->psr_f0,
               wapp->poly_tmid,wapp->coeff,wapp->num_coeffs); 
        period = 1./psrfreq;
/*	printf("Phases 1: %f %f\n",phase, phase0); */
        phase -= phase0;  /* first sample went into bin 0 */
/*	printf("Phases 2: %f %f\n",phase, phase0);  */
        while (phase < 0.) phase += 1.;
      }
      break;
    case 4:
      /* BPP data */
      nbins=bpp_timing.num_phase_bins;
      nchans=bpp_timing.num_chans;
      period=bpp_timing.psr_period;
      read_aoscan(bpp_timing.scan_num, &dayno, &year, &subscan);
      dm = bpp_timing.psr_dm; 
      chtab=bpp_chans(bpp_timing.bandwidth,bpp_timing.mb_start_address,
		      bpp_timing.mb_end_address,bpp_timing.mb_start_board,
		      bpp_timing.mb_end_board,bpp_timing.cb_id,
		      bpp_timing.aib_los,bpp_timing.dfb_sram_freqs,
		      bpp_timing.rf_lo);
      df=foff*nchans;
      profile=pspm_prof(input,nbins,nchans,chtab);
      nifs=1;
      nprds=bpp_timing.num_periods;
      srcname=bpp_timing.psr_name+3;
      pra=pde=0.0;
      break;
    default:
      error_message("unknown machine!");
      break;
    }
  }

  /* user can specify a DM to dedisperse at (default is to value in header) */
  if (usrdm) dm=userdm;

  /* user can specify number of bins in output profiles (decimation) */
  if (userbins==0) userbins=nbins;

  /* set up number of dedisperse subbands (default is 1) */
  if (nbands==0) nbands=nchans;

  /* save starting number of channels */
  nchans0=nchans;

  /* calculate sky frequency of each channel */
  /*  chanfreq=chan_freqs(fmid,fabs(foff),nchans); */
  chanfreq=chan_freqs(fmid,foff,nchans,wapp_off);

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

  /* check to see if reference frequency has been flagged at the command line*/
  if (refrf == -1.0) refrf=fmid; /* use mid frequency if this is the case */

  /* dedisperse profiles if requested and scale by Jyfactors if two-pol data */
  if (dm>0.0) 
    prof_ddis(profile,nbins,nchans,nbands,nifs,chanfreq,period,dm,refrf,jyf1,jyf2);

  /* sum profiles to form subbands if requested */
  if (nbands<nchans) prof_sumc(profile,nbins,nbands,&nchans,nifs,ignore);

  /* decimate profiles if requested */
  if (nbins>userbins) prof_adds(profile,&nbins,nchans,nifs,nbins/userbins);
  
  /* sum IFs if requested -- this alters nifs to 1 */
  if (sumifs) prof_sumifs(profile,nbins,nchans,&nifs);

  /* subtract baseline (median of each profile) if requested */
  if (baseline) {
    prof_sbas(srcname,profile,nbins,nchans,nifs); 
  } else {
    /* no baseline subtraction but normalise by number of periods/channels
       to get units in terms of the digitization range (e.g. 0-15 PSPM) */
    //    for (i=0;i<nbins*nchans*nifs;i++) profile[i]/=(float)nchans0*nprds/nbands;
  }

  /* set up sampling time (us) for output profile (nbins may be different) */
  tsamp=1.0e6*period/nbins;

  /* multiply outgoing profiles by single Jansky calibration factor if supplied */
  if (jyfactor != 1.0) for (i=0;i<nbins*nifs*nchans;i++) profile[i]*=jyfactor;

  if (ascii && !stream) {
    /* ascii output format requested */
    for (i=0;i<nifs;i++) {
      for (c=0;c<nchans;c++) {
	if (!headerless) 
        fprintf(output,"# %.1f %.7f %.10f %d %.3f %.3f %d %c %d %s %.8f\n",
        (float)mjdobs,tstart,period,nprds,chanfreq[c],dm,nbins,tempo_site(telescope_id),subscan,
        srcname,phase);
	for (b=0;b<nbins;b++) 
	  fprintf(output,"%d %f\n",b,profile[c*nbins*nifs+i*nbins+b]);
      }
    }
  } else if (asciipol) {
    /* write profiles in format for Jim's polarization code */
    for (b=0;b<nbins;b++) 
      for (i=0;i<nifs;i++)
	for (c=0;c<nchans;c++) 
	 fprintf(output,"%d %d %d %f\n",b,i,c,profile[c*nbins*nifs+i*nbins+b]);
  } else if (stream) {
    for (i=0;i<nifs;i++) {
      for (c=0;c<nchans;c++) {
	fprintf(output,"#START %d %f %f\n",nbins,tstart,chanfreq[c]);
	for (b=0;b<nbins;b++) {
	 fprintf(output,"%d %f\n",b,profile[c*nbins*nifs+i*nbins+b]);
	}
	fprintf(output,"#STOP\n");
      }
    }
    fprintf(output,"#DONE\n");
  } else {
    /* EPN format requested - set up some general EPN variables */
    sprintf(epn.history,"%s %s timing-mode data reduced using dedisperse",
	    telescope_name(telescope_id),backend_name(machine_id));
    while (strlen(epn.history)<65) strcat(epn.history," ");
    strcpy(epn.jname,srcname);
    strcpy(epn.cname,srcname);
    epn.pbar=period;
    epn.dm=dm;
    epn.rm=0.0;
    strcpy(epn.catref,"none");
    strcpy(epn.bibref,"none");
    epn.raj=pra;
    epn.dec=pde;
    strcpy(epn.telname,telescope_name(telescope_id));
    epn.epoch=(float)mjdobs;
    epn.opos=0.0;
    epn.paflag=' ';
    epn.timflag='U';
    epn.xtel=0.0;
    epn.ytel=0.0;
    epn.ztel=0.0;
    epn.day=0;
    epn.month=0;
    epn.year=year;
    epn.scanno=scanno;
    epn.subscan=subscan;
    epn.npol=nifs;
    epn.nfreq=nchans;
    epn.nbins=nbins;
    epn.tbin=1.0e6*period/(double)nbins;
    epn.nint=0;
    epn.ncal=0;
    epn.lcal=0;
    epn.tres=epn.tbin;
    epn.fluxflag='U';
    epn.navg=nchans0/nbands;
    strcpy(epn.uf,"MHz ");
    epn.df=df;
    df/=(double)nbands;
    epn.df=df*1000.0;
    strcpy(epn.ud,"kHz ");
    if (epn.df>=10000.0) {
      epn.df/=1000.0;
      strcpy(epn.ud,"MHz ");
    }
    epn.tstart=tstart;
    epn.iprofile=(unsigned long *) malloc(epn.nbins*sizeof(long));
    /* loop over IFs and channels writing an EPN record foreach profile */
    for (i=0;i<nifs;i++) {
      strcpy(epn.idfield,"I");
      for (c=0;c<nchans;c++) {
	epn.f0=fch1-foff*c;
	epn.nband=c+1;
	epn.papp=period;
	scale_prof(profile,nbins,epn.iprofile,&epn.scale,&epn.offset); 
	epn.rms=0.0;
	write_epn(output,epn);
	profile+=nbins;
      }
    }
  }
}
Example #4
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);
}
Example #5
0
void write_profiles(float *prof,int nbins, int nchan, int nifs, FILE *out)/*includefile*/
{
  int i,j,c,b,k,m,year,month,day;
  float *profile,scale,offset,rms,epoch;
  double usec,tbin,tres,psec;
  static int first=1;
  struct EPN epn;

  int sta=0;
  short int sj;
  float x,binned_freq[16384],binned_weight[16384],
    binned_offset[16384],binned_scale[16384],binned_data[16384];
  int bitpix=8, naxis=0, nrcvr, nrows, ncols, col;
  static int subint_cnt;
  char datestr[10],timestr[8];
  char Cstr16[16], Estr16[16], Istr16[16];
  char *ttype[20], *tform[20], *tunit[20];
  long naxes[4];
  int junk,rah,ram,ded,dem,subint_hdu,hh,mm;
  float ss;
  int last_scanhdr_hdu;
  double ras,des,dx;
  char rastr[80], destr[80], sra[80], sde[80];
  char date_time[24];
  char *pch[MAX_BLKS];
  char site[MAX_BLKS][2];
  short int nspan[MAX_BLKS];
  short int ncoeff[MAX_BLKS];
  double rfreq[MAX_BLKS];
  double rmjd[MAX_BLKS];
  double rphase[MAX_BLKS];
  double lgfiterr[MAX_BLKS];
  double f0[MAX_BLKS];
  double coeff[MAX_BLKS][MAX_COEFF];
  static double srcl, srcb;
  /* For Pulsar History BINTABLE */
 
  static char *PHtype[18] = {
    "DATE_PRO","PROC_CMD","POL_TYPE","NPOL    ","NBIN    ","NBIN_PRD","TBIN    ","CTR_FREQ",
    "NCHAN   ","CHAN_BW ","PAR_CORR","RM_CORR ","DEDISP  ","DDS_MTHD","SC_MTHD ","CAL_MTHD",
    "CAL_FILE","RFI_MTHD"
  };
  
  static char *PHform[18] = {
    "24A     ","80A     ","8A      ","1I      ","1I      ","1I      ","1D      ","1D      ",
    "1I      ","1D      ","1I      ","1I      ","1I      ","32A     ","32A     ","32A     ",
    "32A     ","32A     "
  };
  
  static char *PHunit[18] = {
    "        ","        ","        ","        ","        ","        ","s       ","MHz     ",
    "        ","MHz     ","        ","        ","        ","        ","        ","        ",
    "        ","        "
  };

  /* subtract baseline from outgoing profiles if requested */
  if (userbase != 0.0) for (i=0;i<multiple*nbins*nifs*nchans;i++) 
    prof[i]-=userbase;

  /* multiply outgoing profiles by Jansky calibration factor if supplied */
  if (jyfactor != 0.0) for (i=0;i<multiple*nbins*nifs*nchans;i++) 
    prof[i]*=jyfactor;

  /* sum first two polarizations together if requested */
  if (totalpower && (nifs > 1)) {
    for (i=0;i<multiple*nbins*nchans;i++) 
      prof[i]+=prof[i+multiple*nbins*nchans];
    nifs=1;
  }

  if (binary) {
    /* write out profiles in binary format */
    folding_period=pfld0*1000.0;
    tadd=tstart;
    tstart=tstart+tsta/86400.0;
    npuls=pulsecount;
    fold_header();
    tstart=tadd;
    for (i=0; i<nifs; i++) {
      for (c=0; c<nchan; c++) {
	for (b=0; b<nbins; b++) 
	  fwrite(&prof[i*nchan*nbins+c*nbins+b],sizeof(float),1,out);
      }
    }
  } else if (stream) {
    /* write out profiles as ASCII streams with START/STOP boundaries */
    k=0;
    for (i=0;i<nifs;i++) {
      for (c=0;c<nchan;c++) {
	fprintf(out,"#START %d %f %f\n",nbins,tsta,fch1+foff*(float)c);
	for (m=0;m<multiple;m++) {
	  for (b=0;b<nbins;b++) {
	    fprintf(out,"%d %f\n",b+m*nbins,prof[i*nchan*nbins+c*nbins+b]);
	  }
	}
	fprintf(out,"#STOP\n");
      }
    }
  } else if (asciipol) {
    /* write profiles in format for Jim's polarization code */
    for (b=0;b<nbins;b++) 
      for (i=0;i<nifs;i++)
	for (c=0;c<nchan;c++) 
	  fprintf(out,"%d %d %d %f\n",b,i,c,prof[i*nchan*nbins+c*nbins+b]);
  } else if (ascii) {
    fprintf(output,"# %.1f %.7f %.10f %ld %.3f %.3f %d %c %d %s\n",
     floor(tstart),(tstart-floor(tstart))*86400.0+tsta,pfld0,pulsecount,fch1,refdm,nbins,tempo_site(telescope_id),1,source_name);
    for (b=0;b<nbins;b++) {
      fprintf(out,"%d",b+1);
      for (i=0;i<nifs;i++) {
	for (c=0;c<nchan;c++) fprintf(out," %f",prof[i*nchan*nbins+c*nbins+b]);
      }
      fprintf(out,"\n");
    }
#ifdef PSRFITS
  } else if (psrfits) {
    if (first) {
      first=0;
      /* write profile in PSRFITS format */
      fits_create_file(&fits, "stdout", &sta);
      fits_create_img(fits,bitpix,naxis,naxes,&sta);
      fits_write_date(fits,&sta);
      /* Get DateTime string - required in various BINTABLEs */
      fits_get_system_time(date_time,&junk,&sta);
      fits_update_key(fits,TSTRING,"HDRVER","1.19","Header version",&sta);
      fits_update_key(fits,TSTRING,"OBSERVER",culprits,
		      "Observer name(s)",&sta);
      fits_update_key(fits,TSTRING,"PROJID",project,"Project name",&sta);
      fits_update_key(fits,TSTRING,"TELESCOP",telescope_name(telescope_id),
		      "Telescope name", &sta);
      fits_update_key(fits,TSTRING,"BACKEND",backend_name(machine_id),
		      "Backend ID",&sta);
      if (nifs>1) 
	nrcvr=2;
      else
	nrcvr=1;
      fits_update_key(fits,TINT,"NRCVR",&nrcvr,
		      "Number of receiver channels (I)",&sta);
      fits_update_key(fits,TSTRING,"OBS_MODE", "PSR",
		      "(PSR, CAL, SEARCH)", &sta);
      fits_update_key(fits,TSTRING,"SRC_NAME", source_name,
		      "Source or scan ID", &sta);
      fits_update_key(fits,TSTRING,"COORD_MD", "J2000",
		      "Coordinate mode (J2000, Gal, Ecliptic, etc.)", &sta);
      
      angle_split(src_raj,&rah,&ram,&ras);
      if (ras<10.0) 
	sprintf(sra,"0%.3f",ras);
      else
	sprintf(sra,"%.3f",ras);
      sprintf(rastr,"%02d:%02d:%s",rah,ram,sra);
      angle_split(src_dej,&ded,&dem,&des);
      if (des<10.0) 
	sprintf(sde,"0%.3f",des);
      else
	sprintf(sde,"%.3f",des);
      sprintf(destr,"%02d:%02d:%s",ded,dem,sde);
      cel2gal(rah,ram,ras,ded,dem,des,&srcl,&srcb);

      fits_update_key(fits, TSTRING, "STT_CRD1", rastr,
		      "Start coord 1 (hh:mm:ss.sss or ddd.ddd)", &sta);
      fits_update_key(fits, TSTRING, "STT_CRD2", destr,
		      "Start coord 2 (-dd:mm:ss.sss or -dd.ddd)", &sta);
      fits_update_key(fits, TSTRING, "TRK_MODE", "TRACK",
		      "Track mode (TRACK, SCANGC, SCANLAT)", &sta);
      fits_update_key(fits, TSTRING, "STP_CRD1", rastr,
		      "Stop coord 1 (hh:mm:ss.sss or ddd.ddd)", &sta);
      fits_update_key(fits, TSTRING, "STP_CRD2", destr,
		      "Stop coord 2 (-dd:mm:ss.sss or -dd.ddd)", &sta);
      fits_update_key(fits, TSTRING, "CAL_MODE", "OFF",
		      "Cal mode (OFF, SYNC, EXT1, EXT2)", &sta);
      fits_create_tbl(fits,BINARY_TBL,0,18,PHtype,
		      PHform,PHunit,"HISTORY",&sta);
      pch[0] = date_time;
      fits_write_col(fits,  TSTRING, 1, 1, 1, 1, pch, &sta);
      pch[0] = "SIGPROC";
      fits_write_col(fits,  TSTRING, 2, 1, 1, 1, pch, &sta);
      pch[0] = "??";
      fits_write_col(fits,  TSTRING, 3, 1, 1, 1, pch, &sta);
      /* Nr of pols  - i.e. actually written out */
      sj = nifs;
      fits_write_col(fits, TSHORT, 4, 1, 1, 1, &sj, &sta );
      
      /* Nr of bins per product (0 for SEARCH mode) */
      sj = nbins;
      fits_write_col( fits, TSHORT, 5, 1, 1, 1, &sj, &sta );
      
      /* Nr of bins per period */
      fits_write_col( fits, TSHORT, 6, 1, 1, 1, &sj, &sta );
      
      /* Bin time */
      dx =  folding_period/sj;
      fits_write_col( fits, TDOUBLE, 7, 1, 1, 1, &dx, &sta );
      
      /* Centre freq. */
      dx = ((double)(nchans/2)-1.0)*foff+fch1;
      fits_write_col( fits, TDOUBLE, 8, 1, 1, 1, &dx, &sta );
      
      /* Number of channels */
      sj = nchans;
      fits_write_col( fits, TSHORT, 9, 1, 1, 1, &sj, &sta );
      
      /* Channel bandwidth */
      dx = foff;
      fits_write_col( fits, TDOUBLE, 10, 1, 1, 1, &dx, &sta );
      
      /* Create frequency array for later */
      /* Get freq. of first channel */
      if ((fch1==0.0) && (foff==0.0)) {
	for (i=0; i<nchans; i++) binned_freq[i]=(float) frequency_table[i];
      } else {
	for (i=0; i<nchans; i++) 
	  binned_freq[i]=fch1+i*foff;
      }
      
      for (i=0; i<16384; i++) {
	binned_scale[i]=1.0;
	binned_offset[i]=0.0;
	binned_weight[i]=1.0;
      }
      
      sj = 0;
      /* Parallactic angle correction applied */
      fits_write_col( fits, TSHORT, 11, 1, 1, 1, &sj, &sta );
      /* RM correction applied */
      fits_write_col( fits, TSHORT, 12, 1, 1, 1, &sj, &sta );
      /* Data dedispersed */
      fits_write_col( fits, TSHORT, 13, 1, 1, 1, &sj, &sta );
      /* Dedispersion method */
      pch[0] = "NONE";
      fits_write_col( fits, TSTRING, 14, 1, 1, 1, pch, &sta );
      /* Scattered power correction method */
      pch[0] = "NONE";
      fits_write_col( fits, TSTRING, 15, 1, 1, 1, pch, &sta );
      /* Calibration method */
      pch[0] = "NONE";
      fits_write_col( fits, TSTRING, 16, 1, 1, 1, pch, &sta );
      /* Name of calibration file */
      pch[0] = "NONE";
      fits_write_col( fits, TSTRING, 17, 1, 1, 1, pch, &sta );
      /* RFI excision method */
      pch[0] = "NONE";
      fits_write_col( fits, TSTRING, 18, 1, 1, 1, pch, &sta );
      /* Store last header hdu number */
      fits_get_hdu_num( fits, &last_scanhdr_hdu );

      subint_cnt=1;
      /* Add START TIME to primary HDU */
      /* Move to primary HDU */
      fits_movabs_hdu( fits, 1, NULL, &sta );
      cal(tstart,&epn.year,&epn.month,&epn.day);
      sprintf(datestr,"%4d-%02d-%02d",epn.year,epn.month,epn.day);
      fits_update_key( fits, TSTRING, "STT_DATE", datestr,
		       "Start UT date (YYYY-MM-DD)", &sta);
      uttime(tstart,&hh,&mm,&ss);
      sprintf(timestr,"%02d:%02d:%02d",hh,mm,ss);
      fits_update_key( fits, TSTRING, "STT_TIME", timestr,
		       "Start UT (hh:mm:ss)", &sta);
      dx=86400*(tstart-floor(tstart));
      fits_update_key( fits, TINT, "STT_SMJD", &dx,
		     "[s] Start time (sec past UTC 00h) (J)", &sta);
      /* Move to last created HDU in scan header */
      fits_movabs_hdu( fits, last_scanhdr_hdu, NULL, &sta );

      /* Create SUBINT BINTABLE */
      ttype[0] = "ISUBINT ";/* Subint number. If NAXIS=-1, 0 indicates EOD. */
      tform[0] = "1J      ";
      tunit[0] = "";
      ttype[1] = "INDEXVAL";    /* Optionally used if INT_TYPE != TIME */
      tform[1] = "1D      ";
      tunit[1] = "";
      ttype[2] = "TSUBINT ";    /* [s] Length of subintegration */
      tform[2] = "1D      ";
      tunit[2] = "";
      ttype[3] = "OFFS_SUB"; /* [s] Offset from Start UTC of subint centre */
      tform[3] = "1D      ";
      tunit[3] = "";
      ttype[4] = "LST_SUB ";    /* [s] LST at subint centre */
      tform[4] = "1D      ";
      tunit[4] = "";
      ttype[5] = "RA_SUB  ";    /* [turns] RA (J2000) at subint centre */
      tform[5] = "1D      ";
      tunit[5] = "";
      ttype[6] = "DEC_SUB ";    /* [turns] Dec (J2000) at subint centre */
      tform[6] = "1D      ";
      tunit[6] = "";
      ttype[7] = "GLON_SUB";    /* [deg] Gal longitude at subint centre */
      tform[7] = "1D      ";
      tunit[7] = "";
      ttype[8] = "GLAT_SUB";    /* [deg] Gal latitude at subint centre */
      tform[8] = "1D      ";
      tunit[8] = "";
      ttype[9] = "FD_ANG  ";    /* [deg] Feed angle at subint centre */
      tform[9] = "1E      ";
      tunit[9] = "";
      ttype[10] = "POS_ANG ";/*[deg] Position angle of feed at subint centre */
      tform[10] = "1E      ";
      tunit[10] = "";
      ttype[11] = "PAR_ANG ";    /* [deg] Parallactic angle at subint centre */
      tform[11] = "1E      ";
      tunit[11] = "";
      ttype[12] = "TEL_AZ  ";    /* [deg] Telescope azimuth at subint centre */
      tform[12] = "1E      ";
      tunit[12] = "";
      ttype[13] = "TEL_ZEN ";/*[deg] Telescope zenith angle at subint centre */
      tform[13] = "1E      ";
      tunit[13] = "";

      sprintf( Cstr16, "%dE", nchans );
      ttype[14] = "DAT_FREQ";
      tform[14] = Cstr16;
      tunit[14] = "";
      ttype[15] = "DAT_WTS ";
      tform[15] = Cstr16;
      tunit[15] = "";
      
      sprintf( Estr16, "%dE", nifs*nchans );
      ttype[16] = "DAT_OFFS";
      tform[16] = Estr16;
      tunit[16] = "";
      ttype[17] = "DAT_SCL ";
      tform[17] = Estr16;
      tunit[17] = "";
      
      sprintf( Istr16, "%dE", nifs*nchans*nbins );
      ttype[18] = "DATA    ";
      tform[18] = Istr16;
      tunit[18] = "Jy      ";
      
      nrows = 0; /* naxis2 - Let CFITSIO sort this out */
      ncols = 19; /* tfields */
      fits_create_tbl( fits, BINARY_TBL, nrows, ncols, 
		       ttype, tform, tunit, "SUBINT  ", &sta);
      
      /* Add dimensions of column 'ncols' = SUBINT Data */
      naxes[0] = nbins;
      naxes[1] = nchans;
      naxes[2] = nifs;
    
      fits_write_tdim( fits, ncols, 3, naxes, &sta );
      
      /* Add keywords */
      fits_update_key( fits, TSTRING, "INT_TYPE", "TIME",
		       "Time axis (TIME, BINPHSPERI, BINLNGASC, etc)", &sta);
      
      fits_update_key( fits, TSTRING, "INT_UNIT", "SEC",
		       "Unit of time axis (SEC, PHS (0-1), DEG)", &sta);
      
      fits_update_key( fits, TINT, "NCH_FILE", &nchans,
		       "Number of channels/sub-bands in this file (I)", &sta);
      j = 0;
      fits_update_key( fits, TINT, "NCH_STRT", &j,
		     "Start channel/sub-band number (0 to NCHAN-1) (I)", &sta);
    
      /* Store subint hdu number */
      fits_get_hdu_num( fits, &subint_hdu );
    }
    
    /* Write SUBINT BINTABLE columns */
    
    /* Fill in columns of table */
    col = 1;
    
    /* Subint number. If NAXIS=-1, 0 indicates EOD. */
    j=subint_cnt;
    fits_write_col( fits, TINT, col, subint_cnt, 1, 1, &j, &sta );
    col++;
    
    /* INDEXVAL - Optionally used if INT_TYPE != TIME */
    dx = 0.0;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;
    
    /* [s] Length of subint */
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dump_time, &sta );
    col++;
    
    /* [s] Offset from Start UTC of subint centre */
    dx = 0.0;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;
    
    /* [s] LST at subint centre */
    dx=0.0;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;
    
    /* [turns] RA (J2000) at subint centre */
    dx=src_raj/360.0;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;
    
    /* [turns] Dec (J2000) at subint centre */
    dx=src_dej/360.0;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;
    
    /* [deg] Gal longitude at subint centre */
    dx=srcl;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;

    /* [deg] Gal latitude at subint centre */
    dx=srcb;
    fits_write_col( fits, TDOUBLE, col, subint_cnt, 1, 1, &dx, &sta );
    col++;

    /* [deg] Feed angle at subint centre */
    x=0.0;
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, 1, &x, &sta );
    col++;

    /* [deg] Parallactic angle at subint centre */

    /* [deg] Position angle of feed at subint centre */
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, 1, &x, &sta );
    col++;

    /* [deg] Parallactic angle at subint centre */
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, 1, &x, &sta );
    col++;

    /* [deg] Telescope azimuth at subint centre */
    x=(float) az_start;
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, 1, &x, &sta );
    col++;

    /* [deg] Telescope zenith angle at subint centre */
    x=(float) za_start;
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, 1, &x, &sta );
    col++;

    /* Centre freq. for each channel - NCHAN floats */
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1,nchans,binned_freq,&sta );
    col++;

    /* Weights for each channel -  NCHAN floats */
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, nchans,
		    binned_weight, &sta );
    col++;

    /* Data offset for each channel - NCHAN*NPOL floats */
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, nchans*nifs,
		    binned_offset, &sta );
    col++;

    /* Data scale factor for each channel - NCHAN*NPOL floats */
    fits_write_col( fits, TFLOAT, col, subint_cnt, 1, nchans*nifs, 
		    binned_scale, &sta );
    col++;

    /* Subint data table - Dimensions of data table = (NBIN,NCHAN,NPOL) */
    for (i=0;i<nchans*nifs*nbins;i++) binned_data[i]=prof[i];

    fits_write_col(fits,TFLOAT,col, subint_cnt, 1, nbins*nchans*nifs, 
		   binned_data, &sta );

    subint_cnt++;
    if (sta) fits_report_error(stderr,sta);
#endif
  } else {
    /* EPN format requested - set up some general EPN variables */
    sprintf(epn.history,"%s %s fast-sampled data reduced using fold",
	    telescope_name(telescope_id),backend_name(machine_id));
    while (strlen(epn.history)<65) strcat(epn.history," ");
    strcpy(epn.jname,"");
    strcpy(epn.cname,"");
    epn.pbar=pfld0;
    epn.dm=refdm;
    epn.rm=0.0;
    strcpy(epn.catref,"none");
    strcpy(epn.bibref,"none");
    epn.raj=0.0;
    epn.dec=0.0;
    strcpy(epn.telname,telescope_name(telescope_id));
    epn.epoch=(float) floor(tstart);
    epn.opos=0.0;
    epn.paflag=' ';
    epn.timflag='U';
    epn.xtel=0.0;
    epn.ytel=0.0;
    epn.ztel=0.0;
    cal((double)epn.epoch,&epn.year,&epn.month,&epn.day);
    epn.scanno=0;
    epn.subscan=0;
    epn.npol=nifs;
    epn.nfreq=nchan;
    epn.nbins=nbins;
    epn.tbin=1.0e6*pfld0/(double)nbins;
    epn.nint=0;
    epn.ncal=0;
    epn.lcal=0;
    epn.tres=epn.tbin;
    epn.fluxflag='U';
    epn.navg=1;
    strcpy(epn.uf,"MHz ");
    epn.df=1000.0*fabs(foff);
    strcpy(epn.ud,"kHz ");
    if (epn.df>=10000.0) {
      epn.df/=1000.0;
      strcpy(epn.ud,"MHz ");
    }
    epn.tstart=(tstart-floor(tstart))*86400.0+tsta;
    epn.tstart*=1.0e6;
    epn.iprofile=(unsigned long *) malloc(epn.nbins*sizeof(long));
    profile = (float *) malloc(sizeof(float)*nbins);
    for (i=0;i<nifs;i++) {
      strcpy(epn.idfield,"I");
      for (c=0;c<nchan;c++) {
	for (b=0;b<nbins;b++) {
	  profile[b]=prof[i*nchan*nbins+c*nbins+b];
	}
	scale_prof(profile,nbins,epn.iprofile,&epn.scale,&epn.offset);
	epn.f0=fch1+foff*c;
	epn.nband=c+1;
	epn.papp=pfld0;
	epn.rms=0.0;
	epn.tres=tbin=1.0e6*pfld0*window/(double)nbins;
	if (c==0) write_epn_header(out,epn);
	write_epn_subheader(out,epn);
      }
    }
    fflush(out);
    free(profile);
    free(epn.iprofile);
  }
}
Example #6
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;
}