CRIRES Pipeline Reference Manual 2.3.17
crires_util_sensitivity.c
1/* $Id: crires_util_sensitivity.c,v 1.32 2012-09-20 12:15:03 yjung Exp $
2 *
3 * This file is part of the crires Pipeline
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: yjung $
23 * $Date: 2012-09-20 12:15:03 $
24 * $Revision: 1.32 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include "crires_recipe.h"
37
38#include "crires_photom.h"
39#include "irplib_utils.h"
40
41/*-----------------------------------------------------------------------------
42 Define
43 -----------------------------------------------------------------------------*/
44
45#define RECIPE_STRING "crires_util_sensitivity"
46
47/*-----------------------------------------------------------------------------
48 Functions prototypes
49 -----------------------------------------------------------------------------*/
50
51static int crires_util_sensitivity_save(const cpl_table **,
52 const cpl_parameterlist *, cpl_frameset *) ;
53
54static char crires_util_sensitivity_description[] =
55"This recipe accepts 1 parameter:\n"
56"First parameter: the table with the conversion factor.\n"
57" (PRO TYPE = "CRIRES_PROTYPE_CONVERS")\n"
58"\n"
59"This recipe produces 1 file:\n"
60"First product: the table with the sensitivity signal.\n"
61" (PRO TYPE = "CRIRES_PROTYPE_SENSIT")\n" ;
62
63CRIRES_RECIPE_DEFINE(crires_util_sensitivity,
64 CRIRES_PARAM_DISPLAY |
65 CRIRES_PARAM_EXPTIME,
66 "Compute the sensitivity",
67 crires_util_sensitivity_description) ;
68
69/*-----------------------------------------------------------------------------
70 Static variables
71 -----------------------------------------------------------------------------*/
72
73static struct {
74 /* Inputs */
75 int display ;
76 double exptime ;
77 /* Outputs */
78 int win_mode ;
79 double sens_med[CRIRES_NB_DETECTORS] ;
80} crires_util_sensitivity_config ;
81
82/*-----------------------------------------------------------------------------
83 Functions code
84 -----------------------------------------------------------------------------*/
85
86/*----------------------------------------------------------------------------*/
93/*----------------------------------------------------------------------------*/
94static int crires_util_sensitivity(
95 cpl_frameset * frameset,
96 const cpl_parameterlist * parlist)
97{
98 const char * sval ;
99 cpl_propertylist * plist ;
100 cpl_frame * fr ;
101 cpl_table * conv_tab[CRIRES_NB_DETECTORS] ;
102 double exptime_comp ;
103 int i ;
104
105 /* Initialise */
106 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
107 crires_util_sensitivity_config.sens_med[i] = -1.0 ;
108 conv_tab[i] = NULL ;
109 }
110
111 /* Retrieve input parameters */
112 crires_util_sensitivity_config.display = crires_parameterlist_get_int(
113 parlist, RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
114 crires_util_sensitivity_config.exptime = crires_parameterlist_get_double(
115 parlist, RECIPE_STRING, CRIRES_PARAM_EXPTIME) ;
116
117 /* Identify the RAW and CALIB frames in the input frameset */
118 if (crires_dfs_set_groups(frameset, NULL)) {
119 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
120 return -1 ;
121 }
122
123 /* Windowing mode ? */
124 fr = cpl_frameset_get_position(frameset, 0);
125 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
126 return -1 ;
127 sval = crires_pfits_get_ncorrs(plist) ;
128 if (!strcmp(sval, "FowlerNsampGRstWin")) {
129 crires_util_sensitivity_config.win_mode = 1 ;
130 } else {
131 crires_util_sensitivity_config.win_mode = 0 ;
132 }
133 cpl_propertylist_delete(plist) ;
134
135 /* Loop on the CRIRES_NB_DETECTORS detectors */
136 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
137 /* Skip some detectors in windowing mode */
138 if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
139 (crires_util_sensitivity_config.win_mode == 1)) {
140 continue ;
141 }
142
143 cpl_msg_info(__func__, "Sensitivity computation for chip %d", i+1) ;
144
145 /* The first frame must be the conversion table */
146 cpl_msg_info(__func__, "Load the extracted table") ;
147 cpl_msg_indent_more() ;
148 fr = cpl_frameset_get_position(frameset, 0);
149 if ((conv_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
150 i+1, CRIRES_PROTYPE_CONVERS, -1, -1, 0)) == NULL) {
151 cpl_msg_indent_less() ;
152 continue ;
153 }
154 cpl_msg_indent_less() ;
155
156 /* Compute */
157 cpl_msg_info(__func__, "Sensitivity computation") ;
158 cpl_msg_indent_more() ;
159 if ((exptime_comp = crires_photom_sens_engine(conv_tab[i],
160 cpl_frame_get_filename(fr),
161 crires_util_sensitivity_config.exptime,
162 crires_util_sensitivity_config.display==i+1)) < 0.0 ) {
163 cpl_msg_error(__func__, "Cannot compute sensitivity") ;
164 cpl_msg_indent_less() ;
165 cpl_table_delete(conv_tab[i]) ;
166 conv_tab[i] = NULL ;
167 continue ;
168 }
169 cpl_msg_indent_less() ;
170 crires_util_sensitivity_config.exptime = exptime_comp ;
171
172 /* Compute the Median */
173 crires_util_sensitivity_config.sens_med[i] =
174 cpl_table_get_column_median(conv_tab[i], CRIRES_COL_SENSITIVITY) ;
175 }
176
177 /* Reconstruct chips in windowing mode using detector 2 */
178 if (crires_util_sensitivity_config.win_mode == 1) {
179 if (conv_tab[1] != NULL) {
180 conv_tab[0] = cpl_table_duplicate(conv_tab[1]) ;
181 cpl_table_set_size(conv_tab[0], 0) ;
182 conv_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(conv_tab[0]) ;
183 }
184 }
185
186 /* Save the product */
187 cpl_msg_info(__func__, "Save the product") ;
188 cpl_msg_indent_more() ;
189 if (crires_util_sensitivity_save((const cpl_table **)conv_tab, parlist,
190 frameset)) {
191 cpl_msg_error(__func__, "Cannot save the product") ;
192 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
193 if (conv_tab[i] != NULL) cpl_table_delete(conv_tab[i]) ;
194 }
195 cpl_msg_indent_less() ;
196 return -1 ;
197 }
198 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
199 if (conv_tab[i] != NULL) cpl_table_delete(conv_tab[i]) ;
200 }
201 cpl_msg_indent_less() ;
202
203 /* Return */
204 if (cpl_error_get_code()) return -1 ;
205 else return 0 ;
206}
207
208/*----------------------------------------------------------------------------*/
216/*----------------------------------------------------------------------------*/
217static int crires_util_sensitivity_save(
218 const cpl_table ** out,
219 const cpl_parameterlist * parlist,
220 cpl_frameset * set)
221{
222 cpl_propertylist ** qclists ;
223 const cpl_frame * ref_frame ;
224 cpl_propertylist * inputlist ;
225 const char * recipe_name = "crires_util_sensitivity" ;
226 int i ;
227
228 /* Get the reference frame */
229 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
230
231 /* Create the QC lists */
232 qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
233 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
234 qclists[i] = cpl_propertylist_new() ;
235 cpl_propertylist_append_double(qclists[i], "ESO QC SENSMED",
236 crires_util_sensitivity_config.sens_med[i]) ;
237 cpl_propertylist_append_double(qclists[i], "ESO QC EXPTIME",
238 crires_util_sensitivity_config.exptime) ;
239
240 /* Propagate some keywords from input raw frame extensions */
241 inputlist = cpl_propertylist_load_regexp(
242 cpl_frame_get_filename(ref_frame), i+1,
243 CRIRES_HEADER_EXT_FORWARD, 0) ;
244 cpl_propertylist_copy_property_regexp(qclists[i], inputlist,
245 CRIRES_HEADER_EXT_FORWARD, 0) ;
246 cpl_propertylist_delete(inputlist) ;
247 }
248
249 /* Write the table */
250 crires_table_save(set,
251 parlist,
252 set,
253 out,
254 recipe_name,
255 CRIRES_EXTRACT_SENS_TAB,
256 CRIRES_PROTYPE_SENSIT,
257 NULL,
258 (const cpl_propertylist **)qclists,
259 PACKAGE "/" PACKAGE_VERSION,
260 "crires_util_sensitivity.fits") ;
261
262 /* Free and return */
263 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
264 cpl_propertylist_delete(qclists[i]) ;
265 }
266 cpl_free(qclists) ;
267 return 0;
268}
269