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); } }
void wapp2fb(FILE *input, FILE *output) /* includefile */ { FILE *bptr, *fpou, *alfa[2]; int pixel[2]; double pra, pdec; double bw, bandwidth, scale, power, hweight, tsamp_us, crate, lmst; double *lag, *sum, *acf, *window, jan1, days, epoch, ras,des,rahr,dede; float zerolag,*block,smin,smax; int doit,i,j,k,two_nlags,nlags,stat,rec_size,idump,swap_bytes,ifnum,opened; int filesize,headersize,beam,utsecs,iymdf[4],rah,ram,ded,dem; unsigned char *cblock, zuc; unsigned short *sblock, zus; unsigned int zul; char message[80], outfile[80]; void *dump; static float realtime=0.0; #ifdef FFTW fftw_plan fftplan; #endif /* establish whether we need to swap bytes (WAPP is little endian) */ swap_bytes=big_endian(); /* initialise correlator parameters used below */ nlags=nchans; two_nlags=2*nlags; bandwidth=foff*nlags; tsamp_us=tsamp*1.0e6; if (bandwidth<0.0) bandwidth *= -1.0; #ifdef FFTW acf = fftw_malloc(sizeof(double) * two_nlags); lag = fftw_malloc(sizeof(double) * two_nlags); /* set up fftw table and acf array when computing power spectra */ fftplan=fftw_plan_r2r_1d(two_nlags,acf,lag,FFTW_R2HC,FFTW_PATIENT); #endif #ifndef FFTW /* set up acf array when computing power spectra */ acf = (double *) malloc(two_nlags * sizeof(double)); lag = (double *) malloc(two_nlags * sizeof(double)); #endif if (compute_spectra) { /* ranges for scaling spectra */ smin=0.0;smax=3.0; } else { /* ranges for scaling correlation functions */ smin=-0.5;smax=1.0; } /* set up the weights for windowing of ACF to monimize FFT leakage */ if (hanning) { /* Hanning window */ hweight=0.50; } else if (hamming) { /* Hamming window */ hweight=0.54; } else { /* no window (default) */ hweight=1.00; } /* define the smoothing window to be applied base on the above weight */ window = (double *) malloc(nlags * sizeof(double)); for (j=0; j<nlags; j++) window[j]=(hweight+(1.0-hweight)*cos(PI*j/nlags)); /* work out number of IFs to loop over */ if (sumifs && (nifs>1)) { smin*=2.0; smax*=2.0; ifnum=2; } else { sumifs=0; ifnum=nifs; } /* calculate required record size for reading - i.e. number of bytes/dump */ rec_size = nifs*nlags*(nbits/8); dump = malloc(rec_size); /* pointer to the correlator dump */ /* correlator data rate */ crate = 1.0/(tsamp_us-WAPP_DEAD_TIME); /* scale factor to normalize correlation functions */ if (bandwidth < 50.0) bw=50.0; /* correct scaling for narrow-band use */ else bw=bandwidth; scale = crate/bw; if (wapp_level==9) scale/=16.0; /* 9-level sampling */ if (wapp_sum) scale/=2.0; /* summed IFs (search mode) */ scale*=pow(2.0,(double)wapp_lagtrunc); /* needed for truncation modes */ /* now define a number of working arrays to store lags and spectra */ block = (float *) malloc(nlags * sizeof(float)); cblock = (unsigned char *) malloc(nlags * sizeof(unsigned char)); sblock = (unsigned short *) malloc(nlags * sizeof(unsigned short)); /* if the file is ALFA data --- do the demultiplexing to two files */ if (wapp_isalfa) { angle_split(src_raj,&rah,&ram,&ras); rahr=(double)rah+(double)ram/60.0+(double)ras/3600.0; angle_split(src_dej,&ded,&dem,&des); if (ded>0) dede=(double)ded+(double)dem/60.0+(double)des/3600.0; else dede=(double)ded-(double)dem/60.0-(double)des/3600.0; /* calculate local sidereal time in hours */ lmst=slaGmst(tstart)*12.0/4.0/atan(1.0)-4.4502051459439667; if (lmst<0.0) lmst+=24.0; slaDjcal(5,tstart,iymdf,&stat); slaCaldj(iymdf[0],1,1,&jan1,&stat); days=tstart-jan1+1.0; epoch=(double)iymdf[0]+days/365.25; utsecs=86400*(tstart-floor(tstart)); pixel[0]=(wapp_number-1)*2; pixel[1]=pixel[0]+1; puts("opening output files for demultiplexed ALFA data..."); for (i=0; i<2; i++) { if (alfa_raj[pixel[i]] == 0.0) { alfa_position(rahr,dede,lmst,epoch,alfa_ang,0.0,0.0,pixel[i],&pra,&pdec); src_raj=h2hms(pra); src_dej=deg2dms(pdec); } else { src_raj=h2hms(alfa_raj[pixel[i]]); src_dej=deg2dms(alfa_dej[pixel[i]]); } sprintf(outfile,"%s_%.0f_%05d_%04d_%s_%d.fil", project,floor(tstart),utsecs, scan_number,source_name,pixel[i]); alfa[i]=open_file(outfile,"wb"); puts(outfile); filterbank_header(alfa[i]); } beam=0; } if (headerfile) { /* write output ASCII header file */ fpou=open_file("head","w"); fprintf(fpou,"Original WAPP file: %s\n",inpfile); fprintf(fpou,"Sample time (us): %f\n",tsamp_us); fprintf(fpou,"Observation time (s): %f\n",wapp_obstime); fprintf(fpou,"Time stamp (MJD): %18.12f\n",tstart); fprintf(fpou,"Number of samples/record: %d\n",512); fprintf(fpou,"Center freq (MHz): %f\n",fch1+(float)nlags*foff/2.0); fprintf(fpou,"Channel band (kHz): %f\n",bandwidth*1000.0/nlags); fprintf(fpou,"Number of channels/record: %d\n",nlags); fprintf(fpou,"Nifs: %d\n",ifnum); fprintf(fpou,"RA (J2000): %f\n",src_raj); fprintf(fpou,"DEC (J2000): %f\n",src_dej); fprintf(fpou,"Gal l: %.4f\n",srcl); fprintf(fpou,"Gal b: %.4f\n",srcb); fprintf(fpou,"Name: %s\n",source_name); fprintf(fpou,"Lagformat: %d\n",wapp_lagformat); fprintf(fpou,"Sum: %d\n",wapp_sum); fprintf(fpou,"Level: %d\n",wapp_level); fprintf(fpou,"AZ at start: %f\n",az_start); fprintf(fpou,"ZA at start: %f\n",za_start); fprintf(fpou,"AST at start: %f\n",ast0); fprintf(fpou,"LST at start: %f\n",lst0); fprintf(fpou,"Project ID: %s\n",project); fprintf(fpou,"Observers: %s\n",culprits); filesize=sizeof_file(inpfile); fprintf(fpou,"File size (bytes): %d\n",filesize); headersize=wapp_header_size+wapp_incfile_length; fprintf(fpou,"Data size (bytes): %d\n",filesize-headersize); fprintf(fpou,"Number of samples: %d\n",nsamples(inpfile,headersize,nbits,nifs,nchans)); fclose(fpou); } /* initialise various counters and flags */ opened=idump=i=j=0; /* main loop reading data from infile until no more left to read */ while( (stat=read(wapp_file,dump,rec_size)) == rec_size) { /* calculate elapsed time and determine whether we process this record */ realtime += (float) tsamp; if ( (doit=process(realtime,start_time,final_time)) == -1) break; if (doit) { /* set ALFA beam output if necessary */ if (wapp_isalfa) { output=alfa[beam]; /* set output file for this loop */ beam=!(beam); /* flip file for next iteration */ } /* clear zerolag and blocksum arrays */ zerolag=0.0; for (j=0; j<nlags; j++) block[j]=0.0; /* loop over the IFs */ for (i=0; i<ifnum; i++) { if (ifstream[i]=='Y') { if (zerolagdump) { /* only interested in the zero lag term for each IF */ switch (nbits) { case 8: zuc = *(((unsigned char *)dump)+i*nlags); zerolag+=zuc; break; case 16: zus = *(((unsigned short *)dump)+i*nlags); if (swap_bytes) swap_short(&zus); zerolag+=zus; break; case 32: zul = *(((unsigned int *)dump)+i*nlags); if (swap_bytes) swap_int(&zul); zerolag+=zul; break; } /* write out the data checking IF number for summed mode */ if ( (sumifs && (i==1)) || (!sumifs) ) { if (obits==32) { if (swapout) swap_float(&zerolag); fwrite(&zerolag,sizeof(float),1,output); } else { sprintf(message,"cannot write %d bits in zerolag mode",obits); error_message(message); } } } else { /* fill lag array with scaled CFs */ for (j=0; j<nlags; j++) { switch (nbits) { case 8: zuc = *(((unsigned char *)dump)+j+i*nlags); lag[j] = scale * (double) zuc - 1.0; break; case 16: zus = *(((unsigned short *)dump)+j+i*nlags); if (swap_bytes) swap_short(&zus); lag[j] = scale * (double) zus - 1.0; break; case 32: zul = *(((unsigned int *)dump)+j+i*nlags); if (swap_bytes) swap_int(&zul); lag[j] = scale * (double) zul - 1.0; break; } } /* calculate power and correct for finite level quantization */ power = inv_cerf(lag[0]); power = 0.1872721836/power/power; if (i<2) { if (do_vanvleck) { if (wapp_level==3) { /* apply standard 3-level van vleck correction */ vanvleck3lev(lag,nlags); } else if (wapp_level==9) { /* apply 9-level van vleck correction */ vanvleck9lev(lag,nlags); } } } if (compute_spectra) { /* form windowed even ACF in array */ for(j=1; j<nlags; j++) { acf[j]=window[j]*lag[j]*power; acf[two_nlags-j]=acf[j]; } acf[nlags]=0.0; acf[0]=lag[0]*power; /* FFT the ACF (which is real and even) -> real and even FFT */ #ifdef FFTW fftw_execute(fftplan); #endif #ifndef FFTW rfft(two_nlags,acf,lag); #endif /* if the band needs to be flipped --- do it here */ if (wapp_flip) { /* use acf as temporary array */ for (j=0;j<nlags;j++) acf[j]=lag[j]; k=nlags-1; for (j=0;j<nlags;j++) { lag[k]=acf[j]; k--; } } /* add lags to block array */ for (j=0; j<nlags; j++) block[j]+=lag[j]; } else { /* just copy correlation functions into block */ for (j=0; j<nlags; j++) block[j]=lag[j]; } /* write out data block checking IF number for summed mode */ if ( (sumifs && (i==1)) || (!sumifs) ) { if (obits==32) { if (swapout) for (j=0; j<nlags; j++) swap_float(&block[j]); fwrite(block,sizeof(float),nlags,output); } else if (obits==16) { float2short(block,nlags,smin,smax,sblock); if (swapout) for (j=0; j<nlags; j++) swap_short(&sblock[j]); fwrite(sblock,sizeof(unsigned short),nlags,output); } else if (obits==8) { float2char(block,nlags,smin,smax,cblock); fwrite(cblock,sizeof(unsigned char),nlags,output); } else if (obits==4) { float2four(block,nlags,smin,smax,cblock); fwrite(cblock,sizeof(unsigned char),nlags/2,output); } else { sprintf(message,"cannot write %d bits in wapp2fb",obits); error_message(message); } } } /* end of zerolagdump if */ if (!sumifs) { /* reset block and zerolag if not summing */ zerolag=0.0; for (j=0; j<nlags; j++) block[j]=0.0; } } /* end of IFstream if */ } /* end of loop over IFs */ } /* end of processing if */ /* increment dump counter and update logfile every 512 dumps */ idump++; if (idump%512 == 0) { if (!opened) { /* open up logfile */ open_log("filterbank.monitor"); opened=1; } sprintf(message,"time:%.1fs",realtime); update_log(message); } } /* end of main read loop*/ /* job done - free up remaining arrays */ free(dump);free(block);free(sblock);free(cblock);free(window); #ifdef FFTW fftw_destroy_plan(fftplan); fftw_free(acf); fftw_free(lag); #endif #ifndef FFTW free(acf); free(lag); #endif }
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); }
int main(int argc, char *argv[]) { /* Cmd line */ static struct option long_opts[] = { {"frontend", 1, NULL, 'F'}, {"offset", 0, NULL, 'o'}, {"srcname", 1, NULL, 's'}, }; int ii; double dtmp; char filename[256], *adr; struct psrfits pf; int c1,c2; double c3; int opt, opti; int scan_id, beam_id; bool have_Stokes = false; bool have_chan_offset = false; bool have_frontend = false; char frontend[3]; bool have_srcname = false; char srcname[24]; while ((opt=getopt_long(argc,argv,"ob:t:j:i:F:s:p:P:F:CuU:SAqh",long_opts,&opti))!=-1) { switch (opt) { case 'F': have_frontend = true; strncpy(frontend, optarg, 3); break; case 'o': have_chan_offset = true; break; case 's': have_srcname = true; strncpy(srcname, optarg, 24); break; case 'S': have_Stokes = true; break; case 'h': default: usage(); exit(0); break; } } // Only set the basefilename and not "filename" // Also, fptr will be set by psrfits_create_searchmode() FILE *pfi; strcpy(filename, argv[optind]); printf("Filename = %s\n", filename); // Read filterbank if((pfi=fopen(filename,"r"))==NULL) { printf("Cannot open file %s\n", filename); exit(-2); } else { printf("Opening file %s\n", filename); } /* try to read the header */ long long header_size, numsamps; header iheader; if (!(header_size=read_header(pfi,&iheader))) { printf("Error reading header\n"); exit(-2); } //printf("header_size = %lld\n", header_size); // Get the number of samples numsamps=nsamples(filename, header_size, &iheader); //printf("numsamps = %lld ra=%lf dec=%lf\n", numsamps, iheader.src_raj, iheader.src_dej); //fflush(stdout); pf.filenum = 0; // This is the crucial one to set to initialize things pf.rows_per_file = 4000; // Need to set this based on PSRFITS_MAXFILELEN // Now set values for our hdrinfo structure pf.hdr.scanlen = 3600; // in sec strcpy(pf.hdr.observer, "Observer"); strcpy(pf.hdr.telescope, "Effelsberg"); strcpy(pf.hdr.obs_mode, "SEARCH"); strcpy(pf.hdr.backend, "PFFTS"); strncpy(pf.hdr.source, iheader.source_name, 24); strcpy(pf.hdr.frontend, "LBand"); strcpy(pf.hdr.project_id, "HTRUN"); strcpy(pf.hdr.date_obs, "2010-01-01T05:15:30.000"); strcpy(pf.hdr.poln_type, "LIN"); strcpy(pf.hdr.poln_order, "IQUV"); strcpy(pf.hdr.track_mode, "TRACK"); strcpy(pf.hdr.cal_mode, "OFF"); strcpy(pf.hdr.feed_mode, "FA"); //printf("filename = %s\n", filename); fflush(stdout); if(strstr(filename,"/") != NULL) { adr=(char *)strrchr(filename,'/')+1; sprintf(pf.filename,"%s",adr); } else strncpy(pf.filename, filename, 80); //printf("filename = %s\n", pf.filename); fflush(stdout); sscanf(pf.filename, "%d_%d_%d_%dbit.fil", &scan_id, &c1, &beam_id, &c2); pf.hdr.dt = iheader.tsamp; pf.hdr.fctr = iheader.fch1+iheader.foff*iheader.nchans/2. - iheader.foff/2.; if (have_chan_offset) pf.hdr.fctr += iheader.foff/2.; pf.hdr.BW = iheader.nchans * iheader.foff; angle_split(iheader.src_raj, &c1, &c2, &c3); sprintf(pf.hdr.ra_str, "%2.2d:%2.2d:%07.4lf", c1, c2, c3); angle_split(iheader.src_dej, &c1, &c2, &c3); sprintf(pf.hdr.dec_str, "%2.2d:%2.2d:%07.4lf", c1, c2, c3); pf.hdr.azimuth = iheader.az_start; pf.hdr.zenith_ang = iheader.za_start; pf.hdr.beam_FWHM = 0.25; pf.hdr.start_lst = 0.0; pf.hdr.start_sec = 0.0; pf.hdr.start_day = 55000; pf.hdr.scan_number = scan_id; pf.hdr.rcvr_polns = 2; pf.hdr.summed_polns = 0; pf.hdr.offset_subint = 0; pf.hdr.nchan = iheader.nchans; pf.hdr.orig_nchan = pf.hdr.nchan; pf.hdr.orig_df = pf.hdr.df = pf.hdr.BW / pf.hdr.nchan; pf.hdr.nbits = iheader.nbits; pf.hdr.npol = iheader.nifs; pf.hdr.onlyI = 0; if (pf.hdr.npol==1) pf.hdr.onlyI = 1; pf.hdr.chan_dm = 0.0; pf.hdr.fd_hand = 1; pf.hdr.fd_sang = 0; pf.hdr.fd_xyph = 0; pf.hdr.be_phase = 1; pf.hdr.ibeam = beam_id; pf.hdr.nsblk = 2048; pf.hdr.MJD_epoch = iheader.tstart; // Note the "L" for long double pf.hdr.ds_time_fact = 1; pf.hdr.ds_freq_fact = 1; //sprintf(pf.basefilename, "%s_%04d_%02d", pf.hdr.backend, pf.hdr.scan_number, pf.hdr.ibeam); //psrfits_create(&pf); // Now set values for our subint structure pf.sub.tsubint = pf.hdr.nsblk * pf.hdr.dt; pf.tot_rows = 0.0; pf.sub.offs = (pf.tot_rows + 0.5) * pf.sub.tsubint; pf.sub.lst = pf.hdr.start_lst; pf.sub.ra = pf.hdr.ra2000; pf.sub.dec = pf.hdr.dec2000; // GD slaEqgal(pf.hdr.ra2000*DEGTORAD, pf.hdr.dec2000*DEGTORAD, //&pf.sub.glon, &pf.sub.glat); //pf.sub.glon *= RADTODEG; //pf.sub.glat *= RADTODEG; pf.sub.feed_ang = 0.0; pf.sub.pos_ang = 0.0; pf.sub.par_ang = 0.0; pf.sub.tel_az = pf.hdr.azimuth; pf.sub.tel_zen = pf.hdr.zenith_ang; pf.sub.bytes_per_subint = (pf.hdr.nbits * pf.hdr.nchan * pf.hdr.npol * pf.hdr.nsblk) / 8; if (pf.hdr.nbits==32) pf.sub.FITS_typecode = TFLOAT; else pf.sub.FITS_typecode = TBYTE; // 11 = byte // if (have_frontend) { if (strncmp(frontend,"S36",3)==0) { strcpy(pf.hdr.backend, "PSRIX"); strcpy(pf.hdr.frontend, "S36"); strcpy(pf.hdr.poln_type, "CIRC"); strcpy(pf.hdr.poln_order, "AABBCRCI"); pf.hdr.fd_hand = -1; // To conform PA and Stokes V to PSR/IEEE } else if (strncmp(frontend,"S60",3)==0) { strcpy(pf.hdr.backend, "PSRIX"); strcpy(pf.hdr.frontend, "S60"); strcpy(pf.hdr.poln_type, "CIRC"); strcpy(pf.hdr.poln_order, "AABBCRCI"); } else if (strncmp(frontend,"S110",3)==0) { strcpy(pf.hdr.backend, "PSRIX"); strcpy(pf.hdr.frontend, "S110"); strcpy(pf.hdr.poln_type, "CIRC"); strcpy(pf.hdr.poln_order, "AABBCRCI"); pf.hdr.be_phase = -1; // To conform Stokes V to PSR/IEEE } else if (strncmp(frontend,"S45",3)==0) { strcpy(pf.hdr.backend, "PSRIX"); strcpy(pf.hdr.frontend, "S45"); strcpy(pf.hdr.poln_type, "LIN"); strcpy(pf.hdr.poln_order, "AABBCRCI"); pf.hdr.be_phase = -1; // To conform Stokes V to PSR/IEEE } else {printf("Frontend %s not recognised\n", frontend);} } if (have_srcname) strncpy(pf.hdr.source, srcname, 24); if (have_Stokes) strcpy(pf.hdr.poln_order, "IQUV"); if (strncmp(pf.hdr.backend, "PFFTS",5)==0) sprintf(pf.basefilename, "%s_%04d_%02d", pf.hdr.backend, pf.hdr.scan_number, pf.hdr.ibeam); else sprintf(pf.basefilename, "%s_%s_%5d_%d", pf.hdr.backend, pf.hdr.source, (int) pf.hdr.MJD_epoch, (int) pf.hdr.fctr); psrfits_create(&pf); // Create and initialize the subint arrays pf.sub.dat_freqs = (float *)malloc(sizeof(float) * pf.hdr.nchan); pf.sub.dat_weights = (float *)malloc(sizeof(float) * pf.hdr.nchan); dtmp = pf.hdr.fctr - 0.5 * pf.hdr.BW + 0.5 * pf.hdr.df; for (ii = 0 ; ii < pf.hdr.nchan ; ii++) { pf.sub.dat_freqs[ii] = dtmp + ii * pf.hdr.df; pf.sub.dat_weights[ii] = 1.0; } pf.sub.dat_offsets = (float *)malloc(sizeof(float) * pf.hdr.nchan * pf.hdr.npol); pf.sub.dat_scales = (float *)malloc(sizeof(float) * pf.hdr.nchan * pf.hdr.npol); for (ii = 0 ; ii < pf.hdr.nchan * pf.hdr.npol ; ii++) { pf.sub.dat_offsets[ii] = 0.0; pf.sub.dat_scales[ii] = 1.0; } // This is what you would update for each time sample (likely just // adjusting the pointer to point to your data) pf.sub.rawdata = (unsigned char *)malloc(pf.sub.bytes_per_subint); // Here is the real data-writing loop do { // Update the pf.sub entries here for each subint // as well as the pf.sub.data pointer fread(pf.sub.rawdata, pf.sub.bytes_per_subint, 1, pfi); pf.sub.offs = (pf.tot_rows + 0.5) * pf.sub.tsubint; psrfits_write_subint(&pf); } while (pf.T < pf.hdr.scanlen && !feof (pfi) && !pf.status); // Close the last file and cleanup fits_close_file(pf.fptr, &(pf.status)); free(pf.sub.dat_freqs); free(pf.sub.dat_weights); free(pf.sub.dat_offsets); free(pf.sub.dat_scales); free(pf.sub.rawdata); printf("Done. Wrote %d subints (%f sec) in %d files. status = %d\n", pf.tot_rows, pf.T, pf.filenum, pf.status); exit(0); }
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); } }
/* 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; }