Пример #1
0
int main(int argc, char *argv[])
{
   float maxpow = 0.0, inx = 0.0, iny = 0.0;
   double centerr, offsetf;
   int zoomlevel, maxzoom, minzoom, xid, psid;
   char *rootfilenm, inchar;
   fftpart *lofp;
   fftview *fv;

   if (argc == 1) {
      printf("\nusage:  explorefft fftfilename\n\n");
      exit(0);
   }

   printf("\n\n");
   printf("      Interactive FFT Explorer\n");
   printf("         by Scott M. Ransom\n");
   printf("            October, 2001\n");
   print_help();

   {
      int hassuffix = 0;
      char *suffix;

      hassuffix = split_root_suffix(argv[1], &rootfilenm, &suffix);
      if (hassuffix) {
         if (strcmp(suffix, "fft") != 0) {
            printf("\nInput file ('%s') must be a FFT file ('.fft')!\n\n", argv[1]);
            free(suffix);
            exit(0);
         }
         free(suffix);
      } else {
         printf("\nInput file ('%s') must be a FFT file ('.fft')!\n\n", argv[1]);
         exit(0);
      }
   }

   /* Read the info file */

   readinf(&idata, rootfilenm);
   if (strlen(remove_whitespace(idata.object)) > 0) {
      printf("Examining %s data from '%s'.\n\n",
             remove_whitespace(idata.object), argv[1]);
   } else {
      printf("Examining data from '%s'.\n\n", argv[1]);
   }
   N = idata.N;
   T = idata.dt * idata.N;
#ifdef USEMMAP
   printf("Memory mapping the input FFT.  This may take a while...\n");
   mmap_file = open(argv[1], O_RDONLY);
   {
      int rt;
      struct stat buf;

      rt = fstat(mmap_file, &buf);
      if (rt == -1) {
         perror("\nError in fstat() in explorefft.c");
         printf("\n");
         exit(-1);
      }
      Nfft = buf.st_size / sizeof(fcomplex);
   }
   lofp = get_fftpart(0, Nfft);
#else
   {
      int numamps;

      fftfile = chkfopen(argv[1], "rb");
      Nfft = chkfilelen(fftfile, sizeof(fcomplex));
      numamps = (Nfft > MAXBINS) ? (int) MAXBINS : (int) Nfft;
      lofp = get_fftpart(0, numamps);
   }
#endif

   /* Plot the initial data */

   {
      int initnumbins = INITIALNUMBINS;

      if (initnumbins > Nfft) {
         initnumbins = next2_to_n(Nfft) / 2;
         zoomlevel = LOGDISPLAYNUM - (int) (log(initnumbins) / log(2.0));
         minzoom = zoomlevel;
      } else {
         zoomlevel = LOGDISPLAYNUM - LOGINITIALNUMBINS;
         minzoom = LOGDISPLAYNUM - LOGMAXBINS;
      }
      maxzoom = LOGDISPLAYNUM - LOGMINBINS;
      centerr = initnumbins / 2;
   }
   fv = get_fftview(centerr, zoomlevel, lofp);

   /* Prep the XWIN device for PGPLOT */

   xid = cpgopen("/XWIN");
   if (xid <= 0) {
      free(fv);
#ifdef USEMMAP
      close(mmap_file);
#else
      fclose(fftfile);
#endif
      free_fftpart(lofp);
      exit(EXIT_FAILURE);
   }
   cpgscr(15, 0.4, 0.4, 0.4);
   cpgask(0);
   cpgpage();
   offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);

   do {
      cpgcurs(&inx, &iny, &inchar);
      if (DEBUGOUT)
         printf("You pressed '%c'\n", inchar);

      switch (inchar) {
      case 'A':                /* Zoom in */
      case 'a':
         centerr = (inx + offsetf) * T;
      case 'I':
      case 'i':
         if (DEBUGOUT)
            printf("  Zooming in  (zoomlevel = %d)...\n", zoomlevel);
         if (zoomlevel < maxzoom) {
            zoomlevel++;
            free(fv);
            fv = get_fftview(centerr, zoomlevel, lofp);
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         } else
            printf("  Already at maximum zoom level (%d).\n", zoomlevel);
         break;
      case 'X':                /* Zoom out */
      case 'x':
      case 'O':
      case 'o':
         if (DEBUGOUT)
            printf("  Zooming out  (zoomlevel = %d)...\n", zoomlevel);
         if (zoomlevel > minzoom) {
            zoomlevel--;
            free(fv);
            fv = get_fftview(centerr, zoomlevel, lofp);
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         } else
            printf("  Already at minimum zoom level (%d).\n", zoomlevel);
         break;
      case '<':                /* Shift left 1 full screen */
         centerr -= fv->numbins + fv->numbins / 8;
      case ',':                /* Shift left 1/8 screen */
         if (DEBUGOUT)
            printf("  Shifting left...\n");
         centerr -= fv->numbins / 8;
         {                      /* Should probably get the previous chunk from the fftfile... */
            double lowestr;

            lowestr = 0.5 * fv->numbins;
            if (centerr < lowestr)
               centerr = lowestr;
         }
         free(fv);
         fv = get_fftview(centerr, zoomlevel, lofp);
         cpgpage();
         offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         break;
      case '>':                /* Shift right 1 full screen */
         centerr += fv->numbins - fv->numbins / 8;
      case '.':                /* Shift right 1/8 screen */
         if (DEBUGOUT)
            printf("  Shifting right...\n");
         centerr += fv->numbins / 8;
         {                      /* Should probably get the next chunk from the fftfile... */
            double highestr;

            highestr = lofp->rlo + lofp->numamps - 0.5 * fv->numbins;
            if (centerr > highestr)
               centerr = highestr;
         }
         free(fv);
         fv = get_fftview(centerr, zoomlevel, lofp);
         cpgpage();
         offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         break;
      case '+':                /* Increase height of powers */
      case '=':
         if (maxpow == 0.0) {
            printf("  Auto-scaling is off.\n");
            maxpow = 1.1 * fv->maxpow;
         }
         maxpow = 3.0 / 4.0 * maxpow;
         cpgpage();
         offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         break;
      case '-':                /* Decrease height of powers */
      case '_':
         if (maxpow == 0.0) {
            printf("  Auto-scaling is off.\n");
            maxpow = 1.1 * fv->maxpow;
         }
         maxpow = 4.0 / 3.0 * maxpow;
         cpgpage();
         offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         break;
      case 'S':                /* Auto-scale */
      case 's':
         if (maxpow == 0.0)
            break;
         else {
            printf("  Auto-scaling is on.\n");
            maxpow = 0.0;
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
            break;
         }
      case 'G':                /* Goto a frequency */
      case 'g':
         {
            char freqstr[50];
            double freq = -1.0;

            while (freq < 0.0) {
               printf("  Enter the frequency (Hz) to go to:\n");
               fgets(freqstr, 50, stdin);
               freqstr[strlen(freqstr) - 1] = '\0';
               freq = atof(freqstr);
            }
            offsetf = 0.0;
            centerr = freq * T;
            printf("  Moving to frequency %.15g.\n", freq);
            free(fv);
            fv = get_fftview(centerr, zoomlevel, lofp);
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, centerr, 2);
         }
         break;
      case 'H':                /* Show harmonics */
      case 'h':
         {
            double retval;

            retval = harmonic_loop(xid, centerr, zoomlevel, lofp);
            if (retval > 0.0) {
               offsetf = 0.0;
               centerr = retval;
               free(fv);
               fv = get_fftview(centerr, zoomlevel, lofp);
               cpgpage();
               offsetf = plot_fftview(fv, maxpow, 1.0, centerr, 2);
            }
         }
         break;
      case '?':                /* Print help screen */
         print_help();
         break;
      case 'D':                /* Show details about a selected point  */
      case 'd':
         {
            double newr;

            printf("  Searching for peak near freq = %.7g Hz...\n", (inx + offsetf));
            newr = find_peak(inx + offsetf, fv, lofp);
            centerr = newr;
            free(fv);
            fv = get_fftview(centerr, zoomlevel, lofp);
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, centerr, 2);
         }
         break;
      case 'L':                /* Load a zaplist */
      case 'l':
         {
            int ii, len;
            char filename[200];
            double *lobins, *hibins;

            printf("  Enter the filename containing the zaplist to load:\n");
            fgets(filename, 199, stdin);
            len = strlen(filename) - 1;
            filename[len] = '\0';
            numzaplist = get_birdies(filename, T, 0.0, &lobins, &hibins);
            lenzaplist = numzaplist + 20;       /* Allow some room to add more */
            if (lenzaplist)
               free(zaplist);
            zaplist = (bird *) malloc(sizeof(bird) * lenzaplist);
            for (ii = 0; ii < numzaplist; ii++) {
               zaplist[ii].lobin = lobins[ii];
               zaplist[ii].hibin = hibins[ii];
            }
            vect_free(lobins);
            vect_free(hibins);
            printf("\n");
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         }
         break;
      case 'Z':                /* Add a birdie to a zaplist */
      case 'z':
         {
            int badchoice = 2;
            float lox, hix, loy, hiy;
            double rs[2];
            char choice;

            if (numzaplist + 1 > lenzaplist) {
               lenzaplist += 10;
               zaplist = (bird *) realloc(zaplist, sizeof(bird) * lenzaplist);
            }
            cpgqwin(&lox, &hix, &loy, &hiy);
            printf("  Click the left mouse button on the first frequency limit.\n");
            while (badchoice) {
               cpgcurs(&inx, &iny, &choice);
               if (choice == 'A' || choice == 'a') {
                  rs[2 - badchoice] = ((double) inx + offsetf) * T;
                  cpgsave();
                  cpgsci(7);
                  cpgmove(inx, 0.0);
                  cpgdraw(inx, hiy);
                  cpgunsa();
                  badchoice--;
                  if (badchoice == 1)
                     printf
                         ("  Click the left mouse button on the second frequency limit.\n");
               } else {
                  printf("  Option not recognized.\n");
               }
            };
            if (rs[1] > rs[0]) {
               zaplist[numzaplist].lobin = rs[0];
               zaplist[numzaplist].hibin = rs[1];
            } else {
               zaplist[numzaplist].lobin = rs[1];
               zaplist[numzaplist].hibin = rs[0];
            }
            printf("    The new birdie has:  f_avg = %.15g  f_width = %.15g\n\n",
                   0.5 * (zaplist[numzaplist].hibin + zaplist[numzaplist].lobin) / T,
                   (zaplist[numzaplist].hibin - zaplist[numzaplist].lobin) / T);
            numzaplist++;
            qsort(zaplist, numzaplist, sizeof(bird), compare_birds);
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         }
         break;
      case 'P':                /* Print the current plot */
      case 'p':
         {
            int len;
            char filename[200];

            printf("  Enter the filename to save the plot as:\n");
            fgets(filename, 196, stdin);
            len = strlen(filename) - 1;
            filename[len + 0] = '/';
            filename[len + 1] = 'P';
            filename[len + 2] = 'S';
            filename[len + 3] = '\0';
            psid = cpgopen(filename);
            cpgslct(psid);
            cpgpap(10.25, 8.5 / 11.0);
            cpgiden();
            cpgscr(15, 0.8, 0.8, 0.8);
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
            cpgclos();
            cpgslct(xid);
            cpgscr(15, 0.4, 0.4, 0.4);
            filename[len] = '\0';
            printf("  Wrote the plot to the file '%s'.\n", filename);
         }
         break;
      case 'N':                /* Changing power normalization */
      case 'n':
         {
            float inx2 = 0.0, iny2 = 0.0;
            char choice;
            unsigned char badchoice = 1;

            printf("  Specify the type of power normalization:\n"
                   "       m,M  :  Median values determined locally\n"
                   "       d,D  :  DC frequency amplitude\n"
                   "       r,R  :  Raw powers (i.e. no normalization)\n"
                   "       u,U  :  User specified interval (the average powers)\n");
            while (badchoice) {
               cpgcurs(&inx2, &iny2, &choice);
               switch (choice) {
               case 'M':
               case 'm':
                  norm_const = 0.0;
                  maxpow = 0.0;
                  badchoice = 0;
                  printf
                      ("  Using local median normalization.  Autoscaling is on.\n");
                  break;
               case 'D':
               case 'd':
                  norm_const = 1.0 / r0;
                  maxpow = 0.0;
                  badchoice = 0;
                  printf
                      ("  Using DC frequency (%f) normalization.  Autoscaling is on.\n",
                       r0);
                  break;
               case 'R':
               case 'r':
                  norm_const = 1.0;
                  maxpow = 0.0;
                  badchoice = 0;
                  printf
                      ("  Using raw powers (i.e. no normalization).  Autoscaling is on.\n");
                  break;
               case 'U':
               case 'u':
                  {
                     char choice2;
                     float xx = inx, yy = iny;
                     int lor, hir, numr;
                     double avg, var;

                     printf
                         ("  Use the left mouse button to select a left and right boundary\n"
                          "  of a region to calculate the average power.\n");
                     do {
                        cpgcurs(&xx, &yy, &choice2);
                     } while (choice2 != 'A' && choice2 != 'a');
                     lor = (int) ((xx + offsetf) * T);
                     cpgsci(7);
                     cpgmove(xx, 0.0);
                     cpgdraw(xx, 10.0 * fv->maxpow);
                     do {
                        cpgcurs(&xx, &yy, &choice2);
                     } while (choice2 != 'A' && choice2 != 'a');
                     hir = (int) ((xx + offsetf) * T);
                     cpgmove(xx, 0.0);
                     cpgdraw(xx, 10.0 * fv->maxpow);
                     cpgsci(1);
                     if (lor > hir) {
                        int tempr;
                        tempr = hir;
                        hir = lor;
                        lor = tempr;
                     }
                     numr = hir - lor + 1;
                     avg_var(lofp->rawpowers + lor - lofp->rlo, numr, &avg, &var);
                     printf("  Selection has:  average = %.5g\n"
                            "                  std dev = %.5g\n", avg, sqrt(var));
                     norm_const = 1.0 / avg;
                     maxpow = 0.0;
                     badchoice = 0;
                     printf
                         ("  Using %.5g as the normalization constant.  Autoscaling is on.\n",
                          avg);
                     break;
                  }
               default:
                  printf("  Unrecognized choice '%c'.\n", choice);
                  break;
               }
            }
            free(fv);
            fv = get_fftview(centerr, zoomlevel, lofp);
            cpgpage();
            offsetf = plot_fftview(fv, maxpow, 1.0, 0.0, 0);
         }
         break;
      case 'Q':                /* Quit */
      case 'q':
         printf("  Quitting...\n");
         free(fv);
         cpgclos();
         break;
      default:
         printf("  Unrecognized option '%c'.\n", inchar);
         break;
      }
   } while (inchar != 'Q' && inchar != 'q');

   free_fftpart(lofp);
