int main (int argc, char *argv[]) { int ntimglobal=0; // number of time samples in original int ngulp_original=0; // number of time samples to look at at once int nskipstart=0; // number skipped at start int nrejects; //ZAPPER int zapswitch = 0; //ZAPPER double tsamp_orig=0; //gsearch setup & defaults float Gsigmacut=6.0; float delta, tstart; vector<Gpulse> * Giant = new vector<Gpulse>[MAXFILES]; bool Gsearched=false; int i,ntim,headersize[MAXFILES],noff=0,gulp; float *time_series[MAXFILES],sum=0.0,sumsq=0.0,mean,meansq,sigma; int MAXMARKERS = 1024; int nfiles = 0; FILE *inputfile[MAXFILES]; char filename[MAXFILES][256]; int spectra=0; int powerspectra=0; double dmoffirstfile; char *killfile; bool dokill=false; bool ssigned=true; bool fsigned=false; int topfold=-1; int topgiant=-1; int toppeak=-1; //?!? sarah added this bool askdevice=false; char devicename[200]; if (argc<2 || help_required(argv[1])) { helpmenu(); // fprintf(stderr,"Usage: giant filenames\n\t(e.g.>> giant *.tim)\n\n\t-s N\tskip N samples\n\t-n N\tread N samples\n\t-S read spectra instead of amplitudes\n-i interpret signed chars as unsigned\n\t-z make a zap list of bad time samples\n"); exit(0); } print_version(argv[0],argv[1]); i=1; while (i<argc) { if (file_exists(argv[i])) { inputfile[nfiles]=open_file(argv[i],"r"); strcpy(filename[nfiles],argv[i]); nfiles++; } if (strings_equal(argv[i],"-s")) sscanf(argv[++i],"%d",&nskipstart); if (strings_equal(argv[i],"-S")) spectra=1; if (strings_equal(argv[i],"-i")) ssigned=false; if (strings_equal(argv[i],"-f")) fsigned=true; if (strings_equal(argv[i],"-n")) sscanf(argv[++i],"%d",&ngulp_original); if (strings_equal(argv[i],"-c")) sscanf(argv[++i],"%f",&Gsigmacut); if (strings_equal(argv[i],"-z")) zapswitch=1; if (strings_equal(argv[i],"-g")) {askdevice=true;sscanf(argv[++i],"%s",&devicename);} if (strings_equal(argv[i],"-k")) {killfile=(char*)malloc(strlen(argv[++i])+1); strcpy(killfile,argv[i]);dokill=true;} if (nfiles>MAXFILES) error_message("too many open files"); i++; } int ntimglobal_smallest=0, nsamp; for (i=0; i<nfiles; i++) { if (spectra){ int npf; double rate; time_series[i]=Creadspec(filename[i],&npf,&rate); tsamp = 1.0/(rate); //normalise(npf,time_series[i]); nsamp = ntimglobal = ntimglobal_smallest = npf; } else { if ((headersize[i]=read_header(inputfile[i]))) { if (! fsigned){ if (isign > 0) { ssigned=false; fprintf(stderr,"using signed header variable to set UNSIGNED\n"); } if (isign < 0) { ssigned=true; fprintf(stderr,"using signed header variable to set SIGNED\n"); } } if (i==0) dmoffirstfile = refdm; if (nbits!=8 && nbits!=32) error_message("giant currently only works for 8- or 32-bit data"); nsamp = nsamples(filename[i],headersize[i],nbits,nifs,nchans); if (i == 0) { ntimglobal_smallest=nsamp; } else { ntimglobal= nsamp; if (ntimglobal < ntimglobal_smallest) ntimglobal_smallest = ntimglobal; } // Space for data (time_series) time_series[i]=(float *) malloc((nsamp+2)*sizeof(float)); if (time_series[i]==NULL){ fprintf(stderr,"Error mallocing %d floats of %d size\n",nsamp, sizeof(float)); exit(-1); } tsamp_orig = tsamp; // Skip data fprintf(stderr,"Skipping %d bytes\n",nskipstart*nbits/8); fseek(inputfile[i],nskipstart*nbits/8,SEEK_CUR); } // each file } // spectra or not } // for (i...) puti(ntimglobal_smallest); if (ngulp_original==0) ngulp_original=ntimglobal_smallest; // ****** SAM'S ZAP SWITCH ****** // Sam Bates 2009 // Integrated into new giant by SBS // Switch to make a .killtchan file for time samples > 3.5 sigma // SARAHZAP tag means addition was added later by Sarah // ****************************** int ngulp=ngulp_original; // int nrejects_max=ngulp_original/100; int * mown = new int[ngulp_original]; int nstart=0; if (zapswitch){ float dummy; int NActuallyRead; char *buffer; buffer = new char[ngulp*nbits/8]; for (i=0; i<nfiles; i++){ NActuallyRead = fread(buffer,nbits/8,ngulp,inputfile[i]); if (nbits==32){ memcpy(time_series[i],buffer,sizeof(float)*ngulp); } else { for (int j=0;j<NActuallyRead;j++){ if (ssigned) time_series[i][j]=(float)buffer[j]; if (!ssigned) time_series[i][j]=(float)((unsigned char)buffer[j]); } } puti(ngulp); find_baseline(ngulp,time_series[i],10.0/tsamp,5.0); mowlawn(ngulp,time_series[i],5,256); } printf("%f\n",dummy); printf("Bad time samples found...\n"); exit(0); } int pgpID; if (askdevice){ pgpID = cpgbeg(0,devicename,1,1); } else { pgpID = cpgbeg(0,"/xs",1,1); } cpgsch(0.5); cpgtext(0.6,0.0,"Press 'h' over the main window for help and full options list."); cpgsch(1.0); /* create the dialog */ dialog * d = new dialog(); /* add the "action" buttons */ int QUIT = d->addbutton(0.02,0.95,"Quit"); int POWER = d->addbutton(0.07,0.85,"POWER"); int SMHRM = d->addbutton(0.075,0.80,"SMHRM"); int FFT = d->addbutton(0.02,0.85,"FFT"); int PLOT = d->addbutton(0.02,0.80,"Plot"); int NEXT = d->addbutton(0.02,0.75,"Next"); int ZAPPEAK = d->addbutton(0.075,0.75,"ZapPeak"); int RESET = d->addbutton(0.02,0.70,"Reset"); int GLOBALRESET = d->addbutton(0.02,0.65,"Global Reset"); int HALVEPLOT = d->addbutton(0.02,0.60,"Halve Plot"); int BASELINE = d->addbutton(0.02,0.50,"Baseline"); int ZAPCOMMON = d->addbutton(0.02,0.45,"Zap Common"); int SUBTRACTMEAN = d->addbutton(0.02,0.40,"ZAP Mean"); int BSCRUNCH = d->addbutton(0.02,0.35,"Bscrunch"); int NORMALISE = d->addbutton(0.02,0.30,"Normalise"); int HISTOGRAM = d->addbutton(0.02,0.25,"Histogram"); int GSEARCH = d->addbutton(0.02,0.20,"Find Giants"); int MOWLAWN = d->addbutton(0.08,0.70,"LAWN"); int SEEFIL = d->addbutton(0.02,0.15,"View Band"); int FWRITE = d->addbutton(0.02,0.05,"Write File"); /* add the plot regions */ d->addplotregion(0.2,0.99,0.98,0.99); float deltay = 0.9/(float)nfiles; for (i=0; i<nfiles; i++) d->addplotregion(0.2,0.99,0.95-deltay*(float)(i+1),0.95-deltay*(float)i); d->draw(); float x,y; char ans; int button=-1; int plotno=-1; int NPIXELS = 1024; float * xaxis = new float[NPIXELS]; float * ymaxes = new float[NPIXELS]; float * ymins = new float[NPIXELS]; int scrunch=1; int nmarkers=0; int * markers= new int[MAXMARKERS]; int nfileptr=nskipstart; int nplot=ngulp_original; nstart=0; //COMMENTED IN ZAPPER VERSION: MAY CAUSE CONFLICTS IN THIS VER. ngulp=ngulp_original; //COMMENTED IN ZAPPER VERSION: MAY CAUSE CONFLICTS IN THIS VER. double trialperiod; int doperiod=-1; double xperiod; bool zoneplot=false; int ngates=0; float xgate=0.0; button=NEXT; if (spectra) button = PLOT; while (button!=QUIT){ // Plot the zone // Entire file is white if (button!=NEXT)button=d->manage(&x,&y,&ans,&plotno); if (ans=='h'){ buttonexplain(); continue; } // printf("manage x %f y %f plotno %d\n",x,y,plotno); if (button==BASELINE) { for (i=0; i<nfiles; i++){ find_baseline(ngulp,time_series[i],10.0/tsamp,5.0); } button = PLOT; zoneplot=false; plotno = -1; } if (button==FWRITE) { // reread first header and close it. Sets globals. fclose(inputfile[0]); inputfile[0]=open_file(argv[1],"r"); headersize[0]=read_header(inputfile[0]); output = open_file("giant.tim","w"); nobits=32; nbands=1; dedisperse_header(); fprintf(stderr,"Opened file, writing data\n"); fwrite(time_series[0],sizeof(float),ngulp,output); fclose(output); button = -1; zoneplot=false; plotno =-1; } if (button==BSCRUNCH) { for (i=0; i<nfiles; i++){ bscrunch(ngulp,time_series[i]); } tsamp*=2; scrunch*=2; ngulp/=2; nplot/=2; button = PLOT; zoneplot=false; Gsearched=false; plotno = -1; } if (button==FFT) { for (i=0; i<nfiles; i++){ ngulp = ngulp_original; find_fft(&ngulp,time_series[i]); // Zap DC spike time_series[i][0]=0.0; time_series[i][1]=0.0; } spectra = 1; nplot = ngulp; button = PLOT; Gsearched=false; plotno = -1; } if (button==POWER) { for (i=0; i<nfiles; i++){ find_formspec(ngulp,time_series[i]); } ngulp/=2; powerspectra = 1; nplot = ngulp; button = PLOT; plotno = -1; } if (button==SMHRM) { nfiles = 6; for (i=1; i<nfiles; i++){ time_series[i]=(float *) malloc((ngulp+2)*sizeof(float)); if (time_series[i]==NULL){ fprintf(stderr,"Error allocating memory\n"); exit(-1); } } for (i=1;i<nfiles;i++) memcpy(time_series[i],time_series[0], (ngulp+2)*sizeof(float)); d->nplotregion=1; float deltay = 0.9/(float)nfiles; for (i=0; i<nfiles; i++) d->addplotregion(0.2,0.99,0.95-deltay*(float)(i+1), 0.95-deltay*(float)i); cpgeras(); d->draw(); float * workspace = new float[ngulp]; // Set up space for data, now actually sumhrm int one=1; newoldsumhrm_(&time_series[0][1],workspace,&ngulp,&one, // newoldsumhrm_(&time_series[0][0],workspace,&ngulp,&one, time_series[1],time_series[2],time_series[3], time_series[4],time_series[5]); /* newnewsumhrm_(time_series[0],&ngulp,&one, time_series[1],time_series[2],time_series[3], time_series[4],time_series[5]);*/ for (int iff=2;iff<6;iff++){ for (int i=0;i<ngulp;i++){ time_series[iff][i]/=sqrt(pow(2.0,(float)(iff-1))); } } delete [] workspace; button = PLOT; plotno = -1; } if (button==NORMALISE) { for (i=0; i<nfiles; i++){ normalise(ngulp,time_series[i],5.0); } button = PLOT; Gsearched=false; plotno = -1; } if (button==HISTOGRAM) { float pdfs[nfiles][MAXSIGMA]; // create pdfs for each beam for (int i=0;i<nfiles;i++) formpdf(pdfs[i],MAXSIGMA,ngulp,time_series[i]); for (int i=0;i<nfiles;i++){ for (int j=0;j<MAXSIGMA; j++){ fprintf(stderr, "pdfs[%d][%2d]=%8.0f %f \%\n", i, j+1, pdfs[i][j], 100*pdfs[i][j]/ngulp); } } button = PLOT; plotno = -1; } if (button==HALVEPLOT) { nplot/=2; button = PLOT; zoneplot=true; Gsearched=false; plotno = -1; } if (button==GLOBALRESET) { plotno = -1; nstart = 0; scrunch=1; tsamp = tsamp_orig; nplot=ngulp_original; ngulp=ngulp_original; button=PLOT; // Skip to end of skipped data for (i=0; i<nfiles; i++){ fseek(inputfile[i],-(nfileptr-nskipstart)*nbits/8,SEEK_CUR); Giant[i].clear(); } nfileptr=nskipstart; zoneplot=false; Gsearched=false; doperiod=-1; button=NEXT; } if (button==SUBTRACTMEAN && nfiles>1) { plotno = -1; nstart = 0; nplot=ngulp_original; ngulp=ngulp_original; button=PLOT; // Skip to end of skipped data for (int jj=0;jj<ngulp;jj++){ float sum; sum=0.0; for (i=1;i<nfiles;i++){ sum+=time_series[i][jj]; } time_series[0][jj]-=sum/(float(nfiles-1)); } Gsearched=false; } if (button==ZAPCOMMON && nfiles>1) { plotno = -1; nstart = 0; nplot=ngulp_original; ngulp=ngulp_original; button=PLOT; float pdfs[nfiles][MAXSIGMA]; // create pdfs for each beam for (int i=0;i<nfiles;i++) formpdf(pdfs[i],MAXSIGMA,ngulp,time_series[i]); // for each point in each beam, mask if improbable float thresh = 3.0; int nbeammax = 5; zap_improbables(pdfs,time_series,nfiles,ngulp,MAXSIGMA,thresh,nbeammax); // Skip to end of skipped data //for (int jj=0;jj<ngulp;jj++){ // float sum; // sum=0.0; // for (i=1;i<nfiles;i++){ // sum+=time_series[i][jj]; // } // time_series[0][jj]-=sum/(float(nfiles-1)); //} //Gsearched=false; } if (button==NEXT) { ngulp=ngulp_original; nstart=0; nplot=ngulp_original; // Read the data int NActuallyRead; // unsigned char *buffer; char *buffer; buffer = new char[ngulp*nbits/8]; //buffer = new char[ngulp*nbits/8]; for (i=0; i<nfiles; i++) { // NActuallyRead = fread(time_series[i],sizeof(float),ngulp,inputfile[i]); NActuallyRead = fread(buffer,nbits/8,ngulp,inputfile[i]); if (nbits==32){ memcpy(time_series[i],buffer,sizeof(float)*ngulp); } else { for (int j=0;j<NActuallyRead;j++){ if (ssigned) time_series[i][j]=(float)buffer[j]; if (!ssigned) time_series[i][j]=(float)((unsigned char)buffer[j]); } } puti(ngulp); if (NActuallyRead!=ngulp){ fprintf(stderr,"Could not read %d floats from file\n",ngulp); ngulp = NActuallyRead; } if(nfiles==1){ // Add fake pulsar here.... // for (int ii=0;ii<ngulp;ii++) time_series[i][ii]+= 10.0*pow(sin(float(ii*2.0*M_PI/60.0)),250.0); } //normalise(ngulp,time_series[i]); } nfileptr+=ngulp; button = PLOT; plotno= -1; zoneplot=true; } if (button==RESET) { button = plotno = -1; nstart=0; nplot=ngulp; button=PLOT; zoneplot=true; Gsearched=false; if (ans=='p'){ doperiod=-1; } } if (plotno>0){ /* if (ans=='p'){ // hit p on a plot to type in a period d->plotregions[plotno].reset(); //plot the thing; fprintf(stderr,"Please enter a period in seconds: "); cin>>trialperiod; xperiod = x; doperiod=plotno; button=PLOT; }*/ if (ans=='p'){ // hit p on a plot to type in a period d->plotregions[plotno].reset(); //plot the thing; fprintf(stderr,"Please enter a period in seconds: "); cin>>trialperiod; xperiod = (double)x; doperiod=plotno; button=PLOT; } if (ans=='m'){ // subtract 0.0000005 seconds from period d->plotregions[plotno].reset(); trialperiod-=0.0000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='/'){ // add 0.0000005 seconds to period d->plotregions[plotno].reset(); trialperiod+=0.0000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans==','){ // subtract 0.000005 seconds from period d->plotregions[plotno].reset(); trialperiod-=0.000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='.'){ // add 0.000005 seconds to period d->plotregions[plotno].reset(); trialperiod+=0.000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='<'){ // subtract 0.001 seconds from period d->plotregions[plotno].reset(); trialperiod-=0.001; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='>'){ // add 0.001 seconds to period d->plotregions[plotno].reset(); trialperiod+=0.001; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='X'){ // right click two points on a plot to calculate and plot a period d->plotregions[plotno].reset(); cpgsci(3); cpgmove(x,-1000); cpgdraw(x,1000); if (ngates==0){ xgate=x; ngates++; } else { min_means_min(&x,&xgate); printf("Period from %f to %f is %f\n",x,xgate,xgate-x); doperiod=plotno; xperiod = (double)x; trialperiod=(double)(xgate-x); ngates=0; button=PLOT; } } if (ans=='D'){ markers[nmarkers]=(int)(x/NPIXELS)*nplot+nstart+nfileptr-ngulp; nmarkers++; zoneplot=true; } if (ans=='A'){ d->plotregions[plotno].reset(); cpgsci(2); cpgmove(x,-1000); cpgdraw(x,1000); if (ngates==0){ xgate=x; ngates++; } else { min_means_min(&x,&xgate); // printf("x %f xgate %f tstart %f\n",x,xgate,tstart); nstart=(int)((x-tstart)/delta)+nstart; nplot=(int)((xgate-x)/delta); //if (nplot<NPIXELS) nplot=NPIXELS; ngates=0; button=PLOT; zoneplot=true; // printf("nplot %d nstart %d\n",nplot,nstart); } } if (ans=='z'){ if (NPIXELS>nplot) { nstart+=(int)x; }else nstart=(int)(x/(float)NPIXELS*nplot)+nstart; printf("nstart %d\n",nstart); nplot/=4; printf("nplot %d\n",nplot); nstart-=nplot/2; printf("nstart %d\n",nstart); //if (nplot<NPIXELS){nplot=NPIXELS;} button=PLOT; zoneplot=true; } }
main(int argc, char** argv) { int dum_int, iread, iw, indx, ITStart, namelen, HdrSize, NTOT, NSkip, NPtsToRead, SkiByte, log2NTOT, NDM, NOPFiles, MAX, NTSampInRead, i, NSampInRead, BytePerFrame, NByteInRead, ITOffset, NBitChan, *ibrev, NRead, IFiles, FOld, FNew, FSwitch, KeepTrack, NTReadOld, NTReadNew, NReadOld, NReadNew, OPFileSize, IOffset, SkipByte, oldper, newper ,NT_Files; long long TotalTrack; float FMin, FMax, FCen, *Inbuf, *Outbuf, timecount; char *filename, *filefull, *parfile; FILE *fpar, *Fout[8192], *Fin; if (argc <= 1) tree_help(); else tree_parms(argc, argv); filename = (char *) calloc(120, sizeof(char)); filefull = (char *) calloc(120, sizeof(char)); parfile = (char *) calloc(120, sizeof(char)); ITStart = 0; NT_Files = 0; /* To read a sample header from first time file */ strcpy(filename, unfname); namelen = strlen(filename); printf("First filename : %s\n", filename); if ((fpar = fopen(filename, "rb")) == NULL) { printf("ERROR opening file %s.\n", filename); exit(0); } HdrSize = read_header(fpar); fclose(fpar); FMin = fch1; FMax = FMin + (float)(nchans - 1) * foff; FCen = (FMin + FMax) / 2.0; printf("No. of frequency channels : %d\n",nchans); printf("Beginning radio frequency : %f MHz\n", FMin - (foff / 2.0)); printf("Ending radio frequency : %f MHz\n", FMax + (foff / 2.0)); printf("Centre frq. of the whole band : %f MHz\n", FCen); printf("\n"); printf("input sampling interval : %f sec\n", tsamp); NTOT = 0; strcpy(filename, unfname); namelen = strlen(filename); if ((fpar = fopen(filename, "rb")) == NULL) { printf("ERROR opening %s.\n", filename); } HdrSize = read_header(fpar); fclose(fpar); NT_Files = (int)nsamples(filename, HdrSize, nbits, nifs, nchans); NTOT += NT_Files; printf("File %5d : %s with %d samples\n", i, filename, NT_Files); NSkip = (int)(TSkip / tsamp); if (NSkip > NT_Files) { printf("Initial Skip-length longer than the first file!\n"); exit(0); } NPtsToRead = (int)(TRead / tsamp); if (NPtsToRead==0) { TRead=NT_Files*tsamp; NPtsToRead=NT_Files; } if (NPtsToRead > NTOT) NPtsToRead = NTOT; SkipByte = (NSkip * nbits * nchans / 8); printf("\n"); printf("This data set contains %d number of samples\n", NTOT); printf("Number of bits per sample : %d\n", nbits); printf("Time length to skip at the begin. : %f\n", TSkip); printf("Bytes to skip at the beginning : %d\n", SkipByte); printf("No. of time samples to skip : %d\n", NSkip); printf("Time length to read after skipping : %f\n", TRead); printf("Samples to read after skipping : %d\n", NPtsToRead); NTOT -= NSkip; printf("Time samples after initial skip : %d\n", NTOT); if (NPtsToRead > NTOT) { NPtsToRead = NTOT; printf("Too many samples to read. Truncated to %d samples\n", NTOT); } else NTOT = NPtsToRead; log2NTOT = (int)(log((double)NTOT) / log((double)2.0)); NTOT = (1 << log2NTOT); printf("After truncating to lower 2^n : %d\n", NTOT); if (DMMin < 0) DMMin = 0; if (DMMax >= nchans) DMMax = (nchans - 1); if ((DMMin == 0) && (DMMax == 0)) { DMMin = 0; DMMax = (nchans - 1); } else if ((DMMin != 0) && (DMMax == 0)) DMMax = (nchans - 1); printf("\n"); printf("Minimum DM index = %d Maximum = %d\n", DMMin, DMMax); DMMinv=(tsamp/8.3e3)*(double)(DMMin)*pow((FMax+FMin)/2.,3.)/fabs(FMax-FMin); DMMaxv=(tsamp/8.3e3)*(double)(DMMax)*pow((FMax+FMin)/2.,3.)/fabs(FMax-FMin); printf("Minimum DM value = %f Maximum = %f\n", DMMinv,DMMaxv); NDM = (DMMax - DMMin + 1); NOPFiles = NDM; OPFileSize = sizeof(float) * NTOT; printf("\n"); printf("Total number of output files : %d\n", NOPFiles); printf("Size of each output file : %d bytes\n", OPFileSize); for (i=0; i<NOPFiles; i++) { strcpy(filefull,unfname); sprintf(&filefull[strlen(filefull)-4], ".DM%.4d.tim", (i+DMMin)); if((Fout[i] = fopen(filefull,"wb")) == NULL) { printf("ERROR opening output file %s.\n", filefull); exit(0); } else { nbands=1; nobits=32; refdm=(tsamp/8.3e3)*(double)(i+DMMin)*pow((FMax+FMin)/2.,3.)/ fabs(FMax-FMin); output=Fout[i]; dedisperse_header(); } } printf("\n"); /* strcpy(parfile, "\0"); strcpy(parfile, unfname); strncat(parfile, ".par", 4); fpar = fopen(parfile, "w"); fprintf(fpar, "%s\n", unfname); fprintf(fpar, "%d %d %10.8f \n ", NTOT, nchans, tsamp); fprintf(fpar, "%d %d %d %d\n", DMMin, DMMax, NDM, NOPFiles); fprintf(fpar, "%d\n", nchans); fprintf(fpar, "%f %f\n", FMin, FMax); fclose(fpar); */ MAX = (1 << 12); if (MAX > NTOT) MAX = NTOT; NTSampInRead = (MAX + (2 * nchans)); NSampInRead = (nchans * NTSampInRead); BytePerFrame = (nchans * nbits / 8); NByteInRead = (NTSampInRead * BytePerFrame); ITOffset = 2 * nchans; IOffset = (-ITOffset * BytePerFrame); NBitChan = (int)(log((double)nchans) / 0.6931471); printf("Time samples to read in one read : %d\n", NTSampInRead); printf("Bytes to read in one read : %d bytes\n", NByteInRead); printf("Offset counter for each read : %d time samples\n", IOffset); printf("\n"); printf("No of bits to represent all channels : %d\n", NBitChan); Inbuf = (float *) calloc(NSampInRead, sizeof(float)); Outbuf = (float *) calloc(NSampInRead, sizeof(float)); ibrev = (int *) calloc(nchans, sizeof(int)); for (i=0; i<nchans; i++) { ibrev[i] = bitrev(i, NBitChan); } NRead = (int)(NTOT / MAX); printf("Total number of reads in the run : %d\n", NRead); IFiles = 0; FOld = 0; FNew = 0; FSwitch = 1; strcpy(filename, unfname); namelen = strlen(filename); printf("First filename : %s\n", filename); if ((Fin = fopen(filename, "rb")) == NULL) { printf("ERROR opening file %s.\n", filename); exit(0); } HdrSize = read_header(Fin); printf("Going into the main loop\n"); fseek(Fin, SkipByte, SEEK_CUR); KeepTrack = (NT_Files - NSkip); TotalTrack = 0; newper = 0.0; oldper = newper; printf("\n\n"); for (iread=0; iread<NRead; iread++) { newper = 100.0 * (((float)iread / (float)NRead)); if (newper > oldper) { timecount = ((float)iread * NTSampInRead * tsamp); printf("\rProcessed : %3d%% Current file : %s Time from beg : %8.2f sec", (int)newper, filename, timecount); fflush(stdout); oldper = newper; } KeepTrack -= NTSampInRead; TotalTrack += NTSampInRead; if (KeepTrack > 0) { read_block(Fin, nbits, Inbuf, NSampInRead); fseek(Fin, IOffset, SEEK_CUR); KeepTrack += ITOffset; TotalTrack -= ITOffset; } if (KeepTrack > 0) FSwitch = 1; memset(&Outbuf[0], 0, (sizeof(float) * NSampInRead)); AxisSwap(Inbuf, Outbuf, nchans, NTSampInRead); if (FlipSwitch == 0) { printf("BEFORE..."); fflush(stdout); FlipBand(Outbuf, nchans, NTSampInRead); printf("AFTER!\n"); } taylor_flt(Outbuf, NSampInRead, nchans); for (iw=DMMin; iw<=DMMax; iw++) { indx = (ibrev[iw] * NTSampInRead); fwrite(&Outbuf[indx], sizeof(float), MAX, Fout[iw-DMMin]); } } newper = 100.0 * ((float)iread / NRead); timecount = ((float)iread * NTSampInRead * tsamp); printf("\rProcessed : %3d%% Current file : %s Time from beg : %8.2f sec", (int)newper, filename, timecount); fflush(stdout); printf("\n\n"); for (i=0; i<NOPFiles; i++) fclose(Fout[i]); exit(0); }
int main (int argc, char *argv[]) { /* local variables */ // char string[180]; int i,useroutput=0,nfiles=0,fileidx,sigproc,scan_number,subscan=0; int numsamps=0; long long int totnumsamps=0; unsigned char * rawdata; unsigned short int * unpacked; //, * times; int nbytesraw; int ibyte,j,k; unsigned char abyte; unsigned short int ** times; unsigned short int * dmzero; int nread; float DM_trial; int ndm=0; double ti = 40.0; double tol = 1.25; float total_MBytes = 0; float start_DM=0.0, end_DM; int counts; int dmlogfile=0; int readsamp = 0; int nreadsamp = 0; int skip = 0; int nskip = 0; int ntoload; int ntodedisp; int maxdelay = 0; int appendable = 0; int ngulp; //max number of samples at a time int gulping = 0; int ngulpsize; int nsampleft; char * killfile; int killing=0; bool doGsearch = 0,doMultibeam=0; int Gwidtol = 30; float Gthresh = 6; int Gscrnch = 256; int Goffset = 0; float Girrel = 3; char *Gfilename; Gfilename = (char *) malloc(13); strcpy(Gfilename,"GResults.txt"); float flo,fhi; if(sizeof(LONG64BIT) != 8 ){ fprintf(stderr,"ERROR: sofware has been compiled with LONG64BIT as a datatype of %d bytes, needs to be 8\n",sizeof(LONG64BIT)); } /* check number of command-line arguments and print help if necessary */ if (argc<2) { inline_dedisperse_all_help(); exit(0); } /* print help if necessary */ if (strcmp(argv[1],"-h")==0) { inline_dedisperse_all_help(); exit(0); } /* set up default globals */ userbins=usrdm=asciipol=stream=clipping=swapout=headerless=0; sumifs=wapp_inv=wapp_off=barycentric=0; nobits=32; ascii=1; fftshift=1; profnum1 = 0; profnum2 = 1000; nbands=baseline=1; clipvalue=refrf=userdm=fcorrect=0.0; refdm=-1.0; output=NULL; randomise=false; zerodm=false; strcpy(ignfile,""); // ************************************** // PARSE COMMAND LINE // ************************************** i = 1; while (i<argc) { if (fopen(argv[i],"rb")!=NULL){ strcpy(inpfile,argv[i]); input=fopen(inpfile,"rb"); if (input==NULL){ fprintf(stderr,"Error opening file %s\n",inpfile); exit(-1); } nfiles++; } else if (!strcmp(argv[i],"-d")) { /* get DM from the user */ start_DM=atof(argv[++i]); end_DM=atof(argv[++i]); usrdm=1; } else if (!strcmp(argv[i],"-l")) { /* Create a log file of the DMs */ dmlogfile=1; } else if (!strcmp(argv[i],"--randomise")) { /* randomise channels */ randomise=true; } else if (!strcmp(argv[i],"--zerodm")) { /* randomise channels */ zerodm=true; } else if (!strcmp(argv[i],"-i")) { /* set intrinsic width */ ti=atof(argv[++i]); } else if (!strcmp(argv[i],"-tol")) { /* set tolerance level (e.g. 25% = 1.25)*/ tol=atof(argv[++i]); } else if (!strcmp(argv[i],"-v")) { verbose=1; } else if (!strcmp(argv[i],"-n")) { /* read only X samples */ ntodedisp=atoi(argv[++i]); readsamp=1; } else if (!strcmp(argv[i],"-s")) { /* skip first X samples */ nskip=atoi(argv[++i]); skip=1; } else if (!strcmp(argv[i],"-m")) { /* sub-band */ nbands=atoi(argv[++i]); } else if (!strcmp(argv[i],"-g")) { ngulpsize=atoi(argv[++i]); gulping=1; } else if (!strcmp(argv[i],"-a")) { appendable=1; } else if (!strcmp(argv[i],"-k")) { killing = 1; killfile = (char *) malloc(strlen(argv[++i])+1); strcpy(killfile,argv[i]); } else if (!strcmp(argv[i],"-or")) { output_rotate=atoi(argv[++i]); } else if (!strcmp(argv[i],"-os")) { output_subtract=atoi(argv[++i]); } else if (!strcmp(argv[i],"-G")) { doGsearch = 1; fprintf(stderr,"Will perform giant pulse search\n"); } else if (!strcmp(argv[i],"-wid")) { Gwidtol = atoi(argv[++i]); } else if (!strcmp(argv[i],"-sig")) { Gthresh = atof(argv[++i]); } else if (!strcmp(argv[i],"-dec")) { Gscrnch = atoi(argv[++i]); } else if (!strcmp(argv[i],"-cut")){ Girrel = atof(argv[++i]); } else if (!strcmp(argv[i],"-file")){ Gfilename = (char *) malloc(strlen(argv[++i])+1); strcpy(Gfilename,argv[i]); } else if (!strcmp(argv[i],"-mb")){ doMultibeam = 1; } else { /* unknown argument passed down - stop! */ inline_dedisperse_all_help(); fprintf(stderr,"unknown argument (%s) passed to %s\n\n",argv[i],argv[0]); exit(1); } i++; } // ************************************** // ERROR TESTING // ************************************** if (doGsearch && nbands>1){ fprintf(stderr,"Can't do gsearch while running in subband mode!\n"); exit(-1); } if (!useroutput) { /* no output file selected, use standard output */ output=stdout; strcpy(outfile,"stdout"); } if (!nfiles) { fprintf(stderr,"File not supplied on command line; please supply filename here: "); strcpy(inpfile,"stdin"); input=fopen(inpfile,"rb"); if (input==NULL){ fprintf(stderr,"Error opening file %s\n",inpfile); exit(-1); } nfiles=1; } fileidx=1; if (nfiles>1) { fprintf(stderr,"Multi file mode not supported yet!\n"); exit(-1); } char tmp[180]; char *tmp2; strcpy(tmp,inpfile); // These few lines strip the input file's tmp2 = basename(tmp);// name from it's directory tag. strcpy(outfile_root,tmp2); // ************************************** // VALUE PRECALCULATION // ************************************** /* read in the header to establish what the input data are... */ sigproc=read_header(input); if (!sigproc) { fprintf(stderr,"Not sigproc data\n"); exit(-1); } if (foff > 0.0) { fprintf(stderr,"dedisperse can't handle low->high frequency ordering!"); exit(1); } // But in case low->high frequency ordering ever gets implemented... if (foff < 0){ flo = (float)fch1 + (nchans*(float)foff); fhi = (float)fch1; } else { flo = (float)fch1; fhi = (float)fch1 + (nchans*(float)foff); } if (fileidx == 1) { /* this is filterbank data */ if (output!=stdout) output=fopen(outfile,"wb"); if (output==NULL){ fprintf(stderr,"Error opening file %s\n",output); exit(-1); } } numsamps = nsamples(inpfile,sigproc,nbits,nifs,nchans); /* get numsamps */ totnumsamps = nsamples(inpfile,sigproc,nbits,nifs,nchans); if (usrdm) maxdelay = DM_shift(end_DM,nchans,tsamp,fch1,foff); cout << "maxdelay = " << maxdelay << endl; if (readsamp) numsamps=ntodedisp+maxdelay; if (gulping) { if (ngulpsize>numsamps) ngulpsize = numsamps-maxdelay; ntodedisp=ngulpsize; } else { ntodedisp=numsamps-maxdelay; ngulpsize=ntodedisp; } ntoload = ntodedisp + maxdelay; Goffset = (int)(ntoload-maxdelay); nbytesraw = nchans * ntoload * nbits/8; if ((rawdata = (unsigned char *) malloc(nbytesraw))==NULL){ fprintf(stderr,"Error allocating %d bytes of RAM for raw data\n",nbytesraw); exit(-1); } // skip either 0 or nskip samples fseek(input, nskip*nchans*nbits/8, SEEK_CUR); nsampleft-=nskip; // some values used for unpacking int sampperbyte = (int)(8/nbits); int andvalue = (int)pow(2,nbits)-1; // ************************************** // SET UP DMTABLE AND SUBBANDS // ************************************** float * DMtable; // a hack for less files when subbanding... M.Keith if (nbands==1) getDMtable(start_DM,end_DM, tsamp*1e6, ti, foff, (fch1+(nchans/2-0.5)*foff)/1000,nchans/nbands, tol, &ndm, DMtable); else getDMtable(0.0,end_DM, tsamp*1e6, ti, foff, (fch1+(nchans/2-0.5)*foff)/1000,nchans/nbands, tol, &ndm, DMtable); // If Gsearch is being run but start_DM is non-zero, add 0.0 to DM table. if (doGsearch && start_DM != 0.0){ float *tempDMtable = new float[ndm+1]; tempDMtable[0] = 0.0; #pragma omp parallel for private(i) for (int i=0;i<ndm;i++){ tempDMtable[i+1] = DMtable[i]; } delete DMtable; DMtable = tempDMtable; ndm++; } fprintf(stderr,"%d subbands from %d chans\n",nbands,nchans); if(nchans % nbands){ fprintf(stderr,"invalid number of subbands selected!\n"); exit(1); } int nwholegulps = (numsamps - maxdelay)/ngulpsize; int nleft = numsamps - ngulpsize * nwholegulps; int ngulps = nwholegulps + 1; // if (!debird){ unpacked = (unsigned short int *) malloc(nchans*ntoload* sizeof(unsigned short int)); if (unpacked==NULL) { fprintf(stderr,"Failed to allocate space for unpacked %d bytes\n", (int)(nchans*ntoload*sizeof(unsigned short int))); exit(-2); } if (zerodm){ dmzero = (unsigned short int *) malloc(sizeof(unsigned short int)*ntodedisp); } times = (unsigned short int **) malloc(sizeof(unsigned short int*)*nbands); for(int band=0; band < nbands; band++){ times[band] = (unsigned short int *) malloc(sizeof(unsigned short int)*ntodedisp); } if (times==NULL){ fprintf(stderr,"Error allocating times array\n"); exit(-1); } // } // (!debird) int * killdata = new int[nchans]; if (killing) int loaded = load_killdata(killdata,nchans,killfile); else { killing=1; for (int i=0;i<nchans;i++) killdata[i]=1; // ie don't kill anything. } int prerotate=0; if (zerodm){ while(nchans*(pow(2,nbits)-1)*(float)(pow(2,prerotate)) < 32768) prerotate++; max_chan_val=(pow(2,nbits)-1)*(float)(pow(2,prerotate)); printf("Using 'ZERODM' RFI reduction method\n"); printf("Multiplying input by %d to increase dynamic range for zerodm removal\n",(int)(pow(2,prerotate))); } int rotate = 0; while(pow(2,prerotate)*nchans*(pow(2,nbits)-1)/(float)nbands/(float)(pow(2,rotate)) > 255) rotate++; rotate-=output_rotate; if (output_rotate){ printf("Warning: Modifying scale factor by %d, some clipping may occur!\n",(int)(pow(2,output_rotate))); } printf("Dividing output by %d to scale to 1 byte per sample per subband\n",(int)(pow(2,rotate))); if(randomise){ sprintf(outfile_root,"%s_RAND",outfile_root); printf("WARNING: Randomising channel order! Data will not be astrophysical.\n"); } // Set up gpulse control variables GPulseState Gholder(ndm); // Giant pulse state to hold trans-DM detections int Gndet; // Integer number of detections in this beam int *Gresults; // Integer array of results: contained as cand1.start.bin cand1.end.bin cand2.start.bin cand2.end.bin...... etc) //Gresults = new int[totnumsamps*ndm]; // This declaration might not be necessary if (doGsearch){ FILE *Gresultsfile; Gresultsfile = fopen(Gfilename,"w"); if (Gresultsfile==NULL){ fprintf(stderr,"Error opening giant search results output file %s.\nWILL NOT RUN GIANT SEARCH.\n\n",Gresultsfile); doGsearch = false; } else { //HERE open Gfile and print out a short "header". Gfilename is the name of the file. // header needs to contain: // infilename, nsamp, tsamp, ctr. frequency, bandwidth, RA, DEC, UTC time/date of obs, snr limit, Ndms, DM search range. fprintf(Gresultsfile,"# %s %lld %g %g %g %g %g %g %.3f %d %.2f %.2f\n",outfile_root,totnumsamps,tsamp,fch1 + 0.5*(nchans*foff),fabs(foff*nchans),(src_raj),(src_dej),(src_dej),Gthresh,ndm,start_DM,end_DM); } fclose(Gresultsfile); } // Start of main loop for (int igulp=0; igulp<ngulps;igulp++){ //rewind a few samples if not at first gulp if (igulp!=0){ fprintf(stderr,"Skipping back %d bytes\n",maxdelay*nbits*nchans/8); int ret = fseek(input,-1*maxdelay*nbits*nchans/8,SEEK_CUR); if(ret){ fprintf(stderr,"Could not skip backwards!\n"); exit(1); } } if (igulp==nwholegulps) { ntoload = nleft; nbytesraw = ntoload*nbits*nchans/8; ntodedisp = nleft - maxdelay; } //read gulp from file fprintf(stderr,"Gulp %d Loading %d samples, i. e. %d bytes of Raw Data\n",igulp, ntoload, nbytesraw); nread = fread(rawdata,nbytesraw,1,input); if (nread != 1){ fprintf(stderr, "Failed to read %d nread = %d \n", nbytesraw, nread); exit(-1); } /* Unpack it if dedispersing */ if (1){ if (verbose) fprintf(stderr,"Reordering data\n"); // all time samples for a given freq channel in order in RAM for (ibyte=0;ibyte<nchans/sampperbyte;ibyte++){ #pragma omp parallel for private (abyte,k,j) for (j=0;j<ntoload;j++){ abyte = rawdata[ibyte+j*nchans/sampperbyte]; for (k=0;k<8;k+=nbits) unpacked[j+((ibyte*8+k)/(int)nbits)*ntoload]=(unsigned short int)((abyte>>k)&andvalue) << prerotate; } } if (1==0){ // Old way. if (killing){ for (int i=0;i<nchans;i++){ if (!killdata[i]){ cout << i << " " ; #pragma omp parallel for private(j) for (int j=0;j<ntoload;j++) unpacked[j+i*ntoload]=0; } } } } cout << endl; // flush the buffer /* for each DM dedisperse it */ /* Start with zero DM */ if (!usrdm) end_DM=1e3; if (dmlogfile && igulp==0){ sprintf(dmlogfilename,"%s.dmlog",outfile_root); dmlogfileptr=fopen(dmlogfilename,"w"); if (dmlogfileptr==NULL) { fprintf(stderr,"Error opening file %s\n",dmlogfilename); exit(-3); } } if (igulp==0) appendable=0; else appendable=1; if (zerodm){ do_dedispersion(&dmzero, unpacked, 1, ntoload, ntoload, 0, killdata); do_zerodm(dmzero, unpacked, ntodedisp, ntoload, killdata); } for (int idm=0;idm<ndm;idm++) { //DM_trial = get_DM(idm,nchans,tsamp,fch1,foff); DM_trial = DMtable[idm]; if ((DM_trial>=start_DM && DM_trial<=end_DM) || (doGsearch && DM_trial==0)){ if (verbose) fprintf(stderr,"DM trial #%d at DM=%f ",idm,DM_trial); if (dmlogfile && igulp==0) fprintf(dmlogfileptr,"%f %07.2f\n",DM_trial, DM_trial); // Name output file, dmlog file if(nbands==1)sprintf(outfile,"%s.%07.2f.tim",outfile_root,DM_trial); else sprintf(outfile,"%s.%07.2f.sub",outfile_root,DM_trial); // open it if (appendable){ outfileptr=fopen(outfile,"a"); if (outfileptr==NULL) { fprintf(stderr,"Error opening file %s\n",outfile); exit(-3); } } if (!appendable){ outfileptr=fopen(outfile,"w"); if (outfileptr==NULL) { fprintf(stderr,"Error opening file %s\n",outfile); exit(-3); } // create a log of the 'sub' dms to dedisperse at // These DMs are chosen fairly arbitraraly, rather than // based on any ridgid mathematics. if(nbands > 1){ float* DMtable_sub; int ndm_sub; float sub_start_DM=0; float sub_end_DM=end_DM; if (idm > 0){ sub_start_DM = (DMtable[idm]+ DMtable[idm-1])/2.0; } if (idm < ndm-1){ sub_end_DM = (DMtable[idm]+ DMtable[idm+1])/2.0; } // make the dm table for the sub-banded data... getDMtable(sub_start_DM,sub_end_DM, tsamp*1e6, ti, foff, (fch1+(nchans/2-0.5)*foff)/1000, nchans, tol, &ndm_sub, DMtable_sub); char subdm_filename[180]; sprintf(subdm_filename,"%s.valid_dms",outfile); FILE* subdm_fileptr = fopen(subdm_filename,"w"); if(subdm_fileptr == NULL){ printf("Error: could not open file %s for writing\n",subdm_filename); exit(2); } for(int ix = 0; ix < ndm_sub; ix++){ double sub_dm_trial = DMtable_sub[ix]; fprintf(subdm_fileptr,"%f\n",sub_dm_trial); } delete[] DMtable_sub; fclose(subdm_fileptr); } } output=outfileptr; // write header variables into globals // dedisperse_header() uses "userdm" not refdm, so make sure to set that. MJK-19042010 refdm = DM_trial; userdm = refdm; nobits = 8; // write header if (!appendable) dedisperse_header(); // do the dedispersion do_dedispersion(times, unpacked, nbands, ntodedisp, ntoload, DM_trial, killdata); // Do the Gsearch for this DM trial if (doGsearch){ int runningmeanval = (int)(2.0/tsamp); //will smooth over two seconds. // sprintf"%f %07.2f\n",DM_trial, //FORMAT DM HERE? float *temptimes = new float[ntodedisp]; if (runningmeanval>ntodedisp){ fprintf(stderr,"***Giants warning: running mean longer than data; will not remove baseline\n"); Gholder.searchforgiants(idm,ntodedisp,(int)(Goffset*igulp),times[0],Gthresh,Gwidtol,Gscrnch,DM_trial,1); } else if (temptimes == NULL) { fprintf(stderr,"***Giants warning: Couldn't allocate %d floats; will not remove baseline\n",ntodedisp); Gholder.searchforgiants(idm,ntodedisp,(int)(Goffset*igulp),times[0],Gthresh,Gwidtol,Gscrnch,DM_trial,1); } else { removebaseline(times[0],temptimes,ntodedisp,runningmeanval,3.0); Gholder.searchforgiants(idm,ntodedisp,(int)(Goffset*igulp),temptimes,Gthresh,Gwidtol,Gscrnch,DM_trial,1); } delete[] temptimes; } // write data if (1==1){ #if SIGNED char lotsofbytes[ntodedisp]; #else unsigned char lotsofbytes[ntodedisp]; #endif for (int d=0;d<ntodedisp;d++){ for(int iband=0; iband<nbands; iband++){ unsigned short int twobytes = times[iband][d]>>rotate; if (output_subtract){ twobytes-=output_subtract; if (twobytes > 32768){ twobytes=0; } } if (output_rotate && twobytes > 255){ twobytes=255; } // unsigned char onebyte = twobytes; #if SIGNED // here we subtract 128 to make signed since numbers were scaled // to be between 0 -> 256 lotsofbytes[d]=(twobytes-128); #else lotsofbytes[d]=(twobytes); #endif } } fwrite(lotsofbytes,ntodedisp,1,outfileptr); } // close file fclose(outfileptr); if (verbose) fprintf(stderr,"%d bytes to file %s\n", ntodedisp*nbands, outfile); total_MBytes+= (ntodedisp*nbands)/1.0e6; } // if DM is in range } // DM for loop if (verbose) fprintf(stderr,"Wrote a total of %6.1f MB to disk\n",total_MBytes); /* close log files if on last input file */ if (dmlogfile && igulp==0) fclose(dmlogfileptr); } //if (!debird) // After gulp's done, pump out the Gsearch results for that gulp if (doGsearch){ if (verbose) fprintf(stderr,"Completeing Gsearch results for this gulp\n"); string UTroot = outfile_root; int pos = UTroot.find(".fil"); if( pos != string::npos) UTroot.replace(pos, 4, ""); Gresults = Gholder.givetimes(&Gndet,tsamp,flo,fhi,Girrel,&UTroot[0],ibeam,Gfilename); Gholder.selfdestruct(); // for (i=0;i<ndm;i++) Gholder.DMtrials[i].erase(Gholder.DMtrials[i].begin(),Gholder.DMtrials[i].end()); // fprintf(stderr,"GRESULTS:\n"); // for (int i=0;i<Gndet;i+=2){ // fprintf (stderr,"Detection %d: %d\t%d\n",i,Gresults[i],Gresults[i+1]); // } if (verbose) fprintf(stderr,"Completed Gsearch results for this gulp\n"); } } //end per-gulp loop