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