MOONS Pipeline Reference Manual 0.13.1
moo_rbn.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 <cpl.h>
29#include "moo_pfits.h"
30#include "moo_fits.h"
31#include "moo_badpix.h"
32#include "moo_rbn.h"
33#include "moo_fibres_table.h"
34/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
50
53/*-----------------------------------------------------------------------------
54 Function codes
55 -----------------------------------------------------------------------------*/
56
57/*----------------------------------------------------------------------------*/
65/*----------------------------------------------------------------------------*/
66moo_rbn *
68{
69 moo_rbn *res = cpl_calloc(1, sizeof(moo_rbn));
70 return res;
71}
72
73/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84moo_rbn *
85moo_rbn_create(const cpl_frame *frame)
86{
87 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
88
89 cpl_errorstate prev_state = cpl_errorstate_get();
90 moo_rbn *res = moo_rbn_new();
91 const char *filename = cpl_frame_get_filename(frame);
92
93 res->filename = filename;
94 res->primary_header = cpl_propertylist_load(filename, 0);
95
96 res->ri = moo_rbn_single_create(filename, MOO_TYPE_RI);
97 res->yj = moo_rbn_single_create(filename, MOO_TYPE_YJ);
98 res->h = moo_rbn_single_create(filename, MOO_TYPE_H);
99
100 if (!cpl_errorstate_is_equal(prev_state)) {
101 cpl_msg_info("moo_rbn", "Error in rbn create ecode=%d filename=%s",
102 cpl_error_get_code(), filename);
103 cpl_errorstate_set(prev_state);
104 }
105 return res;
106}
107
108/*----------------------------------------------------------------------------*/
117/*----------------------------------------------------------------------------*/
118
119void
120moo_rbn_delete(moo_rbn *self)
121{
122 if (self != NULL) {
123 if (self->primary_header != NULL) {
124 cpl_propertylist_delete(self->primary_header);
125 }
126
127 if (self->ri != NULL) {
128 moo_rbn_single_delete(self->ri);
129 }
130 if (self->yj != NULL) {
131 moo_rbn_single_delete(self->yj);
132 }
133 if (self->h != NULL) {
134 moo_rbn_single_delete(self->h);
135 }
136
137 if (self->fibre_table != NULL) {
138 cpl_table_delete(self->fibre_table);
139 }
140
141 cpl_free(self);
142 }
143}
144
145
146/*----------------------------------------------------------------------------*/
158/*----------------------------------------------------------------------------*/
159cpl_error_code
161{
162 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
163
164 cpl_error_code status = CPL_ERROR_NONE;
165
166 moo_rbn_single *single = moo_rbn_get_single(self, type);
167 if (single != NULL) {
168 moo_rbn_single_free(single);
169 }
170 return status;
171}
172
173/*----------------------------------------------------------------------------*/
182/*----------------------------------------------------------------------------*/
183cpl_error_code
184moo_rbn_set_single(moo_rbn *self, moo_rbn_single *single)
185{
186 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
187
188 cpl_error_code status = CPL_ERROR_NONE;
189 cpl_errorstate prev_state = cpl_errorstate_get();
190
191 if (single != NULL) {
192 switch (single->type) {
193 case MOO_TYPE_RI:
194 self->ri = single;
195 break;
196 case MOO_TYPE_YJ:
197 self->yj = single;
198 break;
199 case MOO_TYPE_H:
200 self->h = single;
201 break;
202 }
203 }
204 if (!cpl_errorstate_is_equal(prev_state)) {
205 cpl_msg_error("moo_rbn", "Error for adding rbn to %s (%d)",
206 self->filename, cpl_error_get_code());
207 status = cpl_error_get_code();
208 cpl_errorstate_set(prev_state);
209 }
210 return status;
211}
212
213/*----------------------------------------------------------------------------*/
224/*----------------------------------------------------------------------------*/
225cpl_error_code
226moo_rbn_add_single(moo_rbn *self,
228 moo_rbn_single *single)
229{
230 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
231 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
232 cpl_error_code status = CPL_ERROR_NONE;
233 cpl_errorstate prev_state = cpl_errorstate_get();
234 moo_rbn_set_single(self, single);
235
236 moo_rbn_single_save(single, self->filename, type);
237
238 if (!cpl_errorstate_is_equal(prev_state)) {
239 cpl_msg_error("moo_rbn", "Error for adding single to %s (%s)",
240 self->filename,
241 cpl_error_get_message_default(cpl_error_get_code()));
242 status = cpl_error_get_code();
243 }
244 return status;
245}
246
247/*----------------------------------------------------------------------------*/
257/*----------------------------------------------------------------------------*/
258void
259moo_rbn_save(moo_rbn *self, const char *filename)
260{
261 if (self != NULL) {
262 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
263 moo_rbn_single_save(self->ri, filename, MOO_TYPE_RI);
264 moo_rbn_single_save(self->yj, filename, MOO_TYPE_YJ);
265 moo_rbn_single_save(self->h, filename, MOO_TYPE_H);
266
267 if (self->fibre_table != NULL) {
268 cpl_propertylist *h = cpl_propertylist_new();
269 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
270 MOO_FIBRES_TABLE_EXTNAME);
271 cpl_table_save(self->fibre_table, h, h, filename, CPL_IO_EXTEND);
272 cpl_propertylist_delete(h);
273 }
274 }
275}
276
277/*----------------------------------------------------------------------------*/
286/*----------------------------------------------------------------------------*/
287cpl_error_code
288moo_rbn_add_fibre_table(moo_rbn *self, cpl_table *fibre_table)
289{
290 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
291 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
292 cpl_ensure_code(fibre_table != NULL, CPL_ERROR_NULL_INPUT);
293
294 cpl_error_code status = CPL_ERROR_NONE;
295 cpl_errorstate prev_state = cpl_errorstate_get();
296 self->fibre_table = fibre_table;
297
298 cpl_propertylist *h = cpl_propertylist_new();
299 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
300 MOO_FIBRES_TABLE_EXTNAME);
301 cpl_table_save(self->fibre_table, h, h, self->filename, CPL_IO_EXTEND);
302 cpl_propertylist_delete(h);
303
304 if (!cpl_errorstate_is_equal(prev_state)) {
305 cpl_msg_error(__func__, "Error for adding fibre table to %s (%d)",
306 self->filename, cpl_error_get_code());
307 status = cpl_error_get_code();
308 cpl_errorstate_set(prev_state);
309 }
310 return status;
311}
312
313/*----------------------------------------------------------------------------*/
323/*----------------------------------------------------------------------------*/
324moo_rbn_single *
326{
327 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
328
329 switch (type) {
330 case MOO_TYPE_RI:
331 return self->ri;
332 break;
333 case MOO_TYPE_YJ:
334 return self->yj;
335 break;
336 case MOO_TYPE_H:
337 return self->h;
338 break;
339 default:
340 return NULL;
341 }
342}
343
344/*----------------------------------------------------------------------------*/
358/*----------------------------------------------------------------------------*/
359moo_rbn_single *
360moo_rbn_load_single(moo_rbn *self, moo_detector_type type, unsigned int level)
361{
362 moo_rbn_single *res = NULL;
363
364 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
365
366 switch (type) {
367 case MOO_TYPE_RI:
368 moo_rbn_single_load(self->ri, level);
369 res = self->ri;
370 break;
371 case MOO_TYPE_YJ:
372 moo_rbn_single_load(self->yj, level);
373 res = self->yj;
374 break;
375 case MOO_TYPE_H:
376 moo_rbn_single_load(self->h, level);
377 res = self->h;
378 break;
379 default:
380 res = NULL;
381 break;
382 }
383 return res;
384}
385
386/*----------------------------------------------------------------------------*/
395/*----------------------------------------------------------------------------*/
396cpl_table *
398{
399 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
400
401 if (self->fibre_table == NULL && self->filename != NULL) {
402 cpl_size qnum =
403 cpl_fits_find_extension(self->filename, MOO_FIBRES_TABLE_EXTNAME);
404 if (qnum > 0) {
405 self->fibre_table = cpl_table_load(self->filename, qnum, 0);
406 }
407 }
408 return self->fibre_table;
409}
410
411/*----------------------------------------------------------------------------*/
419/*----------------------------------------------------------------------------*/
420cpl_error_code
421moo_rbn_compute_snr(moo_rbn *self, moo_sky_lines_list *skylines)
422{
423 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
424 cpl_ensure_code(skylines != NULL, CPL_ERROR_NULL_INPUT);
425
426 cpl_table *fibre_table = moo_rbn_get_fibre_table(self);
427 const char *snrs[] = { MOO_FIBRES_TABLE_MEDSNR_RI_RBN,
428 MOO_FIBRES_TABLE_MEDSNR_YJ_RBN,
429 MOO_FIBRES_TABLE_MEDSNR_H_RBN };
430
431 int nrow = cpl_table_get_nrow(fibre_table);
432
433 for (int t = 0; t < nrow; t++) {
434 int targ_idx =
435 cpl_table_get_int(fibre_table, MOO_FIBRES_TABLE_INDEXRBN, t, NULL);
436 int health =
437 cpl_table_get_int(fibre_table, MOO_FIBRES_TABLE_HEALTH, t, NULL);
438 if (health > 0) {
439 for (int i = 0; i < 3; i++) {
440 moo_rbn_single *single =
442 if (single != NULL) {
443 double snr = 0.0;
444 snr =
445 moo_rbn_single_compute_snr(single, targ_idx, skylines);
446
447 cpl_table_set_float(fibre_table, snrs[i], t, snr);
448 }
449 }
450 }
451 }
452 return CPL_ERROR_NONE;
453}
454
455/*----------------------------------------------------------------------------*/
467/*----------------------------------------------------------------------------*/
468cpl_error_code
469moo_rbn_dump(const moo_rbn *self, FILE *stream)
470{
471 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
472 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
473
474 fprintf(stream, "---MOO_RBN\n");
475 fprintf(stream, "filename %s\n", self->filename);
476
477 if (self->ri != NULL) {
478 fprintf(stream, "---RI %p\n", self->ri);
479 moo_rbn_single_dump(self->ri, stream);
480 }
481
482 if (self->yj != NULL) {
483 fprintf(stream, "---YJ %p\n", self->yj);
484 moo_rbn_single_dump(self->yj, stream);
485 }
486
487 if (self->h != NULL) {
488 fprintf(stream, "---H %p\n", self->h);
489 moo_rbn_single_dump(self->h, stream);
490 }
491
492 if (self->fibre_table != NULL) {
493 fprintf(stream, "---FT %p\n", self->fibre_table);
494 }
495
496 return CPL_ERROR_NONE;
497}
498
499/*----------------------------------------------------------------------------*/
510/*----------------------------------------------------------------------------*/
511cpl_propertylist *
513{
514 if (self->primary_header == NULL && self->filename != NULL) {
515 self->primary_header = cpl_propertylist_load(self->filename, 0);
516 }
517 return self->primary_header;
518}
#define MOO_BADPIX_GOOD
Definition: moo_badpix.h:36
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
moo_rbn_single * moo_rbn_single_create(const char *filename, moo_detector_type type)
Create a new moo_rbn_single with the given filename and type.
void moo_rbn_single_save(const moo_rbn_single *self, const char *filename, moo_detector_type type)
Save a moo_rbn_single to a FITS file.
void moo_rbn_single_delete(moo_rbn_single *self)
Delete a moo_rbn_single.
cpl_error_code moo_rbn_single_dump(const moo_rbn_single *self, FILE *stream)
Dump structural information of a Single DET.
cpl_error_code moo_rbn_single_load(moo_rbn_single *self, unsigned int level)
load the data of a moo_rbn_single
double moo_rbn_single_compute_snr(moo_rbn_single *self, int rbn_idx, moo_sky_lines_list *skylines)
Compute SNR for a given target.
cpl_error_code moo_rbn_single_free(moo_rbn_single *self)
Free memory associate to this single RBN.
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_error_code moo_rbn_set_single(moo_rbn *self, moo_rbn_single *single)
assign moo_rbn_single structure in moo_rbn structure
Definition: moo_rbn.c:184
void moo_rbn_delete(moo_rbn *self)
Delete a moo_rbn.
Definition: moo_rbn.c:120
void moo_rbn_save(moo_rbn *self, const char *filename)
Save a moo_rbn to a FITS file.
Definition: moo_rbn.c:259
cpl_propertylist * moo_rbn_get_primary_header(moo_rbn *self)
Get the PRIMARY HEADER in RBN.
Definition: moo_rbn.c:512
moo_rbn_single * moo_rbn_get_single(moo_rbn *self, moo_detector_type type)
Get a RBN single from RBN.
Definition: moo_rbn.c:325
cpl_error_code moo_rbn_dump(const moo_rbn *self, FILE *stream)
Dump structural information of RBN.
Definition: moo_rbn.c:469
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_add_single(moo_rbn *self, moo_detector_type type, moo_rbn_single *single)
Add RBN_SINGLE extension to RBN filename and update moo_rbn structure.
Definition: moo_rbn.c:226
cpl_error_code moo_rbn_add_fibre_table(moo_rbn *self, cpl_table *fibre_table)
Add fibre table to RBN filename and update moo_rbn structure.
Definition: moo_rbn.c:288
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
cpl_table * moo_rbn_get_fibre_table(moo_rbn *self)
Get the FIBRE TABLE in RBN.
Definition: moo_rbn.c:397
moo_rbn * moo_rbn_new(void)
Create a new moo_rbn.
Definition: moo_rbn.c:67
cpl_error_code moo_rbn_free_single(moo_rbn *self, moo_detector_type type)
Free the given type part in RBN.
Definition: moo_rbn.c:160