MOONS Pipeline Reference Manual 0.13.2
moo_telluric.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#include <math.h>
28#include <string.h>
29#include <cpl.h>
30
31#include "moo_telluric.h"
32#include "moo_utils.h"
33#include "moo_pfits.h"
34#include "moo_fits.h"
35#include "moo_scilist.h"
36#include "moo_sci.h"
37/*----------------------------------------------------------------------------*/
50/*----------------------------------------------------------------------------*/
53/*-----------------------------------------------------------------------------
54 Function codes
55 -----------------------------------------------------------------------------*/
56
57/*----------------------------------------------------------------------------*/
65/*----------------------------------------------------------------------------*/
66moo_telluric *
68{
69 moo_telluric *res = cpl_calloc(1, sizeof(moo_telluric));
70 return res;
71}
72
73/*----------------------------------------------------------------------------*/
84/*----------------------------------------------------------------------------*/
85
86cpl_error_code
87moo_telluric_set_image(moo_telluric *self,
89 cpl_image *im)
90{
91 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
92
93 cpl_error_code status = CPL_ERROR_NONE;
94
95 if (type >= 0 && type < 3) {
96 self->data[type] = im;
97 }
98 else {
99 status = CPL_ERROR_ILLEGAL_INPUT;
100 }
101 return status;
102}
103
104/*----------------------------------------------------------------------------*/
114/*----------------------------------------------------------------------------*/
115cpl_image *
117{
118 cpl_image *result = NULL;
119 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
120 if (type >= 0 && type < 3) {
121 result = self->data[type];
122 }
123 return result;
124}
125
126/*----------------------------------------------------------------------------*/
139/*----------------------------------------------------------------------------*/
140moo_telluric *
141moo_telluric_create(moo_scilist *scilist, int badpix_level)
142{
143 moo_telluric *result = NULL;
144
145 cpl_ensure(scilist != NULL, CPL_ERROR_NULL_INPUT, NULL);
146
147 int size = moo_scilist_get_size(scilist);
148
149 result = moo_telluric_new();
150 result->primary_header = cpl_propertylist_new();
151 result->telluric_table = cpl_table_new(size * 3);
152
153 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_INDEXRBN,
154 CPL_TYPE_INT);
155 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_SPECTRO,
156 CPL_TYPE_INT);
157 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_BAND,
158 CPL_TYPE_STRING);
159 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_TELLURIC,
160 CPL_TYPE_INT);
161
162 const char *bandcolnames[] = { MOO_TELLURIC_EXT_RI, MOO_TELLURIC_EXT_YJ,
163 MOO_TELLURIC_EXT_H };
164 for (int il = 0; il < size; il++) {
165 cpl_array *sel = NULL;
166 moo_sci *sci = moo_scilist_get(scilist, il);
167 moo_target_table *target_table = moo_sci_get_target_table(sci);
168 if (target_table->table != NULL) {
169 const char *targname = moo_target_table_get_std_name(target_table);
170 int idtarget =
171 moo_target_table_find_target(sci->target_table, targname);
172 cpl_table_select_all(sci->target_table->table);
173 cpl_table_and_selected_int(sci->target_table->table,
174 MOO_TARGET_TABLE_INDEXTARG, CPL_EQUAL_TO,
175 idtarget);
176 sel = cpl_table_where_selected(sci->target_table->table);
177 int idx = cpl_array_get_cplsize(sel, 0, NULL);
178 int idrbn = cpl_table_get_int(sci->target_table->table,
179 MOO_TARGET_TABLE_INDEXRBN, idx, NULL);
180 const int spectro =
181 cpl_table_get_int(sci->target_table->table,
182 MOO_TARGET_TABLE_SPECTRO, idx, NULL);
183 for (int type = 0; type < 3; type++) {
184 const char *band = bandcolnames[type];
185
186 cpl_table_set_int(result->telluric_table,
187 MOO_TELLURIC_TABLE_INDEXRBN, il * 3 + type,
188 idrbn);
189 cpl_table_set_int(result->telluric_table,
190 MOO_TELLURIC_TABLE_SPECTRO, il * 3 + type,
191 spectro);
192 cpl_table_set_string(result->telluric_table,
193 MOO_TELLURIC_TABLE_BAND, il * 3 + type,
194 band);
195 cpl_table_set_int(result->telluric_table,
196 MOO_TELLURIC_TABLE_TELLURIC, il * 3 + type,
197 il + 1);
198 }
199 cpl_array_delete(sel);
200 }
201 }
202
203 for (int i = 0; i < 3; i++) {
204 cpl_image *im = NULL;
205 int ny = 0;
206 int nx = 0;
207 for (int il = 0; il < size; il++) {
208 moo_sci *sci = moo_scilist_get(scilist, il);
209 moo_target_table *target_table = moo_sci_get_target_table(sci);
210
211 if (target_table->table != NULL) {
212 moo_sci_single *sci_single =
213 moo_sci_load_single(sci, i, badpix_level);
214 if (sci_single != NULL) {
215 cpl_propertylist *sci_header =
216 moo_sci_single_get_header(sci_single);
217
218 if (il == 0) {
219 cpl_propertylist *header = cpl_propertylist_new();
220 cpl_propertylist_copy_property(header, sci_header,
221 MOO_PFITS_CD1_1);
222 cpl_propertylist_copy_property(header, sci_header,
223 MOO_PFITS_CRVAL1);
224 cpl_propertylist_copy_property(header, sci_header,
225 MOO_PFITS_CRPIX1);
226 cpl_propertylist_copy_property(header, sci_header,
227 MOO_PFITS_CUNIT1);
228 result->data_header[i] = header;
229 }
230 hdrl_image *himg = moo_sci_single_get_image(sci_single);
231 nx = hdrl_image_get_size_x(himg);
232 ny++;
233 }
234 }
235 }
236 if (ny > 0) {
237 im = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
238 moo_telluric_set_image(result, i, im);
239 }
240 }
241
242 return result;
243}
244
245/*----------------------------------------------------------------------------*/
257/*----------------------------------------------------------------------------*/
258moo_telluric *
260{
261 moo_telluric *result = NULL;
262
263 cpl_ensure(rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
264
265 cpl_table *fibre_table = moo_rbn_get_fibre_table(rbn);
266 int size = cpl_table_get_nrow(fibre_table);
267 result = moo_telluric_new();
268 result->primary_header = cpl_propertylist_new();
269 result->telluric_table = cpl_table_new(size);
270
271 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_INDEXRBN,
272 CPL_TYPE_INT);
273 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_SPECTRO,
274 CPL_TYPE_STRING);
275 cpl_table_new_column(result->telluric_table, MOO_TELLURIC_TABLE_TELLURIC,
276 CPL_TYPE_INT);
277
278 for (int i = 0; i < size; i++) {
279 int idrbn =
280 cpl_table_get_int(fibre_table, MOO_TARGET_TABLE_INDEXRBN, i, NULL);
281 const int spectro =
282 cpl_table_get_int(fibre_table, MOO_TARGET_TABLE_SPECTRO, i, NULL);
283 cpl_table_set_int(result->telluric_table, MOO_TELLURIC_TABLE_INDEXRBN,
284 i, idrbn);
285 cpl_table_set_int(result->telluric_table, MOO_TELLURIC_TABLE_SPECTRO, i,
286 spectro);
287 cpl_table_set_int(result->telluric_table, MOO_TELLURIC_TABLE_TELLURIC,
288 i, idrbn);
289 }
290
291 for (int i = 0; i < 3; i++) {
292 int ny = 0;
293 int nx = 0;
294
295 moo_rbn_single *rbn_single =
296 moo_rbn_load_single(rbn, i, MOONS_FLAG_GOOD);
297 if (rbn_single != NULL) {
298 cpl_propertylist *rbn_header =
299 moo_rbn_single_get_header(rbn_single);
300 cpl_propertylist *header = cpl_propertylist_new();
301 cpl_propertylist_copy_property(header, rbn_header, MOO_PFITS_CD1_1);
302 cpl_propertylist_copy_property(header, rbn_header,
303 MOO_PFITS_CRVAL1);
304 cpl_propertylist_copy_property(header, rbn_header,
305 MOO_PFITS_CRPIX1);
306 cpl_propertylist_copy_property(header, rbn_header,
307 MOO_PFITS_CUNIT1);
308 result->data_header[i] = header;
309
310 hdrl_image *himg = moo_rbn_single_get_image(rbn_single);
311 nx = hdrl_image_get_size_x(himg);
312 ny = hdrl_image_get_size_y(himg);
313
314 cpl_image *im = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
315 moo_telluric_set_image(result, i, im);
316 }
317 }
318
319 return result;
320}
321
322/*----------------------------------------------------------------------------*/
331/*----------------------------------------------------------------------------*/
332
333void
334moo_telluric_delete(moo_telluric *self)
335{
336 if (self != NULL) {
337 if (self->primary_header != NULL) {
338 cpl_propertylist_delete(self->primary_header);
339 }
340 for (int i = 0; i < 3; i++) {
341 if (self->data[i] != NULL) {
342 cpl_image_delete(self->data[i]);
343 }
344 if (self->data_header[i] != NULL) {
345 cpl_propertylist_delete(self->data_header[i]);
346 }
347 }
348 if (self->telluric_table != NULL) {
349 cpl_table_delete(self->telluric_table);
350 }
351 cpl_free(self);
352 }
353}
354
355/*----------------------------------------------------------------------------*/
365/*----------------------------------------------------------------------------*/
366void
367moo_telluric_save(moo_telluric *self, const char *filename)
368{
369 if (self != NULL) {
370 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
371 const char *extnames[] = { MOO_TELLURIC_EXT_RI, MOO_TELLURIC_EXT_YJ,
372 MOO_TELLURIC_EXT_H };
373
374 for (int i = 0; i < 3; i++) {
375 cpl_propertylist *h = cpl_propertylist_new();
376 cpl_image *im = self->data[i];
377 const char *extname = extnames[i];
378 moo_fits_write_extension_image(im, filename, NULL, extname,
379 CPL_TYPE_FLOAT,
380 self->data_header[i]);
381 cpl_propertylist_delete(h);
382 }
383
384 if (self->telluric_table != NULL) {
385 cpl_propertylist *h = cpl_propertylist_new();
386 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
387 MOO_TELLURIC_EXT_STANDARD_TABLE);
388 cpl_table_save(self->telluric_table, h, h, filename, CPL_IO_EXTEND);
389 cpl_propertylist_delete(h);
390 }
391 }
392}
393
394/*----------------------------------------------------------------------------*/
403/*----------------------------------------------------------------------------*/
404moo_telluric *
405moo_telluric_load(const cpl_frame *frame)
406{
407 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
408
409 const char *filename = cpl_frame_get_filename(frame);
410 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
411 cpl_errorstate prev_state = cpl_errorstate_get();
412
413 moo_telluric *res = moo_telluric_new();
414
415 res->primary_header = cpl_propertylist_load(filename, 0);
416 const char *extnames[] = { MOO_TELLURIC_EXT_RI, MOO_TELLURIC_EXT_YJ,
417 MOO_TELLURIC_EXT_H };
418
419 for (int i = 0; i < 3; i++) {
420 const char *extname = extnames[i];
421 cpl_propertylist *header = NULL;
422 header = moo_fits_load_extension_header(filename, NULL, extname);
423 res->data_header[i] = header;
424
425 if (header != NULL) {
426 int naxis = moo_pfits_get_naxis(header);
427 if (naxis == 2) {
428 res->data[i] =
429 moo_fits_load_extension_image(filename, NULL, extname,
430 CPL_TYPE_DOUBLE);
431 }
432 }
433 }
434 cpl_size qnum =
435 cpl_fits_find_extension(filename, MOO_TELLURIC_EXT_STANDARD_TABLE);
436 if (qnum > 0) {
437 res->telluric_table = cpl_table_load(filename, qnum, 0);
438 }
439
440 if (!cpl_errorstate_is_equal(prev_state)) {
441 cpl_msg_debug(__func__, "Error in loading telluric : %d",
442 cpl_error_get_code());
444 res = NULL;
445 cpl_errorstate_set(prev_state);
446 }
447 return res;
448}
449
450/*----------------------------------------------------------------------------*/
460/*----------------------------------------------------------------------------*/
461
462int
463moo_telluric_get_index(moo_telluric *self,
464 int idrbn,
465 int fspectro,
466 const char *fband)
467{
468 int index = -1;
469 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, -1);
470 cpl_ensure(self->telluric_table != NULL, CPL_ERROR_NULL_INPUT, -1);
471 int nrow = cpl_table_get_nrow(self->telluric_table);
472 int min_diff = 1024;
473
474 for (int i = 0; i < nrow; i++) {
475 int n = cpl_table_get_int(self->telluric_table,
476 MOO_TELLURIC_TABLE_INDEXRBN, i, NULL);
477 const int nindex =
478 cpl_table_get_int(self->telluric_table, MOO_TELLURIC_TABLE_TELLURIC,
479 i, NULL);
480 const int spectro =
481 cpl_table_get_int(self->telluric_table, MOO_TELLURIC_TABLE_SPECTRO,
482 i, NULL);
483 const char *band = cpl_table_get_string(self->telluric_table,
484 MOO_TELLURIC_TABLE_BAND, i);
485 if ((spectro == fspectro) && (strcmp(band, fband) == 0)) {
486 int diff = (int)fabs(idrbn - n);
487
488 if (diff == 0) {
489 index = nindex;
490 break;
491 }
492 else if (min_diff > diff) {
493 index = nindex;
494 min_diff = diff;
495 }
496 }
497 }
498
499 return index;
500}
501
502/*----------------------------------------------------------------------------*/
509/*----------------------------------------------------------------------------*/
510const char *
512{
513 const char *res[] = { MOO_TELLURIC_EXT_RI, MOO_TELLURIC_EXT_YJ,
514 MOO_TELLURIC_EXT_H };
515 int idx = type;
516 return res[idx];
517}
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_error_code moo_fits_write_extension_image(cpl_image *image, const char *filename, const char *name, const char *detectorname, cpl_type type, cpl_propertylist *header)
Write an image as extension in FITS file.
Definition: moo_fits.c:123
cpl_image * moo_fits_load_extension_image(const char *filename, const char *name, const char *detectorname, cpl_type type)
Load an image from FITS file.
Definition: moo_fits.c:311
hdrl_image * moo_rbn_single_get_image(moo_rbn_single *self)
Get image of RBN_SINGLE.
cpl_propertylist * moo_rbn_single_get_header(moo_rbn_single *self)
Get header of rbn single.
moo_rbn_single * moo_rbn_load_single(moo_rbn *self, moo_detector_type type, unsigned int level)
Load the type part in RBN and return it.
Definition: moo_rbn.c:360
cpl_table * moo_rbn_get_fibre_table(moo_rbn *self)
Get the FIBRE TABLE in RBN.
Definition: moo_rbn.c:397
hdrl_image * moo_sci_single_get_image(moo_sci_single *self)
Get image of SCI_SINGLE.
cpl_propertylist * moo_sci_single_get_header(moo_sci_single *self)
Get header of sci single.
moo_sci_single * moo_sci_load_single(moo_sci *self, moo_detector_type type, int level)
Load the type part in SCI and return it.
Definition: moo_sci.c:323
moo_target_table * moo_sci_get_target_table(moo_sci *self)
Get the target table of SCI file.
Definition: moo_sci.c:284
moo_sci * moo_scilist_get(moo_scilist *self, int i)
Get the SCI at the position i in the list.
Definition: moo_scilist.c:384
cpl_size moo_scilist_get_size(const moo_scilist *self)
Get the number of SCI in the scilist.
Definition: moo_scilist.c:198
int moo_target_table_find_target(moo_target_table *self, const char *targname)
find target targindex in TARGET_TABLE
moo_telluric * moo_telluric_create(moo_scilist *scilist, int badpix_level)
Prepare TELLURIC for SCI frame.
Definition: moo_telluric.c:141
moo_telluric * moo_telluric_load(const cpl_frame *frame)
Load a TELLURIC frame and create a moo_telluric.
Definition: moo_telluric.c:405
int moo_telluric_get_index(moo_telluric *self, int idrbn, int fspectro, const char *fband)
Get the index of the telluric for a given indexrbn.
Definition: moo_telluric.c:463
const char * moo_telluric_get_band(moo_detector_type type)
Get the band of the telluric
Definition: moo_telluric.c:511
#define MOO_TELLURIC_EXT_RI
TELLURIC format.
Definition: moo_telluric.h:35
cpl_image * moo_telluric_get_image(moo_telluric *self, moo_detector_type type)
Get a telluric image from TELLURIC.
Definition: moo_telluric.c:116
cpl_error_code moo_telluric_set_image(moo_telluric *self, moo_detector_type type, cpl_image *im)
assign image in moo_telluric
Definition: moo_telluric.c:87
void moo_telluric_delete(moo_telluric *self)
Delete a moo_telluric.
Definition: moo_telluric.c:334
void moo_telluric_save(moo_telluric *self, const char *filename)
Save a moo_telluric to a FITS file.
Definition: moo_telluric.c:367
moo_telluric * moo_telluric_new(void)
Create a new moo_telluric.
Definition: moo_telluric.c:67
moo_telluric * moo_telluric_create_from_rbn(moo_rbn *rbn)
Prepare TELLURIC for SCI frame.
Definition: moo_telluric.c:259
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
Definition: moo_pfits.c:980
the different type of detectors