MOONS Pipeline Reference Manual 0.13.1
moons_science_starenod.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_badpix.h"
34#include "moo_extlist.h"
35#include "moo_correct_tell.h"
36#include "moo_subtract_nod.h"
37#include "moo_combine_pair.h"
38#include "moo_products.h"
39#include <cpl.h>
40
41#include <string.h>
42
43/*-----------------------------------------------------------------------------
44 Plugin registration
45 -----------------------------------------------------------------------------*/
46
47int cpl_plugin_get_info(cpl_pluginlist *list);
48
49/*-----------------------------------------------------------------------------
50 Private function prototypes
51 -----------------------------------------------------------------------------*/
52
53static int _moons_science_starenod_create(cpl_plugin *plugin);
54static int _moons_science_starenod_exec(cpl_plugin *plugin);
55static int _moons_science_starenod_destroy(cpl_plugin *plugin);
56static int _moons_science_starenod(cpl_frameset *frameset,
57 const cpl_parameterlist *parlist);
58
59/*-----------------------------------------------------------------------------
60 Static variables
61 -----------------------------------------------------------------------------*/
62static const char *const _moons_science_starenod_description =
63 "From an observation sequence in StareNod mode, this recipe removes the"
64 " instrumental signature and provides calibrated science products. During "
65 "the"
66 " sequence, the telescope nods from one position on targets (labeled as T "
67 "in "
68 "Fig. 5-12) to a blank sky position (S). In the sequence, different slit "
69 "offset"
70 " positions can be used: as represented in Fig. 5-12, the combination is "
71 "done "
72 "with 1D wavelength calibrated sky subtracted spectra. Using different "
73 "slit "
74 "offset positions implies that different sets of calibration (FF, wave "
75 "maps) "
76 "are given as inputs.\n"
77 "INPUT FRAMES\n"
78 " * science exposure frames n>=1 file(s) (RAW) with tag "
79 "OBJECT_STARENOD : "
80 "science frame in stare+nod mode file\n"
81 " * sky exposure frames n file(s) (RAW) with tag "
82 "SKY_STARENOD : "
83 "Sky Frames illuminated by blank sky only, obtained in the Stare+Nod"
84 " observation mode. The amount of exposure time (or DIT × NDIT for NIR), "
85 "as "
86 "well as the number of exposures, can be less than for the corresponding "
87 "OBJECT_STARENOD frames.\n"
88 " * [OPTIONAL] ReferenceBadPixMask 1 file (QUA) with tag "
89 "BP_MAP_RP : "
90 "cosmetic bad pixel map\n"
91 " * [OPTIONAL] NonLinearityBadPixMask 1 file (QUA) with tag "
92 "BP_MAP_NL : "
93 "cosmetic bad pixel map coming from linearity recipe\n"
94 " * MasterBias 1 file (DET) with tag "
95 "MASTER_BIAS : "
96 "master bias file\n"
97 " * MasterDarkNir 1 file (DET) with tag "
98 "MASTER_DARK_NIR : "
99 "master dark nir for object\n"
100 " * [OPTIONAL] MasterDarkNir 1 file (DET) with tag "
101 "MASTER_DARK_NIR : "
102 "master dark nir file for sky (if exposure time is different from object)\n"
103 " * [OPTIONAL] MasterDarkVis 1 file (DET) with tag "
104 "MASTER_DARK_VIS : "
105 "master dark vis file\n"
106 " * [OPTIONAL] CoeffsCube 1 or 2 file(s) one by offset (3D) with tag "
107 "LINEARITY_COEFF_CUBE : "
108 "coefficients to correct pixels detector linearity\n"
109 " * [OPTIONAL] P2pMap 1 file (DET) with tag "
110 "P2P_MAP : "
111 "pixel to pixel map\n"
112 " * Fftrace 1 or 2 file(s) one by offset (LOC) with tag "
113 "FF_TRACE :"
114 "the localisation table\n"
115 " * [OPTIONAL] MasterFlat 1 or 2 file(s) one by offset (PSF) with tag "
116 "MASTER_FLAT : "
117 "the master flat used for optimal extraction\n"
118 " * Ffext 1 or 2 file(s) one by offset (EXT) with tag "
119 "FF_EXTSPECTRA : "
120 "the extracted flat field\n"
121 " * F2f [OPTIONAL] 1 or 2 file(s) one by offset (F2F) with tag "
122 "F2F_TABLE :"
123 "the fibre-to-fibre relative response\n"
124 " * SFormat 1 file (FMT) with tag "
125 "SPECTRAL_FORMAT : "
126 "the spectral format table\n"
127 " * WaveMap 1 or 2 file(s) one by offset (WMAP) with tag "
128 "WAVE_MAP : "
129 "the wavelength map\n"
130 " * SkyLines 1 file (CAT) with tag "
131 "SKY_LINE_LIST : "
132 "the sky lines list table\n"
133 " * [OPTIONAL] response 1 file (RESPONSE) with "
134 "tag RESPONSE : "
135 "the response spectra in the extracted pixel space\n"
136 " * [OPTIONAL] TellCorr 1 file (TELLURIC_CORR) "
137 "with tag TELLURIC_CORR : "
138 "the telluric correction spectra\n"
139 " * [OPTIONAL] atmosExt 1 file (ATMO) with tag "
140 "ATMOS_EXT : "
141 "the atmospheric extinction table\n"
142 "PRODUCTS\n"
143 " * STARENOD_RBNSPECTRA_OFFSET[offset]_[insmode]_i.fits (RBN) with tag "
144 "STARENOD_RBNSPECTRA : "
145 "Science product individual calibrated frames\n"
146 " * STARENOD_RBNFLXSPECTRA_OFFSET[offset]_[insmode]_i.fits (RBN) with tag "
147 "STARENOD_RBNFLXSPECTRA : "
148 "Science product individual flux calibrated frames\n"
149 " * STARENOD_SKSSPECTRA_OFFSET[offset]_[insmode]_i.fits (SCI) with tag "
150 "STARENOD_SKSSPECTRA : "
151 "Science product individual sky subtracted frames\n"
152 " * STARENOD_SKSSPECTRA_[insmode].fits (SCI) with tag "
153 "STARENOD_SKSSPECTRA : "
154 "Science product coadd calibrated frame\n"
155 "\n";
156
157/*-----------------------------------------------------------------------------
158 Function code
159 -----------------------------------------------------------------------------*/
160
161/*----------------------------------------------------------------------------*/
171/*----------------------------------------------------------------------------*/
172
173int
174cpl_plugin_get_info(cpl_pluginlist *list)
175{
176 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
177 cpl_plugin *plugin = &recipe->interface;
178 if (cpl_plugin_init(plugin, CPL_PLUGIN_API, MOONS_BINARY_VERSION,
179 CPL_PLUGIN_TYPE_RECIPE, "moons_science_starenod",
180 "Reduces science exposure in stare and stare+nod modes",
181 _moons_science_starenod_description, "Regis Haigron",
182 PACKAGE_BUGREPORT, moo_get_license(),
183 _moons_science_starenod_create,
184 _moons_science_starenod_exec,
185 _moons_science_starenod_destroy)) {
186 cpl_msg_error(cpl_func, "Plugin initialization failed");
187 (void)cpl_error_set_where(cpl_func);
188 return 1;
189 }
190 if (cpl_pluginlist_append(list, plugin)) {
191 cpl_msg_error(cpl_func, "Error adding plugin to list");
192 (void)cpl_error_set_where(cpl_func);
193 return 1;
194 }
195 return 0;
196}
197
198
199/*----------------------------------------------------------------------------*/
207/*----------------------------------------------------------------------------*/
208
209static int
210_moons_science_starenod_create(cpl_plugin *plugin)
211{
212 cpl_recipe *recipe;
213 /* Do not create the recipe if an error code is already set */
214 if (cpl_error_get_code() != CPL_ERROR_NONE) {
215 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
216 cpl_func, __LINE__, cpl_error_get_where());
217 return (int)cpl_error_get_code();
218 }
219 if (plugin == NULL) {
220 cpl_msg_error(cpl_func, "Null plugin");
221 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
222 }
223 /* Verify plugin type */
224 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
225 cpl_msg_error(cpl_func, "Plugin is not a recipe");
226 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
227 }
228 /* Get the recipe */
229 recipe = (cpl_recipe *)plugin;
230 /* Create the parameters list in the cpl_recipe object */
231 recipe->parameters = cpl_parameterlist_new();
232 if (recipe->parameters == NULL) {
233 cpl_msg_error(cpl_func, "Parameter list allocation failed");
234 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
235 }
236 moo_params *params = moo_params_new("moons", "moons_science_starenod");
237 /* Fill the parameters list */
238 moo_params_add_keep_temp(params, recipe->parameters);
239 moo_params_add_prepare(params, recipe->parameters);
240 moo_params_add_correct_bias(params, recipe->parameters,
241 MOO_CORRECT_BIAS_METHOD_MASTER);
242 moo_params_add_extract(params, recipe->parameters);
243 moo_params_add_science_wavesol(params, recipe->parameters);
244 moo_params_add_rebin(params, recipe->parameters);
245 moo_params_add_combine_pair(params, recipe->parameters, CPL_FALSE);
246 moo_params_add_target_table(params, recipe->parameters, CPL_FALSE);
247 moo_params_add_sub_sky_stare_wnone(params, recipe->parameters);
248 moo_params_add_coadd(params, recipe->parameters);
249 moo_params_add_create_s1d(params, recipe->parameters);
250 moo_params_delete(params);
251 return 0;
252}
253
254
255/*----------------------------------------------------------------------------*/
261/*----------------------------------------------------------------------------*/
262
263static int
264_moons_science_starenod_exec(cpl_plugin *plugin)
265{
266 cpl_recipe *recipe;
267 int recipe_status;
268 cpl_errorstate initial_errorstate = cpl_errorstate_get();
269 /* Return immediately if an error code is already set */
270 if (cpl_error_get_code() != CPL_ERROR_NONE) {
271 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
272 cpl_func, __LINE__, cpl_error_get_where());
273 return (int)cpl_error_get_code();
274 }
275 if (plugin == NULL) {
276 cpl_msg_error(cpl_func, "Null plugin");
277 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
278 }
279 /* Verify plugin type */
280 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
281 cpl_msg_error(cpl_func, "Plugin is not a recipe");
282 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
283 }
284 /* Get the recipe */
285 recipe = (cpl_recipe *)plugin;
286 /* Verify parameter and frame lists */
287 if (recipe->parameters == NULL) {
288 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
289 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
290 }
291 if (recipe->frames == NULL) {
292 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
293 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
294 }
295 /* Invoke the recipe */
296 recipe_status = _moons_science_starenod(recipe->frames, recipe->parameters);
297 /* Ensure DFS-compliance of the products */
298 if (cpl_dfs_update_product_header(recipe->frames)) {
299 if (!recipe_status)
300 recipe_status = (int)cpl_error_get_code();
301 }
302 if (!cpl_errorstate_is_equal(initial_errorstate)) {
303 /* Dump the error history since recipe execution start.
304 At this point the recipe cannot recover from the error */
305 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
306 }
307 return recipe_status;
308}
309
310
311/*----------------------------------------------------------------------------*/
317/*----------------------------------------------------------------------------*/
318
319static int
320_moons_science_starenod_destroy(cpl_plugin *plugin)
321{
322 cpl_recipe *recipe;
323 if (plugin == NULL) {
324 cpl_msg_error(cpl_func, "Null plugin");
325 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
326 }
327 /* Verify plugin type */
328 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
329 cpl_msg_error(cpl_func, "Plugin is not a recipe");
330 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
331 }
332 /* Get the recipe */
333 recipe = (cpl_recipe *)plugin;
334 cpl_parameterlist_delete(recipe->parameters);
335 return 0;
336}
337
338static cpl_error_code
339_moons_science_starenod_check_sof(cpl_frameset *frameset,
340 cpl_frameset *offset_frameset,
341 const char **bpmap_rp_name,
342 const char **bpmap_nl_name,
343 const cpl_frame **masterbias,
344 const cpl_frame **masterdark_vis,
345 cpl_frameset *masterdark_nir,
346 const cpl_frame **p2pmap,
347 const cpl_frame **sformat,
348 const cpl_frame **sky_lines,
349 const cpl_frame **atmo,
350 const cpl_frame **resp,
351 const cpl_frame **tell,
352 const cpl_frame **airglow_group,
353 const cpl_frame **airglow_var,
354 const cpl_frame **solflux,
355 const moo_sub_sky_stare_params *subsky_params)
356{
357 cpl_ensure_code(moo_dfs_set_groups(frameset) == CPL_ERROR_NONE,
358 cpl_error_get_code());
359 int i;
360 int nb_sky = 0;
361 int nb_stare = 0;
362 int nb_starenod = 0;
363
364 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
365 cpl_frame *current_frame = cpl_frameset_get_position(frameset, i);
366 if (!strcmp(cpl_frame_get_tag(current_frame),
367 MOONS_TAG_OBJECT_STARENOD)) {
368 cpl_frameset_insert(offset_frameset,
369 cpl_frame_duplicate(current_frame));
370 nb_starenod++;
371 }
372 else if (!strcmp(cpl_frame_get_tag(current_frame),
373 MOONS_TAG_OBJECT_STARE)) {
374 cpl_frameset_insert(offset_frameset,
375 cpl_frame_duplicate(current_frame));
376 nb_stare++;
377 }
378 else if (!strcmp(cpl_frame_get_tag(current_frame),
379 MOONS_TAG_SKY_STARENOD)) {
380 cpl_frameset_insert(offset_frameset,
381 cpl_frame_duplicate(current_frame));
382 nb_sky++;
383 }
384 else if (!strcmp(cpl_frame_get_tag(current_frame),
385 MOONS_TAG_BP_MAP_RP)) {
386 *bpmap_rp_name = cpl_frame_get_filename(current_frame);
387 }
388 else if (!strcmp(cpl_frame_get_tag(current_frame),
389 MOONS_TAG_BP_MAP_NL)) {
390 *bpmap_nl_name = cpl_frame_get_filename(current_frame);
391 }
392 else if (!strcmp(cpl_frame_get_tag(current_frame),
393 MOONS_TAG_MASTER_BIAS)) {
394 *masterbias = current_frame;
395 }
396 else if (!strcmp(cpl_frame_get_tag(current_frame),
397 MOONS_TAG_MASTER_DARK_VIS)) {
398 *masterdark_vis = current_frame;
399 }
400 else if (!strcmp(cpl_frame_get_tag(current_frame),
401 MOONS_TAG_MASTER_DARK_NIR)) {
402 cpl_frameset_insert(masterdark_nir,
403 cpl_frame_duplicate(current_frame));
404 }
405 else if (!strcmp(cpl_frame_get_tag(current_frame),
406 MOONS_TAG_MASTER_FLAT)) {
407 cpl_frameset_insert(offset_frameset,
408 cpl_frame_duplicate(current_frame));
409 }
410 else if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_P2P_MAP)) {
411 *p2pmap = current_frame;
412 }
413 else if (!strcmp(cpl_frame_get_tag(current_frame),
414 MOONS_TAG_FF_TRACE)) {
415 cpl_frameset_insert(offset_frameset,
416 cpl_frame_duplicate(current_frame));
417 }
418 else if (!strcmp(cpl_frame_get_tag(current_frame),
419 MOONS_TAG_LINEARITY_COEFF_CUBE)) {
420 cpl_frameset_insert(offset_frameset,
421 cpl_frame_duplicate(current_frame));
422 }
423 else if (!strcmp(cpl_frame_get_tag(current_frame),
424 MOONS_TAG_SPECTRAL_FORMAT)) {
425 *sformat = current_frame;
426 }
427 else if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_WAVEMAP)) {
428 cpl_frameset_insert(offset_frameset,
429 cpl_frame_duplicate(current_frame));
430 }
431 else if (!strcmp(cpl_frame_get_tag(current_frame),
432 MOONS_TAG_SKY_LINE_LIST)) {
433 *sky_lines = current_frame;
434 }
435 else if (!strcmp(cpl_frame_get_tag(current_frame),
436 MOONS_TAG_FF_EXTSPECTRA)) {
437 cpl_frameset_insert(offset_frameset,
438 cpl_frame_duplicate(current_frame));
439 }
440 else if (!strcmp(cpl_frame_get_tag(current_frame),
441 MOONS_TAG_F2F_TABLE)) {
442 cpl_frameset_insert(offset_frameset,
443 cpl_frame_duplicate(current_frame));
444 }
445 else if (!strcmp(cpl_frame_get_tag(current_frame),
446 MOONS_TAG_ATMOS_EXT)) {
447 *atmo = current_frame;
448 }
449 else if (!strcmp(cpl_frame_get_tag(current_frame),
450 MOONS_TAG_RESPONSE)) {
451 *resp = current_frame;
452 }
453 else if (!strcmp(cpl_frame_get_tag(current_frame),
454 MOONS_TAG_TELLURIC_CORR)) {
455 *tell = current_frame;
456 }
457 else if (!strcmp(cpl_frame_get_tag(current_frame),
458 MOONS_TAG_AIRGLOW_GROUP)) {
459 *airglow_group = current_frame;
460 }
461 else if (!strcmp(cpl_frame_get_tag(current_frame),
462 MOONS_TAG_AIRGLOW_VAR)) {
463 *airglow_var = current_frame;
464 }
465 else if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_SOLFLUX)) {
466 *solflux = current_frame;
467 }
468 }
469 if (nb_stare > 0 && nb_starenod > 0) {
470 return (int)cpl_error_set_message(
471 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
472 "SOF have mixed file tags STARE and STARENOD");
473 }
474 if (nb_stare > 0 && nb_sky > 0) {
475 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
476 "remove SKY from sof");
477 }
478 if (nb_starenod > 0 && nb_sky <= 0) {
479 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
480 "SOF does not have any file tagged "
481 "with %s",
482 MOONS_TAG_SKY_STARENOD);
483 }
484 if (*sformat == NULL) {
485 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
486 "SOF does not have any file tagged "
487 "with %s",
488 MOONS_TAG_SPECTRAL_FORMAT);
489 }
490 if (*sky_lines == NULL) {
491 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
492 "SOF does not have any file tagged "
493 "with %s",
494 MOONS_TAG_SKY_LINE_LIST);
495 }
496 if (*airglow_group == NULL &&
497 strcmp(subsky_params->method, MOO_SUB_SKY_STARE_METHOD_SKYCORR) == 0) {
498 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
499 "SOF does not have any file tagged "
500 "with %s",
501 MOONS_TAG_AIRGLOW_GROUP);
502 }
503 if (*airglow_var == NULL &&
504 strcmp(subsky_params->method, MOO_SUB_SKY_STARE_METHOD_SKYCORR) == 0) {
505 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
506 "SOF does not have any file tagged "
507 "with %s",
508 MOONS_TAG_AIRGLOW_VAR);
509 }
510 if (*solflux == NULL &&
511 strcmp(subsky_params->method, MOO_SUB_SKY_STARE_METHOD_SKYCORR) == 0) {
512 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
513 "SOF does not have any file tagged "
514 "with %s",
515 MOONS_TAG_SOLFLUX);
516 }
517 return CPL_ERROR_NONE;
518}
519
520static cpl_error_code
521_moons_science_starenod_check_sof_byoffset(cpl_frameset *frameset,
522 cpl_frameset *object_frameset,
523 cpl_frameset *sky_frameset,
524 const cpl_frame **master_flat,
525 const cpl_frame **fftrace,
526 const cpl_frame **coeffs_cube,
527 const cpl_frame **wmap,
528 const cpl_frame **flat,
529 const cpl_frame **f2f,
530 moo_extract_params *ext_params,
531 moo_sub_sky_stare_params *sky_params,
533{
534 cpl_ensure_code(moo_dfs_set_groups(frameset) == CPL_ERROR_NONE,
535 cpl_error_get_code());
536 int nb_object = 0;
537 int nb_stare = 0;
538 int nb_sky = 0;
539 int i;
540 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
541 cpl_frame *current_frame = cpl_frameset_get_position(frameset, i);
542 if (!strcmp(cpl_frame_get_tag(current_frame),
543 MOONS_TAG_OBJECT_STARENOD)) {
544 cpl_frameset_insert(object_frameset,
545 cpl_frame_duplicate(current_frame));
546 nb_object++;
547 }
548 else if (!strcmp(cpl_frame_get_tag(current_frame),
549 MOONS_TAG_OBJECT_STARE)) {
550 cpl_frameset_insert(object_frameset,
551 cpl_frame_duplicate(current_frame));
552 nb_stare++;
553 nb_object++;
554 }
555 else if (!strcmp(cpl_frame_get_tag(current_frame),
556 MOONS_TAG_SKY_STARENOD)) {
557 cpl_frameset_insert(sky_frameset,
558 cpl_frame_duplicate(current_frame));
559 nb_sky++;
560 }
561 else if (!strcmp(cpl_frame_get_tag(current_frame),
562 MOONS_TAG_MASTER_FLAT)) {
563 *master_flat = current_frame;
564 }
565 else if (!strcmp(cpl_frame_get_tag(current_frame),
566 MOONS_TAG_FF_TRACE)) {
567 *fftrace = current_frame;
568 }
569 else if (!strcmp(cpl_frame_get_tag(current_frame),
570 MOONS_TAG_LINEARITY_COEFF_CUBE)) {
571 *coeffs_cube = current_frame;
572 }
573 else if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_WAVEMAP)) {
574 *wmap = current_frame;
575 }
576 else if (!strcmp(cpl_frame_get_tag(current_frame),
577 MOONS_TAG_FF_EXTSPECTRA)) {
578 *flat = current_frame;
579 }
580 else if (!strcmp(cpl_frame_get_tag(current_frame),
581 MOONS_TAG_F2F_TABLE)) {
582 *f2f = current_frame;
583 }
584 }
585
586 if (nb_object <= 0) {
587 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
588 "SOF does not have any file tagged "
589 "with %s",
590 MOONS_TAG_OBJECT_STARENOD);
591 }
592
593 if (nb_stare > 0) {
594 *mode = MOO_MODE_STARE;
595 cpl_msg_info("test", "Recipe running in STARE mode");
596 }
597 else {
598 *mode = MOO_MODE_STARENOD;
599 cpl_msg_info("test", "Recipe running in STARENOD mode");
600 }
601
602 if (*mode == MOO_MODE_STARENOD) {
603 if (nb_sky == 0) {
604 return (int)cpl_error_set_message(
605 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
606 "SOF (filter by offset) does not have any file tagged "
607 "with %s",
608 MOONS_TAG_SKY_STARENOD);
609 }
610 if (strcmp(sky_params->method, MOO_SUB_SKY_STARE_METHOD_NONE) == 0) {
611 return (int)cpl_error_set_message(
612 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
613 "sky subtraction NONE method not allowed in STARENOD");
614 }
615 }
616
617 if (*fftrace == NULL) {
618 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
619 "SOF does not have any file tagged "
620 "with %s",
621 MOONS_TAG_FF_TRACE);
622 }
623 if (*wmap == NULL) {
624 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
625 "SOF does not have any file tagged "
626 "with %s",
627 MOONS_TAG_WAVEMAP);
628 }
629 if (*flat == NULL) {
630 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
631 "SOF does not have any file tagged "
632 "with %s",
633 MOONS_TAG_FF_EXTSPECTRA);
634 }
635 if (*master_flat == NULL &&
636 strcmp(ext_params->method, MOO_EXTRACT_METHOD_OPTIMAL) == 0) {
637 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
638 "SOF does not have any file tagged "
639 "with %s",
640 MOONS_TAG_MASTER_FLAT);
641 }
642
643 return CPL_ERROR_NONE;
644}
645
646
647static cpl_error_code
648_moons_p2pmap(moo_det *arc, const cpl_frame *p2pmap)
649{
650 cpl_error_code status = CPL_ERROR_NONE;
651 moo_det *p2p = NULL;
652 if (p2pmap != NULL) {
653 moo_try_check(p2p = moo_det_create(p2pmap), " ");
654 moo_try_check(moo_apply_p2p(arc, p2p), " ");
655 }
656moo_try_cleanup:
657 moo_det_delete(p2p);
658 return status;
659}
660
661static cpl_error_code
662_moons_apply_flat(moo_ext *ext,
663 const cpl_frame *flat_frame,
664 const cpl_frame *f2f_frame)
665{
666 cpl_error_code status = CPL_ERROR_NONE;
667 moo_ext *flat = NULL;
668 moo_f2f *f2f = NULL;
669 moo_try_check(flat = moo_ext_create(flat_frame), " ");
670 if (f2f_frame != NULL) {
671 f2f = moo_f2f_load(f2f_frame);
672 }
673 moo_try_check(moo_apply_flat(ext, flat, f2f), " ");
674moo_try_cleanup:
675 moo_ext_delete(flat);
676 moo_f2f_delete(f2f);
677 return status;
678}
679
680static moo_sci *
681_moons_combine_pair(moo_rbn *rbn,
682 moo_rbn *sky_rbn,
683 moo_rbn *obj_rbn,
684 moo_target_table *target_table,
685 const cpl_frame *f2f_frame,
686 const cpl_frame *solflux_frame,
687 const cpl_frame *airglow_group_frame,
688 const cpl_frame *airglow_var_frame,
689 moo_combine_pair_params *combine_pair_params,
690 moo_sub_sky_stare_params *sky_params,
691 const char *sci_filename,
692 moo_products *products,
693 const cpl_frame *ref_frame)
694{
695 moo_sci *result = NULL;
696 moo_f2f *f2f = NULL;
697 if (f2f_frame != NULL) {
698 f2f = moo_f2f_load(f2f_frame);
699 }
700 moo_try_check(result =
701 moo_combine_pair(rbn, sky_rbn, obj_rbn, target_table, f2f,
702 solflux_frame, airglow_group_frame,
703 airglow_var_frame, combine_pair_params,
704 sky_params, sci_filename, products,
705 ref_frame),
706 " ");
707moo_try_cleanup:
708 moo_f2f_delete(f2f);
709 return result;
710}
711
712
713static cpl_frame *
714_moons_starenod_rebin(int inum,
715 const char *mode,
716 int offset,
717 moo_ext *ext,
718 const cpl_frame *ref_frame,
719 moo_spectral_format *sformat,
720 moo_sky_lines_list *sky_lines,
721 moo_map *wmap,
722 const cpl_frame *atmo_frame,
723 const cpl_frame *resp_frame,
724 const cpl_frame *tell_frame,
725 moo_rebin_params *rbn_params,
726 moo_products *products,
727 const char *prefix)
728{
729 cpl_frame *rbn_frame = NULL;
730 moo_atm *atm = NULL;
731 moo_resp *resp = NULL;
732 moo_telluric *telluric = NULL;
733 moo_rbn *rbn = NULL;
734
735 char *rbn_filename = NULL;
736 char *calibflux_filename = NULL;
737 char *tellcorr_filename = NULL;
738
739 cpl_ensure(ext != NULL, CPL_ERROR_NULL_INPUT, NULL);
740 cpl_ensure(ref_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
741
742 rbn_filename = cpl_sprintf("%s_RBNSPECTRA_OFFSET%d_%s_%d.fits", prefix,
743 offset, mode, inum);
744 moo_try_check(rbn = moo_rebin(ext, wmap, sformat, rbn_params, rbn_filename),
745 " ");
746
747 moo_try_check(moo_rbn_compute_snr(rbn, sky_lines), " ");
748
749 moo_try_check(
750 rbn_frame = moo_products_add_rbn(products, rbn, CPL_FRAME_LEVEL_FINAL,
751 MOONS_TAG_SCIENCE_STARENOD_RBNSPECTRA,
752 rbn_filename, ref_frame),
753 " ");
754
755 if (atmo_frame != NULL && resp_frame != NULL) {
756 moo_try_check(atm = moo_atm_load(atmo_frame), " ");
757 moo_try_check(resp = moo_resp_load(resp_frame), " ");
758
759 calibflux_filename = cpl_sprintf("%s_RBNFLXSPECTRA_OFFSET%d_%s_%d.fits",
760 prefix, offset, mode, inum);
761 moo_try_check(moo_calib_flux(rbn, atm, resp), " ");
762 moo_try_check(rbn_frame = moo_products_add_rbn(
763 products, rbn, CPL_FRAME_LEVEL_FINAL,
764 MOONS_TAG_SCIENCE_STARENOD_RBNFLXSPECTRA,
765 calibflux_filename, ref_frame),
766 " ");
767 }
768 else {
769 cpl_msg_info(__func__, "No flux calibration");
770 }
771
772 if (tell_frame != NULL) {
773 moo_try_check(telluric = moo_telluric_load(tell_frame), " ");
774
775 tellcorr_filename =
776 cpl_sprintf("%s_RBNTELLCORRSPECTRA_OFFSET%d_%s_%d.fits", prefix,
777 offset, mode, inum);
778 moo_try_check(moo_correct_tell(rbn, telluric), " ");
779 moo_try_check(rbn_frame = moo_products_add_rbn(
780 products, rbn, CPL_FRAME_LEVEL_FINAL,
781 MOONS_TAG_SCIENCE_STARENOD_RBNTELLCORRSPECTRA,
782 tellcorr_filename, ref_frame),
783 " ");
784 }
785 else {
786 cpl_msg_info(__func__, "No telluric correction");
787 }
788
789moo_try_cleanup:
790 cpl_free(tellcorr_filename);
791 cpl_free(calibflux_filename);
792 cpl_free(rbn_filename);
793 moo_atm_delete(atm);
794 moo_resp_delete(resp);
795 moo_telluric_delete(telluric);
796 moo_rbn_delete(rbn);
797 return rbn_frame;
798}
799
800static moo_sci *
801_moons_sub_sky_stare(moo_rbn *rbn,
802 moo_target_table *target_table,
803 const cpl_frame *f2f_frame,
804 const cpl_frame *solflux_frame,
805 const cpl_frame *airglow_group_frame,
806 const cpl_frame *airglow_var_frame,
807 moo_sub_sky_stare_params *sky_params,
808 const char *sci_filename)
809{
810 moo_sci *result = NULL;
811 moo_f2f *f2f = NULL;
812
813 if (f2f_frame != NULL) {
814 f2f = moo_f2f_load(f2f_frame);
815 }
816 moo_try_check(result =
817 moo_sub_sky_stare(rbn, target_table, rbn, f2f,
818 solflux_frame, airglow_group_frame,
819 airglow_var_frame, sky_params,
820 sci_filename, MOO_SCI1D_NOT_PAIRED),
821 " ");
822
823moo_try_cleanup:
824 moo_f2f_delete(f2f);
825 return result;
826}
827
828static cpl_frame *
829_moons_stare_sci(int inum,
830 const char *mode,
831 int offset,
832 const cpl_frame *ref_frame,
833 const cpl_frame *rbn_frame,
834 moo_sky_lines_list *sky_lines,
835 const cpl_frame *f2f_frame,
836 const cpl_frame *airglow_group_frame,
837 const cpl_frame *airglow_var_frame,
838 const cpl_frame *solflux_frame,
839 moo_target_table_params *target_table_params,
840 moo_sub_sky_stare_params *sky_params,
841 moo_products *products,
842 const char *prefix)
843{
844 cpl_frame *sci_frame = NULL;
845 moo_rbn *rbn = NULL;
846 moo_target_table *target_table = NULL;
847 moo_sci *sci = NULL;
848
849 char *ttable_filename = NULL;
850 char *sci_filename = NULL;
851
852 cpl_ensure(rbn_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
853 cpl_ensure(ref_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
854 rbn = moo_rbn_create(rbn_frame);
855
856 moo_try_check(target_table =
857 moo_create_target_table(rbn, NULL, MOO_MODE_STARE,
858 target_table_params),
859 " ");
860 ttable_filename = cpl_sprintf("%s_TARGET_TABLE_OFFSET%d_%s_%d.fits", prefix,
861 offset, mode, inum);
862 moo_try_check(moo_products_add_target_table(
863 products, target_table, CPL_FRAME_LEVEL_INTERMEDIATE,
864 MOONS_TAG_SCIENCE_STARENOD_TARGET_TABLE, ttable_filename,
865 ref_frame),
866 " ");
867
868 sci_filename =
869 cpl_sprintf("%s_SCI_OFFSET%d_%s_%d.fits", prefix, offset, mode, inum);
870
871 moo_try_check(sci = _moons_sub_sky_stare(rbn, target_table, f2f_frame,
872 solflux_frame, airglow_group_frame,
873 airglow_var_frame, sky_params,
874 sci_filename),
875 " ");
876 moo_try_check(moo_sci_compute_snr(sci, sky_lines), " ");
877
878 moo_try_check(
879 sci_frame = moo_products_add_sci(products, sci, CPL_FRAME_LEVEL_FINAL,
880 MOONS_TAG_SCIENCE_STARENOD_SKSSPECTRA,
881 sci_filename, ref_frame),
882 " ");
883
884moo_try_cleanup:
885 cpl_free(ttable_filename);
886 cpl_free(sci_filename);
887 moo_target_table_delete(target_table);
888 moo_sci_delete(sci);
889 moo_rbn_delete(rbn);
890 return sci_frame;
891}
892
893static cpl_frame *
894_moons_starenod_sci(int inum,
895 const char *mode,
896 int offset,
897 const cpl_frame *obj_frame,
898 const cpl_frame *obj_rbn_frame,
899 const cpl_frame *sky_rbn_frame,
900 const cpl_frame *nod_rbn_frame,
901 moo_sky_lines_list *sky_lines,
902 const cpl_frame *f2f_frame,
903 const cpl_frame *airglow_group_frame,
904 const cpl_frame *airglow_var_frame,
905 const cpl_frame *solflux_frame,
906 moo_target_table_params *target_table_params,
907 moo_combine_pair_params *combine_pair_params,
908 moo_sub_sky_stare_params *sky_params,
909 moo_products *products,
910 const char *prefix)
911{
912 cpl_frame *sci_frame = NULL;
913 moo_rbn *nod_rbn = NULL;
914 moo_rbn *sky_rbn = NULL;
915 moo_rbn *obj_rbn = NULL;
916 moo_target_table *target_table = NULL;
917 moo_sci *sci = NULL;
918
919 char *ttable_filename = NULL;
920 char *sci_filename = NULL;
921
922 cpl_ensure(nod_rbn_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
923 cpl_ensure(obj_rbn_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
924 cpl_ensure(sky_rbn_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
925
926 moo_try_check(nod_rbn = moo_rbn_create(nod_rbn_frame), " ");
927 moo_try_check(obj_rbn = moo_rbn_create(obj_rbn_frame), " ");
928 moo_try_check(sky_rbn = moo_rbn_create(sky_rbn_frame), " ");
929
930 moo_try_check(target_table = moo_create_target_table(obj_rbn, sky_rbn,
931 MOO_MODE_STARENOD,
932 target_table_params),
933 " ");
934 ttable_filename = cpl_sprintf("%s_TARGET_TABLE_OFFSET%d_%s_%d.fits", prefix,
935 offset, mode, inum);
936 moo_try_check(moo_products_add_target_table(
937 products, target_table, CPL_FRAME_LEVEL_INTERMEDIATE,
938 MOONS_TAG_SCIENCE_STARENOD_TARGET_TABLE, ttable_filename,
939 obj_frame),
940 " ");
941
942 sci_filename =
943 cpl_sprintf("%s_SCI_OFFSET%d_%s_%d.fits", prefix, offset, mode, inum);
944 moo_try_check(sci = _moons_combine_pair(nod_rbn, sky_rbn, obj_rbn,
945 target_table, f2f_frame,
946 solflux_frame, airglow_group_frame,
947 airglow_var_frame,
948 combine_pair_params, sky_params,
949 sci_filename, products, obj_frame),
950 " ");
951
952 moo_try_check(moo_sci_compute_snr(sci, sky_lines), " ");
953
954 moo_try_check(
955 sci_frame = moo_products_add_sci(products, sci, CPL_FRAME_LEVEL_FINAL,
956 MOONS_TAG_SCIENCE_STARENOD_SKSSPECTRA,
957 sci_filename, obj_frame),
958 " ");
959
960moo_try_cleanup:
961 cpl_free(ttable_filename);
962 cpl_free(sci_filename);
963 moo_target_table_delete(target_table);
964 moo_sci_delete(sci);
965 moo_rbn_delete(nod_rbn);
966 moo_rbn_delete(sky_rbn);
967 moo_rbn_delete(obj_rbn);
968
969 return sci_frame;
970}
971
972static const cpl_frame *
973find_masterdark_nir(moo_det *det, cpl_frameset *set)
974{
975 const cpl_frame *result = NULL;
976 double dit = 0.0;
977 int ndit = 0;
978 double exptime = 0.0;
979 int num;
980 int type;
981 int find = 0;
982 moo_det *current_det = NULL;
983
984 cpl_ensure(det != NULL, CPL_ERROR_NULL_INPUT, NULL);
985
986 for (num = 1; num <= 2; num++) {
987 for (type = 1; type <= 2; type++) {
988 moo_single *single = moo_det_get_single(det, type, num);
989 if (single != NULL) {
990 moo_try_check(ndit = moo_pfits_get_ndit(single->header), " ");
991 moo_try_check(dit = moo_pfits_get_dit(single->header), " ");
992 exptime = dit * ndit;
993 find = 1;
994 break;
995 }
996 }
997 if (find) {
998 break;
999 }
1000 }
1001
1002 if (find && set != NULL) {
1003 int size = cpl_frameset_get_size(set);
1004 for (int i = 0; i < size; i++) {
1005 const cpl_frame *current = cpl_frameset_get_position_const(set, i);
1006 current_det = moo_det_create(current);
1007 moo_single *current_single =
1008 moo_det_get_single(current_det, type, num);
1009 moo_try_check(ndit = moo_pfits_get_ndit(current_single->header),
1010 " ");
1011 moo_try_check(dit = moo_pfits_get_dit(current_single->header), " ");
1012 double current_exptime = dit * ndit;
1013 if (fabs(exptime - current_exptime) <= 1E-9) {
1014 result = current;
1015 break;
1016 }
1017 moo_det_delete(current_det);
1018 current_det = NULL;
1019 }
1020 }
1021moo_try_cleanup:
1022 moo_det_delete(current_det);
1023 return result;
1024}
1025
1026static cpl_frame *
1027_moons_starenod_subtractnod(int inum,
1028 const char *mode,
1029 int offset,
1030 const cpl_frame *obj_frame,
1031 const cpl_frame *obj_det_frame,
1032 const cpl_frame *sky_det_frame,
1033 moo_products *products)
1034{
1035 cpl_frame *result = NULL;
1036 char *nod_det_filename = NULL;
1037 moo_det *nod_det = NULL;
1038 unsigned int badpix_level = MOO_BADPIX_GOOD;
1039
1040 moo_det *obj_det = moo_det_create(obj_det_frame);
1041 moo_det *sky_det = moo_det_create(sky_det_frame);
1042 moo_try_check(moo_det_load(obj_det, badpix_level), " ");
1043 moo_try_check(moo_det_load(sky_det, badpix_level), " ");
1044
1045 nod_det_filename =
1046 cpl_sprintf("%s_OFFSET%d_%s_%d.fits", MOONS_TAG_STARENOD_SUBTRACTNOD,
1047 offset, mode, inum);
1048
1049 moo_try_check(nod_det =
1050 moo_subtract_nod(obj_det, sky_det, nod_det_filename),
1051 " ");
1052 moo_try_check(result = moo_products_add(products, nod_det,
1053 CPL_FRAME_LEVEL_INTERMEDIATE,
1054 MOONS_TAG_STARENOD_SUBTRACTNOD,
1055 nod_det_filename, obj_frame),
1056 " ");
1057
1058moo_try_cleanup:
1059 cpl_free(nod_det_filename);
1060 moo_det_delete(obj_det);
1061 moo_det_delete(sky_det);
1062 moo_det_delete(nod_det);
1063
1064 return result;
1065}
1066
1067static cpl_frame *
1068_moons_starenod_prepare(int inum,
1069 const char *mode,
1070 int offset,
1071 const cpl_frame *frame,
1072 const char *bpmap_rp_name,
1073 const char *bpmap_nl_name,
1074 const cpl_frame *masterbias,
1075 const cpl_frame *masterdark_vis,
1076 cpl_frameset *masterdark_nir_frameset,
1077 const cpl_frame *p2pmap,
1078 const cpl_frame *coeffs_cube,
1079 moo_prepare_params *prepare_params,
1080 moo_correct_bias_params *cbias_params,
1081 moo_products *products,
1082 int objtype)
1083{
1084 cpl_frame *result = NULL;
1085 const cpl_frame *masterdark_nir = NULL;
1086 moo_det *det = NULL;
1087 char *det_filename = NULL;
1088 char *det_p2p_filename = NULL;
1089 const char *det_tags[] = { MOONS_TAG_STARENOD_OBJ_CORRECTDARK,
1090 MOONS_TAG_STARENOD_SKY_CORRECTDARK };
1091 const char *detp2p_tags[] = { MOONS_TAG_STARENOD_OBJ_APPLYP2P,
1092 MOONS_TAG_STARENOD_SKY_APPLYP2P };
1093
1094
1095 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
1096
1097 moo_try_check(det = moo_prepare(frame, bpmap_rp_name, bpmap_nl_name,
1098 masterbias, coeffs_cube, prepare_params),
1099 " ");
1100 moo_try_check(moo_correct_bias(det, masterbias, cbias_params), " ");
1101
1102 masterdark_nir = find_masterdark_nir(det, masterdark_nir_frameset);
1103
1104 moo_try_check(moo_correct_dark(det, NULL, masterdark_vis, masterdark_nir),
1105 " ");
1106 det_filename = cpl_sprintf("%s_OFFSET%d_%s_%d.fits", det_tags[objtype],
1107 offset, mode, inum);
1108 moo_try_check(moo_products_add(products, det, CPL_FRAME_LEVEL_INTERMEDIATE,
1109 det_tags[objtype], det_filename, frame),
1110 " ");
1111 moo_try_check(_moons_p2pmap(det, p2pmap), " ");
1112 det_p2p_filename = cpl_sprintf("%s_OFFSET%d_%s_%d.fits",
1113 detp2p_tags[objtype], offset, mode, inum);
1114 moo_try_check(result = moo_products_add(products, det,
1115 CPL_FRAME_LEVEL_INTERMEDIATE,
1116 detp2p_tags[objtype],
1117 det_p2p_filename, frame),
1118 " ");
1119
1120moo_try_cleanup:
1121 cpl_free(det_filename);
1122 cpl_free(det_p2p_filename);
1123 moo_det_delete(det);
1124 return result;
1125}
1126
1127static cpl_frame *
1128_moons_starenod_extract(int inum,
1129 const char *mode,
1130 int offset,
1131 const cpl_frame *ref_frame,
1132 const cpl_frame *det_frame,
1133 moo_loc *loc,
1134 moo_map *wmap,
1135 moo_spectral_format *sformat,
1136 moo_sky_lines_list *skylines,
1137 const cpl_frame *masterflat_frame,
1138 const cpl_frame *flat_frame,
1139 const cpl_frame *f2f_frame,
1140 moo_extract_params *extract_params,
1141 moo_products *products,
1142 const char *prefix)
1143{
1144 cpl_frame *ext_frame = NULL;
1145 moo_det *det = NULL;
1146 moo_psf *psf = NULL;
1147 moo_ext *ext = NULL;
1148
1149 char *ext_filename = NULL;
1150 char *extff_filename = NULL;
1151
1152 cpl_ensure(det_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
1153 det = moo_det_create(det_frame);
1154 ext_filename = cpl_sprintf("%s_EXTSPECTRA_OFFSET%d_%s_%d.fits", prefix,
1155 offset, mode, inum);
1156 if (masterflat_frame != NULL) {
1157 psf = moo_psf_load(masterflat_frame);
1158 }
1159 moo_try_check(ext =
1160 moo_extract(det, loc, psf, extract_params, ext_filename),
1161 " ");
1162 moo_try_check(ext_frame = moo_products_add_ext(
1163 products, ext, CPL_FRAME_LEVEL_INTERMEDIATE,
1164 MOONS_TAG_SCIENCE_STARENOD_EXTSPECTRA, ext_filename,
1165 ref_frame),
1166 " ");
1167 moo_try_check(_moons_apply_flat(ext, flat_frame, f2f_frame), " ");
1168 moo_try_check(moo_ext_compute_snr(ext, wmap, sformat, skylines), " ");
1169
1170 extff_filename = cpl_sprintf("%s_EXTFFSPECTRA_OFFSET%d_%s_%d.fits", prefix,
1171 offset, mode, inum);
1172
1173 moo_try_check(ext_frame = moo_products_add_ext(
1174 products, ext, CPL_FRAME_LEVEL_INTERMEDIATE,
1175 MOONS_TAG_SCIENCE_STARENOD_EXTSPECTRA, extff_filename,
1176 ref_frame),
1177 " ");
1178
1179moo_try_cleanup:
1180 cpl_free(ext_filename);
1181 cpl_free(extff_filename);
1182 moo_det_delete(det);
1183 moo_ext_delete(ext);
1184 moo_psf_delete(psf);
1185 return ext_frame;
1186}
1187
1188static int
1189_moons_sort_mjdobs(const cpl_frame *a, const cpl_frame *b)
1190{
1191 int res = 0;
1192
1193 const char *filenameA = cpl_frame_get_filename(a);
1194 const char *filenameB = cpl_frame_get_filename(b);
1195
1196 cpl_propertylist *lista = cpl_propertylist_load(filenameA, 0);
1197 cpl_propertylist *listb = cpl_propertylist_load(filenameB, 0);
1198
1199 if (cpl_propertylist_has(lista, MOO_PFITS_MJDOBS) &&
1200 cpl_propertylist_has(listb, MOO_PFITS_MJDOBS)) {
1201 double mjdA = moo_pfits_get_mjdobs(lista);
1202 double mjdB = moo_pfits_get_mjdobs(listb);
1203 if (mjdA < mjdB) {
1204 res = -1;
1205 }
1206 else if (mjdA > mjdB) {
1207 res = 1;
1208 }
1209 else {
1210 res = 0;
1211 }
1212 }
1213 cpl_propertylist_delete(listb);
1214 cpl_propertylist_delete(lista);
1215 return res;
1216}
1217
1218static int
1219_moons_compare_offset(const cpl_frame *a, const cpl_frame *b)
1220{
1221 int res = 0;
1222 double oA, oB;
1223 cpl_propertylist *lista = NULL;
1224 cpl_propertylist *listb = NULL;
1225 const char *filenameA = cpl_frame_get_filename(a);
1226 lista = cpl_propertylist_load(filenameA, 0);
1227
1228 moo_try_check(oA = moo_pfits_get_slit_offset(lista), "in filename :%s",
1229 filenameA);
1230 const char *filenameB = cpl_frame_get_filename(b);
1231 listb = cpl_propertylist_load(filenameB, 0);
1232
1233 moo_try_check(oB = moo_pfits_get_slit_offset(listb), "in filename :%s",
1234 filenameB);
1235 if (fabs(oA - oB) < 1E-12) {
1236 res = 1;
1237 }
1238moo_try_cleanup:
1239 cpl_propertylist_delete(lista);
1240 cpl_propertylist_delete(listb);
1241 return res;
1242}
1243
1244static int
1245_starenod_getsky_index(const cpl_frame *obj_frame,
1246 const cpl_frameset *sky_frameset)
1247{
1248 int result = 0;
1249
1250 cpl_ensure(obj_frame != NULL, CPL_ERROR_NULL_INPUT, 0);
1251 cpl_ensure(sky_frameset != NULL, CPL_ERROR_NULL_INPUT, 0);
1252
1253 const char *obj_filename = cpl_frame_get_filename(obj_frame);
1254 cpl_propertylist *obj_list = cpl_propertylist_load(obj_filename, 0);
1255 double obj_mjd = moo_pfits_get_mjdobs(obj_list);
1256 double odiff = -1;
1257 int size = cpl_frameset_get_size(sky_frameset);
1258 for (int i = 0; i < size; i++) {
1259 const cpl_frame *sky_frame =
1260 cpl_frameset_get_position_const(sky_frameset, i);
1261 const char *sky_filename = cpl_frame_get_filename(sky_frame);
1262 cpl_propertylist *sky_list = cpl_propertylist_load(sky_filename, 0);
1263 double sky_mjd = moo_pfits_get_mjdobs(sky_list);
1264 double diff = fabs(sky_mjd - obj_mjd);
1265 if (odiff < 0) {
1266 odiff = diff;
1267 }
1268 else if (odiff > diff) {
1269 odiff = diff;
1270 result = i;
1271 }
1272 cpl_propertylist_delete(sky_list);
1273 }
1274 cpl_propertylist_delete(obj_list);
1275 return result;
1276}
1277
1278static int
1279_moons_science_starenod_byoffset(cpl_frameset *frameset,
1280 cpl_frameset *sci_frameset,
1281 cpl_frameset *ref_frameset,
1282 const char *bpmap_rp_name,
1283 const char *bpmap_nl_name,
1284 const cpl_frame *masterbias,
1285 const cpl_frame *masterdark_vis,
1286 cpl_frameset *masterdark_nir,
1287 const cpl_frame *p2pmap,
1288 const cpl_frame *sformat_frame,
1289 moo_sky_lines_list *sky_lines,
1290 const cpl_frame *airglow_group_frame,
1291 const cpl_frame *solflux_frame,
1292 const cpl_frame *airglow_var_frame,
1293 const cpl_frame *atmo_frame,
1294 const cpl_frame *resp_frame,
1295 const cpl_frame *tell_frame,
1296 moo_prepare_params *prepare_params,
1297 moo_correct_bias_params *correct_bias_params,
1298 moo_extract_params *extract_params,
1299 moo_wavesol_params *wavesol_params,
1300 moo_rebin_params *rbn_params,
1301 moo_target_table_params *target_table_params,
1302 moo_combine_pair_params *combine_pair_params,
1303 moo_sub_sky_stare_params *sky_params,
1304 moo_products *products)
1305{
1306 /* parameters */
1307 int *skyindex = NULL;
1308 moo_loc *loc = NULL;
1309 moo_spectral_format *sformat = NULL;
1310 moo_map *wmap = NULL;
1311 moo_map *wmap_tuned = NULL;
1312 moo_extlist *obj_extlist = NULL;
1313 moo_extlist *sky_extlist = NULL;
1314 moo_extlist *nod_extlist = NULL;
1315 moo_extlist *wavemap_extlist = NULL;
1316 moo_ext *sum_ext = NULL;
1317 cpl_frameset *obj_frameset = NULL;
1318 cpl_frameset *sky_frameset = NULL;
1319 cpl_frameset *sky_det_frameset = NULL;
1320 cpl_frameset *obj_det_frameset = NULL;
1321 cpl_frameset *nod_det_frameset = NULL;
1322
1323 cpl_frameset *nod_ext_frameset = NULL;
1324 cpl_frameset *sky_ext_frameset = NULL;
1325 cpl_frameset *obj_ext_frameset = NULL;
1326
1327 cpl_frameset *obj_rbn_frameset = NULL;
1328 cpl_frameset *sky_rbn_frameset = NULL;
1329 cpl_frameset *nod_rbn_frameset = NULL;
1330
1331 const cpl_frame *master_flat = NULL;
1332 const cpl_frame *fftrace = NULL;
1333 const cpl_frame *coeffs_cube = NULL;
1334 const cpl_frame *wmap_frame = NULL;
1335 const cpl_frame *flat_frame = NULL;
1336 const cpl_frame *f2f_frame = NULL;
1337 char *sum_ext_filename = NULL;
1338 const char *ref_filename = NULL;
1339 cpl_propertylist *ref_header = NULL;
1340 moo_mode_type mode;
1341 const char *mode_name = NULL;
1342 int offset;
1343 int wavemap_extlist_size = 0;
1344
1345 char *wmap_tuned_filename = NULL;
1346 moo_target_table_mode target_mode = MOO_MODE_STARENOD;
1347
1348 cpl_errorstate prestate = cpl_errorstate_get();
1349 obj_frameset = cpl_frameset_new();
1350 sky_frameset = cpl_frameset_new();
1351
1352 sky_det_frameset = cpl_frameset_new();
1353 obj_det_frameset = cpl_frameset_new();
1354 nod_det_frameset = cpl_frameset_new();
1355
1356 nod_ext_frameset = cpl_frameset_new();
1357 sky_ext_frameset = cpl_frameset_new();
1358 obj_ext_frameset = cpl_frameset_new();
1359
1360 obj_rbn_frameset = cpl_frameset_new();
1361 sky_rbn_frameset = cpl_frameset_new();
1362 nod_rbn_frameset = cpl_frameset_new();
1363
1364 moo_try_check(_moons_science_starenod_check_sof_byoffset(
1365 frameset, obj_frameset, sky_frameset, &master_flat,
1366 &fftrace, &coeffs_cube, &wmap_frame, &flat_frame,
1367 &f2f_frame, extract_params, sky_params, &target_mode),
1368 " ");
1369 cpl_frameset_join(ref_frameset, obj_frameset);
1370 int nb_sky = cpl_frameset_get_size(sky_frameset);
1371 int nb_obj = cpl_frameset_get_size(obj_frameset);
1372
1373 cpl_frame *frame = cpl_frameset_get_position(obj_frameset, 0);
1374 ref_filename = cpl_frame_get_filename(frame);
1375 moo_try_check(mode = moo_mode_get(frame), " ");
1376 mode_name = moo_mode_get_name(mode);
1377 moo_try_check(ref_header = cpl_propertylist_load(ref_filename, 0), " ");
1378 moo_try_check(offset = moo_pfits_get_slit_offset(ref_header), " ");
1379 cpl_propertylist_delete(ref_header);
1380 ref_header = NULL;
1381 cpl_msg_info(__func__, "Do offset %d Use mode %s", offset, mode_name);
1382
1383 for (int i = 0; i < nb_sky; i++) {
1384 cpl_msg_info(__func__, "---Prepare sky: %d/%d", i + 1, nb_sky);
1385 cpl_frame *sky_frame = NULL;
1386 moo_try_check(sky_frame = cpl_frameset_get_position(sky_frameset, i),
1387 " ");
1388 cpl_frame *result =
1389 _moons_starenod_prepare(i, mode_name, offset, sky_frame,
1390 bpmap_rp_name, bpmap_nl_name, masterbias,
1391 masterdark_vis, masterdark_nir, p2pmap,
1392 coeffs_cube, prepare_params,
1393 correct_bias_params, products, 1);
1394 cpl_frameset_insert(sky_det_frameset, cpl_frame_duplicate(result));
1395 }
1396
1397 for (int i = 0; i < nb_obj; i++) {
1398 cpl_msg_info(__func__, "---Prepare objects: %d/%d", i + 1, nb_obj);
1399 cpl_frame *obj_frame = NULL;
1400 moo_try_check(obj_frame = cpl_frameset_get_position(obj_frameset, i),
1401 " ");
1402 cpl_frame *result =
1403 _moons_starenod_prepare(i, mode_name, offset, obj_frame,
1404 bpmap_rp_name, bpmap_nl_name, masterbias,
1405 masterdark_vis, masterdark_nir, p2pmap,
1406 coeffs_cube, prepare_params,
1407 correct_bias_params, products, 0);
1408 cpl_frameset_insert(obj_det_frameset, cpl_frame_duplicate(result));
1409 }
1410
1411 if (nb_sky > 0) {
1412 skyindex = cpl_calloc(nb_obj, sizeof(int));
1413 for (int i = 0; i < nb_obj; i++) {
1414 cpl_frame *obj_frame = NULL;
1415 cpl_frame *obj_det_frame = NULL;
1416 cpl_frame *sky_det_frame = NULL;
1417 moo_try_check(obj_frame =
1418 cpl_frameset_get_position(obj_frameset, i),
1419 " ");
1420 skyindex[i] = _starenod_getsky_index(obj_frame, sky_frameset);
1421 moo_try_check(obj_det_frame =
1422 cpl_frameset_get_position(obj_det_frameset, i),
1423 " ");
1424 moo_try_check(sky_det_frame =
1425 cpl_frameset_get_position(sky_det_frameset,
1426 skyindex[i]),
1427 " ");
1428 cpl_msg_info(__func__,
1429 "---Prepare nod %d/%d using object:%s sky: %s", i + 1,
1430 nb_obj, cpl_frame_get_filename(obj_det_frame),
1431 cpl_frame_get_filename(sky_det_frame));
1432 cpl_frame *result =
1433 _moons_starenod_subtractnod(i, mode_name, offset, obj_frame,
1434 obj_det_frame, sky_det_frame,
1435 products);
1436 cpl_frameset_insert(nod_det_frameset, cpl_frame_duplicate(result));
1437 }
1438 }
1439 int nb_nod = cpl_frameset_get_size(nod_det_frameset);
1440 moo_try_check(wmap = moo_map_load(wmap_frame), " ");
1441 moo_try_check(loc = moo_loc_load(fftrace), " ");
1442 moo_try_check(sformat = moo_spectral_format_load(sformat_frame, mode), " ");
1443
1444 for (int i = 0; i < nb_sky; i++) {
1445 cpl_frame *sky_frame = NULL;
1446 cpl_frame *sky_det_frame = NULL;
1447 cpl_msg_info(__func__, "---Extract sky: %d/%d", i + 1, nb_sky);
1448 moo_try_check(sky_frame = cpl_frameset_get_position(sky_frameset, i),
1449 " ");
1450 moo_try_check(sky_det_frame =
1451 cpl_frameset_get_position(sky_det_frameset, i),
1452 " ");
1453 cpl_frame *result =
1454 _moons_starenod_extract(i, mode_name, offset, sky_frame,
1455 sky_det_frame, loc, wmap, sformat,
1456 sky_lines, master_flat, flat_frame,
1457 f2f_frame, extract_params, products,
1458 "STARENOD_SKY");
1459 cpl_frameset_insert(sky_ext_frameset, cpl_frame_duplicate(result));
1460 }
1461
1462 for (int i = 0; i < nb_obj; i++) {
1463 cpl_frame *obj_frame = NULL;
1464 cpl_frame *obj_det_frame = NULL;
1465 cpl_msg_info(__func__, "---Extract objects: %d/%d", i + 1, nb_obj);
1466 moo_try_check(obj_frame = cpl_frameset_get_position(obj_frameset, i),
1467 " ");
1468 moo_try_check(obj_det_frame =
1469 cpl_frameset_get_position(obj_det_frameset, i),
1470 " ");
1471 cpl_frame *result =
1472 _moons_starenod_extract(i, mode_name, offset, obj_frame,
1473 obj_det_frame, loc, wmap, sformat,
1474 sky_lines, master_flat, flat_frame,
1475 f2f_frame, extract_params, products,
1476 "STARENOD_OBJ");
1477 cpl_frameset_insert(obj_ext_frameset, cpl_frame_duplicate(result));
1478 }
1479
1480 for (int i = 0; i < nb_nod; i++) {
1481 cpl_frame *obj_frame = NULL;
1482 cpl_frame *nod_det_frame = NULL;
1483 cpl_msg_info(__func__, "---Extract nod: %d/%d", i + 1, nb_nod);
1484 moo_try_check(obj_frame = cpl_frameset_get_position(obj_frameset, i),
1485 " ");
1486 moo_try_check(nod_det_frame =
1487 cpl_frameset_get_position(nod_det_frameset, i),
1488 " ");
1489 cpl_frame *result =
1490 _moons_starenod_extract(i, mode_name, offset, obj_frame,
1491 nod_det_frame, loc, wmap, sformat,
1492 sky_lines, master_flat, flat_frame,
1493 f2f_frame, extract_params, products,
1494 "STARENOD_NOD");
1495 cpl_frameset_insert(nod_ext_frameset, cpl_frame_duplicate(result));
1496 }
1497
1498 obj_extlist = moo_extlist_create(obj_ext_frameset);
1499 sky_extlist = moo_extlist_create(sky_ext_frameset);
1500 nod_extlist = moo_extlist_create(nod_ext_frameset);
1501 wavemap_extlist = moo_extlist_new();
1502
1503 for (int i = 0; i < nb_obj; i++) {
1504 moo_ext *ext = moo_extlist_get(obj_extlist, i);
1505 moo_extlist_set(wavemap_extlist, ext, wavemap_extlist_size);
1506 wavemap_extlist_size++;
1507 }
1508 for (int i = 0; i < nb_sky; i++) {
1509 moo_ext *ext = moo_extlist_get(sky_extlist, i);
1510 moo_extlist_set(wavemap_extlist, ext, wavemap_extlist_size);
1511 wavemap_extlist_size++;
1512 }
1513
1514 sum_ext_filename =
1515 cpl_sprintf("STARENOD_EXT_SUM_OFFSET%d_%s.fits", offset, mode_name);
1516 sum_ext = moo_extlist_sum(wavemap_extlist, sum_ext_filename);
1517 cpl_frame *ref_frame = cpl_frameset_get_position(obj_frameset, 0);
1518 moo_try_check(moo_products_add_ext(products, sum_ext,
1519 CPL_FRAME_LEVEL_TEMPORARY,
1520 MOONS_TAG_SCIENCE_STARENOD_EXTSPECTRA,
1521 sum_ext_filename, ref_frame),
1522 " ");
1523 moo_map *used_wmap = NULL;
1524 const char *wavesol_control = wavesol_params->control;
1525 if ((strcmp(wavesol_control, MOO_WAVESOL_CONTROL_CHECK) == 0) ||
1526 (strcmp(wavesol_control, MOO_WAVESOL_CONTROL_UPDATE) == 0)) {
1527 const char *sky_lines_name = sky_lines->filename;
1528 moo_try_check(wmap_tuned = moo_wavesol(sum_ext, sky_lines_name, sformat,
1529 loc, wmap, wavesol_params),
1530 " ");
1531 if (strcmp(wavesol_control, MOO_WAVESOL_CONTROL_UPDATE) == 0) {
1532 used_wmap = wmap_tuned;
1533 wmap_tuned_filename =
1534 cpl_sprintf("STARENOD_WMAP_TUNED_OFFSET%d_%s.fits", offset,
1535 mode_name);
1536 moo_map_save(wmap_tuned, wmap_tuned_filename);
1537 }
1538 }
1539 else {
1540 used_wmap = wmap;
1541 }
1542
1543 for (int i = 0; i < nb_obj; i++) {
1544 cpl_frame *obj_frame = NULL;
1545 moo_ext *obj_ext = moo_extlist_get(obj_extlist, i);
1546 cpl_msg_info(__func__, "---Rebin objects: %d/%d", i + 1, nb_obj);
1547
1548 moo_try_check(obj_frame = cpl_frameset_get_position(obj_frameset, i),
1549 " ");
1550 cpl_frame *result =
1551 _moons_starenod_rebin(i, mode_name, offset, obj_ext, obj_frame,
1552 sformat, sky_lines, used_wmap, atmo_frame,
1553 resp_frame, tell_frame, rbn_params, products,
1554 "STARENOD_OBJ");
1555
1556 cpl_frameset_insert(obj_rbn_frameset, cpl_frame_duplicate(result));
1557 }
1558
1559 for (int i = 0; i < nb_sky; i++) {
1560 cpl_frame *sky_frame = NULL;
1561 moo_ext *sky_ext = moo_extlist_get(sky_extlist, i);
1562 cpl_msg_info(__func__, "---Rebin sky: %d/%d", i + 1, nb_sky);
1563 moo_try_check(sky_frame = cpl_frameset_get_position(sky_frameset, i),
1564 " ");
1565 cpl_frame *result =
1566 _moons_starenod_rebin(i, mode_name, offset, sky_ext, sky_frame,
1567 sformat, sky_lines, used_wmap, atmo_frame,
1568 resp_frame, tell_frame, rbn_params, products,
1569 "STARENOD_SKY");
1570 cpl_frameset_insert(sky_rbn_frameset, cpl_frame_duplicate(result));
1571 }
1572
1573 for (int i = 0; i < nb_nod; i++) {
1574 cpl_frame *nod_frame = NULL;
1575 moo_ext *nod_ext = moo_extlist_get(nod_extlist, i);
1576 cpl_msg_info(__func__, "---Rebin nod: %d/%d", i + 1, nb_nod);
1577 moo_try_check(nod_frame = cpl_frameset_get_position(obj_frameset, i),
1578 " ");
1579 cpl_frame *result =
1580 _moons_starenod_rebin(i, mode_name, offset, nod_ext, nod_frame,
1581 sformat, sky_lines, used_wmap, atmo_frame,
1582 resp_frame, tell_frame, rbn_params, products,
1583 "STARENOD_NOD");
1584 cpl_frameset_insert(nod_rbn_frameset, cpl_frame_duplicate(result));
1585 }
1586
1587 if (nb_sky == 0) {
1588 for (int i = 0; i < nb_obj; i++) {
1589 cpl_frame *obj_frame = NULL;
1590 cpl_frame *obj_rbn_frame = NULL;
1591 cpl_msg_info(__func__, "---Create sci: %d/%d", i + 1, nb_obj);
1592 moo_try_check(obj_frame =
1593 cpl_frameset_get_position(obj_frameset, i),
1594 " ");
1595 moo_try_check(obj_rbn_frame =
1596 cpl_frameset_get_position(obj_rbn_frameset, i),
1597 " ");
1598 cpl_frame *result =
1599 _moons_stare_sci(i, mode_name, offset, obj_frame, obj_rbn_frame,
1600 sky_lines, f2f_frame, airglow_group_frame,
1601 airglow_var_frame, solflux_frame,
1602 target_table_params, sky_params, products,
1603 "STARENOD_OBJ");
1604 cpl_frameset_insert(sci_frameset, cpl_frame_duplicate(result));
1605 }
1606 }
1607 else {
1608 for (int i = 0; i < nb_nod; i++) {
1609 cpl_frame *obj_frame = NULL;
1610 cpl_frame *obj_rbn_frame = NULL;
1611 cpl_frame *sky_rbn_frame = NULL;
1612 cpl_frame *nod_rbn_frame = NULL;
1613 cpl_frame *sci_frame = NULL;
1614 moo_try_check(obj_frame =
1615 cpl_frameset_get_position(obj_frameset, i),
1616 " ");
1617 moo_try_check(obj_rbn_frame =
1618 cpl_frameset_get_position(obj_rbn_frameset, i),
1619 " ");
1620 moo_try_check(sky_rbn_frame =
1621 cpl_frameset_get_position(sky_rbn_frameset,
1622 skyindex[i]),
1623 " ");
1624 moo_try_check(nod_rbn_frame =
1625 cpl_frameset_get_position(nod_rbn_frameset, i),
1626 " ");
1627 cpl_msg_info(__func__,
1628 "---Create sci: %d/%d using nod %s object:%s sky: %s",
1629 i + 1, nb_nod, cpl_frame_get_filename(nod_rbn_frame),
1630 cpl_frame_get_filename(obj_rbn_frame),
1631 cpl_frame_get_filename(sky_rbn_frame));
1632
1633 moo_try_check(sci_frame = _moons_starenod_sci(
1634 i, mode_name, offset, obj_frame, obj_rbn_frame,
1635 sky_rbn_frame, nod_rbn_frame, sky_lines,
1636 f2f_frame, airglow_group_frame, airglow_var_frame,
1637 solflux_frame, target_table_params,
1638 combine_pair_params, sky_params, products,
1639 "STARENOD_NOD"),
1640 " ");
1641 cpl_frameset_insert(sci_frameset, cpl_frame_duplicate(sci_frame));
1642 }
1643 }
1644
1645moo_try_cleanup:
1646 if (!cpl_errorstate_is_equal(prestate)) {
1647 cpl_propertylist_delete(ref_header);
1648 }
1649 moo_extlist_delete(obj_extlist);
1650 moo_extlist_delete(sky_extlist);
1651 moo_extlist_delete(nod_extlist);
1652 if (wavemap_extlist != NULL) {
1653 for (int i = 0; i < wavemap_extlist_size; i++) {
1654 moo_extlist_unset(wavemap_extlist, 0);
1655 }
1656 }
1657 moo_extlist_delete(wavemap_extlist);
1658 moo_ext_delete(sum_ext);
1659 moo_loc_delete(loc);
1660 moo_map_delete(wmap);
1661 moo_map_delete(wmap_tuned);
1662 cpl_free(sum_ext_filename);
1663 cpl_free(wmap_tuned_filename);
1665
1666 cpl_frameset_delete(obj_rbn_frameset);
1667 cpl_frameset_delete(sky_rbn_frameset);
1668 cpl_frameset_delete(nod_rbn_frameset);
1669
1670 cpl_frameset_delete(sky_det_frameset);
1671 cpl_frameset_delete(obj_det_frameset);
1672 cpl_frameset_delete(nod_det_frameset);
1673
1674 cpl_frameset_delete(obj_frameset);
1675 cpl_frameset_delete(sky_frameset);
1676 cpl_frameset_delete(nod_ext_frameset);
1677 cpl_frameset_delete(sky_ext_frameset);
1678 cpl_frameset_delete(obj_ext_frameset);
1679
1680 cpl_free(skyindex);
1681
1682 return (int)cpl_error_get_code();
1683}
1684
1685static int
1686_moons_science_starenod(cpl_frameset *frameset,
1687 const cpl_parameterlist *parlist)
1688{
1689 /* parameters */
1690 moo_prepare_params *prepare_params = NULL;
1691 moo_correct_bias_params *correct_bias_params = NULL;
1692 moo_extract_params *extract_params = NULL;
1693 moo_wavesol_params *wavesol_params = NULL;
1694 moo_rebin_params *rbn_params = NULL;
1695 moo_target_table_params *target_table_params = NULL;
1696 moo_combine_pair_params *combine_pair_params = NULL;
1697 moo_sub_sky_stare_params *sky_params = NULL;
1698 moo_coadd_params *coadd_params = NULL;
1699 moo_create_s1d_params *s1d_params = NULL;
1700
1701 moo_scilist *sci_list = NULL;
1702 moo_sci *sci_coadd = NULL;
1703
1704 cpl_frameset *offset_frameset = NULL;
1705 cpl_frameset *set = NULL;
1706 cpl_frameset *sci_set = NULL;
1707 cpl_frameset *ref_frameset = NULL;
1708
1709 const char *bpmap_rp_name = NULL;
1710 const char *bpmap_nl_name = NULL;
1711 const cpl_frame *masterbias = NULL;
1712 const cpl_frame *masterdark_vis = NULL;
1713 cpl_frameset *masterdark_nir = NULL;
1714 const cpl_frame *p2pmap = NULL;
1715 const cpl_frame *sformat_frame = NULL;
1716 const cpl_frame *sky_lines_frame = NULL;
1717 const cpl_frame *atmo_frame = NULL;
1718 const cpl_frame *resp_frame = NULL;
1719 const cpl_frame *tell_frame = NULL;
1720 const cpl_frame *airglow_group_frame = NULL;
1721 const cpl_frame *solflux_frame = NULL;
1722 const cpl_frame *airglow_var_frame = NULL;
1723 const cpl_frame *ref_frame = NULL;
1724 cpl_size nsel = 0;
1725 cpl_size *selection = NULL;
1726 moo_sky_lines_list *skylines = NULL;
1727 char *coadd_filename = NULL;
1728 char *calibflux_filename = NULL;
1729
1730 cpl_errorstate prestate = cpl_errorstate_get();
1731
1732 moo_products *products =
1733 moo_products_new(frameset, parlist, "moons_science_starenod",
1734 PACKAGE "/" PACKAGE_VERSION);
1735 const moo_params *params = moo_products_get_params(products);
1736 moo_try_check(prepare_params = moo_params_get_prepare(params, parlist),
1737 " ");
1738
1739 moo_try_check(correct_bias_params =
1740 moo_params_get_correct_bias(params, parlist),
1741 " ");
1742 moo_try_check(extract_params = moo_params_get_extract(params, parlist),
1743 " ");
1744 moo_try_check(rbn_params = moo_params_get_rebin(params, parlist), " ");
1745 moo_try_check(target_table_params =
1746 moo_params_get_target_table(params, parlist),
1747 " ");
1748 moo_try_check(combine_pair_params =
1749 moo_params_get_combine_pair(params, parlist),
1750 " ");
1751 moo_try_check(sky_params = moo_params_get_sub_sky_stare(params, parlist),
1752 " ");
1753 moo_try_check(coadd_params = moo_params_get_coadd(params, parlist), " ");
1754 moo_try_check(wavesol_params =
1755 moo_params_get_science_wavesol(params, parlist),
1756 " ");
1757 moo_try_check(s1d_params = moo_params_get_create_s1d(params, parlist), " ");
1758
1759 /* SOF file */
1760 offset_frameset = cpl_frameset_new();
1761 sci_set = cpl_frameset_new();
1762 ref_frameset = cpl_frameset_new();
1763 masterdark_nir = cpl_frameset_new();
1764
1765 moo_try_check(_moons_science_starenod_check_sof(
1766 frameset, offset_frameset, &bpmap_rp_name, &bpmap_nl_name,
1767 &masterbias, &masterdark_vis, masterdark_nir, &p2pmap,
1768 &sformat_frame, &sky_lines_frame, &atmo_frame,
1769 &resp_frame, &tell_frame, &airglow_group_frame,
1770 &airglow_var_frame, &solflux_frame, sky_params),
1771 " ");
1772
1773 cpl_frameset_sort(offset_frameset, _moons_sort_mjdobs);
1774
1775 moo_try_check(selection =
1776 cpl_frameset_labelise(offset_frameset,
1777 &_moons_compare_offset, &nsel),
1778 " ");
1779
1780 moo_try_check(skylines = moo_sky_lines_list_load(sky_lines_frame), " ");
1781
1782 for (int i = 0; i < nsel; i++) {
1783 moo_try_check(set = cpl_frameset_extract(offset_frameset, selection, i),
1784 " ");
1785 moo_try_check(_moons_science_starenod_byoffset(
1786 set, sci_set, ref_frameset, bpmap_rp_name,
1787 bpmap_nl_name, masterbias, masterdark_vis,
1788 masterdark_nir, p2pmap, sformat_frame, skylines,
1789 airglow_group_frame, solflux_frame, airglow_var_frame,
1790 atmo_frame, resp_frame, tell_frame, prepare_params,
1791 correct_bias_params, extract_params, wavesol_params,
1792 rbn_params, target_table_params, combine_pair_params,
1793 sky_params, products),
1794 " ");
1795
1796 cpl_frameset_delete(set);
1797 set = NULL;
1798 }
1799
1800 moo_try_check(ref_frame = cpl_frameset_get_position_const(ref_frameset, 0),
1801 " ");
1802 moo_mode_type mode = moo_mode_get(ref_frame);
1803
1804 sci_list = moo_scilist_create(sci_set);
1805 coadd_filename =
1806 cpl_sprintf("%s_%s.fits", MOONS_TAG_STARENOD_COMBINED_SPECTRA,
1807 moo_mode_get_name(mode));
1808
1809 sci_coadd = moo_coadd(sci_list, coadd_params, coadd_filename);
1810
1811 moo_try_check(moo_sci_compute_snr(sci_coadd, skylines), " ");
1812 cpl_frame *sci_final = NULL;
1813 moo_try_check(sci_final =
1814 moo_products_add_sci(products, sci_coadd,
1815 CPL_FRAME_LEVEL_FINAL,
1816 MOONS_TAG_STARENOD_COMBINED_SPECTRA,
1817 coadd_filename, ref_frame),
1818 " ");
1819 moo_create_s1d(sci_final, s1d_params, MOONS_TAG_SCIENCE_STARENOD_1DSPECTRUM,
1820 products);
1821
1822moo_try_cleanup:
1823 if (!cpl_errorstate_is_equal(prestate)) {
1824 cpl_frameset_delete(set);
1825 }
1826 cpl_free(coadd_filename);
1827 cpl_free(calibflux_filename);
1828 cpl_free(selection);
1829 moo_sci_delete(sci_coadd);
1830 moo_scilist_delete(sci_list);
1831 moo_sky_lines_list_delete(skylines);
1832
1833 cpl_frameset_delete(sci_set);
1834 cpl_frameset_delete(ref_frameset);
1835 cpl_frameset_delete(offset_frameset);
1836 cpl_frameset_delete(masterdark_nir);
1837
1838 moo_extract_params_delete(extract_params);
1839 moo_wavesol_params_delete(wavesol_params);
1840 moo_rebin_params_delete(rbn_params);
1841 moo_target_table_params_delete(target_table_params);
1842
1843 moo_combine_pair_params_delete(combine_pair_params);
1844 moo_sub_sky_stare_params_delete(sky_params);
1845 moo_coadd_params_delete(coadd_params);
1846 moo_correct_bias_params_delete(correct_bias_params);
1847 moo_create_s1d_params_delete(s1d_params);
1848 moo_prepare_params_delete(prepare_params);
1849
1850 moo_products_delete(products);
1851 return (int)cpl_error_get_code();
1852}
moo_atm * moo_atm_load(const cpl_frame *frame)
Load a ATM frame and create a moo_atm.
Definition: moo_atm.c:83
void moo_atm_delete(moo_atm *self)
Delete a moo_atm.
Definition: moo_atm.c:154
#define MOO_BADPIX_GOOD
Definition: moo_badpix.h:36
moo_det * moo_det_create(const cpl_frame *frame)
Create a new moo_det from the given DET frame.
Definition: moo_det.c:91
cpl_error_code moo_det_load(moo_det *self, unsigned int level)
Load all parts in DET.
Definition: moo_det.c:135
void moo_det_delete(moo_det *self)
Delete a moo_det.
Definition: moo_det.c:472
moo_single * moo_det_get_single(moo_det *self, moo_detector_type type, int num)
Get the type part in DET and return it.
Definition: moo_det.c:359
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
moo_ext * moo_ext_create(const cpl_frame *frame)
Create a new empty EXT filename.
Definition: moo_ext.c:86
void moo_ext_delete(moo_ext *self)
Delete a moo_ext.
Definition: moo_ext.c:370
cpl_error_code moo_ext_compute_snr(moo_ext *self, moo_map *wmap, moo_spectral_format *sformat, moo_sky_lines_list *skylines)
Compute SNR for all fibres EXT.
Definition: moo_ext.c:553
moo_ext * moo_extlist_get(moo_extlist *self, int i)
Get the DET at the position i in the list.
Definition: moo_extlist.c:197
cpl_error_code moo_extlist_set(moo_extlist *self, moo_ext *ext, cpl_size pos)
Insert a DET into an moo_extlist.
Definition: moo_extlist.c:376
moo_ext * moo_extlist_unset(moo_extlist *self, cpl_size pos)
Remove a DET from a DET list.
Definition: moo_extlist.c:467
moo_ext * moo_extlist_sum(moo_extlist *self, const char *filename)
Sum EXT element from the list and save result in filename.
Definition: moo_extlist.c:581
void moo_extlist_delete(moo_extlist *self)
Free all memory used by a moo_extlist object including the DET.
Definition: moo_extlist.c:561
moo_extlist * moo_extlist_create(cpl_frameset *frameset)
Create a new moo_extlist from the given DET frameset.
Definition: moo_extlist.c:72
moo_extlist * moo_extlist_new(void)
Create a new moo_extlist.
Definition: moo_extlist.c:57
void moo_f2f_delete(moo_f2f *self)
Delete a moo_f2f.
Definition: moo_f2f.c:413
moo_f2f * moo_f2f_load(const cpl_frame *f2f_frame)
Load a F2F table from a fits file.
Definition: moo_f2f.c:139
moo_loc * moo_loc_load(const cpl_frame *locframe)
Load a LOC frame and create a moo_loc.
Definition: moo_loc.c:109
void moo_loc_delete(moo_loc *self)
Delete a moo_loc.
Definition: moo_loc.c:332
void moo_map_save(moo_map *self, const char *filename)
Save a moo_map to a FITS file.
Definition: moo_map.c:275
void moo_map_delete(moo_map *self)
Delete a moo_map.
Definition: moo_map.c:236
moo_coadd_params * moo_params_get_coadd(const moo_params *self, const cpl_parameterlist *list)
Get coadd parameters from moons parameters list.
Definition: moo_params.c:3147
cpl_error_code moo_params_add_create_s1d(moo_params *self, cpl_parameterlist *list)
Add default parameters for create_s1d.
Definition: moo_params.c:2437
void moo_create_s1d_params_delete(moo_create_s1d_params *self)
Delete a moo_create_s1d_params.
Definition: moo_params.c:2353
cpl_error_code moo_params_add_extract(moo_params *self, cpl_parameterlist *list)
Add default parameters for extraction.
Definition: moo_params.c:902
moo_create_s1d_params * moo_params_get_create_s1d(const moo_params *self, const cpl_parameterlist *list)
Get create s1d parameters from moons parameters list.
Definition: moo_params.c:1614
moo_prepare_params * moo_params_get_prepare(const moo_params *self, const cpl_parameterlist *list)
Get remove prepare parameters from moons parameters list.
Definition: moo_params.c:1073
moo_sub_sky_stare_params * moo_params_get_sub_sky_stare(const moo_params *self, const cpl_parameterlist *list)
Get sub sky stare parameters from moons parameters list.
Definition: moo_params.c:2909
moo_combine_pair_params * moo_params_get_combine_pair(const moo_params *self, const cpl_parameterlist *list)
Get combine_pair parameters from moons parameters list.
Definition: moo_params.c:3035
cpl_error_code moo_params_add_target_table(moo_params *self, cpl_parameterlist *list, int value)
Add default parameters for moo_create_target_table.
Definition: moo_params.c:2616
cpl_error_code moo_params_add_combine_pair(moo_params *self, cpl_parameterlist *list, int value)
Add default parameters for moo_combine_pair.
Definition: moo_params.c:2591
moo_target_table_params * moo_params_get_target_table(const moo_params *self, const cpl_parameterlist *list)
Get target_table parameters from moons parameters list.
Definition: moo_params.c:3063
cpl_error_code moo_params_add_science_wavesol(moo_params *self, cpl_parameterlist *list)
Add default science parameters for moo_wavesol.
Definition: moo_params.c:2091
moo_correct_bias_params * moo_params_get_correct_bias(const moo_params *self, const cpl_parameterlist *list)
Get correct_bias parameters from moons parameters list.
Definition: moo_params.c:1167
moo_extract_params * moo_params_get_extract(const moo_params *self, const cpl_parameterlist *list)
Get extraction parameters from moons parameters list.
Definition: moo_params.c:1197
moo_wavesol_params * moo_params_get_science_wavesol(const moo_params *self, const cpl_parameterlist *list)
Get science_wavesol parameters from moons parameters list.
Definition: moo_params.c:2790
void moo_params_delete(moo_params *self)
Delete a moo_params.
Definition: moo_params.c:85
cpl_error_code moo_params_add_sub_sky_stare_wnone(moo_params *self, cpl_parameterlist *list)
Add default parameters for moo_sub_sky_stare.
Definition: moo_params.c:2220
cpl_error_code moo_params_add_correct_bias(moo_params *self, cpl_parameterlist *list, const char *method)
Add default parameters for correct_bias.
Definition: moo_params.c:860
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
cpl_error_code moo_params_add_prepare(moo_params *self, cpl_parameterlist *list)
Add default parameters for prepare.
Definition: moo_params.c:667
moo_rebin_params * moo_params_get_rebin(const moo_params *self, const cpl_parameterlist *list)
Get rebin parameters from moons parameters list.
Definition: moo_params.c:2866
cpl_error_code moo_params_add_coadd(moo_params *self, cpl_parameterlist *list)
Add default parameters for moo_coadd.
Definition: moo_params.c:2687
moo_params * moo_params_new(const char *pid, const char *recipe_id)
Create a new moo_params.
Definition: moo_params.c:62
cpl_error_code moo_params_add_rebin(moo_params *self, cpl_parameterlist *list)
Add default parameters for moo_rebin.
Definition: moo_params.c:2152
void moo_psf_delete(moo_psf *self)
Delete a moo_psf.
Definition: moo_psf.c:264
moo_psf * moo_psf_load(const cpl_frame *psfframe)
Load a PSF frame and create a moo_psf.
Definition: moo_psf.c:109
void moo_rbn_delete(moo_rbn *self)
Delete a moo_rbn.
Definition: moo_rbn.c:120
moo_rbn * moo_rbn_create(const cpl_frame *frame)
Create a new empty RBN filename.
Definition: moo_rbn.c:85
cpl_error_code moo_rbn_compute_snr(moo_rbn *self, moo_sky_lines_list *skylines)
Compute SNR for targets in RBN.
Definition: moo_rbn.c:421
void moo_resp_delete(moo_resp *self)
Delete a moo_resp.
Definition: moo_resp.c:320
moo_resp * moo_resp_load(const cpl_frame *frame)
Load a RESP frame and create a moo_resp.
Definition: moo_resp.c:401
void moo_sci_delete(moo_sci *self)
Delete a moo_sci.
Definition: moo_sci.c:84
cpl_error_code moo_sci_compute_snr(moo_sci *self, moo_sky_lines_list *skylines)
Compute SNR for all targets in SCI.
Definition: moo_sci.c:415
void moo_scilist_delete(moo_scilist *self)
Free all memory used by a moo_scilist object including the SCI.
Definition: moo_scilist.c:176
moo_scilist * moo_scilist_create(cpl_frameset *frameset)
Create a new moo_scilist from the given SCI frameset.
Definition: moo_scilist.c:321
moo_sky_lines_list * moo_sky_lines_list_load(const cpl_frame *frame)
Load a SKY_LINES_LIST frame and create a moo_sky_lines_list.
void moo_sky_lines_list_delete(moo_sky_lines_list *self)
Delete a moo_sky_lines_list.
void moo_spectral_format_delete(moo_spectral_format *self)
Delete a moo_spectral_format.
moo_spectral_format * moo_spectral_format_load(const cpl_frame *frame, moo_mode_type m)
Load a SPECTRAL FORMAT frame and create a moo_spectral_format.
void moo_target_table_delete(moo_target_table *self)
Delete a moo_target_table.
enum _moo_target_table_mode_ moo_target_table_mode
The type code type.
moo_target_table * moo_create_target_table(moo_rbn *rbnA, moo_rbn *rbnB, moo_target_table_mode mode, moo_target_table_params *params)
Prepare target table for SCI frames using their attached fibre.
moo_telluric * moo_telluric_load(const cpl_frame *frame)
Load a TELLURIC frame and create a moo_telluric.
Definition: moo_telluric.c:405
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:104
moo_ext * moo_extract(moo_det *det, moo_loc *loc, moo_psf *master_flat, moo_extract_params *params, const char *filename)
extract the 1D spectrum of fibres
Definition: moo_extract.c:523
cpl_error_code moo_correct_tell(moo_rbn *rbn, moo_telluric *tell)
Apply the telluric correction to 1D rebinned spectra.
moo_sci * moo_coadd(moo_scilist *scilist, moo_coadd_params *params, const char *filename)
Co-add reduced science frames in SCI format.
Definition: moo_coadd.c:134
moo_map * moo_wavesol(moo_ext *arc_ext, const char *lineCatname, moo_spectral_format *sformat, moo_loc *loc, moo_map *wmap, moo_wavesol_params *params)
Computes the wavelength solution from 1D extracted arc spectra.
Definition: moo_wavesol.c:2728
cpl_error_code moo_apply_p2p(moo_det *flat, moo_det *p2pmap)
Divide DET by the pixel-to-pixel variation map.
cpl_error_code moo_create_s1d(cpl_frame *sci_frame, moo_create_s1d_params *params, const char *tag, moo_products *products)
Apply the telluric correction to SCI.
moo_rbn * moo_rebin(moo_ext *ext, moo_map *wmap, moo_spectral_format *sformat, moo_rebin_params *params, const char *filename)
Rebin an EXT spectra in RBN format.
Definition: moo_rebin.c:783
moo_det * moo_prepare(const cpl_frame *rawframe, const char *const badpixmask_rp, const char *const badpixmask_nl, const cpl_frame *masterbias, const cpl_frame *cube_frame, moo_prepare_params *params)
This function transforms RAW frames in DET frames attaching the default bad pixel map and an error im...
Definition: moo_prepare.c:324
moo_sci * moo_combine_pair(moo_rbn *nod_rbn, moo_rbn *sky_rbn, moo_rbn *obj_rbn, moo_target_table *target_table, moo_f2f *f2f, const cpl_frame *solflux_frame, const cpl_frame *airglow_group_frame, const cpl_frame *airglow_var_frame, moo_combine_pair_params *combine_pair_params, moo_sub_sky_stare_params *sky_params, const char *filename, moo_products *products, const cpl_frame *ref_frame)
Combine the extracted spectra from a same objec.
cpl_error_code moo_calib_flux(moo_rbn *rbn, moo_atm *atm, moo_resp *resp)
Apply the relative flux calibration to 1D rebinned spectra.
cpl_error_code moo_apply_flat(moo_ext *ext, moo_ext *flat, moo_f2f *f2f)
Divide spectra by 1D flat-field.
cpl_error_code moo_correct_bias(moo_det *det, const cpl_frame *masterbias_frame, moo_correct_bias_params *params)
Subtracts the Master Bias frame from a DET frame.
Definition: moo_drl.c:86
moo_det * moo_subtract_nod(moo_det *object, moo_det *sky, const char *filename)
Sky from object subtraction.
moo_sci * moo_sub_sky_stare(moo_rbn *obja_rbn, moo_target_table *target_table, moo_rbn *objb_rbn, moo_f2f *f2f, const cpl_frame *solflux_frame, const cpl_frame *airglow_group_frame, const cpl_frame *airglow_var_frame, moo_sub_sky_stare_params *params, const char *filename, int ispaired)
This function subtracts the sky in wavelength calibrated and extracted science frames obtain in Stare...
cpl_error_code moo_correct_dark(moo_det *det, moo_det *detoff, const cpl_frame *masterDarkVis, const cpl_frame *masterDarkNir)
Subtracts the master dark frame from a frame after scaling for exposure time (RI)....
Definition: moo_drl.c:250
int moo_pfits_get_ndit(const cpl_propertylist *plist)
find out the ESO DET NDIT value
Definition: moo_pfits.c:382
double moo_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
Definition: moo_pfits.c:362
double moo_pfits_get_mjdobs(const cpl_propertylist *plist)
find out the MJD-OBS
Definition: moo_pfits.c:328
int moo_pfits_get_slit_offset(const cpl_propertylist *plist)
find out the INS SLIT OFFSET value
Definition: moo_pfits.c:1110
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:51
cpl_frame * moo_products_add_sci(moo_products *self, moo_sci *sci, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a SCI object
Definition: moo_products.c:372
cpl_frame * moo_products_add_rbn(moo_products *self, moo_rbn *rbn, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a RBN object
Definition: moo_products.c:328
cpl_frame * moo_products_add_ext(moo_products *self, moo_ext *ext, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a EXT object
Definition: moo_products.c:285
cpl_frame * moo_products_add_target_table(moo_products *self, moo_target_table *ttable, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a TARGET_TABLE object
Definition: moo_products.c:726
cpl_frame * moo_products_add(moo_products *self, moo_det *det, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a DET object
Definition: moo_products.c:188
const moo_params * moo_products_get_params(const moo_products *self)
get the moo_params object
Definition: moo_products.c:85
const char * moo_get_license(void)
Get the pipeline copyright and license.
Definition: moo_utils.c:81
the different type of detectors