void plot_channel_data() { int samp=0; int pg_id; pg_id = cpgopen("/XSERVE"); cpgpap(8.0, 0.8); cpgask(0); cpgpage(); cpgslct(pg_id); cpgsci(3); cpgeras(); cpgsvp(0.15f, 0.95f, 0.2f, 0.8f); cpgupdt(); cpgsch(2.0); cpgswin(0, read_count, -0.1f, 0.1f); cpgbox("BC1NST",0.0,0,"BCNST",0.0,0); cpglab("Time [samples]", "Voltage [volts]", "Antenna Measurement Receiver"); cpgmove(samp, voltarray[0]); for (samp=2; samp<read_count; samp++) { cpgdraw(samp, voltarray[samp]); } return 0; }
void Cpg_Reset(void) { /* Erase scene */ cpgeras(); /* Redraw bounding box */ Cpg_Box("BCNST", 0.0, 0, "BCNST", 0.0, 0); return; }
int plot_freq_data(void) { int bin=0; printf("\nPlotting ..."); cpgask(0); cpgpage(); cpgslct(pg_id); cpgsci(1); cpgeras(); cpgsvp(0.15f, 0.95f, 0.2f, 0.8f); cpgupdt(); cpgsch(2.0); cpgswin(0, (N/2)+1, 0.0f, 0.005f); // cpgswin(80, 120, 0.0f, 0.01f); cpgbox("BC1NST",0.0,0,"BCNST",0.0,0); cpglab("Frequency [bins]", "Peak Voltage [volts]", "Antenna Measurement Receiver"); cpgmove(bin, accumFreqData[0]); for (bin=1; bin<(N/2)+1; bin++) { cpgdraw(bin, accumFreqData[bin]); } return 0; }
int main (int argc, char *argv[]) { int ntimglobal=0; // number of time samples in original int ngulp_original=0; // number of time samples to look at at once int nskipstart=0; // number skipped at start int nrejects; //ZAPPER int zapswitch = 0; //ZAPPER double tsamp_orig=0; //gsearch setup & defaults float Gsigmacut=6.0; float delta, tstart; vector<Gpulse> * Giant = new vector<Gpulse>[MAXFILES]; bool Gsearched=false; int i,ntim,headersize[MAXFILES],noff=0,gulp; float *time_series[MAXFILES],sum=0.0,sumsq=0.0,mean,meansq,sigma; int MAXMARKERS = 1024; int nfiles = 0; FILE *inputfile[MAXFILES]; char filename[MAXFILES][256]; int spectra=0; int powerspectra=0; double dmoffirstfile; char *killfile; bool dokill=false; bool ssigned=true; bool fsigned=false; int topfold=-1; int topgiant=-1; int toppeak=-1; //?!? sarah added this bool askdevice=false; char devicename[200]; if (argc<2 || help_required(argv[1])) { helpmenu(); // fprintf(stderr,"Usage: giant filenames\n\t(e.g.>> giant *.tim)\n\n\t-s N\tskip N samples\n\t-n N\tread N samples\n\t-S read spectra instead of amplitudes\n-i interpret signed chars as unsigned\n\t-z make a zap list of bad time samples\n"); exit(0); } print_version(argv[0],argv[1]); i=1; while (i<argc) { if (file_exists(argv[i])) { inputfile[nfiles]=open_file(argv[i],"r"); strcpy(filename[nfiles],argv[i]); nfiles++; } if (strings_equal(argv[i],"-s")) sscanf(argv[++i],"%d",&nskipstart); if (strings_equal(argv[i],"-S")) spectra=1; if (strings_equal(argv[i],"-i")) ssigned=false; if (strings_equal(argv[i],"-f")) fsigned=true; if (strings_equal(argv[i],"-n")) sscanf(argv[++i],"%d",&ngulp_original); if (strings_equal(argv[i],"-c")) sscanf(argv[++i],"%f",&Gsigmacut); if (strings_equal(argv[i],"-z")) zapswitch=1; if (strings_equal(argv[i],"-g")) {askdevice=true;sscanf(argv[++i],"%s",&devicename);} if (strings_equal(argv[i],"-k")) {killfile=(char*)malloc(strlen(argv[++i])+1); strcpy(killfile,argv[i]);dokill=true;} if (nfiles>MAXFILES) error_message("too many open files"); i++; } int ntimglobal_smallest=0, nsamp; for (i=0; i<nfiles; i++) { if (spectra){ int npf; double rate; time_series[i]=Creadspec(filename[i],&npf,&rate); tsamp = 1.0/(rate); //normalise(npf,time_series[i]); nsamp = ntimglobal = ntimglobal_smallest = npf; } else { if ((headersize[i]=read_header(inputfile[i]))) { if (! fsigned){ if (isign > 0) { ssigned=false; fprintf(stderr,"using signed header variable to set UNSIGNED\n"); } if (isign < 0) { ssigned=true; fprintf(stderr,"using signed header variable to set SIGNED\n"); } } if (i==0) dmoffirstfile = refdm; if (nbits!=8 && nbits!=32) error_message("giant currently only works for 8- or 32-bit data"); nsamp = nsamples(filename[i],headersize[i],nbits,nifs,nchans); if (i == 0) { ntimglobal_smallest=nsamp; } else { ntimglobal= nsamp; if (ntimglobal < ntimglobal_smallest) ntimglobal_smallest = ntimglobal; } // Space for data (time_series) time_series[i]=(float *) malloc((nsamp+2)*sizeof(float)); if (time_series[i]==NULL){ fprintf(stderr,"Error mallocing %d floats of %d size\n",nsamp, sizeof(float)); exit(-1); } tsamp_orig = tsamp; // Skip data fprintf(stderr,"Skipping %d bytes\n",nskipstart*nbits/8); fseek(inputfile[i],nskipstart*nbits/8,SEEK_CUR); } // each file } // spectra or not } // for (i...) puti(ntimglobal_smallest); if (ngulp_original==0) ngulp_original=ntimglobal_smallest; // ****** SAM'S ZAP SWITCH ****** // Sam Bates 2009 // Integrated into new giant by SBS // Switch to make a .killtchan file for time samples > 3.5 sigma // SARAHZAP tag means addition was added later by Sarah // ****************************** int ngulp=ngulp_original; // int nrejects_max=ngulp_original/100; int * mown = new int[ngulp_original]; int nstart=0; if (zapswitch){ float dummy; int NActuallyRead; char *buffer; buffer = new char[ngulp*nbits/8]; for (i=0; i<nfiles; i++){ NActuallyRead = fread(buffer,nbits/8,ngulp,inputfile[i]); if (nbits==32){ memcpy(time_series[i],buffer,sizeof(float)*ngulp); } else { for (int j=0;j<NActuallyRead;j++){ if (ssigned) time_series[i][j]=(float)buffer[j]; if (!ssigned) time_series[i][j]=(float)((unsigned char)buffer[j]); } } puti(ngulp); find_baseline(ngulp,time_series[i],10.0/tsamp,5.0); mowlawn(ngulp,time_series[i],5,256); } printf("%f\n",dummy); printf("Bad time samples found...\n"); exit(0); } int pgpID; if (askdevice){ pgpID = cpgbeg(0,devicename,1,1); } else { pgpID = cpgbeg(0,"/xs",1,1); } cpgsch(0.5); cpgtext(0.6,0.0,"Press 'h' over the main window for help and full options list."); cpgsch(1.0); /* create the dialog */ dialog * d = new dialog(); /* add the "action" buttons */ int QUIT = d->addbutton(0.02,0.95,"Quit"); int POWER = d->addbutton(0.07,0.85,"POWER"); int SMHRM = d->addbutton(0.075,0.80,"SMHRM"); int FFT = d->addbutton(0.02,0.85,"FFT"); int PLOT = d->addbutton(0.02,0.80,"Plot"); int NEXT = d->addbutton(0.02,0.75,"Next"); int ZAPPEAK = d->addbutton(0.075,0.75,"ZapPeak"); int RESET = d->addbutton(0.02,0.70,"Reset"); int GLOBALRESET = d->addbutton(0.02,0.65,"Global Reset"); int HALVEPLOT = d->addbutton(0.02,0.60,"Halve Plot"); int BASELINE = d->addbutton(0.02,0.50,"Baseline"); int ZAPCOMMON = d->addbutton(0.02,0.45,"Zap Common"); int SUBTRACTMEAN = d->addbutton(0.02,0.40,"ZAP Mean"); int BSCRUNCH = d->addbutton(0.02,0.35,"Bscrunch"); int NORMALISE = d->addbutton(0.02,0.30,"Normalise"); int HISTOGRAM = d->addbutton(0.02,0.25,"Histogram"); int GSEARCH = d->addbutton(0.02,0.20,"Find Giants"); int MOWLAWN = d->addbutton(0.08,0.70,"LAWN"); int SEEFIL = d->addbutton(0.02,0.15,"View Band"); int FWRITE = d->addbutton(0.02,0.05,"Write File"); /* add the plot regions */ d->addplotregion(0.2,0.99,0.98,0.99); float deltay = 0.9/(float)nfiles; for (i=0; i<nfiles; i++) d->addplotregion(0.2,0.99,0.95-deltay*(float)(i+1),0.95-deltay*(float)i); d->draw(); float x,y; char ans; int button=-1; int plotno=-1; int NPIXELS = 1024; float * xaxis = new float[NPIXELS]; float * ymaxes = new float[NPIXELS]; float * ymins = new float[NPIXELS]; int scrunch=1; int nmarkers=0; int * markers= new int[MAXMARKERS]; int nfileptr=nskipstart; int nplot=ngulp_original; nstart=0; //COMMENTED IN ZAPPER VERSION: MAY CAUSE CONFLICTS IN THIS VER. ngulp=ngulp_original; //COMMENTED IN ZAPPER VERSION: MAY CAUSE CONFLICTS IN THIS VER. double trialperiod; int doperiod=-1; double xperiod; bool zoneplot=false; int ngates=0; float xgate=0.0; button=NEXT; if (spectra) button = PLOT; while (button!=QUIT){ // Plot the zone // Entire file is white if (button!=NEXT)button=d->manage(&x,&y,&ans,&plotno); if (ans=='h'){ buttonexplain(); continue; } // printf("manage x %f y %f plotno %d\n",x,y,plotno); if (button==BASELINE) { for (i=0; i<nfiles; i++){ find_baseline(ngulp,time_series[i],10.0/tsamp,5.0); } button = PLOT; zoneplot=false; plotno = -1; } if (button==FWRITE) { // reread first header and close it. Sets globals. fclose(inputfile[0]); inputfile[0]=open_file(argv[1],"r"); headersize[0]=read_header(inputfile[0]); output = open_file("giant.tim","w"); nobits=32; nbands=1; dedisperse_header(); fprintf(stderr,"Opened file, writing data\n"); fwrite(time_series[0],sizeof(float),ngulp,output); fclose(output); button = -1; zoneplot=false; plotno =-1; } if (button==BSCRUNCH) { for (i=0; i<nfiles; i++){ bscrunch(ngulp,time_series[i]); } tsamp*=2; scrunch*=2; ngulp/=2; nplot/=2; button = PLOT; zoneplot=false; Gsearched=false; plotno = -1; } if (button==FFT) { for (i=0; i<nfiles; i++){ ngulp = ngulp_original; find_fft(&ngulp,time_series[i]); // Zap DC spike time_series[i][0]=0.0; time_series[i][1]=0.0; } spectra = 1; nplot = ngulp; button = PLOT; Gsearched=false; plotno = -1; } if (button==POWER) { for (i=0; i<nfiles; i++){ find_formspec(ngulp,time_series[i]); } ngulp/=2; powerspectra = 1; nplot = ngulp; button = PLOT; plotno = -1; } if (button==SMHRM) { nfiles = 6; for (i=1; i<nfiles; i++){ time_series[i]=(float *) malloc((ngulp+2)*sizeof(float)); if (time_series[i]==NULL){ fprintf(stderr,"Error allocating memory\n"); exit(-1); } } for (i=1;i<nfiles;i++) memcpy(time_series[i],time_series[0], (ngulp+2)*sizeof(float)); d->nplotregion=1; float deltay = 0.9/(float)nfiles; for (i=0; i<nfiles; i++) d->addplotregion(0.2,0.99,0.95-deltay*(float)(i+1), 0.95-deltay*(float)i); cpgeras(); d->draw(); float * workspace = new float[ngulp]; // Set up space for data, now actually sumhrm int one=1; newoldsumhrm_(&time_series[0][1],workspace,&ngulp,&one, // newoldsumhrm_(&time_series[0][0],workspace,&ngulp,&one, time_series[1],time_series[2],time_series[3], time_series[4],time_series[5]); /* newnewsumhrm_(time_series[0],&ngulp,&one, time_series[1],time_series[2],time_series[3], time_series[4],time_series[5]);*/ for (int iff=2;iff<6;iff++){ for (int i=0;i<ngulp;i++){ time_series[iff][i]/=sqrt(pow(2.0,(float)(iff-1))); } } delete [] workspace; button = PLOT; plotno = -1; } if (button==NORMALISE) { for (i=0; i<nfiles; i++){ normalise(ngulp,time_series[i],5.0); } button = PLOT; Gsearched=false; plotno = -1; } if (button==HISTOGRAM) { float pdfs[nfiles][MAXSIGMA]; // create pdfs for each beam for (int i=0;i<nfiles;i++) formpdf(pdfs[i],MAXSIGMA,ngulp,time_series[i]); for (int i=0;i<nfiles;i++){ for (int j=0;j<MAXSIGMA; j++){ fprintf(stderr, "pdfs[%d][%2d]=%8.0f %f \%\n", i, j+1, pdfs[i][j], 100*pdfs[i][j]/ngulp); } } button = PLOT; plotno = -1; } if (button==HALVEPLOT) { nplot/=2; button = PLOT; zoneplot=true; Gsearched=false; plotno = -1; } if (button==GLOBALRESET) { plotno = -1; nstart = 0; scrunch=1; tsamp = tsamp_orig; nplot=ngulp_original; ngulp=ngulp_original; button=PLOT; // Skip to end of skipped data for (i=0; i<nfiles; i++){ fseek(inputfile[i],-(nfileptr-nskipstart)*nbits/8,SEEK_CUR); Giant[i].clear(); } nfileptr=nskipstart; zoneplot=false; Gsearched=false; doperiod=-1; button=NEXT; } if (button==SUBTRACTMEAN && nfiles>1) { plotno = -1; nstart = 0; nplot=ngulp_original; ngulp=ngulp_original; button=PLOT; // Skip to end of skipped data for (int jj=0;jj<ngulp;jj++){ float sum; sum=0.0; for (i=1;i<nfiles;i++){ sum+=time_series[i][jj]; } time_series[0][jj]-=sum/(float(nfiles-1)); } Gsearched=false; } if (button==ZAPCOMMON && nfiles>1) { plotno = -1; nstart = 0; nplot=ngulp_original; ngulp=ngulp_original; button=PLOT; float pdfs[nfiles][MAXSIGMA]; // create pdfs for each beam for (int i=0;i<nfiles;i++) formpdf(pdfs[i],MAXSIGMA,ngulp,time_series[i]); // for each point in each beam, mask if improbable float thresh = 3.0; int nbeammax = 5; zap_improbables(pdfs,time_series,nfiles,ngulp,MAXSIGMA,thresh,nbeammax); // Skip to end of skipped data //for (int jj=0;jj<ngulp;jj++){ // float sum; // sum=0.0; // for (i=1;i<nfiles;i++){ // sum+=time_series[i][jj]; // } // time_series[0][jj]-=sum/(float(nfiles-1)); //} //Gsearched=false; } if (button==NEXT) { ngulp=ngulp_original; nstart=0; nplot=ngulp_original; // Read the data int NActuallyRead; // unsigned char *buffer; char *buffer; buffer = new char[ngulp*nbits/8]; //buffer = new char[ngulp*nbits/8]; for (i=0; i<nfiles; i++) { // NActuallyRead = fread(time_series[i],sizeof(float),ngulp,inputfile[i]); NActuallyRead = fread(buffer,nbits/8,ngulp,inputfile[i]); if (nbits==32){ memcpy(time_series[i],buffer,sizeof(float)*ngulp); } else { for (int j=0;j<NActuallyRead;j++){ if (ssigned) time_series[i][j]=(float)buffer[j]; if (!ssigned) time_series[i][j]=(float)((unsigned char)buffer[j]); } } puti(ngulp); if (NActuallyRead!=ngulp){ fprintf(stderr,"Could not read %d floats from file\n",ngulp); ngulp = NActuallyRead; } if(nfiles==1){ // Add fake pulsar here.... // for (int ii=0;ii<ngulp;ii++) time_series[i][ii]+= 10.0*pow(sin(float(ii*2.0*M_PI/60.0)),250.0); } //normalise(ngulp,time_series[i]); } nfileptr+=ngulp; button = PLOT; plotno= -1; zoneplot=true; } if (button==RESET) { button = plotno = -1; nstart=0; nplot=ngulp; button=PLOT; zoneplot=true; Gsearched=false; if (ans=='p'){ doperiod=-1; } } if (plotno>0){ /* if (ans=='p'){ // hit p on a plot to type in a period d->plotregions[plotno].reset(); //plot the thing; fprintf(stderr,"Please enter a period in seconds: "); cin>>trialperiod; xperiod = x; doperiod=plotno; button=PLOT; }*/ if (ans=='p'){ // hit p on a plot to type in a period d->plotregions[plotno].reset(); //plot the thing; fprintf(stderr,"Please enter a period in seconds: "); cin>>trialperiod; xperiod = (double)x; doperiod=plotno; button=PLOT; } if (ans=='m'){ // subtract 0.0000005 seconds from period d->plotregions[plotno].reset(); trialperiod-=0.0000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='/'){ // add 0.0000005 seconds to period d->plotregions[plotno].reset(); trialperiod+=0.0000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans==','){ // subtract 0.000005 seconds from period d->plotregions[plotno].reset(); trialperiod-=0.000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='.'){ // add 0.000005 seconds to period d->plotregions[plotno].reset(); trialperiod+=0.000005; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='<'){ // subtract 0.001 seconds from period d->plotregions[plotno].reset(); trialperiod-=0.001; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='>'){ // add 0.001 seconds to period d->plotregions[plotno].reset(); trialperiod+=0.001; fprintf(stderr,"Trial period is now %lf\n",trialperiod); doperiod=plotno; button=PLOT; } if (ans=='X'){ // right click two points on a plot to calculate and plot a period d->plotregions[plotno].reset(); cpgsci(3); cpgmove(x,-1000); cpgdraw(x,1000); if (ngates==0){ xgate=x; ngates++; } else { min_means_min(&x,&xgate); printf("Period from %f to %f is %f\n",x,xgate,xgate-x); doperiod=plotno; xperiod = (double)x; trialperiod=(double)(xgate-x); ngates=0; button=PLOT; } } if (ans=='D'){ markers[nmarkers]=(int)(x/NPIXELS)*nplot+nstart+nfileptr-ngulp; nmarkers++; zoneplot=true; } if (ans=='A'){ d->plotregions[plotno].reset(); cpgsci(2); cpgmove(x,-1000); cpgdraw(x,1000); if (ngates==0){ xgate=x; ngates++; } else { min_means_min(&x,&xgate); // printf("x %f xgate %f tstart %f\n",x,xgate,tstart); nstart=(int)((x-tstart)/delta)+nstart; nplot=(int)((xgate-x)/delta); //if (nplot<NPIXELS) nplot=NPIXELS; ngates=0; button=PLOT; zoneplot=true; // printf("nplot %d nstart %d\n",nplot,nstart); } } if (ans=='z'){ if (NPIXELS>nplot) { nstart+=(int)x; }else nstart=(int)(x/(float)NPIXELS*nplot)+nstart; printf("nstart %d\n",nstart); nplot/=4; printf("nplot %d\n",nplot); nstart-=nplot/2; printf("nstart %d\n",nstart); //if (nplot<NPIXELS){nplot=NPIXELS;} button=PLOT; zoneplot=true; } }
int main() { char text[80]; int ci, crval1, crval2, ilat, ilng, j, k, latpole, lonpole, stat[361], status; float xr[512], yr[512]; double lat[181], lng[361], phi[361], theta[361], x[361], y[361]; struct celprm native, celestial; printf( "Testing WCSLIB celestial coordinate transformation routines (tcel1.c)\n" "---------------------------------------------------------------------\n"); /* List status return messages. */ printf("\nList of cel status return values:\n"); for (status = 1; status <= 6; status++) { printf("%4d: %s.\n", status, cel_errmsg[status]); } printf("\n"); /* Initialize. */ celini(&native); /* Reference angles for the native graticule (in fact, the defaults). */ native.ref[0] = 0.0; native.ref[1] = 0.0; /* Set up Bonne's projection with conformal latitude at +35. */ strcpy(native.prj.code, "BON"); native.prj.pv[1] = 35.0; /* Celestial graticule. */ celini(&celestial); celestial.prj = native.prj; /* PGPLOT initialization. */ strcpy(text, "/xwindow"); cpgbeg(0, text, 1, 1); /* Define pen colours. */ cpgscr(0, 0.0f, 0.0f, 0.0f); cpgscr(1, 1.0f, 1.0f, 0.0f); cpgscr(2, 1.0f, 1.0f, 1.0f); cpgscr(3, 0.5f, 0.5f, 0.8f); cpgscr(4, 0.8f, 0.5f, 0.5f); cpgscr(5, 0.8f, 0.8f, 0.8f); cpgscr(6, 0.5f, 0.5f, 0.8f); cpgscr(7, 0.8f, 0.5f, 0.5f); cpgscr(8, 0.3f, 0.5f, 0.3f); /* Define PGPLOT viewport. */ cpgenv(-180.0f, 180.0f, -90.0f, 140.0f, 1, -2); /* Loop over CRVAL2, LONPOLE, and LATPOLE with CRVAL1 incrementing by */ /* 15 degrees each time (it has an uninteresting effect). */ crval1 = -180; for (crval2 = -90; crval2 <= 90; crval2 += 30) { for (lonpole = -180; lonpole <= 180; lonpole += 30) { for (latpole = -1; latpole <= 1; latpole += 2) { /* For the celestial graticule, set the celestial coordinates of * the reference point of the projection (which for Bonne's * projection is at the intersection of the native equator and * prime meridian), the native longitude of the celestial pole, * and extra information needed to determine the celestial * latitude of the native pole. These correspond to FITS keywords * CRVAL1, CRVAL2, LONPOLE, and LATPOLE. */ celestial.ref[0] = (double)crval1; celestial.ref[1] = (double)crval2; celestial.ref[2] = (double)lonpole; celestial.ref[3] = (double)latpole; /* Skip invalid values of LONPOLE. */ if (celset(&celestial)) { continue; } /* Skip redundant values of LATPOLE. */ if (latpole == 1 && fabs(celestial.ref[3]) < 0.1) { continue; } /* Buffer PGPLOT output. */ cpgbbuf(); cpgeras(); /* Write a descriptive title. */ sprintf(text, "Bonne's projection (BON) - 15 degree graticule"); printf("\n%s\n", text); cpgtext(-180.0f, -100.0f, text); sprintf(text, "centred on celestial coordinates (%7.2f,%6.2f)", celestial.ref[0], celestial.ref[1]); printf("%s\n", text); cpgtext (-180.0f, -110.0f, text); sprintf(text, "with north celestial pole at native coordinates " "(%7.2f,%7.2f)", celestial.ref[2], celestial.ref[3]); printf("%s\n", text); cpgtext(-180.0f, -120.0f, text); /* Draw the native graticule faintly in the background. */ cpgsci(8); /* Draw native meridians of longitude. */ for (j = 0, ilat = -90; ilat <= 90; ilat++, j++) { lat[j] = (double)ilat; } for (ilng = -180; ilng <= 180; ilng += 15) { lng[0] = (double)ilng; if (ilng == -180) lng[0] = -179.99; if (ilng == 180) lng[0] = 179.99; /* Dash the longitude of the celestial pole. */ if ((ilng-lonpole)%360 == 0) { cpgsls(2); cpgslw(5); } cels2x(&native, 1, 181, 1, 1, lng, lat, phi, theta, x, y, stat); k = 0; for (j = 0; j < 181; j++) { if (stat[j]) { if (k > 1) cpgline(k, xr, yr); k = 0; continue; } xr[k] = -x[j]; yr[k] = y[j]; k++; } cpgline(k, xr, yr); cpgsls(1); cpgslw(1); } /* Draw native parallels of latitude. */ lng[0] = -179.99; lng[360] = 179.99; for (j = 1, ilng = -179; ilng < 180; ilng++, j++) { lng[j] = (double)ilng; } for (ilat = -90; ilat <= 90; ilat += 15) { lat[0] = (double)ilat; cels2x(&native, 361, 1, 1, 1, lng, lat, phi, theta, x, y, stat); k = 0; for (j = 0; j < 361; j++) { if (stat[j]) { if (k > 1) cpgline(k, xr, yr); k = 0; continue; } xr[k] = -x[j]; yr[k] = y[j]; k++; } cpgline(k, xr, yr); } /* Draw a colour-coded celestial coordinate graticule. */ ci = 1; /* Draw celestial meridians of longitude. */ for (j = 0, ilat = -90; ilat <= 90; ilat++, j++) { lat[j] = (double)ilat; } for (ilng = -180; ilng <= 180; ilng += 15) { lng[0] = (double)ilng; if (++ci > 7) ci = 2; cpgsci(ilng?ci:1); /* Dash the reference longitude. */ if ((ilng-crval1)%360 == 0) { cpgsls(2); cpgslw(5); } cels2x(&celestial, 1, 181, 1, 1, lng, lat, phi, theta, x, y, stat); k = 0; for (j = 0; j < 181; j++) { if (stat[j]) { if (k > 1) cpgline(k, xr, yr); k = 0; continue; } /* Test for discontinuities. */ if (j > 0) { if (fabs(x[j]-x[j-1]) > 4.0 || fabs(y[j]-y[j-1]) > 4.0) { if (k > 1) cpgline(k, xr, yr); k = 0; } } xr[k] = -x[j]; yr[k] = y[j]; k++; } cpgline(k, xr, yr); cpgsls(1); cpgslw(1); } /* Draw celestial parallels of latitude. */ for (j = 0, ilng = -180; ilng <= 180; ilng++, j++) { lng[j] = (double)ilng; } ci = 1; for (ilat = -90; ilat <= 90; ilat += 15) { lat[0] = (double)ilat; if (++ci > 7) ci = 2; cpgsci(ilat?ci:1); /* Dash the reference latitude. */ if (ilat == crval2) { cpgsls(2); cpgslw(5); } cels2x(&celestial, 361, 1, 1, 1, lng, lat, phi, theta, x, y, stat); k = 0; for (j = 0; j < 361; j++) { if (stat[j]) { if (k > 1) cpgline(k, xr, yr); k = 0; continue; } /* Test for discontinuities. */ if (j > 0) { if (fabs(x[j]-x[j-1]) > 4.0 || fabs(y[j]-y[j-1]) > 4.0) { if (k > 1) cpgline(k, xr, yr); k = 0; } } xr[k] = -x[j]; yr[k] = y[j]; k++; } cpgline(k, xr, yr); cpgsls(1); cpgslw(1); } /* Flush PGPLOT buffer. */ cpgebuf(); printf(" Type <RETURN> for next page: "); getc(stdin); /* Cycle through celestial longitudes. */ if ((crval1 += 15) > 180) crval1 = -180; /* Skip boring celestial latitudes. */ if (crval2 == 0) break; } if (crval2 == 0) break; } } cpgask(0); cpgend(); return 0; }
int main() { /* Set up a 2 x 2 lookup table. */ const int M = 2; const int K[] = {K1, K2}; const int map[] = {0, 1}; const double crval[] = {0.0, 0.0}; char text[80]; int i, j, k, l, l1, l2, l3, lstep, m, stat[NP*NP], status; float array[NP][NP], clev[31], v0, v1, w; const float scl = 2.0f/(NP-1); float ltm[6]; double x[NP][NP][2], world[NP][NP][2]; struct tabprm tab; printf("Testing WCSLIB coordinate lookup table routines (ttab2.c)\n" "---------------------------------------------------------\n"); /* List status return messages. */ printf("\nList of tab status return values:\n"); for (status = 1; status <= 5; status++) { printf("%4d: %s.\n", status, tab_errmsg[status]); } printf("\n"); /* PGPLOT initialization. */ strcpy(text, "/xwindow"); cpgbeg(0, text, 1, 1); cpgvstd(); cpgsch(0.7f); /* The viewport is slightly oversized. */ cpgwnad(-0.65f, 1.65f, -0.65f, 1.65f); for (l = 0; l <= 30; l++) { clev[l] = 0.2f*(l-10); } ltm[0] = -scl*(1.0f + (NP-1)/4.0f); ltm[1] = scl; ltm[2] = 0.0f; ltm[3] = -scl*(1.0f + (NP-1)/4.0f); ltm[4] = 0.0f; ltm[5] = scl; /* Set up the lookup table. */ tab.flag = -1; if ((status = tabini(1, M, K, &tab))) { printf("tabini ERROR %d: %s.\n", status, tab_errmsg[status]); return 1; } tab.M = M; for (m = 0; m < tab.M; m++) { tab.K[m] = K[m]; tab.map[m] = map[m]; tab.crval[m] = crval[m]; for (k = 0; k < tab.K[m]; k++) { tab.index[m][k] = (double)k; } } /* Subdivide the interpolation element. */ for (i = 0; i < NP; i++) { for (j = 0; j < NP; j++) { x[i][j][0] = j*(K1-1.0)*scl - 0.5 - crval[0]; x[i][j][1] = i*(K2-1.0)*scl - 0.5 - crval[1]; } } /* The first coordinate element is static. */ tab.coord[0] = 0.0; tab.coord[2] = 0.0; tab.coord[4] = 0.0; tab.coord[6] = 0.0; /* (k1,k2) = (0,0). */ tab.coord[1] = 0.0; /* The second coordinate element varies in three of the corners. */ for (l3 = 0; l3 <= 100; l3 += 20) { /* (k1,k2) = (1,1). */ tab.coord[7] = 0.01 * l3; for (l2 = 0; l2 <= 100; l2 += 20) { /* (k1,k2) = (0,1). */ tab.coord[5] = 0.01 * l2; cpgpage(); for (l1 = 0; l1 <= 100; l1 += 2) { /* (k1,k2) = (1,0). */ tab.coord[3] = 0.01 * l1; /* Compute coordinates within the interpolation element. */ tab.flag = 0; if ((status = tabx2s(&tab, NP*NP, 2, (double *)x, (double *)world, stat))) { printf("tabx2s ERROR %d: %s.\n", status, tab_errmsg[status]); } /* Start a new plot. */ cpgbbuf(); cpgeras(); cpgsci(1); cpgslw(3); cpgbox("BCNST", 0.0f, 0, "BCNSTV", 0.0f, 0); cpgmtxt("T", 0.7f, 0.5f, 0.5f, "-TAB coordinates: " "linear interpolation / extrapolation in 2-D"); /* Draw the boundary of the interpolation element in red. */ cpgsci(2); cpgmove(-0.5f, 0.0f); cpgdraw( 1.5f, 0.0f); cpgmove( 1.0f, -0.5f); cpgdraw( 1.0f, 1.5f); cpgmove( 1.5f, 1.0f); cpgdraw(-0.5f, 1.0f); cpgmove( 0.0f, 1.5f); cpgdraw( 0.0f, -0.5f); /* Label the value of the coordinate element in each corner. */ sprintf(text, "%.1f", tab.coord[1]); cpgtext(-0.09f, -0.05f, text); sprintf(text, "%.2f", tab.coord[3]); cpgtext( 1.02f, -0.05f, text); sprintf(text, "%.1f", tab.coord[5]); cpgtext(-0.13f, 1.02f, text); sprintf(text, "%.1f", tab.coord[7]); cpgtext( 1.02f, 1.02f, text); cpgsci(1); /* Contour labelling: bottom. */ v0 = world[0][0][1]; v1 = world[0][NP-1][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%4.1f", l*0.01f); cpgptxt(w+0.04f, -0.56f, 0.0f, 1.0f, text); } } /* Contour labelling: left. */ v0 = world[0][0][1]; v1 = world[NP-1][0][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%4.1f", l*0.01f); cpgptxt(-0.52f, w-0.02f, 0.0f, 1.0f, text); } } /* Contour labelling: right. */ v0 = world[0][NP-1][1]; v1 = world[NP-1][NP-1][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%.1f", l*0.01f); cpgptxt(1.52f, w-0.02f, 0.0f, 0.0f, text); } } /* Contour labelling: top. */ v0 = world[NP-1][0][1]; v1 = world[NP-1][NP-1][1]; if (v0 != v1) { lstep = (abs((int)((v1-v0)/0.2f)) < 10) ? 20 : 40; for (l = -200; l <= 300; l += lstep) { w = -0.5f + 2.0f * (l*0.01f - v0) / (v1 - v0); if (w < -0.5 || w > 1.5) continue; sprintf(text, "%4.1f", l*0.01f); cpgptxt(w+0.04f, 1.52f, 0.0f, 1.0f, text); } } /* Draw contours for the second coordinate element. */ for (i = 0; i < NP; i++) { for (j = 0; j < NP; j++) { array[i][j] = world[i][j][1]; } } cpgsci(4); cpgslw(2); cpgcont(array[0], NP, NP, 1, NP, 1, NP, clev, 10, ltm); cpgsci(7); cpgcont(array[0], NP, NP, 1, NP, 1, NP, clev+10, 1, ltm); cpgsci(5); cpgcont(array[0], NP, NP, 1, NP, 1, NP, clev+11, 20, ltm); cpgebuf(); } } } cpgend(); tabfree(&tab); return 0; }
void plot(GRAPHCONTROL *gr, SET *p) { char t[1024]; cpgsch(FS); cpgsci(1); cpgsvp(0.07, 0.93, 0.35, 0.9); cpgeras(); cpgswin(gr->xmin, gr->xmax, gr->ymin, gr->ymax); cpgbox("BCNST", 0.0, 0, "BCNST", 0.0, 0); cpgbbuf(); cpgsch(0.8); float yp = 3.4; sprintf(t,"[n] Ano: %d/%d", p->y1, p->y2); cpgmtxt("T", yp, 0.0, 0.0, t); sprintf(t,"[m] Magnitude: %.2f/%.2f", p->m1, p->m2); cpgmtxt("T", yp, 0.25, 0.0, t); sprintf(t,"[s/0] Selecionar Regiao"); (p->region) ? cpgsci(ON) : cpgsci(OFF); cpgmtxt("T", yp, 0.6, 0.0, t); cpgsci(1); cpgmtxt("T", yp, 0.85, 0.0, "[=] Salvar Print-out"); yp -= 1.2; sprintf(t,"N: %ld",p->n); cpgmtxt("T", yp, 0.0, 0.0, t); sprintf(t,"[p] Profundidade(p): %.1f/%.1f",p->d1, p->d2); cpgmtxt("T", yp, 0.25, 0.0, t); sprintf(t,"Longitude: %.2f/%.2f",p->lon1, p->lon2); cpgmtxt("T", yp, 0.6, 0.0, t); cpgmtxt("T", yp, 0.85, 0.0, "[J] Definir intervalo"); yp -= 1.2; sprintf(t,"Latitude: %.2f/%.2f", p->lat1, p->lat2); cpgmtxt("T", yp, 0.6, 0.0, t); sprintf(t,"[w] Zoom para todo o mapa"); cpgmtxt("T", yp, 0.25, 0.0, t); cpgmtxt("T", yp, 0.85, 0.0, " de ajuste"); sprintf(t,"[c] Cor: %s", (gr->colormode == COLORDEPTH) ? "Profundidade" : (gr->colormode == COLORMAG) ? "Magnitude" : "Neutra"); cpgmtxt("R", 1.0, 1.0, 1.0, t); (gr->hascontinents) ? cpgsci(ON) : cpgsci(OFF); sprintf(t,"[1] Continentes"); cpgmtxt("R", 1.0, 0.25, 0.0, t); cpgsci(1); (gr->hasplates) ? cpgsci(ON) : cpgsci(OFF); sprintf(t,"[2] Placas"); cpgmtxt("R", 1.0, 0.0, 0.0, t); cpgsci(1); // Legenda cores cpgsci(1); cpgsch(FS); /* Graphs */ int i; if (gr->haspoints && p->n > 0) { int symbol = 17; (p->n > 50) ? cpgsch(0.4) : cpgsch(FS); if (gr->colormode == COLORDEPTH) for(i = 0; i< p->n; i++) { cpgsci(depthcolor(p->d[i])); cpgpt1(p->x[i], p->y[i], symbol); } else if (gr->colormode == COLORMAG) for(i = 0; i< p->n; i++) { cpgsci(magcolor(p->m[i])); cpgpt1(p->x[i], p->y[i], symbol); } else cpgpt(p->n, p->x, p->y, symbol); cpgsci(1); cpgsch(FS); } if (gr->hascontinents >= 1) { cpgsci(1); cpgslw(2); for(i=0; i < ncontinentes; i++) { if (continentes[i][0] == -999 && continentes[i][1] == 999 ) { i++; cpgmove(continentes[i][0], continentes[i][1]); continue; } cpgdraw(continentes[i][0], continentes[i][1]); } if (gr->hascontinents >=2) { cpgslw(1); cpgsci(15); for(i=0; i < nborders; i++) { if (borders[i][0] == -999 && borders[i][1] == 999 ) { i++; cpgmove(borders[i][0], borders[i][1]); continue; } cpgdraw(borders[i][0], borders[i][1]); } } } if (gr->hasplates == 1) { cpgsci(3); cpgslw(3); for(i=0; i < nplates; i++) { if (plates[i][0] == -999 && plates[i][1] == 999 ) { i++; cpgmove(plates[i][0], plates[i][1]); continue; } if (fabs(plates[i][0] - plates[i-1][0]) > 180) { cpgmove(plates[i][0], plates[i][1]); } cpgdraw(plates[i][0], plates[i][1]); } } if (gr->colormode == COLORMAG) scalemag(); else if (gr->colormode == COLORDEPTH) scaledep(); cpgsci(1); cpgslw(1); cpgebuf(); cpgsvp(0.07, 0.93, 0.35, 0.9); cpgswin(gr->xmin, gr->xmax, gr->ymin, gr->ymax); return; }
int closure ( const char ctypeS[9], double restfrq, double restwav, int naxisj, double crpixj, double cdeltX, double crvalX) { char ptype, sname[32], title[80], units[8], xtype, ylab[80]; int nFail = 0, restreq, stat1[NSPEC], stat2[NSPEC], status; register int j; float tmp, x[NSPEC], xmin, xmax, y[NSPEC], ymax, ymin; double cdeltS, clos[NSPEC], crvalS, dSdX, resid, residmax, spec1[NSPEC], spec2[NSPEC]; struct spcprm spc; /* Get keyvalues for the required spectral axis type. */ if ((status = spcxps(ctypeS, crvalX, restfrq, restwav, &ptype, &xtype, &restreq, &crvalS, &dSdX))) { printf("ERROR %d from spcxps() for %s.\n", status, ctypeS); return 1; } cdeltS = cdeltX * dSdX; spcini(&spc); if (ctypeS[5] == 'G') { /* KPNO MARS spectrograph grism parameters. */ spc.pv[0] = mars[0]; spc.pv[1] = mars[1]; spc.pv[2] = mars[2]; spc.pv[3] = mars[3]; spc.pv[4] = mars[4]; spc.pv[5] = mars[5]; spc.pv[6] = mars[6]; } /* Construct the axis. */ for (j = 0; j < naxisj; j++) { spec1[j] = (j+1 - crpixj)*cdeltS; } printf("%4s (CRVALk+w) range: %13.6e to %13.6e, step: %13.6e\n", ctypeS, crvalS+spec1[0], crvalS+spec1[naxisj-1], cdeltS); /* Initialize. */ spc.flag = 0; spc.crval = crvalS; spc.restfrq = restfrq; spc.restwav = restwav; strncpy(spc.type, ctypeS, 4); spc.type[4] = '\0'; strcpy(spc.code, ctypeS+5); /* Convert the first to the second. */ if ((status = spcx2s(&spc, naxisj, 1, 1, spec1, spec2, stat1))) { printf("spcx2s ERROR %d: %s.\n", status, spc_errmsg[status]); } /* Convert the second back to the first. */ if ((status = spcs2x(&spc, naxisj, 1, 1, spec2, clos, stat2))) { printf("spcs2x ERROR %d: %s.\n", status, spc_errmsg[status]); } residmax = 0.0; /* Test closure. */ for (j = 0; j < naxisj; j++) { if (stat1[j]) { printf("%s: w =%20.12e -> %s = ???, stat = %d\n", ctypeS, spec1[j], spc.type, stat1[j]); continue; } if (stat2[j]) { printf("%s: w =%20.12e -> %s =%20.12e -> w = ???, stat = %d\n", ctypeS, spec1[j], spc.type, spec2[j], stat2[j]); continue; } resid = fabs((clos[j] - spec1[j])/cdeltS); if (resid > residmax) residmax = resid; if (resid > tol) { nFail++; printf("%s: w =%20.12e -> %s =%20.12e ->\n w =%20.12e, " "resid =%20.12e\n", ctypeS, spec1[j], spc.type, spec2[j], clos[j], resid); } } printf("%s: Maximum closure residual = %.1e pixel.\n", ctypeS, residmax); /* Draw graph. */ cpgbbuf(); cpgeras(); xmin = (float)(crvalS + spec1[0]); xmax = (float)(crvalS + spec1[naxisj-1]); ymin = (float)(spec2[0]) - xmin; ymax = ymin; for (j = 0; j < naxisj; j++) { x[j] = (float)(j+1); y[j] = (float)(spec2[j] - (crvalS + spec1[j])); if (y[j] > ymax) ymax = y[j]; if (y[j] < ymin) ymin = y[j]; } j = (int)crpixj + 1; if (y[j] < 0.0) { tmp = ymin; ymin = ymax; ymax = tmp; } cpgask(0); cpgenv(1.0f, (float)naxisj, ymin, ymax, 0, -1); cpgsci(1); cpgbox("ABNTS", 0.0f, 0, "BNTS", 0.0f, 0); spctyp(ctypeS, 0x0, 0x0, sname, units, 0x0, 0x0, 0x0); sprintf(ylab, "%s - correction [%s]", sname, units); sprintf(title, "%s: CRVALk + w [%s]", ctypeS, units); cpglab("Pixel coordinate", ylab, title); cpgaxis("N", 0.0f, ymax, (float)naxisj, ymax, xmin, xmax, 0.0f, 0, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f); cpgaxis("N", (float)naxisj, ymin, (float)naxisj, ymax, (float)(ymin/cdeltS), (float)(ymax/cdeltS), 0.0f, 0, 0.5f, 0.0f, 0.5f, 0.1f, 0.0f); cpgmtxt("R", 2.2f, 0.5f, 0.5f, "Pixel offset"); cpgline(naxisj, x, y); cpgsci(7); cpgpt1((float)crpixj, 0.0f, 24); cpgebuf(); printf("Type <RETURN> for next page: "); (void)getchar(); printf("\n"); return nFail; }
int plot_map() { int nx=720; int ny=180; int deli, delj; float value; int counter; float mapplot[720][180]; int i=0, j=0, k=0; float tr[6]= {0.0, 0.5, 0.0, 0.0, 0.0, 0.5}; float fmin=1, fmax=0; // float RL[9]={-0.5, 0.004, 0.006, 0.008, 0.02, 0.04, 0.06, 0.08, 0.1}; float RL[9]={-0.5, 0.0, 0.04, 0.08, 0.2, 0.4, 0.6, 0.8, 1.0}; float RR[9]={0.0, 0.0, 0.0, 0.0, 0.6, 1.0, 1.0, 1.0, 1.0}; float RG[9]={0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.6, 1.1, 1.0}; float RB[9]={0.0, 0.3, 0.8, 1.0, 0.3, 0.0, 0.0, 0.0, 1.0}; float bright=0.5; //0.53 float contra=1.0; //1.0 //map larger array into smaller array for (j=1; j<ny; j++) { for (i=1; i<nx; i++) { value=0; counter=0; for (deli=0; deli<=5; deli++) { for (delj=0; delj<=5; delj++) { value=value+mapx[sat_choice][(5*i)+deli][(5*j)+delj]; if (mapx[sat_choice][(5*i)+deli][(5*j)+delj]>0) { counter++; // printf("%i %f\n", counter, value/counter); } } } if (counter==0) mapplot[i][j]=value; else mapplot[i][j]=value/counter; } } for (j=1; j<ny; j++) { for (i=1; i<nx; i++) { k=(j-1)*nx + (i-1); f[k]=mapplot[i][j]; if (f[k] <fmin) fmin = f[k]; if (f[k] >fmax) fmax = f[k]; } } printf("min=%f max=%f\n", fmin, fmax); fmax=0.1; cpgslct(pg_id); cpgeras(); cpgenv(0.0, 360, 0.0, 90, 1.0, -2); cpglab("Azimuth", "Elevation", "Antenna Power Pattern [Data: May 7-22, 2006]"); cpgctab(RL, RR, RG, RB, 9, contra, bright); cpgimag(f, (float)nx, (float)ny, 1.0, (float)nx, 1.0, (float)ny, fmin, fmax, tr); cpgbox("BCNST1",0.0,0,"BCNST1",0.0,0); return 0; }
static void _pgeras (void) { cpgeras(); }
void getTOA_alg1(ptime_observation *obs,pheader *header,tmplStruct *tmpl,toaStruct *toa,FILE *fout_log) { int i,j,k; int nbin = header->nbin; int nchan = header->nchan; int npol = header->npol; double chisq; double diffVals[nbin]; double phiRot = 0; double step1 = 1.0/nbin/2.0; double step = step1; double baseline = 0; double scale = 1; double tmplEval; int chan = 0; int pol = 0; double phi,bestPhi; double phi0,phi1,bestChisq; int it; double chisqVals[nbin*2]; double phiVals[nbin*2]; int nChisqVals,ibest; int iterateAgain; int maxIterations = 10; int plotOutput=0; double error; double *fitX; double *fitY; double *fitE; int *fitI,*fitJ,*fitK; int nfit = npol+1; // Up to 4 baselines per profile + 1 scaling factor double outputParams_v[nfit]; double outputParams_e[nfit]; double results_v[npol*nchan+nchan]; double results_e[npol*nchan+nchan]; int weight = 1; double bestParameters[npol*nchan+nchan]; double chisqTot; // Covariance matrix double **cvm; cvm = (double **)malloc(sizeof(double*)*nfit); for (i=0;i<nfit;i++) cvm[i] = (double *)malloc(sizeof(double)*nfit); if (!(fitX = (double *)malloc(sizeof(double)*npol*nchan*nbin))){ printf("Unable to allocate enough memory for fitX\n"); exit(1); } if (!(fitY = (double *)malloc(sizeof(double)*npol*nchan*nbin))){ printf("Unable to allocate enough memory for fitY\n"); exit(1); } if (!(fitE = (double *)malloc(sizeof(double)*npol*nchan*nbin))){ printf("Unable to allocate enough memory for fitE\n"); exit(1); } if (!(fitI = (int *)malloc(sizeof(int)*npol*nchan*nbin))){ printf("Unable to allocate enough memory for fitI\n"); exit(1); } if (!(fitJ = (int *)malloc(sizeof(int)*npol*nchan*nbin))){ printf("Unable to allocate enough memory for fitJ\n"); exit(1); } if (!(fitK = (int *)malloc(sizeof(int)*npol*nchan*nbin))){ printf("Unable to allocate enough memory for fitK\n"); exit(1); } printf("Baseline sdev = %g, mean = %g\n",obs->chan[0].pol[0].sdev,obs->chan[0].pol[0].baselineVal); printf("On the next line\n"); printf("npol = %d \n",npol); printf("Doing fit\n"); it = 0; do { printf("Iteration %d\n",it+1); if (it == 0) { phi0 = -0.5; phi1 = 0.5; } else { phi0 = bestPhi - step; phi1 = bestPhi + step; step/=(double)10.0; } nChisqVals = 0; for (phiRot = phi0;phiRot < phi1;phiRot += step) { printf("Complete: %.1f percent\n",(phiRot-phi0)/(phi1-phi0)*100); // phiRot = 0.0; // Least squares fit for baseline and amplitude at given phiRot // printf("Doing fit\n"); chisqTot=0; for (j=0;j<nchan;j++){ for (i=0;i<npol;i++){ for (k=0;k<nbin;k++){ // printf("Setting %d %d %d %d %d\n",i,j,k,npol*nchan*nbin,i*(nchan*nbin)+j*nbin+k); fitI[i*nbin+k] = i; fitJ[i*nbin+k] = j; fitK[i*nbin+k] = k; fitX[i*nbin+k] = (double)k/(double)nbin; // printf("This far\n"); // printf("Searching for %g\n",obs->chan[j].pol[i].val[k]); fitY[i*nbin+k] = obs->chan[j].pol[i].val[k]; fitE[i*nbin+k] = obs->chan[j].pol[i].sdev; } } TKleastSquares_svd(fitX,fitY,fitE,fitI,fitJ,fitK,npol*nbin,outputParams_v,outputParams_e,nfit,cvm, &chisq, fitFunc, tmpl, weight,phiRot); chisqTot += chisq; for (i=0;i<npol+1;i++) { results_v[(npol+1)*j + i] = outputParams_v[i]; results_e[(npol+1)*j + i] = outputParams_e[i]; } // for (i=0;i<npol*nbin;i++) // { // printf("FitVals = %g %g %g\n",fitX[i],fitY[i],fitE[i]); // } // for (i=0;i<nbin;i++) // printf("Best %g %g\n",fitX[i],outputParams_v[4]*evaluateTemplateChannel(tmpl,fitX[i],j,0,phiRot)+outputParams_v[0]); // for (i=0;i<nbin;i++) // printf("Best %g %g\n",fitX[i],outputParams_v[4]*evaluateTemplateChannel(tmpl,fitX[i],j,1,phiRot)+outputParams_v[1]); // for (i=0;i<nbin;i++) // printf("Best %g %g\n",fitX[i],outputParams_v[4]*evaluateTemplateChannel(tmpl,fitX[i],j,2,phiRot)+outputParams_v[2]); // for (i=0;i<nbin;i++) // printf("Best %g %g\n",fitX[i],outputParams_v[4]*evaluateTemplateChannel(tmpl,fitX[i],j,3,phiRot)+outputParams_v[3]); // for (i=0;i<nfit;i++){ // printf("%d %g %g\n",i,outputParams_v[i],outputParams_e[i]); } // printf("Done fit\n"); // baseline = outputParams_v[0]; // scale = outputParams_v[1]; // for (i=0;i<nfit;i++){ // printf("%d %g %g\n",i,outputParams_v[i],outputParams_e[i]); // } // exit(1); chisqVals[nChisqVals] = chisqTot; phiVals[nChisqVals] = phiRot; if (nChisqVals==0){ bestPhi = phiRot; bestChisq = chisqTot; for (i=0;i<nchan*npol+nchan;i++){ bestParameters[i] = outputParams_v[i]; } ibest = nChisqVals; } else { if (bestChisq > chisqTot){ bestChisq = chisqTot; bestPhi = phiRot; ibest = nChisqVals; for (i=0;i<nchan*npol+nchan;i++){ bestParameters[i] = outputParams_v[i]; } } } // printf("Chisq = %g\n",chisq); // exit(1); nChisqVals++; } printf("nvals =%d\n",nChisqVals); // Should check if we need to iterate again - do check based on how chisq is changing - i.e, must get a good measure of the chisq increasing by 1 iterateAgain = 1; for (i=ibest+1;i<nChisqVals;i++) { if (chisqVals[i] < bestChisq + 1){ iterateAgain = 0; break; } } it++; } while (iterateAgain == 1 && it < maxIterations); printf("ibest = %d, nChisqVals = %d, bestPhi = %g\n",ibest,nChisqVals,bestPhi); // exit(1); { int foundStart = 0; double start,end; // Should think how to improve this method for (i=0;i<nChisqVals;i++) { fprintf(fout_log,"chisqVals %g %g\n",phiVals[i],chisqVals[i]); if (foundStart==0 && chisqVals[i] <= bestChisq + 1) { foundStart = 1; start = phiVals[i]; } else if (foundStart == 1 && chisqVals[i] >= bestChisq + 1) { end = phiVals[i]; break; } } error = (end-start)/2.0; } printf("Number of iterations = %d\n",it); printf("bestPhi = %g\n",bestPhi); printf("bestChisq = %g\n",bestChisq); for (i=0;i<nchan*npol+nchan;i++){ printf("Best parameter: %d %g\n",i,bestParameters[i]); } fprintf(fout_log,"Number of iterations = %d\n",it); fprintf(fout_log,"bestPhi = %g\n",bestPhi); fprintf(fout_log,"bestChisq = %g\n",bestChisq); for (i=0;i<nchan*npol+nchan;i++){ fprintf(fout_log,"Best parameter: %d %g\n",i,bestParameters[i]); } if (plotOutput == 1){ float fx[nbin*2],fy[nbin*2],ft[nbin*2],dy[nbin*2]; float miny,maxy; float miny2,maxy2; for (i=0;i<nbin;i++) { fx[i] = (float)i/(float)nbin; fx[i+nbin] = fx[i]+1; // tmplEval = bestScale*evaluateTemplateChannel(tmpl,fx[i],chan,pol,bestPhi)+bestBaseline; fy[i] = obs->chan[chan].pol[pol].val[i]; fy[i+nbin] = fy[i]; ft[i] = tmplEval; ft[i+nbin] = ft[i]; dy[i] = fy[i]-ft[i]; dy[i+nbin] = dy[i]; } findMinMax(nbin,fy,&miny,&maxy); findMinMax(nbin,dy,&miny2,&maxy2); cpgbeg(0,"/xs",1,1); cpgsvp(0.1,0.9,0.5,0.9); cpgeras(); cpgswin(0,2,miny,maxy); cpgbox("BCTS",0.0,0,"BCTSN",0.0,0); cpgbin(nbin*2,fx,fy,1); cpgsci(2); cpgline(nbin*2,fx,ft); cpgsci(1); cpgsvp(0.1,0.9,0.15,0.5); cpgswin(0,2,miny2,maxy2); cpgbox("BCTSN",0.0,0,"BCTSN",0.0,0); cpglab("Phase","prof-tmpl",""); cpgbin(nbin*2,fx,dy,1); cpgend(); } toa->dphi = bestPhi; toa->dphiErr = error; for (i=0;i<nfit;i++) free(cvm[i]); free(cvm); free(fitX); free(fitY); free(fitE); free(fitI); free(fitJ); free(fitK); return; }