CRIRES Pipeline Reference Manual 2.3.17
crires_util_genlines.c
1/* $Id: crires_util_genlines.c,v 1.16 2011-11-24 08:27:46 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: 2011-11-24 08:27:46 $
24 * $Revision: 1.16 $
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/*-----------------------------------------------------------------------------
39 Define
40 -----------------------------------------------------------------------------*/
41
42#define RECIPE_STRING "crires_util_genlines"
43
44/*-----------------------------------------------------------------------------
45 Functions prototypes
46 -----------------------------------------------------------------------------*/
47
48static int crires_util_genlines_save(cpl_table *, const cpl_parameterlist *,
49 cpl_frameset *);
50
51static char crires_util_genlines_description[] =
52"This recipe is used to generate spectrum calibration tables.\n"
53"The sof file contains the names of the input ASCII file\n"
54"tagged with "CRIRES_UTIL_GENLINES_RAW".\n"
55"The ASCII file must contain two columns:\n"
56"1st: Wavelengths in increasing order (the unit is corrected by\n"
57" the factor option to obtain nanometers).\n"
58"2nd: The atmospheric emission.\n"
59"The ASCII files are in the catalogs/ directory of the CRIRES distribution.\n"
60"This recipe produces 1 file:\n"
61"First product: the table with the lines.\n"
62" (PRO TYPE = "CRIRES_PROTYPE_CATALOG")\n" ;
63
64CRIRES_RECIPE_DEFINE(crires_util_genlines,
65 CRIRES_PARAM_FILL_BLANKS |
66 CRIRES_PARAM_PLOT |
67 CRIRES_PARAM_LINES_MODE |
68 CRIRES_PARAM_WL_FACTOR,
69 "Generate spectrum calibration FITS tables",
70 crires_util_genlines_description) ;
71
72/*-----------------------------------------------------------------------------
73 Static variables
74 -----------------------------------------------------------------------------*/
75
76static struct {
77 /* Inputs */
78 int fill_blanks ;
79 int display ;
80 int mode ;
81 double wl_factor ;
82 /* Outputs */
83} crires_util_genlines_config ;
84
85/*-----------------------------------------------------------------------------
86 Functions code
87 -----------------------------------------------------------------------------*/
88
89/*----------------------------------------------------------------------------*/
98/*----------------------------------------------------------------------------*/
99static int crires_util_genlines(
100 cpl_frameset * framelist,
101 const cpl_parameterlist * parlist)
102{
103 cpl_bivector * bivec ;
104 double * pbivec_x ;
105 double * pbivec_y ;
106 cpl_bivector * bivec_fill ;
107 double * pbivec_fill_x ;
108 double * pbivec_fill_y ;
109 cpl_frame * cur_frame ;
110 int nvals, nb_new_vals ;
111 double wavel ;
112 cpl_table * tab ;
113 int i ;
114
115 /* Retrieve input parameters */
116 crires_util_genlines_config.fill_blanks = crires_parameterlist_get_bool(
117 parlist, RECIPE_STRING, CRIRES_PARAM_FILL_BLANKS) ;
118 crires_util_genlines_config.display = crires_parameterlist_get_bool(
119 parlist, RECIPE_STRING, CRIRES_PARAM_PLOT) ;
120 crires_util_genlines_config.mode = crires_parameterlist_get_int(
121 parlist, RECIPE_STRING, CRIRES_PARAM_LINES_MODE) ;
122 crires_util_genlines_config.wl_factor = crires_parameterlist_get_double(
123 parlist, RECIPE_STRING, CRIRES_PARAM_WL_FACTOR) ;
124
125 /* Identify the RAW and CALIB frames in the input frameset */
126 if (crires_dfs_set_groups(framelist, NULL)) {
127 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
128 return -1 ;
129 }
130
131 /* Load the file */
132 cur_frame = cpl_frameset_get_position(framelist, 0) ;
133 if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
134 cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
135 return -1 ;
136 }
137 nvals = cpl_bivector_get_size(bivec) ;
138
139 /* If fill_blanks is requested */
140 if (crires_util_genlines_config.fill_blanks) {
141 nb_new_vals = 3 * nvals ;
142 bivec_fill = cpl_bivector_new(nb_new_vals) ;
143 pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill) ;
144 pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill) ;
145 pbivec_x = cpl_bivector_get_x_data(bivec) ;
146 pbivec_y = cpl_bivector_get_y_data(bivec) ;
147 for (i=0 ; i<nvals ; i++) {
148 wavel = pbivec_x[i] * crires_util_genlines_config.wl_factor ;
149 pbivec_fill_x[3*i] = wavel - 0.01 ;
150 pbivec_fill_y[3*i] = 0.0 ;
151 pbivec_fill_x[3*i+1] = wavel ;
152 pbivec_fill_y[3*i+1] = pbivec_y[i] ;
153 pbivec_fill_x[3*i+2] = wavel + 0.01 ;
154 pbivec_fill_y[3*i+2] = 0.0 ;
155 }
156 cpl_bivector_delete(bivec);
157 bivec = bivec_fill ;
158 bivec_fill = NULL ;
159 nvals = cpl_bivector_get_size(bivec) ;
160 } else {
161 cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
162 crires_util_genlines_config.wl_factor) ;
163 }
164
165 /* Display if requested */
166 if (crires_util_genlines_config.display) {
167 cpl_plot_bivector(
168 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
169 "t 'Catalog lines' w lines", "", bivec);
170 }
171
172 /* Allocate the data container */
173 tab = cpl_table_new(nvals) ;
174 cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
175 CRIRES_COL_WAVELENGTH) ;
176 cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec),
177 CRIRES_COL_EMISSION) ;
178
179 /* Save the table */
180 cpl_msg_info(__func__, "Saving the table with %d rows", nvals) ;
181 if (crires_util_genlines_save(tab, parlist, framelist) == -1) {
182 cpl_msg_error(__func__, "Cannot write the table") ;
183 cpl_bivector_delete(bivec) ;
184 cpl_table_unwrap(tab, CRIRES_COL_WAVELENGTH) ;
185 cpl_table_unwrap(tab, CRIRES_COL_EMISSION) ;
186 cpl_table_delete(tab) ;
187 return -1 ;
188 }
189 cpl_bivector_delete(bivec) ;
190 cpl_table_unwrap(tab, CRIRES_COL_WAVELENGTH) ;
191 cpl_table_unwrap(tab, CRIRES_COL_EMISSION) ;
192 cpl_table_delete(tab) ;
193 return 0 ;
194}
195
196/*----------------------------------------------------------------------------*/
204/*----------------------------------------------------------------------------*/
205static int crires_util_genlines_save(
206 cpl_table * out_table,
207 const cpl_parameterlist * parlist,
208 cpl_frameset * set)
209{
210 cpl_propertylist * plist ;
211 const char * procat ;
212
213 if (crires_util_genlines_config.mode == 1)
214 procat = CRIRES_CALPRO_HITRAN_CAT ;
215 else if (crires_util_genlines_config.mode == 2)
216 procat = CRIRES_CALPRO_OH_CAT ;
217 else if (crires_util_genlines_config.mode == 3)
218 procat = CRIRES_CALPRO_THAR_CAT ;
219 else if (crires_util_genlines_config.mode == 4)
220 procat = CRIRES_CALPRO_N2O_CAT ;
221 else if (crires_util_genlines_config.mode == 5)
222 procat = CRIRES_CALPRO_MODEL_WAVEEG ;
223 else
224 procat = "UNKNOWN" ;
225
226 plist = cpl_propertylist_new();
227 cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
228 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, procat) ;
229 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
230 CRIRES_PROTYPE_CATALOG) ;
231
232 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
233 NULL, "crires_util_genlines", plist, NULL,
234 PACKAGE "/" PACKAGE_VERSION,
235 "crires_util_genlines.fits") != CPL_ERROR_NONE) {
236 cpl_msg_error(__func__, "Cannot save the table") ;
237 return -1 ;
238 }
239 cpl_propertylist_delete(plist) ;
240
241 /* Return */
242 return 0 ;
243}