MOONS Pipeline Reference Manual 0.13.2
moo_resp.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_resp.h"
32#include "moo_utils.h"
33#include "moo_pfits.h"
34#include "moo_scilist.h"
35#include "moo_sci.h"
36/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
52/*-----------------------------------------------------------------------------
53 Function codes
54 -----------------------------------------------------------------------------*/
55
56/*----------------------------------------------------------------------------*/
64/*----------------------------------------------------------------------------*/
65moo_resp *
67{
68 moo_resp *res = cpl_calloc(1, sizeof(moo_resp));
69 return res;
70}
71
72/*----------------------------------------------------------------------------*/
82/*----------------------------------------------------------------------------*/
83
84cpl_error_code
85moo_resp_set_table(moo_resp *self, moo_detector_type type, cpl_table *t)
86{
87 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
88
89 cpl_error_code status = CPL_ERROR_NONE;
90 cpl_errorstate prev_state = cpl_errorstate_get();
91
92 if (t != NULL) {
93 switch (type) {
94 case MOO_TYPE_RI:
95 self->ri = t;
96 break;
97 case MOO_TYPE_YJ:
98 self->yj = t;
99 break;
100 case MOO_TYPE_H:
101 self->h = t;
102 break;
103 }
104 }
105 if (!cpl_errorstate_is_equal(prev_state)) {
106 status = cpl_error_get_code();
107 cpl_errorstate_set(prev_state);
108 }
109 return status;
110}
111
112/*----------------------------------------------------------------------------*/
122/*----------------------------------------------------------------------------*/
123cpl_table *
125{
126 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
127
128 switch (type) {
129 case MOO_TYPE_RI:
130 return self->ri;
131 break;
132 case MOO_TYPE_YJ:
133 return self->yj;
134 break;
135 case MOO_TYPE_H:
136 return self->h;
137 break;
138 default:
139 return NULL;
140 }
141}
142
143/*----------------------------------------------------------------------------*/
156/*----------------------------------------------------------------------------*/
157moo_resp *
158moo_resp_create(moo_scilist *scilist, int badpix_level)
159{
160 moo_resp *result = NULL;
161
162 cpl_ensure(scilist != NULL, CPL_ERROR_NULL_INPUT, NULL);
163
164 int size = moo_scilist_get_size(scilist);
165
166 result = moo_resp_new();
167 result->primary_header = cpl_propertylist_new();
168 result->response_table = cpl_table_new(size);
169
170 cpl_table_new_column(result->response_table, MOO_RESP_TABLE_INDEXRBN,
171 CPL_TYPE_INT);
172 cpl_table_new_column(result->response_table, MOO_RESP_TABLE_SPECTRO,
173 CPL_TYPE_INT);
174 cpl_table_new_column(result->response_table, MOO_RESP_TABLE_RESPONSE,
175 CPL_TYPE_STRING);
176
177 for (int il = 0; il < size; il++) {
178 cpl_array *sel = NULL;
179 char *response_colname = NULL;
180 moo_sci *sci = moo_scilist_get(scilist, il);
181 moo_target_table *target_table = moo_sci_get_target_table(sci);
182 if (target_table->table != NULL) {
183 const char *targname = moo_target_table_get_std_name(target_table);
184 int idtarget =
185 moo_target_table_find_target(sci->target_table, targname);
186 cpl_table_select_all(sci->target_table->table);
187 cpl_table_and_selected_int(sci->target_table->table,
188 MOO_TARGET_TABLE_INDEXTARG, CPL_EQUAL_TO,
189 idtarget);
190 sel = cpl_table_where_selected(sci->target_table->table);
191 int idx = cpl_array_get_cplsize(sel, 0, NULL);
192 int idrbn = cpl_table_get_int(sci->target_table->table,
193 MOO_TARGET_TABLE_INDEXRBN, idx, NULL);
194 const int spectro =
195 cpl_table_get_int(sci->target_table->table,
196 MOO_TARGET_TABLE_SPECTRO, idx, NULL);
197 cpl_table_set_int(result->response_table, MOO_RESP_TABLE_INDEXRBN,
198 il, idrbn);
199 response_colname = cpl_sprintf("%s_%d", MOO_RESP_RESPONSE, il);
200 cpl_table_set_int(result->response_table, MOO_RESP_TABLE_SPECTRO,
201 il, spectro);
202 cpl_table_set_string(result->response_table,
203 MOO_RESP_TABLE_RESPONSE, il, response_colname);
204 cpl_array_delete(sel);
205 cpl_free(response_colname);
206 }
207 }
208
209 for (int i = 0; i < 3; i++) {
210 cpl_table *table = NULL;
211
212 for (int il = 0; il < size; il++) {
213 const char *response_colname =
214 cpl_table_get_string(result->response_table,
215 MOO_RESP_TABLE_RESPONSE, il);
216 moo_sci *sci = moo_scilist_get(scilist, il);
217 moo_target_table *target_table = moo_sci_get_target_table(sci);
218 if (target_table->table != NULL) {
219 const char *targname =
220 moo_target_table_get_std_name(target_table);
221 int idtarget =
222 moo_target_table_find_target(sci->target_table, targname);
223
224 moo_sci_single *sci_single =
225 moo_sci_load_single(sci, i, badpix_level);
226
227 if (sci_single != NULL) {
228 cpl_propertylist *header =
229 moo_sci_single_get_header(sci_single);
230
231 double crpix1 = moo_pfits_get_crpix1(header);
232 double crval1 = moo_pfits_get_crval1(header);
233 double cd1_1 = moo_pfits_get_cd1_1(header);
234
235 hdrl_image *himg = moo_sci_single_get_image(sci_single);
236 cpl_image *img = hdrl_image_get_image(himg);
237 int nx = cpl_image_get_size_x(img);
238
239 if (il == 0) {
240 table = cpl_table_new(nx);
241 cpl_table_new_column(table, MOO_RESP_WAVE,
242 CPL_TYPE_DOUBLE);
243 for (int j = 1; j <= nx; j++) {
244 double w = (j - crpix1) * cd1_1 + crval1;
245 cpl_table_set_double(table, MOO_RESP_WAVE, j - 1,
246 w);
247 }
248 }
249 cpl_table_new_column(table, response_colname,
250 CPL_TYPE_DOUBLE);
251#if MOO_DEBUG_RESP
252 {
253 char *flux_colname =
254 cpl_sprintf("%s_%s", response_colname,
255 MOO_RESP_SPECFLUX);
256 char *fluxfilter_colname =
257 cpl_sprintf("%s_%s", response_colname,
258 MOO_RESP_SPECFLUXFILTER);
259 char *flx_colname =
260 cpl_sprintf("%s_%s", response_colname,
261 MOO_RESP_FLX);
262 char *atm_colname =
263 cpl_sprintf("%s_%s", response_colname,
264 MOO_RESP_ATM);
265
266
267 cpl_table_new_column(table, flux_colname,
268 CPL_TYPE_DOUBLE);
269 cpl_table_new_column(table, fluxfilter_colname,
270 CPL_TYPE_DOUBLE);
271 cpl_table_new_column(table, flx_colname,
272 CPL_TYPE_DOUBLE);
273 cpl_table_new_column(table, atm_colname,
274 CPL_TYPE_DOUBLE);
275
276 cpl_free(flux_colname);
277 cpl_free(fluxfilter_colname);
278 cpl_free(flx_colname);
279 cpl_free(atm_colname);
280 }
281#endif
282 for (int j = 1; j <= nx; j++) {
283 int rej;
284 double f = cpl_image_get(img, j, idtarget, &rej);
285 if (rej != 0) {
286 f = NAN;
287 }
288
289 cpl_table_set_double(table, response_colname, j - 1, f);
290#if MOO_DEBUG_RESP
291 {
292 char *flux_colname =
293 cpl_sprintf("%s_%s", response_colname,
294 MOO_RESP_SPECFLUX);
295 cpl_table_set_double(table, flux_colname, j - 1, f);
296 cpl_free(flux_colname);
297 }
298#endif
299 }
300 }
301 }
302 }
303 moo_resp_set_table(result, i, table);
304 }
305 return result;
306}
307
308/*----------------------------------------------------------------------------*/
317/*----------------------------------------------------------------------------*/
318
319void
320moo_resp_delete(moo_resp *self)
321{
322 if (self != NULL) {
323 if (self->primary_header != NULL) {
324 cpl_propertylist_delete(self->primary_header);
325 }
326 if (self->ri != NULL) {
327 cpl_table_delete(self->ri);
328 }
329 if (self->yj != NULL) {
330 cpl_table_delete(self->yj);
331 }
332 if (self->h != NULL) {
333 cpl_table_delete(self->h);
334 }
335 if (self->response_table != NULL) {
336 cpl_table_delete(self->response_table);
337 }
338 cpl_free(self);
339 }
340}
341
342/*----------------------------------------------------------------------------*/
352/*----------------------------------------------------------------------------*/
353void
354moo_resp_save(moo_resp *self, const char *filename)
355{
356 if (self != NULL) {
357 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
358 if (self->ri != NULL) {
359 cpl_propertylist *h = cpl_propertylist_new();
360 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
362 cpl_table_save(self->ri, h, h, filename, CPL_IO_EXTEND);
363 cpl_propertylist_delete(h);
364 }
365 if (self->yj != NULL) {
366 cpl_propertylist *h = cpl_propertylist_new();
367 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
368 MOO_RESP_EXT_YJ);
369 cpl_table_save(self->yj, h, h, filename, CPL_IO_EXTEND);
370 cpl_propertylist_delete(h);
371 }
372 if (self->h != NULL) {
373 cpl_propertylist *h = cpl_propertylist_new();
374 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
375 MOO_RESP_EXT_H);
376 cpl_table_save(self->h, h, h, filename, CPL_IO_EXTEND);
377 cpl_propertylist_delete(h);
378 }
379
380 if (self->response_table != NULL) {
381 cpl_propertylist *h = cpl_propertylist_new();
382 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
383 MOO_RESP_EXT_RESPONSE_TABLE);
384 cpl_table_save(self->response_table, h, h, filename, CPL_IO_EXTEND);
385 cpl_propertylist_delete(h);
386 }
387 }
388}
389
390/*----------------------------------------------------------------------------*/
399/*----------------------------------------------------------------------------*/
400moo_resp *
401moo_resp_load(const cpl_frame *frame)
402{
403 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
404
405 const char *filename = cpl_frame_get_filename(frame);
406 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
407 cpl_errorstate prev_state = cpl_errorstate_get();
408
409 moo_resp *res = moo_resp_new();
410
411 res->primary_header = cpl_propertylist_load(filename, 0);
412
413 cpl_size qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_RI);
414 if (qnum > 0) {
415 res->ri = cpl_table_load(filename, qnum, 0);
416 }
417 qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_YJ);
418 if (qnum > 0) {
419 res->yj = cpl_table_load(filename, qnum, 0);
420 }
421
422 qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_H);
423 if (qnum > 0) {
424 res->h = cpl_table_load(filename, qnum, 0);
425 }
426
427 qnum = cpl_fits_find_extension(filename, MOO_RESP_EXT_RESPONSE_TABLE);
428 if (qnum > 0) {
429 res->response_table = cpl_table_load(filename, qnum, 0);
430 }
431
432 if (!cpl_errorstate_is_equal(prev_state)) {
433 cpl_msg_debug(__func__, "Error in loading response : %d",
434 cpl_error_get_code());
435 moo_resp_delete(res);
436 res = NULL;
437 cpl_errorstate_set(prev_state);
438 }
439 return res;
440}
441
442/*----------------------------------------------------------------------------*/
451/*----------------------------------------------------------------------------*/
452
453const char *
454moo_resp_get_colname(moo_resp *self, int idrbn, int fspectro)
455{
456 const char *colname = NULL;
457 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
458 cpl_ensure(self->response_table != NULL, CPL_ERROR_NULL_INPUT, NULL);
459 int nrow = cpl_table_get_nrow(self->response_table);
460 int min_diff = 1024;
461
462 for (int i = 0; i < nrow; i++) {
463 int n = cpl_table_get_int(self->response_table, MOO_RESP_TABLE_INDEXRBN,
464 i, NULL);
465 const char *ncolname = cpl_table_get_string(self->response_table,
466 MOO_RESP_TABLE_RESPONSE, i);
467 const int spectro = cpl_table_get_int(self->response_table,
468 MOO_RESP_TABLE_SPECTRO, i, NULL);
469 if (spectro == fspectro) {
470 int diff = (int)fabs(idrbn - n);
471
472 if (diff == 0) {
473 colname = ncolname;
474 break;
475 }
476 else if (min_diff > diff) {
477 colname = ncolname;
478 min_diff = diff;
479 }
480 }
481 }
482
483 return colname;
484}
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
#define MOO_RESP_EXT_RI
RESPONSE format.
Definition: moo_resp.h:35
void moo_resp_delete(moo_resp *self)
Delete a moo_resp.
Definition: moo_resp.c:320
cpl_error_code moo_resp_set_table(moo_resp *self, moo_detector_type type, cpl_table *t)
assign table in moo_resp
Definition: moo_resp.c:85
void moo_resp_save(moo_resp *self, const char *filename)
Save a moo_resp to a FITS file.
Definition: moo_resp.c:354
cpl_table * moo_resp_get_table(moo_resp *self, moo_detector_type type)
Get a reponse table from RESP.
Definition: moo_resp.c:124
moo_resp * moo_resp_new(void)
Create a new moo_resp.
Definition: moo_resp.c:66
moo_resp * moo_resp_create(moo_scilist *scilist, int badpix_level)
Prepare RESP for SCI frame.
Definition: moo_resp.c:158
moo_resp * moo_resp_load(const cpl_frame *frame)
Load a RESP frame and create a moo_resp.
Definition: moo_resp.c:401
const char * moo_resp_get_colname(moo_resp *self, int idrbn, int fspectro)
Get the name of the response colname for a given indexrbn.
Definition: moo_resp.c:454
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
double moo_pfits_get_cd1_1(const cpl_propertylist *plist)
find out the CD1_1 value
Definition: moo_pfits.c:1256
double moo_pfits_get_crval1(const cpl_propertylist *plist)
find out the CRVAL1 value
Definition: moo_pfits.c:1176
double moo_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
Definition: moo_pfits.c:1196
the different type of detectors