#ifdef USEMMAP
   close(mmap_file);
#else
   fclose(fftfile);
#endif
   if (lenzaplist)
      free(zaplist);
   printf("Done\n\n");
   return 0;
}
Пример #2
0
int main(int argc,char *argv[])
{
  int i;
  char fname[128];
  dSet *data; 
  float freq,bw,chanbw;
  int nchan,npol;
  float bpass[4096];
  float fx[4096];
  float miny,maxy,minx,maxx;
  float ominy,omaxy,ominx,omaxx;
  float mx,my,mx2,my2;
  float binw;
  char key;
  char grDev[128]="/xs";
  int interactive=1;
  int noc1=0;
  int zapChannels[4096];
  int nzap=0;
  int overlay=-1;
  float overlayVal[MAX_OVERLAY];
  char overlayStr[MAX_OVERLAY][128];
  char overlayFile[128];
  int noverlay=0;
  fitsfile *fp;

  data = initialiseDset();
  

  for (i=0;i<argc;i++)
    {
      if (strcmp(argv[i],"-f")==0)
	strcpy(fname,argv[++i]);
      else if (strcmp(argv[i],"-noc1")==0)
	noc1=1;
      else if (strcmp(argv[i],"-g")==0)
	{
	  strcpy(grDev,argv[++i]);
	  interactive=0;
	}
      else if (strcmp(argv[i],"-h")==0)
	help();
      else if (strcmp(argv[i],"-overlay")==0)
	{
	  strcpy(overlayFile,argv[++i]);
	  overlay=1;
	}
    }
  if (overlay==1)
    {
      FILE *fin;
      char line[1024];
      noverlay=0;

      if (!(fin = fopen(overlayFile,"r")))
	  printf("Unable to open overlay file >%s<\n",overlayFile);
      else
	{
	  while (!feof(fin))
	    {
	      fgets(overlayStr[noverlay],1024,fin);
	      if (fscanf(fin,"%f",&overlayVal[noverlay])==1)
		{
		  if (overlayStr[noverlay][strlen(overlayStr[noverlay])-1] == '\n')
		    overlayStr[noverlay][strlen(overlayStr[noverlay])-1]='\0';
		  noverlay++;
		}
	    }
	  fclose(fin);
	}
    }
  fp   = openFitsFile(fname); 
  loadPrimaryHeader(fp,data);
  displayHeaderInfo(data);
  readBandpass(fp,bpass);
  nchan = data->phead.nchan;
  freq  = data->phead.freq;
  bw    = data->phead.bw;
  chanbw = data->phead.chanbw;

  for (i=0;i<nchan;i++)
    {
      fx[i] = freq-bw/2+(i+0.5)*chanbw;
      if (i==noc1)
	{
	  miny = maxy = bpass[i];
	  minx = maxx = fx[i];
	}
      else if (i!=0)
	{
	  if (bpass[i] > maxy) maxy = bpass[i];
	  if (bpass[i] < miny) miny = bpass[i];
	  if (fx[i] > maxx) maxx = fx[i];
	  if (fx[i] < minx) minx = fx[i];
	}
    }
  ominx = minx;
  omaxx = maxx;
  ominy = miny;
  omaxy = maxy;
  binw = fx[1]-fx[0];
  printf("Complete\n");

  cpgbeg(0,grDev,1,1);
  cpgask(0);
  do {
    cpgenv(minx,maxx,miny,maxy,0,1);
    cpglab("Frequency (MHz)","Amplitude (arbitrary)",fname);
    cpgbin(nchan-noc1,fx+noc1,bpass+noc1,0);
    if (overlay==1)
      {
	float tx[2],ty[2];
	cpgsls(4); cpgsci(2); cpgsch(0.8);
	for (i=0;i<noverlay;i++)
	  {
	    tx[0] = tx[1] = overlayVal[i];
	    ty[0] = miny;
	    ty[1] = maxy;
	    if (tx[1] > minx && tx[1] < maxx)
	      {
		cpgline(2,tx,ty);
		//		cpgtext(tx[1],ty[1]-0.05*(maxy-miny),overlayStr[i]);
		cpgptxt(tx[1]-0.004*(maxx-minx),ty[0]+0.05*(maxy-miny),90,0.0,overlayStr[i]);
	      }
	  }
	cpgsci(1); cpgsls(1); cpgsch(1);
      }
    if (interactive==1)
      {
	cpgcurs(&mx,&my,&key);
	if (key=='A')
	  {
	    int cc=-1;
	    int i;
	    for (i=0;i<nchan-1;i++)
	      {
		//		if ((bw > 0 && (mx > fx[i]-binw/2 && mx < fx[i]+binw/2)) ||
		//		    (bw < 0 && (mx > fx[i]+binw/2 && mx < fx[i]-binw/2)))
		if ((bw > 0 && (mx > fx[i] && mx < fx[i]+binw)) ||
		    (bw < 0 && (mx > fx[i] && mx < fx[i]+binw)))
		  {
		    cc = i;
		    break;
		  }
	      }
	    printf("mouse x = %g MHz, mouse y = %g, channel = %d, channel frequency = %g MHz\n",mx,my,cc,fx[cc]);
	  }
	else if (key=='X')
	  {
	    int cc=-1;
	    int i;
	    printf("Deleting %g %g %g\n",mx,fx[10],binw);
	    for (i=0;i<nchan-1;i++)
	      {
		//		if ((bw > 0 && (mx > fx[i]-binw/2 && mx < fx[i]+binw/2)) ||
		//		    (bw < 0 && (mx > fx[i]+binw/2 && mx < fx[i]-binw/2)))
		if ((bw > 0 && (mx > fx[i] && mx < fx[i]+binw)) ||
		    (bw < 0 && (mx > fx[i] && mx < fx[i]+binw)))
		  {
		    cc = i;
		    break;
		  }
	      }
	    printf("Want to delete = %d\n",cc);
	    if (cc != -1)
	      {
		bpass[cc] = 0;
		omaxy = bpass[noc1];
		zapChannels[nzap++] = cc;
		for (i=noc1;i<nchan;i++)
		  {
		    if (omaxy < bpass[i]) omaxy = bpass[i];
		  }
	      }
	  }
	else if (key=='z')
	  {
	    cpgband(2,0,mx,my,&mx2,&my2,&key);
	    if (mx > mx2) {maxx = mx; minx = mx2;}
	    else {maxx = mx2; minx = mx;}
	    
	    if (my > my2) {maxy = my; miny = my2;}
	    else {maxy = my2; miny = my;}	   
	  }
	else if (key=='u')
	  {
	    minx = ominx;
	    maxx = omaxx;
	    miny = ominy;
	    maxy = omaxy;
	  }
	else if (key=='l') // List the channels and frequencies to zap
	  {
	    int i;
	    sortInt(zapChannels,nzap);
	    printf("-------------------------------------------------------\n");
	    printf("Zap channels with first channel = 0\n\n");
	    for (i=0;i<nzap;i++)
	      printf("%d ",zapChannels[i]);
	    printf("\n\n");
	    printf("Zap channels with first channel = 1\n\n");
	    for (i=0;i<nzap;i++)
	      printf("%d ",zapChannels[i]+1);
	    printf("\n\n");
	    printf("Zap channels frequencies:\n\n");
	    for (i=0;i<nzap;i++)
	      printf("%g ",fx[zapChannels[i]]);
	    printf("\n\n");
	    printf("-------------------------------------------------------\n");
	  }
	else if (key=='%') // Enter percentage of the band edges to zap
	  {
	    float percent;
	    int i;

	    printf("Enter band edge percentage to zap ");
	    scanf("%f",&percent);
	    for (i=0;i<nchan;i++)
	      {
		if (i < nchan*percent/100.0 || i > nchan-(nchan*percent/100.0))
		  {
		    bpass[i] = 0;		    
		    zapChannels[nzap++] = i;

		  }
	      }
	    omaxy = bpass[noc1];
	    for (i=noc1;i<nchan;i++)
	      {
		if (omaxy < bpass[i]) omaxy = bpass[i];
	      }

	    // Unzoom
	    minx = ominx;
	    maxx = omaxx;
	    miny = ominy;
	    maxy = omaxy;
	  }

      }
  } while (key != 'q' && interactive==1);
  cpgend();
}
Пример #3
0
static double harmonic_loop(int xid, double rr, int zoomlevel, fftpart * fp)
{
   float inx = 0.0, iny = 0.0;
   double retval = 0.0;
   int xid2, psid, badchoice = 1;
   char choice;

   xid2 = cpgopen("/XWIN");
   cpgpap(10.25, 8.5 / 11.0);
   cpgask(0);
   cpgslct(xid2);
   plot_harmonics(rr, zoomlevel, fp);
   printf("  Click on the harmonic to go it,\n"
          "    press 'P' to print, or press 'Q' to close.\n");
   while (badchoice) {
      cpgcurs(&inx, &iny, &choice);
      if (choice == 'Q' || choice == 'q') {
         badchoice = 0;
      } else if (choice == 'P' || choice == 'p') {
         int len, numharmbins;
         double offsetf;
         char filename[200];
         fftpart *harmpart;
         fftview *harmview;

         printf("  Enter the filename to save the plot as:\n");
         fgets(filename, 195, stdin);
         len = strlen(filename) - 1;
         strcpy(filename + len, "/CPS");
         psid = cpgopen(filename);
         cpgslct(psid);
         cpgpap(10.25, 8.5 / 11.0);
         cpgiden();
         cpgscr(15, 0.8, 0.8, 0.8);
         numharmbins = (1 << (LOGDISPLAYNUM - zoomlevel));
         harmpart = get_fftpart((int) (rr - numharmbins), 2 * numharmbins);
         harmview = get_fftview(rr, zoomlevel, harmpart);
         free_fftpart(harmpart);
         offsetf = plot_fftview(harmview, 0.0, 1.0, rr, 2);
         cpgpage();
         plot_harmonics(rr, zoomlevel, fp);
         cpgclos();
         cpgslct(xid2);
         cpgscr(15, 0.4, 0.4, 0.4);
         filename[len] = '\0';
         printf("  Wrote the plot to the file '%s'.\n", filename);
      } else if (choice == 'A' || choice == 'a') {
         if (iny > 1.0)
            retval = rr * (int) (inx);
         else if (iny > 0.0)
            retval = rr * ((int) (inx) + 4.0);
         else if (iny > -1.0)
            retval = rr / (int) (inx);
         else
            retval = rr / ((int) (inx) + 4.0);
         badchoice = 0;
      } else {
         printf("  Option not recognized.\n");
      }
   };
   cpgclos();
   cpgslct(xid);
   return retval;
}
Пример #4
0
int main(int argc, char **argv)

