30#include "moo_sci_single.h"
33#include "moo_badpix.h"
71 moo_sci_single *res = cpl_calloc(1,
sizeof(moo_sci_single));
75 res->extname = extname;
76 res->header = cpl_propertylist_new();
95 if (self->header != NULL) {
96 cpl_propertylist_delete(self->header);
99 if (self->image != NULL) {
100 hdrl_image_delete(self->image);
102 if (self->qual != NULL) {
103 cpl_image_delete(self->qual);
105 if (self->sky != NULL) {
106 cpl_image_delete(self->sky);
125 const char *filename,
130 if (self != NULL && self->image != NULL) {
132 filename, NULL, extname,
133 MOO_SCI_SINGLE_DATA_TYPE, self->header);
135 cpl_propertylist *err_header = cpl_propertylist_new();
139 filename, MOO_SCI_SINGLE_ERR, extname,
140 MOO_SCI_SINGLE_ERR_TYPE, err_header);
141 cpl_propertylist_delete(err_header);
142 cpl_propertylist *qual_header = cpl_propertylist_new();
146 MOO_SCI_SINGLE_QUAL, extname,
147 MOO_SCI_SINGLE_QUAL_TYPE, qual_header);
148 cpl_propertylist_delete(qual_header);
151 cpl_propertylist *h = cpl_propertylist_new();
152 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME, extname);
153 cpl_propertylist_save(h, filename, CPL_IO_EXTEND);
154 cpl_propertylist_delete(h);
157 extname, MOO_SCI_SINGLE_ERR_TYPE, NULL);
159 extname, MOO_SCI_SINGLE_QUAL_TYPE, NULL);
162 if (self != NULL && self->sky != NULL) {
163 cpl_propertylist *sky_header = cpl_propertylist_new();
164 cpl_propertylist_copy_property(sky_header, self->header,
166 cpl_propertylist_copy_property(sky_header, self->header,
168 cpl_propertylist_copy_property(sky_header, self->header,
170 cpl_propertylist_copy_property(sky_header, self->header,
172 cpl_propertylist_copy_property(sky_header, self->header,
174 cpl_propertylist_copy_property(sky_header, self->header,
183 extname, CPL_TYPE_FLOAT, sky_header);
184 cpl_propertylist_delete(sky_header);
188 extname, MOO_SCI_SINGLE_ERR_TYPE, NULL);
207 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
209 moo_sci_single *single = NULL;
210 cpl_propertylist *header = NULL;
214 header = moo_fits_load_extension_header(filename, NULL, extname);
215 if (header != NULL) {
219 single->filename = filename;
220 cpl_propertylist_append(single->header, header);
222 cpl_propertylist_delete(header);
240 if ((self->filename != NULL) && (self->extname != NULL)) {
241 if (self->header == NULL) {
243 cpl_fits_find_extension(self->filename, self->extname);
246 cpl_propertylist_load(self->filename, extnum);
249 if (self->qual == NULL) {
253 self->extname, CPL_TYPE_INT);
255 if (self->image == NULL) {
256 cpl_image *data = NULL;
257 cpl_image *err = NULL;
267 if (data != NULL && err != NULL) {
268 self->image = hdrl_image_create(data, err);
269 cpl_image_delete(data);
270 cpl_image_delete(err);
276 cpl_mask *mask = hdrl_image_get_mask(self->image);
279 return CPL_ERROR_NONE;
296 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
316 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
318 if (self->header == NULL && self->filename != NULL &&
319 self->extname != NULL) {
321 moo_fits_load_extension_header(self->filename, NULL, self->extname);
322 if (self->header == NULL) {
323 self->header = cpl_propertylist_new();
344 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
346 if (self->sky == NULL && self->filename != NULL && self->extname != NULL) {
349 self->extname, CPL_TYPE_DOUBLE);
368 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
370 if (self->qual == NULL) {
371 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
372 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
376 MOO_SCI_SINGLE_QUAL_TYPE);
394 moo_sky_lines_list *skylines)
398 double *zwmin = NULL;
399 double *zwmax = NULL;
400 cpl_array *sel = NULL;
401 cpl_vector *data = NULL;
402 hdrl_image *fimg = NULL;
403 cpl_vector *row = NULL;
405 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0.0);
406 cpl_errorstate prev_state = cpl_errorstate_get();
410 int nx = hdrl_image_get_size_x(himg);
414 double wmin = (0.5 - crpix1) * cd1_1 + crval1;
415 double wmax = (nx + 0.5 - crpix1) * cd1_1 + crval1;
419 &zwmin, &zwmax, &sel),
421 int size = cpl_array_get_size(sel);
423 for (
int i = 0; i < size; i++) {
424 int idx = cpl_array_get_cplsize(sel, i, NULL);
425 double cmin = zwmin[idx];
426 double cmax = zwmax[idx];
427 int start = (cmin - crval1) / cd1_1 + crpix1;
428 int stop = (cmax - crval1) / cd1_1 + crpix1;
429 fimg = hdrl_image_extract(himg, start, targ_idx, stop, targ_idx);
430 hdrl_image_reject_value(fimg, CPL_VALUE_NAN);
431 cpl_image *img = hdrl_image_get_image(fimg);
432 cpl_image *error = hdrl_image_get_error(fimg);
433 cpl_image_reject_value(error, CPL_VALUE_NAN | CPL_VALUE_ZERO);
434 cpl_image_divide(img, error);
435 row = cpl_vector_new_from_image_row(img, 1);
437 data = cpl_vector_duplicate(row);
440 int vsize = cpl_vector_get_size(data);
441 int row_size = cpl_vector_get_size(row);
442 cpl_vector_set_size(data, vsize + row_size);
443 for (
int j = 0; j < row_size; j++) {
444 double v = cpl_vector_get(row, j);
445 cpl_vector_set(data, j + vsize, v);
448 cpl_vector_delete(row);
449 hdrl_image_delete(fimg);
453 snr = cpl_vector_get_median(data);
456 if (!cpl_errorstate_is_equal(prev_state)) {
457 cpl_vector_delete(row);
458 hdrl_image_delete(fimg);
460 cpl_msg_error(__func__,
"Can't compute snr for target %d", targ_idx);
461 cpl_errorstate_set(prev_state);
463 cpl_vector_delete(data);
464 cpl_array_delete(sel);
cpl_error_code moo_badpix_to_mask(cpl_image *badpix, cpl_mask *mask, unsigned int level)
Apply the badpix map on the given mask.
const char * moo_detector_get_name(moo_detector_type type)
Get the extension name of a detector.
enum _moo_detector_type_ moo_detector_type
The type code type.
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.
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.
moo_sci_single * moo_sci_single_new(moo_detector_type type)
Create a new moo_sci_single.
void moo_sci_single_delete(moo_sci_single *self)
Delete a moo_sci_single.
cpl_image * moo_sci_single_get_sky(moo_sci_single *self)
Get sky of sci single.
hdrl_image * moo_sci_single_get_image(moo_sci_single *self)
Get image of SCI_SINGLE.
cpl_image * moo_sci_single_get_qual(moo_sci_single *self)
Get image of qual.
cpl_error_code moo_sci_single_load(moo_sci_single *self, unsigned int level)
Load a moo_sci_single from the filename.
cpl_propertylist * moo_sci_single_get_header(moo_sci_single *self)
Get header of sci single.
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_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.
double moo_pfits_get_cd1_1(const cpl_propertylist *plist)
find out the CD1_1 value
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.
double moo_pfits_get_crval1(const cpl_propertylist *plist)
find out the CRVAL1 value
double moo_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value