CRIRES Pipeline Reference Manual  2.3.15
crires_util_conversion.c
1 /* $Id: crires_util_conversion.c,v 1.37 2012-09-19 14:10:27 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-19 14:10:27 $
24  * $Revision: 1.37 $
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_conversion"
46 
47 /*-----------------------------------------------------------------------------
48  Functions prototypes
49  -----------------------------------------------------------------------------*/
50 
51 static int crires_util_conversion_save(const cpl_table **,
52  const cpl_parameterlist *, cpl_frameset *) ;
53 
54 static char crires_util_conversion_description[] =
55 "This recipe accepts 2 parameters:\n"
56 "First parameter: the table of the extracted spectrum in wavelength.\n"
57 " (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n"
58 "Second parameter: the table with the standard star photospheric flux.\n"
59 " (PRO TYPE = "CRIRES_PROTYPE_PHO_FLUX")\n"
60 "\n"
61 "This recipe produces 1 file:\n"
62 "First product: the table with the conversion signal.\n"
63 " (PRO TYPE = "CRIRES_PROTYPE_CONVERS")\n" ;
64 
65 CRIRES_RECIPE_DEFINE(crires_util_conversion,
66  CRIRES_PARAM_DISPLAY,
67  "Compute the conversion factor",
68  crires_util_conversion_description) ;
69 
70 /*-----------------------------------------------------------------------------
71  Static variables
72  -----------------------------------------------------------------------------*/
73 
74 static struct {
75  /* Inputs */
76  int display ;
77  /* Outputs */
78  int win_mode ;
79  double conv_med[CRIRES_NB_DETECTORS] ;
80  double thro_med[CRIRES_NB_DETECTORS] ;
81 } crires_util_conversion_config ;
82 
83 /*-----------------------------------------------------------------------------
84  Functions code
85  -----------------------------------------------------------------------------*/
86 
87 /*----------------------------------------------------------------------------*/
94 /*----------------------------------------------------------------------------*/
95 static int crires_util_conversion(
96  cpl_frameset * frameset,
97  const cpl_parameterlist * parlist)
98 {
99  cpl_frame * fr ;
100  const char * sval ;
101  cpl_propertylist * plist ;
102  cpl_table * ext_tab[CRIRES_NB_DETECTORS] ;
103  cpl_table * std_flux ;
104  cpl_bivector * std_biv ;
105  int i, j ;
106 
107  /* Initialise */
108  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
109  crires_util_conversion_config.conv_med[i] = -1.0 ;
110  crires_util_conversion_config.thro_med[i] = -1.0 ;
111  ext_tab[i] = NULL ;
112  }
113 
114  /* Retrieve input parameters */
115  crires_util_conversion_config.display = crires_parameterlist_get_int(
116  parlist, RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
117 
118  /* Identify the RAW and CALIB frames in the input frameset */
119  if (crires_dfs_set_groups(frameset, NULL)) {
120  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
121  return -1 ;
122  }
123 
124  /* The second frame must be the std star flux */
125  cpl_msg_info(__func__, "Second frame validity check") ;
126  cpl_msg_indent_more() ;
127  fr = cpl_frameset_get_position(frameset, 1);
128  if ((std_flux = crires_load_table_check(cpl_frame_get_filename(fr), 1,
129  CRIRES_PROTYPE_PHO_FLUX, -1, -1, 0)) == NULL) {
130  cpl_msg_error(__func__, "Second frame PRO TYPE must be %s",
131  CRIRES_PROTYPE_PHO_FLUX) ;
132  cpl_msg_indent_less() ;
133  return -1 ;
134  }
135  cpl_msg_indent_less() ;
136 
137  /* Get the requested star */
138  cpl_msg_info(__func__, "Standard star photospheric flux retrieval") ;
139  cpl_msg_indent_more() ;
140  if ((std_biv = crires_photom_conv_get_star(std_flux,
141  cpl_frame_get_filename(cpl_frameset_get_position(frameset, 0))))==NULL){
142  cpl_msg_error(__func__, "Cannot find the star") ;
143  cpl_msg_indent_less() ;
144  cpl_table_delete(std_flux) ;
145  return -1 ;
146  }
147  cpl_table_delete(std_flux) ;
148  cpl_msg_indent_less() ;
149 
150  /* Windowing mode ? */
151  fr = cpl_frameset_get_position(frameset, 0);
152  if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
153  return -1 ;
154  sval = crires_pfits_get_ncorrs(plist) ;
155  if (!strcmp(sval, "FowlerNsampGRstWin")) {
156  crires_util_conversion_config.win_mode = 1 ;
157  } else {
158  crires_util_conversion_config.win_mode = 0 ;
159  }
160  cpl_propertylist_delete(plist) ;
161 
162  /* Loop on the CRIRES_NB_DETECTORS detectors */
163  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
164  /* Skip some detectors in windowing mode */
165  if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
166  (crires_util_conversion_config.win_mode == 1)) {
167  continue ;
168  }
169 
170  cpl_msg_info(__func__, "Conversion factor for chip %d", i+1) ;
171 
172  /* The first frame must be the extracted spectrum in wavelength */
173  cpl_msg_info(__func__, "Load the extracted spectrum") ;
174  cpl_msg_indent_more() ;
175  fr = cpl_frameset_get_position(frameset, 0);
176  if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
177  i+1, CRIRES_PROTYPE_SPEC_WL, -1, -1, 0)) == NULL) {
178  cpl_msg_indent_less() ;
179  continue ;
180  }
181  cpl_msg_indent_less() ;
182 
183  /* Compute */
184  cpl_msg_info(__func__, "Conversion factor computation") ;
185  cpl_msg_indent_more() ;
186  if (crires_photom_conv_engine(ext_tab[i], std_biv, i+1,
187  crires_util_conversion_config.display==i+1) == -1) {
188  cpl_msg_error(__func__, "Cannot compute conversion factor") ;
189  cpl_msg_indent_less() ;
190  cpl_bivector_delete(std_biv) ;
191  for (j=0 ; j<i ; j++) {
192  if (ext_tab[j] != NULL) cpl_table_delete(ext_tab[j]) ;
193  }
194  return -1 ;
195  }
196  cpl_msg_indent_less() ;
197 
198  /* Compute the Median */
199  crires_util_conversion_config.conv_med[i] =
200  cpl_table_get_column_median(ext_tab[i], CRIRES_COL_CONVERSION_RECT);
201  crires_util_conversion_config.thro_med[i] =
202  cpl_table_get_column_median(ext_tab[i], CRIRES_COL_THROUGHPUT) ;
203  }
204  cpl_bivector_delete(std_biv) ;
205 
206  /* Reconstruct chips in windowing mode using detector 2 */
207  if (crires_util_conversion_config.win_mode == 1) {
208  if (ext_tab[1] != NULL) {
209  ext_tab[0] = cpl_table_duplicate(ext_tab[1]) ;
210  cpl_table_set_size(ext_tab[0], 0) ;
211  ext_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(ext_tab[0]) ;
212  }
213  }
214 
215  /* Save the product */
216  cpl_msg_info(__func__, "Save the product") ;
217  cpl_msg_indent_more() ;
218  if (crires_util_conversion_save((const cpl_table **)ext_tab, parlist,
219  frameset)) {
220  cpl_msg_error(__func__, "Cannot save the product") ;
221  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
222  if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
223  }
224  cpl_msg_indent_less() ;
225  return -1 ;
226  }
227  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
228  if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
229  }
230  cpl_msg_indent_less() ;
231 
232  /* Return */
233  if (cpl_error_get_code()) return -1 ;
234  else return 0 ;
235 }
236 
237 /*----------------------------------------------------------------------------*/
245 /*----------------------------------------------------------------------------*/
246 static int crires_util_conversion_save(
247  const cpl_table ** out,
248  const cpl_parameterlist * parlist,
249  cpl_frameset * set)
250 {
251  cpl_propertylist ** qclists ;
252  const cpl_frame * ref_frame ;
253  cpl_propertylist * inputlist ;
254  const char * recipe_name = "crires_util_conversion" ;
255  int i ;
256 
257  /* Get the reference frame */
258  ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
259 
260  /* Create the QC lists */
261  qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
262  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
263  qclists[i] = cpl_propertylist_new() ;
264  cpl_propertylist_append_double(qclists[i], "ESO QC CONVMED",
265  crires_util_conversion_config.conv_med[i]) ;
266  cpl_propertylist_append_double(qclists[i], "ESO QC THROMED",
267  crires_util_conversion_config.thro_med[i]) ;
268 
269  /* Propagate some keywords from input raw frame extensions */
270  inputlist = cpl_propertylist_load_regexp(
271  cpl_frame_get_filename(ref_frame), i+1,
272  CRIRES_HEADER_EXT_FORWARD, 0) ;
273  cpl_propertylist_copy_property_regexp(qclists[i], inputlist,
274  CRIRES_HEADER_EXT_FORWARD, 0) ;
275  cpl_propertylist_delete(inputlist) ;
276  }
277 
278  /* Write the table */
279  crires_table_save(set,
280  parlist,
281  set,
282  out,
283  recipe_name,
284  CRIRES_EXTRACT_CONV_TAB,
285  CRIRES_PROTYPE_CONVERS,
286  NULL,
287  (const cpl_propertylist **)qclists,
288  PACKAGE "/" PACKAGE_VERSION,
289  "crires_util_conversion.fits") ;
290 
291  /* Free and return */
292  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
293  cpl_propertylist_delete(qclists[i]) ;
294  }
295  cpl_free(qclists) ;
296  return 0;
297 }
298