IIINSTRUMENT Pipeline Reference Manual 4.5.1
visir_util_spc_txt2fits.c
1/* $Id: visir_util_spc_txt2fits.c,v 1.49 2012-08-04 18:05:51 llundin Exp $
2 *
3 * This file is part of the VISIR 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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: llundin $
23 * $Date: 2012-08-04 18:05:51 $
24 * $Revision: 1.49 $
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 "visir_recipe.h"
37
38#include <string.h>
39
40/*-----------------------------------------------------------------------------
41 Recipe defines
42 -----------------------------------------------------------------------------*/
43
44#define RECIPE_STRING "visir_util_spc_txt2fits"
45
46#ifndef FLEN_VALUE
47 /* Copied from CFITSIO */
48#define FLEN_VALUE 71
49#endif
50
51/*-----------------------------------------------------------------------------
52 Private Functions prototypes
53 -----------------------------------------------------------------------------*/
54static cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset *,
55 const cpl_parameterlist *,
56 const cpl_propertylist *,
57 const char *,
58 const cpl_table *);
59
60static cpl_error_code visir_propertylist_fill_comments(cpl_propertylist *,
61 FILE *);
62
63VISIR_RECIPE_DEFINE(visir_util_spc_txt2fits, VISIR_PARAM_QEFF,
64 "Generate spectrum calibration FITS tables",
65 "This recipe shall be used to generate spectrum "
66 "calibration tables.\n"
67 "The sof file shall consist of 1 line with the name of an "
68 "ASCII-file\n"
69 "currently tagged with either " VISIR_SPC_LINES_ASCII " or "
70 VISIR_SPC_QEFF_ASCII ".\n"
71 "The file must comprise two columns:\n"
72 "1st: Must be wavelengths in increasing order in units "
73 "of meter\n"
74 "2nd: For " VISIR_SPC_LINES_ASCII "-files must be the "
75 "atmospheric emission, "
76 "while\n"
77 " for " VISIR_SPC_QEFF_ASCII "-files must be the "
78 "quantum efficiency of "
79 "the detector.\n"
80 "A " VISIR_SPC_LINES_ASCII "-file will generate a "
81 VISIR_CALIB_LINES_SPC
82 "-file, and \n"
83 "a " VISIR_SPC_QEFF_ASCII "-file will generate a "
84 VISIR_CALIB_QEFF_SPC
85 "-file.\n"
86 "The current " VISIR_CALIB_LINES_SPC "- and "
87 VISIR_CALIB_QEFF_SPC
88 "-files are\n"
89 "generated using the ASCII-files in the catalogs/ "
90 "directory of the VISIR\n"
91 "source-code distribution.");
92
93/*----------------------------------------------------------------------------*/
97/*----------------------------------------------------------------------------*/
98
99/*-----------------------------------------------------------------------------
100 Functions code
101 -----------------------------------------------------------------------------*/
102
103/*----------------------------------------------------------------------------*/
113/*----------------------------------------------------------------------------*/
114static int visir_util_spc_txt2fits(cpl_frameset * framelist,
115 const cpl_parameterlist * parlist)
116{
117 const char * label2 = NULL; /* Avoid uninit warning */
118 const char * procat;
119 cpl_propertylist * qclist = cpl_propertylist_new();
120 irplib_framelist * allframes = NULL;
121 irplib_framelist * rawframes = NULL;
122 cpl_bivector * spectrum = NULL;
123 cpl_table * tab = NULL;
124 FILE * stream = NULL;
125 int nframes;
126 int i, nvals;
127
128 /* Identify the RAW frames in the input frameset */
129 skip_if (visir_dfs_set_groups(framelist));
130
131 /* Objects observation */
132 allframes = irplib_framelist_cast(framelist);
133 skip_if(allframes == NULL);
134 rawframes = irplib_framelist_extract_regexp(allframes, "^("
135 VISIR_SPC_LINES_ASCII "|"
136 VISIR_SPC_QEFF_ASCII ")$",
137 CPL_FALSE);
138 skip_if (rawframes == NULL);
139
140 nframes = irplib_framelist_get_size(rawframes);
141
142 /* Loop on all input frames */
143 for (i = 0; i < nframes; i++) {
144 const cpl_frame * frame = irplib_framelist_get_const(rawframes, i);
145 const char * filename = cpl_frame_get_filename(frame);
146 const char * basepos;
147 const char * dotpos;
148 char extname[FLEN_VALUE];
149 size_t baselen;
150
151 skip_if(filename == NULL);
152 cpl_propertylist_empty(qclist);
153
154 basepos = strrchr(filename, '/');
155 basepos = basepos == NULL ? filename : basepos + 1;
156 dotpos = strrchr(basepos, '.');
157 baselen = dotpos == NULL ? strlen(basepos) : (size_t)(dotpos - basepos);
158 if (baselen > 0) {
159 strncpy(extname, basepos, CX_MIN(FLEN_VALUE, baselen));
160 extname[CX_MIN(FLEN_VALUE - 1, baselen)] = '\0';
161
162 skip_if(cpl_propertylist_append_string(qclist, "EXTNAME", extname));
163 }
164
165 if (stream != NULL) fclose(stream);
166 stream = fopen(filename, "r");
167
168 skip_if (stream == NULL);
169
170 cpl_bivector_delete(spectrum);
171 spectrum = cpl_bivector_new(2481); /* Some 1st guess at actual length */
172 skip_if( visir_bivector_load(spectrum, stream));
173
174 if (strcmp(cpl_frame_get_tag(frame), VISIR_SPC_LINES_ASCII)) {
175 /* The file is a quantum efficiency file */
176
177 procat = VISIR_SPEC_CAL_QEFF_PROCATG;
178 label2 = "Efficiency";
179
180 skip_if(cpl_propertylist_set_comment(qclist, "EXTNAME", "Match "
181 "with HIERARCH.ESO."
182 "DET.CHIP.NAME"));
183
184
185 } else {
186 /* The file is a sky lines file */
187 procat = VISIR_SPEC_CAL_LINES_PROCATG;
188 label2 = "Emission";
189 }
190
191 /* Get the number of values in the model */
192 nvals = cpl_bivector_get_size(spectrum);
193
194 /* Allocate the data container */
195 if (tab != NULL) {
196 cpl_table_unwrap(tab, "Wavelength");
197 cpl_table_unwrap(tab, label2);
198 cpl_table_delete(tab);
199 }
200 tab = cpl_table_new(nvals);
201
202 skip_if( tab == NULL );
203
204 skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_x_data(spectrum),
205 "Wavelength"));
206 skip_if (cpl_table_set_column_unit(tab, "Wavelength", "m"));
207
208 skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_y_data(spectrum),
209 label2));
210
211 skip_if (cpl_table_set_column_unit(tab, label2, "[0;1] (Unitless)"));
212
213 skip_if(visir_propertylist_fill_comments(qclist, stream));
214
215 /* Save the table */
216 cpl_msg_info(cpl_func, "Saving the table with %d rows", nvals);
217 if (i == 0) {
218
219 skip_if (visir_util_spc_txt2fits_save(framelist, parlist, qclist,
220 procat, tab));
221 } else {
222 skip_if (cpl_table_save(tab, NULL, qclist, RECIPE_STRING
223 CPL_DFS_FITS, CPL_IO_EXTEND));
224 }
225 }
226
227 end_skip;
228
229 if (stream != NULL) fclose(stream);
230 cpl_bivector_delete(spectrum);
231 if (tab) {
232 cpl_table_unwrap(tab, "Wavelength");
233 cpl_table_unwrap(tab, label2);
234 cpl_table_delete(tab);
235 }
236 irplib_framelist_delete(allframes);
237 irplib_framelist_delete(rawframes);
238 cpl_propertylist_delete(qclist);
239
240 return cpl_error_get_code();
241}
242
243/*----------------------------------------------------------------------------*/
255/*----------------------------------------------------------------------------*/
256static
257cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset * set,
258 const cpl_parameterlist * parlist,
259 const cpl_propertylist * qclist,
260 const char * procat,
261 const cpl_table * tab)
262{
263
264 cpl_propertylist * applist = cpl_propertylist_new();
265
266
267 bug_if(cpl_propertylist_append_string(applist, "INSTRUME", "VISIR"));
268
269 skip_if (irplib_dfs_save_table(set, parlist, set, tab, qclist, RECIPE_STRING,
270 procat, applist, NULL, visir_pipe_id,
271 RECIPE_STRING CPL_DFS_FITS));
272
273 end_skip;
274
275 cpl_propertylist_delete(applist);
276
277 return cpl_error_get_code();
278}
279
280
281/*----------------------------------------------------------------------------*/
291/*----------------------------------------------------------------------------*/
292static cpl_error_code visir_propertylist_fill_comments(cpl_propertylist * self,
293 FILE * stream)
294{
295
296 bug_if(self == NULL);
297 bug_if(stream == NULL);
298
299 if (!fseek(stream, 0, SEEK_SET)) {
300
301 char line[1024];
302
303 while (fgets(line, 1024, stream) != NULL) {
304
305 if (line[0] == '#') {
306 char value[FLEN_VALUE];
307 strncpy(value, line+1, FLEN_VALUE);
308 value[FLEN_VALUE - 1] = '\0';
309 cpl_propertylist_append_string(self, "COMMENT", value);
310 }
311 }
312 }
313
314 end_skip;
315
316 return cpl_error_get_code();
317}
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72