flat_ini.c

00001 
00002 /*----------------------------------------------------------------------------
00003    
00004    File name    :   flat_ini.c
00005    Author       :   Juergen Schreiber
00006    Created on   :   Mar 04, 2002
00007    Description  :   prepare flatfield frames ini file handling for SPIFFI
00008 
00009  ---------------------------------------------------------------------------*/
00010 
00011 
00012 
00013 /*---------------------------------------------------------------------------
00014                                 Includes
00015  ---------------------------------------------------------------------------*/
00016 
00017 #include "flat_ini.h"
00018 
00019 /*---------------------------------------------------------------------------
00020                     Functions private to this module
00021  ---------------------------------------------------------------------------*/
00022 
00023 static void     parse_section_general(dictionary *, flat_config *, int *);
00024 static void     parse_section_frames(dictionary *, flat_config *, int *);
00025 static void     parse_section_cleanmean(dictionary *, flat_config *, int *);
00026 static void     parse_section_badpixel(dictionary *, flat_config *, int *);
00027 static void     parse_section_badpix(dictionary *, flat_config *, int *);
00028 static void     parse_section_thresh(dictionary *, flat_config *, int *);
00029 
00030 /*-------------------------------------------------------------------------*/
00044 /*--------------------------------------------------------------------------*/
00045 
00046 
00047 
00048 int generateFlat_ini_file(
00049     char * ini_name,
00050     char * name_i,
00051     char * name_o
00052 )
00053 {
00054     FILE * ini_file ;
00055 
00056     if (file_exists(ini_name)) {
00057         cpl_msg_warning("generateFlat_ini_file","overwriting %s", ini_name) ;
00058     }
00059     if ((ini_file = fopen(ini_name, "w")) == (FILE*)NULL) {
00060         cpl_msg_error("generateFlat_ini_file","cannot create .ini file %s", ini_name) ;
00061         return -1 ;
00062     }
00063 
00064     fprintf(ini_file,
00065 "#\n"
00066 "# Configuration file for handling of flatfield fits files\n"
00067 "#\n") ;
00068 
00069     fprintf(ini_file, "#\n\n[Eclipse]\n") ;
00070     fprintf(ini_file, "VersionNumber = %s\n\n", get_eclipse_version()) ;
00071     fprintf(ini_file,
00072 "\n"
00073 "#\n"
00074 "# ----- [General] configures various software stuff\n"
00075 "#\n"
00076 "# This section is not mandatory. All eclipse routines\n"
00077 "# should be set once for all in a .eclipse-rc file.\n"
00078 "# If you choose to use the variables here, they will\n"
00079 "# override the settings you have in the environment.\n"
00080 "# See the eclipse installation manual to see what\n"
00081 "# these variables refer to.\n"
00082 "#\n"
00083 "\n"
00084 "[General]\n"
00085 "# MaximumMemory = 512 ;   integer, maximum megs to allocate in RAM\n"
00086 "# MaximumSwap   = 2048 ;  integer, maximum megs to allocate in swap\n"
00087 "# TmpDirName    = .   ;   path to temporary directory\n"
00088 "\n"
00089 "Verbose       = no ;   verbose mode activation\n"
00090 "Debug         = no ;   debug mode activation\n"
00091 "\n"
00092 "# LogFile       = yes ;   activate message logging to a file\n"
00093 "# LogFileName   = /tmp/spiffi-log ;  log file name\n"
00094 "\n"
00095 "\n") ;
00096     fprintf(ini_file,
00097 "#\n"
00098 "# the following are the names given in the argument of the python script\n"
00099 "#\n"
00100 "\n") ;
00101         fprintf(ini_file,
00102 "InFile    = %s ; input file including the names of the stacked frames\n" 
00103 "OutName   = %s ; name of output fits file\n"
00104 , name_i, name_o ) ;
00105 
00106     fprintf(ini_file,
00107 "#\n"
00108 "# [CleanMean] takes an average of the stacks of similar flatfields by\n"
00109 "# rejecting low and high intensity values to get rid of the cosmic rays\n"
00110 "# [BadPixel] interpolates static bad pixels indicated by a given bad pixel\n"
00111 "# mask.\n"
00112 "\n"
00113 "[CleanMean]\n"
00114 "LoReject              = 0.1 ;           percentage of rejected low intensity\n"
00115 "                   pixels before averaging\n" 
00116 "HiReject              = 0.1 ;           percentage of rejected high intensity\n"
00117 "                   pixels before averaging\n" 
00118 "[BadPixel]\n"
00119 "interpolInd           = yes ;           indicator if the bad pixels of the\n"
00120 "                   flatfield should be interpolated\n"
00121 "Mask                  = mask.fits ;     name of the bad pixel mask fits file\n"
00122 "MaxRad                = 4 ;             maximal pixel distance from bad pixel\n"
00123 "                   to take valid pixels\n"
00124 "SlitposList           = slitpos.list ;  name of the ASCII file containing\n"
00125 "                   the edge positions of the slitlets\n"
00126 "[BadPix]\n"
00127 "badInd                = yes ;           indicator if a bad pixel mask should\n"
00128 "                   be generated or not\n"
00129 "maskname              = newmask.fits ;  name of the bad pixel mask fits file\n"
00130 "                   to be generated\n"
00131 "sigmaFactor           = 5. ;            factor of the sigma noise limit; to\n"
00132 "                   remove the column intensity tilt only\n"
00133 "                   pixels which lie within a defined\n"
00134 "                   noise limit are used to fit a\n"
00135 "                   straight line\n"
00136 "factor                = 3. ;            if |pixel - median| > factor *\n"
00137 "                   standard deviation -> then the pixel\n"
00138 "                   value is replaced by the median of\n"
00139 "                   the 8 nearest neighbors\n"
00140 "iterations            = 8 ;             number of iterations to of median\n"
00141 "                   filtering to find bad pixel\n"
00142 "                   clusters\n"
00143 "badLoReject           = 10. ;           percentage of rejected low intensity\n"
00144 "                   pixels before averaging\n"
00145 "badHiReject           = 10. ;           percentage of rejected high intensity\n"
00146 "                   pixels before averaging\n"
00147 "llx                   = 20 ;            to compute image statistics on a\n"
00148 "                   rectangular zone of the image the\n"
00149 "                   coordinates of the rectangle are\n"
00150 "                   needed, llx is the lower left x\n" "                    coordinate in pixel units \n"
00151 "lly                   = 20 ;            lower left y coordinate \n"
00152 "urx                   = 200 ;           upper right x coordinate \n"
00153 "ury                   = 200 ;           upper right y coordinate \n"
00154 "[Thresh]\n"
00155 "ThreshInd             = yes ;           indicator that indicates if the\n"
00156 "                   values beyond a threshold deviation\n"
00157 "                   from the mean are flagged as bad\n"
00158 "                   pixels\n"
00159 "Meanfactor            = 10. ;           factor to the clean standard\n"
00160 "                   deviation to define the threshold\n"
00161 "                   deviation from the clean mean \n"
00162 "\n"
00163 "\n") ;
00164 
00165     fclose(ini_file) ;
00166     return 0 ;
00167 }
00168 
00169 
00170 /*-------------------------------------------------------------------------*/
00181 /*--------------------------------------------------------------------------*/
00182 
00183 flat_config * parse_flat_ini_file(char * ini_name)
00184 {
00185         dictionary    *       sym ;
00186         flat_config   *       cfg ;
00187         int                   status ;
00188 
00189         if (!file_exists(ini_name)) {
00190                 cpl_msg_error("parse_flat_ini_file","cannot find ini file [%s]: aborting", ini_name) ;
00191                 return NULL ;
00192         }
00193         sym = iniparser_load(ini_name) ;
00194         if (sym == NULL) {
00195                 cpl_msg_error("parse_flat_ini_file","in parsing ini file [%s]: aborting", ini_name) ;
00196                 return NULL ;
00197         }
00198 
00199         cfg = flat_cfg_create();
00200         if (cfg==NULL) {
00201                 cpl_msg_error("parse_flat_ini_file","allocating flat_config struct");
00202                 iniparser_freedict(sym) ;
00203                 return NULL ;
00204         }
00205 
00206         /*
00207          * Perform sanity checks, fill up the structure with what was
00208          * found in the ini file
00209          */
00210 
00211         status = 0 ;
00212         parse_section_general   (sym, cfg, &status);
00213         parse_section_frames    (sym, cfg, &status);
00214         parse_section_cleanmean (sym, cfg, &status);
00215         parse_section_badpixel  (sym, cfg, &status); 
00216         parse_section_badpix    (sym, cfg, &status); 
00217         parse_section_thresh    (sym, cfg, &status); 
00218 
00219         iniparser_freedict(sym);
00220 
00221         if (status>0) {
00222                 cpl_msg_error("parse_flat_ini_file","%d errors in ini file [%s]", status, ini_name);
00223                 flat_cfg_destroy(cfg);
00224                 cfg = NULL ;
00225                 return NULL ;
00226         }
00227         return cfg ;
00228 }
00229 
00230 
00231 /*---------------------------------------------------------------------------
00232    Functions:   parse_section_xxx()
00233    In           :       symbolic table read from ini file
00234    Out          :       void
00235    Job          :       update a flat_config structure from what can be
00236                             found in the ini file.
00237    Notice       :       all of these functions update a status integer to
00238                             indicate if an error occurred, or leave it as it is if
00239                             everything went Ok.
00240 
00241         parse_section_general()
00242         parse_section_frames ()
00243         parse_section_cleanmean ()
00244         parse_section_badpixel  () 
00245         parse_section_badpix ()
00246         parse_section_thresh ()
00247 
00248  ---------------------------------------------------------------------------*/
00249 
00250 
00251 static void     parse_section_general(
00252         dictionary * sym,
00253         flat_config * cfg,
00254         int *status
00255 )
00256 {
00257         char    *       cval ;
00258         int             ival ;
00259 
00260         /*
00261          * General section
00262          */
00263         cval = iniparser_getstr(sym, "eclipse:versionnumber") ;
00264         if (cval!=NULL) {
00265                 if (strcmp(cval, get_eclipse_version())) {
00266                         cpl_msg_warning("parse_section_general","this ini file produced by version %s", cval);
00267                         cpl_msg_warning("parse_section_general","you are running version %s", get_eclipse_version());
00268                 }
00269         } else {
00270                 cpl_msg_warning("parse_section_general","no eclipse version number found in file");
00271         }
00272 
00273         ival = iniparser_getint(sym, "general:maximummemory", -1);
00274         if (ival>0) set_memory_parameter("max_ram", ival);
00275         ival = iniparser_getint(sym, "general:maximumswap", -1);
00276         if (ival>0) set_memory_parameter("max_swap", ival);
00277         set_verbose(iniparser_getboolean(sym, "general:verbose", 0));
00278         set_debug(iniparser_getboolean(sym, "general:debug", 0));
00279 
00280         cval = iniparser_getstr(sym, "general:tmpdirname");
00281         if (cval!=NULL) {
00282                 set_tmpdirname(cval);
00283         } else {
00284                 set_tmpdirname(".");
00285         }
00286 
00287         ival = iniparser_getboolean(sym, "general:logfile", 0);
00288         if (ival) {
00289                 cval = iniparser_getstr(sym, "general:logfilename");
00290                 if (cval!=NULL) {
00291                         set_logfile(1);
00292                         set_logfilename(cval);
00293                 } else {
00294                         set_logfile(0) ;
00295                 }
00296         }
00297         cval = iniparser_getstr(sym, "general:outname");
00298         if (cval!=NULL) {
00299                 strcpy (cfg -> outName , cval ) ;
00300         } else {
00301             cpl_msg_error("parse_section_general:"," OutName in the .ini file was not found!\n") ;
00302             (*status)++ ;
00303         }
00304 
00305         if (verbose_active())
00306                 print_memory_parameters();
00307         return ;
00308 }
00309 
00310 static void     parse_section_frames(
00311         dictionary * sym,
00312         flat_config * cfg,
00313         int *status
00314 )
00315 {
00316         char            *       listname ;
00317         charmatrix      *       charm ;
00318         int                     i, j ;
00319         char            *       name,
00320                         *       type ;
00321         int                     nval, nobj, noff ;
00322         int                     nditherobj, nditheroff ;
00323         int                     found_sky ;
00324         int                     found_dither ;
00325         char            **      framelist ;
00326         int             *       frametypes ;
00327         int             *       frameposition ;
00328 
00329         listname = iniparser_getstr(sym, "general:infile");
00330         if (is_ascii_list(listname)!=1) {
00331                 cpl_msg_error("parse_section_frames","file [%s] is not an ASCII list: aborting", listname);
00332                 (*status)++ ;
00333                 return ;
00334         }
00335 
00336         /* Read input char matrix */
00337         charm = charmatrix_read(listname);
00338         if (charm==NULL) {
00339                 cpl_msg_error("parse_section_frames","parsing input list [%s]", listname);
00340                 (*status)++;
00341                 return ;
00342         }
00343 
00344         /* Check input matrix */
00345         nval = charm->ly ;
00346         for (j=0 ; j<charm->ly ; j++) {
00347                 /* Check file existence */
00348                 name = charmatrix_elem(charm, 0, j);
00349                 if (file_exists(name)!=1) {
00350                         cpl_msg_warning("parse_section_frames","file [%s] declared in list does not exist", name);
00351                         nval -- ;
00352                 }
00353         }
00354         if (nval<1) {
00355                 cpl_msg_error("parse_section_frames","no valid plane found in list [%s]", listname);
00356 
00357                 charmatrix_del(charm);
00358                 (*status)++ ;
00359                 return ;
00360         }
00361 
00362         /* Allocate structures to go into the blackboard */
00363         framelist     = cpl_malloc(nval * sizeof(char*));
00364         frametypes    = cpl_malloc(nval * sizeof(int));
00365         frameposition = cpl_malloc(nval * sizeof(int));
00366 
00367         found_sky     = 0 ;
00368         found_dither  = 0 ;
00369         nobj          = 0 ;
00370         noff          = 0 ;
00371         nditheroff    = 0 ;
00372         nditherobj    = 0 ;
00373 
00374         /* Browse through the charmatrix to get names and file types */
00375         i = 0 ;
00376         for (j=0 ; j<charm->ly ; j++) 
00377         {
00378                 name = charmatrix_elem(charm, 0, j);
00379                 if (file_exists(name)==1) 
00380                 {
00381                         /* Store file name into framelist */
00382                         framelist[i] = cpl_strdup(name);
00383                         /* Check if a file type is present */
00384                         if (charm->lx>1) 
00385                         {
00386                                 /* Get file type */
00387                                 type = charmatrix_elem(charm, 1, j);
00388                                 strlwc(type);
00389                                 /* Checking if the type contains 'off' or 'sky' */
00390                                 if (strstr(type, "sky")!=NULL || strstr(type, "off") != NULL) 
00391                                 {
00392                                     frametypes[i] = FRAME_OFF ;
00393                                     found_sky = 1 ;
00394                                     /* Checking if the type contains 'pos1' or 'pos2' */
00395                                     if (strstr(type, "2")!=NULL) 
00396                                     {
00397                                         frameposition[i] = FRAME_POS2 ;
00398                                         found_dither = 1 ;
00399                                         nditheroff++ ;
00400                                     }
00401                                     else 
00402                                     {
00403                                         frameposition[i] = FRAME_POS1 ;
00404                                         noff++ ;
00405                                     }
00406 
00407                                 }
00408                                 else  
00409                                 {
00410                                     frametypes[i] = FRAME_ON ;
00411                                     /* Checking if the type contains 'pos1' or 'pos2' */
00412                                     if (strstr(type, "2")!=NULL) 
00413                                     {
00414                                         frameposition[i] = FRAME_POS2 ;
00415                                         found_dither = 1 ;
00416                                         nditherobj++ ;
00417                                     } 
00418                                     else 
00419                                     {
00420                                         frameposition[i] = FRAME_POS1 ;
00421                                         nobj++ ;
00422                                     }
00423                                 }
00424                         } 
00425                         else 
00426                         {
00427                                 /* No type means an object */
00428                                 frametypes[i] = FRAME_ON ;
00429                                 /* No type means position 1 */
00430                                 frameposition[i] = FRAME_POS1 ;
00431                                 nobj ++ ;
00432                         }
00433                         i++ ;
00434                 }
00435         }
00436         charmatrix_del(charm);
00437 
00438         /* Copy relevant information into the blackboard */
00439         cfg->framelist       = framelist ;
00440         cfg->frametype       = frametypes ;
00441         cfg->frameposition   = frameposition ;
00442         cfg->nframes         = nval ;
00443         cfg->nobj            = nobj ;
00444         cfg->noff            = noff ;
00445         cfg->nditherobj      = nditherobj ;
00446         cfg->nditheroff      = nditheroff ;
00447         cfg->contains_sky    = found_sky ;
00448         cfg->contains_dither = found_dither ;
00449 
00450         return ;
00451 }
00452 
00453 static void     parse_section_cleanmean(
00454         dictionary * sym,
00455         flat_config * cfg,
00456         int *status )
00457 {
00458         float           dval ;
00459 
00460         dval = iniparser_getdouble(sym, "cleanmean:loreject", -1.) ;
00461         if (dval!=-1.) 
00462         {
00463             cfg -> loReject = dval ; 
00464         }
00465         else 
00466         {
00467             cpl_msg_error("parse_section_cleanmean:"," LoReject in the .ini file was not found!\n") ;
00468             (*status)++ ;
00469         }
00470         dval = iniparser_getdouble(sym, "cleanmean:hireject", -1.) ;
00471         if (dval!=-1.) 
00472         {
00473             cfg -> hiReject = dval ; 
00474         }
00475         else 
00476         {
00477             cpl_msg_error("parse_section_cleanmean:"," hiReject in the .ini file was not found!\n") ;
00478             (*status)++ ;
00479         }
00480         return ;
00481 }
00482 
00483 static void     parse_section_badpixel(
00484         dictionary * sym,
00485         flat_config * cfg,
00486         int *status )
00487 {
00488         int             ival ;
00489         char        *   cval ;
00490 
00491         ival = iniparser_getboolean(sym, "badpixel:interpolind", -1) ;
00492         if (ival != -1)
00493         {
00494             cfg -> interpolInd = ival ;
00495         }
00496         else
00497         {
00498             cpl_msg_error("parse_section_badpixel:"," interpolInd in the .ini file was not found!\n") ;
00499             (*status)++ ;
00500         }
00501         cval = iniparser_getstr(sym, "badpixel:mask") ;
00502         if (cval != NULL) 
00503         {
00504             strcpy (cfg -> mask , cval)  ;
00505         }
00506         else
00507         {
00508             cpl_msg_error("parse_section_badpixel:"," mask in the .ini file was not found!\n") ;
00509             (*status)++ ;
00510         }
00511         ival = iniparser_getint(sym, "badpixel:maxrad", -1) ;
00512         if (ival!=-1) 
00513         {
00514             cfg -> maxRad = ival ; 
00515         }
00516         else 
00517         {
00518             cpl_msg_error("parse_section_badpixel:"," MaxRad in the .ini file was not found!\n") ;
00519             (*status)++ ;
00520         }
00521         cval = iniparser_getstr(sym, "badpixel:slitposlist") ;
00522         if (cval != NULL) 
00523         {
00524             strcpy (cfg -> slitposList , cval) ;
00525         }
00526         else
00527         {
00528             cpl_msg_error("parse_section_badpixel:"," SlitposList in the .ini file was not found!\n") ;
00529             (*status)++ ;
00530         }
00531 }
00532 
00533 static void     parse_section_badpix(
00534         dictionary * sym,
00535         flat_config * cfg,
00536         int *status )
00537 {
00538         int             ival ;
00539         float           dval ;
00540         char       *    cval ;
00541 
00542         ival = iniparser_getboolean(sym, "badpix:badind", -1) ;
00543         if (ival != -1)
00544         {
00545             cfg -> badInd = ival ;
00546         }
00547         else
00548         {
00549             cpl_msg_error("parse_section_badpix:"," badInd in the .ini file was not found!\n") ;
00550             (*status)++ ;
00551         }
00552         cval = iniparser_getstr(sym, "badpix:maskname") ;
00553         if (cval != NULL) 
00554         {
00555             strcpy (cfg -> maskname , cval) ;
00556         }
00557         else
00558         {
00559             cpl_msg_error("parse_section_badpix:"," mask in the .ini file was not found!\n") ;
00560             (*status)++ ;
00561         }
00562         dval = iniparser_getdouble(sym, "badpix:sigmafactor", -1.) ;
00563         if (dval!=-1.)
00564         {
00565             cfg -> sigmaFactor = dval ;
00566         }
00567         else
00568         {
00569             cpl_msg_error("parse_section_badpix:"," sigmaFactor in the .ini file was not found!\n") ;
00570             (*status)++ ;
00571         }
00572         dval = iniparser_getdouble(sym, "badpix:factor", -1.) ;
00573         if (dval!=-1.)
00574         {
00575             cfg -> factor = dval ;
00576         }
00577         else
00578         {
00579             cpl_msg_error("parse_section_badpix:"," factor in the .ini file was not found!\n") ;
00580             (*status)++ ;
00581         }
00582         ival = iniparser_getint(sym, "badpix:iterations", -1) ;
00583         if (ival!=-1)
00584         {
00585             cfg -> iterations = ival ;
00586         }
00587         else
00588         {
00589             cpl_msg_error("parse_section_badpix:"," iterations in the .ini file was not found!\n") ;
00590             (*status)++ ;
00591         }
00592         dval = iniparser_getdouble(sym, "badpix:badloreject", -1.) ;
00593         if (dval!=-1.)
00594         {
00595             cfg -> badLoReject = dval ;
00596         }
00597         else
00598         {
00599             cpl_msg_error("parse_section_badpix:"," badLoReject in the .ini file was not found!\n") ;
00600             (*status)++ ;
00601         }
00602         dval = iniparser_getdouble(sym, "badpix:badhireject", -1.) ;
00603         if (dval!=-1.)
00604         {
00605             cfg -> badHiReject = dval ;
00606         }
00607         else
00608         {
00609             cpl_msg_error("parse_section_badpix:"," badHiReject in the .ini file was not found!\n") ;
00610             (*status)++ ;
00611         }
00612         ival = iniparser_getint(sym, "badpix:llx", -1) ;
00613         if (ival!=-1)
00614         {
00615             cfg -> llx = ival ;
00616         }
00617         else
00618         {
00619             cpl_msg_error("parse_section_badpix:"," factor in the .ini file was not found!\n") ;
00620             (*status)++ ;
00621         }
00622         ival = iniparser_getint(sym, "badpix:lly", -1) ;
00623         if (ival!=-1)
00624         {
00625             cfg -> lly = ival ;
00626         }
00627         else
00628         {
00629             cpl_msg_error("parse_section_badpix:"," factor in the .ini file was not found!\n") ;
00630             (*status)++ ;
00631         }
00632         ival = iniparser_getint(sym, "badpix:urx", -1) ;
00633         if (ival!=-1)
00634         {
00635             cfg -> urx = ival ;
00636         }
00637         else
00638         {
00639             cpl_msg_error("parse_section_badpix:"," factor in the .ini file was not found!\n") ;
00640             (*status)++ ;
00641         }
00642         ival = iniparser_getint(sym, "badpix:ury", -1) ;
00643         if (ival!=-1)
00644         {
00645             cfg -> ury = ival ;
00646         }
00647         else
00648         {
00649             cpl_msg_error("parse_section_badpix:"," factor in the .ini file was not found!\n") ;
00650             (*status)++ ;
00651         }
00652         return ;
00653 }
00654 
00655 static void     parse_section_thresh(
00656         dictionary * sym,
00657         flat_config * cfg,
00658         int *status )
00659 {
00660         int             ival ;
00661         float           dval ;
00662 
00663         ival = iniparser_getboolean(sym, "thresh:threshind", -1) ;
00664         if (ival != -1)
00665         {
00666             cfg -> threshInd = ival ;
00667         }
00668         else
00669         {
00670             cpl_msg_error("parse_section_thresh:"," ThreshInd in the .ini file was not found!\n") ;
00671             (*status)++ ;
00672         }
00673         dval = iniparser_getdouble(sym, "thresh:meanfactor", -1.) ;
00674         if (dval!=-1.)
00675         {
00676             cfg -> meanfactor = dval ;
00677         }
00678         else
00679         {
00680             cpl_msg_error("parse_section_thresh:"," meanfactor in the .ini file was not found!\n") ;
00681             (*status)++ ;
00682         }
00683 }
00684 

Generated on Wed Oct 26 13:08:52 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001