CR2RE Pipeline Reference Manual 1.6.2
cr2res_util_genstd.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <locale.h>
29#include <cpl.h>
30#include "hdrl.h"
31
32#include "cr2res_utils.h"
33#include "cr2res_dfs.h"
34#include "cr2res_io.h"
35
36/*-----------------------------------------------------------------------------
37 Define
38 -----------------------------------------------------------------------------*/
39
40#define RECIPE_STRING "cr2res_util_genstd"
41
42/*-----------------------------------------------------------------------------
43 Plugin registration
44 -----------------------------------------------------------------------------*/
45
46int cpl_plugin_get_info(cpl_pluginlist * list);
47
48/*-----------------------------------------------------------------------------
49 Private function prototypes
50 -----------------------------------------------------------------------------*/
51
52static int cr2res_util_genstd_create(cpl_plugin *);
53static int cr2res_util_genstd_exec(cpl_plugin *);
54static int cr2res_util_genstd_destroy(cpl_plugin *);
55static int cr2res_util_genstd(cpl_frameset *, const cpl_parameterlist *);
56
57/*-----------------------------------------------------------------------------
58 Static variables
59 -----------------------------------------------------------------------------*/
60
61static char cr2res_util_genstd_description[] = "\
62Photospheric flux table generation \n\
63 \n\
64 This utility is used to generate the photospheric flux table \n\
65 \n\
66 Inputs \n\
67 raw.txt " CR2RES_PHOTO_FLUX_TXT_RAW " [1 to n] \n\
68 The specified files are named after the standard star they represent\n\
69 (e.g. HIP61007.txt). \n\
70 The first line of the file must contain the RA and DEC (hh mm ss). \n\
71 (e.g. # 13 20 35.818 -36 42 44.26). \n\
72 The rest of the file must contain two columns: \n\
73 1st: Wavelengths in increasing order \n\
74 2nd: The atmospheric emission \n\
75 \n\
76 Outputs \n\
77 cr2res_util_genstd.fits " CR2RES_PHOTO_FLUX_PROCATG" \n\
78 \n\
79 Algorithm \n\
80 \n\
81 Library functions used \n\
82" ;
83
84/*-----------------------------------------------------------------------------
85 Functions code
86 -----------------------------------------------------------------------------*/
87
88/*----------------------------------------------------------------------------*/
98/*----------------------------------------------------------------------------*/
99int cpl_plugin_get_info(cpl_pluginlist * list)
100{
101 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
102 cpl_plugin * plugin = &recipe->interface;
103
104 if (cpl_plugin_init(plugin,
105 CPL_PLUGIN_API,
106 CR2RES_BINARY_VERSION,
107 CPL_PLUGIN_TYPE_RECIPE,
108 RECIPE_STRING,
109 "Generate standard star FITS tables",
110 cr2res_util_genstd_description,
111 CR2RES_PIPELINE_AUTHORS,
112 PACKAGE_BUGREPORT,
114 cr2res_util_genstd_create,
115 cr2res_util_genstd_exec,
116 cr2res_util_genstd_destroy)) {
117 cpl_msg_error(cpl_func, "Plugin initialization failed");
118 (void)cpl_error_set_where(cpl_func);
119 return 1;
120 }
121
122 if (cpl_pluginlist_append(list, plugin)) {
123 cpl_msg_error(cpl_func, "Error adding plugin to list");
124 (void)cpl_error_set_where(cpl_func);
125 return 1;
126 }
127 return 0;
128}
129
130/*----------------------------------------------------------------------------*/
138/*----------------------------------------------------------------------------*/
139static int cr2res_util_genstd_create(cpl_plugin * plugin)
140{
141 cpl_recipe * recipe ;
142 cpl_parameter * p ;
143
144 /* Check that the plugin is part of a valid recipe */
145 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
146 recipe = (cpl_recipe *)plugin;
147 else
148 return -1;
149
150 /* Create the parameters list in the cpl_recipe object */
151 recipe->parameters = cpl_parameterlist_new();
152
153 /* Fill the parameters list */
154 p = cpl_parameter_new_value("cr2res_util_genstd.display",
155 CPL_TYPE_BOOL, "Flag to plot", RECIPE_STRING, FALSE);
156 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "display");
157 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
158 cpl_parameterlist_append(recipe->parameters, p);
159
160 return 0;
161}
162/*----------------------------------------------------------------------------*/
168/*----------------------------------------------------------------------------*/
169static int cr2res_util_genstd_exec(cpl_plugin * plugin)
170{
171 cpl_recipe *recipe;
172
173 /* Get the recipe out of the plugin */
174 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
175 recipe = (cpl_recipe *)plugin;
176 else return -1;
177
178 return cr2res_util_genstd(recipe->frames, recipe->parameters);
179}
180
181/*----------------------------------------------------------------------------*/
187/*----------------------------------------------------------------------------*/
188static int cr2res_util_genstd_destroy(cpl_plugin * plugin)
189{
190 cpl_recipe *recipe;
191
192 /* Get the recipe out of the plugin */
193 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
194 recipe = (cpl_recipe *)plugin;
195 else return -1 ;
196
197 cpl_parameterlist_delete(recipe->parameters);
198 return 0 ;
199}
200
201/*----------------------------------------------------------------------------*/
210/*----------------------------------------------------------------------------*/
211static int cr2res_util_genstd(
212 cpl_frameset * framelist,
213 const cpl_parameterlist * parlist)
214{
215 const cpl_parameter * par ;
216 char line[1024] ;
217 cpl_bivector * bivec_ref ;
218 int ra1, ra2, dec1, dec2;
219 double ra3, dec3;
220 char isign;
221 cpl_frame * cur_frame ;
222 const char * cur_fname ;
223 cpl_table * tab ;
224 int nframes, nvals_ref, display ;
225 double * pwave_ref ;
226 cpl_array * array ;
227 int i, j ;
228
229 /* Needed for sscanf() */
230 setlocale(LC_NUMERIC, "C");
231
232 /* Retrieve input parameters */
233 par=cpl_parameterlist_find_const(parlist, "cr2res_util_genstd.display");
234 display = cpl_parameter_get_bool(par);
235
236 /* Identify the RAW and CALIB frames in the input frameset */
237 if (cr2res_dfs_set_groups(framelist)) {
238 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
239 return -1 ;
240 }
241 nframes = cpl_frameset_get_size(framelist) ;
242
243 /* Get the first star */
244 cur_frame = cpl_frameset_get_position(framelist, 0) ;
245 cur_fname = cpl_frame_get_filename(cur_frame) ;
246 if ((bivec_ref = cpl_bivector_read(cur_fname))==NULL){
247 cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
248 return -1 ;
249 }
250 pwave_ref = cpl_bivector_get_x_data(bivec_ref) ;
251 nvals_ref = cpl_bivector_get_size(bivec_ref) ;
252
253 /* Create the table */
254 tab = cpl_table_new(nframes+1) ;
255 cpl_table_new_column(tab, CR2RES_COL_STDNAME, CPL_TYPE_STRING) ;
256 cpl_table_new_column(tab, CR2RES_COL_RA, CPL_TYPE_DOUBLE) ;
257 cpl_table_new_column(tab, CR2RES_COL_DEC, CPL_TYPE_DOUBLE) ;
258 cpl_table_new_column_array(tab, CR2RES_COL_PHOTOFLUX, CPL_TYPE_DOUBLE,
259 nvals_ref) ;
260
261 /* Write the wavelength */
262 cpl_table_set_string(tab, CR2RES_COL_STDNAME, 0, "WAVE") ;
263 cpl_table_set_double(tab, CR2RES_COL_RA, 0, -1.0) ;
264 cpl_table_set_double(tab, CR2RES_COL_DEC, 0, -1.0) ;
265 array = cpl_array_wrap_double(pwave_ref, nvals_ref) ;
266 cpl_table_set_array(tab, CR2RES_COL_PHOTOFLUX, 0, array) ;
267 cpl_array_unwrap(array) ;
268
269 /* Loop on the input frames */
270 for (i = 0; i < nframes; i++) {
271 FILE *in;
272 cpl_bivector *bivec;
273 double ra, dec;
274 double *pwave;
275 double *pemiss;
276 int nvals;
277
278 /* Get the frame */
279 cur_frame = cpl_frameset_get_position(framelist, i) ;
280 cur_fname = cpl_frame_get_filename(cur_frame) ;
281
282 /* Get the RA / DEC */
283 if ((in = fopen(cur_fname, "r")) == NULL) {
284 cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
285 cpl_table_delete(tab) ;
286 cpl_bivector_delete(bivec_ref) ;
287 return -1 ;
288 }
289 if (fgets(line, 1024, in) == NULL) {
290 fclose(in) ;
291 cpl_table_delete(tab) ;
292 cpl_bivector_delete(bivec_ref) ;
293 return -1 ;
294 }
295 if (sscanf(line, "#%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
296 &dec1, &dec2, &dec3) != 7) {
297 cpl_msg_error(__func__, "Invalid first line in file %s", cur_fname);
298 fclose(in) ;
299 cpl_table_delete(tab) ;
300 cpl_bivector_delete(bivec_ref) ;
301 return -1 ;
302 }
303 fclose(in) ;
304 ra = cr2res_ra_hms2deg(ra1, ra2, ra3) ;
305 dec = cr2res_dec_hms2deg(dec1, dec2, dec3) ;
306 if (isign == '-') dec *= -1.0 ;
307
308 /* Load the file */
309 if ((bivec = cpl_bivector_read(cur_fname))==NULL){
310 cpl_msg_error(__func__, "Cannot load the file in the bivector") ;
311 cpl_bivector_delete(bivec_ref) ;
312 cpl_table_delete(tab) ;
313 return -1 ;
314 }
315 pwave = cpl_bivector_get_x_data(bivec) ;
316 pemiss = cpl_bivector_get_y_data(bivec) ;
317 nvals = cpl_bivector_get_size(bivec) ;
318
319 /* Check the file size */
320 if (nvals != nvals_ref) {
321 cpl_msg_error(__func__, "Invalid file size: %s", cur_fname) ;
322 cpl_bivector_delete(bivec_ref) ;
323 cpl_bivector_delete(bivec) ;
324 cpl_table_delete(tab) ;
325 return -1 ;
326 }
327
328 /* Check that the wavelength bins are the same */
329 for (j=0 ; j<nvals ; j++) {
330 if (pwave[j] != pwave_ref[j]) {
331 cpl_msg_error(__func__, "Invalid bins in %s", cur_fname) ;
332 cpl_bivector_delete(bivec_ref) ;
333 cpl_bivector_delete(bivec) ;
334 cpl_table_delete(tab) ;
335 return -1 ;
336 }
337 }
338
339 /* Display if requested */
340 if (display) {
341 cpl_plot_bivector(
342 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Flux (jy)';",
343 "t 'Photospheric flux' w lines", "", bivec);
344 }
345
346 /* Write the star name */
347 cpl_table_set_string(tab, CR2RES_COL_STDNAME, i+1,
349
350 /* Write the RA/DEC */
351 cpl_table_set_double(tab, CR2RES_COL_RA, i+1, ra) ;
352 cpl_table_set_double(tab, CR2RES_COL_DEC, i+1, dec) ;
353
354 /* Write the signal */
355 array = cpl_array_wrap_double(pemiss, nvals) ;
356 cpl_table_set_array(tab, CR2RES_COL_PHOTOFLUX, i+1, array) ;
357 cpl_array_unwrap(array) ;
358
359 cpl_bivector_delete(bivec) ;
360 }
361 cpl_bivector_delete(bivec_ref) ;
362
363 /* Save the table */
364 cpl_msg_info(__func__, "Save the table") ;
365
366 if (cr2res_io_save_PHOTO_FLUX("cr2res_util_genstd.fits", tab,
367 parlist, framelist, RECIPE_STRING) == -1) {
368 cpl_msg_error(__func__, "Cannot write the table") ;
369 cpl_table_delete(tab) ;
370 return -1 ;
371 }
372 cpl_table_delete(tab) ;
373
374 return 0 ;
375}
cpl_error_code cr2res_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: cr2res_dfs.c:53
int cr2res_io_save_PHOTO_FLUX(const char *filename, cpl_table *out_table, const cpl_parameterlist *parlist, cpl_frameset *set, const char *recipe)
Save PHOTO_FLUX file.
Definition: cr2res_io.c:1389
double cr2res_ra_hms2deg(int hh, int mm, double ss)
Convert the RA from hh mm ss to degrees.
Definition: cr2res_utils.c:121
double cr2res_dec_hms2deg(int dd, int mm, double ss)
Convert the DEC from dd mm ss to degrees.
Definition: cr2res_utils.c:139
char * cr2res_get_root_name(const char *filename)
Find out the root part of a basename (name without extension).
char * cr2res_get_base_name(const char *filename)
Find out the base name of a file (i.e. without prefix path)
const char * cr2res_get_license(void)
Get the pipeline copyright and license.