CRIRES Pipeline Reference Manual  2.3.15
crires_util_wlassign.c
1 /* $Id: crires_util_wlassign.c,v 1.37 2012-09-20 13:09:54 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 13:09:54 $
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 #include "crires_wlcalib.h"
38 
39 /*-----------------------------------------------------------------------------
40  Define
41  -----------------------------------------------------------------------------*/
42 
43 #define RECIPE_STRING "crires_util_wlassign"
44 
45 /*-----------------------------------------------------------------------------
46  Functions prototypes
47  -----------------------------------------------------------------------------*/
48 
49 static int crires_util_wlassign_save(const cpl_table **,
50  const cpl_parameterlist *, cpl_frameset *) ;
51 
52 static char crires_util_wlassign_description[] =
53 "This recipe accepts 2 parameters:\n"
54 "First parameter: the table of the extracted spectrum in pixels\n"
55 " (PRO TYPE = "CRIRES_PROTYPE_SPEC_PIX")\n"
56 "Second parameter: the table with the wavelength solution(s)\n"
57 " (PRO TYPE = "CRIRES_PROTYPE_WL_POLY")\n"
58 "\n"
59 "This recipe produces 1 file:\n"
60 "First product: the table with the extracted spectrum in wavelength.\n"
61 " (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n" ;
62 
63 CRIRES_RECIPE_DEFINE(crires_util_wlassign,
64  CRIRES_PARAM_DISPLAY,
65  "Put the wavelength in the extracted table",
66  crires_util_wlassign_description) ;
67 
68 /*-----------------------------------------------------------------------------
69  Static variables
70  -----------------------------------------------------------------------------*/
71 
72 static struct {
73  /* Inputs */
74  int display ;
75  /* Outputs */
76  int win_mode ;
77 } crires_util_wlassign_config ;
78 
79 /*-----------------------------------------------------------------------------
80  Functions code
81  -----------------------------------------------------------------------------*/
82 
83 /*----------------------------------------------------------------------------*/
90 /*----------------------------------------------------------------------------*/
91 static int crires_util_wlassign(
92  cpl_frameset * frameset,
93  const cpl_parameterlist * parlist)
94 {
95  cpl_frame * fr ;
96  cpl_propertylist * plist ;
97  const char * sval ;
98  cpl_table * wl_tab ;
99  cpl_image * wl_map ;
100  cpl_table * ext_tab[CRIRES_NB_DETECTORS] ;
101  int nbrows, pix, spec_pos ;
102  double wl ;
103  int i, j ;
104 
105  /* Initialise */
106  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
107  ext_tab[i] = NULL ;
108  }
109 
110  /* Retrieve input parameters */
111  crires_util_wlassign_config.display = crires_parameterlist_get_int(parlist,
112  RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
113 
114  /* Identify the RAW and CALIB frames in the input frameset */
115  if (crires_dfs_set_groups(frameset, NULL)) {
116  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
117  return -1 ;
118  }
119 
120  /* Windowing mode ? */
121  fr = cpl_frameset_get_position(frameset, 0);
122  if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL)
123  return -1 ;
124  sval = crires_pfits_get_ncorrs(plist) ;
125  if (!strcmp(sval, "FowlerNsampGRstWin")) {
126  crires_util_wlassign_config.win_mode = 1 ;
127  } else {
128  crires_util_wlassign_config.win_mode = 0 ;
129  }
130  cpl_propertylist_delete(plist) ;
131 
132  /* Loop on the detectors */
133  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
134  /* Skip some detectors in windowing mode */
135  if ((i==0 || i==CRIRES_NB_DETECTORS-1) &&
136  (crires_util_wlassign_config.win_mode == 1)) {
137  continue ;
138  }
139  cpl_msg_info(__func__, "Wavelength assignment for chip %d", i+1) ;
140 
141  /* First Read the Wavelength calibration from the second frame */
142  cpl_msg_info(__func__, "Wavelength retrieval") ;
143  fr = cpl_frameset_get_position(frameset, 1);
144  if ((wl_tab=crires_load_table_check(cpl_frame_get_filename(fr), i+1,
145  CRIRES_PROTYPE_WL_POLY, -1, -1, 0)) == NULL) {
146  cpl_msg_error(__func__, "Cannot load Wavelength from chip %d",i+1) ;
147  return -1 ;
148  }
149 
150  /* Create the wave map */
151  if ((wl_map = crires_wlcalib_gen_wlmap_one_chip(
152  (const cpl_table *)wl_tab)) == NULL) {
153  cpl_msg_error(__func__, "Cannot compute the Wavelength Map") ;
154  cpl_table_delete(wl_tab);
155  return -1 ;
156  }
157  cpl_table_delete(wl_tab);
158 
159  /* The first frame must be the extracted spectrum in pixels */
160  fr = cpl_frameset_get_position(frameset, 0);
161  if ((ext_tab[i] = crires_load_table_check(cpl_frame_get_filename(fr),
162  i+1, CRIRES_PROTYPE_SPEC_PIX, -1, -1, 0)) == NULL) {
163  cpl_msg_warning(__func__, "Empty extension") ;
164  continue ;
165  }
166 
167  /* Create the output table */
168  cpl_msg_info(__func__, "Wavelength column computation") ;
169  cpl_table_new_column(ext_tab[i], CRIRES_COL_WAVELENGTH,CPL_TYPE_DOUBLE);
170  cpl_table_new_column(ext_tab[i], CRIRES_COL_WAVELENGTH_MODEL,
171  CPL_TYPE_DOUBLE);
172  nbrows = cpl_table_get_nrow(ext_tab[i]) ;
173  spec_pos = crires_get_y_spec_position(cpl_frame_get_filename(fr), i+1);
174  for (j=0 ; j<nbrows ; j++) {
175  wl = cpl_image_get(wl_map, j+1, spec_pos, &pix) ;
176  cpl_table_set_double(ext_tab[i], CRIRES_COL_WAVELENGTH, j, wl);
177  cpl_table_set_double(ext_tab[i], CRIRES_COL_WAVELENGTH_MODEL, j,
178  0.0);
179  }
180  cpl_image_delete(wl_map) ;
181 
182  /* Plot if requested */
183  if(crires_util_wlassign_config.display == i+1) {
184  cpl_plot_column(
185 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity OPT (ADU/sec)';",
186  "t 'Extracted spectrum OPT' w lines", "", ext_tab[i],
187  CRIRES_COL_WAVELENGTH, CRIRES_COL_EXTRACT_INT_OPT) ;
188  cpl_plot_column(
189 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Intensity RECT (ADU/sec)';",
190  "t 'Extracted spectrum RECT' w lines", "", ext_tab[i],
191  CRIRES_COL_WAVELENGTH, CRIRES_COL_EXTRACT_INT_RECT) ;
192  }
193  }
194 
195  /* Reconstruct chips in windowing mode using detector 2 */
196  if (crires_util_wlassign_config.win_mode == 1) {
197  if (ext_tab[1] != NULL) {
198  ext_tab[0] = cpl_table_duplicate(ext_tab[1]) ;
199  cpl_table_set_size(ext_tab[0], 0) ;
200  ext_tab[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(ext_tab[0]) ;
201  }
202  }
203 
204  /* Save the product */
205  cpl_msg_info(__func__, "Save the product") ;
206  cpl_msg_indent_more() ;
207  if (crires_util_wlassign_save((const cpl_table **)ext_tab, parlist,
208  frameset)) {
209  cpl_msg_error(__func__, "Cannot save the product") ;
210  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
211  if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
212  }
213  cpl_msg_indent_less() ;
214  return -1 ;
215  }
216  for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
217  if (ext_tab[i] != NULL) cpl_table_delete(ext_tab[i]) ;
218  }
219  cpl_msg_indent_less() ;
220 
221  /* Return */
222  if (cpl_error_get_code()) return -1 ;
223  else return 0 ;
224 }
225 
226 /*----------------------------------------------------------------------------*/
234 /*----------------------------------------------------------------------------*/
235 static int crires_util_wlassign_save(
236  const cpl_table ** out,
237  const cpl_parameterlist * parlist,
238  cpl_frameset * set)
239 {
240  const char * recipe_name = "crires_util_wlassign" ;
241 
242  /* Write the table */
243  crires_table_save(set,
244  parlist,
245  set,
246  out,
247  recipe_name,
248  CRIRES_OBS_EXTRACT_WL_TAB,
249  CRIRES_PROTYPE_SPEC_WL,
250  NULL,
251  NULL,
252  PACKAGE "/" PACKAGE_VERSION,
253  "crires_util_wlassign.fits") ;
254 
255  return 0;
256 }