long long nsamples(char *filename,int headersize, header *h) /*includefile*/ { long long datasize,numsamps; datasize=sizeof_file(filename)-headersize; numsamps=(long long) (long double) (datasize)/ (((long double) h->nbits) / 8.0) /(long double) h->nifs/(long double) h->nchans; return(numsamps); }
main(int argc, char *argv[]) { FILE *fileptr[50]; FILE *output; float buffer[4096]; //max channels 4096 float spectra_sum[128]; float scale_factor; char filename[80],*telescope,*backend,*datatype,message[80],unit[16], outfile[120]; int i,j, k, year,month,day,check,rah,ram,ded,dem, nfiles, opened=0; double ras,des,frac,tobs; char sra[6],sde[6],decsign; int raw,uth,utm,uts; long long numsamps,datasize,headersize; long long mindatasize=0; int writeobsdbline; /* get these from first file on command line, apply to output file */ int output_nchans=0, output_nbits=0, output_nifs=0; long long output_datasize=0; long long output_headersize=0; int output_ptr; char quantval; double mean=0.0, sigma=0.0; /* work out how many files are on the command line */ i=1; nfiles=0; while(file_exists(argv[i]) && (i < argc)) { //printf("opening %s\n", argv[i]); nfiles++; i++; } if (!nfiles) { error_message("no input files supplied on command line!"); exit(1); } if (nfiles > 50) { error_message("too many input files supplied on command line (max 50)!"); exit(1); } for(i = 0; i<nfiles; i++) { fileptr[i]=open_file(argv[i+1],"rb"); } if (argc>nfiles) { i=nfiles+1; while (i<argc) { if (strings_equal(argv[i],"-o")) { /* get and open file for output */ strcpy(outfile,argv[++i]); if(file_exists(outfile)) { sprintf(message,"output file (%s) exists!",argv[i]); error_message(message); exit(1); } output=fopen(outfile,"wb"); opened=1; } else { /* unknown argument passed down - stop! */ sprintf(message,"unknown argument (%s) passed to filterbank.",argv[i]); error_message(message); exit(1); } i++; } } if (!opened) { error_message("must have an output file (-o <output>)!"); exit(1); } pulsarcentric=barycentric=0; writeobsdbline=0; for(i = 0; i<nfiles; i++) { headersize=read_header(fileptr[i]); rewind(fileptr[i]); datasize=sizeof_file(argv[i+1])-headersize; numsamps=nsamples(argv[i+1],headersize,nbits,nifs,nchans); if(output_datasize == 0 || datasize < output_datasize) { output_datasize = datasize; output_headersize = headersize; output_ptr = i; } if(output_nchans == 0) { output_nchans = nchans; output_nbits = nbits; output_nifs = nifs; } if(nchans != output_nchans || nbits != output_nbits || nifs != output_nifs) { sprintf(message,"channel/if/bit mismatch, exiting..."); error_message(message); exit(1); } //printf("file: %s headersize: %d nbits: %d nifs: %d nchans: %d\n", argv[i+1], headersize, nbits, nifs, nchans); } if (output_nbits != 32) { error_message("This routine only works on 32 bit (float) filterbank data!"); exit(1); } printf("minimum data size is: %d\n", output_datasize); printf("will dump: %d\n", (output_datasize / (long long) (output_nifs * output_nchans))); headersize=read_header(fileptr[output_ptr]); rewind(fileptr[output_ptr]); printf("header size lead file: %d\n", headersize); fread(buffer, sizeof(char), headersize, fileptr[output_ptr]); fwrite(buffer, sizeof(char), headersize, output); rewind(fileptr[output_ptr]); /* bump past header for all input files */ for(i = 0; i<nfiles; i++) { headersize=read_header(fileptr[i]); } /* rewind and bump past header for all input files */ for(i = 0; i<nfiles; i++) { rewind( fileptr[i] ); headersize=read_header(fileptr[i]); } scale_factor = output_datasize / ((float) (output_nifs * output_nchans)); /* outer loop, read through all spectra, quantize to 8 bits, write to file */ for (j = 0; j < (output_datasize / (long long) (output_nifs * output_nchans)); j++){ /* read n spectra (1 spectra x n files), sum */ for(i=0;i<128;i++) spectra_sum[i] = 0.0; for(i = 0; i<nfiles; i++) { fread(&buffer, sizeof(float), (output_nifs * output_nchans), fileptr[i]); for(k=0; k<128; k++) spectra_sum[k] = (spectra_sum[k] + (buffer[k] / scale_factor) ); } fwrite(&spectra_sum, sizeof(float), 128, output); } /* if (argc>1) { print_version(argv[0],argv[1]); if (help_required(argv[1])) { header_help(); exit(0); } else if (file_exists(argv[1])) { strcpy(filename,argv[1]); fileptr=open_file(filename,"rb"); } else if (!file_exists(argv[1]) && (strncmp(argv[1],"-",1) !=0)) { sprintf(message,"Data file: %s not found...\n",argv[1]); error_message(message); exit(1); } } */ }
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); }
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); }