Main Page   File List  

visir_img_ff.c

00001 /* $Id: visir_img_ff.c,v 1.63 2006/01/22 10:31:04 llundin Exp $
00002  *
00003  * This file is part of the VISIR Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2006/01/22 10:31:04 $
00024  * $Revision: 1.63 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <assert.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_utils.h"
00041 #include "irplib_flat.h"
00042 
00043 #include "visir_utils.h"
00044 #include "visir_pfits.h"
00045 #include "visir_dfs.h"
00046 #include "visir_inputs.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049                             Functions prototypes
00050  -----------------------------------------------------------------------------*/
00051 
00052 static int visir_img_ff_create(cpl_plugin *);
00053 static int visir_img_ff_exec(cpl_plugin *);
00054 static int visir_img_ff_destroy(cpl_plugin *);
00055 static int visir_img_ff(cpl_parameterlist *, cpl_frameset *);
00056 static cpl_imagelist * visir_img_ff_reduce(const cpl_frameset *);
00057 static int visir_img_ff_save(const cpl_imagelist *, const cpl_image *,
00058                              const cpl_parameterlist *, cpl_frameset *);
00059 
00060 /*-----------------------------------------------------------------------------
00061                             Static variables
00062  -----------------------------------------------------------------------------*/
00063 
00064 #define pafcopy "^(DATE-OBS|ARCFILE|ESO TPL ID|ESO DET DIT|MJD-OBS)$"
00065 #define paf_img "^(ESO INS FILT1 NAME|ESO INS PFOV)$"
00066 #define paf_spc "^(ESO INS FILT2 NAME|ESO INS RESOL|ESO INS SLIT1 WID" \
00067                  "|ESO INS GRAT1 WLEN|ESO INS GRAT1 NAME)$"
00068 
00069 /* Different powers of two */
00070 #define visir_ff_mode_default (1<<0)
00071 #define visir_ff_mode_spc    (1<<1)
00072 #define visir_ff_mode_tech    (1<<2)
00073 
00074 enum _visir_ff_mode_ {
00075     visir_ff_none     = 0,
00076     visir_ff_img      = visir_ff_mode_default,
00077     visir_ff_spc      = visir_ff_mode_default | visir_ff_mode_spc,
00078     visir_ff_img_tech = visir_ff_mode_default | visir_ff_mode_tech,
00079     visir_ff_spc_tech = visir_ff_mode_default | visir_ff_mode_tech
00080                       | visir_ff_mode_spc
00081 };
00082 
00083 typedef enum _visir_ff_mode_ visir_ff_mode;
00084 
00085 static const char * recipename = "visir_img_ff";
00086 
00087 static struct {
00088     /* Inputs */
00089     visir_ff_mode img_mode;
00090     double        bpm_lo_thresh;
00091     double        bpm_hi_thresh;
00092     /* Outputs */
00093     int           bpm_nb_bad;
00094 } visir_img_ff_config;
00095 
00096 static char visir_img_ff_description[] =
00097 "This recipe computes the flatfield.\n"
00098 "The files listed in the Set Of Frames (sof-file) must be tagged either\n"
00099 "VISIR-flatfield-raw-file.fits " VISIR_IMG_FF_RAW " or\n"
00100 "VISIR-flatfield-raw-file.fits " VISIR_IMG_TECH_FF_RAW " or\n"
00101 "VISIR-flatfield-raw-file.fits " VISIR_SPC_FF_RAW " or\n"
00102 "VISIR-flatfield-raw-file.fits " VISIR_SPC_TECH_FF_RAW "\n"
00103 "\n"
00104 "The corresponding primary product will have a FITS card\n"
00105 "'HIERARCH ESO PRO CATG' with a value of\n"
00106 VISIR_IMG_FF_PROCATG " or\n"
00107 VISIR_IMG_TECH_FF_PROCATG " or\n"
00108 VISIR_SPC_FF_PROCATG " or\n"
00109 VISIR_SPC_TECH_FF_PROCATG
00110 "\n";
00111 
00112 /*-----------------------------------------------------------------------------
00113                                 Functions code
00114  -----------------------------------------------------------------------------*/
00115 
00116 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 int cpl_plugin_get_info(cpl_pluginlist * list)
00127 {
00128     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00129     cpl_plugin  *   plugin = &recipe->interface;
00130 
00131 
00132     if (cpl_plugin_init(plugin,
00133                     CPL_PLUGIN_API,
00134                     VISIR_BINARY_VERSION,
00135                     CPL_PLUGIN_TYPE_RECIPE,
00136                     recipename,
00137                     "Flat field recipe",
00138                     visir_img_ff_description,
00139                     "Lars Lundin",
00140                     PACKAGE_BUGREPORT,
00141                     visir_get_license(),
00142                     visir_img_ff_create,
00143                     visir_img_ff_exec,
00144                     visir_img_ff_destroy)) return 1;
00145 
00146     if (cpl_pluginlist_append(list, plugin)) return 1;
00147     
00148     return 0;
00149 }
00150 
00151 /*----------------------------------------------------------------------------*/
00160 /*----------------------------------------------------------------------------*/
00161 static int visir_img_ff_create(cpl_plugin * plugin)
00162 {
00163     cpl_recipe      *   recipe = (cpl_recipe *)plugin;
00164 
00165     /* Verify plugin type */
00166     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00167 
00168     /* Create the parameters list in the cpl_recipe object */
00169     recipe->parameters = cpl_parameterlist_new();
00170 
00171     /* Fill the parameters list */
00172     return visir_parameter_set(recipe->parameters, recipename,
00173                                VISIR_PARAM_LOWLIM | VISIR_PARAM_HIGHLIM);
00174 
00175 }
00176 
00177 /*----------------------------------------------------------------------------*/
00183 /*----------------------------------------------------------------------------*/
00184 static int visir_img_ff_exec(cpl_plugin * plugin)
00185 {
00186     cpl_recipe * recipe = (cpl_recipe *)plugin;
00187 
00188     /* Verify plugin type */
00189     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00190 
00191     return visir_img_ff(recipe->parameters, recipe->frames);
00192 }
00193 
00194 /*----------------------------------------------------------------------------*/
00200 /*----------------------------------------------------------------------------*/
00201 static int visir_img_ff_destroy(cpl_plugin * plugin)
00202 {
00203     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00204 
00205     /* Verify plugin type */
00206     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00207     cpl_parameterlist_delete(recipe->parameters);
00208     return  0;
00209 }
00210 
00211 /*----------------------------------------------------------------------------*/
00218 /*----------------------------------------------------------------------------*/
00219 static int visir_img_ff(
00220         cpl_parameterlist   *   parlist,
00221         cpl_frameset        *   framelist)
00222 {
00223     cpl_parameter   *   par;
00224     cpl_frameset    *   rawframes = NULL;
00225     cpl_imagelist   *   flat = NULL;
00226     cpl_image       *   bpm_int = NULL;
00227     cpl_mask        *   bpm = NULL;
00228     
00229 
00230     if (cpl_error_get_code()) return cpl_error_get_code();
00231 
00232     visir_img_ff_config.bpm_lo_thresh = -1.0;
00233     visir_img_ff_config.bpm_hi_thresh = -1.0;
00234     visir_img_ff_config.bpm_nb_bad = 0;
00235 
00236     /* Retrieve input parameters */
00237     par = cpl_parameterlist_find(parlist, "visir.visir_img_ff.low");
00238     visir_img_ff_config.bpm_lo_thresh = cpl_parameter_get_double(par);
00239     par = cpl_parameterlist_find(parlist, "visir.visir_img_ff.high");
00240     visir_img_ff_config.bpm_hi_thresh = cpl_parameter_get_double(par);
00241 
00242     skip_if (cpl_error_get_code());
00243 
00244     /* Identify the RAW and CALIB frames in the input frameset */
00245     skip_if (visir_dfs_set_groups(framelist));
00246 
00247     /* Objects observation */
00248     rawframes = irplib_frameset_extract_regexp(framelist, "^(" VISIR_IMG_FF_RAW
00249                                               "|" VISIR_IMG_TECH_FF_RAW
00250                                               "|" VISIR_SPC_TECH_FF_RAW
00251                                               "|" VISIR_SPC_FF_RAW ")$");
00252     skip_if (rawframes == NULL);
00253 
00254     skip_if(visir_dfs_check_frameset_tag(rawframes));
00255 
00256     /* Verify uniqueness of frame type */
00257     visir_img_ff_config.img_mode = visir_ff_none;
00258     if (cpl_frameset_find(rawframes, VISIR_IMG_FF_RAW))
00259         visir_img_ff_config.img_mode = visir_ff_img;
00260     if (cpl_frameset_find(rawframes, VISIR_SPC_FF_RAW)) {
00261         skip_if (visir_img_ff_config.img_mode);
00262         visir_img_ff_config.img_mode = visir_ff_spc;
00263     }
00264     if (cpl_frameset_find(rawframes, VISIR_IMG_TECH_FF_RAW)) {
00265         skip_if (visir_img_ff_config.img_mode);
00266         visir_img_ff_config.img_mode = visir_ff_img_tech;
00267     }
00268     if (cpl_frameset_find(rawframes, VISIR_SPC_TECH_FF_RAW)) {
00269         skip_if (visir_img_ff_config.img_mode);
00270         visir_img_ff_config.img_mode = visir_ff_spc_tech;
00271     }
00272     
00273     /* Compute the flatfield  */
00274     cpl_msg_info(__func__, "Compute the flatfield");
00275     if ((flat = visir_img_ff_reduce(rawframes)) == NULL) {
00276         cpl_msg_error(__func__, "Could not compute the flatfield image");
00277         skip_if(1);
00278     }
00279 
00280     /* Compute the bad pixels map */
00281     cpl_msg_info(__func__, "Compute the bad pixels map");
00282     bpm = cpl_mask_threshold_image_create(cpl_imagelist_get(flat, 0),
00283                                           visir_img_ff_config.bpm_lo_thresh,
00284                                           visir_img_ff_config.bpm_hi_thresh);
00285     if (bpm == NULL) {
00286         cpl_msg_error(__func__, "Could not compute the bad pixel map");
00287         skip_if(1);
00288     }
00289     skip_if (cpl_mask_not(bpm));
00290     visir_img_ff_config.bpm_nb_bad = cpl_mask_count(bpm);
00291     bpm_int = cpl_image_new_from_mask(bpm);
00292     
00293     /* The values <bpm_lo_thresh and >bpm_hi_thresh are set to 1 */
00294     skip_if (cpl_image_threshold(cpl_imagelist_get(flat, 0), 
00295             visir_img_ff_config.bpm_lo_thresh,
00296             visir_img_ff_config.bpm_hi_thresh, 
00297             1.0, 1.0));
00298     
00299     /* Save the products */
00300     cpl_msg_info(__func__, "Saving the results");
00301     if (cpl_error_get_code()) {
00302       cpl_msg_warning(__func__, "'%s' in %s",
00303                       cpl_error_get_message(), cpl_error_get_where());
00304       skip_if(1);
00305     }
00306     if (visir_img_ff_save(flat, bpm_int, parlist, framelist)) {
00307         cpl_msg_error(__func__, "Could not save products: '%s' in %s",
00308                       cpl_error_get_message(), cpl_error_get_where());
00309         skip_if(1);
00310     }
00311 
00312     end_skip;
00313 
00314     cpl_frameset_delete(rawframes);
00315     cpl_imagelist_delete(flat);
00316     cpl_mask_delete(bpm);
00317     cpl_image_delete(bpm_int);
00318 
00319     return cpl_error_get_code();
00320 }
00321 
00322 /*----------------------------------------------------------------------------*/
00328 /*----------------------------------------------------------------------------*/
00329 static cpl_imagelist * visir_img_ff_reduce(const cpl_frameset * rawframes)
00330 {
00331     cpl_imagelist * iset = NULL;
00332     cpl_imagelist * fitted = NULL;
00333 
00334 
00335     skip_if (cpl_error_get_code());
00336 
00337     skip_if (rawframes == NULL);
00338 
00339     /* Load the image set */
00340     iset = cpl_imagelist_load_frameset(rawframes, CPL_TYPE_FLOAT, 1, 0);
00341     skip_if (iset == NULL);
00342 
00343     /* Apply the Fit and compute the gain, intercept map and error */
00344     fitted = irplib_flat_fit_set(iset, 1);
00345 
00346     skip_if (fitted == NULL);
00347 
00348     end_skip;
00349 
00350     cpl_imagelist_delete(iset);
00351 
00352     return fitted;
00353 }
00354 
00355 /*----------------------------------------------------------------------------*/
00364 /*----------------------------------------------------------------------------*/
00365 
00366 static int visir_img_ff_save(
00367         const cpl_imagelist     * flat,
00368         const cpl_image         * bpm,
00369         const cpl_parameterlist * parlist,
00370         cpl_frameset            * set)
00371 {
00372     /* The filename of the reference frame */
00373     const char       * ref_file =
00374         cpl_frame_get_filename(irplib_frameset_get_first_from_group(set,
00375                                                          CPL_FRAME_GROUP_RAW));
00376     cpl_propertylist * plist = cpl_propertylist_load(ref_file, 0);
00377     cpl_propertylist * qclist = cpl_propertylist_new();
00378     const char       * capa = visir_get_capa(plist);
00379     const char       * procatg_ff;
00380     const char       * procatg_bpm;
00381     FILE             * paf = NULL;
00382 
00383 
00384     /* This will catch ref_file == NULL or plist == NULL */
00385     skip_if (cpl_error_get_code());
00386 
00387     skip_if (cpl_propertylist_copy_property_regexp(qclist, plist,
00388                               (visir_img_ff_config.img_mode & visir_ff_mode_spc)
00389                             ? pafcopy "|" paf_spc : pafcopy "|" paf_img));
00390 
00391     cpl_propertylist_empty(plist); /* Just to reduce number of pointers */
00392 
00393     /* THE PAF FILE FOR QC PARAMETERS */
00394     skip_if (!(paf = visir_paf_init(recipename)));
00395 
00396     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00397 
00398     /* Reuse the propertylist for QC parameters */
00399     cpl_propertylist_empty(qclist);
00400 
00401     /* QC.CAPA */
00402     skip_if (capa != NULL && 
00403              cpl_propertylist_append_string(qclist, "ESO QC CAPA", capa));
00404 
00405     switch (visir_img_ff_config.img_mode) {
00406     case visir_ff_img:
00407         procatg_ff  = VISIR_IMG_FF_PROCATG;
00408         procatg_bpm = VISIR_IMG_FF_BPM_PROCATG;
00409         break;
00410     case visir_ff_spc:
00411         procatg_ff  = VISIR_SPC_FF_PROCATG;
00412         procatg_bpm = VISIR_SPC_FF_BPM_PROCATG;
00413         break;
00414     case visir_ff_img_tech:
00415         procatg_ff  = VISIR_IMG_TECH_FF_PROCATG;
00416         procatg_bpm = VISIR_IMG_TECH_FF_BPM_PROCATG;
00417         break;
00418     case visir_ff_spc_tech:
00419         procatg_ff  = VISIR_SPC_TECH_FF_PROCATG;
00420         procatg_bpm = VISIR_SPC_TECH_FF_BPM_PROCATG;
00421         break;
00422     default:
00423         assert (0);
00424     }
00425 
00426     /* SAVE THE FLAT IMAGE */
00427     skip_if (visir_image_save(parlist, set, cpl_imagelist_get(flat, 0),
00428                               recipename, procatg_ff, qclist, NULL));
00429 
00430     /* QC.NBBADPIX */
00431     skip_if(cpl_propertylist_append_int(qclist, "ESO QC NBBADPIX", 
00432                                         visir_img_ff_config.bpm_nb_bad));
00433 
00434     /* SAVE THE BPM IMAGE */
00435     skip_if (visir_image_save(parlist, set, bpm, recipename,
00436                               procatg_bpm, qclist, "_bpm"));
00437 
00438     /* PRO.CATG */
00439     skip_if (cpl_propertylist_append_string(qclist, "ESO PRO CATG", procatg_ff));
00440 
00441     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00442 
00443     end_skip;
00444 
00445     visir_paf_end(paf);
00446 
00447     cpl_propertylist_delete(qclist);
00448     cpl_propertylist_delete(plist);
00449 
00450     return cpl_error_get_code();
00451 
00452 }

Generated on Mon Jan 23 12:16:36 2006 for VISIR Pipeline Reference Manual by doxygen1.2.18