MOONS Pipeline Reference Manual 0.13.1
moo_sci.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_sci.h"
32#include "moo_badpix.h"
33#include "moo_target_table.h"
34/*----------------------------------------------------------------------------*/
48/*----------------------------------------------------------------------------*/
49
52/*-----------------------------------------------------------------------------
53 Function codes
54 -----------------------------------------------------------------------------*/
55
56/*----------------------------------------------------------------------------*/
64/*----------------------------------------------------------------------------*/
65moo_sci *
67{
68 moo_sci *res = cpl_calloc(1, sizeof(moo_sci));
69 return res;
70}
71
72/*----------------------------------------------------------------------------*/
81/*----------------------------------------------------------------------------*/
82
83void
84moo_sci_delete(moo_sci *self)
85{
86 if (self != NULL) {
87 if (self->primary_header != NULL) {
88 cpl_propertylist_delete(self->primary_header);
89 }
90
91 if (self->ri != NULL) {
92 moo_sci_single_delete(self->ri);
93 }
94 if (self->yj != NULL) {
95 moo_sci_single_delete(self->yj);
96 }
97 if (self->h != NULL) {
98 moo_sci_single_delete(self->h);
99 }
100
101 if (self->target_table != NULL) {
102 moo_target_table_delete(self->target_table);
103 }
104 cpl_free(self);
105 }
106}
107
108cpl_error_code
109moo_sci_load(moo_sci *self, unsigned int level)
110{
111 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
112
113 for (int i = 0; i < 3; i++) {
114 moo_sci_load_single(self, i, level);
115 }
117
118 return CPL_ERROR_NONE;
119}
120/*----------------------------------------------------------------------------*/
130/*----------------------------------------------------------------------------*/
131cpl_error_code
133{
134 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
135 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
136 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
137
138 cpl_error_code status = CPL_ERROR_NONE;
139 cpl_errorstate prev_state = cpl_errorstate_get();
140 self->target_table = table;
141
142 cpl_propertylist *h = cpl_propertylist_new();
143
144 // FIXME: Use cpl_propertylist_update family of functions, rather than
145 // cpl_propertylist_append. This avoids unintentional keyword
146 // duplication!
147 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
148 MOO_TARGET_TABLE_EXTNAME);
149 cpl_table_save(self->target_table->table, h, h, self->filename,
150 CPL_IO_EXTEND);
151
152 cpl_propertylist_delete(h);
153
154 if (!cpl_errorstate_is_equal(prev_state)) {
155 cpl_msg_error(__func__, "Error for adding target table to %s (%d)",
156 self->filename, cpl_error_get_code());
157 status = cpl_error_get_code();
158 cpl_errorstate_set(prev_state);
159 }
160 return status;
161}
162
163/*----------------------------------------------------------------------------*/
172/*----------------------------------------------------------------------------*/
173cpl_error_code
174moo_sci_set_single(moo_sci *self, moo_sci_single *single)
175{
176 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
177
178 cpl_error_code status = CPL_ERROR_NONE;
179 cpl_errorstate prev_state = cpl_errorstate_get();
180
181 if (single != NULL) {
182 switch (single->type) {
183 case MOO_TYPE_RI:
184 self->ri = single;
185 break;
186 case MOO_TYPE_YJ:
187 self->yj = single;
188 break;
189 case MOO_TYPE_H:
190 self->h = single;
191 break;
192 }
193 }
194 if (!cpl_errorstate_is_equal(prev_state)) {
195 cpl_msg_error("moo_sci", "Error for adding SCI_SINGLE to file %s (%d)",
196 self->filename, cpl_error_get_code());
197 status = cpl_error_get_code();
198 cpl_errorstate_set(prev_state);
199 }
200 return status;
201}
202
203/*----------------------------------------------------------------------------*/
214/*----------------------------------------------------------------------------*/
215cpl_error_code
216moo_sci_add_single(moo_sci *self,
218 moo_sci_single *single)
219{
220 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
221 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
222 cpl_error_code status = CPL_ERROR_NONE;
223 cpl_errorstate prev_state = cpl_errorstate_get();
224
225 moo_sci_set_single(self, single);
226
227 moo_sci_single_save(single, self->filename, type);
228 if (!cpl_errorstate_is_equal(prev_state)) {
229 cpl_msg_error("moo_sci", "Error for adding single to %s (%s)",
230 self->filename,
231 cpl_error_get_message_default(cpl_error_get_code()));
232 status = cpl_error_get_code();
233 }
234 return status;
235}
236
237/*----------------------------------------------------------------------------*/
247/*----------------------------------------------------------------------------*/
248moo_sci *
249moo_sci_create(const cpl_frame *frame)
250{
251 const char *filename = NULL;
252 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
253
254 cpl_errorstate prev_state = cpl_errorstate_get();
255 moo_sci *sci = moo_sci_new();
256
257 filename = cpl_frame_get_filename(frame);
258 sci->filename = filename;
259 sci->primary_header = cpl_propertylist_load(filename, 0);
260 sci->ri = moo_sci_single_create(filename, MOO_TYPE_RI);
261 sci->yj = moo_sci_single_create(filename, MOO_TYPE_YJ);
262 sci->h = moo_sci_single_create(filename, MOO_TYPE_H);
263 if (!cpl_errorstate_is_equal(prev_state)) {
264 cpl_msg_info("moo_sci", "sci create using filename %s : %d", filename,
265 cpl_error_get_code());
266 cpl_errorstate_set(prev_state);
267 }
268 return sci;
269}
270
271/*----------------------------------------------------------------------------*/
282/*----------------------------------------------------------------------------*/
285{
286 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
287 if (self->target_table == NULL && self->filename != NULL) {
288 cpl_size qnum =
289 cpl_fits_find_extension(self->filename, MOO_TARGET_TABLE_EXTNAME);
290 if (qnum > 0) {
291 self->target_table = moo_target_table_new();
292
293 cpl_errorstate prev_state = cpl_errorstate_get();
294 self->target_table->table = cpl_table_load(self->filename, qnum, 0);
295
296 if (!cpl_errorstate_is_equal(prev_state) &&
297 CPL_ERROR_DATA_NOT_FOUND == cpl_error_get_code()) {
298 cpl_errorstate_set(prev_state);
299 }
300 self->target_table->primary_header =
301 cpl_propertylist_load(self->filename, qnum);
302 }
303 }
304 return self->target_table;
305}
306
307/*----------------------------------------------------------------------------*/
321/*----------------------------------------------------------------------------*/
322moo_sci_single *
323moo_sci_load_single(moo_sci *self, moo_detector_type type, int level)
324{
325 moo_sci_single *res = NULL;
326
327 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
328
329 res = moo_sci_get_single(self, type);
330
331 moo_sci_single_load(res, level);
332
333 return res;
334}
335
336/*----------------------------------------------------------------------------*/
349/*----------------------------------------------------------------------------*/
350moo_sci_single *
352{
353 moo_sci_single *result = NULL;
354
355 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
356 switch (type) {
357 case MOO_TYPE_RI:
358 result = self->ri;
359 break;
360 case MOO_TYPE_YJ:
361 result = self->yj;
362 break;
363 case MOO_TYPE_H:
364 result = self->h;
365 break;
366 default:
367 result = NULL;
368 break;
369 }
370 return result;
371}
372
373/*----------------------------------------------------------------------------*/
383/*----------------------------------------------------------------------------*/
384void
385moo_sci_save(moo_sci *self, const char *filename)
386{
387 if (self != NULL) {
388 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
389 moo_sci_single_save(self->ri, filename, MOO_TYPE_RI);
390 moo_sci_single_save(self->yj, filename, MOO_TYPE_YJ);
391 moo_sci_single_save(self->h, filename, MOO_TYPE_H);
392
393 if (self->target_table != NULL) {
394 cpl_propertylist *h = cpl_propertylist_new();
395 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
396 MOO_TARGET_TABLE_EXTNAME);
397
398 cpl_table_save(self->target_table->table, h, h, filename,
399 CPL_IO_EXTEND);
400 cpl_propertylist_delete(h);
401 }
402 }
403}
404
405/*----------------------------------------------------------------------------*/
413/*----------------------------------------------------------------------------*/
414cpl_error_code
415moo_sci_compute_snr(moo_sci *self, moo_sky_lines_list *skylines)
416{
417 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
418 cpl_ensure_code(skylines != NULL, CPL_ERROR_NULL_INPUT);
419
420 unsigned int badpix_level =
422
423 const char *snrs[] = { MOO_TARGET_TABLE_MEDSNRRI_SCI,
424 MOO_TARGET_TABLE_MEDSNRYJ_SCI,
425 MOO_TARGET_TABLE_MEDSNRH_SCI };
426
427 moo_target_table *target_table = moo_sci_get_target_table(self);
428
429 cpl_table *table = target_table->table;
430 int nrow = cpl_table_get_nrow(table);
431 for (int t = 0; t < nrow; t++) {
432 int targ_idx =
433 cpl_table_get_int(table, MOO_TARGET_TABLE_INDEXTARG, t, NULL);
434 for (int i = 0; i < 3; i++) {
435 moo_sci_single *single = moo_sci_load_single(self, i, badpix_level);
436 if (single != NULL) {
437 double snr =
438 moo_sci_single_compute_snr(single, targ_idx, skylines);
439 cpl_table_set_double(table, snrs[i], t, snr);
440 }
441 }
442 }
443
444 return CPL_ERROR_NONE;
445}
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
Definition: moo_badpix.h:62
#define MOO_BADPIX_CALIB_DEFECT
Definition: moo_badpix.h:48
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
void moo_sci_single_save(moo_sci_single *self, const char *filename, moo_detector_type type)
Save a moo_sci_single to a FITS file.
void moo_sci_single_delete(moo_sci_single *self)
Delete a moo_sci_single.
cpl_error_code moo_sci_single_load(moo_sci_single *self, unsigned int level)
Load a moo_sci_single from the filename.
double moo_sci_single_compute_snr(moo_sci_single *self, int targ_idx, moo_sky_lines_list *skylines)
Compute SNR for a given target.
moo_sci_single * moo_sci_single_create(const char *filename, moo_detector_type type)
Create a new moo_sci_single with the given filename and type.
cpl_error_code moo_sci_set_single(moo_sci *self, moo_sci_single *single)
assign moo_sci_single structure in moo_sci structure
Definition: moo_sci.c:174
cpl_error_code moo_sci_add_target_table(moo_sci *self, moo_target_table *table)
Add target table to SCI file and update moo_sci structure.
Definition: moo_sci.c:132
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
void moo_sci_delete(moo_sci *self)
Delete a moo_sci.
Definition: moo_sci.c:84
moo_sci * moo_sci_new(void)
Create a new moo_sci.
Definition: moo_sci.c:66
moo_sci * moo_sci_create(const cpl_frame *frame)
Create a new empty SCI filename.
Definition: moo_sci.c:249
cpl_error_code moo_sci_add_single(moo_sci *self, moo_detector_type type, moo_sci_single *single)
Add SCI_SINGLE extension to SCI file and update moo_sci structure.
Definition: moo_sci.c:216
moo_sci_single * moo_sci_get_single(moo_sci *self, moo_detector_type type)
Get the type part in SCI and return it.
Definition: moo_sci.c:351
void moo_sci_save(moo_sci *self, const char *filename)
Save a moo_sci to a FITS file.
Definition: moo_sci.c:385
moo_target_table * moo_sci_get_target_table(moo_sci *self)
Get the target table of SCI file.
Definition: moo_sci.c:284
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_target_table_delete(moo_target_table *self)
Delete a moo_target_table.
moo_target_table * moo_target_table_new(void)
Create a new moo_target_table.
the different type of detectors