コード例 #1
0
ファイル: cpgdemo.c プロジェクト: Milkyway-at-home/nemo
static void demo1()
{
  int i;
  static float xs[] = {1.0, 2.0, 3.0, 4.0, 5.0 };
  static float ys[] = {1.0, 4.0, 9.0, 16.0, 25.0 };
  float xr[60], yr[60];
  int n = sizeof(xr) / sizeof(xr[0]);
  /*
   * Call cpgenv to specify the range of the axes and to draw a box, and
   * cpglab to label it. The x-axis runs from 0 to 10, and y from 0 to 20.
   */
  cpgenv(0.0, 10.0, 0.0, 20.0, 0, 1);
  cpglab("(x)", "(y)", "PGPLOT Example 1: y = x\\u2\\d");
  /*
   * Mark five points (coordinates in arrays XS and YS), using symbol
   * number 9.
   */
  cpgpt(5, xs, ys, 9);
  /*
   * Compute the function at 'n=60' points, and use cpgline to draw it.
   */
  for(i=0; i<n; i++) {
    xr[i] = 0.1*i;
    yr[i] = xr[i]*xr[i];
  }
  cpgline(n, xr, yr);
  return;
}
コード例 #2
0
ファイル: pgplot-module.c プロジェクト: hankem/ISIS
/* draw several graph markers */
static void _pgpt (int *symbol)
{
   SLang_Array_Type *x, *y;

   if (-1 == pop_two_float_vectors (&x, &y))
     return;

   cpgpt ((int) x->num_elements, (float *)x->data, (float *)y->data, *symbol);

   free_arrays (x, y, NULL, NULL);
}
コード例 #3
0
ファイル: xyline.c プロジェクト: MilesCranmer/presto
void plot_profile(int proflen, float *profile, const char *title,
                  const char *probtxt, const char *foldtxt,
                  int showerr, float *errors, int showid)
{
    int ii;
    float *x, overy, ymin, ymax;
    float errmin = 0.0, errmax = 0.0, offset, avg = 0.0, av[2];

    find_min_max_arr(proflen, profile, &ymin, &ymax);
    if (showerr)
        find_min_max_arr(proflen, errors, &errmin, &errmax);
    overy = 0.1 * (ymax + errmax - ymin - errmin);
    ymax = ymax + overy + errmax;
    ymin = ymin - overy - errmin;
    x = gen_fvect(proflen);
    for (ii = 0; ii < proflen; ii++)
        x[ii] = (float) ii / (float) proflen;
    cpgenv(0.0, 1.00001, ymin, ymax, 0, 0);
    cpgscf(2);
    cpglab("Pulse Phase", "Counts", "");
    if (showid)
        cpgiden();
    cpgslw(5);
    if (showerr) {
        cpgbin(proflen, x, profile, 0);
    } else {
        cpgline(proflen, x, profile);
    }
    cpgslw(1);
    if (showerr) {
        offset = 0.5 / (float) proflen;
        for (ii = 0; ii < proflen; ii++)
            x[ii] += offset;
        cpgerrb(6, proflen, x, profile, errors, 2);
        cpgpt(proflen, x, profile, 5);
    }
    for (ii = 0; ii < proflen; ii++)
        avg += profile[ii];
    avg /= proflen;
    cpgsls(4);
    x[0] = 0.0;
    x[1] = 1.0;
    av[0] = avg;
    av[1] = avg;
    cpgline(2, x, av);
    cpgsls(1);
    cpgsch(1.3);
    cpgmtxt("T", +2.0, 0.5, 0.5, title);
    cpgsch(1.0);
    cpgmtxt("T", +0.8, 0.5, 0.5, foldtxt);
    cpgmtxt("T", -1.5, 0.5, 0.5, probtxt);
    vect_free(x);
}
コード例 #4
0
ファイル: mandelbrot.c プロジェクト: LStoney/Fractals
void main()
{
   
   float RES = (XMAX - XMIN)/N;                          //resolution
   
   int i,j,p;
   
    
   
   //************************* PGPLOT CODE ***************************
  
  cpgbeg(0,"?",1,1);
  cpgpage();
  
  cpgsci(1);                                           // axis color
  
  cpgpap(0,1);
  
                                                      //axis limits
  cpgswin(XMIN,XMAX,YMIN,YMAX);
  
  cpgbox("BCN",1, 0, "BCN", 1, 0);                  // draw the axes
  
  cpgsci(1);                                          //data color

  cpgsch(0.00000000000001);                        //data point size
  
  
  
  //******************* GRID ALGORITHM AND PLOTTING ********************
  
  
  struct cnum z;                          // z = (0,0) = initial number 
  struct cnum c;                          // c is a complex variable
  z.cx = 0;
  z.cy = 0;
   
   
  for(i=0;i<N;i++)                     //look at every point on grid
  {
    for(j=0;j<N;j++)                       
    {
      
       c.cx = XMIN + i*RES;            //assign c = current point
       c.cy = YMIN + j*RES;            
       
       CPRINT(c);
       
       for(p=0;p<MNI;p++)                  //apply MNI iterations to z
       {                                   //using c = current point
	 z = FMANDEL(z,c);
	 
	 if ( z.cx*z.cx + z.cy*z.cy > R)    // if iteration "blows up"... 
	 {
	   z.cx = 0;
	   z.cy = 0;                       //stay at z=c=0
	   c.cx = 0;
	   c.cy = 0;	   
	 }
	   
       }                                  //end of interation. z = final number
           
           
       if (z.cx*z.cx + z.cy*z.cy < R)      //if iteration hasn't blown up...
       {
	 float X[1], Y[1];
	 X[0] = c.cx;
	 Y[0] = c.cy;
	 cpgpt(1,X,Y,17);                  // plot point c
       }
       
       
       
    }
    
   }
   
  printf("\n\n");                             
 
  cpgend();                       
  
  
 
}
コード例 #5
0
ファイル: sample.c プロジェクト: mitacDUO/cmp_project2
// Main code
int main()
{
  /**** Count to 10 in integers ****/

  // Declare integer for loop counting
  int i;

  // Loop from 0 to 10, printing at each step
  for(i=0; i<10; i++)
  {
    printf("i= %d\n", i);
  }

  /**** Plot a function y=f(x) ****/

  // Declare arrays of N real numbers
  float ax[N]; // x
  float ay[N]; // y

  float aylow[N];  // lower error in y
  float ayhigh[N]; // upper error in y

  // Set minimum and maximum for x
  float xmin = 0.0;
  float xmax = 10.0;

  // Assigning ax with N values for x between xmin and xmax
  for(i=0;i<N;i++)
  {
    ax[i] = xmin + (xmax-xmin)*(float)i/(float)(N-1);
  }

  // Fill ay using function fy
  for(i=0;i<N;i++)
  {
    ay[i] = fy(ax[i]);
  }

  // Fill aylow and ayhigh using sqrt(y) as the error
  for(i=0;i<N;i++)
  {
    aylow[i]  = ay[i] - sqrt(ay[i]);
    ayhigh[i] = ay[i] + sqrt(ay[i]);
  }

  /**** Use pgplot to plot this function ****/

  // cpgbeg starts a plotting page, in this case with 2x1 panels
  cpgbeg(0,"?",2,1);

  // sets colour: 1-black, 2-red, 3-green, 4-blue
  cpgsci(1);

  // sets line style: 1-solid, 2-dashed, 3-dot-dashed, 4-dotted
  cpgsls(1);

  // sets charachter height, larger number = bigger
  cpgsch(2.);

  // cpgpage() moves to the next panel
  cpgpage();

  // sets the axes limits in the panel
  cpgswin(xmin,xmax,0.,100.);

  // draw the axes
  cpgbox("BCNST", 0.0, 0, "BCNST", 0.0, 0);

  // label the bottom axis
  cpgmtxt("B",2.,.5,.5,"x");
  // label the left axis
  cpgmtxt("L",2.5,.5,.5,"f");

  // connect N points in ax and ay with a line
  cpgline(N,ax,ay);

  // cpgpage() moves to the next panel
  cpgpage();

  // sets the axes limits in the panel
  cpgswin(xmin,xmax,0.,100.);

  // draw the axes
  cpgbox("BCNST", 0.0, 0, "BCNST", 0.0, 0);

  // label the bottom axis
  cpgmtxt("B",2.,.5,.5,"x");
  // label the left axis
  cpgmtxt("L",2.5,.5,.5,"f");

  // draw N points in ax and ay
  //   17 - filled circles, 16 - filled squares, 13 - filled triangles
  cpgpt(N,ax,ay,17);

  // draw y error bars on the points
  cpgerry(N,ax,aylow,ayhigh,1.0);

  // close all pgplot applications
  cpgend();

  // end program
  return 0;
}
コード例 #6
0
ファイル: find_dither.c プロジェクト: MCTwo/CodeCDF
int plot_shifts(Secat *shiftcat, int nshift)
{
  int i;                     /* Looping variable */
  int no_error=1;            /* Flag set to 0 on error */
  float x1,x2,y1,y2;         /* Limits on plot */
  float *fdx=NULL;           /* float version of x offsets */
  float *fdy=NULL;           /* float version of y offsets */
  float *fxptr,*fyptr;       /* Navigation pointers */
  double *dx=NULL;           /* x offsets */
  double *dy=NULL;           /* y offsets */
  double *xptr,*yptr;        /* Navigation pointers */
  double xmean, xsig, xmed;  /* Statistics on dx */
  double ymean, ysig, ymed;  /* Statistics on dx */
  Secat *sptr;               /* Pointer to navigate shiftcat */
  FILE *ofp=NULL;            /* Output file pointer */

  /*
   * Allocate memory for dx and dy arrays
   */

  if(!(dx = new_doubarray(nshift))) {
    fprintf(stderr,"ERROR: calc_shift_stats\n");
    return 1;
  }
  if(!(dy = new_doubarray(nshift)))
    no_error = 0;
  if(!(fdx = new_array(nshift,1)))
    no_error = 0;
  if(!(fdy = new_array(nshift,1)))
    no_error = 0;

  if(no_error) {

    /*
     * Transfer info to new arrays
     */

    for(i=0,sptr=shiftcat,xptr=dx,yptr=dy,fxptr=fdx,fyptr=fdy; 
	i<nshift; i++,sptr++,xptr++,yptr++,fxptr++,fyptr++) {
      *xptr = sptr->dx;
      *yptr = sptr->dy;
      *fxptr = (float) sptr->dx;
      *fyptr = (float) sptr->dy;
    }

    /*
     * Calculate statistics on dx and dy
     */

    doubstats(dx,nshift,&xmean,&xsig,&xmed);
    doubstats(dy,nshift,&ymean,&ysig,&ymed);

    /*
     * Give output values
     */

    printf("\nStatistics on x shift:\n");
    printf("  mean = %f\n",xmean);
    printf("  rms = %f\n",xsig);
    printf("  median = %f\n",xmed);
    printf("Statistics on y shift:\n");
    printf("  mean = %f\n",ymean);
    printf("  rms = %f\n",ysig);
    printf("  median = %f\n",ymed);

    /*
     * Set the limits and median
     */


    x1 = xmed - 5.0 * xsig;
    x2 = xmed + 5.0 * xsig;
    y1 = ymed - 5.0 * ysig;
    y2 = ymed + 5.0 * ysig;

    /*
     * Plot distribution
     */

    cpgslct(2);
    cpgenv(x1,x2,y1,y2,0,1);
    cpglab("x shift","y shift","Calculated Shifts");
    cpgpt(nshift,fdx,fdy,9);

    /*
     * Plot median
     */

    cpgsci(2);
    cpgslw(5);
    fdx[0] = fdx[1] = xmed;
    fdy[0] = y1;
    fdy[1] = y2;
    cpgline(2,fdx,fdy);
    fdy[0] = fdy[1] = ymed;
    fdx[0] = x1;
    fdx[1] = x2;
    cpgline(2,fdx,fdy);
    cpgsci(1);
    cpgslw(1);
  }

  /*
   * Write median shifts to output file -- NB: for these to be the
   *  proper shifts for an iraf imcombine offsets file, the value
   *  need to be the negative of what the above calculation gives.
   */

  if(!(ofp = open_writefile("tmp.offsets")))
    no_error = 0;
  else
    fprintf(ofp,"%8.2f %8.2f\n",-xmed,-ymed);

  /*
   * Clean up and exit
   */

  dx = del_doubarray(dx);
  dy = del_doubarray(dy);
  fdx = del_array(fdx);
  fdy = del_array(fdy);
  if(ofp)
    fclose(ofp);
  if(no_error)
    return 0;
  else {
    fprintf(stderr,"ERROR: calc_shift_stats\n");
    return 1;
  }
}
コード例 #7
0
ファイル: main.c プロジェクト: marcelobianchi/evsel
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;
}
コード例 #8
0
ファイル: Plotter2.cpp プロジェクト: schiebel/casa
void Plotter2::plot() {
    open();

    if ((width > 0.0) && (aspect > 0.0)) {
        cpgpap(width, aspect);
    }

    cpgscr(0, 1.0, 1.0, 1.0); // set background color white
    cpgscr(1, 0.0, 0.0, 0.0); // set foreground color black

    for (unsigned int i = 0; i < vInfo.size(); ++i) {
        Plotter2ViewportInfo vi = vInfo[i];

	if (vi.showViewport) {
	    resetAttributes(vi);

	    // setup viewport
            cpgsvp(vi.vpPosXMin, vi.vpPosXMax, vi.vpPosYMin, vi.vpPosYMax);
	    cpgswin(vi.vpRangeXMin, vi.vpRangeXMax, vi.vpRangeYMin, vi.vpRangeYMax);

	    // background color (default is transparent)
	    if (vi.vpBColor >= 0) {
	        cpgsci(vi.vpBColor);
	        cpgrect(vi.vpRangeXMin, vi.vpRangeXMax, vi.vpRangeYMin, vi.vpRangeYMax);
	        cpgsci(1);  // reset foreground colour to the initial one (black)
	    }

	    // data
	    for (unsigned int j = 0; j < vi.vData.size(); ++j) {
	        resetAttributes(vi);

	        Plotter2DataInfo di = vi.vData[j];
	        std::vector<float> vxdata = di.xData;
                int ndata = vxdata.size();
	        float* pxdata = new float[ndata];
	        float* pydata = new float[ndata];
	        for (int k = 0; k < ndata; ++k) {
	            pxdata[k] = di.xData[k];
	            pydata[k] = di.yData[k];
	        }

	        if (di.drawLine) {
  	            cpgsls(di.lineStyle);
	            cpgslw(di.lineWidth);
		    int colorIdx = di.lineColor;
		    if (colorIdx < 0) {
		        colorIdx = (j + 1) % 15 + 1;
		    }
	            cpgsci(colorIdx);
	            cpgline(ndata, pxdata, pydata);
	        }

	        if (di.drawMarker) {
	            cpgsch(di.markerSize);
	            cpgsci(di.markerColor);
	            cpgpt(ndata, pxdata, pydata, di.markerType);
	        }

	        delete [] pxdata;
	        delete [] pydata;
	    }

	    //calculate y-range of xmasks
	    std::vector<float> yrange = vi.getRangeY();
	    float yexcess = 0.1*(yrange[1] - yrange[0]);
	    float xmaskymin = yrange[0] - yexcess;
	    float xmaskymax = yrange[1] + yexcess;

	    // masks
	    for (unsigned int j = 0; j < vi.vRect.size(); ++j) {
	        resetAttributes(vi);

	        Plotter2RectInfo ri = vi.vRect[j];
                cpgsci(ri.color);
	        cpgsfs(ri.fill);
	        cpgslw(ri.width);
	        cpgshs(45.0, ri.hsep, 0.0);
	        float* mxdata = new float[4];
	        float* mydata = new float[4];
	        mxdata[0] = ri.xmin;
	        mxdata[1] = ri.xmax;
	        mxdata[2] = ri.xmax;
	        mxdata[3] = ri.xmin;
	        mydata[0] = xmaskymin;
	        mydata[1] = xmaskymin;
	        mydata[2] = xmaskymax;
	        mydata[3] = xmaskymax;
                cpgpoly(4, mxdata, mydata);
	    }

	    // arrows
	    for (unsigned int j = 0; j < vi.vArro.size(); ++j) {
  	        resetAttributes(vi);

		Plotter2ArrowInfo ai = vi.vArro[j];
		cpgsci(ai.color);
		cpgslw(ai.width);
                cpgsls(ai.lineStyle);
		cpgsch(ai.headSize);
		cpgsah(ai.headFillStyle, ai.headAngle, ai.headVent);
		cpgarro(ai.xtail, ai.ytail, ai.xhead, ai.yhead);
	    }

	    // arbitrary texts
	    for (unsigned int j = 0; j < vi.vText.size(); ++j) {
  	        resetAttributes(vi);

		Plotter2TextInfo ti = vi.vText[j];
		cpgsch(ti.size);
		cpgsci(ti.color);
		cpgstbg(ti.bgcolor);
		cpgptxt(ti.posx, ti.posy, ti.angle, ti.fjust, ti.text.c_str());
	    }

	    // viewport outline and ticks
	    resetAttributes(vi);

            cpgbox("BCTS",  vi.majorTickIntervalX, vi.nMinorTickWithinMajorTicksX, 
	           "BCTSV", vi.majorTickIntervalY, vi.nMinorTickWithinMajorTicksY);

	    // viewport numberings
	    std::string numformatx, numformaty;
	    if (vi.numLocationX == "b") {
	        numformatx = "N";
	    } else if (vi.numLocationX == "t") {
	        numformatx = "M";
	    } else if (vi.numLocationX == "") {
	        numformatx = "";
	    }
	    if (vi.numLocationY == "l") {
	        numformaty = "NV";
	    } else if (vi.numLocationY == "r") {
	        numformaty = "MV";
	    } else if (vi.numLocationY == "") {
	        numformaty = "";
	    }

            cpgbox(numformatx.c_str(), vi.majorTickIntervalX * vi.nMajorTickWithinTickNumsX, 0, 
	           numformaty.c_str(), vi.majorTickIntervalY * vi.nMajorTickWithinTickNumsY, 0);

	    float xpos, ypos;

	    // x-label
	    vi.getWorldCoordByWindowCoord(vi.labelXPosX, vi.labelXPosY, &xpos, &ypos);
	    cpgsch(vi.labelXSize);
            cpgsci(vi.labelXColor);
            cpgstbg(vi.labelXBColor); //outside viewports, works ONLY with /xwindow
            cpgptxt(xpos, ypos, vi.labelXAngle, vi.labelXFJust, vi.labelXString.c_str());

	    // y-label
	    vi.getWorldCoordByWindowCoord(vi.labelYPosX, vi.labelYPosY, &xpos, &ypos);
	    cpgsch(vi.labelYSize);
            cpgsci(vi.labelYColor);
            cpgstbg(vi.labelYBColor); //outside viewports, works ONLY with /xwindow
            cpgptxt(xpos, ypos, vi.labelYAngle, vi.labelYFJust, vi.labelYString.c_str());

	    // title
	    vi.getWorldCoordByWindowCoord(vi.titlePosX, vi.titlePosY, &xpos, &ypos);
	    cpgsch(vi.titleSize);
            cpgsci(vi.titleColor);
            cpgstbg(vi.titleBColor); //outside viewports, works ONLY with /xwindow
            cpgptxt(xpos, ypos, vi.titleAngle, vi.titleFJust, vi.titleString.c_str());
	}

    }

    close();
}
コード例 #9
0
void nrpoint(float x[],float y[],float azy[],float ely[],float azmod[],float elmod[],float sig[],int ndata,int num_gauss,int flag,int ant_num,int plotflag,char
*header)
{
	FILE *fp1,*fp2,*fp3,*fp4,*fp5;
	float rms(float *,int);
	float arg, guessed_parameters,xmin,xmax,ymin,ymax,tmp,rms_fac;
	float alamda,chisq,ochisq,**covar,**alpha,*a;
	int i,*ia,itst,j,k,l,numplot,i_maxy,i_miny,MA, NPT;
	char ans[200],f_line[200],c;
	char file_n1[160],file_n2[160],file_n3[160],file_n4[160],file_n5[160];
	char xtitle[60],ytitle[60],title[60],plotant[10];
	FILE *fpsummary, *headerfp;
	char fullfilename[250];
	char buffer[2048]; /* must be larger than length of header */
	char *token[MAX_TOKENS];
	int tokens;
	char rxlabelhigh[30];
	char rxlabellow[30];

	float xx[1600],yy[1600],yyy[1600],res[1600];

/* 	following for aperture efficiency 16 Nov 04, TK */
        char etaCommand[130], rawfilename[256];
        FILE *fpi_eta,*fpo_eta, *fph_eta;
        int  use_beam, time_stamp; 
        float tau_zenith,Tcmbr,Tatm,Thot,Tamb,Tcab,eta_l,delVsource,Vhot,Vsky,err,el,SB;
        float Frequency, TBright, VhotL, VhotH, VskyL, VskyH;
        float PlanetDia, WidthFwhm,fwhm_beam, EtaA, EtaB;
        char  object[20], date[30];
/* 	aperture efficiecny additions end */


	sprintf(file_n1,"/usr/PowerPC/common/data/rpoint/ant%d/load.fitted.dat",ant_num);
	sprintf(file_n2,"/usr/PowerPC/common/data/rpoint/ant%d/load.initial.dat",ant_num);
	sprintf(file_n3,"/usr/PowerPC/common/data/rpoint/ant%d/load.temp.dat",ant_num);
	sprintf(file_n4,"/usr/PowerPC/common/data/rpoint/ant%d/load.results.dat",ant_num);
	sprintf(file_n5,"/usr/PowerPC/common/data/rpoint/ant%d/rpoint.ant%1d",ant_num,ant_num);

	if ((fp1=fopen(file_n1,"w"))==NULL){
	  printf("nrpoint: cannot open n1 = %s\n",file_n1);
	  exit(1);
	}
	chmod(file_n1,0666);
	if ((fp3=fopen(file_n3,"w"))==NULL){
	  printf("nrpoint: cannot open n2 (first time) = %s\n",file_n3);
	  exit(1);
	}
	chmod(file_n3,0666);
	if ((fp4=fopen(file_n4,"a"))==NULL){
	  printf("nrpoint: cannot open n4 = %s\n",file_n4);
	  exit(1);
	}
	chmod(file_n4,0666);
	if ((fp5=fopen(file_n5,"a"))==NULL){
	  printf("nrpoint: cannot open n5 = %s\n",file_n5);
	  exit(1);
	}
	chmod(file_n5,0666);

	NPT=ndata;MA=num_gauss;
/*
	printf("number of data = %d number of fitting components = %d flag = %d\n", NPT,MA/5,flag);

*/
	ia=ivector(1,MA);
	a=vector(1,MA);
	covar=matrix(1,MA,1,MA);
	alpha=matrix(1,MA,1,MA);
	
/* read data */
	xmin=1e6;ymin=1e6;
	xmax=-1e6;ymax=-1e6;
	for (i=1;i<=NPT;i++) {
	  xx[i-1]=x[i];
	  yy[i-1]=y[i];
	  if(xmin>=x[i]) xmin=x[i];
	  if(xmax<x[i]) xmax=x[i];
	  if(ymin>=y[i]){ymin=y[i];i_miny=i;}
	  if(ymax<y[i]) {ymax=y[i];i_maxy=i;}
	  /*
	    if(i<10)	printf("%d %f %f %f %f %f %f\n",i,x[i],y[i],ymin,ymax,azy[i],ely[i],sig[i]);
	  */
	  fprintf(fp3,"%f %f\n",x[i],y[i]);
	}

	tmp=ymax-ymin;
	ymax=tmp*0.2+ymax;
	ymin=ymin-tmp*0.2;
	fclose(fp3);

/*    PGPLOT */
	sprintf(plotant,"%d/xs",(ant_num+10));
	if(plotflag){
	  if(cpgbeg(0,plotant,1,1)!=1) exit(1);
	  cpgenv(xmin,xmax,ymin,ymax,0,0); 
	  cpgpt(NPT,xx,yy,2);
	  cpgline(NPT,xx,yy);
	  tokens = tokenize(header,token);
	  strcpy(rxlabelhigh,token[RX_LABEL_HIGH]);
	  strcpy(rxlabellow,token[RX_LABEL_LOW]);
	  if (lowfreqflag == 0) {
	    sprintf(title,"Antenna %1d  High-frequency (%s) Raw data",ant_num,rxlabelhigh);
	  } else {
	    sprintf(title,"Antenna %1d  Low-frequency (%s) Raw data",ant_num,rxlabellow);
	  }
	  if(flag){
	    sprintf(xtitle,"Antenna %ld  Azoff (arcsec)",ant_num);
	  } else {
	    sprintf(xtitle,"Antenna %ld  Eloff (arcsec)",ant_num);
	  }
	  sprintf(ytitle,"Intensity (Volts)");
	  cpglab(xtitle,ytitle,title);
	  cpgend();
	}
	
/*	initial values of parameters::::::	*/

	if ((fp2=fopen(file_n2,"w"))==NULL){
	  printf("nrpoint: cannot open n2 (second time) = %s\n",file_n2);
	  exit(1);
	}
	chmod(file_n2,0666);

	if(fabs(ymax)>=fabs(ymin)) {
		fprintf(fp2,"%f\n",ymax-ymin);
		fprintf(fp2,"%f\n",x[i_maxy]);
	}
	if(fabs(ymin)>fabs(ymax)) {
		fprintf(fp2,"%f\n",ymin-ymax);
		fprintf(fp2,"%f\n",x[i_miny]);
	}
	fprintf(fp2,"%f\n",20.0);
	fprintf(fp2,"%f\n",0.0);
	fprintf(fp2,"%f\n",y[1]);
	fclose(fp2);

	if ((fp2=fopen(file_n2,"r"))==NULL){
	  printf("nrpoint: cannot open n2 for read = %s\n",file_n2);
	  exit(1);
	}

	for(i=1;i<=MA;i++) 
	{
	  fscanf(fp2,"%f\n",&guessed_parameters);
	  a[i]=guessed_parameters;
	  ia[i]=i;
	}

	fclose(fp2);

/*      start fitting	*/ 
	alamda = -1;
	mrqmin(x,y,sig,NPT,a,ia,MA,covar,alpha,&chisq,fgauss2,&alamda);
	k=1;
	itst=0;
	for (;;) {
	  /*
	    printf("\n%s %2d %17s %9.3e %10s %9.3e\n","Iteration #",k, "chi-squared:",chisq,"alamda:",alamda);
	    for (i=1;i<=MA;i++) printf("%5.3e ",a[i]);
	    printf("\n");
	  */
	  k++;
	  
	  ochisq=chisq;
	  mrqmin(x,y,sig,NPT,a,ia,MA,covar,alpha,&chisq,fgauss2,&alamda);
	  if (chisq > ochisq)
	    itst=0;
	  else if ((fabs(ochisq-chisq) < 0.01 &&
		    fabs(chisq) < .1) || (k>10))
	    {itst++;}
	  if (itst < 4) continue;
	  
	  /*
	    if ((fp2=fopen(file_n2,"w"))==NULL){
	    printf("cannot open %s\n",file_n2);
	    exit(1);
	    }
	  */
	  
	  /*
	    for (i=1;i<=MA;i++) fprintf(fp2,"%f\n",a[i]);
	  */
	  for (i=1;i<=MA;i++) fprintf(fp4,"%f ",a[i]);
	  fprintf(fp4,"%f ",chisq);
	  printf("%f\n ",chisq);
	  /*
	    fprintf(fp2,"\n");
	  */
	  fprintf(fp4,"\n\n");
	  /*
	    fclose(fp2);
	  */
	  
	  for(j=1;j<=NPT;j++){
	    yyy[j-1]=0.0;
	    for(k=1;k<=MA;k+=5){
	      arg=(x[j]-a[k+1])/a[k+2];
	      yyy[j-1]+=a[k]*exp(-arg*arg)+a[k+3]*x[j]+a[k+4];
	    }
	    res[j-1]=y[j]-yyy[j-1]+a[5];
	    fprintf (fp1,"%.6f %.6f %.6f %.6f\n",x[j],y[j],yyy[j-1],res[j-1]);
	  }
	  fclose(fp1);
	  
	  alamda=0.0;
	  mrqmin(x,y,sig,NPT,a,ia,MA,covar,alpha,&chisq,fgauss2,&alamda);
	  rms_fac=rms(res,NPT);
	  printf("\nUncertainties:\n");
	  for (i=1;i<=MA;i++) printf("%8.4e ",rms_fac*sqrt(covar[i][i]));
	  printf("\n");
	  
	  fprintf(fp4,"\nUncertainties:\n");
	  for (i=1;i<=MA;i++) fprintf(fp4,"%8.4e ",rms_fac*sqrt(covar[i][i]));
	  fprintf(fp4,"\n");
	  printf("Generating plot....\n");
	  break;
	}
fclose(fp4);

	if(flag){
	  if (lowfreqflag == 0) {
	    sprintf(title,"Antenna %1d  High-frequency (%s) AZ scan  Fitted data",ant_num,rxlabelhigh);
	  } else {
	    sprintf(title,"Antenna %1d  Low-frequency (%s) AZ scan  Fitted data",ant_num,rxlabellow);
	  }
	  sprintf(xtitle,"Antenna %ld  Azoff (arcsec)",ant_num);
	}
	else{
	  if (lowfreqflag == 0) {
	    sprintf(title,"Antenna %1d  High-frequency (%s) El scan  Fitted data",ant_num,rxlabelhigh);
	  } else {
	    sprintf(title,"Antenna %1d  Low-frequency (%s) El scan  Fitted data",ant_num,rxlabellow);
	  }
	  sprintf(xtitle,"Antenna %ld  Eloff (arcsec)",ant_num);
	}
	sprintf(ytitle,"Intensity (Volts)");

/*    PGPLOT */
	sprintf(plotant,"%d/xs",(ant_num+10));
	if(plotflag){
	  if(cpgbeg(0,plotant,1,1)!=1) exit(1);
	  /* These do nothing helpful:
	  cpgeras();
	  cpgupdt();
	  */
	  cpgenv(xmin,xmax,ymin,ymax,0,0); 
	  cpgpt(NPT,xx,yy,2);
	  cpgline(NPT,xx,yyy);
	  cpgpt(NPT,xx,res,-1);
	  cpglab(xtitle,ytitle,title);
	  sprintf(f_line,"az= %10.4f deg",azy[i_maxy]);
	  cpgmtxt("t",-2.5,0.05,0,f_line);
	  sprintf(f_line,"el = %10.4f deg",ely[i_maxy]);
	  cpgmtxt("t",-4.0,0.05,0,f_line);
	  sprintf(f_line,"y= %10.4f",a[1]);
	  cpgmtxt("t",-7.0,0.05,0,f_line);
	  sprintf(f_line,"x = %10.4f arcsec",a[2]);
	  cpgmtxt("t",-5.5,0.05,0,f_line);
	  sprintf(f_line,"width = %10.4f",a[3]*2*0.83255);
	  cpgmtxt("t",-8.5,0.05,0,f_line);
	  sprintf(f_line,"chisq = %10.4e",chisq);
	  cpgmtxt("t",-10.0,0.05,0,f_line);
	  cpgend();
	}
        fpsummary = fopen(summary_file_name,"r");
	if (fpsummary == NULL) {
	  fpsummary = fopen(summary_file_name,"w");
	} else {
	  fclose(fpsummary);
	  fpsummary = fopen(summary_file_name,"a");
	}
	if (fpsummary == NULL) {
	  printf("Could not write to summary file = %s\n",summary_file_name);
	} else {
#if USE_HEADER
	  sprintf(fullfilename,"/data/engineering/rpoint/ant%d/header.dat",ant_num);
	  headerfp = fopen(fullfilename,"r");
	  /* skip the first line */
	  fgets(buffer,sizeof(buffer),headerfp);
	  fgets(buffer,sizeof(buffer),headerfp);
	  fclose(headerfp);
	  /* cut off the final carriage return */
	  buffer[strlen(buffer)-1] = 0;
	  fprintf(fpsummary,"%s,",buffer);
#endif
	  if (flag == 1) {
	    fprintf(fpsummary,"rpoint: azoff %f %f %f %f ",a[1],a[2],
			a[3]*2*0.83255, rms_fac*sqrt(covar[2][2]));
	  } else {
	    fprintf(fpsummary,"rpoint: eloff %f %f %f %f ",a[1],a[2],
			a[3]*2*0.83255, rms_fac*sqrt(covar[2][2]));
	  }
          /* Following lines added 16 Nov 04, for aperture efficiecncy: TK */
          /* create a temporary file eta_tmp and run the aperture efficiency program */
	  /* needed: 
	  /*  4: source - object
	     14: planetdia
	     29: temperature
  	     37: cabin temperature
             40: elcmd
	     91: rest freq
	     92: sidebandA 
	     a[1]=intensity
	     a[2]=offset
	     a[3]*2*0.83255=scanwidth
	  */
	  printf("Computing aperture efficiency....\n");
          fpi_eta=fopen("aperInput.tmp","w");
	  use_beam=USE_BEAM;
	  delVsource=a[1];
	  WidthFwhm=a[3]*2*0.83255;
	  sprintf(etaCommand, "nawk -F, \' (NR>=2) {print $4,$14,$29,$37,$40,$91,$92,$107}\' /data/engineering/rpoint/ant%d/header.dat > picked.tmp",ant_num);
/*	  printf("%s\n", etaCommand); */
	  system(etaCommand);
	  fph_eta=fopen("picked.tmp","r");
	  fscanf(fph_eta,"%s %f %f %f %f %f %f",object,&PlanetDia,&Tamb,&Tcab,&el,&Frequency,&SB);
	  fscanf(fph_eta, "%s %f %d %f %d %f %d %f %d %f %d %f %d %f %d %f %f", rawfilename, &VhotL, &time_stamp, &VhotH, &time_stamp, &VskyL, &time_stamp, &VskyH, &time_stamp, &tau_zenith, &time_stamp, &Tatm , &time_stamp, &eta_l, &time_stamp, &Frequency, &SB);
	  if (lowfreqflag == 0) {
		Vhot=VhotL;
		Vsky=VskyL;
	  }
	  else {
		Vhot=VhotH;
		Vsky=VskyH;
	  }
		
/*	  fscanf(fph_eta, "%s %f %f %f %f %f %f %f %f", rawfilename, &Thot, &tau_zenith, &eta_l, &Vhot, &Vsky, &delVsource,
          &WidthFwhm); */
	  printf("raw file name: %s\n", rawfilename);
	  Tamb = (Tamb+Tcab)/2.0;
	  Thot=Tamb;
/*	  Frequency=Frequency-SB*5.0; */
/*	 
	  Thot=
	  Vhot=
	  Vsky=
	  delVsource=
	  fwhm_beam=52.0;
	  WidthFwhm=
	  Tbright=100;
	  TBright=
*/
	  if (object=="jupiter") TBright=TB_JUP;
	  if (object=="saturn")  TBright=TB_SAT;
	  if (strstr(object,"jupiter")!=NULL) TBright=TB_JUP;
	  if (strstr(object,"saturn")!=NULL)  TBright=TB_SAT;
	  err=0.0;
          fprintf(fpi_eta, "%s %d %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %d\n", rawfilename, ant_num, object, el, tau_zenith, Thot,Tamb,Tatm,eta_l,Vhot,Vsky,delVsource,fwhm_beam,Frequency, PlanetDia, WidthFwhm,TBright,err,use_beam);
	   sprintf(etaCommand, "aperEff aperInput.tmp");
          system(etaCommand);
          fpo_eta=fopen("aperResults.tmp","w");
/*	  fscanf(fpo_eta, "%f %f %f", &EtaA,&EtaB,&fwhm_beam); */
	  fprintf(fpo_eta,"%3.2f %3.2f %4.1f\n",EtaA,EtaB,fwhm_beam);
	  fprintf(fpsummary,"%3.2f %3.2f %4.1f\n",EtaA,EtaB,fwhm_beam);
	  fclose(fpsummary);
	  fclose(fpi_eta);
	  fclose(fpo_eta);
	  fclose(fph_eta);
/*	  remove("aperResults.tmp");
	  remove("aperInput.tmp"); */
	}
	printf("recorded! \n");
	printf("azfit %s  | %10.5f %10.5f %10.5f %10.4f %10.2f +- %10.2f %8.1f %8.1f\n",header,azy[i_maxy],ely[i_maxy],a[1],a[3]*2*0.83255,a[2],rms_fac*sqrt(covar[2][2]),azmod[i_maxy],elmod[i_maxy]);
	if(flag==1) fprintf(fp5,"azfit %s  | %10.5f %10.5f %10.5f %10.4f %10.2f +- %10.2f %8.1f %8.1f\n",header,azy[i_maxy],ely[i_maxy],a[1],a[3]*2*0.83255,a[2],rms_fac*sqrt(covar[2][2]),azmod[i_maxy],elmod[i_maxy]);
	else fprintf(fp5,"elfit %s | %10.5f %10.5f %10.5f %10.4f %10.2f +- %10.2f %8.1f %8.1f \n",header,azy[i_maxy],ely[i_maxy],a[1],a[3]*2*0.83255,a[2],rms_fac*sqrt(covar[2][2]),azmod[i_maxy],elmod[i_maxy]);

	fclose(fp5);
	free_matrix(alpha,1,MA,1,MA);
	free_matrix(covar,1,MA,1,MA);
	free_ivector(ia,1,MA);
	free_vector(a,1,MA);
}
コード例 #10
0
ファイル: plotMany_plug.C プロジェクト: zhuww/tempo2
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();
}
コード例 #11
0
int main(int argc, char *argv[]) {
    float *x=NULL,*y=NULL,minx,maxx,miny,maxy,cx, *oparams=NULL,*nparams=NULL;
    float *rx=NULL,*ry=NULL,*w=NULL,*wparams=NULL,*wx=NULL,*wy=NULL,*ww=NULL;
    float *y_sault_fit=NULL, *x_fit=NULL, *y_new_fit=NULL, *y_reynolds_fit=NULL;
    float *y_stevens_fit=NULL,*y_whole_fit=NULL,*y_old_fit=NULL;
    int i,j,n=0,n_fit=100,new_fit_order=2,whole_fit_order=5,nr=0,nw=0;
    /* float extra_x[NEXTRA]={ 93, 95 }, extra_y[NEXTRA] = { 0.1223, 0.1168 }; */
    float extra_x[NEXTRA]= { 93, 95 }, extra_y[NEXTRA] = { 0.1116, 0.1056 };
    float extra_u[NEXTRA]= { 0.01356, 0.01399 };
    float fitp_sault[NFIT_SAULT]= { -202.6259, 149.7321, -36.4943, 2.9372 };
    float fitp_reynolds[NFIT_REYNOLDS]= { -30.7667, 26.4908, -7.0977, 0.605334 };
    float fitp_stevens[NFIT_STEVENS]= { -1.237160, 2.005317, -0.400622 };
    float fitp_old[NFIT_OLD]= { -23.839, 19.569, -4.8168, 0.35836 };
    float *ratio_reynolds_fit=NULL,*ratio_stevens_fit=NULL,*ratio_sault_fit=NULL;
    float *ratio_new_fit=NULL;
    float vpx1, vpx2, vpy1, vpy2, vpy3, lx, ly, dly;
    char fitlabel[BUFSIZE];

    /* Generate the cm fit points. */
    for (cx=1.0; cx<10.0; cx+=0.1) {
        nr++;
        rx = realloc(rx, nr * sizeof(float));
        ry = realloc(ry, nr * sizeof(float));
        rx[nr-1] = log10f(cx * 1000);
        ry[nr-1] = 0.0;
        for (i=0; i<NFIT_REYNOLDS; i++) {
            ry[nr-1] += fitp_reynolds[i] * powf(rx[n-1], (float)i);
        }
    }

    /* Generate the 15mm fit points. */
    for (cx=10.0; cx<=24.0; cx+=0.128) {
        n++;
        x = realloc(x, n * sizeof(float));
        y = realloc(y, n * sizeof(float));
        w = realloc(w, n * sizeof(float));
        x[n-1] = log10f(cx * 1000);
        y[n-1] = 0.0;
        for (i=0; i<NFIT_REYNOLDS; i++) {
            y[n-1] += fitp_sault[i] * powf(x[n-1], (float)i);
        }
        w[n-1] = 1.0/0.1;
    }

    /* Do the fit. */
    linfit_order(NFIT_SAULT, n, x, y, w, &oparams);
    for (i=0; i<NFIT_SAULT; i++) {
        printf("i = %d c[i] = %.4f\n", i, oparams[i]);
    }

    /* Add the 3mm flux points. */
    for (i=0; i<NEXTRA; i++) {
        n++;
        x = realloc(x, n * sizeof(float));
        y = realloc(y, n * sizeof(float));
        w = realloc(w, n * sizeof(float));
        x[n-1] = log10f(extra_x[i] * 1000);
        y[n-1] = log10f(extra_y[i]);
        w[n-1] = 1/extra_u[i];
    }

    /* Do another fit. */
    linfit_order(new_fit_order, n, x, y, w, &nparams);
    for (i=0; i<new_fit_order; i++) {
        printf("i = %d nc[i] = %.4f\n", i, nparams[i]);
    }

    /* Generate the whole range fit points. */
    minx=log10f(900);
    maxx=log10f(100000);
    miny=-2;
    maxy=log10f(20);
    x_fit = malloc(n_fit * sizeof(float));
    for (i=0; i<n_fit; i++) {
        x_fit[i] = minx + i * ((maxx - minx)/(float)n_fit);
        nw++;
        wx = realloc(wx, nw * sizeof(float));
        wy = realloc(wy, nw * sizeof(float));
        ww = realloc(ww, nw * sizeof(float));
        wx[nw-1] = x_fit[i];
        wy[nw-1] = 0.0;
        ww[nw-1] = 1;
        if (x_fit[i] < log10f(11143)) {
            /* Use the Reynolds fit. */
            for (j=0; j<NFIT_REYNOLDS; j++) {
                wy[nw-1] += fitp_reynolds[j] * powf(x_fit[i], (float)j);
            }
        } else {
            /* Use the new fit. */
            for (j=0; j<new_fit_order; j++) {
                wy[nw-1] += nparams[j] * powf(x_fit[i], (float)j);
            }
        }
    }

    /* Do a whole-range fit. */
    linfit_order(whole_fit_order, nw, wx, wy, ww, &wparams);
    for (i=0; i<whole_fit_order; i++) {
        printf("i = %d wc[i] = %.4f\n", i, wparams[i]);
    }

    // minmax(n, x, &minx, &maxx);
    // minmax(n, y, &miny, &maxy);

    y_sault_fit = malloc(n_fit * sizeof(float));
    y_reynolds_fit = malloc(n_fit * sizeof(float));
    y_stevens_fit = malloc(n_fit * sizeof(float));
    y_new_fit = malloc(n_fit * sizeof(float));
    y_whole_fit = malloc(n_fit * sizeof(float));
    y_old_fit = malloc(n_fit * sizeof(float));
    ratio_reynolds_fit = malloc(n_fit * sizeof(float));
    ratio_stevens_fit = malloc(n_fit * sizeof(float));
    ratio_sault_fit = malloc(n_fit * sizeof(float));
    ratio_new_fit = malloc(n_fit * sizeof(float));
    /* minx=log10f(50); */
    minx=log10f(1000);
    /* maxx=log10f(500000); */
    maxx=log10f(110000);
    for (i=0; i<n_fit; i++) {
        x_fit[i] = minx + i * ((maxx - minx)/(float)n_fit);
        y_sault_fit[i] = 0.0;
        y_reynolds_fit[i] = 0.0;
        y_new_fit[i] = 0.0;
        y_stevens_fit[i] = 0.0;
        y_whole_fit[i] = 0.0;
        y_old_fit[i] = 0.0;
        for (j=0; j<NFIT_SAULT; j++) {
            y_sault_fit[i] += fitp_sault[j] * powf(x_fit[i], (float)j);
        }
        for (j=0; j<NFIT_REYNOLDS; j++) {
            y_reynolds_fit[i] += fitp_reynolds[j] * powf(x_fit[i], (float)j);
        }
        for (j=0; j<NFIT_STEVENS; j++) {
            y_stevens_fit[i] += fitp_stevens[j] * powf(x_fit[i], (float)j);
        }
        for (j=0; j<new_fit_order; j++) {
            y_new_fit[i] += nparams[j] * powf(x_fit[i], (float)j);
        }
        for (j=0; j<whole_fit_order; j++) {
            y_whole_fit[i] += wparams[j] * powf(x_fit[i], (float)j);
        }
        for (j=0; j<NFIT_OLD; j++) {
            y_old_fit[i] += fitp_old[j] * powf(x_fit[i], (float)j);
        }
        ratio_reynolds_fit[i] = powf(10, (y_reynolds_fit[i] - y_whole_fit[i]));
        ratio_stevens_fit[i] = powf(10, (y_stevens_fit[i] - y_whole_fit[i]));
        ratio_sault_fit[i] = powf(10, (y_sault_fit[i] - y_whole_fit[i]));
        ratio_new_fit[i] = powf(10, (y_new_fit[i] - y_whole_fit[i]));
    }

    /* cpgopen("11/xs"); */
    cpgopen("1934-638_models.ps/cps");
    /* cpgopen("1934-638_models.png/png"); */
    cpgqvp(0, &vpx1, &vpx2, &vpy1, &vpy2);
    vpy3 = vpy1 + (vpy2 - vpy1) / 5.0;
    /* cpgsvp(vpx1, vpx2, vpy3, vpy2); */
    cpgswin(minx, maxx, miny, maxy);
    lx = minx + (maxx - minx) / 9.0;
    ly = miny + (maxy - miny) / 3.0;
    dly = (maxy - miny) / 20.0;
    cpgsch(1.0);
    cpgbox("BCLNTS",0,0,"BCLNTS",0,0);
    cpglab("Frequency (MHz)", "Flux Density (Jy)", "1934-638 Model Comparison");
    cpgsch(0.8);
    cpgpt(n, x, y, 4);
    /* cpgpt(nw, wx, wy, 4); */
    cpgsci(2);
    /* cpgpt(nr, rx, ry, 4); */
    cpgline(n_fit, x_fit, y_sault_fit);
    strcpy(fitlabel, "Sault: ");
    fitstring(fitp_sault, NFIT_SAULT, fitlabel);
    cpgtext(lx, ly, fitlabel);
    cpgsci(3);
    cpgline(n_fit, x_fit, y_new_fit);
    strcpy(fitlabel, "Stevens (linear): ");
    fitstring(nparams, new_fit_order, fitlabel);
    ly -= dly;
    cpgtext(lx, ly, fitlabel);
    cpgsci(4);
    cpgline(n_fit, x_fit, y_reynolds_fit);
    strcpy(fitlabel, "Reynolds: ");
    fitstring(fitp_reynolds, NFIT_REYNOLDS, fitlabel);
    ly -= dly;
    cpgtext(lx, ly, fitlabel);
    cpgsci(5);
    cpgline(n_fit, x_fit, y_stevens_fit);
    strcpy(fitlabel, "Stevens (Miriad): ");
    fitstring(fitp_stevens, NFIT_STEVENS, fitlabel);
    ly -= dly;
    cpgtext(lx, ly, fitlabel);
    cpgsci(6);
    cpgline(n_fit, x_fit, y_old_fit);
    strcpy(fitlabel, "Pre-1994: ");
    fitstring(fitp_old, NFIT_OLD, fitlabel);
    ly -= dly;
    cpgtext(lx, ly, fitlabel);
    /* cpgsci(6); */
    /* cpgline(n_fit, x_fit, y_whole_fit); */
    /* strcpy(fitlabel, "Stevens (New): "); */
    /* fitstring(wparams, whole_fit_order, fitlabel); */
    /* ly -= dly; */
    /* cpgtext(lx, ly, fitlabel); */
    /* cpgsvp(vpx1, vpx2, vpy1, vpy3); */
    /* cpgsci(1); */
    /* cpgswin(minx, maxx, 0.9, 1.1); */
    /* cpgsch(1.0); */
    /* cpgbox("BCLNTS",0,0,"BCMTS",0,0); */
    /* cpglab("Frequency (MHz)", "Model Ratio", ""); */
    /* cpgsci(2); */
    /* cpgline(n_fit, x_fit, ratio_sault_fit); */
    /* cpgsci(3); */
    /* cpgline(n_fit, x_fit, ratio_new_fit); */
    /* cpgsci(4); */
    /* cpgline(n_fit, x_fit, ratio_reynolds_fit); */
    /* cpgsci(5); */
    /* cpgline(n_fit, x_fit, ratio_stevens_fit); */
    cpgclos();

    exit(0);
}
コード例 #12
0
ファイル: julia.c プロジェクト: LStoney/Fractals
void main()
{
   
   float RES = (XMAX - XMIN)/N;                          //resolution
   
   int i,j,p;
   
    
   
   //************************* PGPLOT CODE ***************************
  
  cpgbeg(0,"?",1,1);
  cpgpage();
  
  cpgsci(1);                                           // axis color
  
  cpgpap(0,1);
  
                                                      //axis limits
  cpgswin(XMIN,XMAX,YMIN,YMAX);
  
  cpgbox("BCN",1, 0, "BCN", 1, 0);                  // draw the axes
  
  cpgsci(1);                                          //data color

  cpgsch(0.00000000000001);                        //data point size
  
  
  
  //******************* GRID ALGORITHM AND PLOTTING *******************
  
  
  struct cnum z;                      //complex variables z and c introduced
  struct cnum c;
  
  for(i=0;i<N;i++)                    //look at every point on grid
  {
    for(j=0;j<N;j++)                       
    {
       z.cx = XMIN + i*RES;           // z = current point
       z.cy = YMIN + j*RES;         
       
       CPRINT(z);                     
       
       c.cx = z.cx;                   // keep z, feed c=z in to iteration
       c.cy = z.cy;
          
       for(p=0;p<MNI;p++)                  //apply MNI iterations to c
       {                                     
	 c = FJULIA(c);
	 
	 if ( c.cx*c.cx + c.cy*c.cy > R)    // if iteration "blows up"... 
	 {
	   z.cx = 0;
	   z.cy = 0;
	   c.cx = 0;
	   c.cy = 0;
	 }
	   
       }                                  
           
           
       if (c.cx*c.cx + c.cy*c.cy < R)      //if iteration hasn't blown up...
       {
	 float X[1], Y[1];
	 X[0] = z.cx;
	 Y[0] = z.cy;
	 cpgpt(1,X,Y,17);                  // plot point z
       }
       
       
    }
    
  }
   
  printf("\n\n");                             
 
  cpgend();                       
  
  
   
}
コード例 #13
0
ファイル: splk_plug.C プロジェクト: gdesvignes/tempo2
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();
}