Ejemplo n.º 1
0
int main(int argc,char *argv[])
{
  int i,j,k,l,m;
  struct catalog c;
  struct transformation t;
  double ra0,de0;
  float rmsmin;
  float x[NMAX],y[NMAX],rx[NMAX],ry[NMAX];
  struct image img;
  char filename[128];

  if (argc==1) 
    strcpy(filename,"test.fits");
  else if (argc==2)
    strcpy(filename,argv[1]);

  img=read_fits(filename);
  printf("files read\n");
  c=read_catalog("out.dat");
  printf("files read\n");

  // Initial fit
  t.ra0=c.ra[0];
  t.de0=c.de[0];
  t.x0=(float) img.naxis1/2.0;
  t.y0=(float) img.naxis2/2.0;

  for (l=0;l<10;l++) {
    for (j=0;j<5;j++) {
      // Transform
      for (i=0;i<c.n;i++) 
	forward(t.ra0,t.de0,c.ra[i],c.de[i],&c.rx[i],&c.ry[i]);
      
      // Select
      for (i=0,k=0;i<c.n;i++) {
	if (c.usage[i]==1) {
	  x[k]=c.x[i];
	  y[k]=c.y[i];
	  rx[k]=c.rx[i];
	  ry[k]=c.ry[i];
	  k++;
	}
      }

      // Fit
      lfit2d(x,y,rx,k,t.a);
      lfit2d(x,y,ry,k,t.b);
      printf("%f %f %f %f %f %f %f %f\n",t.ra0,t.de0,t.a[0],t.a[1],t.a[2],t.b[0],t.b[1],t.b[2]);
      
      // Move reference point
      reverse(t.ra0,t.de0,t.a[0],t.b[0],&ra0,&de0);
      t.ra0=ra0;
      t.de0=de0;
    }

    // Compute and plot residuals
    for (i=0,c.xrms=0.0,c.yrms=0.0,m=0;i<c.n;i++) {
      if (c.usage[i]==1) {
	c.xres[i]=c.rx[i]-(t.a[0]+t.a[1]*c.x[i]+t.a[2]*c.y[i]);
	c.yres[i]=c.ry[i]-(t.b[0]+t.b[1]*c.x[i]+t.b[2]*c.y[i]);
	printf("%12.4f %12.4f %12.4f %12.4f %10.4f %10.4f\n",c.x[i],c.y[i],c.rx[i],c.ry[i],c.xres[i],c.yres[i]);
	c.res[i]=sqrt(c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i]);
	c.xrms+=c.xres[i]*c.xres[i];
	c.yrms+=c.yres[i]*c.yres[i];
	c.rms+=c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i];
	m++;
      }
    }
    c.xrms=sqrt(c.xrms/(float) m);
    c.yrms=sqrt(c.yrms/(float) m);
    c.rms=sqrt(c.rms/(float) m);
    
    // Deselect outliers
    for (i=0;i<c.n;i++) {
      if (c.res[i]>2*c.rms)
	c.usage[i]=0;
    }
  }
  printf("%12.8lf %10.6lf %10.6lf %8.4f %8.4f %8.4f %8.4f\n",img.mjd,t.ra0,t.de0,t.a[1],t.a[2],t.b[1],t.b[2]);
  printf("%d/%d %f %f %f\n",m,c.n,c.xrms,c.yrms,c.rms);

  //  add_fits_keywords(t,"test.fits");
  modify_fits_keywords(t,filename);

  return 0;
}
Ejemplo n.º 2
0
int main(int argc,char *argv[])
{
  int i,j,k,l,m;
  struct transformation t;
  struct image img;
  char *fitsfile=NULL,*reffile=NULL,catfile[128],calfile[128];
  FILE *outfile;
  struct catalog c;
  float mmin=10.0,rmin=10.0;
  double mjd0=51544.5,ra0,de0,ra1,de1;
  float q0,q1;
  float rmsmin;
  float x[NMAX],y[NMAX],rx[NMAX],ry[NMAX];
  int arg=0,plot=0,add=0,track=0;
  char *env,starfile[128];

  // Environment variables
  env=getenv("ST_DATADIR");
  sprintf(starfile,"%s/data/tycho2.dat",env);

  // Decode options
  if (argc>1) {
    while ((arg=getopt(argc,argv,"f:r:m:R:hpnta"))!=-1) {
      switch (arg) {
	
      case 'f':
	fitsfile=optarg;
	break;
	
      case 'r':
	reffile=optarg;
	break;
	
      case 'm':
	mmin=atof(optarg);
	break;

      case 't':
	track=1;
	break;

      case 'R':
	rmin=atof(optarg);
	break;
	
      case 'p':
	plot=1;
	break;

      case 'a':
	add=1;
	break;
	
      case 'h':
	usage(mmin,rmin);
	return 0;
	
      default:
	usage(mmin,rmin);
	return 0;
      }
    } 
  } else {
    usage(mmin,rmin);
    return 0;
  }

  // Check if minimum input is provided
  if (fitsfile==NULL || reffile==NULL) {
    usage(mmin,rmin);
    return 0;
  }

  // Check this is indeed a FITS file 
  if (is_fits_file(fitsfile)!=1) {
    printf("%s is not a FITS file\n",fitsfile);
    return -1 ;
  }
    
  // Check this is indeed a FITS file 
  if (is_fits_file(reffile)!=1) {
    printf("%s is not a FITS file\n",reffile);
    return -1 ;
  }  

  // Read fits file
  img=read_fits(fitsfile);
  sprintf(catfile,"%s.cat",fitsfile);
  sprintf(calfile,"%s.cal",fitsfile);

  // Read reference transformation
  t=reference(reffile);

  // Correct astrometry for fixed or tracked setup
  if (track==0) {
    precess(mjd0,t.ra0,t.de0,t.mjd,&ra1,&de1);
    ra1=modulo(ra1+gmst(img.mjd)-gmst(t.mjd),360.0);
    precess(img.mjd,ra1,de1,mjd0,&t.ra0,&t.de0);
  }

  // Match catalog
  c=match_catalogs(catfile,starfile,t,img,rmin,mmin);

  
  // Plot
  if (plot==1)
    plot_image(img,t,c,catfile,mmin);

  // Do fit
  if (c.n>10) {
    for (l=0;l<10;l++) {
      for (j=0;j<5;j++) {
	// Transform
	for (i=0;i<c.n;i++) 
	  forward(t.ra0,t.de0,c.ra[i],c.de[i],&c.rx[i],&c.ry[i]);
      
	// Select
	for (i=0,k=0;i<c.n;i++) {
	  if (c.usage[i]==1) {
	    x[k]=c.x[i];
	    y[k]=c.y[i];
	    rx[k]=(float) c.rx[i];
	    ry[k]=(float) c.ry[i];
	    k++;
	  }
	}
	// Fit
	lfit2d(x,y,rx,k,t.a);
	lfit2d(x,y,ry,k,t.b);
	
	// Move reference point
	reverse(t.ra0,t.de0,t.a[0],t.b[0],&ra0,&de0);
	t.ra0=ra0;
	t.de0=de0;
      }
      
      // Compute and plot residuals
      for (i=0,t.xrms=0.0,t.yrms=0.0,m=0;i<c.n;i++) {
	if (c.usage[i]==1) {
	  c.xres[i]=c.rx[i]-(t.a[0]+t.a[1]*c.x[i]+t.a[2]*c.y[i]);
	  c.yres[i]=c.ry[i]-(t.b[0]+t.b[1]*c.x[i]+t.b[2]*c.y[i]);
	  
	  c.res[i]=sqrt(c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i]);
	  t.xrms+=c.xres[i]*c.xres[i];
	  t.yrms+=c.yres[i]*c.yres[i];
	  t.rms+=c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i];
	  m++;
	}
      }
      t.xrms=sqrt(t.xrms/(float) m);
      t.yrms=sqrt(t.yrms/(float) m);
      t.rms=sqrt(t.rms/(float) m);
      
      // Deselect outliers
      for (i=0;i<c.n;i++) {
	if (c.res[i]>2*t.rms)
	  c.usage[i]=0;
      }
    }
  } else {
    t.xrms=0.0;
    t.yrms=0.0;
    t.rms=0.0;
  }

  // Print results
  outfile=fopen(calfile,"w");
  for (i=0;i<c.n;i++) 
    if (c.usage[i]==1)
      fprintf(outfile,"%10.4f %10.4f %10.6f %10.6f %8.3f %8.3f %8.3f %8.3f %8.3f\n",c.x[i],c.y[i],c.ra[i],c.de[i],c.vmag[i],c.imag[i],c.fb[i],c.fm[i],c.bg[i]);
  fclose(outfile);

  printf("%s %8.4lf %8.4lf ",fitsfile,t.ra0,t.de0);
  printf("%3d/%3d %6.1f %6.1f %6.1f\n",m,c.n,t.xrms,t.yrms,t.rms);

  // Add keywords
  if (add==1)
    add_fits_keywords(t,fitsfile);
  else
    modify_fits_keywords(t,fitsfile);

  return 0;
}