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

extract/lib/proc_mask.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 #define LIMITS_RANGE 0.2
00021 
00022 /* === Doxygen Comment ======================================= */
00026 /* =========================================================== */
00027 
00028 void norm_mask_param(Channel channel, double par[MASK_NPARAM])
00029 {
00030   /* PARAMETERS: 
00031      Collimator: f,E,lc[3]          (0,1,2..4)
00032      Camera:     f,E,lc[3]          (5,6,7..9)
00033      Grism:      A,tiltx,tilty,rotz (10,11,12,13)
00034      Center:     x,y                (14,15)
00035      => Total = 16 = MASK_NPARAM
00036   */
00037 
00038   /* One has to divide by ABS(default) for possible negative values */
00039 
00040   switch (channel) {
00041   case BLUE_CHANNEL:
00042     par[0] /= SNIFSB_COLL_F;
00043     par[1] /= ABS(SNIFSB_COLL_E);
00044     par[2] /= ABS(SNIFSB_COLL_A1);
00045     par[3] /= ABS(SNIFSB_COLL_A2);
00046     par[4] /= ABS(SNIFSB_COLL_A3);
00047 
00048     par[5] /= SNIFSB_CAM_F;
00049     par[6] /= ABS(SNIFSB_CAM_E);
00050     par[7] /= ABS(SNIFSB_CAM_A1);
00051     par[8] /= ABS(SNIFSB_CAM_A2);
00052     par[9] /= ABS(SNIFSB_CAM_A3);
00053 
00054     par[10] /= SNIFSB_GRISM_ANG*M_PI/180.;
00055 
00056     par[14] /= CCDB_NX/2.;
00057     par[15] /= CCDB_NY/2.;
00058     break;
00059   case RED_CHANNEL:
00060     par[0] /= SNIFSR_COLL_F;
00061     par[1] /= ABS(SNIFSR_COLL_E);
00062     par[2] /= ABS(SNIFSR_COLL_A1);
00063     par[3] /= ABS(SNIFSR_COLL_A2);
00064     par[4] /= ABS(SNIFSR_COLL_A3);
00065 
00066     par[5] /= SNIFSR_CAM_F;
00067     par[6] /= ABS(SNIFSR_CAM_E);
00068     par[7] /= ABS(SNIFSR_CAM_A1);
00069     par[8] /= ABS(SNIFSR_CAM_A2);
00070     par[9] /= ABS(SNIFSR_CAM_A3);
00071 
00072     par[10] /= SNIFSR_GRISM_ANG*M_PI/180.;
00073 
00074     par[14] /= CCDR_NX/2.;
00075     par[15] /= CCDR_NY/2.;
00076     break;
00077   case UNKNOWN_CHANNEL:
00078   case NB_CHANNELS:
00079     break;
00080   }
00081 }
00082 
00083 /* === Doxygen Comment ======================================= */
00087 /* =========================================================== */
00088 
00089 void unnorm_mask_param(Channel channel, double par[MASK_NPARAM])
00090 {
00091   /* One has to divide by ABS(default) for possible negative values */
00092 
00093   switch (channel) {
00094   case BLUE_CHANNEL:
00095     par[0]  *= SNIFSB_COLL_F;
00096     par[1]  *= ABS(SNIFSB_COLL_E);
00097     par[2]  *= ABS(SNIFSB_COLL_A1);
00098     par[3]  *= ABS(SNIFSB_COLL_A2);
00099     par[4]  *= ABS(SNIFSB_COLL_A3);
00100 
00101     par[5]  *= SNIFSB_CAM_F;
00102     par[6]  *= ABS(SNIFSB_CAM_E);
00103     par[7]  *= ABS(SNIFSB_CAM_A1);
00104     par[8]  *= ABS(SNIFSB_CAM_A2);
00105     par[9]  *= ABS(SNIFSB_CAM_A3);
00106                      
00107     par[10] *= SNIFSB_GRISM_ANG*M_PI/180.;
00108                      
00109     par[14] *= CCDB_NX/2.;
00110     par[15] *= CCDB_NY/2.;
00111     break;
00112   case RED_CHANNEL:
00113     par[0]  *= SNIFSR_COLL_F;
00114     par[1]  *= ABS(SNIFSR_COLL_E);
00115     par[2]  *= ABS(SNIFSR_COLL_A1);
00116     par[3]  *= ABS(SNIFSR_COLL_A2);
00117     par[4]  *= ABS(SNIFSR_COLL_A3);
00118 
00119     par[5]  *= SNIFSR_CAM_F;
00120     par[6]  *= ABS(SNIFSR_CAM_E);
00121     par[7]  *= ABS(SNIFSR_CAM_A1);
00122     par[8]  *= ABS(SNIFSR_CAM_A2);
00123     par[9]  *= ABS(SNIFSR_CAM_A3);
00124                      
00125     par[10] *= SNIFSR_GRISM_ANG*M_PI/180.;
00126                      
00127     par[14] *= CCDR_NX/2.;
00128     par[15] *= CCDR_NY/2.;
00129     break;
00130   case UNKNOWN_CHANNEL:
00131   case NB_CHANNELS:
00132     break;
00133   }
00134 }
00135 
00136 /* === Doxygen Comment ======================================= */
00140 /* =========================================================== */
00141 
00142 void set_mask_param_limits(double par[MASK_NPARAM], 
00143                            double parlb[MASK_NPARAM], double parub[MASK_NPARAM])
00144 {
00145   int i;
00146 
00147   /* ===== Setting limits ============================== */
00148 
00149   print_warning("Setting mask param limits to +-%d%% (non-tilts) "
00150                 "and +-5 (tilts)",(int)(LIMITS_RANGE*100));
00151   
00152   for (i=0; i<MASK_NPARAM; i++) {
00153     if (par[i]) {
00154       parlb[i] = par[i]*((par[i]>0)? (1-LIMITS_RANGE) : (1+LIMITS_RANGE));
00155       parub[i] = par[i]*((par[i]>0)? (1+LIMITS_RANGE) : (1-LIMITS_RANGE));
00156     }
00157     else {
00158       parlb[i] = -5./180.*M_PI;
00159       parub[i] = +5./180.*M_PI;
00160     }
00161   }
00162 
00163   /* DEBUG DEBUG DEBUG */
00164   print_warning("DEBUG DEBUG DEBUG: Lateral colors coeffs kept fixed");
00165   for (i=2; i<=4; i++) parlb[i] = parub[i] = par[i];
00166   for (i=7; i<=9; i++) parlb[i] = parub[i] = par[i];
00167   /* DEBUG DEBUG DEBUG */
00168 
00169 //  /* DEBUG DEBUG DEBUG */
00170 //  print_warning("DEBUG DEBUG DEBUG: Everything but center kept fixed");
00171 //  for (i=0; i<=13; i++) parlb[i] = parub[i] = par[i];
00172 //  /* DEBUG DEBUG DEBUG */
00173 }
00174 
00175 /* === Doxygen Comment ======================================= */
00179 /* =========================================================== */
00180 
00181 void optics2mask_param(SnifsOptics *optics, double par[MASK_NPARAM])
00182 {
00183   int i;
00184 
00185   /* Collimator */
00186   par[0] = optics->collimator.focal;
00187   par[1] = optics->collimator.distor;
00188   for (i=0; i<3; i++) 
00189     par[2+i] = optics->collimator.LateralColor.coef[i];
00190 
00191   /* Camera */
00192   par[5] = optics->camera.focal;
00193   par[6] = optics->camera.distor;
00194   for (i=0; i<3; i++) 
00195     par[7+i] = optics->camera.LateralColor.coef[i];
00196 
00197   /* Grism */
00198   par[10] = optics->grism.A;
00199   par[11] = optics->grism.tiltx;
00200   par[12] = optics->grism.tilty;
00201   par[13] = optics->grism.rotz;
00202 
00203   /* Center of distortion */
00204   par[14] = optics->center.xc;
00205   par[15] = optics->center.yc;
00206 }
00207 
00208 /* === Doxygen Comment ======================================= */
00212 /* =========================================================== */
00213 
00214 void mask_param2optics(double par[MASK_NPARAM], SnifsOptics *optics)
00215 {
00216   int i;
00217 
00218   /* Collimator */
00219   optics->collimator.focal  = par[0];
00220   optics->collimator.distor = par[1];
00221   for (i=0; i<3; i++) 
00222     optics->collimator.LateralColor.coef[i] = par[2+i];
00223 
00224   /* Camera */
00225   optics->camera.focal  = par[5];
00226   optics->camera.distor = par[6];
00227   for (i=0; i<3; i++) 
00228     optics->camera.LateralColor.coef[i] = par[7+i];
00229 
00230   /* Grism */
00231   optics->grism.A     = par[10];
00232   optics->grism.tiltx = par[11];
00233   optics->grism.tilty = par[12];
00234   optics->grism.rotz  = par[13];
00235 
00236   /* Center of distortion */
00237   optics->center.xc = par[14];
00238   optics->center.yc = par[15];
00239 }
00240 
00241 /* === Doxygen Comment ======================================= */
00245 /* =========================================================== */
00246 
00247 void print_mask_param(double par[MASK_NPARAM])
00248 {
00249   print_msg("Mask parameters:");
00250   print_msg("   Collimator: f = %.3g mm, E = %.3g",par[0],par[1]);
00251   print_msg("               lc = %10.3g, %10.3g, %10.3g",par[2],par[3],par[4]);
00252   print_msg("   Camera:     f = %.3g mm, E = %.3g",par[5],par[6]);
00253   print_msg("               lc = %10.3g, %10.3g, %10.3g",par[7],par[8],par[9]);
00254   print_msg("   Grism:      A = %.3g deg",par[10]/M_PI*180);
00255   print_msg("               x = %.3g deg, y = %.3g deg, z = %.3g deg",
00256             par[11]/M_PI*180,par[12]/M_PI*180,par[13]/M_PI*180);
00257   print_msg("   Center of dist.: %.2f x %.2f",par[14],par[15]);
00258 }
00259 
00260 /* === Doxygen Comment ======================================= */
00264 /* =========================================================== */
00265 
00266 void pmpin_line(char *format, double par, double parlb, double parub)
00267 {
00268   char fmt[lg_ident+1];
00269 
00270   sprintf(fmt,"%s %s",format,(parlb==parub)? "[FIXED]":"[%.3g,%.3g]");
00271   if (parlb == parub) {
00272     if (DEBUG) print_msg(fmt,par);
00273   }
00274   else print_msg(fmt,par,parlb,parub);
00275 }
00276 
00277 /* === Doxygen Comment ======================================= */
00281 /* =========================================================== */
00282 
00283 void print_mask_param_in(double par[MASK_NPARAM], 
00284                          double parlb[MASK_NPARAM], double parub[MASK_NPARAM])
00285 {
00286   print_msg("Input mask parameters [limits]:");
00287   pmpin_line("   Collimator: f  = %.3g mm",par[0],parlb[0],parub[0]);
00288   pmpin_line("               E  = %.3g",par[1],parlb[1],parub[1]);
00289   pmpin_line("               lc = %10.3g",par[2],parlb[2],parub[2]);
00290   pmpin_line("                    %10.3g",par[3],parlb[3],parub[3]);
00291   pmpin_line("                    %10.3g",par[4],parlb[4],parub[4]);
00292   pmpin_line("   Camera:     f  = %.3g mm",par[5],parlb[5],parub[5]);
00293   pmpin_line("               E  = %.3g",par[6],parlb[6],parub[6]);
00294   pmpin_line("               lc = %10.3g",par[7],parlb[7],parub[7]);
00295   pmpin_line("                    %10.3g",par[8],parlb[8],parub[8]);
00296   pmpin_line("                    %10.3g",par[9],parlb[9],parub[9]);
00297   pmpin_line("   Grism:      A = %.3g deg",
00298              par[10]/M_PI*180,parlb[10]/M_PI*180,parub[10]/M_PI*180);
00299   pmpin_line("               Tilt x = %.3g deg",
00300              par[11]/M_PI*180,parlb[11]/M_PI*180,parub[11]/M_PI*180);
00301   pmpin_line("               Tilt y = %.3g deg",
00302              par[12]/M_PI*180,parlb[12]/M_PI*180,parub[12]/M_PI*180);
00303   pmpin_line("               Rot  z = %.3g deg",
00304              par[13]/M_PI*180,parlb[13]/M_PI*180,parub[13]/M_PI*180);
00305   pmpin_line("   Center of dist.: x = %.3g",par[14],parlb[14],parub[14]);
00306   pmpin_line("                    y = %.3g",par[15],parlb[15],parub[15]);
00307 }
00308 
00309 /* === Doxygen Comment ======================================= */
00313 /* =========================================================== */
00314 
00315 void pmpout_line(char *format, double par, double epar)
00316 {
00317   char fmt[lg_ident+1];
00318 
00319   sprintf(fmt,"%s %s",format,(epar==0)? "[FIXED]":"[+- %.3g]");
00320   if (epar == 0) {
00321     if (DEBUG) print_msg(fmt,par);
00322   }
00323   else print_msg(fmt,par,epar);
00324 }
00325 
00326 /* === Doxygen Comment ======================================= */
00330 /* =========================================================== */
00331 
00332 void print_mask_param_out(double par[MASK_NPARAM], double epar[MASK_NPARAM])
00333 {
00334   print_msg("Output mask parameters [errors]:");
00335   pmpout_line("   Collimator: f  = %.3g mm",par[0],epar[0]);
00336   pmpout_line("               E  = %.3g",par[1],epar[1]);
00337   pmpout_line("               lc = %10.3g",par[2],epar[2]);
00338   pmpout_line("                    %10.3g",par[3],epar[3]);
00339   pmpout_line("                    %10.3g",par[4],epar[4]);
00340   pmpout_line("   Camera:     f  = %.3g mm",par[5],epar[5]);
00341   pmpout_line("               E  = %.3g",par[6],epar[6]);
00342   pmpout_line("               lc = %10.3g",par[7],epar[7]);
00343   pmpout_line("                    %10.3g",par[8],epar[8]);
00344   pmpout_line("                    %10.3g",par[9],epar[9]);
00345   pmpout_line("   Grism:      A = %.3g deg",
00346              par[10]/M_PI*180,epar[10]/M_PI*180);
00347   pmpout_line("               Tilt x = %.3g deg",
00348              par[11]/M_PI*180,epar[11]/M_PI*180);
00349   pmpout_line("               Tilt y = %.3g deg",
00350              par[12]/M_PI*180,epar[12]/M_PI*180);
00351   pmpout_line("               Rot  z = %.3g deg",
00352              par[13]/M_PI*180,epar[13]/M_PI*180);
00353   pmpout_line("   Center of dist.: x = %.3g",par[14],epar[14]);
00354   pmpout_line("                    y = %.3g",par[15],epar[15]);
00355 }
00356 
00357 /* === Doxygen Comment ======================================= */
00366 /* =========================================================== */
00367 
00368 int open_mask(char *maskname, TABLE *mask, char *mode)
00369 {
00370   int class;
00371   
00372   print_msg("Opening mask '%s' [%s]...", maskname,mode);
00373 
00374   if (open_table(mask,maskname,mode) < 0) {
00375     print_error("Cannot open mask %s",maskname);
00376     return(ERR_OPEN);
00377   }
00378 
00379   if ((class = read_file_class(mask)) != MSK_FILE) {
00380     print_error("Wrong fclass (%d) for `%s' (should be an `Extraction Mask')",
00381                 class,maskname);
00382     return(ERR_BAD_TYPE);
00383   }
00384 
00385   print_msg("   '%s': %d col, %d rows (%d selected)",
00386             mask->ident,mask->col,mask->allrow,mask->row);
00387 
00388   return(OK);
00389 }
00390 
00391 /* === Doxygen Comment ======================================= */
00400 /* =========================================================== */
00401 
00402 int read_local_model(TABLE *mask, int flag, double ***coeff)
00403 {
00404   char tmpname[lg_ident+1], desc[lg_label], pref[4];
00405   int nc;                         /* Nb of coefficients in local adjustment */
00406   int i,j, col;
00407   float *farr;
00408 
00409   switch(flag) {
00410   case 1: 
00411     print_msg("Reading local adjustment to mask positions..."); 
00412     strcpy(desc,MSK_DXNC);
00413     strcpy(pref,"DX");
00414     break;
00415   case 2:
00416     print_msg("Reading cross-dispersion chromatic model from max analysis...");
00417     strcpy(desc,MSK_SIGNC);
00418     strcpy(pref,"SIG");
00419     break;
00420   default:
00421     print_error("What are you talking about?...");
00422     return(ERR_NOIMPL);
00423   }
00424 
00425   nc = 0;
00426   *coeff = NULL;
00427 
00428   if (RD_desc(mask,desc,INT,1,&nc) < 0) {
00429     print_error("Cannot read descriptor %s in mask",desc);
00430     return(ERR_NODESC);
00431   }
00432 
00433   print_msg("   %s(lambda): %d coefficients",pref,nc);
00434   if (nc <= 0) return(0);                    /* No local correction */
00435 
00436   farr = (float *)malloc(mask->row*sizeof(float));
00437   alloc2d(coeff,mask->row,nc,DOUBLE);
00438 
00439   for (i=0; i<nc; i++) {                     /* xx0,xx1,... columns */
00440     sprintf(tmpname,"%s%d",pref,i);
00441     col = get_col_ref(mask,tmpname);
00442     if (col < 0) {
00443       print_error("Cannot read column %s from mask",tmpname);
00444       return(ERR_BAD_COL);
00445     }
00446     RD_col(mask,col,farr);                   /* Read the float column */
00447     for (j=0; j<mask->row; j++) (*coeff)[j][i] = farr[j];
00448   }
00449   free(farr);
00450 
00451   return(nc);
00452 }
00453 
00454 /* === Doxygen Comment ======================================= */
00462 /* =========================================================== */
00463 
00464 int has_local_model(int nc, double coeff[])
00465 {
00466   int i, flag;
00467 
00468   for (flag=0, i=0; i<nc; i++) flag = flag || coeff[i];
00469   
00470   return(flag);
00471 }
00472 
00473 /* === Doxygen Comment ======================================= */
00484 /* =========================================================== */
00485 
00486 int read_mask_arc(TABLE *mask, char *maskname, char *refname)
00487 {
00488   char name[lg_name+1], *path, *maskpath;
00489 
00490   print_msg("Looking for reference arc [%s] in mask",MSK_ARC);
00491 
00492   if (RD_desc(mask,MSK_ARC,CHAR,lg_name+1, name) <= 0) 
00493     return(ERR_BAD_DESC);
00494 
00495   /* Expanding the descriptor to full pathname: By default, the reference
00496      calibration frame is located along the mask */
00497 
00498   maskpath = get_path(maskname);
00499   
00500   path = get_path(name);
00501   if (strcmp(path,"") && strcmp(path,".") && 
00502       strcmp(path,"./")) {                   /* PATCH*/
00503     print_error("Cannot find Reference arc in mask path %s",maskpath);
00504     return(ERR_BAD_IMA);  
00505   }
00506 
00507   sprintf(refname,"%s/%s",maskpath,name);
00508   
00509   free(maskpath); free(path);                /* Allocated by get_path */
00510 
00511   return(OK);
00512 }
00513 

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