{
  char alt = '\0', *header, idents[3][80], *infile, keyword[16], nlcprm[1],
       opt[2], pgdev[16];
  int  c0[] = {-1, -1, -1, -1, -1, -1, -1};
  int  alts[27], gcode[2], hdunum = 1, hdutype, i, ic, naxes, naxis[2],
       nkeyrec, nreject, nwcs, stat[NWCSFIX], status;
  float  blc[2], trc[2];
  double cache[257][4], grid1[3], grid2[3], nldprm[1];
  struct wcsprm *wcs;
  nlfunc_t pgwcsl_;
  fitsfile *fptr;


  /* Parse options. */
  strcpy(pgdev, "/XWINDOW");
  for (i = 1; i < argc && argv[i][0] == '-'; i++) {
    if (!argv[i][1]) break;

    switch (argv[i][1]) {
    case 'a':
      alt = toupper(argv[i][2]);
      break;

    case 'd':
      if (argv[i][2] == '?') {
        cpgldev();
        return 0;
      }

      if (argv[i][2] == '/') {
        strncpy(pgdev+1, argv[i]+3, 15);
      } else {
        strncpy(pgdev+1, argv[i]+2, 15);
      }
      break;

    case 'h':
      hdunum = atoi(argv[i]+2);
      break;

    default:
      fprintf(stderr, "%s", usage);
      return 1;
    }
  }

  if (i < argc) {
    infile = argv[i++];

    if (i < argc) {
      fprintf(stderr, "%s", usage);
      return 1;
    }
  } else {
    infile = "-";
  }

  /* Check accessibility of the input file. */
  if (strcmp(infile, "-") && access(infile, R_OK) == -1) {
    printf("wcsgrid: Cannot access %s.\n", infile);
    return 1;
  }


  /* Open the FITS file and move to the required HDU. */
  status = 0;
  if (fits_open_file(&fptr, infile, READONLY, &status)) goto fitserr;
  if (fits_movabs_hdu(fptr, hdunum, &hdutype, &status)) goto fitserr;
  if (hdutype != IMAGE_HDU) {
    fprintf(stderr, "ERROR, HDU number %d does not contain an image array.\n",
      hdunum);
    return 1;
  }

  /* Check that we have at least two image axes. */
  if (fits_read_key(fptr, TINT, "NAXIS",  &naxes, NULL, &status)) {
    goto fitserr;
  }

  if (naxes < 2) {
    fprintf(stderr, "ERROR, HDU number %d does not contain a 2-D image.\n",
      hdunum);
    return 1;
  } else if (naxes > 2) {
    printf("HDU number %d contains a %d-D image array.\n", hdunum, naxes);
  }

  /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */
  if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) {
    goto fitserr;
  }


  /* Interpret the WCS keywords. */
  if ((status = wcspih(header, nkeyrec, WCSHDR_all, -3, &nreject, &nwcs,
                       &wcs))) {
    fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcshdr_errmsg[status]);
    return 1;
  }
  free(header);

  /* Read -TAB arrays from the binary table extension (if necessary). */
  if (fits_read_wcstab(fptr, wcs->nwtb, (wtbarr *)wcs->wtb, &status)) {
    goto fitserr;
  }

  /* Translate non-standard WCS keyvalues. */
  if ((status = wcsfix(7, 0, wcs, stat))) {
    status = 0;
    for (i = 0; i < NWCSFIX; i++) {
      if (stat[i] > 0) {
         fprintf(stderr, "wcsfix ERROR %d: %s.\n", stat[i],
                 wcsfix_errmsg[stat[i]]);

        /* Ignore problems with CDi_ja and DATE-OBS. */
        if (!(i == CDFIX || i == DATFIX)) status = 1;
      }
    }

    if (status) return 1;
  }

  /* Sort out alternates. */
  if (alt) {
    wcsidx(nwcs, &wcs, alts);

    if (alt == ' ') {
      if (alts[0] == -1) {
        fprintf(stderr, "WARNING, no primary coordinate representation, "
                        "doing all.\n");
        alt = '\0';
      }

    } else if (alt < 'A' || alt > 'Z') {
      fprintf(stderr, "WARNING, alternate specifier \"%c\" is invalid, "
                      "doing all.\n", alt);
      alt = '\0';

    } else {
      if (alts[alt - 'A' + 1] == -1) {
        fprintf(stderr, "WARNING, no alternate coordinate representation "
                        "\"%c\", doing all.\n", alt);
        alt = '\0';
      }
    }
  }

  /* Get image dimensions from the header. */
  sprintf(keyword, "NAXIS%d", wcs->lng + 1);
  fits_read_key(fptr, TINT, "NAXIS1", naxis,   NULL, &status);
  sprintf(keyword, "NAXIS%d", wcs->lat + 1);
  fits_read_key(fptr, TINT, "NAXIS2", naxis+1, NULL, &status);

  if ((naxis[0] < 2) || (naxis[1] < 2)) {
    fprintf(stderr, "ERROR, HDU number %d contains degenerate image axes.\n",
      hdunum);
    return 1;
  }

  fits_close_file(fptr, &status);


  /* Plot setup. */
  blc[0] = 0.5f;
  blc[1] = 0.5f;
  trc[0] = naxis[0] + 0.5f;
  trc[1] = naxis[1] + 0.5f;

  if (cpgbeg(0, pgdev, 1, 1) != 1) {
    fprintf(stderr, "ERROR, failed to open PGPLOT device %s.\n", pgdev);
    return 1;
  }
  cpgvstd();

  cpgwnad(blc[0], trc[0], blc[0], trc[1]);
  cpgask(1);
  cpgpage();

  /* Compact lettering. */
  cpgsch(0.8f);

  /* Draw full grid lines. */
  gcode[0] = 2;
  gcode[1] = 2;
  grid1[0] =    0.0;
  grid2[0] =    0.0;

  /* These are for the projection boundary. */
  grid1[1] = -180.0;
  grid1[2] =  180.0;
  grid2[1] =  -90.0;
  grid2[2] =   90.0;

  cpgsci(1);

  for (i = 0; i < nwcs; i++) {
    if (alt && (wcs+i)->alt[0] != alt) {
      continue;
    }

    if ((status = wcsset(wcs+i))) {
      fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]);
      continue;
    }

    /* Draw the frame. */
    cpgbox("BC", 0.0f, 0, "BC", 0.0f, 0);

    /* Axis labels; use CNAMEia in preference to CTYPEia. */
    if ((wcs+i)->cname[0][0]) {
      strcpy(idents[0], (wcs+i)->cname[0]);
    } else {
      strcpy(idents[0], (wcs+i)->ctype[0]);
    }

    if ((wcs+i)->cname[1][0]) {
      strcpy(idents[1], (wcs+i)->cname[1]);
    } else {
      strcpy(idents[1], (wcs+i)->ctype[1]);
    }

    /* Title; use WCSNAME. */
    strcpy(idents[2], (wcs+i)->wcsname);
    if (strlen(idents[2])) {
      printf("\n%s\n", idents[2]);
    }

    /* Formatting control for celestial coordinates. */
    if (strncmp((wcs+i)->ctype[0], "RA", 2) == 0) {
      /* Right ascension in HMS, declination in DMS. */
      opt[0] = 'G';
      opt[1] = 'E';
    } else {
      /* Other angles in decimal degrees. */
      opt[0] = 'A';
      opt[1] = 'B';
    }

    /* Draw the celestial grid.  The grid density is set for each world */
    /* coordinate by specifying LABDEN = 1224. */
    ic = -1;
    cpgsbox(blc, trc, idents, opt, 0, 1224, c0, gcode, 0.0, 0, grid1, 0,
      grid2, 0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)(wcs+i), nldprm, 256,
      &ic, cache, &status);

    /* Delimit the projection boundary. */
    if ((wcs+i)->cel.prj.category != ZENITHAL) {
      /* Reset to the native coordinate graticule. */
      (wcs+i)->crval[0] = (wcs+i)->cel.prj.phi0;
      (wcs+i)->crval[1] = (wcs+i)->cel.prj.theta0;
      (wcs+i)->lonpole  = 999.0;
      (wcs+i)->latpole  = 999.0;
      status = wcsset(wcs+i);

      ic = -1;
      cpgsbox(blc, trc, idents, opt, -1, 0, c0, gcode, 0.0, 2, grid1, 2,
        grid2, 0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)(wcs+i), nldprm, 256,
        &ic, cache, &status);
    }

    cpgpage();
  }

  status = wcsvfree(&nwcs, &wcs);

  return 0;

