#include #include #include #include #include #include #define LIM 128 struct spectrogram { int nsub,nchan; double *mjd; double freq,samp_rate; float *length; float *z; char nfd0[32]; }; double nfd2mjd(char *date); double date2mjd(int year,int month,double day); void dec2sex(double x,char *s,int f,int len); struct spectrogram read_spectrogram(char *prefix,int isub,int nsub,double f0,double df0,int nbin) { int i,j,k,l,flag=0,status,msub; char filename[128],header[256],nfd[32]; FILE *file; struct spectrogram s; float *z; int nch,j0,j1; double freq,samp_rate; float length; int nchan; // Open first file to get number of channels sprintf(filename,"%s_%06d.bin",prefix,isub); // Open file file=fopen(filename,"r"); if (file==NULL) { printf("%s does not exist\n",filename); return s; } // Read header status=fread(header,sizeof(char),256,file); status=sscanf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\n",s.nfd0,&s.freq,&s.samp_rate,&length,&nch); // Close file fclose(file); // Compute plotting channel if (f0>0.0 && df0>0.0) { s.nchan=(int) (df0/s.samp_rate*(float) nch); j0=(int) ((f0-0.5*df0-s.freq+0.5*s.samp_rate)*(float) nch/s.samp_rate); j1=(int) ((f0+0.5*df0-s.freq+0.5*s.samp_rate)*(float) nch/s.samp_rate); if (j0<0 || j1>nch) fprintf(stderr,"Requested frequency range out of limits\n"); } else { s.nchan=nch; j0=0; j1=s.nchan; } // Number of subints s.nsub=nsub/nbin; // Allocate s.z=(float *) malloc(sizeof(float)*s.nchan*s.nsub); z=(float *) malloc(sizeof(float)*nch); s.mjd=(double *) malloc(sizeof(double)*s.nsub); s.length=(float *) malloc(sizeof(float)*s.nsub); // Initialize for (j=0;j0.0 && df0>0.0) { s.freq=f0; s.samp_rate=df0; } // Free free(z); return s; } void time_axis(double *mjd,int n,float xmin,float xmax,float ymin,float ymax) { int i,imin,imax; double mjdt,mjdmin,mjdmax; float dt,t,tmin,tmax; int lsec,ssec,sec; char stime[16]; // Find extrema for (i=0;imjdmax) mjdmax=mjd[i]; } } dt=(float) 86400*(mjdmax-mjdmin); // Choose tickmarks if (dt>43000) { lsec=10800; ssec=3600; } else if (dt>21600) { lsec=10800; ssec=3600; } else if (dt>7200) { lsec=1800; ssec=300; } else if (dt>3600) { lsec=600; ssec=120; } else if (dt>900) { lsec=300; ssec=60; } else { lsec=60; ssec=10; } // Extrema tmin=86400.0*(mjdmin-floor(mjdmin)); tmax=tmin+dt; tmin=lsec*floor(tmin/lsec); tmax=lsec*ceil(tmax/lsec); // Large tickmarks for (t=tmin;t<=tmax;t+=lsec) { mjdt=floor(mjdmin)+t/86400.0; if (mjdt>=mjdmin && mjdt=mjd[i] && mjdt=mjdmin && mjdt=mjd[i] && mjdt=(int) xmax) break; if (j0<0) j0=0; if (j1>=s.nchan) j1=s.nchan-1; zzmax=0.0; jmax=0; for (j=j0;jzzmax) { zzmax=s.z[i+s.nsub*j]; jmax=j; } } printf("%d\n",jmax); cpgpt1((float) i,(float) jmax,17); } i0=(int) x; jmax=(int) y; for (i=i0;;i--) { j0=(int) floor(jmax-10); j1=(int) ceil(jmax+10); if (i<(int) xmin) break; if (i>=(int) xmax) break; if (j0<0) j0=0; if (j1>=s.nchan) j1=s.nchan-1; zzmax=0.0; jmax=0; for (j=j0;jzzmax) { zzmax=s.z[i+s.nsub*j]; jmax=j; } } printf("%d\n",jmax); cpgpt1((float) i,(float) jmax,17); } continue; } // Mark single point if (c=='D') { file=fopen("mark.dat","a"); i=(int) floor(x); j=(int) floor(y); f=s.freq-0.5*s.samp_rate+(double) j*s.samp_rate/(double) s.nchan; if (s.mjd[i]>1.0) { fprintf(file,"%lf %lf %f 4171\n",s.mjd[i],f,s.z[i+s.nsub*j]); printf("%lf %lf %f 4171\n",s.mjd[i],f,s.z[i+s.nsub*j]); } fclose(file); } // Mark if (c=='m') { i0=(int) floor(xmin); i1=(int) ceil(xmax); j0=(int) floor(ymin); j1=(int) ceil(ymax); if (i0<0) i0=0; if (i1>=s.nsub) i1=s.nsub-1; if (j0<0) j0=0; if (j1>=s.nchan) j1=s.nchan-1; file=fopen("out.dat","w"); // Loop over image for (i=i0;izzmax) { zzmax=s.z[i+s.nsub*j]; jmax=j; } } f=s.freq-0.5*s.samp_rate+(double) jmax*s.samp_rate/(double) s.nchan; if (s.mjd[i]>1.0) fprintf(file,"%lf %lf %f 4171\n",s.mjd[i],f,zzmax); cpgpt1((float) i,(float) jmax,17); } fclose(file); } // Center if (c=='c') { xmin=x-width; xmax=x+width; ymin=y-width; ymax=y+width; redraw=1; continue; } // Width if (isdigit(c)) { width=1000.0/(c-'0'); xmin=x-width; xmax=x+width; ymin=y-width; ymax=y+width; redraw=1; continue; } // Zoom if (c=='+' || c=='=') { width/=1.5; xmin=x-width; xmax=x+width; ymin=y-width; ymax=y+width; redraw=1; continue; } // Unzoom if (c=='x' || c=='-') { width*=1.5; xmin=x-width; xmax=x+width; ymin=y-width; ymax=y+width; redraw=1; continue; } // Reset if (c=='r') { xmin=0.0; xmax=(float) s.nsub; ymin=0.0; ymax=(float) s.nchan; redraw=1; continue; } // Zoom if (c=='z') { click=1; mode=2; } // Pan if (c=='\t') { // Set area x=width*(ix+0.5); y=width*(iy+0.5); xmin=x-width; xmax=x+width; ymin=y-width; ymax=y+width; // Increment iy++; if (width*ix>(float) s.nsub) { ix=0; iy=0; } if (width*iy>(float) s.nchan) { ix++; iy=0; } redraw=1; continue; } // Execute zoom, or box delete if (c=='A') { if (click==0) { click=1; } else if (click==1 && mode==2) { xmin=(x0x) ? x0 : x; ymin=(y0y) ? y0 : y; click=0; mode=0; redraw=1; } else { click=0; mode=0; redraw=1; } } // Save x0=x; y0=y; } cpgend(); // Free free(s.z); free(s.mjd); return 0; } // nfd2mjd double nfd2mjd(char *date) { int year,month,day,hour,min; double mjd,dday; float sec; sscanf(date,"%04d-%02d-%02dT%02d:%02d:%f",&year,&month,&day,&hour,&min,&sec); dday=day+hour/24.0+min/1440.0+sec/86400.0; mjd=date2mjd(year,month,dday); return mjd; } // Compute Julian Day from Date double date2mjd(int year,int month,double day) { int a,b; double jd; if (month<3) { year--; month+=12; } a=floor(year/100.); b=2.-a+floor(a/4.); if (year<1582) b=0; if (year==1582 && month<10) b=0; if (year==1852 && month==10 && day<=4) b=0; jd=floor(365.25*(year+4716))+floor(30.6001*(month+1))+day+b-1524.5; return jd-2400000.5; } // Convert Decimal into Sexagesimal void dec2sex(double x,char *s,int f,int len) { int i; double sec,deg,min; char sign; char *form[]={"::",",,","hms"," "}; sign=(x<0 ? '-' : ' '); x=3600.*fabs(x); sec=fmod(x,60.); x=(x-sec)/60.; min=fmod(x,60.); x=(x-min)/60.; // deg=fmod(x,60.); deg=x; if (len==7) sprintf(s,"%c%02i%c%02i%c%07.4f%c",sign,(int) deg,form[f][0],(int) min,form[f][1],sec,form[f][2]); if (len==6) sprintf(s,"%c%02i%c%02i%c%06.3f%c",sign,(int) deg,form[f][0],(int) min,form[f][1],sec,form[f][2]); if (len==5) sprintf(s,"%c%02i%c%02i%c%05.2f%c",sign,(int) deg,form[f][0],(int) min,form[f][1],sec,form[f][2]); if (len==4) sprintf(s,"%c%02i%c%02i%c%04.1f%c",sign,(int) deg,form[f][0],(int) min,form[f][1],sec,form[f][2]); if (len==2) sprintf(s,"%c%02i%c%02i%c%02i%c",sign,(int) deg,form[f][0],(int) min,form[f][1],(int) floor(sec),form[f][2]); return; }