31#include "moo_badpix.h"
32#include "moo_single.h"
75 cpl_ensure(ntas <= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
77 moo_single *res = (moo_single *)cpl_calloc(1,
sizeof(moo_single));
82 res->extname = extname;
83 res->header = cpl_propertylist_new();
107 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
109 const char *extname = NULL;
110 moo_single *single = NULL;
111 cpl_size ext_num = -1;
113 moo_try_check(ext_num = cpl_fits_find_extension(filename, extname),
" ");
116 cpl_propertylist *header = cpl_propertylist_load(filename, ext_num);
119 cpl_propertylist_erase_regexp(header,
"CONTINUE*", 0);
120 if (!cpl_propertylist_has(header, MOO_PFITS_CUNIT1)) {
121 cpl_propertylist_append_string(header, MOO_PFITS_CUNIT1,
127 single->filename = filename;
128 cpl_propertylist_append(single->header, header);
130 cpl_propertylist_delete(header);
154 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
156 self->badpix_mask = level;
158 if ((self->filename != NULL) && (self->extname != NULL)) {
159 if (self->header == NULL) {
161 cpl_fits_find_extension(self->filename, self->extname);
163 self->header = cpl_propertylist_load(self->filename, extnum);
166 if (self->qual == NULL) {
167 const char *extname =
169 cpl_size extnum = cpl_fits_find_extension(self->filename, extname);
172 cpl_image_load(self->filename, CPL_TYPE_INT, 0, extnum);
175 if (self->image == NULL) {
176 cpl_image *data = NULL;
177 cpl_image *err = NULL;
181 self->extname, CPL_TYPE_DOUBLE);
183 self->extname, CPL_TYPE_DOUBLE);
185 if (data != NULL && err != NULL) {
186 self->image = hdrl_image_create(data, err);
187 cpl_image_delete(data);
188 cpl_image_delete(err);
192 if (self->image != NULL && self->qual != NULL) {
193 cpl_mask *mask = hdrl_image_get_mask(self->image);
197 return CPL_ERROR_NONE;
214 if (self->header != NULL) {
215 cpl_propertylist_delete(self->header);
217 if (self->image != NULL) {
218 hdrl_image_delete(self->image);
220 if (self->qual != NULL) {
221 cpl_image_delete(self->qual);
241 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
242 cpl_image *res = NULL;
244 if (self->image != NULL) {
245 res = cpl_image_duplicate(hdrl_image_get_image(self->image));
264 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
289 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
290 cpl_ensure_code(mask != NULL, CPL_ERROR_NULL_INPUT);
295 cpl_mask *hmask = hdrl_image_get_mask(self->image);
296 cpl_mask_or(hmask, mask);
298 return CPL_ERROR_NONE;
314 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
332 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
351 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
353 if (self->filename != NULL && self->extname != NULL) {
354 if (self->header != NULL) {
355 cpl_propertylist_delete(self->header);
358 if (self->image != NULL) {
359 hdrl_image_delete(self->image);
362 if (self->qual != NULL) {
363 cpl_image_delete(self->qual);
367 return CPL_ERROR_NONE;
385 const char *filename,
392 if (self->image != NULL) {
393 if (self->header == NULL) {
394 self->header = cpl_propertylist_new();
395 cpl_propertylist_append_string(self->header, MOO_PFITS_EXTNAME,
397 cpl_propertylist_append_double(self->header, MOO_PFITS_EXPTIME,
400 else if (cpl_propertylist_has(self->header, MOO_PFITS_EXTNAME) ==
402 cpl_propertylist_append_string(self->header, MOO_PFITS_EXTNAME,
405 cpl_image_save(hdrl_image_get_image(self->image), filename,
406 CPL_TYPE_FLOAT, self->header, CPL_IO_EXTEND);
408 cpl_propertylist *err_header = cpl_propertylist_new();
413 filename, MOO_ERR, extname,
414 CPL_TYPE_FLOAT, err_header);
415 cpl_propertylist_delete(err_header);
418 cpl_propertylist *h = cpl_propertylist_new();
419 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME, extname);
420 cpl_propertylist_save(h, filename, CPL_IO_EXTEND);
421 cpl_propertylist_delete(h);
423 CPL_TYPE_FLOAT, NULL);
425 cpl_propertylist *qual_header = cpl_propertylist_new();
429 CPL_TYPE_INT, qual_header);
430 cpl_propertylist_delete(qual_header);
433 cpl_propertylist *h = cpl_propertylist_new();
434 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME, extname);
435 cpl_propertylist_save(h, filename, CPL_IO_EXTEND);
436 cpl_propertylist_delete(h);
439 CPL_TYPE_FLOAT, NULL);
462 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
465 fprintf(stream,
"---MOO_SINGLE\n");
466 fprintf(stream,
"extname %s\n", self->extname);
467 fprintf(stream,
"filename %s\n", self->filename);
468 fprintf(stream,
"image: ");
469 if (self->image != NULL) {
470 cpl_image_dump_structure(hdrl_image_get_image(self->image), stream);
473 fprintf(stream,
"null\n");
475 fprintf(stream,
"header %p\n", (
void *)self->header);
476 fprintf(stream,
"qual: ");
477 if (self->qual != NULL) {
478 cpl_image_dump_structure(self->qual, stream);
481 fprintf(stream,
"null\n");
483 fprintf(stream,
"mask %p\n", (
void *)self->mask);
486 return CPL_ERROR_NONE;
504 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
505 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
510 hdrl_image_sub_image(a_img, b_img);
514 cpl_image_or(a->qual, qual1, qual2);
516 return CPL_ERROR_NONE;
534 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
535 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
540 hdrl_image_div_image(a_img, b_img);
544 cpl_image_or(a->qual, qual1, qual2);
546 return CPL_ERROR_NONE;
564 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
570 hdrl_image_mul_scalar(a_img, v);
572 return CPL_ERROR_NONE;
590 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
591 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
596 hdrl_image_add_image(a_img, b_img);
601 cpl_image_or(a->qual, qual1, qual2);
603 return CPL_ERROR_NONE;
620 cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
621 cpl_ensure_code(b != NULL, CPL_ERROR_NULL_INPUT);
626 hdrl_image_add_image(a_img, b_img);
627 cpl_image *img = hdrl_image_get_image(a_img);
628 cpl_image *err = hdrl_image_get_error(a_img);
629 cpl_image_divide_scalar(img, 2.);
630 cpl_image_divide_scalar(err, 2.);
634 cpl_image_or(a->qual, qual1, qual2);
636 return CPL_ERROR_NONE;
654 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
657 cpl_mask *img_mask = hdrl_image_get_mask(img);
659 cpl_image *im = hdrl_image_get_image(img);
660 cpl_image *err = hdrl_image_get_error(img);
661 cpl_image *snr = cpl_image_divide_create(im, err);
663 int nx = cpl_image_get_size_x(snr);
664 int ny = cpl_image_get_size_y(snr);
665 cpl_mask *mask = cpl_mask_new(nx, ny);
667 for (
int j = 1; j <= ny; j++) {
668 for (
int i = 1; i <= nx; i++) {
670 double val = cpl_image_get(snr, i, j, &rej);
671 if (fabs(val) >= DBL_EPSILON && val < min_snr) {
672 cpl_mask_set(img_mask, i, j, CPL_BINARY_1);
673 cpl_mask_set(mask, i, j, CPL_BINARY_1);
679 cpl_mask_delete(mask);
680 cpl_image_delete(snr);
681 return CPL_ERROR_NONE;
686moo_single_add_create(moo_single *a, moo_single *b)
688 cpl_ensure(a != NULL, CPL_ERROR_NULL_INPUT, NULL);
689 cpl_ensure(b != NULL, CPL_ERROR_NULL_INPUT, NULL);
692 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CRPIX1);
693 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CRPIX2);
694 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CRVAL1);
695 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CRVAL2);
696 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CD1_1);
697 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CD1_2);
698 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CD2_1);
699 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CD2_2);
700 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CTYPE1);
701 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CUNIT1);
702 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_CTYPE2);
703 cpl_propertylist_copy_property(result->header, a->header, MOO_PFITS_BUNIT);
714moo_single_get_exptime(moo_single *self)
716 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
722moo_single_get_header(moo_single *self)
724 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
725 if (self->header == NULL) {
726 if (self->extname != NULL && self->filename != NULL) {
727 self->header = moo_fits_load_extension_header(self->filename,
"",
730 if (self->header == NULL) {
731 self->header = cpl_propertylist_new();
738moo_single_set_exptime(moo_single *self,
double val)
740 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
741 cpl_ensure_code(self->header != NULL, CPL_ERROR_NULL_INPUT);
cpl_error_code moo_mask_to_badpix(cpl_image *badpix, cpl_mask *mask, unsigned int level)
Add the mask of the badpix level to the badpix map.
#define MOO_BADPIX_LOW_QE
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_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.
const char * moo_detector_get_qual_extname(moo_detector_type type, int ntas)
Get the QUAL extension name of a detector.
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.
hdrl_image * moo_single_get_image(moo_single *self)
Get the IMAGE part (DATA,ERR) of single DET.
cpl_error_code moo_single_load(moo_single *self, unsigned int level)
Load data in a single structure.
cpl_error_code moo_single_mean(moo_single *a, moo_single *b)
Mean of two single DET.
cpl_image * moo_single_get_data(moo_single *self)
Get the DATA part of single DET.
cpl_error_code moo_single_sub(moo_single *a, moo_single *b)
Subtract two single DET.
cpl_error_code moo_single_apply_mask(moo_single *self, cpl_mask *mask, unsigned int level)
Apply the given mask on the QUAL part.
cpl_image * moo_single_get_err(moo_single *self)
Get the ERROR part of single DET.
void moo_single_delete(moo_single *self)
Delete a moo_single.
moo_single * moo_single_create(const char *filename, moo_detector_type type, int ntas)
Create a new moo_single from the given filename.
cpl_error_code moo_single_dump(const moo_single *self, FILE *stream)
Dump structural information of a Single DET.
cpl_error_code moo_single_free(moo_single *self)
Free memory associate to this single DET.
cpl_error_code moo_single_sum(moo_single *a, moo_single *b)
Add two single DET.
moo_single * moo_single_new(moo_detector_type type, int ntas)
Create a new moo_single.
cpl_image * moo_single_get_qual(moo_single *self)
Get the QUAL part of single DET.
cpl_error_code moo_single_divide(moo_single *a, moo_single *b)
Divide two single DET.
cpl_error_code moo_single_multiply_scalar(moo_single *a, double scalar)
Elementwise multiplication of a single with a scalar.
void moo_single_save(moo_single *self, const char *filename, moo_detector_type type, int ntas)
Save a moo_single to a FITS file.
cpl_error_code moo_single_filter_snr(moo_single *self, double min_snr)
Flag low snr pixels from single DET structure.
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_update_exptime(cpl_propertylist *plist, double value)
Set the EXPTIME value.
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_exptime(const cpl_propertylist *plist)
find out the EXPTIME value
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value