fitserr:
  fits_report_error(stderr, status);
  fits_close_file(fptr, &status);
  return 1;
}
Пример #5
0
void doPlot(pulsar *psr,int npsr,float *scale,int nScale,char *grDev,int plotUs,float fontSize,float centreMJD,int ptStyle,float ptSize,int error,float minyv,float maxyv,float minxv,float maxxv,int nOverlay,float labelsize,float fracX)
{
  int i,j,fitFlag=2,exitFlag=0,scale1=0,scale2,count[MAX_PSR],p,xautoscale=0,k,graphics=1;
  int yautoscale=0,plotpre=1;
  int ps,pe,pi;
  int time=0;
  char xstr[1000],ystr[1000];
  float px[2],py[2],pye1[2],pye2[2];
  float x[MAX_PSR][MAX_OBSN],y[MAX_PSR][MAX_OBSN],yerr1[MAX_PSR][MAX_OBSN],yerr2[MAX_PSR][MAX_OBSN],tmax,tmin,tmaxy1,tminy1,tmaxy2,tminy2;
  float sminy[MAX_PSR],smaxy[MAX_PSR];
  float minx[MAX_PSR],maxx[MAX_PSR],miny[MAX_PSR],maxy[MAX_PSR],plotx1,plotx2,ploty1,ploty2,mean;
  float fx[2],fy[2];
  float mouseX,mouseY;
  char key;
  //  float widthPap=0.0,aspectPap=0.618;
  float widthPap=0.0,aspectPap=1;
  float xx[MAX_OBSN],yy[MAX_OBSN],yyerr1[MAX_OBSN],yyerr2[MAX_OBSN];
  int num=0,colour;

  /* Obtain a graphical PGPLOT window */
  cpgbeg(0,grDev,1,1);
  //    cpgpap(widthPap,aspectPap);
  cpgsch(fontSize);
  cpgscf(2);
  cpgslw(2);
  cpgask(0);

  for (p=0;p<npsr;p++)
    {
      scale2 = psr[p].nobs;
      
      /*      sprintf(xstr,"MJD-%.1Lf",psr[0].param[param_pepoch].val[0]); */
      if (centreMJD == -1)
	sprintf(xstr,"Year"); 
      else
	sprintf(xstr,"MJD-%.1f",centreMJD); 

      sprintf(ystr,"Residual (\\gmsec)");
      
      count[p]=0;
      printf("points = %d\n",psr[p].nobs);
      for (i=0;i<psr[p].nobs;i++)
	{	  
	  if (psr[p].obsn[i].deleted == 0 &&
	      (psr[p].param[param_start].paramSet[0]!=1 || psr[p].param[param_start].fitFlag[0]!=1 ||
	       psr[p].param[param_start].val[0] < psr[p].obsn[i].bat) &&
	      (psr[p].param[param_finish].paramSet[0]!=1 || psr[p].param[param_finish].fitFlag[0]!=1 ||
	       psr[p].param[param_finish].val[0] > psr[p].obsn[i].bat))
	    {
	      /* x[p][count[p]] = (double)(psr[p].obsn[i].bat-psr[0].param[param_pepoch].val[0]);	     	       */
	      if (centreMJD == -1)
		x[p][count[p]] = calcYr(psr[p].obsn[i].bat);
	      else
		x[p][count[p]] = (double)(psr[p].obsn[i].bat-centreMJD); 
	      y[p][count[p]] = (double)psr[p].obsn[i].residual*1.0e6;
	      if (nScale>0)
		y[p][count[p]] *= scale[p];
	      count[p]++;
	    }
	}
      /* Remove mean from the residuals and calculate error bars */
      mean = findMean(y[p],psr,p,scale1,count[p]);
      count[p]=0;
      for (i=0;i<psr[p].nobs;i++)
	{
	  if (psr[p].obsn[i].deleted==0   &&
	      (psr[p].param[param_start].paramSet[0]!=1 || psr[p].param[param_start].fitFlag[0]!=1 ||
	       psr[p].param[param_start].val[0] < psr[p].obsn[i].bat) &&
	      (psr[p].param[param_finish].paramSet[0]!=1 || psr[p].param[param_finish].fitFlag[0]!=1 ||
	       psr[p].param[param_finish].val[0] > psr[p].obsn[i].bat))
	    {
	      psr[p].obsn[i].residual-=mean/1.0e6;
	      y[p][count[p]]-=mean;
	      yerr1[p][count[p]] = y[p][count[p]]-(float)psr[p].obsn[i].toaErr;
	      yerr2[p][count[p]] = y[p][count[p]]+(float)psr[p].obsn[i].toaErr;
	      count[p]++;
	    }
	}
    	  
      /* Get scaling for graph */
      if (minxv == maxxv) {
	minx[p] = findMin(x[p],psr,p,scale1,count[p]);
	maxx[p] = findMax(x[p],psr,p,scale1,count[p]);
      }
      else {
	minx[p] = minxv;
	maxx[p] = maxxv;
      }
      if (minyv == maxyv){
	miny[p] = findMin(y[p],psr,p,scale1,count[p]);
	maxy[p] = findMax(y[p],psr,p,scale1,count[p]);
      }
      else {
	miny[p] = minyv;
	maxy[p] = maxyv;
      }
      sminy[p] = miny[p]/1e6;
      smaxy[p] = maxy[p]/1e6;
    }
  for (p=0;p<npsr;p++)
    {
      for (i=0;i<count[p];i++)
	{
	  y[p][i] = (y[p][i]-miny[p])/(maxy[p]-miny[p]);
	  yerr1[p][i] = (yerr1[p][i]-miny[p])/(maxy[p]-miny[p]);
	  yerr2[p][i] = (yerr2[p][i]-miny[p])/(maxy[p]-miny[p]);
	}
      //      maxy[p] = 1.0;
      //      miny[p] = 0.0;
    }
  

  tmin = findMinVal(minx,npsr);
  tmax = findMaxVal(maxx,npsr);

  tminy2 = 0.0; //findMinVal(miny,npsr);
  tmaxy2 = 1.0; //findMaxVal(maxy,npsr);

  plotx1 = tmin-(tmax-tmin)*0.1;
  plotx2 = tmax+(tmax-tmin)*0.1;
  
  //  ploty1 = tminy2-(tmaxy2-tminy2)*0.1;
  //  ploty2 = tmaxy2+(tmaxy2-tminy2)*0.1;
	
  ploty1 = 0.1;
  ploty2 = 0.9;

  for (p=0;p<npsr;p++)
    {
      for (i=0;i<count[p];i++)
	{
	  y[p][i]=(p)+ploty1+y[p][i]*(ploty2-ploty1);
	  yerr1[p][i]=(p)+ploty1+yerr1[p][i]*(ploty2-ploty1);
	  yerr2[p][i]=(p)+ploty1+yerr2[p][i]*(ploty2-ploty1);
	}
    } 
  
  printf("ytick = %g\n",ploty2-ploty1);
      /*  cpgenv(plotx1,plotx2,ploty1,ploty2+(ploty2-ploty1)*(npsr-1),0,0); */
  //  cpgenv(plotx1,plotx2,0,npsr+1,0,-1);

  if (labelsize!=-1)
    cpgsch(labelsize);
  cpgsvp(fracX,1.0,0.1,1.0);
  cpgswin(0,1,0,npsr);
  cpgbox("ABC",0.0,0,"C",0.0,0);
  cpgsch(fontSize);
  char str[1000];
  for (p=0;p<npsr;p++)
    {
      cpgsch(fontSize);
      //      cpgtext(tmax+(tmax-tmin)*0.05,p+1.5-0.5,psr[p].name);
      cpgtext(0,p+0.6,psr[p].name);
      //      cpgsch(fontSize);
      if (plotUs==0)
	{
	  sprintf(str,"%.2f",(double)((smaxy[p]-sminy[p])*psr[p].param[param_f].val[0]));
	  cpgtext(0,p+0.4,str);
	  //	  cpgtext(tmax+(tmax-tmin)*0.05,p+1.1-0.5,str);
	}
      else
	{
	  sprintf(str,"%.2f\\gms",(double)((smaxy[p]-sminy[p])/1e-6));
	  //	  cpgtext(tmax+(tmax-tmin)*0.05,p+1.1-0.5,str);
	  cpgtext(0,p+0.1,str);
	}
      cpgsch(1);
      px[0] = 0;
      //      px[1] = tmax; //+(tmax-tmin)*0.03;
	px[1] = 1;
      py[0] = p;
      py[1] = p;
      cpgline(2,px,py);
      
    }
  if (labelsize!=-1)
    cpgsch(labelsize);

  cpgsvp(0.1,fracX,0.1,1.0);
  cpgswin(plotx1,plotx2,0,npsr);
  cpgbox("ATNSBC",0.0,0,"B",0.0,0);
  cpglab(xstr,"","");	    
  cpgsch(fontSize);

  for (p=0;p<npsr;p++)
    {
      cpgsls(1);
      px[0] = plotx1;
      //      px[1] = tmax; //+(tmax-tmin)*0.03;
      px[1] = plotx2;
      py[0] = p;
      py[1] = p;
      cpgline(2,px,py);
      cpgsls(4);
      px[0] = tmin;
      px[1] = tmax+(tmax-tmin)*0.03;

      py[0]=py[1] =(p)+ploty1+(-miny[p]/(maxy[p]-miny[p]))*(ploty2-ploty1);
      //      py[0]=py[1] = (p)+ploty1;
      //      py[0] = py[1] = (0-miny[p])/(maxy[p]-miny[p])/(ploty2-ploty1)+p;
      cpgline(2,px,py);

      px[0] = plotx1+0.005*(plotx2-plotx1);
      py[0] = p;
      pye1[0] = p + 5/(ploty2-ploty1);
      pye2[0] = p - 5/(ploty2-ploty1);
      cpgsls(1);
      cpgsch(3);
      //      cpgerry(1,px,pye1,pye2,1); 
      cpgsch(1);

      for (colour=0;colour<5;colour++)
	{
	  num=0;
	  for (i=0;i<count[p];i++)
	    {
	      if ((colour==0 && psr[p].obsn[i].freq<=500) ||
		  (colour==1 && psr[p].obsn[i].freq>500 && psr[p].obsn[i].freq<=1000) ||
		  (colour==2 && psr[p].obsn[i].freq>1000 && psr[p].obsn[i].freq<=1500) ||
		  (colour==3 && psr[p].obsn[i].freq>1500 && psr[p].obsn[i].freq<=3300) ||
		  (colour==4 && psr[p].obsn[i].freq>3300))
		{
		  xx[num]=x[p][i];
		  yy[num]=y[p][i];
		  yyerr1[num]=yerr1[p][i];
		  yyerr2[num]=yerr2[p][i];
		  //		  printf("plotting: %g\n",yy[num]);

		  num++;
		}
	    }
	  cpgsci(colour+1);
	  cpgsch(ptSize);
	  cpgpt(num,xx,yy,ptStyle);
	  if (error==1)
	    cpgerry(num,xx,yyerr1,yyerr2,1);
	  cpgsch(fontSize);
	  // Plot arrow giving one period
	  fx[0] = fx[1] = tmin-(tmax-tmin)*0.05;
	  //	  fy[0] = (p+1)+0.5-(float)(1.0/psr[p].param[param_f].val[0])/2.0/(ploty2-ploty1);
	  //	  fy[1] = (p+1)+0.5+(float)(1.0/psr[p].param[param_f].val[0])/2.0/(ploty2-ploty1);

	  //	  fy[0] = (-(float)(1.0/psr[p].param[param_f].val[0])/2.0/1.0e6 - miny[p])/(maxy[p]-miny[p])/(ploty2-ploty1) + (p+1)+0.5;
	  //	  fy[1] = ((float)(1.0/psr[p].param[param_f].val[0])/2.0/1.0e6 - miny[p])/(maxy[p]-miny[p])/(ploty2-ploty1) + (p+1)+0.5;
	  fy[0] = (p+1)+0.5+(float)(1.0/psr[p].param[param_f].val[0])/2.0/(maxy[p]-miny[p])*1e6;
	  fy[1] = (p+1)+0.5-(float)(1.0/psr[p].param[param_f].val[0])/2.0/(maxy[p]-miny[p])*1e6;
	  if (fy[0] > (p+1)+1) fy[0] = (p+1)+1;
	  if (fy[1] < (p+1)) fy[1] = (p+1);
	  
	  //	  cpgsls(1); cpgline(2,fx,fy); cpgsls(1);
	}
      cpgsci(1);
    }

  
  cpgend();
}
Пример #6
0
int main()

