CRIRES Pipeline Reference Manual
2.3.2
|
00001 /* $Id: crires_win_dark.c,v 1.4 2012-09-19 14:52:00 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: 2012-09-19 14:52:00 $ 00024 * $Revision: 1.4 $ 00025 * $Name: not supported by cvs2svn $ 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 * dark[CRIRES_NB_DETECTORS] ; 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 dark[i] = NULL ; 00114 } 00115 00116 /* Retrieve input parameters */ 00117 crires_win_dark_config.hsize = crires_parameterlist_get_int(parlist, 00118 RECIPE_STRING, CRIRES_PARAM_RON_SZ) ; 00119 crires_win_dark_config.nsamples = crires_parameterlist_get_int(parlist, 00120 RECIPE_STRING, CRIRES_PARAM_RON_SAMPLES) ; 00121 00122 /* Identify the RAW and CALIB frames in the input frameset */ 00123 if (crires_dfs_set_groups(frameset, "crires_win_dark")) { 00124 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ; 00125 return -1 ; 00126 } 00127 00128 /* Retrieve raw frames */ 00129 if ((rawframes = crires_extract_frameset(frameset, 00130 CRIRES_WIN_DARK_RAW)) == NULL) { 00131 cpl_msg_error(__func__, "No raw frame in input") ; 00132 return -1 ; 00133 } 00134 00135 /* At least 3 frames */ 00136 if (cpl_frameset_get_size(rawframes) < 3) { 00137 cpl_msg_error(__func__, "Not enough input frames"); 00138 cpl_frameset_delete(rawframes) ; 00139 return -1 ; 00140 } 00141 00142 /* Get DIT / NDIT from the header */ 00143 ref_frame = cpl_frameset_get_position(rawframes, 0) ; 00144 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame), 00145 0)) == NULL) { 00146 cpl_msg_error(__func__, "Cannot get header from frame"); 00147 cpl_msg_indent_less() ; 00148 cpl_frameset_delete(rawframes) ; 00149 return -1 ; 00150 } 00151 dit = crires_pfits_get_dit(plist) ; 00152 ndit = crires_pfits_get_ndit(plist) ; 00153 cpl_propertylist_delete(plist) ; 00154 if (cpl_error_get_code() != CPL_ERROR_NONE) { 00155 cpl_msg_error(__func__, "Cannot get the DIT/NDIT from the header") ; 00156 cpl_msg_indent_less() ; 00157 cpl_frameset_delete(rawframes) ; 00158 return -1 ; 00159 } 00160 cpl_msg_info(__func__, "DIT value: %g sec.", dit) ; 00161 cpl_msg_info(__func__, "NDIT value: %d", ndit) ; 00162 00163 /* Loop on the chips */ 00164 cpl_msg_info(__func__, "Dark computation") ; 00165 cpl_msg_indent_more() ; 00166 for (i=1 ; i<CRIRES_NB_DETECTORS-1 ; 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_frameset_delete(rawframes) ; 00176 for (j=1 ; j<i ; j++) 00177 cpl_image_delete(dark[j]) ; 00178 return -1 ; 00179 } 00180 /* Compute the current dark */ 00181 cpl_msg_info(__func__, "Collapse images for chip number %d", i+1) ; 00182 if ((dark[i] = cpl_imagelist_collapse_create(darks_chip)) == NULL) { 00183 cpl_msg_error(__func__, "Cannot average for chip number %d", i+1) ; 00184 cpl_msg_indent_less() ; 00185 cpl_frameset_delete(rawframes) ; 00186 for (j=1 ; j<i ; j++) 00187 cpl_image_delete(dark[j]) ; 00188 cpl_imagelist_delete(darks_chip) ; 00189 return -1 ; 00190 } 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 /* Reconstruct chips (windowing mode) using detector 2 */ 00217 if (dark[1] != NULL) { 00218 dark[0] = cpl_image_duplicate(dark[1]) ; 00219 cpl_image_multiply_scalar(dark[0], 0.0) ; 00220 dark[CRIRES_NB_DETECTORS-1] = cpl_image_duplicate(dark[0]) ; 00221 } 00222 00223 /* Reconstruct the darks imagelist */ 00224 darks = cpl_imagelist_new() ; 00225 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) 00226 cpl_imagelist_set(darks, dark[i], i) ; 00227 00228 /* Divide by DIT */ 00229 cpl_msg_info(__func__, "Division by DIT") ; 00230 cpl_imagelist_divide_scalar(darks, dit) ; 00231 00232 /* Save the product */ 00233 cpl_msg_info(__func__, "Save the product") ; 00234 cpl_msg_indent_more() ; 00235 if (crires_win_dark_save(darks, parlist, frameset)) { 00236 cpl_msg_error(__func__, "Cannot save the product") ; 00237 cpl_imagelist_delete(darks) ; 00238 cpl_msg_indent_less() ; 00239 return -1 ; 00240 } 00241 cpl_imagelist_delete(darks) ; 00242 cpl_msg_indent_less() ; 00243 00244 /* Return */ 00245 if (cpl_error_get_code()) return -1 ; 00246 else return 0 ; 00247 } 00248 00249 /*----------------------------------------------------------------------------*/ 00257 /*----------------------------------------------------------------------------*/ 00258 static double crires_win_dark_ron( 00259 const cpl_image * ima1, 00260 const cpl_image * ima2, 00261 int ndit) 00262 { 00263 cpl_image * ima ; 00264 double norm ; 00265 double ron ; 00266 00267 /* Test entries */ 00268 if (ima1 == NULL) return -1.0 ; 00269 if (ima2 == NULL) return -1.0 ; 00270 if (ndit < 1) return -1.0 ; 00271 00272 /* Compute norm */ 00273 norm = 0.5 * ndit ; 00274 norm = sqrt(norm) ; 00275 00276 /* Subtraction */ 00277 if ((ima = cpl_image_subtract_create(ima2, ima1)) == NULL) return -1.0 ; 00278 00279 /* RON measurement */ 00280 cpl_flux_get_noise_window(ima, NULL, crires_win_dark_config.hsize, 00281 crires_win_dark_config.nsamples, &ron, NULL) ; 00282 cpl_image_delete(ima) ; 00283 return norm*ron ; 00284 } 00285 00286 /*----------------------------------------------------------------------------*/ 00294 /*----------------------------------------------------------------------------*/ 00295 static int crires_win_dark_save( 00296 const cpl_imagelist * dark, 00297 const cpl_parameterlist * parlist, 00298 cpl_frameset * set) 00299 { 00300 cpl_propertylist ** qclists ; 00301 const cpl_frame * ref_frame ; 00302 cpl_propertylist * inputlist ; 00303 const char * recipe_name = "crires_win_dark" ; 00304 int i ; 00305 00306 /* Get the reference frame */ 00307 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ; 00308 00309 /* Create the QC lists */ 00310 qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ; 00311 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) { 00312 qclists[i] = cpl_propertylist_new() ; 00313 cpl_propertylist_append_double(qclists[i], "ESO QC RON1", 00314 crires_win_dark_config.ron1[i]) ; 00315 cpl_propertylist_append_double(qclists[i], "ESO QC RON2", 00316 crires_win_dark_config.ron2[i]) ; 00317 cpl_propertylist_append_double(qclists[i], "ESO QC DARKMED", 00318 crires_win_dark_config.dark_med[i]) ; 00319 cpl_propertylist_append_double(qclists[i], "ESO QC DARKSTDEV", 00320 crires_win_dark_config.dark_stdev[i]) ; 00321 00322 /* Propagate some keywords from input raw frame extensions */ 00323 inputlist = cpl_propertylist_load_regexp( 00324 cpl_frame_get_filename(ref_frame), i+1, 00325 CRIRES_HEADER_EXT_FORWARD, 0) ; 00326 cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 00327 CRIRES_HEADER_EXT_FORWARD, 0) ; 00328 cpl_propertylist_delete(inputlist) ; 00329 } 00330 00331 /* Write the combined image */ 00332 crires_image_save(set, 00333 parlist, 00334 set, 00335 dark, 00336 recipe_name, 00337 CRIRES_CALPRO_DARK_WIN, 00338 CRIRES_PROTYPE_DARK, 00339 CRIRES_ILLUM_FULL_DETECTOR, 00340 NULL, 00341 (const cpl_propertylist**)qclists, 00342 PACKAGE "/" PACKAGE_VERSION, 00343 "crires_win_dark.fits") ; 00344 00345 /* Remove the keywords for the FITS extensions */ 00346 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) { 00347 cpl_propertylist_erase_regexp(qclists[i], CRIRES_HEADER_EXT_FORWARD, 0); 00348 } 00349 00350 /* Free and return */ 00351 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) { 00352 cpl_propertylist_delete(qclists[i]) ; 00353 } 00354 cpl_free(qclists) ; 00355 return 0; 00356 } 00357