30#include "moo_ext_single.h"
33#include "moo_badpix.h"
34#include "moo_spectral_format.h"
35#include "moo_fibres_table.h"
73 moo_ext_single *res = cpl_calloc(1,
sizeof(moo_ext_single));
78 res->extname = extname;
79 res->header = cpl_propertylist_new();
99 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
101 moo_ext_single *single = NULL;
102 cpl_propertylist *header = NULL;
106 header = moo_fits_load_extension_header(filename, NULL, extname);
107 if (header != NULL) {
111 single->filename = filename;
112 cpl_propertylist_append(single->header, header);
114 cpl_propertylist_delete(header);
135 if ((self->filename != NULL) && (self->extname != NULL)) {
136 if (self->header == NULL) {
138 cpl_fits_find_extension(self->filename, self->extname);
141 cpl_propertylist_load(self->filename, extnum);
144 if (self->qual == NULL) {
148 self->extname, CPL_TYPE_INT);
150 if (self->image == NULL) {
151 cpl_image *data = NULL;
152 cpl_image *err = NULL;
162 if (data != NULL && err != NULL) {
163 self->image = hdrl_image_create(data, err);
164 cpl_image_delete(data);
165 cpl_image_delete(err);
170 cpl_mask *mask = hdrl_image_get_mask(self->image);
173 return CPL_ERROR_NONE;
192 if (self->header != NULL) {
193 cpl_propertylist_delete(self->header);
195 if (self->image != NULL) {
196 hdrl_image_delete(self->image);
198 if (self->qual != NULL) {
199 cpl_image_delete(self->qual);
220 const char *filename,
226 if (self->image != NULL) {
228 filename, NULL, extname,
229 CPL_TYPE_FLOAT, self->header);
231 cpl_propertylist *err_header = cpl_propertylist_new();
236 filename, MOO_EXT_SINGLE_ERR,
237 extname, MOO_EXT_SINGLE_ERR_TYPE,
240 cpl_propertylist_delete(err_header);
241 cpl_propertylist *qual_header = cpl_propertylist_new();
243 self->header, self->badpix_mask);
245 MOO_EXT_SINGLE_QUAL, extname,
246 MOO_EXT_SINGLE_QUAL_TYPE,
248 cpl_propertylist_delete(qual_header);
252 cpl_propertylist *h = cpl_propertylist_new();
253 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME, extname);
254 cpl_propertylist_save(h, filename, CPL_IO_EXTEND);
255 cpl_propertylist_delete(h);
258 extname, MOO_EXT_SINGLE_ERR_TYPE, NULL);
260 extname, MOO_EXT_SINGLE_QUAL_TYPE, NULL);
280 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
281 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
283 fprintf(stream,
"---MOO_EXT_SINGLE\n");
284 fprintf(stream,
"filename %s extname %s\n", self->filename, self->extname);
286 return CPL_ERROR_NONE;
303 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
321 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
323 cpl_image *res = NULL;
325 if (self->image != NULL) {
326 res = hdrl_image_get_image(self->image);
346 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
348 cpl_image *res = NULL;
350 if (self->image != NULL) {
351 res = hdrl_image_get_error(self->image);
370 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
371 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
372 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
374 if (self->qual == NULL) {
378 MOO_EXT_SINGLE_QUAL_TYPE);
398 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
399 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
400 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
402 if (self->header == NULL) {
404 moo_fits_load_extension_header(self->filename, NULL, self->extname);
405 if (self->header == NULL) {
406 self->header = cpl_propertylist_new();
413moo_ext_single_compute_qc(moo_ext_single *self,
415 const char *dersnr_colname,
416 cpl_table *fibres_table)
418 cpl_array *index = NULL;
419 cpl_image *snr = NULL;
420 cpl_error_code status = CPL_ERROR_NONE;
422 cpl_errorstate prestate = cpl_errorstate_get();
426 cpl_mask *mask = hdrl_image_get_mask(image);
428 cpl_ensure_code(image != NULL, CPL_ERROR_NULL_INPUT);
434 cpl_image *data = hdrl_image_get_image(image);
435 cpl_image *err = hdrl_image_get_error(image);
437 int nx = cpl_image_get_size_x(data);
438 double min = cpl_image_get_min(data);
439 double max = cpl_image_get_max(data);
440 double rms = cpl_image_get_stdev(data);
441 double median = cpl_image_get_median(data);
442 double mean = cpl_image_get_mean(data);
451 cpl_image_reject_value(err, CPL_VALUE_ZERO);
452 int nbrej = cpl_image_count_rejected(err);
453 int allpix = cpl_image_get_size_x(err) * cpl_image_get_size_y(err);
454 if (nbrej < allpix) {
455 moo_try_check(snr = cpl_image_divide_create(data, err),
" ");
456 double max_snr = cpl_image_get_max(snr);
457 double min_snr = cpl_image_get_min(snr);
458 double median_snr = cpl_image_get_median(snr);
464 moo_try_check(index = cpl_table_where_selected(fibres_table),
" ");
465 int size = cpl_array_get_size(index);
467 for (
int i = 0; i < size; i++) {
468 int idx = cpl_array_get_cplsize(index, i, NULL);
469 int h = cpl_table_get(fibres_table, MOO_FIBRES_TABLE_HEALTH,
474 cpl_errorstate fstate = cpl_errorstate_get();
475 val = cpl_image_get_median_window(snr, 1, i + 1, nx, i + 1);
476 if (cpl_errorstate_is_equal(fstate)) {
477 cpl_table_set(fibres_table, colname, idx, val);
480 cpl_table_set(fibres_table, colname, idx, NAN);
481 cpl_errorstate_set(fstate);
484 cpl_vector_new_from_image_row(data, i + 1);
487 cpl_table_set(fibres_table, dersnr_colname, idx, dersnr);
488 cpl_vector_delete(row_filter);
489 cpl_vector_delete(row);
496 cpl_array_delete(index);
497 cpl_image_delete(snr);
499 if (!cpl_errorstate_is_equal(prestate)) {
500 status = cpl_error_get_code();
501 cpl_msg_error(
"moo_ext_single",
"Can't compute QC for file %s",
531 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
532 cpl_ensure_code(self->header != NULL, CPL_ERROR_ILLEGAL_INPUT);
534 cpl_error_code code = CPL_ERROR_NONE;
536 cpl_propertylist_append_double(self->header, MOO_PFITS_CRPIX1, crpix1);
537 cpl_ensure_code(code == CPL_ERROR_NONE, code);
539 cpl_propertylist_append_double(self->header, MOO_PFITS_CRVAL1, crval1);
540 cpl_ensure_code(code == CPL_ERROR_NONE, code);
541 code = cpl_propertylist_append_double(self->header, MOO_PFITS_CD1_1, cd1_1);
542 cpl_ensure_code(code == CPL_ERROR_NONE, code);
544 cpl_propertylist_append_string(self->header, MOO_PFITS_CTYPE1, ctype1);
545 cpl_ensure_code(code == CPL_ERROR_NONE, code);
547 cpl_propertylist_append_string(self->header, MOO_PFITS_CUNIT1, cunit1);
548 cpl_ensure_code(code == CPL_ERROR_NONE, code);
566 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
568 if (self->filename != NULL && self->extname != NULL) {
569 if (self->header != NULL) {
570 cpl_propertylist_delete(self->header);
573 if (self->image != NULL) {
574 hdrl_image_delete(self->image);
577 if (self->qual != NULL) {
578 cpl_image_delete(self->qual);
582 return CPL_ERROR_NONE;
599 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
600 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
605 hdrl_image_add_image(a_img, b_img);
610 cpl_image_or(a->qual, qual1, qual2);
612 return CPL_ERROR_NONE;
632 moo_spectral_format_info *sinfo,
633 moo_sky_lines_list *skylines,
637 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0.0);
638 cpl_ensure(wmap != NULL, CPL_ERROR_NULL_INPUT, 0.0);
639 cpl_ensure(sinfo != NULL, CPL_ERROR_NULL_INPUT, 0.0);
642 int nx = cpl_image_get_size_x(wmap);
645 int direction = sinfo->direction;
647 double wmin = cpl_image_get(wmap, start, ext_idx, &rej);
649 while (isnan(wmin)) {
651 wmin = cpl_image_get(wmap, start, ext_idx, &rej);
656 double wmax = cpl_image_get(wmap, stop, ext_idx, &rej);
657 while (isnan(wmax)) {
659 wmax = cpl_image_get(wmap, stop, ext_idx, &rej);
670 double *zwmin = NULL;
671 double *zwmax = NULL;
672 cpl_array *sel = NULL;
676 int size = cpl_array_get_size(sel);
677 cpl_vector *data = NULL;
678 cpl_vector *data_dersnr = NULL;
679 for (
int i = 0; i < size; i++) {
680 int idx = cpl_array_get_cplsize(sel, i, NULL);
681 double cmin = zwmin[idx];
682 double cmax = zwmax[idx];
685 double zmin = cpl_image_get(wmap, zstart, ext_idx, &rej);
686 while (zmin < cmin) {
688 zmin = cpl_image_get(wmap, zstart, ext_idx, &rej);
692 double zmax = cpl_image_get(wmap, zstop, ext_idx, &rej);
694 while (zmax < cmax && zstop < stop) {
696 zmax = cpl_image_get(wmap, zstop, ext_idx, &rej);
704 hdrl_image_extract(himg, zstart, ext_idx, zstop, ext_idx);
705 hdrl_image_reject_value(fimg, CPL_VALUE_NAN);
706 cpl_image *img = hdrl_image_get_image(fimg);
707 cpl_image *error = hdrl_image_get_error(fimg);
708 cpl_image_reject_value(error, CPL_VALUE_NAN | CPL_VALUE_ZERO);
709 int enx = cpl_image_get_size_x(error);
710 int enbrej = cpl_image_count_rejected(error);
712 cpl_vector *imrow = cpl_vector_new_from_image_row(img, 1);
714 if (data_dersnr == NULL) {
715 data_dersnr = cpl_vector_duplicate(imrow);
718 int vsize = cpl_vector_get_size(data_dersnr);
719 int row_size = cpl_vector_get_size(imrow);
720 cpl_vector_set_size(data_dersnr, vsize + row_size);
721 for (
int j = 0; j < row_size; j++) {
722 double v = cpl_vector_get(imrow, j);
723 cpl_vector_set(data_dersnr, j + vsize, v);
726 cpl_image_divide(img, error);
728 cpl_vector *row = cpl_vector_new_from_image_row(img, 1);
730 data = cpl_vector_duplicate(row);
733 int vsize = cpl_vector_get_size(data);
734 int row_size = cpl_vector_get_size(row);
735 cpl_vector_set_size(data, vsize + row_size);
736 for (
int j = 0; j < row_size; j++) {
737 double v = cpl_vector_get(row, j);
738 cpl_vector_set(data, j + vsize, v);
741 cpl_vector_delete(row);
742 cpl_vector_delete(imrow);
744 hdrl_image_delete(fimg);
746 if (data_dersnr != NULL) {
748 snr = cpl_vector_get_median(data);
755 cpl_vector_delete(data);
756 cpl_vector_delete(data_dersnr);
757 cpl_array_delete(sel);
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
#define MOO_BADPIX_CALIB_DEFECT
cpl_error_code moo_badpix_to_mask(cpl_image *badpix, cpl_mask *mask, unsigned int level)
Apply the badpix map on the given mask.
#define MOO_BADPIX_COSMETIC
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.
moo_ext_single * moo_ext_single_new(moo_detector_type type, int ntas)
Create a new moo_ext_single.
hdrl_image * moo_ext_single_get_image(moo_ext_single *self)
Get image of EXT_SINGLE.
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.
cpl_image * moo_ext_single_get_qual(moo_ext_single *self)
Get image of qual.
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_set_wcs1(moo_ext_single *self, double crpix1, double crval1, double cd1_1, const char *ctype1, const char *cunit1)
Set the WCS1 of the extension.
cpl_image * moo_ext_single_get_errs(moo_ext_single *self)
Get image of errs.
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.
cpl_propertylist * moo_ext_single_get_header(moo_ext_single *self)
Get header of ext single.
cpl_image * moo_ext_single_get_data(moo_ext_single *self)
Get image of data.
cpl_error_code moo_fits_write_extension_image(cpl_image *image, const char *filename, const char *name, const char *detectorname, cpl_type type, cpl_propertylist *header)
Write an image as extension in FITS file.
cpl_image * moo_fits_load_extension_image(const char *filename, const char *name, const char *detectorname, cpl_type type)
Load an image from FITS file.
cpl_error_code moo_sky_lines_list_get_free_zones(moo_sky_lines_list *self, double wmin, double wmax, double **zwmin, double **zwmax, cpl_array **sel)
Get free zones for a specific wave range.
cpl_error_code moo_pfits_append_hduclass_quality(cpl_propertylist *plist, moo_detector_type type, int ntas, const cpl_propertylist *sci_header, int mask)
Set the HDUCLASS QUALITY Keyword.
cpl_error_code moo_pfits_append_hduclass_error(cpl_propertylist *plist, moo_detector_type type, int ntas, const cpl_propertylist *sci_header)
Set the HDUCLASS ERROR Keyword.
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
cpl_error_code moo_qc_set_mflat_max(cpl_propertylist *plist, double val)
Set the QC.MFLAT.MAX value.
cpl_error_code moo_qc_set_mflat_min(cpl_propertylist *plist, double val)
Set the QC.MFLAT.MIN value.
cpl_error_code moo_qc_set_mflat_sn_max(cpl_propertylist *plist, double val)
Set the QC.MFLAT.SN.MAX value.
cpl_error_code moo_qc_set_mflat_sn_min(cpl_propertylist *plist, double val)
Set the QC.MFLAT.SN.MIN value.
cpl_error_code moo_qc_set_mflat_rms(cpl_propertylist *plist, double val)
Set the QC.MFLAT.RMS value.
cpl_error_code moo_qc_set_mflat_sn_med(cpl_propertylist *plist, double val)
Set the QC.MFLAT.SN.MED value.
cpl_error_code moo_qc_set_mflat_med(cpl_propertylist *plist, double val)
Set the QC.MFLAT.MED value.
cpl_error_code moo_qc_set_mflat_avg(cpl_propertylist *plist, double val)
Set the QC.MFLAT.AVG value.
cpl_vector * moo_vector_filter_nan(cpl_vector *v)
Create new vector with nan values filter.
double moo_vector_get_dersnr(const cpl_vector *ve)
This function computes the signal to noise ratio DER_SNR following the definition set forth by the Sp...