{
  char text[80];
  int  naxisj, nFail = 0, status;
  double cdeltX, crpixj, crvalX, restfrq, restwav, x1, x2;


  printf(
    "Testing closure of WCSLIB spectral transformation routines (tspc.c)\n"
    "-------------------------------------------------------------------\n");

  /* List status return messages. */
  printf("\nList of spc status return values:\n");
  for (status = 1; status <= 4; status++) {
    printf("%4d: %s.\n", status, spc_errmsg[status]);
  }


  /* PGPLOT initialization. */
  strcpy(text, "/xwindow");
  cpgbeg(0, text, 1, 1);

  naxisj = NSPEC;
  crpixj = naxisj/2 + 1;

  restfrq = 1420.40595e6;
  restwav = C/restfrq;
  x1 = 1.0e9;
  x2 = 2.0e9;
  cdeltX = (x2 - x1)/(naxisj - 1);
  crvalX = x1 + (crpixj - 1.0)*cdeltX;
  printf("\nLinear frequency axis, span: %.1f to %.1f (GHz), step: %.3f "
         "(kHz)\n---------------------------------------------------------"
         "-----------------\n", x1*1e-9, x2*1e-9, cdeltX*1e-3);
  nFail += closure("WAVE-F2W",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VOPT-F2W",     0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ZOPT-F2W",     0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AWAV-F2A",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VELO-F2V", restfrq,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("BETA-F2V", restfrq,     0.0, naxisj, crpixj, cdeltX, crvalX);

  restwav = 700.0e-9;
  restfrq = C/restwav;
  x1 = 300.0e-9;
  x2 = 900.0e-9;
  cdeltX = (x2 - x1)/(naxisj - 1);
  crvalX = x1 + (crpixj - 1.0)*cdeltX;
  printf("\nLinear vacuum wavelength axis, span: %.0f to %.0f (nm), "
         "step: %f (nm)\n---------------------------------------------"
         "-----------------------------\n", x1*1e9, x2*1e9, cdeltX*1e9);
  nFail += closure("FREQ-W2F",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AFRQ-W2F",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ENER-W2F",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVN-W2F",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VRAD-W2F", restfrq,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AWAV-W2A",     0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VELO-W2V",     0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("BETA-W2V",     0.0, restwav, naxisj, crpixj, cdeltX, crvalX);


  printf("\nLinear air wavelength axis, span: %.0f to %.0f (nm), "
         "step: %f (nm)\n------------------------------------------"
         "--------------------------------\n", x1*1e9, x2*1e9, cdeltX*1e9);
  nFail += closure("FREQ-A2F", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AFRQ-A2F", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ENER-A2F", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVN-A2F", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VRAD-A2F", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVE-A2W", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VOPT-A2W", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ZOPT-A2W", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VELO-A2V", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("BETA-A2V", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);


  restfrq = 1420.40595e6;
  restwav = C/restfrq;
  x1 = -0.96*C;
  x2 =  0.96*C;
  cdeltX = (x2 - x1)/(naxisj - 1);
  crvalX = x1 + (crpixj - 1.0)*cdeltX;
  printf("\nLinear velocity axis, span: %.0f to %.0f m/s, step: %.0f "
         "(m/s)\n------------------------------------------------------"
         "--------------------\n", x1, x2, cdeltX);
  nFail += closure("FREQ-V2F", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AFRQ-V2F", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ENER-V2F", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVN-V2F", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VRAD-V2F", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVE-V2W", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VOPT-V2W", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ZOPT-V2W", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AWAV-V2A", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);


  restwav = 650.0e-9;
  restfrq = C/restwav;
  x1 =  300e-9;
  x2 = 1000e-9;
  cdeltX = (x2 - x1)/(naxisj - 1);
  crvalX = x1 + (crpixj - 1.0)*cdeltX;
  printf("\nVacuum wavelength grism axis, span: %.0f to %.0f (nm), "
         "step: %f (nm)\n--------------------------------------------"
         "------------------------------\n", x1*1e9, x2*1e9, cdeltX*1e9);
  nFail += closure("FREQ-GRI", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AFRQ-GRI", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ENER-GRI", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVN-GRI", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VRAD-GRI", restfrq, 0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("WAVE-GRI", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VOPT-GRI", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("ZOPT-GRI", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("AWAV-GRI", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VELO-GRI", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("BETA-GRI", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);


  /* Reproduce Fig. 5 of Paper III. */
  naxisj = 1700;
  crpixj = 719.8;
  crvalX = 7245.2e-10;
  cdeltX = 2.956e-10;
  restwav = 8500.0e-10;
  restfrq = C/restwav;
  x1 = crvalX + (1 - crpixj)*cdeltX;
  x2 = crvalX + (naxisj - crpixj)*cdeltX;
  mars[5] = 0.0;
  mars[6] = 0.0;
  printf("\nAir wavelength grism axis, span: %.0f to %.0f (nm), "
         "step: %f (nm)\n--------------------------------------------"
         "------------------------------\n", x1*1e9, x2*1e9, cdeltX*1e9);
  nFail += closure("AWAV-GRA", 0.0,     0.0, naxisj, crpixj, cdeltX, crvalX);
  nFail += closure("VELO-GRA", 0.0, restwav, naxisj, crpixj, cdeltX, crvalX);

  cpgask(0);
  cpgend();

  if (nFail) {
    printf("\nFAIL: %d closure residuals exceed reporting tolerance.\n",
      nFail);
  } else {
    printf("\nPASS: All closure residuals are within reporting tolerance.\n");
  }

  return nFail;
}
Пример #7
0
int main()

{
  char infile[] = "pih.fits";
  char devtyp[16], idents[3][80], nlcprm[1], opt[2];
  int  c0[] = {-1, -1, -1, -1, -1, -1, -1};
  int  i, ic, gcode[2], naxis[2], nkeyrec, nreject, nwcs, relax, status;
  float  blc[2], trc[2];
  double cache[257][4], grid1[1], grid2[1], nldprm[1];
  struct wcsprm *wcs;
  nlfunc_t pgwcsl_;
#if defined HAVE_CFITSIO && defined DO_CFITSIO
  char *header;
  fitsfile *fptr;
#else
  char keyrec[81], header[28801];
  int  gotend, j, k;
  FILE *fptr;
#endif


  /* Set line buffering in case stdout is redirected to a file, otherwise
   * stdout and stderr messages will be jumbled (stderr is unbuffered). */
  setvbuf(stdout, NULL, _IOLBF, 0);

  printf("Testing WCSLIB parser for FITS image headers (tpih2.c)\n"
         "------------------------------------------------------\n\n");

  /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */
#if defined HAVE_CFITSIO && defined DO_CFITSIO
  status = 0;

  if (fits_open_file(&fptr, infile, READONLY, &status)) {
    fits_report_error(stderr, status);
    return 1;
  }

  if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) {
    fits_report_error(stderr, status);
    return 1;
  }

  fits_close_file(fptr, &status);
#else
  if ((fptr = fopen(infile, "r")) == 0x0) {
    printf("ERROR opening %s\n", infile);
    return 1;
  }

  k = 0;
  nkeyrec = 0;
  gotend = 0;
  for (j = 0; j < 10; j++) {
    for (i = 0; i < 36; i++) {
      if (fgets(keyrec, 81, fptr) == 0) {
        break;
      }

      if (strncmp(keyrec, "        ", 8) == 0) continue;
      if (strncmp(keyrec, "COMMENT ", 8) == 0) continue;
      if (strncmp(keyrec, "HISTORY ", 8) == 0) continue;

      strncpy(header+k, keyrec, 80);
      k += 80;
      nkeyrec++;

      if (strncmp(keyrec, "END     ", 8) == 0) {
        /* An END keyrecord was read, but read the rest of the block. */
        gotend = 1;
      }
    }

    if (gotend) break;
  }
  fclose(fptr);
#endif

  fprintf(stderr, "Found %d non-comment header keyrecords.\n", nkeyrec);

  relax = WCSHDR_all;
  if ((status = wcspih(header, nkeyrec, relax, 2, &nreject, &nwcs, &wcs))) {
    fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcs_errmsg[status]);
  }
#if defined HAVE_CFITSIO && defined DO_CFITSIO
  free(header);
#endif

  /* Plot setup. */
  naxis[0] = 1024;
  naxis[1] = 1024;

  blc[0] = 0.5f;
  blc[1] = 0.5f;
  trc[0] = naxis[0] + 0.5f;
  trc[1] = naxis[1] + 0.5f;

  strcpy(devtyp, "/XWINDOW");
  cpgbeg(0, devtyp, 1, 1);
  cpgvstd();

  cpgwnad(0.0f, 1.0f, 0.0f, 1.0f);
  cpgask(1);
  cpgpage();

  /* Annotation. */
  strcpy(idents[0], "Right ascension");
  strcpy(idents[1], "Declination");

  opt[0] = 'G';
  opt[1] = 'E';

  /* Compact lettering. */
  cpgsch(0.8f);

  /* Draw full grid lines. */
  cpgsci(1);
  gcode[0] = 2;
  gcode[1] = 2;
  grid1[0] = 0.0;
  grid2[0] = 0.0;

  for (i = 0; i < nwcs; i++) {
    if ((status = wcsset(wcs+i))) {
      fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]);
      continue;
    }

    /* Get WCSNAME out of the wcsprm struct. */
    strcpy(idents[2], (wcs+i)->wcsname);
    printf("\n%s\n", idents[2]);

    /* Draw the celestial grid.  The grid density is set for each world */
    /* coordinate by specifying LABDEN = 1224. */
    ic = -1;
    cpgsbox(blc, trc, idents, opt, 0, 1224, c0, gcode, 0.0, 0, grid1, 0,
      grid2, 0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)(wcs+i), nldprm, 256,
      &ic, cache, &status);

    /* Draw the frame. */
    cpgbox("BC", 0.0f, 0, "BC", 0.0f, 0);

    cpgpage();
  }

  status = wcsvfree(&nwcs, &wcs);

  return 0;
}
Пример #8
0
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;
}
Пример #9
0
void doPlot(pulsar *psr,int npsr,int overlay)
{
  int i,j,fitFlag=1,exitFlag=0,scale1=0,scale2,count,p,xautoscale=1,k,graphics=1;
  int yautoscale=1,plotpre=1;
  int time=0;
  char xstr[1000],ystr[1000];
  float x[MAX_OBSN],y[MAX_OBSN],yerr1[MAX_OBSN],yerr2[MAX_OBSN],tmax,tmin,tmaxy1,tminy1,tmaxy2,tminy2;
  float minx,maxx,miny,maxy,plotx1,plotx2,ploty1,ploty2,mean;
  float mouseX,mouseY;
  float fontSize=1.8;
  char key;
  float widthPap=0.0,aspectPap=0.618;

  /* Obtain a graphical PGPLOT window */
  if (overlay==1)
    cpgbeg(0,"?",2,1);
  else
    cpgbeg(0,"?",2,npsr);
  cpgpap(widthPap,aspectPap);
  cpgsch(fontSize);
  cpgask(0);

  do {
    for (p=0;p<npsr;p++)
      {
	scale2 = psr[p].nobs;
	for (j=0;j<2;j++)
	  {
	    if (j==0) fitFlag=1;
	    else if (j==1) fitFlag=2;

	    ld_sprintf(xstr,"MJD-%.1Lf",psr[0].param[param_pepoch].val[0]); 
	    sprintf(ystr,"Residual (\\gmsec)");

	    count=0;
	    for (i=0;i<psr[p].nobs;i++)
	      {
		if (psr[p].obsn[i].deleted == 0  &&
	    (psr[p].param[param_start].paramSet[0]!=1 || psr[p].param[param_start].fitFlag[0]!=1 ||
	      psr[p].param[param_start].val[0] < psr[p].obsn[i].bat) &&
	    (psr[p].param[param_finish].paramSet[0]!=1 || psr[p].param[param_finish].fitFlag[0]!=1 ||
	     psr[p].param[param_finish].val[0] > psr[p].obsn[i].bat))
		  {
		    if (xautoscale==1)
		      x[count] = (double)(psr[p].obsn[i].bat-psr[p].param[param_pepoch].val[0]);
		    else
		      x[count] = (double)(psr[p].obsn[i].bat-psr[0].param[param_pepoch].val[0]);
		    
		    if (fitFlag==1)  /* Get pre-fit residual */
		      y[count] = (double)psr[p].obsn[i].prefitResidual*1.0e6;
		    else if (fitFlag==2) /* Post-fit residual */
		      y[count] = (double)psr[p].obsn[i].residual*1.0e6;
		    count++;
		  }
	      }
	    /* Remove mean from the residuals and calculate error bars */
	    mean = findMean(y,psr,p,scale1,count);
	    count=0;
	    for (i=0;i<psr[p].nobs;i++)
	      {
		if (psr[p].obsn[i].deleted==0   &&
	    (psr[p].param[param_start].paramSet[0]!=1 || psr[p].param[param_start].fitFlag[0]!=1 ||
	      psr[p].param[param_start].val[0] < psr[p].obsn[i].bat) &&
	    (psr[p].param[param_finish].paramSet[0]!=1 || psr[p].param[param_finish].fitFlag[0]!=1 ||
	     psr[p].param[param_finish].val[0] > psr[p].obsn[i].bat))
		  {
		    psr[p].obsn[i].residual-=mean/1.0e6;
		    y[count]-=mean;
		    yerr1[count] = y[count]-(float)psr[p].obsn[i].toaErr;
		    yerr2[count] = y[count]+(float)psr[p].obsn[i].toaErr;
		    count++;
		  }
	      }
	    
	    /* Get scaling for graph */
	    minx = findMin(x,psr,p,scale1,count);
	    maxx = findMax(x,psr,p,scale1,count);
	    if (xautoscale==1)
	      {
		plotx1 = minx-(maxx-minx)*0.1;
		plotx2 = maxx+(maxx-minx)*0.1;
	      }
	    else
	      {
		plotx1 = tmin-(tmax-tmin)*0.1;
		plotx2 = tmax+(tmax-tmin)*0.1;
	      }
	    miny = findMin(y,psr,p,scale1,count);
	    maxy = findMax(y,psr,p,scale1,count);

	    if (yautoscale==1)
	      {
		ploty1 = miny-(maxy-miny)*0.1;
		ploty2 = maxy+(maxy-miny)*0.1;
	      }
	    else
	      {
		if (j==0)
		  {
		    ploty1 = tminy1-(tmaxy1-tminy1)*0.1;
		    ploty2 = tmaxy1+(tmaxy1-tminy1)*0.1;
		  }
		else
		  {
		    ploty1 = tminy2-(tmaxy2-tminy2)*0.1;
		    ploty2 = tmaxy2+(tmaxy2-tminy2)*0.1;
		  }
	      }

	    /* Plot the residuals */	    
	    if (plotpre==1 || j!=0)
	      {
		float xx[MAX_OBSN],yy[MAX_OBSN],yyerr1[MAX_OBSN],yyerr2[MAX_OBSN];
		int num=0,colour;
		if (overlay==0 || (overlay==1 && p==0))
		  {
		    cpgenv(plotx1,plotx2,ploty1,ploty2,0,0);
		    cpglab(xstr,ystr,psr[p].name);	    
		  }

		for (colour=0;colour<5;colour++)
		  {
		    num=0;
		    for (i=0;i<count;i++)
		      {
			if ((colour==0 && psr[p].obsn[i].freq<=500) ||
			    (colour==1 && psr[p].obsn[i].freq>500 && psr[p].obsn[i].freq<=1000) ||
			    (colour==2 && psr[p].obsn[i].freq>1000 && psr[p].obsn[i].freq<=1500) ||
			    (colour==3 && psr[p].obsn[i].freq>1500 && psr[p].obsn[i].freq<=3300) ||
			    (colour==4 && psr[p].obsn[i].freq>3300))
			  {
			    xx[num]=x[i];
			    yy[num]=y[i];
			    yyerr1[num]=yerr1[i];
			    yyerr2[num]=yerr2[i];
			    num++;
			  }
		      }
		    cpgsci(colour+1);
		    if (overlay==1)
		      cpgsci(p+1);
		    cpgpt(num,xx,yy,16);
		    cpgerry(num,xx,yyerr1,yyerr2,1);
		  }
		cpgsci(1);
	      }
	  }
      }
    printf("------------------------------\n");
    printf("`a'     set aspect ratio\n");
    printf("`f'     set font size\n");
    printf("`g'     set graphics device\n");
    printf("`q'     quit\n");
    printf("`x'     toggle autoscale x axis\n");
    printf("`y'     toggle autoscale y axis\n");
    printf("`p'     toggle prefit plotting\n");
    printf("`r'     output residuals to file\n");

    if (graphics==1)
      {
	cpgcurs(&mouseX,&mouseY,&key);
	
	/* Check key press */
	if (key=='q') exitFlag=1;
	if (key=='p') {
	  plotpre*=-1;
	  if (plotpre==-1)
	    {
	      cpgend();
	      if (overlay==1)
		cpgbeg(0,"/xs",1,1);
	      else
		cpgbeg(0,"/xs",1,npsr);
	      cpgpap(widthPap,aspectPap);
	      cpgsch(fontSize);
	      cpgask(0);	      
	    }
	  else
	    {
	      cpgend();
	      if (overlay==1)
		cpgbeg(0,"/xs",2,1);
	      else
		cpgbeg(0,"/xs",2,npsr);
	      cpgpap(widthPap,aspectPap);
	      cpgsch(fontSize);
	      cpgask(0);	      
	    }
	}
	else if (key=='a') /* Change aspect ratio */
	  {
	    printf("Please enter a new aspect ratio ");
	    scanf("%f",&aspectPap);
	    cpgend();
	    cpgbeg(0,"/xs",2,npsr);
	    cpgpap(widthPap,aspectPap);
	    cpgsch(fontSize);
	    cpgask(0);	      
	  }
	else if (key=='f') /* Change font size */
	  {
	    printf("Please enter a new font size ");
	    scanf("%f",&fontSize);
	    cpgend();
	    cpgbeg(0,"/xs",2,npsr);
	    cpgpap(widthPap,aspectPap);
	    cpgsch(fontSize);
	    cpgask(0);	      
	  }
	else if (key=='g')
	  {
	    graphics=0;
	    cpgend();
	    if (plotpre==-1)
	      {
		cpgend();
		if (overlay==1)
		  cpgbeg(0,"?",1,1);
		else
		  cpgbeg(0,"?",1,npsr);
		cpgpap(widthPap,aspectPap);
		cpgsch(fontSize);
		cpgask(0);	      
	      }
	    else
	      {
		cpgend();
		if (overlay==1)
		  cpgbeg(0,"?",1,1);
		else
		  cpgbeg(0,"?",2,npsr);
		cpgpap(widthPap,aspectPap);
		cpgsch(fontSize);
		cpgask(0);	      
	      }
	  }
	else if (key=='r') /* Output residuals to file */
	  {
	    FILE *fout;
	    char fname[1000];
	    int ii,jj;

	    for (ii=0;ii<npsr;ii++)
	      {
		sprintf(fname,"%s.res",psr[ii].name);
		fout = fopen(fname,"w");
		/* Print header */
		fprintf(fout,"#PSR %s\n",psr[ii].name);
		ld_fprintf(fout,"#F0  %.14Lf\n",psr[ii].param[param_f].val[0]);
		fprintf(fout,"#RAJ %s\n",psr[ii].rajStrPre);
		fprintf(fout,"#DECJ %s\n",psr[ii].decjStrPre);
		for (jj=0;jj<psr[ii].nobs;jj++)
		  fprintf(fout,"%.5lf %.5lg %.5lg\n",
			  (double)(psr[ii].obsn[jj].bat-psr[0].param[param_pepoch].val[0]),
			  (double)(psr[ii].obsn[jj].residual),(double)(psr[ii].obsn[jj].toaErr)/1.0e6);
		fclose(fout);
	      }
	  }
	else if (key=='x') 
	  {
	    xautoscale*=-1;
	    if (xautoscale==-1)
	      {
		for (k=0;k<npsr;k++)
		  {
		    count=0;
		    for (i=0;i<psr[k].nobs;i++)
		      {
			if (psr[k].obsn[i].deleted==0   &&
			    (psr[k].param[param_start].paramSet[0]!=1 || psr[k].param[param_start].fitFlag[0]!=1 ||
			     psr[k].param[param_start].val[0] < psr[k].obsn[i].bat) &&
			    (psr[k].param[param_finish].paramSet[0]!=1 || psr[k].param[param_finish].fitFlag[0]!=1 ||
			     psr[k].param[param_finish].val[0] > psr[k].obsn[i].bat))

			  {x[count] = (double)(psr[k].obsn[i].bat-psr[0].param[param_pepoch].val[0]); count++;}
		      }
		    minx = findMin(x,psr,k,scale1,count);
		    maxx = findMax(x,psr,k,scale1,count);
		    if (k==0)
		      {
			tmin = minx;
			tmax = maxx;
			printf("Have1 tmin = %f, tmax = %f\n",tmin,tmax);
		      }
		    else
		      {
			if (tmin > minx) tmin = minx;
			if (tmax < maxx) tmax = maxx;
			printf("Have2 tmin = %f, tmax = %f\n",tmin,tmax);

		      }
		  }
	      }
	  }       
	else if (key=='y') 
	  {
	    yautoscale*=-1;
	    if (yautoscale==-1)
	      {
		for (k=0;k<npsr;k++)
		  {
		    count=0;
		    for (i=0;i<psr[k].nobs;i++)
		      {
			if (psr[k].obsn[i].deleted==0   &&
			    (psr[k].param[param_start].paramSet[0]!=1 || psr[k].param[param_start].fitFlag[0]!=1 ||
			     psr[k].param[param_start].val[0] < psr[k].obsn[i].bat) &&
			    (psr[k].param[param_finish].paramSet[0]!=1 || psr[k].param[param_finish].fitFlag[0]!=1 ||
			     psr[k].param[param_finish].val[0] > psr[k].obsn[i].bat))			  
			  {y[count] = (double)psr[k].obsn[i].prefitResidual*1e6; count++;}
		      }
		    miny = findMin(y,psr,k,scale1,count);
		    maxy = findMax(y,psr,k,scale1,count);
		    if (k==0)
		      {
			tminy1 = miny;
			tmaxy1 = maxy;
		      }
		    else
		      {
			if (tminy1 > miny) tminy1 = miny;
			if (tmaxy1 < maxy) tmaxy1 = maxy;
		      }

		    count=0;
		    for (i=0;i<psr[k].nobs;i++)
		      {
			if (psr[k].obsn[i].deleted==0   &&
			    (psr[k].param[param_start].paramSet[0]!=1 || psr[k].param[param_start].fitFlag[0]!=1 ||
			     psr[k].param[param_start].val[0] < psr[k].obsn[i].bat) &&
			    (psr[k].param[param_finish].paramSet[0]!=1 || psr[k].param[param_finish].fitFlag[0]!=1 ||
			     psr[k].param[param_finish].val[0] > psr[k].obsn[i].bat))
			  {y[count] = (double)psr[k].obsn[i].residual*1e6; count++;}
		      }
		    miny = findMin(y,psr,k,scale1,count);
		    maxy = findMax(y,psr,k,scale1,count);
		    if (k==0)
		      {
			tminy2 = miny;
			tmaxy2 = maxy;
		      }
		    else
		      {
			if (tminy2 > miny) tminy2 = miny;
			if (tmaxy2 < maxy) tmaxy2 = maxy;
		      }
		  }
		printf("Have tminy2 = %g %g\n",tminy2,tmaxy2);
	      }
	  }       
	else printf("Unknown key press %c\n",key);
      }
    else
      {
	graphics=1;

	cpgend();
	if (plotpre==-1)
	  {
	    cpgend();
	    if (overlay==1)
	      cpgbeg(0,"/xs",1,1);
	    else
	      cpgbeg(0,"/xs",1,npsr);
	    cpgpap(widthPap,aspectPap);
	    cpgsch(fontSize);
	    cpgask(0);	      
	  }
	else
	  {
	    cpgend();
	    if (overlay==1)
	      cpgbeg(0,"/xs",2,1);
	    else
	      cpgbeg(0,"/xs",2,npsr);
	    cpgpap(widthPap,aspectPap);
	    cpgsch(fontSize);
	    cpgask(0);	      
	  }
      }
  } while (exitFlag==0);
  
  cpgend();
}
Пример #10
0
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;
}
Пример #11
0
int main()

