32#include "moo_loc_single.h"
35#include "moo_single.h"
72 moo_loc_single *res = cpl_calloc(1,
sizeof(moo_loc_single));
91 moo_loc_single *res = NULL;
93 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
94 cpl_ensure(extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
98 res->extname = extname;
99 res->filename = filename;
120 if (self->f_centroids != NULL) {
121 cpl_image_delete(self->f_centroids);
123 if (self->m_centroids != NULL) {
124 cpl_image_delete(self->m_centroids);
126 if (self->f_wlow != NULL) {
127 cpl_image_delete(self->f_wlow);
129 if (self->f_wup != NULL) {
130 cpl_image_delete(self->f_wup);
132 if (self->m_wlow != NULL) {
133 cpl_image_delete(self->m_wlow);
135 if (self->m_wup != NULL) {
136 cpl_image_delete(self->m_wup);
138 if (self->flags != NULL) {
139 cpl_image_delete(self->flags);
141 if (self->header != NULL) {
142 cpl_propertylist_delete(self->header);
161 const char *filename,
166 MOO_LOC_SINGLE_MCENTROID, self->extname,
167 CPL_TYPE_FLOAT, self->header);
169 MOO_LOC_SINGLE_FCENTROID, self->extname,
170 CPL_TYPE_FLOAT, self->header);
172 MOO_LOC_SINGLE_WLO, self->extname,
173 CPL_TYPE_FLOAT, self->header);
175 MOO_LOC_SINGLE_WUP, self->extname,
176 CPL_TYPE_FLOAT, self->header);
178 if (keep_points == 1) {
180 MOO_LOC_SINGLE_MWLO, self->extname,
181 CPL_TYPE_FLOAT, self->header);
183 MOO_LOC_SINGLE_MWUP, self->extname,
184 CPL_TYPE_FLOAT, self->header);
186 MOO_LOC_SINGLE_FLAGS, self->extname,
208 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
209 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
211 fprintf(stream,
"---MOO_LOC_SINGLE\n");
212 fprintf(stream,
"filename %s extname %s\n", self->filename, self->extname);
213 fprintf(stream,
"mcentroids %p\n", (
void *)self->m_centroids);
214 fprintf(stream,
"fcentroids %p\n", (
void *)self->f_centroids);
215 return CPL_ERROR_NONE;
233 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
234 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
235 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
237 if (self->header == NULL) {
238 self->header = moo_fits_load_extension_header(self->filename,
239 MOO_LOC_SINGLE_MCENTROID,
241 if (self->header == NULL) {
242 self->header = cpl_propertylist_new();
262 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
263 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
264 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
266 if (self->f_centroids == NULL) {
269 MOO_LOC_SINGLE_FCENTROID,
270 self->extname, CPL_TYPE_DOUBLE);
272 return self->f_centroids;
299 int x1 = (int)floor(x);
300 int x2 = (int)ceil(x);
301 double y1 = cpl_image_get(fcentroids, x1, indexext, &rej1);
302 double y2 = cpl_image_get(fcentroids, x2, indexext, &rej2);
304 res = y1 * (x - x1) + y2 * (x2 - x);
323 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
324 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
325 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
327 if (self->m_centroids == NULL) {
330 MOO_LOC_SINGLE_MCENTROID,
331 self->extname, CPL_TYPE_DOUBLE);
333 return self->m_centroids;
351 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
352 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
353 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
355 if (self->f_wlow == NULL) {
358 self->extname, CPL_TYPE_DOUBLE);
377 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
378 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
379 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
381 if (self->f_wup == NULL) {
384 self->extname, CPL_TYPE_DOUBLE);
403 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
404 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
405 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
407 if (self->flags == NULL) {
410 self->extname, CPL_TYPE_INT);
416moo_loc_single_compute_qc_guess(moo_loc_single *self,
int deg_poly)
418 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
426 cpl_image *diff = cpl_image_subtract_create(mc, fc);
427 cpl_image *width = cpl_image_add_create(flo, fup);
429 int nx = cpl_image_get_size_x(diff);
430 int ny = cpl_image_get_size_y(diff);
432 cpl_binary *mask_data = cpl_mask_get_data(cpl_image_get_bpm(diff));
433 int *flags_data = cpl_image_get_data(flags);
434 int noutliers_pts = 0;
435 int noutliers_wid = 0;
438 for (
int i = 0; i < nx * ny; i++) {
439 if (flags_data[i] > 0) {
440 mask_data[i] = CPL_BINARY_1;
441 if (flags_data[i] == MOONS_FLAG_YDIFF_OUTLIERS) {
444 else if (flags_data[i] == MOONS_FLAG_WDIFF_OUTLIERS) {
447 else if (flags_data[i] == MOONS_FLAG_BADPIX) {
452 cpl_image_reject_value(diff, CPL_VALUE_NAN);
453 cpl_image_reject_value(width, CPL_VALUE_NAN);
455 double sigma = cpl_image_get_stdev(diff);
456 double median = cpl_image_get_median(diff);
457 double mean = cpl_image_get_mean(diff);
464 sigma = cpl_image_get_stdev(width);
465 median = cpl_image_get_median(width);
466 mean = cpl_image_get_mean(width);
474 cpl_image_delete(diff);
475 cpl_image_delete(width);
477 return CPL_ERROR_NONE;
481moo_loc_single_compute_qc_trace(moo_loc_single *self,
483 moo_loc_single *guess)
485 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
486 cpl_ensure_code(guess != NULL, CPL_ERROR_NULL_INPUT);
494 cpl_ensure_code(guess_mc != NULL, CPL_ERROR_NULL_INPUT);
496 cpl_image_reject_value(guess_mc, CPL_VALUE_NAN);
499 cpl_image *fdiff = cpl_image_subtract_create(fc, guess_fc);
501 int nx = cpl_image_get_size_x(mc);
502 int ny = cpl_image_get_size_y(mc);
504 cpl_binary *mask_data = cpl_mask_get_data(cpl_image_get_bpm(mc));
505 int *flags_data = cpl_image_get_data(flags);
508 for (
int i = 0; i < nx * ny; i++) {
509 if (flags_data[i] > 0) {
510 mask_data[i] = CPL_BINARY_1;
511 if (flags_data[i] == MOONS_FLAG_BADPROFILE) {
517 cpl_image *mdiff = cpl_image_subtract_create(mc, guess_mc);
519 cpl_image_reject_value(mdiff, CPL_VALUE_NAN);
520 cpl_image_reject_value(fdiff, CPL_VALUE_NAN);
522 double sigma = cpl_image_get_stdev(mdiff);
523 double median = cpl_image_get_median(mdiff);
524 double mean = cpl_image_get_mean(mdiff);
534 sigma = cpl_image_get_stdev(fdiff);
535 median = cpl_image_get_median(fdiff);
536 mean = cpl_image_get_mean(fdiff);
542 cpl_image_delete(fdiff);
543 cpl_image_delete(mdiff);
545 return CPL_ERROR_NONE;
563 cpl_mask *mask = NULL;
565 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
571 int size_x = cpl_image_get_size_x(fc);
572 int nb_fibres = cpl_image_get_size_y(fc);
574 mask = cpl_mask_new(size_x, size_y);
576 for (
int j = 1; j <= nb_fibres; j++) {
577 for (
int i = 1; i <= size_x; i++) {
579 double fcv = cpl_image_get(fc, i, j, &rej);
581 double ylo = fcv - cpl_image_get(fwlo, i, j, &rej);
582 double yup = fcv + cpl_image_get(fwup, i, j, &rej);
583 int iylo = floor(ylo);
584 int iyup = ceil(yup);
585 for (
int k = iylo; k <= iyup; k++) {
586 cpl_mask_set(mask, i, k, CPL_BINARY_1);
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_loc_single_delete(moo_loc_single *self)
Delete a moo_loc_single.
double moo_loc_single_eval_f_centroids(moo_loc_single *self, double x, int indexext, int *rej)
Get the Y fit centroid for a x value.
void moo_loc_single_save(const moo_loc_single *self, const char *filename, int keep_points)
Save a moo_loc_single to a FITS file.
cpl_error_code moo_loc_single_dump(const moo_loc_single *self, FILE *stream)
Dump structural information of LOC_SINGLE.
moo_loc_single * moo_loc_single_new(void)
Create a new moo_loc_single.
cpl_image * moo_loc_single_get_f_wup(moo_loc_single *self)
Get image of width low.
cpl_image * moo_loc_single_get_f_centroids(moo_loc_single *self)
Get image of fit centroids.
cpl_image * moo_loc_single_get_flags(moo_loc_single *self)
Get image of flags.
cpl_image * moo_loc_single_get_m_centroids(moo_loc_single *self)
Get image of measured centroids.
cpl_propertylist * moo_loc_single_get_header(moo_loc_single *self)
Get header of loc single.
moo_loc_single * moo_loc_single_create(const char *filename, const char *extname)
Create a new moo_loc_single from the given LOC filename.
cpl_image * moo_loc_single_get_f_wlo(moo_loc_single *self)
Get image of width low.
cpl_mask * moo_loc_single_get_ODR(moo_loc_single *self, int size_y)
Get outside data range mask far a DET single.
cpl_error_code moo_qc_set_widthy_sd(cpl_propertylist *plist, double val)
Set the QC.WIDTHY.STD value.
cpl_error_code moo_qc_set_shifty_fit_sd(cpl_propertylist *plist, double val)
Set the QC.SHIFTY.FIT.SD value.
cpl_error_code moo_qc_set_widthy_med(cpl_propertylist *plist, double val)
Set the QC.WIDTHY.MED value.
cpl_error_code moo_qc_set_nbadprofile(cpl_propertylist *plist, int val)
Set the QC.NBADPROFILE value.
cpl_error_code moo_qc_set_noutlier_wid(cpl_propertylist *plist, int val)
Set the QC.NOUTLIER.WID value.
cpl_error_code moo_qc_set_residy_med(cpl_propertylist *plist, double val)
Set the QC.RESIDY.MED value.
cpl_error_code moo_qc_set_shifty_pts_avg(cpl_propertylist *plist, double val)
Set the QC.SHIFTY.PTS.AVG value.
cpl_error_code moo_qc_set_widthy_avg(cpl_propertylist *plist, double val)
Set the QC.WIDTH.AVG value.
cpl_error_code moo_qc_set_residy_avg(cpl_propertylist *plist, double val)
Set the QC.RESIDY.AVG value.
cpl_error_code moo_qc_set_shifty_fit_avg(cpl_propertylist *plist, double val)
Set the QC.SHIFTY.FIT.AVG value.
cpl_error_code moo_qc_set_shifty_pts_sd(cpl_propertylist *plist, double val)
Set the QC.SHIFTY.PTS.SD value.
cpl_error_code moo_qc_set_fit_deg(cpl_propertylist *plist, int val)
Set the QC.RESIDY.AVG value.
cpl_error_code moo_qc_set_nbadpix(cpl_propertylist *plist, int val)
Set the QC.NBADPIX value.
cpl_error_code moo_qc_set_noutlier_pts(cpl_propertylist *plist, int val)
Set the QC.NOUTLIER.PTS value.
cpl_error_code moo_qc_set_shifty_pts_med(cpl_propertylist *plist, double val)
Set the QC.SHIFTY.PTS.MED value.
cpl_error_code moo_qc_set_shifty_fit_med(cpl_propertylist *plist, double val)
Set the QC.SHIFTY.FIT.MED value.
cpl_error_code moo_qc_set_residy_sd(cpl_propertylist *plist, double val)
Set the QC.RESIDY.STD value.