crires_win_dark.c

00001 /* $Id: crires_win_dark.c,v 1.3 2010/04/23 08:58:43 yjung Exp $
00002  *
00003  * This file is part of the CRIRES 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: yjung $
00023  * $Date: 2010/04/23 08:58:43 $
00024  * $Revision: 1.3 $
00025  * $Name: crire-2_1_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "crires_recipe.h"
00037 
00038 /*-----------------------------------------------------------------------------
00039                                 Define
00040  -----------------------------------------------------------------------------*/
00041 
00042 #define RECIPE_STRING "crires_win_dark"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static double crires_win_dark_ron(const cpl_image *, const cpl_image *, int) ;
00049 static int crires_win_dark_save(const cpl_imagelist *, 
00050         const cpl_parameterlist *, cpl_frameset *) ;
00051 
00052 static char crires_win_dark_description[] =
00053 "crires_win_dark -- Dark recipe in Windowing Mode\n"
00054 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00055 "raw-file.fits "CRIRES_WIN_DARK_RAW".\n" ;
00056 
00057 CRIRES_RECIPE_DEFINE(crires_win_dark,
00058         CRIRES_PARAM_RON_SAMPLES    |
00059         CRIRES_PARAM_RON_SZ,
00060         "Dark recipe in Windowing Mode",
00061         crires_win_dark_description) ;
00062 
00063 /*-----------------------------------------------------------------------------
00064                             Static variables
00065  -----------------------------------------------------------------------------*/
00066 
00067 static struct {
00068     /* Inputs */
00069     int                 hsize ;
00070     int                 nsamples ;
00071     /* Outputs */
00072     double              dark_med[CRIRES_NB_DETECTORS] ;
00073     double              dark_stdev[CRIRES_NB_DETECTORS] ;
00074     double              ron1[CRIRES_NB_DETECTORS] ;
00075     double              ron2[CRIRES_NB_DETECTORS] ;
00076 } crires_win_dark_config ;
00077 
00078 /*-----------------------------------------------------------------------------
00079                                 Functions code
00080  -----------------------------------------------------------------------------*/
00081 
00082 /*----------------------------------------------------------------------------*/
00089 /*----------------------------------------------------------------------------*/
00090 static int crires_win_dark(
00091         cpl_frameset            *   frameset,
00092         const cpl_parameterlist *   parlist)
00093 {
00094     cpl_frameset        *   rawframes ;
00095     cpl_frame           *   ref_frame ;
00096     cpl_propertylist    *   plist ;
00097     double                  dit ;
00098     int                     ndit ;
00099     cpl_imagelist       *   darks ;
00100     cpl_imagelist       *   darks_chip ;
00101     cpl_image           *   tmp_dark ;
00102     cpl_vector          *   medians ;
00103     double                  med ;
00104     int                     i, j ;
00105 
00106     /* Initialise */
00107     rawframes = NULL ;
00108     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00109         crires_win_dark_config.ron1[i] = -1.0 ;
00110         crires_win_dark_config.ron2[i] = -1.0 ;
00111         crires_win_dark_config.dark_med[i] = -1.0 ;
00112         crires_win_dark_config.dark_stdev[i] = -1.0 ;
00113     }
00114 
00115     /* Retrieve input parameters */
00116     crires_win_dark_config.hsize = crires_parameterlist_get_int(parlist,
00117             RECIPE_STRING, CRIRES_PARAM_RON_SZ) ;
00118     crires_win_dark_config.nsamples = crires_parameterlist_get_int(parlist, 
00119             RECIPE_STRING, CRIRES_PARAM_RON_SAMPLES) ;
00120  
00121     /* Identify the RAW and CALIB frames in the input frameset */
00122     if (crires_dfs_set_groups(frameset, "crires_win_dark")) {
00123         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00124         return -1 ;
00125     }
00126 
00127     /* Retrieve raw frames */
00128     if ((rawframes = crires_extract_frameset(frameset, 
00129                     CRIRES_WIN_DARK_RAW)) == NULL) {
00130         cpl_msg_error(__func__, "No raw frame in input") ;
00131         return -1 ;
00132     }
00133 
00134     /* At least 3 frames */
00135     if (cpl_frameset_get_size(rawframes) < 3) {
00136         cpl_msg_error(__func__, "Not enough input frames");
00137         cpl_frameset_delete(rawframes) ;
00138         return -1 ;
00139     }
00140 
00141     /* Get DIT / NDIT from the header */
00142     ref_frame = cpl_frameset_get_frame(rawframes, 0) ;
00143     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
00144                     0)) == NULL) {
00145         cpl_msg_error(__func__, "Cannot get header from frame");
00146         cpl_msg_indent_less() ;
00147         cpl_frameset_delete(rawframes) ;
00148         return -1 ;
00149     }
00150     dit = crires_pfits_get_dit(plist) ;
00151     ndit = crires_pfits_get_ndit(plist) ;
00152     cpl_propertylist_delete(plist) ;
00153     if (cpl_error_get_code() != CPL_ERROR_NONE) {
00154         cpl_msg_error(__func__, "Cannot get the DIT/NDIT from the header") ;
00155         cpl_msg_indent_less() ;
00156         cpl_frameset_delete(rawframes) ;
00157         return -1 ;
00158     }
00159     cpl_msg_info(__func__, "DIT value: %g sec.", dit) ;
00160     cpl_msg_info(__func__, "NDIT value: %d", ndit) ;
00161 
00162     /* Loop on the chips */
00163     darks = cpl_imagelist_new() ;
00164     cpl_msg_info(__func__, "Dark computation") ;
00165     cpl_msg_indent_more() ;
00166     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00167         
00168         /* Load the chips */
00169         cpl_msg_info(__func__, "Load chip number %d", i+1) ;
00170         if ((darks_chip = crires_load_frameset(rawframes, 
00171                         CRIRES_ILLUM_FULL_DETECTOR, i+1, 
00172                         CPL_TYPE_FLOAT)) == NULL) {
00173             cpl_msg_error(__func__, "Cannot load chip number %d", i+1) ;
00174             cpl_msg_indent_less() ;
00175             cpl_imagelist_delete(darks) ;
00176             cpl_frameset_delete(rawframes) ;
00177             return -1 ;
00178         }
00179         /* Compute the current dark */
00180         cpl_msg_info(__func__, "Collapse images for chip number %d", i+1) ;
00181         if ((tmp_dark = cpl_imagelist_collapse_create(darks_chip)) == NULL) {
00182             cpl_msg_error(__func__, "Cannot average for chip number %d", i+1) ;
00183             cpl_msg_indent_less() ;
00184             cpl_imagelist_delete(darks) ;
00185             cpl_frameset_delete(rawframes) ;
00186             cpl_imagelist_delete(darks_chip) ;
00187             return -1 ;
00188         }
00189         /* Put the result in the image list */
00190         cpl_imagelist_set(darks, tmp_dark, i) ;
00191         
00192         /* Compute the dark_med and stdev */
00193         medians = cpl_vector_new(cpl_imagelist_get_size(darks_chip));
00194         for (j=0 ; j<cpl_imagelist_get_size(darks_chip) ; j++) {
00195             med = cpl_image_get_median(cpl_imagelist_get(darks_chip, j)) ;
00196             cpl_vector_set(medians, j, med) ;
00197         }
00198         crires_win_dark_config.dark_med[i] = cpl_vector_get_mean(medians) ;
00199         crires_win_dark_config.dark_stdev[i] = cpl_vector_get_stdev(medians) ;
00200         cpl_vector_delete(medians) ;
00201         
00202         /* Compute the RONs */
00203         crires_win_dark_config.ron1[i] = crires_win_dark_ron(
00204                 cpl_imagelist_get(darks_chip, 0),
00205                 cpl_imagelist_get(darks_chip, 1), 
00206                 ndit) ;
00207         crires_win_dark_config.ron2[i] = crires_win_dark_ron(
00208                 cpl_imagelist_get(darks_chip, 1),
00209                 cpl_imagelist_get(darks_chip, 2), 
00210                 ndit) ;
00211         cpl_imagelist_delete(darks_chip) ;
00212     }
00213     cpl_frameset_delete(rawframes) ;
00214     cpl_msg_indent_less() ;
00215 
00216     /* Divide by DIT */
00217     cpl_msg_info(__func__, "Division by DIT") ;
00218     cpl_imagelist_divide_scalar(darks, dit) ;
00219     
00220     /* Save the product */
00221     cpl_msg_info(__func__, "Save the product") ;
00222     cpl_msg_indent_more() ;
00223     if (crires_win_dark_save(darks, parlist, frameset)) {
00224         cpl_msg_error(__func__, "Cannot save the product") ;
00225         cpl_imagelist_delete(darks) ;
00226         cpl_msg_indent_less() ;
00227         return -1 ;
00228     }
00229     cpl_imagelist_delete(darks) ;
00230     cpl_msg_indent_less() ;
00231 
00232     /* Return */
00233     if (cpl_error_get_code()) return -1 ;
00234     else return 0 ;
00235 }
00236 
00237 /*----------------------------------------------------------------------------*/
00245 /*----------------------------------------------------------------------------*/
00246 static double crires_win_dark_ron(
00247         const cpl_image     *   ima1, 
00248         const cpl_image     *   ima2, 
00249         int                     ndit) 
00250 {
00251     cpl_image       *   ima ;
00252     double              norm ;
00253     double              ron ;
00254 
00255     /* Test entries */
00256     if (ima1 == NULL)   return -1.0 ;
00257     if (ima2 == NULL)   return -1.0 ;
00258     if (ndit < 1)       return -1.0 ;
00259 
00260     /* Compute norm */
00261     norm = 0.5 * ndit ;
00262     norm = sqrt(norm) ;
00263 
00264     /* Subtraction */
00265     if ((ima = cpl_image_subtract_create(ima2, ima1)) == NULL) return -1.0 ;
00266    
00267     /* RON measurement */
00268     cpl_flux_get_noise_window(ima, NULL, crires_win_dark_config.hsize,
00269             crires_win_dark_config.nsamples, &ron, NULL) ;
00270     cpl_image_delete(ima) ;
00271     return norm*ron ;
00272 }
00273 
00274 /*----------------------------------------------------------------------------*/
00282 /*----------------------------------------------------------------------------*/
00283 static int crires_win_dark_save(
00284         const cpl_imagelist     *   dark,
00285         const cpl_parameterlist *   parlist,
00286         cpl_frameset            *   set)
00287 {
00288     cpl_propertylist    **  qclists ;
00289     const cpl_frame     *   ref_frame ;
00290     cpl_propertylist    *   inputlist ;
00291     const char          *   recipe_name = "crires_win_dark" ;
00292     int                     i ;
00293 
00294     /* Get the reference frame */
00295     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00296 
00297     /* Create the QC lists */
00298     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00299     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00300         qclists[i] = cpl_propertylist_new() ;
00301         cpl_propertylist_append_double(qclists[i], "ESO QC RON1",
00302                 crires_win_dark_config.ron1[i]) ;
00303         cpl_propertylist_append_double(qclists[i], "ESO QC RON2",
00304                 crires_win_dark_config.ron2[i]) ;
00305         cpl_propertylist_append_double(qclists[i], "ESO QC DARKMED",
00306                 crires_win_dark_config.dark_med[i]) ;
00307         cpl_propertylist_append_double(qclists[i], "ESO QC DARKSTDEV",
00308                 crires_win_dark_config.dark_stdev[i]) ;
00309 
00310         /* Propagate some keywords from input raw frame extensions */
00311         inputlist = cpl_propertylist_load_regexp(
00312                 cpl_frame_get_filename(ref_frame), i+1,
00313                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00314         cpl_propertylist_copy_property_regexp(qclists[i], inputlist,
00315                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00316         cpl_propertylist_delete(inputlist) ;
00317     }
00318 
00319     /* Write the combined image */
00320     crires_image_save(set,
00321             parlist,
00322             set, 
00323             dark, 
00324             recipe_name,
00325             CRIRES_CALPRO_DARK_WIN, 
00326             CRIRES_PROTYPE_DARK,
00327             CRIRES_ILLUM_FULL_DETECTOR,
00328             NULL,
00329             (const cpl_propertylist**)qclists,
00330             PACKAGE "/" PACKAGE_VERSION,
00331             "crires_win_dark.fits") ;
00332 
00333     /* Remove the keywords for the FITS extensions */
00334     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00335         cpl_propertylist_erase_regexp(qclists[i], CRIRES_HEADER_EXT_FORWARD, 0);
00336     }
00337 
00338     /* Free and return */
00339     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00340         cpl_propertylist_delete(qclists[i]) ;
00341     }
00342     cpl_free(qclists) ;
00343     return  0;
00344 }
00345 

Generated on 22 Mar 2011 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1