Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

extract/source/check_optics.c

Go to the documentation of this file.
00001 /* === Doxygen Comment ======================================= */
00013 /* =========================================================== */
00014 
00015 #include <IFU_io.h>
00016 #include <IFU_math.h>
00017 #include <snifs.h>
00018 #include <extract.h>
00019 
00020 #ifdef HAVE_LIBDISLIN
00021 #include <dislin.h>
00022 #endif
00023 
00024 /* === Doxygen Comment ======================================= */
00040 /* =========================================================== */
00041 
00042 int main(int argc, char **argv)
00043 {
00044   SnifsConfig config;
00045   SnifsOptics optics;
00046 
00047   TABLE mask,table;
00048 
00049   char **argval, **arglabel;
00050   char *maskname, *tabname, *devname;
00051   char labcol[lg_label+1];
00052 
00053   int *nopup;
00054   int *colxb,*colyb,*colxc,*colyc;
00055   int i,order,line;
00056   int DEFAULT,DEV;
00057   int minorder,maxorder,singlelens,status;
00058   int colno,colxd,colyd,colxp,colyp,colxo,colyo;
00059   
00060   float *xppup,*yppup;
00061   float lambda,lbdaref;
00062   float val;
00063   
00064   double *xccd, *yccd, *xpccd, *ypccd;
00065   double *xpup,*ypup,*xmla,*ymla;
00066   double lbda_inf,lbda_sup,xc,yc,pixsize;
00067   double xblue,yblue,xred,yred;
00068 
00069 #ifdef HAVE_LIBDISLIN
00070   char title[4*lg_ident+1];
00071   int color,symbol;
00072 #endif
00073 
00074   /* ##### INTRODUCTION ############################## */
00075 
00076   set_purpose("Test-program for the optical and configuration procedures");
00077   set_arglist("-mask mask -out snifs_optics -lambda 0 -default "
00078               "-orders 0,2 -lens 0 -dev NULL");
00079 
00080   init_snifs("$Name:  $","$Revision: 1.19 $");
00081   init_session(argv,argc,&arglabel,&argval);
00082 
00083   if (DEBUG) {
00084     print_msg("$Id: check_optics.c,v 1.19 2005/09/15 21:43:14 ycopin Exp $");
00085     print_msg("Compilation: %s, %s",__DATE__,__TIME__);
00086   }
00087 
00088   /* ===== Parameters ============================== */
00089 
00090   maskname = argval[0];                               /* 0 Input mask      */
00091   tabname  = argval[1];                               /* 1 Output table    */
00092   get_argval(2,"%f",&lambda);                         /* 2 Input lambda    */
00093   DEFAULT = is_true(argval[3]);                       /* 3 Configuration   */
00094   if (sscanf(argval[4], "%d,%d",&minorder,&maxorder) != 2) { /* 4 Orders   */
00095     print_error("Invalid option -orders min_order,max_order (has '%s')",
00096                 argval[4]);
00097     exit_session(ERR_BAD_PARAM);
00098   }
00099   get_argval(5,"%d",&singlelens);                     /* 5 Single lens     */
00100   if ((DEV = is_set(argval[6]))) devname = argval[6]; /* 6 Output device   */
00101 
00102   print_msg("o Input mask: %s",maskname);
00103   print_msg("o Output table: %s",tabname);
00104   if (lambda==0) print_msg("o Test at mpup wavelength");
00105   else           print_msg("o Test at %.1f AA",lambda);
00106   if (DEFAULT) print_msg("o Default configuration used");
00107   else         print_msg("o Reading configuration from mask");
00108   print_msg("o Orders: %d to %d",minorder,maxorder);
00109   if (singlelens) print_msg("o Testing single lens #%d",singlelens);
00110   if (DEV) {
00111 #ifdef HAVE_LIBDISLIN
00112     print_msg("o Output DISLIN device: %s",devname);
00113 #else
00114     print_error("DISLIN output not supported in current executable");
00115     exit_session(ERR_BAD_PARAM);
00116 #endif
00117   }
00118 
00119   /* ===== Mask ============================== */
00120 
00121   if ((status = open_mask(maskname,&mask,"IO"))) exit_session(status);
00122   handle_select_flag(&mask,'W',NULL);
00123 
00124   /* ===== Configuration ============================== */
00125 
00126   /* ----- Channel ------------------------------ */
00127 
00128   lbdaref = read_lbdaref(&mask);
00129 
00130   /* ----- Configuration ------------------------------ */
00131 
00132   if (DEFAULT) {
00133     Channel channel;
00134 
00135     if (!(channel = read_channel(&mask))) exit_session(channel);
00136     read_default_snifs_config(channel,&config);
00137 
00138     if (write_snifs_config_to_mask(&mask,&config)) 
00139       exit_session(ERR_BAD_DESC);
00140   }
00141   else if (read_snifs_config_from_mask(&mask,&config))
00142     exit_session(ERR_NODESC);
00143 
00144   print_snifs_config(&config);
00145 
00146   optics = config.optics;
00147 
00148   /* ----- Variables ------------------------------ */
00149 
00150   lbda_inf = optics.filter.inf_util;         /* Wavelength coverage */
00151   lbda_sup = optics.filter.sup_util;
00152   xc       = optics.center.xc;               /* Center of distortion */
00153   yc       = optics.center.yc;
00154 
00155   pixsize  = config.ccd.pixsize*1e-3;        /* Pixel size [mm] */
00156 
00157   /* ===== Mpup coordinates ============================== */
00158 
00159   print_msg("Reading mpup coordinates (%.1f AA) from mask...",lbdaref);
00160 
00161   colno = get_col_ref(&mask,LAB_COL_NO);
00162   colxd = get_col_ref(&mask,LAB_COL_XLD);
00163   colyd = get_col_ref(&mask,LAB_COL_YLD);
00164   if (colno<0 || colxd<0 || colyd<0) {
00165     print_error("Cannot read specific mask columns in %s",maskname);
00166     exit_session(ERR_NOCOL);
00167   }
00168 
00169   nopup =   (int *)malloc(mask.row*sizeof(int));    /* Mpup number */
00170   xppup = (float *)malloc(mask.row*sizeof(float));  /* Mpup position [px] */
00171   yppup = (float *)malloc(mask.row*sizeof(float)); 
00172 
00173   /* [CCD/px] */
00174   RD_col(&mask,colno,nopup);
00175   RD_col(&mask,colxd,xppup);
00176   RD_col(&mask,colyd,yppup);
00177 
00178   print_msg("   %d (1st-order) mpups",mask.row);
00179 
00180   close_table(&mask);
00181 
00182   /* ===== Input wavelength ============================== */
00183 
00184   if (lambda==0) lambda = lbdaref;
00185 
00186   print_msg("Single wavelength test: %.1f AA",lambda);
00187 
00188   /* ===== Output table ============================== */
00189 
00190   if (!singlelens) {
00191     print_msg("Creating output table %s...",tabname);
00192 
00193     if (create_table(&table,tabname,-1,-1,'Q',"Optics checks") < 0) {
00194       print_error("Cannot create table %s",tabname);
00195       exit_session(ERR_CREAT);
00196     }
00197 
00198     colno = create_col(&table,LAB_COL_NO, INT,  'R',"I4","");
00199     colxd = create_col(&table,LAB_COL_XLD,FLOAT,'R',"F9.6","px");
00200     colyd = create_col(&table,LAB_COL_YLD,FLOAT,'R',"F9.6","px");
00201     colxp = create_col(&table,"XPUP",     FLOAT,'R',"F9.6","mm");
00202     colyp = create_col(&table,"YPUP",     FLOAT,'R',"F9.6","mm");
00203     colxo = create_col(&table,"XMLA",     FLOAT,'R',"F9.6","mm");
00204     colyo = create_col(&table,"YMLA",     FLOAT,'R',"F9.6","mm");
00205 
00206     colxb = (int *)malloc((maxorder-minorder+1)*sizeof(int));
00207     colyb = (int *)malloc((maxorder-minorder+1)*sizeof(int));
00208     colxc = (int *)malloc((maxorder-minorder+1)*sizeof(int));
00209     colyc = (int *)malloc((maxorder-minorder+1)*sizeof(int));
00210 
00211     for (order=minorder; order<=maxorder; order++) {
00212       sprintf(labcol,"XDET%.1d",order);
00213       colxb[order] = create_col(&table,labcol,FLOAT,'R',"F9.6","mm");
00214       sprintf(labcol,"YDET%.1d",order);
00215       colyb[order] = create_col(&table,labcol,FLOAT,'R',"F9.6","mm");
00216       sprintf(labcol,"XCCD%.1d",order);
00217       colxc[order] = create_col(&table,labcol,FLOAT,'R',"F9.6","px");
00218       sprintf(labcol,"YCCD%.1d",order);
00219       colyc[order] = create_col(&table,labcol,FLOAT,'R',"F9.6","px");
00220     }
00221   }
00222   
00223   /* ##### OPTICS ############################## */
00224 
00225   /* ===== Simple tests ============================== */
00226 
00227   if (DEBUG) {
00228 
00229     /* ----- Null deviation wavelength ------------------------------ */
00230 
00231     snifs_optics_test_forward(&optics, 0., 0., &xblue, &yblue, 4300., 1);
00232     xblue /= pixsize; yblue /= pixsize;
00233     print_msg("Central position (O%d): xccd,yccd = %g px, %g px\n",
00234               1,xblue,yblue);
00235     snifs_optics_forward(&optics, 0., 0., &xblue, &yblue, 4300., 0);
00236     xblue /= pixsize; yblue /= pixsize;
00237     print_msg("Central position (O%d): xccd,yccd = %g px, %g px",
00238               0,xblue,yblue);
00239     snifs_optics_forward(&optics, 0., 0., &xblue, &yblue, 4300., 2);
00240     xblue /= pixsize; yblue /= pixsize;
00241     print_msg("Central position (O%d): xccd,yccd = %g px, %g px\n",
00242               2,xblue,yblue);
00243 
00244     /* ----- Dispersion by pixel ------------------------------ */
00245 
00246     snifs_optics_forward(&optics, 0., 0., &xblue, &yblue, 
00247                               optics.filter.inf_util, 1);
00248     xblue /= pixsize; yblue /= pixsize;
00249     print_msg("Central position (%.1f AA): xccd,yccd = %g px, %g px",
00250               optics.filter.inf_util,xblue,yblue);
00251     snifs_optics_forward(&optics, 0., 0., &xred, &yred, 
00252                               optics.filter.sup_util, 1);
00253     xred /= pixsize; yred /= pixsize;
00254     print_msg("Central position (%.1f AA): xccd,yccd = %g px, %g px",
00255               optics.filter.sup_util,xred,yred);
00256 
00257     print_msg("Dispersion [AA/px]: def. = %f, comp. = %f",
00258               optics.grism.w_disp*pixsize,
00259               ABS(optics.filter.width_util/(yred - yblue)));
00260     print_msg("Dispersion orientation: def. = '%s', comp. = '%s'",
00261               (optics.grism.rotz<M_PI_2)? "red is down":"red is up",
00262               (yblue>yred)? "red is down":"red is up");
00263   }
00264   
00265   /* ===== Optics simulation ============================== */
00266 
00267   xpup = (double *)malloc(mask.row*sizeof(double)); /* Mpup position [mm] */
00268   ypup = (double *)malloc(mask.row*sizeof(double));
00269   xmla = (double *)malloc(mask.row*sizeof(double)); /* Lens position [mm] */
00270   ymla = (double *)malloc(mask.row*sizeof(double));
00271 
00272   xccd = (double *)malloc(mask.row*sizeof(double)); /* CCD position [mm] */
00273   yccd = (double *)malloc(mask.row*sizeof(double));
00274   xpccd= (double *)malloc(mask.row*sizeof(double)); /* CCD position [px] */
00275   ypccd= (double *)malloc(mask.row*sizeof(double));
00276 
00277 #ifdef HAVE_LIBDISLIN
00278   if (DEV) {
00279     filmod("delete");                        /* overwrite */
00280     plot_set_device(devname);                /* Output device */
00281     setpag("DA4P");                          /* portrait */
00282 
00283     disini();
00284     errmod("CHECK","OFF"); /* No check on boundaries */
00285 
00286     if (DEBUG) 
00287       paghdr("$Id: check_optics.c,v 1.19 2005/09/15 21:43:14 ycopin Exp $",
00288              "",4,0); /* Technical header */
00289 
00290     axspos(250,2700); /* Use center(); to center axes */
00291     axslen(1700,2400); /* Axe lengthes */
00292     labtyp("VERT","Y");
00293     intax(); /* Integer labels */
00294     ticpos("REVERS", "XY"); /* Ticks inward */
00295     name("x [px]","x"); name("y [px]","y");
00296 
00297     setvlt("SPEC"); /* Another brighter LUT */
00298     hsymbl(10); /* symbol size (default: 35) */
00299     /* Fonts: disalf,simplx,complx,duplx,triplx,gothic,serif,helve,helves */
00300     complx(); 
00301 
00302     graf(0,config.ccd.nx,500,500,0,config.ccd.ny,500,500); /* start,end,1st_lab,lab_step */
00303 
00304     sprintf(title,"SNIFS optical test\n\nMask: %s ",maskname);
00305     plot_title(title);
00306   }
00307 #endif
00308 
00309   for (line=i=0; i<mask.row; i++) {
00310     if (singlelens && nopup[i]!=singlelens) continue;
00311 
00312     print_msg("Processing lens #%d (%6.1fx%6.1f)",nopup[i],xppup[i],yppup[i]);
00313 
00314     /* [CCD/mm] */
00315     xpup[i] = (xppup[i] - xc)*pixsize;
00316     ypup[i] = (yppup[i] - yc)*pixsize;
00317 
00318     /* [MLA/mm] */
00319     snifs_optics_backward(&optics, xpup[i],ypup[i], xmla+i,ymla+i, lbdaref,1);
00320 
00321     /* Output storage */
00322     if (!singlelens) {
00323       WR_tbl(&table,line,colno,&(nopup[i]));
00324       WR_tbl(&table,line,colxd,&(xppup[i]));
00325       WR_tbl(&table,line,colyd,&(yppup[i]));
00326       val = xpup[i]; WR_tbl(&table,line,colxp,&val);
00327       val = ypup[i]; WR_tbl(&table,line,colyp,&val);
00328       val = xmla[i]; WR_tbl(&table,line,colxo,&val);
00329       val = ymla[i]; WR_tbl(&table,line,colyo,&val);
00330     }
00331     
00332     for (order=minorder; order<=maxorder; order++) {
00333       /* [CCD/mm] */
00334       if (singlelens && DEBUG) {
00335         snifs_optics_test_forward(&optics, xmla[i],ymla[i],
00336                                   xccd+i,yccd+i,lambda,order);
00337       }
00338       else {
00339         snifs_optics_forward(&optics, xmla[i],ymla[i], 
00340                              xccd+i,yccd+i,lambda,order);
00341       }
00342         
00343       /* [CCD/px] */
00344       xpccd[i] = xccd[i]/pixsize + xc;
00345       ypccd[i] = yccd[i]/pixsize + yc;
00346 
00347       if (DEBUG) print_msg("   O%d[%.1f AA]: %6.1fx%6.1f",
00348                            order,lambda,xpccd[i],ypccd[i]);
00349 
00350 #ifdef HAVE_LIBDISLIN
00351       if (DEV) { /* plotting current mpup data */
00352         color = 10*i%255 + 1;
00353         switch (order) {
00354         case 0: symbol = 3;  break; /* +  */
00355         case 1: symbol = 16; break; /* [] */
00356         case 2: symbol = 15; break; /* 0  */
00357         }
00358         setclr(color);
00359         rlsymb(symbol,xpccd[i],ypccd[i]);
00360       }
00361 #endif
00362       
00363       /* Output storage */
00364       if (!singlelens) {
00365         val = xccd[i];  WR_tbl(&table,line,colxb[order],&val);
00366         val = yccd[i];  WR_tbl(&table,line,colyb[order],&val);
00367         val = xpccd[i]; WR_tbl(&table,line,colxc[order],&val);
00368         val = ypccd[i]; WR_tbl(&table,line,colyc[order],&val);
00369       }
00370     } /* loop over orders (order) */
00371 
00372     line ++;
00373   } /* loop over mpups (i) */
00374   
00375 #ifdef HAVE_LIBDISLIN
00376   if (DEV) { /* Graphic output conclusion */
00377     disfin();
00378   }
00379 #endif
00380 
00381   free(nopup); free(xppup); free(yppup);
00382   
00383   free(xpup); free(ypup); free(xmla);  free(ymla);
00384   free(xccd); free(yccd); free(xpccd); free(ypccd);
00385   
00386   if (!singlelens) {
00387     free(colxb); free(colyb); free(colxc); free(colyc);
00388 
00389     close_table(&table);
00390   }
00391     
00392   exit_session(OK);
00393   return(OK);
00394 }
00395 

Generated on Wed Oct 26 23:59:39 2005 for Snifs by doxygen 1.3.5