MOONS Pipeline Reference Manual 0.13.2
moons_molecfit_calctrans.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#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include "moo_utils.h"
29#include "moo_pfits.h"
30#include "moo_dfs.h"
31#include "moo_params.h"
32#include "moo_drl.h"
33#include "moo_products.h"
34#include <cpl.h>
35#include "hdrl.h"
36#include <string.h>
37#include "moo_molecfit.h"
38/*-----------------------------------------------------------------------------
39 Plugin registration
40 -----------------------------------------------------------------------------*/
41
42int cpl_plugin_get_info(cpl_pluginlist *list);
43
44/*-----------------------------------------------------------------------------
45 Private function prototypes
46 -----------------------------------------------------------------------------*/
47
48static int _moons_molecfit_calctrans_create(cpl_plugin *plugin);
49static int _moons_molecfit_calctrans_exec(cpl_plugin *plugin);
50static int _moons_molecfit_calctrans_destroy(cpl_plugin *plugin);
51static int _moons_molecfit_calctrans(cpl_frameset *frameset,
52 const cpl_parameterlist *parlist);
53
54/*-----------------------------------------------------------------------------
55 Static variables
56 -----------------------------------------------------------------------------*/
57
58static const char *const _moons_molecfit_calctrans_description =
59 "Produces the model using molecfit\n"
60 "INPUT FRAMES\n"
61 " * file (RBN) with tag SCIENCE 1 file : "
62 "molecfit_calctrans star file\n"
63 " * file (MOLECULES) with tag MOLECULES 1 file : "
64 "molecules list file\n"
65 "PRODUCTS\n"
66 " * GDAS User GDAS profile"
67 " * GDAS_BEFORE If ESO DB GDAS is used, file before the MJD-OBS"
68 " * GDAS_AFTER If ESO DB GDAS is used, file after the MJD-OBS"
69 "\n";
70
71/*-----------------------------------------------------------------------------
72 Function code
73 -----------------------------------------------------------------------------*/
74
75/*----------------------------------------------------------------------------*/
85/*----------------------------------------------------------------------------*/
86
87int
88cpl_plugin_get_info(cpl_pluginlist *list)
89{
90 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
91 cpl_plugin *plugin = &recipe->interface;
92
93 if (cpl_plugin_init(plugin, CPL_PLUGIN_API, MOONS_BINARY_VERSION,
94 CPL_PLUGIN_TYPE_RECIPE, "moons_molecfit_calctrans",
95 "Produces the instrumental response for relative flux "
96 "calibration, and the telluric absorption spectrum.",
97 _moons_molecfit_calctrans_description, "Regis Haigron",
98 PACKAGE_BUGREPORT, moo_get_license(),
99 _moons_molecfit_calctrans_create,
100 _moons_molecfit_calctrans_exec,
101 _moons_molecfit_calctrans_destroy)) {
102 cpl_msg_error(cpl_func, "Plugin initialization failed");
103 (void)cpl_error_set_where(cpl_func);
104 return 1;
105 }
106
107 if (cpl_pluginlist_append(list, plugin)) {
108 cpl_msg_error(cpl_func, "Error adding plugin to list");
109 (void)cpl_error_set_where(cpl_func);
110 return 1;
111 }
112
113 return 0;
114}
115
116
117/*----------------------------------------------------------------------------*/
125/*----------------------------------------------------------------------------*/
126
127static int
128_moons_molecfit_calctrans_create(cpl_plugin *plugin)
129{
130 cpl_recipe *recipe;
131
132 /* Do not create the recipe if an error code is already set */
133 if (cpl_error_get_code() != CPL_ERROR_NONE) {
134 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
135 cpl_func, __LINE__, cpl_error_get_where());
136 return (int)cpl_error_get_code();
137 }
138
139 if (plugin == NULL) {
140 cpl_msg_error(cpl_func, "Null plugin");
141 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
142 }
143
144 /* Verify plugin type */
145 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
146 cpl_msg_error(cpl_func, "Plugin is not a recipe");
147 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
148 }
149
150 /* Get the recipe */
151 recipe = (cpl_recipe *)plugin;
152
153 /* Create the parameters list in the cpl_recipe object */
154 recipe->parameters = cpl_parameterlist_new();
155 if (recipe->parameters == NULL) {
156 cpl_msg_error(cpl_func, "Parameter list allocation failed");
157 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
158 }
159
160 moo_params *params = moo_params_new("moons", "moons_molecfit_calctrans");
161
162 /* Fill the parameters list */
163 moo_params_add_keep_temp(params, recipe->parameters);
164 moo_params_add_molecfit_calctrans(params, recipe->parameters);
165 moo_params_delete(params);
166
167 return 0;
168}
169
170
171/*----------------------------------------------------------------------------*/
177/*----------------------------------------------------------------------------*/
178
179static int
180_moons_molecfit_calctrans_exec(cpl_plugin *plugin)
181{
182 cpl_recipe *recipe;
183 int recipe_status;
184 cpl_errorstate initial_errorstate = cpl_errorstate_get();
185
186 /* Return immediately if an error code is already set */
187 if (cpl_error_get_code() != CPL_ERROR_NONE) {
188 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
189 cpl_func, __LINE__, cpl_error_get_where());
190 return (int)cpl_error_get_code();
191 }
192
193 if (plugin == NULL) {
194 cpl_msg_error(cpl_func, "Null plugin");
195 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
196 }
197
198 /* Verify plugin type */
199 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
200 cpl_msg_error(cpl_func, "Plugin is not a recipe");
201 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
202 }
203
204 /* Get the recipe */
205 recipe = (cpl_recipe *)plugin;
206
207 /* Verify parameter and frame lists */
208 if (recipe->parameters == NULL) {
209 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
210 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
211 }
212 if (recipe->frames == NULL) {
213 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
214 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
215 }
216
217 /* Invoke the recipe */
218 recipe_status =
219 _moons_molecfit_calctrans(recipe->frames, recipe->parameters);
220
221 /* Ensure DFS-compliance of the products */
222 if (cpl_dfs_update_product_header(recipe->frames)) {
223 if (!recipe_status)
224 recipe_status = (int)cpl_error_get_code();
225 }
226
227 if (!cpl_errorstate_is_equal(initial_errorstate)) {
228 /* Dump the error history since recipe execution start.
229 At this point the recipe cannot recover from the error */
230 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
231 }
232
233 return recipe_status;
234}
235
236
237/*----------------------------------------------------------------------------*/
243/*----------------------------------------------------------------------------*/
244
245static int
246_moons_molecfit_calctrans_destroy(cpl_plugin *plugin)
247{
248 cpl_recipe *recipe;
249
250 if (plugin == NULL) {
251 cpl_msg_error(cpl_func, "Null plugin");
252 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
253 }
254
255 /* Verify plugin type */
256 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
257 cpl_msg_error(cpl_func, "Plugin is not a recipe");
258 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
259 }
260
261 /* Get the recipe */
262 recipe = (cpl_recipe *)plugin;
263
264 cpl_parameterlist_delete(recipe->parameters);
265
266 return 0;
267}
268
269static cpl_error_code
270_moons_molecfit_calctrans_check_sof(cpl_frameset *frameset,
271 const cpl_frame **sci_frame,
272 const cpl_frame **molecules_frame,
273 const cpl_frame **atm_frame,
274 const cpl_frame **best_fit_frame,
275 const cpl_frame **kernel_lib_frame)
276{
277 cpl_ensure_code(moo_dfs_set_groups(frameset) == CPL_ERROR_NONE,
278 cpl_error_get_code());
279 int i;
280
281 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
282 cpl_frame *current_frame = cpl_frameset_get_position(frameset, i);
283 if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_MOLECFIT_SCI)) {
284 *sci_frame = current_frame;
285 }
286 else if (!strcmp(cpl_frame_get_tag(current_frame),
287 MOONS_TAG_MOLECFIT_MOLECULES)) {
288 *molecules_frame = current_frame;
289 }
290 else if (!strcmp(cpl_frame_get_tag(current_frame),
291 MOONS_TAG_MOLECFIT_ATM_PARAMS)) {
292 *atm_frame = current_frame;
293 }
294 else if (!strcmp(cpl_frame_get_tag(current_frame),
295 MOONS_TAG_MOLECFIT_BEST_FIT_PARAMS)) {
296 *best_fit_frame = current_frame;
297 }
298 else if (!strcmp(cpl_frame_get_tag(current_frame),
299 MOONS_TAG_MOLECFIT_KERNEL_LIBRARY)) {
300 *kernel_lib_frame = current_frame;
301 }
302 }
303 if (*sci_frame == NULL) {
304 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
305 "SOF does not have any file tagged "
306 "with %s",
307 MOONS_TAG_MOLECFIT_SCI);
308 }
309
310 if (*molecules_frame == NULL) {
311 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
312 "SOF does not have any file tagged "
313 "with %s",
314 MOONS_TAG_MOLECFIT_MOLECULES);
315 }
316
317 if (*atm_frame == NULL) {
318 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
319 "SOF does not have any file tagged "
320 "with %s",
321 MOONS_TAG_MOLECFIT_ATM_PARAMS);
322 }
323
324 if (*best_fit_frame == NULL) {
325 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
326 "SOF does not have any file tagged "
327 "with %s",
328 MOONS_TAG_MOLECFIT_BEST_FIT_PARAMS);
329 }
330 return CPL_ERROR_NONE;
331}
332
333
334/*----------------------------------------------------------------------------*/
341/*----------------------------------------------------------------------------*/
342static int
343_moons_molecfit_calctrans(cpl_frameset *frameset,
344 const cpl_parameterlist *parlist)
345{
346 /* parameters */
347 moo_mode_type mode;
348 const char *modename = NULL;
349
350 const cpl_frame *science_frame = NULL;
351 const cpl_frame *molecule_frame = NULL;
352 const cpl_frame *atm_frame = NULL;
353 const cpl_frame *best_fit_frame = NULL;
354 const cpl_frame *kernel_lib_frame = NULL;
355 moo_telluric *telluric = NULL;
356 char *telluric_filename = NULL;
357 moo_molecfit_calctrans_params *molecfit_calctrans_params = NULL;
358
359 moo_products *products =
360 moo_products_new(frameset, parlist, "moons_molecfit_calctrans",
361 PACKAGE "/" PACKAGE_VERSION);
362
363
364 const moo_params *params = moo_products_get_params(products);
365 moo_try_check(molecfit_calctrans_params =
366 moo_params_get_molecfit_calctrans(params, parlist),
367 " ");
368
369 /* SOF file */
370 moo_try_check(_moons_molecfit_calctrans_check_sof(frameset, &science_frame,
371 &molecule_frame,
372 &atm_frame,
373 &best_fit_frame,
374 &kernel_lib_frame),
375 " ");
376
377 moo_try_check(mode = moo_mode_get(science_frame), " ");
378 modename = moo_mode_get_name(mode);
379
380 moo_try_check(telluric =
381 moo_molecfit_calctrans(science_frame, modename,
382 molecule_frame, atm_frame,
383 best_fit_frame, kernel_lib_frame,
384 molecfit_calctrans_params),
385 " ");
386 telluric_filename = cpl_sprintf("%s_%s.fits", MOONS_TAG_TELLURIC_CORR,
387 moo_mode_get_name(mode));
388
389 moo_try_check(moo_products_add_telluric(products, telluric,
390 CPL_FRAME_LEVEL_FINAL,
391 MOONS_TAG_TELLURIC_CORR,
392 telluric_filename, science_frame),
393 " ");
394
395moo_try_cleanup:
396 cpl_free(telluric_filename);
397 moo_telluric_delete(telluric);
398 moo_molecfit_calctrans_params_delete(molecfit_calctrans_params);
399 moo_products_delete(products);
400 return (int)cpl_error_get_code();
401}
moo_mode_type moo_mode_get(const cpl_frame *frame)
Get the name of a mode from a frame.
Definition: moo_detector.c:224
const char * moo_mode_get_name(moo_mode_type type)
Get the name of a mode.
Definition: moo_detector.c:204
cpl_error_code moo_params_add_molecfit_calctrans(moo_params *self, cpl_parameterlist *list)
Add default parameters for molecfit_calctrans.
Definition: moo_params.c:2409
moo_molecfit_calctrans_params * moo_params_get_molecfit_calctrans(const moo_params *self, const cpl_parameterlist *list)
Get molecfit calctrans parameters from moons parameters list.
Definition: moo_params.c:1584
void moo_molecfit_calctrans_params_delete(moo_molecfit_calctrans_params *self)
Delete a moo_molecfit_calctrans_params.
Definition: moo_params.c:2316
void moo_params_delete(moo_params *self)
Delete a moo_params.
Definition: moo_params.c:85
cpl_error_code moo_params_add_keep_temp(moo_params *self, cpl_parameterlist *list)
Add default parameters for keep-temp.
Definition: moo_params.c:932
moo_params * moo_params_new(const char *pid, const char *recipe_id)
Create a new moo_params.
Definition: moo_params.c:62
void moo_telluric_delete(moo_telluric *self)
Delete a moo_telluric.
Definition: moo_telluric.c:334
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
moo_telluric * moo_molecfit_calctrans(const cpl_frame *rbn_frame, const char *modename, const cpl_frame *molecules_frame, const cpl_frame *atm_frame, const cpl_frame *best_fit_frame, const cpl_frame *kernel_lib_frame, moo_molecfit_calctrans_params *params)
Apply the molcecfit calctrans to the RBN to compute telle telluric correction.
moo_products * moo_products_new(cpl_frameset *framelist, const cpl_parameterlist *parlist, const char *recid, const char *pipeline_id)
create a moo_product object for a recipe
Definition: moo_products.c:53
const moo_params * moo_products_get_params(const moo_products *self)
get the moo_params object
Definition: moo_products.c:87
cpl_frame * moo_products_add_telluric(moo_products *self, moo_telluric *tell, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a TELLURIC object
Definition: moo_products.c:508
const char * moo_get_license(void)
Get the pipeline copyright and license.
Definition: moo_utils.c:81