CRIRES Pipeline Reference Manual 2.3.19
crires_util_genstd.c
1/* $Id: crires_util_genstd.c,v 1.12 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.12 $
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 <locale.h>
37#include "crires_recipe.h"
38
39/*-----------------------------------------------------------------------------
40 Define
41 -----------------------------------------------------------------------------*/
42
43#define RECIPE_STRING "crires_util_genstd"
44
45/*-----------------------------------------------------------------------------
46 Functions prototypes
47 -----------------------------------------------------------------------------*/
48
49static int crires_util_genstd_save(cpl_table *, const cpl_parameterlist *,
50 cpl_frameset *);
51
52static char crires_util_genstd_description[] =
53"This recipe is used to generate standard star photospheric flux tables.\n"
54"The sof consists of file names tagged with "CRIRES_UTIL_GENSTD_RAW".\n"
55"The specified files are named after the standard star they represent\n"
56"(e.g. HIP61007.txt).\n"
57"The first line of the file must contain the RA and DEC (hh mm ss).\n"
58"(e.g. # 13 20 35.818 -36 42 44.26).\n"
59"The rest of the file must contain two columns:\n"
60"1st: Wavelengths in increasing order (the unit is corrected by\n"
61" the factor option to obtain nanometers).\n"
62"2nd: The atmospheric emission.\n"
63"The file is generated using the ASCII files in the catalogs/stdstar\n"
64"directory of the CRIRES source-code distribution."
65"\n"
66"This recipe produces 1 file for each input file:\n"
67"First product: the table with the photospheric flux of the std.\n"
68" (PRO TYPE = "CRIRES_PROTYPE_PHO_FLUX")\n" ;
69
70CRIRES_RECIPE_DEFINE(crires_util_genstd,
71 CRIRES_PARAM_PLOT,
72 "Generate standard star FITS tables",
73 crires_util_genstd_description) ;
74
75/*-----------------------------------------------------------------------------
76 Static variables
77 -----------------------------------------------------------------------------*/
78
79static struct {
80 /* Inputs */
81 int display ;
82 /* Outputs */
83} crires_util_genstd_config ;
84
85/*-----------------------------------------------------------------------------
86 Functions code
87 -----------------------------------------------------------------------------*/
88
89/*----------------------------------------------------------------------------*/
98/*----------------------------------------------------------------------------*/
99static int crires_util_genstd(
100 cpl_frameset * framelist,
101 const cpl_parameterlist * parlist)
102{
103 FILE * in ;
104 char line[1024] ;
105 cpl_bivector * bivec_ref ;
106 cpl_bivector * bivec ;
107 int ra1, ra2, dec1, dec2;
108 double ra3, dec3;
109 double ra, dec ;
110 char isign;
111 cpl_frame * cur_frame ;
112 const char * cur_fname ;
113 cpl_table * tab ;
114 int nvals, nframes, nvals_ref ;
115 double * pwave ;
116 double * pemiss ;
117 double * pwave_ref ;
118 cpl_array * array ;
119 int i, j ;
120
121 /* Needed for sscanf() */
122 setlocale(LC_NUMERIC, "C");
123
124 /* Retrieve input parameters */
125 crires_util_genstd_config.display = crires_parameterlist_get_bool(
126 parlist, RECIPE_STRING, CRIRES_PARAM_PLOT) ;
127
128 /* Identify the RAW and CALIB frames in the input frameset */
129 if (crires_dfs_set_groups(framelist, NULL)) {
130 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
131 return -1 ;
132 }
133 nframes = cpl_frameset_get_size(framelist) ;
134
135 /* Get the first star */
136 cur_frame = cpl_frameset_get_position(framelist, 0) ;
137 cur_fname = cpl_frame_get_filename(cur_frame) ;
138 if ((bivec_ref = cpl_bivector_read(cur_fname))==NULL){
139 cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
140 return -1 ;
141 }
142 pwave_ref = cpl_bivector_get_x_data(bivec_ref) ;
143 nvals_ref = cpl_bivector_get_size(bivec_ref) ;
144
145 /* Create the table */
146 tab = cpl_table_new(nframes+1) ;
147 cpl_table_new_column(tab, CRIRES_COL_STDNAME, CPL_TYPE_STRING) ;
148 cpl_table_new_column(tab, CRIRES_COL_RA, CPL_TYPE_DOUBLE) ;
149 cpl_table_new_column(tab, CRIRES_COL_DEC, CPL_TYPE_DOUBLE) ;
150 cpl_table_new_column_array(tab, CRIRES_COL_PHOTOFLUX, CPL_TYPE_DOUBLE,
151 nvals_ref) ;
152
153 /* Write the wavelength */
154 cpl_table_set_string(tab, CRIRES_COL_STDNAME, 0, "WAVE") ;
155 cpl_table_set_double(tab, CRIRES_COL_RA, 0, -1.0) ;
156 cpl_table_set_double(tab, CRIRES_COL_DEC, 0, -1.0) ;
157 array = cpl_array_wrap_double(pwave_ref, nvals_ref) ;
158 cpl_table_set_array(tab, CRIRES_COL_PHOTOFLUX, 0, array) ;
159 cpl_array_unwrap(array) ;
160
161 /* Loop on the input frames */
162 for (i=0 ; i<nframes ; i++) {
163
164 /* Get the frame */
165 cur_frame = cpl_frameset_get_position(framelist, i) ;
166 cur_fname = cpl_frame_get_filename(cur_frame) ;
167
168 /* Get the RA / DEC */
169 if ((in = fopen(cur_fname, "r")) == NULL) {
170 cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
171 cpl_table_delete(tab) ;
172 cpl_bivector_delete(bivec_ref) ;
173 return -1 ;
174 }
175 if (fgets(line, 1024, in) == NULL) {
176 fclose(in) ;
177 cpl_table_delete(tab) ;
178 cpl_bivector_delete(bivec_ref) ;
179 return -1 ;
180 }
181 if (sscanf(line, "#%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
182 &dec1, &dec2, &dec3) != 7) {
183 cpl_msg_error(__func__, "Invalid first line in file %s", cur_fname);
184 fclose(in) ;
185 cpl_table_delete(tab) ;
186 cpl_bivector_delete(bivec_ref) ;
187 return -1 ;
188 }
189 fclose(in) ;
190 ra = crires_ra_hms2deg(ra1, ra2, ra3) ;
191 dec = crires_dec_hms2deg(dec1, dec2, dec3) ;
192 if (isign == '-') dec *= -1.0 ;
193
194 /* Load the file */
195 if ((bivec = cpl_bivector_read(cur_fname))==NULL){
196 cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
197 cpl_bivector_delete(bivec_ref) ;
198 cpl_table_delete(tab) ;
199 return -1 ;
200 }
201 pwave = cpl_bivector_get_x_data(bivec) ;
202 pemiss = cpl_bivector_get_y_data(bivec) ;
203 nvals = cpl_bivector_get_size(bivec) ;
204
205 /* Check the file size */
206 if (nvals != nvals_ref) {
207 cpl_msg_error(__func__, "Invalid file size: %s", cur_fname) ;
208 cpl_bivector_delete(bivec_ref) ;
209 cpl_bivector_delete(bivec) ;
210 cpl_table_delete(tab) ;
211 return -1 ;
212 }
213
214 /* Check that the wavelength bins are the same */
215 for (j=0 ; j<nvals ; j++) {
216 if (pwave[j] != pwave_ref[j]) {
217 cpl_msg_error(__func__, "Invalid bins in %s", cur_fname) ;
218 cpl_bivector_delete(bivec_ref) ;
219 cpl_bivector_delete(bivec) ;
220 cpl_table_delete(tab) ;
221 return -1 ;
222 }
223 }
224
225 /* Display if requested */
226 if (crires_util_genstd_config.display) {
227 cpl_plot_bivector(
228 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Flux (jy)';",
229 "t 'Photospheric flux' w lines", "", bivec);
230 }
231
232 /* Write the star name */
233 cpl_table_set_string(tab, CRIRES_COL_STDNAME, i+1,
234 crires_get_root_name(crires_get_base_name(cur_fname))) ;
235
236 /* Write the RA/DEC */
237 cpl_table_set_double(tab, CRIRES_COL_RA, i+1, ra) ;
238 cpl_table_set_double(tab, CRIRES_COL_DEC, i+1, dec) ;
239
240 /* Write the signal */
241 array = cpl_array_wrap_double(pemiss, nvals) ;
242 cpl_table_set_array(tab, CRIRES_COL_PHOTOFLUX, i+1, array) ;
243 cpl_array_unwrap(array) ;
244
245 cpl_bivector_delete(bivec) ;
246 }
247 cpl_bivector_delete(bivec_ref) ;
248
249 /* Save the table */
250 cpl_msg_info(__func__, "Save the table") ;
251 if (crires_util_genstd_save(tab, parlist, framelist) == -1) {
252 cpl_msg_error(__func__, "Cannot write the table") ;
253 cpl_table_delete(tab) ;
254 return -1 ;
255 }
256 cpl_table_delete(tab) ;
257
258 return 0 ;
259}
260
261/*----------------------------------------------------------------------------*/
269/*----------------------------------------------------------------------------*/
270static int crires_util_genstd_save(
271 cpl_table * out_table,
272 const cpl_parameterlist * parlist,
273 cpl_frameset * set)
274{
275 cpl_propertylist * plist ;
276
277 plist = cpl_propertylist_new();
278 cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
279 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
280 CRIRES_CALPRO_STD_PHOTOFLUX) ;
281 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
282 CRIRES_PROTYPE_PHO_FLUX) ;
283
284 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
285 NULL, "crires_util_genstd", plist, NULL,
286 PACKAGE "/" PACKAGE_VERSION,
287 "crires_util_genstd.fits") != CPL_ERROR_NONE) {
288 cpl_msg_error(__func__, "Cannot save the table") ;
289 return -1 ;
290 }
291 cpl_propertylist_delete(plist) ;
292
293 /* Return */
294 return 0 ;
295}