30#include "moo_rbn_single.h"
33#include "moo_badpix.h"
34#include "moo_fibres_table.h"
71 moo_rbn_single *res = cpl_calloc(1,
sizeof(moo_rbn_single));
75 res->extname = extname;
76 res->header = cpl_propertylist_new();
95 if (self->header != NULL) {
96 cpl_propertylist_delete(self->header);
98 if (self->image != NULL) {
99 hdrl_image_delete(self->image);
101 if (self->qual != NULL) {
102 cpl_image_delete(self->qual);
123 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
125 moo_rbn_single *single = NULL;
126 cpl_propertylist *header = NULL;
130 header = moo_fits_load_extension_header(filename, NULL, extname);
131 if (header != NULL) {
135 single->filename = filename;
136 cpl_propertylist_append(single->header, header);
138 cpl_propertylist_delete(header);
155 const char *filename,
160 if (self->image != NULL) {
162 filename, NULL, extname,
163 CPL_TYPE_FLOAT, self->header);
165 cpl_propertylist *err_header = cpl_propertylist_new();
169 filename, MOO_RBN_SINGLE_ERR,
170 extname, MOO_RBN_SINGLE_ERR_TYPE,
172 cpl_propertylist_delete(err_header);
173 cpl_propertylist *qual_header = cpl_propertylist_new();
175 self->header, self->badpix_mask);
177 MOO_RBN_SINGLE_QUAL, extname,
178 MOO_RBN_SINGLE_QUAL_TYPE,
180 cpl_propertylist_delete(qual_header);
184 cpl_propertylist *h = cpl_propertylist_new();
185 cpl_propertylist_append_string(h, MOO_PFITS_EXTNAME, extname);
186 cpl_propertylist_save(h, filename, CPL_IO_EXTEND);
187 cpl_propertylist_delete(h);
190 extname, MOO_RBN_SINGLE_ERR_TYPE, NULL);
192 extname, MOO_RBN_SINGLE_QUAL_TYPE, NULL);
221 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
222 cpl_ensure_code(self->header != NULL, CPL_ERROR_ILLEGAL_INPUT);
224 cpl_error_code code = CPL_ERROR_NONE;
226 cpl_propertylist_append_double(self->header, MOO_PFITS_CRPIX1, crpix1);
227 cpl_ensure_code(code == CPL_ERROR_NONE, code);
229 cpl_propertylist_append_double(self->header, MOO_PFITS_CRVAL1, crval1);
230 cpl_ensure_code(code == CPL_ERROR_NONE, code);
231 code = cpl_propertylist_append_double(self->header, MOO_PFITS_CD1_1, cd1_1);
232 cpl_ensure_code(code == CPL_ERROR_NONE, code);
234 cpl_propertylist_append_string(self->header, MOO_PFITS_CTYPE1, ctype1);
235 cpl_ensure_code(code == CPL_ERROR_NONE, code);
237 cpl_propertylist_append_string(self->header, MOO_PFITS_CUNIT1, cunit1);
238 cpl_ensure_code(code == CPL_ERROR_NONE, code);
257 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
275 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
277 cpl_image *res = NULL;
279 if (self->image != NULL) {
280 res = hdrl_image_get_image(self->image);
300 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
302 cpl_image *res = NULL;
304 if (self->image != NULL) {
305 res = hdrl_image_get_error(self->image);
324 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
325 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
326 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
328 if (self->qual == NULL) {
332 MOO_RBN_SINGLE_QUAL_TYPE);
352 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
354 if (self->header == NULL && self->extname != NULL &&
355 self->filename != NULL) {
357 moo_fits_load_extension_header(self->filename, NULL, self->extname);
358 if (self->header == NULL) {
359 self->header = cpl_propertylist_new();
379 if ((self->filename != NULL) && (self->extname != NULL)) {
380 if (self->header == NULL) {
382 cpl_fits_find_extension(self->filename, self->extname);
385 cpl_propertylist_load(self->filename, extnum);
388 if (self->qual == NULL) {
392 self->extname, CPL_TYPE_INT);
394 if (self->image == NULL) {
395 cpl_image *data = NULL;
396 cpl_image *err = NULL;
406 if (data != NULL && err != NULL) {
407 self->image = hdrl_image_create(data, err);
408 cpl_image_delete(data);
409 cpl_image_delete(err);
414 cpl_mask *mask = hdrl_image_get_mask(self->image);
417 return CPL_ERROR_NONE;
434 moo_sky_lines_list *skylines)
437 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0.0);
442 int nx = hdrl_image_get_size_x(himg);
446 double wmin = (0.5 - crpix1) * cd1_1 + crval1;
447 double wmax = (nx + 0.5 - crpix1) * cd1_1 + crval1;
449 double *zwmin = NULL;
450 double *zwmax = NULL;
451 cpl_array *sel = NULL;
454 int size = cpl_array_get_size(sel);
455 cpl_vector *data = NULL;
456 for (
int i = 0; i < size; i++) {
457 int idx = cpl_array_get_cplsize(sel, i, NULL);
458 double cmin = zwmin[idx];
459 double cmax = zwmax[idx];
460 int start = (cmin - crval1) / cd1_1 + crpix1;
461 int stop = (cmax - crval1) / cd1_1 + crpix1;
464 hdrl_image_extract(himg, start, rbn_idx, stop, rbn_idx);
465 hdrl_image_reject_value(fimg, CPL_VALUE_NAN);
466 cpl_image *img = hdrl_image_get_image(fimg);
467 cpl_image *error = hdrl_image_get_error(fimg);
468 cpl_image_reject_value(error, CPL_VALUE_NAN | CPL_VALUE_ZERO);
470 int enx = cpl_image_get_size_x(error);
471 int enbrej = cpl_image_count_rejected(error);
474 cpl_image_divide(img, error);
475 cpl_vector *row = cpl_vector_new_from_image_row(img, 1);
477 data = cpl_vector_duplicate(row);
480 int vsize = cpl_vector_get_size(data);
481 int row_size = cpl_vector_get_size(row);
482 cpl_vector_set_size(data, vsize + row_size);
483 for (
int j = 0; j < row_size; j++) {
484 double v = cpl_vector_get(row, j);
485 cpl_vector_set(data, j + vsize, v);
488 cpl_vector_delete(row);
490 hdrl_image_delete(fimg);
493 snr = cpl_vector_get_median(data);
499 cpl_vector_delete(data);
500 cpl_array_delete(sel);
521 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
524 fprintf(stream,
"---MOO_RBN_SINGLE\n");
525 fprintf(stream,
"extname %s\n", self->extname);
526 fprintf(stream,
"filename %s\n", self->filename);
527 fprintf(stream,
"image: ");
528 if (self->image != NULL) {
529 cpl_image_dump_structure(hdrl_image_get_image(self->image), stream);
532 fprintf(stream,
"null\n");
534 fprintf(stream,
"header %p\n", (
void *)self->header);
535 fprintf(stream,
"qual: ");
536 if (self->qual != NULL) {
537 cpl_image_dump_structure(self->qual, stream);
540 fprintf(stream,
"null\n");
544 return CPL_ERROR_NONE;
561 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
563 if (self->filename != NULL && self->extname != NULL) {
564 if (self->header != NULL) {
565 cpl_propertylist_delete(self->header);
568 if (self->image != NULL) {
569 hdrl_image_delete(self->image);
572 if (self->qual != NULL) {
573 cpl_image_delete(self->qual);
577 return CPL_ERROR_NONE;
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.
hdrl_image * moo_rbn_single_get_image(moo_rbn_single *self)
Get image of RBN_SINGLE.
moo_rbn_single * moo_rbn_single_create(const char *filename, moo_detector_type type)
Create a new moo_rbn_single with the given filename and type.
void moo_rbn_single_save(const moo_rbn_single *self, const char *filename, moo_detector_type type)
Save a moo_rbn_single to a FITS file.
cpl_propertylist * moo_rbn_single_get_header(moo_rbn_single *self)
Get header of rbn single.
moo_rbn_single * moo_rbn_single_new(moo_detector_type type)
Create a new moo_rbn_single.
void moo_rbn_single_delete(moo_rbn_single *self)
Delete a moo_rbn_single.
cpl_error_code moo_rbn_single_dump(const moo_rbn_single *self, FILE *stream)
Dump structural information of a Single DET.
cpl_error_code moo_rbn_single_load(moo_rbn_single *self, unsigned int level)
load the data of a moo_rbn_single
cpl_image * moo_rbn_single_get_data(moo_rbn_single *self)
Get image of data.
double moo_rbn_single_compute_snr(moo_rbn_single *self, int rbn_idx, moo_sky_lines_list *skylines)
Compute SNR for a given target.
cpl_error_code moo_rbn_single_free(moo_rbn_single *self)
Free memory associate to this single RBN.
cpl_error_code moo_rbn_single_set_wcs1(moo_rbn_single *self, double crpix1, double crval1, double cd1_1, const char *ctype1, const char *cunit1)
Set the WCS1 of the extension.
cpl_image * moo_rbn_single_get_qual(moo_rbn_single *self)
Get image of qual.
cpl_image * moo_rbn_single_get_errs(moo_rbn_single *self)
Get image of errs.
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