{
  void prjplt();
  int  status;
  char text[80], text1[80], text2[80];
  struct prjprm prj;


  printf("Testing WCSLIB spherical projection routines (tprj2.c)\n"
         "------------------------------------------------------\n");

  /* List status return messages. */
  printf("\nList of prj status return values:\n");
  for (status = 1; status <= 4; status++) {
    printf("%4d: %s.\n", status, prj_errmsg[status]);
  }

  printf("\n");


  /* PGPLOT initialization. */
  strcpy(text, "/xwindow");
  cpgbeg(0, text, 1, 1);

  /* Define pen colours. */
  cpgscr(0, 0.00f, 0.00f, 0.00f);
  cpgscr(1, 1.00f, 1.00f, 0.00f);
  cpgscr(2, 1.00f, 1.00f, 1.00f);
  cpgscr(3, 0.50f, 0.50f, 0.80f);
  cpgscr(4, 0.80f, 0.50f, 0.50f);
  cpgscr(5, 0.80f, 0.80f, 0.80f);
  cpgscr(6, 0.50f, 0.50f, 0.80f);
  cpgscr(7, 0.80f, 0.50f, 0.50f);
  cpgscr(8, 0.30f, 0.50f, 0.30f);

  strcpy(text1, "\n%s projection\n");
  strcpy(text2, "\n%s projection\nParameters:");

  prjini(&prj);

  /* AZP: zenithal/azimuthal perspective. */
  prj.pv[1] =   2.0;
  prj.pv[2] =  30.0;
  printf(text2, "Zenithal/azimuthal perspective");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("AZP", 90, -90, &prj);

  /* SZP: slant zenithal perspective. */
  prj.pv[1] =   2.0;
  prj.pv[2] = 210.0;
  prj.pv[3] =  60.0;
  printf(text2, "Slant zenithal perspective");
  printf("%12.5f%12.5f%12.5f\n", prj.pv[1], prj.pv[2], prj.pv[3]);
  prjplt("SZP", 90, -90, &prj);

  /* TAN: gnomonic. */
  printf(text1, "Gnomonic");
  prjplt("TAN", 90,   5, &prj);

  /* STG: stereographic. */
  printf(text1, "Stereographic");
  prjplt("STG", 90, -85, &prj);

  /* SIN: orthographic. */
  prj.pv[1] = -0.3;
  prj.pv[2] =  0.5;
  printf(text2, "Orthographic/synthesis");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("SIN", 90, -90, &prj);

  /* ARC: zenithal/azimuthal equidistant. */
  printf(text1, "Zenithal/azimuthal equidistant");
  prjplt("ARC", 90, -90, &prj);

  /* ZPN: zenithal/azimuthal polynomial. */
  prj.pv[0] =  0.05000;
  prj.pv[1] =  0.95000;
  prj.pv[2] = -0.02500;
  prj.pv[3] = -0.15833;
  prj.pv[4] =  0.00208;
  prj.pv[5] =  0.00792;
  prj.pv[6] = -0.00007;
  prj.pv[7] = -0.00019;
  prj.pv[8] =  0.00000;
  prj.pv[9] =  0.00000;
  printf(text2, "Zenithal/azimuthal polynomial");
  printf("%12.5f%12.5f%12.5f%12.5f%12.5f\n",
    prj.pv[0], prj.pv[1], prj.pv[2], prj.pv[3], prj.pv[4]);
  printf("           %12.5f%12.5f%12.5f%12.5f%12.5f\n",
    prj.pv[5], prj.pv[6], prj.pv[7], prj.pv[8], prj.pv[9]);
  prjplt("ZPN", 90,  10, &prj);

  /* ZEA: zenithal/azimuthal equal area. */
  printf(text1, "Zenithal/azimuthal equal area");
  prjplt("ZEA", 90, -90, &prj);

  /* AIR: Airy's zenithal projection. */
  prj.pv[1] = 45.0;
  printf(text2, "Airy's zenithal");
  printf("%12.5f\n", prj.pv[1]);
  prjplt("AIR", 90, -85, &prj);

  /* CYP: cylindrical perspective. */
  prj.pv[1] = 3.0;
  prj.pv[2] = 0.8;
  printf(text2, "Cylindrical perspective");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("CYP", 90, -90, &prj);

  /* CEA: cylindrical equal area. */
  prj.pv[1] = 0.75;
  printf(text2, "Cylindrical equal area");
  printf("%12.5f\n", prj.pv[1]);
  prjplt("CEA", 90, -90, &prj);

  /* CAR: plate carree. */
  printf(text1, "Plate carree");
  prjplt("CAR", 90, -90, &prj);

  /* MER: Mercator's. */
  printf(text1, "Mercator's");
  prjplt("MER", 85, -85, &prj);

  /* SFL: Sanson-Flamsteed. */
  printf(text1, "Sanson-Flamsteed (global sinusoid)");
  prjplt("SFL", 90, -90, &prj);

  /* PAR: parabolic. */
  printf(text1, "Parabolic");
  prjplt("PAR", 90, -90, &prj);

  /* MOL: Mollweide's projection. */
  printf(text1, "Mollweide's");
  prjplt("MOL", 90, -90, &prj);

  /* AIT: Hammer-Aitoff. */
  printf(text1, "Hammer-Aitoff");
  prjplt("AIT", 90, -90, &prj);

  /* COP: conic perspective. */
  prj.pv[1] =  60.0;
  prj.pv[2] =  15.0;
  printf(text2, "Conic perspective");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("COP", 90, -25, &prj);

  /* COE: conic equal area. */
  prj.pv[1] =  60.0;
  prj.pv[2] = -15.0;
  printf(text2, "Conic equal area");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("COE", 90, -90, &prj);

  /* COD: conic equidistant. */
  prj.pv[1] = -60.0;
  prj.pv[2] =  15.0;
  printf(text2, "Conic equidistant");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("COD", 90, -90, &prj);

  /* COO: conic orthomorphic. */
  prj.pv[1] = -60.0;
  prj.pv[2] = -15.0;
  printf(text2, "Conic orthomorphic");
  printf("%12.5f%12.5f\n", prj.pv[1], prj.pv[2]);
  prjplt("COO", 85, -90, &prj);

  /* BON: Bonne's projection. */
  prj.pv[1] = 30.0;
  printf(text2, "Bonne's");
  printf("%12.5f\n", prj.pv[1]);
  prjplt("BON", 90, -90, &prj);

  /* PCO: polyconic. */
  printf(text1, "Polyconic");
  prjplt("PCO", 90, -90, &prj);

  /* TSC: tangential spherical cube. */
  printf(text1, "Tangential spherical cube");
  prjplt("TSC", 90, -90, &prj);

  /* CSC: COBE quadrilateralized spherical cube. */
  printf(text1, "COBE quadrilateralized spherical cube");
  prjplt("CSC", 90, -90, &prj);

  /* QSC: quadrilateralized spherical cube. */
  printf(text1, "Quadrilateralized spherical cube");
  prjplt("QSC", 90, -90, &prj);

  /* HPX: HEALPix projection. */
  prj.pv[1] = 4.0;
  prj.pv[2] = 3.0;
  printf(text1, "HEALPix");
  prjplt("HPX", 90, -90, &prj);

  /* XPH: HEALPix polar, aka "butterfly" projection. */
  printf(text1, "Butterfly");
  prjplt("XPH", 90, -90, &prj);

  cpgask(0);
  cpgend();

  return 0;
}
Пример #12
0
static void _pgask (int *i)
{
   cpgask (*i);
}