sinfo_flat_ini_by_cpl.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019 /*----------------------------------------------------------------------------
00020    
00021    File name    :   sinfo_flat_ini_by_cpl.c
00022    Author       :   Andrea Modigliani
00023    Created on   :   May 19, 2004
00024    Description  :   read cpl input for SPIFFI
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 /*---------------------------------------------------------------------------
00030                                 Includes
00031  ---------------------------------------------------------------------------*/
00032 #include "sinfo_flat_ini_by_cpl.h"
00033 #include "sinfo_hidden.h"
00034 #include "sinfo_pro_types.h"
00035 #include "sinfo_raw_types.h"
00036 #include "sinfo_functions.h"
00037 #include "sinfo_file_handling.h"
00038 /*---------------------------------------------------------------------------
00039                     Functions private to this module
00040  ---------------------------------------------------------------------------*/
00041 void sinfo_flat_free_alloc(flat_config * cfg);
00042 static void 
00043 parse_section_frames(flat_config *, cpl_frameset* sof, 
00044                      cpl_frameset** raw, int* status);
00045 static void parse_section_cleanmean(flat_config *, cpl_parameterlist* cpl_cfg);
00046 static void parse_section_badpixel(flat_config *, cpl_parameterlist* cpl_cfg);
00047 static void parse_section_badpix(flat_config *, cpl_parameterlist* cpl_cfg);
00048 static void parse_section_thresh(flat_config *, cpl_parameterlist* cpl_cfg);
00049 static void     
00050 parse_section_qclog(flat_config * cfg, cpl_parameterlist *   cpl_cfg);
00072 flat_config * 
00073 sinfo_parse_cpl_input_flat(cpl_parameterlist* cpl_cfg, 
00074                                    cpl_frameset* sof, 
00075                                    cpl_frameset** raw)
00076 {
00077         flat_config   *       cfg = sinfo_flat_cfg_create();
00078         int status=0;
00079         /*
00080          * Perform sanity checks, fill up the structure with what was
00081          * found in the ini file
00082          */
00083         parse_section_badpixel  (cfg, cpl_cfg);
00084         parse_section_cleanmean (cfg, cpl_cfg);
00085         parse_section_badpix    (cfg, cpl_cfg);
00086         parse_section_thresh    (cfg, cpl_cfg); 
00087         parse_section_qclog     (cfg,cpl_cfg);
00088       
00089         parse_section_frames    (cfg, sof, raw,  &status);
00090         if (status > 0) {
00091                 sinfo_msg_error("parsing cpl input");
00092                 sinfo_flat_cfg_destroy(cfg);
00093                 cfg = NULL ;
00094                 return NULL ;
00095         }
00096         return cfg ;
00097 }
00098 
00108 static void     
00109 parse_section_frames(flat_config * cfg,
00110              cpl_frameset * sof,
00111              cpl_frameset ** raw,
00112                      int* status)
00113 {
00114    int                     i;
00115 
00116    char           *       name ;
00117 
00118    int                     nobj, noff ;
00119    int                     nditherobj, nditheroff ;
00120    int                     found_sky ;
00121    int                     found_dither ;
00122    int nframes=0;
00123    int nraw=0;
00124    char* tag;
00125    char spat_res[FILE_NAME_SZ];
00126    char lamp_status[FILE_NAME_SZ];
00127    char band[FILE_NAME_SZ];
00128    int ins_set=0;
00129 
00130    cpl_frame* frame   = NULL;
00131 
00132    nframes = cpl_frameset_get_size(sof);
00133 
00134    sinfo_extract_raw_frames_type(sof,raw,RAW_FLAT_LAMP);
00135    nraw=cpl_frameset_get_size(*raw);
00136 
00137    if (nraw==0) {
00138       sinfo_extract_raw_frames_type(sof,raw,RAW_FLAT_NS);
00139    }   
00140 
00141 
00142    nraw=cpl_frameset_get_size(*raw);
00143    if (nraw==0) {
00144        sinfo_msg("Frame %s or %s not found!", RAW_FLAT_LAMP,RAW_FLAT_NS);
00145        (*status)++;
00146        return   ;
00147    }
00148 
00149    nraw  = cpl_frameset_get_size(*raw);
00150    frame = cpl_frameset_get_frame(*raw,0);
00151 
00152    sinfo_get_spatial_res(frame,spat_res);
00153    switch(sinfo_frame_is_on(frame))
00154      {
00155 
00156     case 0: 
00157       strcpy(lamp_status,"on");
00158       break;
00159     case 1: 
00160       strcpy(lamp_status,"off");
00161       break;
00162     case -1:
00163       strcpy(lamp_status,"undefined");
00164       break;
00165     default: 
00166       strcpy(lamp_status,"undefined");
00167       break;
00168 
00169      }
00170 
00171    sinfo_get_band(frame,band);
00172    sinfo_msg("spatial resolution: %s lamp status: %s band: %s \n",
00173                      spat_res,              lamp_status,    band);
00174 
00175    
00176    sinfo_get_ins_set(band,&ins_set);
00177 
00178    if(nraw<1) {
00179      sinfo_msg_error("no good raw frame in input, something wrong!");
00180      (*status)++;
00181      return;
00182    }
00183    /* Allocate structures to go into the blackboard */
00184    cfg->framelist     = cpl_malloc(nraw * sizeof(char*));
00185    cfg->frametype     = cpl_malloc(nraw * sizeof(int));
00186    cfg->frameposition = cpl_malloc(nraw * sizeof(int));
00187 
00188    found_sky     = 0 ;
00189    found_dither  = 0 ;
00190    nobj          = 0 ;
00191    noff          = 0 ;
00192    nditheroff    = 0 ;
00193    nditherobj    = 0 ;
00194    for (i=0;i<nraw;i++) {
00195      cfg->framelist[i]=NULL;
00196      cfg->frametype[i]=-1;
00197      cfg->frameposition[i]=-1;
00198    }
00199 
00200 
00201    cfg->nframes         = nraw ;
00202     /* Browse through the charmatrix to get names and file types */
00203    for (i=0 ; i<nraw ; i++) {
00204       frame = cpl_frameset_get_frame(*raw,i);
00205       name= (char*) cpl_frame_get_filename(frame);
00206       if(sinfo_file_exists(name)==1) {
00207     /* to go on the file must exist */
00208     if(cpl_frame_get_tag(frame) != NULL) {
00209       /* If the frame has a tag we process it. Else it is an object */ 
00210       tag= (char*) cpl_frame_get_tag(frame);
00211           /* sinfo_msg("frame %s tag =%s \n",name,tag); */
00212           if((sinfo_frame_is_on(frame)  == 0) ||
00213              (sinfo_frame_is_sky(frame)  == 1) ) 
00214         {
00215 
00216              cfg->framelist[i]=(char*) cpl_frame_get_filename(frame);
00217              cfg->frametype[i] = FRAME_OFF ;
00218              found_sky = 1;
00219              if (sinfo_frame_is_dither(frame)) 
00220            {
00221                 cfg->frameposition[i] = FRAME_POS2 ;
00222                 found_dither = 1 ;
00223                 nditheroff++ ;
00224            }
00225              else 
00226            {
00227                  cfg->frameposition[i] = FRAME_POS1 ;
00228                  noff++ ;
00229            }
00230         }
00231           else if(sinfo_frame_is_on(frame)  == 1) 
00232         {
00233           cfg->framelist[i]=(char*) cpl_frame_get_filename(frame);
00234           cfg->frametype[i] = FRAME_ON ;
00235           if (sinfo_frame_is_dither(frame))
00236         {
00237           cfg->frameposition[i] = FRAME_POS2 ;
00238           found_dither = 1 ;
00239           nditherobj++ ;
00240         }
00241               else 
00242         {
00243           cfg->frameposition[i] = FRAME_POS1 ;
00244           nobj++ ;
00245         }
00246         }
00247  
00248     }
00249        
00250         else 
00251       {
00252             /* No type means an object */
00253             /* No type means position 1 */
00254     /*
00255         cfg->frametype[i] = FRAME_ON ;
00256         cfg->frameposition[i] = FRAME_POS1 ;
00257         nobj ++ ;
00258     */
00259       }
00260       }
00261       /* Store file name into framelist */
00262       /* sinfo_msg("frame=%s\n",cfg->framelist[i]); */
00263     }
00264    
00265    /*
00266    sinfo_msg("Noff= %d Nobj= %d Nditheroff= %d Nditherobj= %d",
00267                      noff,nobj,nditheroff,nditherobj);
00268    */
00269 
00270 
00271    if((nobj<1) && (nditherobj< 1)) {
00272      sinfo_msg_error("no ON raw frame in input, something wrong!");
00273      sinfo_flat_free_alloc(cfg);
00274      (*status)++;
00275      return;
00276    }
00277  
00278    /* Copy relevant information into the blackboard */
00279    cfg->nobj            = nobj ;
00280    cfg->noff            = noff ;
00281    cfg->nditherobj      = nditherobj ;
00282    cfg->nditheroff      = nditheroff ;
00283    cfg->contains_sky    = found_sky ;
00284    cfg->contains_dither = found_dither ;
00285 
00286    strcpy(cfg -> outName, LAMP_FLATS_OUT_FILENAME);
00287 
00288    if(cfg->interpolInd != 0) {
00289 
00290       if(NULL != cpl_frameset_find(sof,PRO_BP_MAP)) {
00291          frame = cpl_frameset_find(sof,PRO_BP_MAP);
00292          strcpy(cfg -> mask,cpl_frame_get_filename(frame));
00293          if(sinfo_file_exists(cfg->mask)==1) {
00294          } else {
00295             sinfo_msg_error("Filename %s for Frame %s not found!", 
00296                        cfg->mask, PRO_BP_MAP);
00297             sinfo_flat_free_alloc(cfg);
00298             (*status)++;
00299            return;
00300  
00301          }
00302 
00303       } else {
00304         sinfo_msg_error("Frame %s not found!", PRO_BP_MAP);
00305         sinfo_flat_free_alloc(cfg);
00306         (*status)++;
00307        return;
00308       } 
00309       if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS)) {
00310          frame = cpl_frameset_find(sof,PRO_SLIT_POS);
00311          strcpy(cfg -> slitposList,cpl_frame_get_filename(frame));
00312          if(sinfo_file_exists(cfg->mask) == 1) {
00313          } else {
00314             sinfo_msg_error("Filename %s for Frame %s not found!", 
00315                        cfg->slitposList, PRO_SLIT_POS);
00316             sinfo_flat_free_alloc(cfg);
00317             (*status)++;
00318             return;
00319      }
00320       } else {
00321         sinfo_msg_error("Frame %s not found!", PRO_SLIT_POS);
00322         sinfo_flat_free_alloc(cfg);
00323         (*status)++;
00324         return;
00325       }
00326 
00327    }
00328 }
00329 
00337 static void     
00338 parse_section_cleanmean(flat_config * cfg, cpl_parameterlist * cpl_cfg)
00339 {
00340    cpl_parameter *p; 
00341   
00342    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.low_rejection");
00343    cfg -> loReject = cpl_parameter_get_double(p);
00344 
00345    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.high_rejection");
00346    cfg -> hiReject = cpl_parameter_get_double(p);
00347 
00348 }
00349 
00357 static void     
00358 parse_section_badpixel(flat_config * cfg, cpl_parameterlist * cpl_cfg)
00359 {
00360    cpl_parameter *p; 
00361 
00362    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.interpol_index");
00363    cfg -> interpolInd = cpl_parameter_get_bool(p);
00364 
00365    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.max_rad");
00366    cfg -> maxRad =  cpl_parameter_get_int(p);
00367 
00368 }
00369 
00377 static void     
00378 parse_section_badpix(flat_config * cfg, cpl_parameterlist * cpl_cfg)
00379 {
00380 
00381    cpl_parameter* p;
00382 
00383    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.bad_ind");
00384    cfg ->  badInd = cpl_parameter_get_bool(p);
00385 
00386    strcpy(cfg -> maskname, LAMP_FLATS_OUT_BPMAP);
00387 
00388    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.sigma_factor");
00389    cfg -> sigmaFactor = cpl_parameter_get_double(p);
00390 
00391    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.factor");
00392    cfg -> factor = cpl_parameter_get_double(p);
00393 
00394    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.iterations");
00395    cfg -> iterations = cpl_parameter_get_int(p);
00396 
00397    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.bad_low_rejection");
00398    cfg -> badLoReject = cpl_parameter_get_double(p);
00399 
00400    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.bad_high_rejection");
00401    cfg -> badHiReject = cpl_parameter_get_double(p);
00402 
00403    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.llx");
00404    cfg -> llx = cpl_parameter_get_int(p);
00405 
00406    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.lly");
00407    cfg -> lly = cpl_parameter_get_int(p);
00408  
00409    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.urx");
00410    cfg -> urx = cpl_parameter_get_int(p);
00411 
00412    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.ury");
00413    cfg -> ury = cpl_parameter_get_int(p);
00414 
00415 }
00416 
00424 static void     
00425 parse_section_thresh(flat_config * cfg,cpl_parameterlist* cpl_cfg)
00426 {
00427 
00428    cpl_parameter* p;
00429    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.thresh_ind");
00430    cfg -> threshInd =cpl_parameter_get_bool(p);
00431 
00432    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.mean_factor");
00433    cfg -> meanfactor = cpl_parameter_get_double(p);
00434 
00435 
00436 }
00437 
00438 
00446 static void     
00447 parse_section_qclog(flat_config * cfg, cpl_parameterlist *   cpl_cfg)
00448 {
00449    cpl_parameter *p;  
00450 
00451    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmin1");
00452    cfg -> qc_fpn_xmin1 = cpl_parameter_get_int(p);
00453 
00454    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmax1");
00455    cfg -> qc_fpn_xmax1 = cpl_parameter_get_int(p);
00456 
00457    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymin1");
00458    cfg -> qc_fpn_ymin1 = cpl_parameter_get_int(p);
00459 
00460    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymax1");
00461    cfg -> qc_fpn_ymax1 = cpl_parameter_get_int(p);
00462 
00463    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmin2");
00464    cfg -> qc_fpn_xmin2 = cpl_parameter_get_int(p);
00465    
00466    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmax2");
00467    cfg -> qc_fpn_xmax2 = cpl_parameter_get_int(p);
00468 
00469    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymin2");
00470    cfg -> qc_fpn_ymin2 = cpl_parameter_get_int(p);
00471 
00472    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymax2");
00473    cfg -> qc_fpn_ymax2 = cpl_parameter_get_int(p);
00474 
00475    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_thresh_min");
00476    cfg -> qc_thresh_min = cpl_parameter_get_int(p);
00477 
00478    p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_thresh_max");
00479    cfg -> qc_thresh_max = cpl_parameter_get_int(p);
00480 
00481 }
00489 void
00490 sinfo_flat_free(flat_config ** cfg)
00491 {  
00492   if ((*cfg) != NULL) {
00493     sinfo_flat_free_alloc(*cfg);
00494     sinfo_flat_cfg_destroy(*cfg);
00495     *cfg=NULL;
00496   }
00497   return;
00498 
00499 }
00507 void 
00508 sinfo_flat_free_alloc(flat_config * cfg)
00509 {
00510   if(cfg->frametype != NULL){
00511        cpl_free(cfg->frametype); 
00512   }
00513   if(cfg->framelist != NULL) {
00514      cpl_free(cfg->framelist);
00515   }
00516   if(cfg->frameposition != NULL) {
00517      cpl_free(cfg->frameposition);
00518   }
00519 return ;
00520 }

Generated on Wed Jan 17 08:33:42 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4