32#include "moo_badpix.h"
36#include "moo_fibres_table.h"
71 moo_ext *res = cpl_calloc(1,
sizeof(moo_ext));
88 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
90 cpl_errorstate prev_state = cpl_errorstate_get();
92 const char *filename = cpl_frame_get_filename(frame);
93 res->filename = filename;
94 res->primary_header = cpl_propertylist_load(filename, 0);
103 if (!cpl_errorstate_is_equal(prev_state)) {
104 cpl_errorstate_set(prev_state);
105 cpl_msg_error(
"moo_ext",
"Can't create EXT from file : %s", filename);
128 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
130 for (
int i = 0; i < 2; i++) {
136 return CPL_ERROR_NONE;
161 moo_ext_single *res = NULL;
163 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
164 cpl_ensure(num >= 1 && num <= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
169 res = self->ri[num - 1];
173 res = self->yj[num - 1];
177 res = self->h[num - 1];
201 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
205 return self->ri[ntas - 1];
208 return self->yj[ntas - 1];
211 return self->h[ntas - 1];
234 moo_ext_single *single)
236 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
237 cpl_ensure_code(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT);
239 cpl_error_code status = CPL_ERROR_NONE;
240 cpl_errorstate prev_state = cpl_errorstate_get();
244 self->ri[ntas - 1] = single;
247 self->yj[ntas - 1] = single;
250 self->h[ntas - 1] = single;
253 if (!cpl_errorstate_is_equal(prev_state)) {
254 cpl_msg_error(
"moo_ext",
"Error for adding ext to %s (%d)",
255 self->filename, cpl_error_get_code());
256 status = cpl_error_get_code();
257 cpl_errorstate_set(prev_state);
277 moo_ext_single *single,
281 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
282 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
283 cpl_error_code status = CPL_ERROR_NONE;
284 cpl_errorstate prev_state = cpl_errorstate_get();
287 if (!cpl_errorstate_is_equal(prev_state)) {
289 cpl_msg_error(
"moo_ext",
"Error for adding single to %s %s (%s)",
290 self->filename, extname,
291 cpl_error_get_message_default(cpl_error_get_code()));
292 status = cpl_error_get_code();
311 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
312 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
313 cpl_ensure_code(fibre_table != NULL, CPL_ERROR_NULL_INPUT);
315 cpl_error_code status = CPL_ERROR_NONE;
316 cpl_errorstate prev_state = cpl_errorstate_get();
317 self->fibre_table = fibre_table;
319 cpl_propertylist *h = cpl_propertylist_new();
320 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
321 MOO_FIBRES_TABLE_EXTNAME);
322 cpl_table_save(self->fibre_table, h, h, self->filename, CPL_IO_EXTEND);
323 cpl_propertylist_delete(h);
325 if (!cpl_errorstate_is_equal(prev_state)) {
326 cpl_msg_error(
"moo_loc",
"Error for adding fibre table to %s (%d)",
327 self->filename, cpl_error_get_code());
328 status = cpl_error_get_code();
329 cpl_errorstate_set(prev_state);
346 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
348 if (self->fibre_table == NULL && self->filename != NULL) {
350 cpl_fits_find_extension(self->filename, MOO_FIBRES_TABLE_EXTNAME);
352 self->fibre_table = cpl_table_load(self->filename, qnum, 0);
355 return self->fibre_table;
374 if (self->primary_header != NULL) {
375 cpl_propertylist_delete(self->primary_header);
377 for (i = 0; i < 2; i++) {
378 if (self->ri[i] != NULL) {
381 if (self->yj[i] != NULL) {
384 if (self->h[i] != NULL) {
388 if (self->fibre_table != NULL) {
389 cpl_table_delete(self->fibre_table);
409 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
411 for (i = 0; i < 2; i++) {
417 if (self->fibre_table != NULL) {
418 cpl_propertylist *h = cpl_propertylist_new();
419 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME,
420 MOO_FIBRES_TABLE_EXTNAME);
421 cpl_table_save(self->fibre_table, h, h, filename, CPL_IO_EXTEND);
422 cpl_propertylist_delete(h);
443 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
444 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
446 fprintf(stream,
"---MOO_EXT\n");
447 fprintf(stream,
"filename %s\n", self->filename);
449 for (i = 0; i < 2; i++) {
450 if (self->ri[i] != NULL) {
453 if (self->yj[i] != NULL) {
456 if (self->h[i] != NULL) {
460 if (self->fibre_table) {
461 fprintf(stream,
"fibre table %p\n", self->fibre_table);
464 return CPL_ERROR_NONE;
488 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
489 cpl_ensure_code(num >= 1, CPL_ERROR_ILLEGAL_INPUT);
490 cpl_ensure_code(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE);
492 cpl_error_code status = CPL_ERROR_NONE;
495 if (single != NULL) {
516 cpl_ensure_code(self != NULL, CPL_ERROR_ILLEGAL_INPUT);
517 cpl_ensure_code(ext != NULL, CPL_ERROR_ILLEGAL_INPUT);
520 for (i = 0; i < 2; i++) {
521 moo_ext_single *ri = self->ri[i];
522 moo_ext_single *extri = ext->ri[i];
526 moo_ext_single *yj = self->yj[i];
528 moo_ext_single *extyj = ext->yj[i];
531 moo_ext_single *h = self->h[i];
533 moo_ext_single *exth = ext->h[i];
537 return CPL_ERROR_NONE;
555 moo_spectral_format *sformat,
556 moo_sky_lines_list *skylines)
558 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
559 cpl_ensure_code(wmap != NULL, CPL_ERROR_NULL_INPUT);
560 cpl_ensure_code(skylines != NULL, CPL_ERROR_NULL_INPUT);
564 const char *snrs[] = { MOO_FIBRES_TABLE_MEDSNR_RI_EXT,
565 MOO_FIBRES_TABLE_MEDSNR_YJ_EXT,
566 MOO_FIBRES_TABLE_MEDSNR_H_EXT };
568 const char *dersnrs[] = { MOO_FIBRES_TABLE_DERSNR_RI_EXT,
569 MOO_FIBRES_TABLE_DERSNR_YJ_EXT,
570 MOO_FIBRES_TABLE_DERSNR_H_EXT };
572 int nrow = cpl_table_get_nrow(fibre_table);
574 for (
int t = 0; t < nrow; t++) {
576 cpl_table_get_int(fibre_table, MOO_FIBRES_TABLE_INDEXEXT, t, NULL);
578 cpl_table_get_int(fibre_table, MOO_FIBRES_TABLE_HEALTH, t, NULL);
580 cpl_table_get_int(fibre_table, MOO_FIBRES_TABLE_SPECTRO, t, NULL);
582 if (spectro == MOO_FIBRES_TABLE_SPECTRO_2) {
586 for (
int i = 0; i < 3; i++) {
587 moo_ext_single *single =
589 moo_spectral_format_info *sinfo =
591 cpl_image *smap = wmap->data[i + (num - 1) * 3];
592 if (single != NULL) {
594 double der_snr = 0.0;
596 sinfo, skylines, &der_snr);
598 moo_try_check(cpl_table_set_float(fibre_table, snrs[i], t,
601 moo_try_check(cpl_table_set_float(fibre_table, dersnrs[i],
612 return CPL_ERROR_NONE;
cpl_error_code moo_ext_free_single(moo_ext *self, moo_detector_type type, int num)
Free the given type part in EXT.
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
enum _moo_detector_type_ moo_detector_type
The type code type.
double moo_ext_single_compute_snr(moo_ext_single *self, int ext_idx, cpl_image *wmap, moo_spectral_format_info *sinfo, moo_sky_lines_list *skylines, double *dersnr)
Compute SNR for a given target.
cpl_error_code moo_ext_single_load(moo_ext_single *self, unsigned int level)
load a moo_ext_single using the level for badpixel
cpl_error_code moo_ext_single_free(moo_ext_single *self)
Free memory associate to this single EXT.
void moo_ext_single_delete(moo_ext_single *self)
Delete a moo_ext_single.
moo_ext_single * moo_ext_single_create(const char *filename, moo_detector_type type, int ntas)
Create a new moo_ext_single from the given EXT filename.
void moo_ext_single_save(const moo_ext_single *self, const char *filename, moo_detector_type type, int ntas)
Save a moo_ext_single to a FITS file.
cpl_error_code moo_ext_single_dump(const moo_ext_single *self, FILE *stream)
Dump structural information of EXT_SINGLE.
cpl_error_code moo_ext_single_sum(moo_ext_single *a, moo_ext_single *b)
Add two single EXT.
moo_ext_single * moo_ext_get_single(moo_ext *self, moo_detector_type type, int ntas)
Get a EXT single from EXT.
moo_ext * moo_ext_new(void)
Create a new moo_ext.
cpl_error_code moo_ext_add_fibre_table(moo_ext *self, cpl_table *fibre_table)
Add fibre table to EXT filename and update moo_ext structure.
cpl_error_code moo_ext_set_single(moo_ext *self, moo_detector_type type, int ntas, moo_ext_single *single)
assign moo_ext_single structure in moo_ext structure
cpl_error_code moo_ext_add_single(moo_ext *self, moo_ext_single *single, moo_detector_type type, int ntas)
Add EXT_SINGLE extension to EXT filename and update moo_ext structure.
moo_ext_single * moo_ext_load_single(moo_ext *self, moo_detector_type type, int num, unsigned int level)
Load the type part in EXT and return it.
cpl_error_code moo_ext_load(moo_ext *self, unsigned int level)
Load all parts in EXT.
moo_ext * moo_ext_create(const cpl_frame *frame)
Create a new empty EXT filename.
cpl_error_code moo_ext_sum(moo_ext *self, moo_ext *ext)
Sum EXT structure.
cpl_table * moo_ext_get_fibre_table(moo_ext *self)
Get the FIBRE TABLE in EXT.
cpl_error_code moo_ext_dump(const moo_ext *self, FILE *stream)
Dump structural information of EXT.
void moo_ext_save(moo_ext *self, const char *filename)
Save a moo_ext to a FITS file.
void moo_ext_delete(moo_ext *self)
Delete a moo_ext.
cpl_error_code moo_ext_compute_snr(moo_ext *self, moo_map *wmap, moo_spectral_format *sformat, moo_sky_lines_list *skylines)
Compute SNR for all fibres EXT.
cpl_error_code moo_qc_set_snr_range(cpl_propertylist *plist, const char *val)
Set the QC.SNR.RANGE value.