KMOS Pipeline Reference Manual  1.3.11
kmo_noise_map.c
00001 /*
00002  * This file is part of the KMOS Pipeline
00003  * Copyright (C) 2002,2003 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, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 #include <string.h>
00025 
00026 #include <cpl.h>
00027 
00028 #include "kmo_utils.h"
00029 #include "kmo_dfs.h"
00030 #include "kmo_error.h"
00031 #include "kmo_priv_noise_map.h"
00032 #include "kmo_constants.h"
00033 #include "kmo_debug.h"
00034 
00035 static int kmo_noise_map_create(cpl_plugin *);
00036 static int kmo_noise_map_exec(cpl_plugin *);
00037 static int kmo_noise_map_destroy(cpl_plugin *);
00038 static int kmo_noise_map(cpl_parameterlist *, cpl_frameset *);
00039 
00040 static char kmo_noise_map_description[] =
00041 "The noise in each pixel of the input data is estimated using gain and readnoise.\n"
00042 "The readnoise is expected to be in the primary header (ESO DET CHIP RON), the\n"
00043 "gain (ESO DET CHIP GAIN) has to be in each of the subsequent headers of each \n"
00044 "detector frame. The output is the initial noise map of the data frame.\n"
00045 "\n"
00046 "-------------------------------------------------------------------------------\n"
00047 "  Input files:\n"
00048 "\n"
00049 "   DO                    KMOS                                                  \n"
00050 "   category              Type   Explanation                    Required #Frames\n"
00051 "   --------              -----  -----------                    -------- -------\n"
00052 "   <none or any>         RAW    raw data frame                     Y       1   \n"
00053 "\n"
00054 "  Output files:\n"
00055 "\n"
00056 "   DO                    KMOS\n"
00057 "   category              Type   Explanation\n"
00058 "   --------              -----  -----------\n"
00059 "   NOISE_MAP             F2D    Initial noise map\n"
00060 "                                (6 Extensions, 3 data and 3 noise)\n"
00061 "-------------------------------------------------------------------------------\n"
00062 "\n";
00063 
00080 int cpl_plugin_get_info(cpl_pluginlist *list)
00081 {
00082     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00083     cpl_plugin *plugin = &recipe->interface;
00084 
00085     cpl_plugin_init(plugin,
00086                         CPL_PLUGIN_API,
00087                         KMOS_BINARY_VERSION,
00088                         CPL_PLUGIN_TYPE_RECIPE,
00089                         "kmo_noise_map",
00090                         "Generate a noise map from a raw frame",
00091                         kmo_noise_map_description,
00092                         "Alex Agudo Berbel",
00093                         "usd-help@eso.org",
00094                         kmos_get_license(),
00095                         kmo_noise_map_create,
00096                         kmo_noise_map_exec,
00097                         kmo_noise_map_destroy);
00098 
00099     cpl_pluginlist_append(list, plugin);
00100 
00101     return 0;
00102 }
00103 
00111 static int kmo_noise_map_create(cpl_plugin *plugin)
00112 {
00113     cpl_recipe *recipe;
00114 
00115     /* Check that the plugin is part of a valid recipe */
00116     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00117         recipe = (cpl_recipe *)plugin;
00118     else
00119         return -1;
00120 
00121     /* Create the parameters list in the cpl_recipe object */
00122     recipe->parameters = cpl_parameterlist_new();
00123 
00124     return 0;
00125 }
00126 
00132 static int kmo_noise_map_exec(cpl_plugin *plugin)
00133 {
00134     cpl_recipe  *recipe;
00135 
00136     /* Get the recipe out of the plugin */
00137     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00138         recipe = (cpl_recipe *)plugin;
00139     else return -1 ;
00140 
00141     return kmo_noise_map(recipe->parameters, recipe->frames);
00142 }
00143 
00149 static int kmo_noise_map_destroy(cpl_plugin *plugin)
00150 {
00151     cpl_recipe *recipe;
00152 
00153     /* Get the recipe out of the plugin */
00154     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00155         recipe = (cpl_recipe *)plugin;
00156     else return -1 ;
00157 
00158     cpl_parameterlist_delete(recipe->parameters);
00159     return 0 ;
00160 }
00161 
00180 static int kmo_noise_map(cpl_parameterlist *parlist, cpl_frameset *frameset)
00181 {
00182     int                 i                   = 0,
00183                         ret_val             = 0,
00184                         ndsamples           = 0;
00185     cpl_propertylist    *sub_header         = NULL,
00186                         *main_header        = NULL;
00187     cpl_image           *img                = NULL,
00188                         *noise_img          = NULL;
00189     double              gain                = 0.0,
00190                         readnoise           = 0.0;
00191     main_fits_desc      desc;
00192     cpl_frame           *frame              = NULL;
00193     const char          *readmode           = NULL;
00194 
00195     KMO_TRY
00196     {
00197         kmo_init_fits_desc(&desc);
00198 
00199         /* --- check input --- */
00200         KMO_TRY_ASSURE((parlist != NULL) &&
00201                        (frameset != NULL),
00202                        CPL_ERROR_NULL_INPUT,
00203                        "Not all input data is provided!");
00204 
00205         KMO_TRY_ASSURE(cpl_frameset_get_size(frameset) == 1,
00206                        CPL_ERROR_NULL_INPUT,
00207                        "A fits-file must be provided!");
00208 
00209         KMO_TRY_EXIT_IF_NULL(
00210             frame = kmo_dfs_get_frame(frameset, "0"));
00211 
00212         desc = kmo_identify_fits_header(
00213                         cpl_frame_get_filename(frame));
00214         KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
00215                                       "in KMOS-format!");
00216 
00217         KMO_TRY_ASSURE((desc.fits_type == raw_fits),
00218                        CPL_ERROR_ILLEGAL_INPUT,
00219                        "Input data hasn't correct data type "
00220                        "(KMOSTYPE must be RAW)!");
00221 
00222         KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_noise_map") == 1,
00223                        CPL_ERROR_ILLEGAL_INPUT,
00224                        "Cannot identify RAW and CALIB frames!");
00225 
00226         cpl_msg_info("", "--- Parameter setup for kmo_noise_map ----");
00227 
00228         cpl_msg_info("", "No parameters to set.");
00229         cpl_msg_info("", "-------------------------------------------");
00230 
00231         /* --- save primary extension --- */
00232         KMO_TRY_EXIT_IF_ERROR(
00233             kmo_dfs_save_main_header(frameset, NOISE_MAP, "", frame,
00234                                      NULL, parlist, cpl_func));
00235 
00236         /* --- load each data-frame, save it, calculate noise,
00237                save it as well --- */
00238         for (i = 0; i < desc.nr_ext; i++)
00239         {
00240             /* load data and save it away again */
00241             KMO_TRY_EXIT_IF_NULL(
00242                 sub_header = kmo_dfs_load_sub_header(frameset, "0", i + 1,
00243                                                      FALSE));
00244 
00245             KMO_TRY_EXIT_IF_NULL(
00246                 img = kmo_dfs_load_image(frameset, "0", i + 1, FALSE, TRUE, NULL));
00247 
00248             KMO_TRY_EXIT_IF_ERROR(
00249                 kmo_update_sub_keywords(sub_header,
00250                                         FALSE,
00251                                         FALSE,
00252                                         desc.frame_type,
00253                                         desc.sub_desc[i].device_nr));
00254 
00255             KMO_TRY_EXIT_IF_ERROR(
00256                 kmo_dfs_save_image(img, NOISE_MAP, "", sub_header, 0./0.));
00257 
00258             /* calculate initial noise estimate */
00259             KMO_TRY_EXIT_IF_NULL(
00260                 main_header = kmo_dfs_load_primary_header(frameset, "0"));
00261 
00262             readmode = cpl_propertylist_get_string(main_header, READMODE);
00263             KMO_TRY_CHECK_ERROR_STATE("ESO DET READ CURNAME keyword in main "
00264                                       "header missing!");
00265             gain = kmo_dfs_get_property_double(sub_header, GAIN);
00266             KMO_TRY_CHECK_ERROR_STATE_MSG(
00267                 "GAIN-keyword in fits-header is missing!");
00268 
00269             if (strcmp(readmode, "Nondest") == 0) {
00270                 // NDR: non-destructive readout mode
00271                 ndsamples = cpl_propertylist_get_int(main_header, NDSAMPLES);
00272                 KMO_TRY_CHECK_ERROR_STATE("ESO DET READ NDSAMPLES keyword in main "
00273                                           "header missing!");
00274 
00275                 readnoise = kmo_calc_readnoise_ndr(ndsamples);
00276                 KMO_TRY_CHECK_ERROR_STATE();
00277             } else {
00278                 // normal readout mode
00279                 readnoise = kmo_dfs_get_property_double(sub_header, RON);
00280                 KMO_TRY_CHECK_ERROR_STATE_MSG(
00281                     "READNOISE-keyword in fits-header is missing!");
00282 
00283             }
00284             KMO_TRY_EXIT_IF_NULL(
00285                 noise_img = kmo_calc_noise_map(img, gain, readnoise));
00286 
00287             cpl_propertylist_delete(main_header); main_header = NULL;
00288 
00289             /* save noise */
00290             KMO_TRY_EXIT_IF_ERROR(
00291                 kmo_update_sub_keywords(sub_header,
00292                                         TRUE,
00293                                         FALSE,
00294                                         desc.frame_type,
00295                                         desc.sub_desc[i].device_nr));
00296 
00297             KMO_TRY_EXIT_IF_ERROR(
00298                 kmo_dfs_save_image(noise_img, NOISE_MAP, "", sub_header, 0./0.));
00299 
00300             cpl_propertylist_delete(sub_header); sub_header = NULL;
00301             cpl_image_delete(img); img = NULL;
00302             cpl_image_delete(noise_img); noise_img= NULL;
00303         }
00304     }
00305     KMO_CATCH
00306     {
00307         KMO_CATCH_MSG();
00308 
00309         ret_val = -1;
00310     }
00311 
00312     cpl_propertylist_delete(sub_header); sub_header = NULL;
00313     cpl_image_delete(img); img = NULL;
00314     cpl_image_delete(noise_img); noise_img = NULL;
00315     kmo_free_fits_desc(&desc);
00316     return ret_val;
00317 }
00318