MOONS Pipeline Reference Manual 0.13.2
moo_dfs.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "cpl_errorstate.h"
21#include "cpl_frame.h"
22#include "cpl_msg.h"
23#include "cpl_propertylist.h"
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include "cpl_error.h"
29#include "cpl_frameset.h"
30
31/*-----------------------------------------------------------------------------
32 Includes
33 -----------------------------------------------------------------------------*/
34
35#include <string.h>
36#include <math.h>
37
38#include <cpl.h>
39
40#include "moo_pfits.h"
41#include "moo_dfs.h"
42
43/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
50
53/*----------------------------------------------------------------------------*/
64/*----------------------------------------------------------------------------*/
65cpl_frameset **
66moo_dfs_split_by_offset(cpl_frameset *set)
67{
68 cpl_frameset **res;
69 cpl_ensure(set != NULL, CPL_ERROR_NULL_INPUT, NULL);
70
71 res = cpl_calloc(sizeof(cpl_frameset *), 2);
72 res[0] = cpl_frameset_new();
73 res[1] = cpl_frameset_new();
74
75 int i;
76
77 for (i = 0; i < cpl_frameset_get_size(set); ++i) {
78 const cpl_frame *frame = cpl_frameset_get_position_const(set, i);
79 const char *filename = cpl_frame_get_filename(frame);
80 cpl_propertylist *list = cpl_propertylist_load(filename, 0);
81
82 double offset = moo_pfits_get_slit_offset(list);
83
84 if (fabs(offset - MOONS_SLIT_OFFSET_POS1) <=
85 MOONS_SLIT_OFFSET_EPSILON) {
86 cpl_frameset_insert(res[0], cpl_frame_duplicate(frame));
87 }
88 else if (fabs(offset - MOONS_SLIT_OFFSET_POS2) <=
89 MOONS_SLIT_OFFSET_EPSILON) {
90 cpl_frameset_insert(res[1], cpl_frame_duplicate(frame));
91 }
92 cpl_propertylist_delete(list);
93 }
94
95 return res;
96}
97
98cpl_error_code
99moo_dfs_group_offsets(const cpl_frameset *set,
100 cpl_frameset *group1,
101 cpl_frameset *group2,
102 cpl_frameset *other)
103{
104 cpl_ensure(set != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
105 cpl_ensure(group1 != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
106 cpl_ensure(group2 != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
107
108 cpl_errorstate status = cpl_errorstate_get();
109 cpl_frameset_iterator *it = cpl_frameset_iterator_new(set);
110 const cpl_frame *frame = NULL;
111 while ((frame = cpl_frameset_iterator_get_const(it)) != NULL) {
112 const char *filename = cpl_frame_get_filename(frame);
113
114 // FIXME: May need error handling for the next operation too.
115 const cpl_propertylist *properties = cpl_propertylist_load(filename, 0);
116
117 // Check slit position keyword in the frame header. If the keyword does not exist, i.e. there
118 // is not slit position for the current frame an error is set. Clear it and put the frame into
119 // the third group.
120 cpl_errorstate _status = cpl_errorstate_get();
121 double offset = moo_pfits_get_slit_offset(properties);
122 if (!cpl_errorstate_is_equal(_status) &&
123 (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND)) {
124 cpl_errorstate_set(_status);
125 }
126
127 if (fabs(offset - MOONS_SLIT_OFFSET_POS1) <=
128 MOONS_SLIT_OFFSET_EPSILON) {
129 cpl_frameset_insert(group1, cpl_frame_duplicate(frame));
130 }
131 else if (fabs(offset - MOONS_SLIT_OFFSET_POS2) <=
132 MOONS_SLIT_OFFSET_EPSILON) {
133 cpl_frameset_insert(group2, cpl_frame_duplicate(frame));
134 }
135 else if (other != NULL) {
136 cpl_frameset_insert(other, cpl_frame_duplicate(frame));
137 }
138 else {
139 // If no frameset is provided for collecting frames without slit position
140 // information, put it in both output framesets. This makes each of
141 // the a complete data set for each slit position
142 cpl_frameset_insert(group1, cpl_frame_duplicate(frame));
143 cpl_frameset_insert(group2, cpl_frame_duplicate(frame));
144 }
145 cpl_propertylist_delete((cpl_propertylist *)properties);
146
147 _status = cpl_errorstate_get();
148 cpl_frameset_iterator_advance(it, 1);
149 if (!cpl_errorstate_is_equal(_status) &&
150 (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE)) {
151 cpl_errorstate_set(_status);
152 }
153 }
154 cpl_frameset_iterator_delete(it);
155
156 if (!cpl_errorstate_is_equal(status)) {
157 return cpl_error_get_code();
158 }
159 return CPL_ERROR_NONE;
160}
161
162cpl_frameset *
163moo_dfs_extract_tag(const cpl_frameset *set, const char *tag)
164{
165 cpl_ensure(set != NULL, CPL_ERROR_NULL_INPUT, NULL);
166 cpl_ensure(tag != NULL, CPL_ERROR_NULL_INPUT, NULL);
167
168 cpl_frameset *selected = cpl_frameset_new();
169
170 cpl_errorstate status = cpl_errorstate_get();
171 cpl_frameset_iterator *it = cpl_frameset_iterator_new(set);
172 const cpl_frame *frame = NULL;
173 while ((frame = cpl_frameset_iterator_get_const(it)) != NULL) {
174 if (strcmp(tag, cpl_frame_get_tag(frame)) == 0) {
175 cpl_frameset_insert(selected, cpl_frame_duplicate(frame));
176 }
177 cpl_errorstate _status = cpl_errorstate_get();
178 cpl_frameset_iterator_advance(it, 1);
179 if (!cpl_errorstate_is_equal(_status) &&
180 (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE)) {
181 cpl_errorstate_set(_status);
182 }
183 }
184 cpl_frameset_iterator_delete(it);
185
186 if (!cpl_errorstate_is_equal(status)) {
187 cpl_frameset_delete(selected);
188 return NULL;
189 }
190 return selected;
191}
192
193/*----------------------------------------------------------------------------*/
203/*----------------------------------------------------------------------------*/
204
205cpl_error_code
206moo_dfs_set_groups(cpl_frameset *set)
207{
208 cpl_errorstate prestate = cpl_errorstate_get();
209
210
211 /* Loop on frames */
212 cpl_frameset_iterator *it = cpl_frameset_iterator_new(set);
213
214 int i = 0;
215 cpl_frame *frame = NULL;
216 while ((frame = cpl_frameset_iterator_get(it)) != NULL) {
217 const char *tag = cpl_frame_get_tag(frame);
218
219 if (tag == NULL) {
220 cpl_msg_warning(cpl_func, "Frame %d has no tag", i);
221 }
222 else if (!strcmp(tag, MOONS_TAG_BIAS) ||
223 !strcmp(tag, MOONS_TAG_LINEARITY) ||
224 !strcmp(tag, MOONS_TAG_LINEARITY_OFF) ||
225 !strcmp(tag, MOONS_TAG_OBJECT) ||
226 !strcmp(tag, MOONS_TAG_DARK_VIS) ||
227 !strcmp(tag, MOONS_TAG_DARK_NIR) ||
228 !strcmp(tag, MOONS_TAG_FLAT) ||
229 !strcmp(tag, MOONS_TAG_FLAT_ATTACHED) ||
230 !strcmp(tag, MOONS_TAG_FLAT_OFF) ||
231 !strcmp(tag, MOONS_TAG_ARC) ||
232 !strcmp(tag, MOONS_TAG_ARC_OFF) ||
233 !strcmp(tag, MOONS_TAG_STD_FLUX) ||
234 !strcmp(tag, MOONS_TAG_STD_TELL) ||
235 !strcmp(tag, MOONS_TAG_OBJECT_STARE) ||
236 !strcmp(tag, MOONS_TAG_OBJECT_STARENOD) ||
237 !strcmp(tag, MOONS_TAG_SKY_STARENOD) ||
238 !strcmp(tag, MOONS_TAG_OBJECT_XSWITCH) ||
239 !strcmp(tag, MOONS_TAG_MOLECFIT_SCI)) {
240 /* RAW frames */
241 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
242 }
243 else if (!strcmp(tag, MOONS_TAG_LINE_CATALOG) ||
244 !strcmp(tag, MOONS_TAG_FLUX_STD_CATALOG) ||
245 !strcmp(tag, MOONS_TAG_ATMOS_EXT) ||
246 !strcmp(tag, MOONS_TAG_ARC_LINE_LIST) ||
247 !strcmp(tag, MOONS_TAG_SKY_LINE_LIST) ||
248 !strcmp(tag, MOONS_TAG_MASTER_BIAS) ||
249 !strcmp(tag, MOONS_TAG_MASTER_DARK_VIS) ||
250 !strcmp(tag, MOONS_TAG_MASTER_DARK_NIR) ||
251 !strcmp(tag, MOONS_TAG_BP_MAP_RP) ||
252 !strcmp(tag, MOONS_TAG_BP_MAP_NL) ||
253 !strcmp(tag, MOONS_TAG_FF_TRACE_GUESS) ||
254 !strcmp(tag, MOONS_TAG_FF_TRACE) ||
255 !strcmp(tag, MOONS_TAG_P2P_MAP) ||
256 !strcmp(tag, MOONS_TAG_SPECTRAL_FORMAT) ||
257 !strcmp(tag, MOONS_TAG_LAYOUT) ||
258 !strcmp(tag, MOONS_TAG_WAVEMAP_GUESS) ||
259 !strcmp(tag, MOONS_TAG_WAVEMAP) ||
260 !strcmp(tag, MOONS_TAG_F2F_TABLE) ||
261 !strcmp(tag, MOONS_TAG_FF_EXTSPECTRA) ||
262 !strcmp(tag, MOONS_TAG_LINEARITY_COEFF_CUBE) ||
263 !strcmp(tag, MOONS_TAG_TELLURIC_CORR) ||
264 !strcmp(tag, MOONS_TAG_RESPONSE) ||
265 !strcmp(tag, MOONS_TAG_MOLECFIT_MOLECULES) ||
266 !strcmp(tag, MOONS_TAG_MOLECFIT_ATM_PARAMS) ||
267 !strcmp(tag, MOONS_TAG_MOLECFIT_BEST_FIT_PARAMS) ||
268 !strcmp(tag, MOONS_TAG_MOLECFIT_KERNEL_LIBRARY) ||
269 !strcmp(tag, MOONS_TAG_MOLECFIT_WINCLUDE)) {
270 /* CALIB frames */
271 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB);
272 }
273
274 cpl_frameset_iterator_advance(it, 1);
275 ++i;
276 }
277 cpl_frameset_iterator_delete(it);
278
279 if (!cpl_errorstate_is_equal(prestate)) {
280 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
281 "Could not identify RAW and CALIB "
282 "frames");
283 }
284
285 return CPL_ERROR_NONE;
286}
287
288/*----------------------------------------------------------------------------*/
298/*----------------------------------------------------------------------------*/
299double *
300moo_dfs_get_exptime(cpl_frameset *set)
301{
302 double *res = NULL;
303 cpl_ensure(set != NULL, CPL_ERROR_NULL_INPUT, NULL);
304 int size = cpl_frameset_get_size(set);
305 res = cpl_malloc(sizeof(double) * size);
306
307 int i;
308
309 for (i = 0; i < size; i++) {
310 const cpl_frame *frame = cpl_frameset_get_position_const(set, i);
311 const char *filename = cpl_frame_get_filename(frame);
312 cpl_propertylist *list = cpl_propertylist_load(filename, 0);
313 res[i] = moo_pfits_get_exptime(list);
314 cpl_propertylist_delete(list);
315 }
316
317 return res;
318}
319
320
double * moo_dfs_get_exptime(cpl_frameset *set)
Get the EXPTIME from the frameset.
Definition: moo_dfs.c:300
cpl_error_code moo_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: moo_dfs.c:206
cpl_frameset ** moo_dfs_split_by_offset(cpl_frameset *set)
Get the EXPTIME from the frameset.
Definition: moo_dfs.c:66
int moo_pfits_get_slit_offset(const cpl_propertylist *plist)
find out the INS SLIT OFFSET value
Definition: moo_pfits.c:1110
double moo_pfits_get_exptime(const cpl_propertylist *plist)
find out the EXPTIME value
Definition: moo_pfits.c:1039