IIINSTRUMENT Pipeline Reference Manual 6.2.5
isaac_util_seds.c
1/* $Id: isaac_util_seds.c,v 1.15 2013-03-12 08:06:48 llundin Exp $
2 *
3 * This file is part of the ISAAC 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: 2013-03-12 08:06:48 $
24 * $Revision: 1.15 $
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 "irplib_utils.h"
37
38#include "isaac_utils.h"
39#include "isaac_pfits.h"
40#include "isaac_dfs.h"
41
42#include <string.h>
43#include <math.h>
44
45/*-----------------------------------------------------------------------------
46 Defines
47 -----------------------------------------------------------------------------*/
48
49#define RECIPE_NAME "isaac_util_seds"
50
51/*-----------------------------------------------------------------------------
52 Functions prototypes
53 -----------------------------------------------------------------------------*/
54
55cpl_recipe_define(isaac_util_seds, ISAAC_BINARY_VERSION,
56 "Lars Lundin", PACKAGE_BUGREPORT, "2008",
57 "ISAAC SEDS table creation",
58 RECIPE_NAME " -- ISAAC SEDS table creation.\n"
59 "The files listed in the Set Of Frames (sof-file) "
60 "must be tagged:\n"
61 "ASCII-SEDS.txt " ISAAC_UTIL_SEDS_RAW "\n"
62 "The first three characters of the input file names must be "
63 "unique. Lines starting with a '#' are ignored. The other "
64 "lines must have (at least) two columns, the first with a "
65 "wavelength, the second with an SED. All files must have "
66 "an identical number of non-comment rows and all files are "
67 "assumed to contain identical sequences of wavelengths.\n");
68
69static
70cpl_error_code isaac_util_seds_save(cpl_frameset *, const cpl_table *,
71 const cpl_parameterlist *);
72
73/*-----------------------------------------------------------------------------
74 Functions code
75 -----------------------------------------------------------------------------*/
76
77/*----------------------------------------------------------------------------*/
85/*----------------------------------------------------------------------------*/
86static
87cpl_error_code isaac_util_seds_fill_parameterlist(cpl_parameterlist * self) {
88
89 return self != NULL ? CPL_ERROR_NONE
90 : cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
91}
92
93/*----------------------------------------------------------------------------*/
101/*----------------------------------------------------------------------------*/
102static int isaac_util_seds(cpl_frameset * framelist,
103 const cpl_parameterlist * parlist)
104{
105 cpl_frameset * rawframes = NULL;
106 cpl_table * out = NULL;
107 cpl_bivector * seds = NULL;
108 cpl_vector * sedsx = NULL;
109 cpl_vector * sedsx1 = NULL;
110 cpl_vector * sedsy = NULL;
111 int nframes = 0;
112 int nlines = 0;
113 int i;
114
115 /* Identify the RAW and CALIB frames in the input frameset */
116 skip_if (isaac_dfs_set_groups(framelist));
117
118 /* Retrieve raw frames */
119 rawframes = isaac_extract_frameset(framelist, ISAAC_UTIL_SEDS_RAW);
120 irplib_ensure(rawframes != NULL, CPL_ERROR_DATA_NOT_FOUND,
121 "Could not find raw frames in the input list");
122
123 nframes = cpl_frameset_get_size(rawframes);
124 irplib_ensure(nframes > 0, CPL_ERROR_DATA_NOT_FOUND,
125 "Could not find raw frames in the input list");
126
127 /* Loop over ASCII files */
128 for (i = 0; i < nframes; i++) {
129 const cpl_frame * rawframe = cpl_frameset_get_position_const(rawframes, i);
130
131 /* Create the 3-letter label name from the catalogue name */
132 char name[4];
133 const char * rawfile = cpl_frame_get_filename(rawframe);
134 /* Skip path, if any */
135 const char * cat_name = strrchr(rawfile, '/');
136
137 cat_name = cat_name ? 1+cat_name : rawfile;
138 bug_if(cat_name == NULL);
139 (void)strncpy(name, cat_name, 3);
140 name[3] = '\0';
141
142 /* Load ASCII file */
143 seds = cpl_bivector_read(cpl_frame_get_filename(rawframe));
144 irplib_ensure(seds != NULL, cpl_error_get_code(),
145 "Could not load SED no. %d of %d", i+1, nframes);
146 sedsx = cpl_bivector_get_x(seds);
147 sedsy = cpl_bivector_get_y(seds);
148 cpl_bivector_unwrap_vectors(seds);
149 seds = NULL;
150
151 if (i == 0) {
152 nlines = cpl_vector_get_size(sedsx);
153
154 /* Create the table */
155 out = cpl_table_new(nlines);
156 /* - and the wavelength column */
157 bug_if(cpl_table_wrap_double(out, cpl_vector_get_data(sedsx),
158 "Wavelength"));
159 sedsx1 = sedsx;
160 sedsx = NULL;
161 } else {
162 /* Verify the consistency of length */
163 const int ilines = cpl_vector_get_size(sedsx);
164 double dmin, dmax;
165
166 irplib_ensure(ilines == nlines, CPL_ERROR_INCOMPATIBLE_INPUT,
167 "Number of lines in SED no. %d and %d differ: "
168 "%d <=> %d", 1, i+1, nlines, ilines);
169
170 /* FIXME: Support different wavelengths ? */
171 bug_if(cpl_vector_subtract(sedsx, sedsx1));
172
173 dmin = cpl_vector_get_min(sedsx);
174 dmax = cpl_vector_get_max(sedsx);
175
176 if (dmin != 0.0 || dmax != 0.0) {
177 cpl_msg_warning(cpl_func, "Wavelengths in SED no. %d differ "
178 "from those in no. 1 (min, max): %g %g",
179 i+1, dmin, dmax);
180 }
181
182 cpl_vector_delete(sedsx);
183 sedsx = NULL;
184 }
185
186 /* Create the column */
187 skip_if(cpl_table_wrap_double(out, cpl_vector_get_data(sedsy), name));
188 (void)cpl_vector_unwrap(sedsy);
189 sedsy = NULL;
190 }
191
192 /* Save the table */
193 skip_if (isaac_util_seds_save(framelist, out, parlist));
194
195 end_skip;
196
197 cpl_bivector_unwrap_vectors(seds);
198 (void)cpl_vector_unwrap(sedsx1);
199 cpl_vector_delete(sedsx);
200 cpl_vector_delete(sedsy);
201 cpl_frameset_delete(rawframes);
202 cpl_table_delete(out);
203
204 return cpl_error_get_code();
205}
206
207/*----------------------------------------------------------------------------*/
216/*----------------------------------------------------------------------------*/
217static
218cpl_error_code isaac_util_seds_save(cpl_frameset * set,
219 const cpl_table * sed,
220 const cpl_parameterlist * parlist)
221{
222 cpl_propertylist * plist = cpl_propertylist_new();
223
224 bug_if(cpl_propertylist_append_string(plist, "INSTRUME", "ISAAC"));
225
226 /* Write the table */
227 skip_if(irplib_dfs_save_table(set,
228 parlist,
229 set,
230 sed,
231 NULL,
232 RECIPE_NAME,
233 ISAAC_UTIL_SEDS_RES,
234 plist,
235 NULL,
236 PACKAGE "/" PACKAGE_VERSION,
237 RECIPE_NAME CPL_DFS_FITS));
238
239 end_skip;
240
241 cpl_propertylist_delete(plist);
242
243 return cpl_error_get_code();
244}
245
246
int isaac_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: isaac_dfs.c:60
cpl_frameset * isaac_extract_frameset(const cpl_frameset *self, const char *tag)
Extract the frames with the given tag from a frameset.
Definition: isaac_utils.c:356