MOONS Pipeline Reference Manual 0.13.2
moo_f2f.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#include "moo_pfits.h"
31#include "moo_fits.h"
32#include "moo_qc.h"
33#include "moo_f2f.h"
34#include "moo_fibres_table.h"
35#include "moo_utils.h"
36/*----------------------------------------------------------------------------*/
50/*----------------------------------------------------------------------------*/
51
54/*-----------------------------------------------------------------------------
55 Function codes
56 -----------------------------------------------------------------------------*/
57
58/*----------------------------------------------------------------------------*/
66/*----------------------------------------------------------------------------*/
67moo_f2f *
69{
70 moo_f2f *res = cpl_calloc(1, sizeof(moo_f2f));
71 return res;
72}
73
74/*----------------------------------------------------------------------------*/
84/*----------------------------------------------------------------------------*/
85moo_f2f *
86moo_f2f_create(int nbrows, cpl_table *fibre_table)
87{
88 moo_f2f *res = moo_f2f_new();
89
90 res->table = cpl_table_new(nbrows);
91 cpl_table_new_column(res->table, MOO_F2F_TRANS_RI, MOO_F2F_TRANS_RI_TYPE);
92 cpl_table_new_column(res->table, MOO_F2F_TRANS_YJ, MOO_F2F_TRANS_YJ_TYPE);
93 cpl_table_new_column(res->table, MOO_F2F_TRANS_H, MOO_F2F_TRANS_H_TYPE);
94
95 cpl_table_duplicate_column(res->table, MOO_F2F_INDEX, fibre_table,
96 MOO_FIBRES_TABLE_INDEX);
97 cpl_table_duplicate_column(res->table, MOO_FIBRES_TABLE_SPECTRO,
98 fibre_table, MOO_FIBRES_TABLE_SPECTRO);
99 cpl_table_duplicate_column(res->table, MOO_FIBRES_TABLE_INDEXEXT,
100 fibre_table, MOO_FIBRES_TABLE_INDEXEXT);
101 cpl_table_new_column(res->table, MOO_FIBRES_TABLE_INDEXRBN, CPL_TYPE_INT);
102
103 cpl_table *moons1 = moo_fibres_table_get_spectro_table(fibre_table, 1);
104 int moons1_nrow = cpl_table_get_nrow(moons1);
105 cpl_table_delete(moons1);
106
107 const int *indexext =
108 cpl_table_get_data_int_const(res->table, MOO_FIBRES_TABLE_INDEXEXT);
109 const int *spectro =
110 cpl_table_get_data_int_const(res->table, MOO_FIBRES_TABLE_SPECTRO);
111 int *indexrbn =
112 cpl_table_get_data_int(res->table, MOO_FIBRES_TABLE_INDEXRBN);
113 for (int i = 0; i < nbrows; i++) {
114 indexrbn[i] = indexext[i];
115 cpl_table_set_float(res->table, MOO_F2F_TRANS_RI, i, 0.);
116 cpl_table_set_float(res->table, MOO_F2F_TRANS_YJ, i, 0.);
117 cpl_table_set_float(res->table, MOO_F2F_TRANS_H, i, 0.);
118
119 if (MOO_FIBRES_TABLE_SPECTRO_2 == spectro[i] && indexrbn[i] != 0) {
120 indexrbn[i] += moons1_nrow;
121 }
122 }
123 res->primary_header = cpl_propertylist_new();
124
125 return res;
126}
127
128/*----------------------------------------------------------------------------*/
137/*----------------------------------------------------------------------------*/
138moo_f2f *
139moo_f2f_load(const cpl_frame *f2f_frame)
140{
141 cpl_ensure(f2f_frame, CPL_ERROR_NULL_INPUT, NULL);
142 cpl_errorstate prev_state = cpl_errorstate_get();
143
144 const char *filename = cpl_frame_get_filename(f2f_frame);
145 moo_f2f *res = moo_f2f_new();
146 res->filename = filename;
147 moo_try_check(res->table = cpl_table_load(filename, 1, 0), " ");
148 moo_try_check(res->primary_header = cpl_propertylist_load(filename, 0),
149 " ");
150
151moo_try_cleanup:
152 if (!cpl_errorstate_is_equal(prev_state)) {
153 moo_f2f_delete(res);
154 res = NULL;
155 cpl_errorstate_set(prev_state);
156 }
157 return res;
158}
159
160/*----------------------------------------------------------------------------*/
167/*----------------------------------------------------------------------------*/
168cpl_error_code
170{
171 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
172
173 cpl_propertylist *order = cpl_propertylist_new();
174 cpl_propertylist_append_bool(order, MOO_FIBRES_TABLE_INDEXRBN, CPL_FALSE);
175 cpl_table_sort(self->table, order);
176 cpl_propertylist_delete(order);
177
178 return CPL_ERROR_NONE;
179}
180
181/*----------------------------------------------------------------------------*/
188/*----------------------------------------------------------------------------*/
189cpl_error_code
191{
192 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
193
194 cpl_propertylist *order = cpl_propertylist_new();
195 cpl_propertylist_append_bool(order, MOO_FIBRES_TABLE_SPECTRO, CPL_FALSE);
196 cpl_propertylist_append_bool(order, MOO_FIBRES_TABLE_INDEX, CPL_FALSE);
197 cpl_table_sort(self->table, order);
198 cpl_propertylist_delete(order);
199
200 return CPL_ERROR_NONE;
201}
202/*----------------------------------------------------------------------------*/
214/*----------------------------------------------------------------------------*/
215cpl_error_code
216moo_f2f_set_trans(moo_f2f *self,
218 cpl_array *idxtab,
219 cpl_vector *values)
220{
221 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
222 cpl_ensure_code(idxtab != NULL, CPL_ERROR_NULL_INPUT);
223 cpl_ensure_code(values != NULL, CPL_ERROR_NULL_INPUT);
224
225 cpl_error_code status = CPL_ERROR_NONE;
226
227 const char *colname = NULL;
228 switch (type) {
229 case MOO_TYPE_RI:
230 colname = MOO_F2F_TRANS_RI;
231 break;
232 case MOO_TYPE_YJ:
233 colname = MOO_F2F_TRANS_YJ;
234 break;
235 case MOO_TYPE_H:
236 colname = MOO_F2F_TRANS_H;
237 break;
238 }
239
240 int size = cpl_array_get_size(idxtab);
241
242 for (int i = 0; i < size; i++) {
243 cpl_size idx = cpl_array_get_cplsize(idxtab, i, NULL);
244 float v = (float)cpl_vector_get(values, i);
245 cpl_table_set(self->table, colname, idx, v);
246 }
247 return status;
248}
249
250/*----------------------------------------------------------------------------*/
258/*----------------------------------------------------------------------------*/
259const char *
261{
262 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
263
264 const char *colname = NULL;
265 switch (type) {
266 case MOO_TYPE_RI:
267 colname = MOO_F2F_TRANS_RI;
268 break;
269 case MOO_TYPE_YJ:
270 colname = MOO_F2F_TRANS_YJ;
271 break;
272 case MOO_TYPE_H:
273 colname = MOO_F2F_TRANS_H;
274 break;
275 }
276 return colname;
277}
278/*----------------------------------------------------------------------------*/
287/*----------------------------------------------------------------------------*/
288cpl_vector *
289moo_f2f_get_trans(moo_f2f *self, moo_detector_type type, cpl_array *idxtab)
290{
291 cpl_vector *res = NULL;
292
293 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
294 cpl_ensure(idxtab != NULL, CPL_ERROR_NULL_INPUT, NULL);
295
296 int size = cpl_array_get_size(idxtab);
297 const char *colname = moo_f2f_get_trans_colname(self, type);
298 if (size > 0) {
299 res = cpl_vector_new(size);
300 for (int i = 0; i < size; i++) {
301 cpl_size idx = cpl_array_get_cplsize(idxtab, i, NULL);
302 double v = cpl_table_get(self->table, colname, idx, NULL);
303 cpl_vector_set(res, i, v);
304 }
305 }
306 return res;
307}
308
309/*----------------------------------------------------------------------------*/
317/*----------------------------------------------------------------------------*/
318float *
320{
321 float *res = NULL;
322
323 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
324
325 const char *colname = NULL;
326 switch (type) {
327 case MOO_TYPE_RI:
328 colname = MOO_F2F_TRANS_RI;
329 break;
330 case MOO_TYPE_YJ:
331 colname = MOO_F2F_TRANS_YJ;
332 break;
333 case MOO_TYPE_H:
334 colname = MOO_F2F_TRANS_H;
335 break;
336 }
337
338 res = cpl_table_get_data_float(self->table, colname);
339
340 return res;
341}
342
343cpl_error_code
344moo_f2f_compute_qc(moo_f2f *self, int *ref, cpl_array *bad)
345{
346 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
347 cpl_ensure_code(ref != NULL, CPL_ERROR_NULL_INPUT);
348
349 cpl_table *selected = NULL;
350
351 for (int i = 0; i < 2; i++) {
352 moo_try_check(moo_qc_set_fibtrans_ref(self->primary_header, i + 1,
353 ref[i]),
354 " ");
355 }
356
357 int size = cpl_array_get_size(bad);
358
359 for (int i = 0; i < size; i++) {
360 int idx = cpl_array_get_cplsize(bad, i, NULL);
361 cpl_table_unselect_row(self->table, idx);
362 }
363 selected = cpl_table_extract_selected(self->table);
364
365 const char *transname[] = { MOO_F2F_TRANS_RI, MOO_F2F_TRANS_YJ,
366 MOO_F2F_TRANS_H };
367
368 for (int i = 0; i < 3; i++) {
369 if (cpl_table_has_valid(selected, transname[i])) {
370 double min = cpl_table_get_column_min(selected, transname[i]);
371 double max = cpl_table_get_column_max(selected, transname[i]);
372 double med = cpl_table_get_column_median(selected, transname[i]);
373
374 moo_qc_set_fibtrans_min(self->primary_header, i, min);
375 moo_qc_set_fibtrans_max(self->primary_header, i, max);
376 moo_qc_set_fibtrans_med(self->primary_header, i, med);
377 }
378 }
379moo_try_cleanup:
380 cpl_table_delete(selected);
381
382 return cpl_error_get_code();
383}
384
385cpl_error_code
386moo_f2f_save(moo_f2f *self, const char *filename)
387{
388 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
389 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
390
391 cpl_propertylist *extheader = cpl_propertylist_new();
392 cpl_propertylist_append_string(extheader, MOO_PFITS_EXTNAME,
393 MOO_F2F_EXTNAME);
394 self->filename = filename;
395 cpl_table_save(self->table, self->primary_header, extheader, filename,
396 CPL_IO_CREATE);
397 cpl_propertylist_delete(extheader);
398
399 return cpl_error_get_code();
400}
401/*----------------------------------------------------------------------------*/
410/*----------------------------------------------------------------------------*/
411
412void
413moo_f2f_delete(moo_f2f *self)
414{
415 if (self != NULL) {
416 if (self->table != NULL) {
417 cpl_table_delete(self->table);
418 }
419 if (self->primary_header != NULL) {
420 cpl_propertylist_delete(self->primary_header);
421 }
422 cpl_free(self);
423 }
424}
425
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
@ MOO_TYPE_YJ
Definition: moo_detector.h:50
@ MOO_TYPE_H
Definition: moo_detector.h:54
@ MOO_TYPE_RI
Definition: moo_detector.h:46
float * moo_f2f_get_trans_column(moo_f2f *self, moo_detector_type type)
Get transmission column from table.
Definition: moo_f2f.c:319
moo_f2f * moo_f2f_new(void)
Create a new moo_f2f.
Definition: moo_f2f.c:68
const char * moo_f2f_get_trans_colname(moo_f2f *self, moo_detector_type type)
Get transmission column name from table.
Definition: moo_f2f.c:260
cpl_error_code moo_f2f_order_by_indexrbn(moo_f2f *self)
Order F2F by INDEXRBN (ASC)
Definition: moo_f2f.c:169
void moo_f2f_delete(moo_f2f *self)
Delete a moo_f2f.
Definition: moo_f2f.c:413
cpl_vector * moo_f2f_get_trans(moo_f2f *self, moo_detector_type type, cpl_array *idxtab)
Get transmission values from table.
Definition: moo_f2f.c:289
moo_f2f * moo_f2f_load(const cpl_frame *f2f_frame)
Load a F2F table from a fits file.
Definition: moo_f2f.c:139
cpl_error_code moo_f2f_set_trans(moo_f2f *self, moo_detector_type type, cpl_array *idxtab, cpl_vector *values)
Set transmission values in table.
Definition: moo_f2f.c:216
moo_f2f * moo_f2f_create(int nbrows, cpl_table *fibre_table)
Create a new moo_f2f and an empty structure in memory.
Definition: moo_f2f.c:86
cpl_error_code moo_f2f_order_by_index(moo_f2f *self)
Order F2F by SPECTRO,INDEX (ASC)
Definition: moo_f2f.c:190
cpl_table * moo_fibres_table_get_spectro_table(cpl_table *table, int num)
get the selection of a spectro in the fibre table
cpl_error_code moo_qc_set_fibtrans_ref(cpl_propertylist *plist, int ntas, int val)
Set the QC.SPECTRO.FIBTRANS.REF.value.
Definition: moo_qc.c:1650
cpl_error_code moo_qc_set_fibtrans_med(cpl_propertylist *plist, moo_detector_type type, double val)
Set the QC.FIBTRANS.MED value.
Definition: moo_qc.c:1786
cpl_error_code moo_qc_set_fibtrans_min(cpl_propertylist *plist, moo_detector_type type, double val)
Set the QC.FIBTRANS.MIN value.
Definition: moo_qc.c:1700
cpl_error_code moo_qc_set_fibtrans_max(cpl_propertylist *plist, moo_detector_type type, double val)
Set the QC.FIBTRANS.MAX value.
Definition: moo_qc.c:1743