83static void hdrl_persistence_verify_inputs(
85 const double det_turnover,
86 const double mean_trim,
87 const cpl_boolean cleanQ,
88 const cpl_array * dates_,
89 const cpl_array * exps_,
91 const cpl_imagelist * srcmasks_,
92 const cpl_image * maximum,
93 const cpl_image * density,
94 const cpl_image * fullwell,
95 const cpl_table * frac);
97static void hdrl_persistence_threshhold_img(
102static void hdrl_persistence_zero_flagged_pixels(
104 const cpl_mask * mask,
105 const cpl_binary value);
107static void hdrl_persistence_compute_qi(
113 const double exptime,
118static cpl_propertylist * hdrl_persistence_calc_stats(
120 const double trim_perc);
122static int hdrl_persistence_get_time_const(
123 const cpl_array * nus);
126 const cpl_array * nus,
132 const cpl_array * nus,
136static void hdrl_persistence_sum_Qtots(
152static void hdrl_persistence_qc_dump(
153 cpl_propertylist * plist,
156 cpl_error_ensure(plist && qcname, CPL_ERROR_NULL_INPUT,
return,
" ");
158 if (cpl_propertylist_has(plist, qcname)){
160 cpl_msg_debug(cpl_func,
"Statistics: %s = %g",qcname,
161 cpl_propertylist_get_double(plist,qcname));
182static void hdrl_persistence_verify_inputs(
184 const double det_turnover,
185 const double mean_trim,
186 const cpl_boolean cleanQ,
187 const cpl_array * dates_,
188 const cpl_array * exps_,
190 const cpl_imagelist * srcmasks_,
191 const cpl_image * maximum,
192 const cpl_image * density,
193 const cpl_image * fullwell,
194 const cpl_table * frac)
197 cpl_error_ensure(gain > 0.0,
198 CPL_ERROR_ILLEGAL_INPUT,
return,
"gain has to be larger than zero");
201 cpl_error_ensure(det_turnover < DBL_MAX,
202 CPL_ERROR_ILLEGAL_INPUT,
return,
"det_turnover exceeds range");
204 cpl_error_ensure(mean_trim >= 0.0 && mean_trim < 100.0,
205 CPL_ERROR_ILLEGAL_INPUT,
return,
"mean_trim < 0.0 or >= 100.0");
207 cpl_error_ensure(cleanQ == CPL_TRUE || cleanQ == CPL_FALSE,
208 CPL_ERROR_ILLEGAL_INPUT,
return,
"cleanQ isn't TRUE or FALSE");
211 cpl_error_ensure(dates_, CPL_ERROR_NULL_INPUT,
return,
"NULL 'dates' "
213 const cpl_size ndates = cpl_array_get_size(dates_);
214 cpl_error_ensure(ndates > 0, CPL_ERROR_DATA_NOT_FOUND,
return,
"Empty "
216 cpl_array_delete(cpl_array_cast(dates_, CPL_TYPE_DOUBLE));
217 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(),
return,
218 "'dates' input type must be numeric");
221 cpl_error_ensure(exps_, CPL_ERROR_NULL_INPUT,
return,
"NULL 'exps' input");
222 const cpl_size nexps = cpl_array_get_size(exps_);
223 cpl_error_ensure(nexps > 0, CPL_ERROR_DATA_NOT_FOUND,
return,
"Empty "
225 cpl_array_delete(cpl_array_cast(exps_, CPL_TYPE_DOUBLE));
226 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(),
return,
227 "'exps' input type must be numeric");
228 cpl_error_ensure(nexps == ndates - 1, CPL_ERROR_INCOMPATIBLE_INPUT,
return,
229 "'dates' should be 1 larger than 'exps'");
232 cpl_error_ensure(illums_, CPL_ERROR_NULL_INPUT,
return,
"NULL 'illums' "
235 cpl_error_ensure(nillums > 0, CPL_ERROR_DATA_NOT_FOUND,
return,
"Empty "
237 cpl_error_ensure(nillums == ndates - 1, CPL_ERROR_INCOMPATIBLE_INPUT,
238 return,
"'dates' should be 1 larger than 'illums'");
244 const cpl_size nsrcmasks = cpl_imagelist_get_size(srcmasks_);
245 cpl_error_ensure(nsrcmasks == nillums, CPL_ERROR_INCOMPATIBLE_INPUT,
246 return,
"The sizes of the 'srcmasks' & "
247 "'illum' inputs differ");
249 for (cpl_size i=0; i<nsrcmasks; ++i) {
250 const cpl_image * srcmask = cpl_imagelist_get_const(srcmasks_, i);
251 cpl_error_ensure(srcmask, CPL_ERROR_NULL_INPUT,
return,
"One of "
252 "the 'srcmasks' is NULL");
253 const cpl_size src_szx = cpl_image_get_size_x(srcmask);
254 const cpl_size src_szy = cpl_image_get_size_y(srcmask);
255 cpl_error_ensure(src_szx == illum_szx && src_szy == illum_szy,
256 CPL_ERROR_INCOMPATIBLE_INPUT,
return,
"The dimensions of "
257 "the 'srcmasks' & 'illum' inputs differ");
258 const cpl_type src_typ = cpl_image_get_type(srcmask);
259 cpl_error_ensure(src_typ == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
260 return,
"One of the 'srcmasks' is not of type int");
267 cpl_error_ensure(maximum && density && fullwell && frac, CPL_ERROR_NULL_INPUT,
return,
" ");
269 cpl_error_ensure(cpl_image_get_size_x(maximum) == illum_szx &&
270 cpl_image_get_size_y(maximum) == illum_szy,
271 CPL_ERROR_INCOMPATIBLE_INPUT,
return,
"Dimensions of maximum "
272 "trap map & 'illums' input differ");
275 cpl_error_ensure(cpl_image_get_size_x(density) == illum_szx &&
276 cpl_image_get_size_y(density) == illum_szy ,
277 CPL_ERROR_INCOMPATIBLE_INPUT,
return,
"Dimensions of trap "
278 "density map & 'illums' input differ");
281 cpl_error_ensure(cpl_image_get_size_x(fullwell) == illum_szx &&
282 cpl_image_get_size_y(fullwell) == illum_szy ,
283 CPL_ERROR_INCOMPATIBLE_INPUT,
return,
"Dimensions of trap "
284 "fullwell map & 'illums' input differ");
287 cpl_error_ensure(cpl_table_has_column(frac,
"TAU") &&
288 cpl_table_has_column(frac,
"NU"), CPL_ERROR_INCOMPATIBLE_INPUT,
289 return,
"Trap fraction table missing required columns");
290 cpl_error_ensure(6==cpl_table_get_nrow(frac), CPL_ERROR_INCOMPATIBLE_INPUT,
291 return,
"Trap fraction table contains wrong number of rows");
292 const cpl_type tau_t = cpl_table_get_column_type(frac,
"TAU");
293 const cpl_type nu_t = cpl_table_get_column_type(frac,
"NU");
294 cpl_error_ensure(tau_t == CPL_TYPE_FLOAT && nu_t == CPL_TYPE_FLOAT,
295 CPL_ERROR_INVALID_TYPE,
return,
"Trap fraction table columns "
296 "are of the wrong type");
334 const double turnover,
335 const double mean_trim,
336 const cpl_boolean cleanQ,
337 const cpl_array * dateobs,
338 const cpl_array * exptimes,
340 const cpl_imagelist * ilist_obj,
341 const cpl_image * maximum,
342 const cpl_image * density,
343 const cpl_image * fullwell,
344 const cpl_table * frac,
346 cpl_propertylist ** persistence_qc)
349 hdrl_persistence_verify_inputs(gain, turnover, mean_trim, cleanQ, dateobs, exptimes, ilist_persistence,
350 ilist_obj, maximum, density, fullwell, frac);
351 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(),
return cpl_error_get_code(),
360 cpl_msg_debug(cpl_func,
"Converting frames from ADUs to electrons."
361 " For this a gain of %g is used", gain);
371 const cpl_size nrows = cpl_table_get_nrow(frac);
372 const float * taud = cpl_table_get_data_float_const(frac,
"TAU");
373 const float * nud = cpl_table_get_data_float_const(frac,
"NU");
374 cpl_array * nus = cpl_array_wrap_float((
float *)nud, nrows);
377 cpl_msg_debug(cpl_func,
"cleanQ: %s", cleanQ ?
"true" :
"false");
381 const int n_tau = hdrl_persistence_get_time_const(nus);
385 hdrl_imagelist * Q = hdrl_persistence_create_hilist(n_tau, nx_0, ny_0);
386 hdrl_imagelist * Qacc = hdrl_persistence_create_hilist(n_tau, nx_0, ny_0);
387 hdrl_imagelist * Qtot = hdrl_persistence_create_hilist(n_tau, nx_0, ny_0);
392 hdrl_persistence_convert_max_traps(nus, himax, n_tau);
396 hdrl_persistence_calculate_rho(nus, hiden, n_tau);
397 cpl_array_unwrap(nus);
400 const double mjdobs = cpl_array_get(dateobs, 0, NULL);
401 for (
int index=0; index<nimgs; index++) {
402 const double exptime = cpl_array_get(exptimes, index, NULL);
403 const double diff_ = mjdobs - cpl_array_get(dateobs, index+1, NULL);
404 const double diff = diff_ * 24.0 * 3600.0;
405 cpl_msg_debug(cpl_func,
"illumination frame #%d taken %f secs before "
406 "the correction frame", index+1, diff);
421 double * imgd = cpl_image_get_data_double(img);
423 const double * fullwell_vals = NULL;
424 if (cpl_image_get_type(fullwell) != CPL_TYPE_DOUBLE) {
425 cpl_image * fullwellnew = cpl_image_cast(fullwell, CPL_TYPE_DOUBLE);
426 fullwell_vals = cpl_image_get_data_double_const(fullwellnew);
427 for (
size_t i=0; i<npix; i++) {
428 imgd[i] = (imgd[i] <= turnover ? fullwell_vals[i] : imgd[i]);
430 cpl_image_delete(fullwellnew);
432 fullwell_vals = cpl_image_get_data_double_const(fullwell);
433 for (
size_t i=0; i<npix; i++) {
434 imgd[i] = (imgd[i] <= turnover ? fullwell_vals[i] : imgd[i]);
439 hdrl_persistence_compute_qi(
440 Q, Qacc, diff, hirhos, taud, exptime, hiillum, n_tau, himaxs);
445 for (
int j=0; j<n_tau; j++) {
448 hdrl_persistence_threshhold_img(qj, hizero, qj);
453 for (
int j=0; j<n_tau; j++) {
455 const cpl_image * im = cpl_imagelist_get_const(ilist_obj, index);
456 cpl_mask * sm = cpl_mask_threshold_image_create(im, -0.5, 0.5);
458 hdrl_persistence_zero_flagged_pixels(qj, sm, CPL_BINARY_1);
463 hdrl_persistence_sum_Qtots(Qtot1_n, Qtot, Q, n_tau);
487 *persistence = Qtot1_n;
488 *persistence_qc = hdrl_persistence_calc_stats(Qtot1_n, mean_trim);
490 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MEAN");
491 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MEANERR");
492 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MINMAX MEAN");
493 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MINMAX MEANERR");
494 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST SIGCLIP MEAN");
495 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST SIGCLIP MEANERR");
496 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST WEIGHTED MEAN");
497 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST WEIGHTED MEANERR");
498 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MEDIAN");
499 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MEDIANERR");
500 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST STD");
501 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MIN");
502 hdrl_persistence_qc_dump(*persistence_qc,
"ESO QC PERSIST MAX");
505 if (cpl_msg_get_level() == CPL_MSG_DEBUG) {
506 cpl_propertylist_save(NULL,
"hdrl_debug_persistence_qtot.fits", CPL_IO_CREATE);
507 cpl_propertylist_save(NULL,
"hdrl_debug_persistence_qtot_error.fits", CPL_IO_CREATE);
509 for (
int i=0; i<n_tau; i++) {
514 cpl_image_save(qtoti_ima,
"hdrl_debug_persistence_qtot.fits", CPL_TYPE_DOUBLE, hdrl_persistence_calc_stats(qtoti, mean_trim), CPL_IO_EXTEND );
515 cpl_image_save(qtoti_err,
"hdrl_debug_persistence_qtot_error.fits", CPL_TYPE_DOUBLE, hdrl_persistence_calc_stats(qtoti, mean_trim), CPL_IO_EXTEND );
520 return cpl_error_get_code();
534static void hdrl_persistence_threshhold_img(
540 input && lower && upper, CPL_ERROR_NULL_INPUT,
return,
" ");
546 double * imgd = cpl_image_get_data_double(img);
549 const double * upper_vals = cpl_image_get_data_double_const(up);
551 const double * lower_vals = cpl_image_get_data_double_const(low);
553 for (
size_t i=0; i<npix; i++) {
554 imgd[i] = (imgd[i] <= upper_vals[i] ? imgd[i] : upper_vals[i]);
555 imgd[i] = (imgd[i] >= lower_vals[i] ? imgd[i] : lower_vals[i]);
564static void hdrl_persistence_sum_Qtots(
570 cpl_error_ensure(Qtot1_n && Qtot && Q, CPL_ERROR_NULL_INPUT,
return,
" ");
571 cpl_error_ensure(n_tau > -1, CPL_ERROR_ILLEGAL_INPUT,
return,
" ");
573 for (
int i=0; i<n_tau; i++) {
578 for (
int i=0; i<n_tau; i++) {
597static void hdrl_persistence_zero_flagged_pixels(
599 const cpl_mask * mask,
600 const cpl_binary value)
602 cpl_error_ensure(hdrl_img && mask, CPL_ERROR_NULL_INPUT,
return,
" ");
603 cpl_error_ensure(value == CPL_BINARY_0 || value == CPL_BINARY_1,
604 CPL_ERROR_ILLEGAL_INPUT,
return,
" ");
610 double * imgd = cpl_image_get_data_double(img);
612 const cpl_binary * mskd = cpl_mask_get_data_const(mask);
614 for (
size_t i=0; i<npix; i++) {
615 imgd[i] = (mskd[i] == value ? 0.00 : imgd[i]);
633static void hdrl_persistence_compute_qi(
639 const double exptime,
644 cpl_error_ensure(Q && Qacc && hirhos && taud && hiillum && himaxs,
645 CPL_ERROR_NULL_INPUT,
return,
" ");
646 cpl_error_ensure(n_tau > -1, CPL_ERROR_ILLEGAL_INPUT,
return,
" ");
647 cpl_error_ensure(exptime > -1, CPL_ERROR_ILLEGAL_INPUT,
return,
" ");
649 for (
int i=0; i<n_tau; i++) {
666 const hdrl_value val = { 1.0 - exp(-exptime/taud[i]), 0.0 };
670 hdrl_persistence_threshhold_img(QacciC, QacciC, himax);
701 for (
int i=0; i<n; i++) {
713static int hdrl_persistence_get_time_const(
714 const cpl_array * nus)
718 for (
int i=0; i<cpl_array_get_size(nus); i++) {
719 if (cpl_array_get(nus, i, NULL) > 0) {
733 const cpl_array * nus,
739 for (
int i=0; i<n_tau; i++) {
741 const double nu_i = cpl_array_get(nus, i, NULL);
755 const cpl_array * nus,
761 for (
int i=0; i<n_tau; i++) {
763 const double nu_i = cpl_array_get(nus, i, NULL);
783static cpl_propertylist * hdrl_persistence_calc_stats(
785 const double trim_perc)
787 cpl_error_ensure(Qtot, CPL_ERROR_NULL_INPUT,
return NULL,
" ");
788 cpl_error_ensure(trim_perc >= 0.0 && trim_perc < 100.0,
789 CPL_ERROR_ILLEGAL_INPUT,
return NULL,
"0 <= trim_perc < 100");
794 cpl_mask * mask_threshold =
796 cpl_mask_not(mask_threshold);
798 cpl_mask_or(Qtot_local_mask, mask_threshold);
799 cpl_mask_delete(mask_threshold);
801 cpl_propertylist * qclist = cpl_propertylist_new();
804 double trim = (double)floor((nx_0*ny_0)* trim_perc/2.0);
812 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MEAN",
814 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MEANERR",
817 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MINMAX MEAN",
819 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MINMAX MEANERR",
822 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST SIGCLIP MEAN",
824 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST SIGCLIP MEANERR",
827 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST WEIGHTED MEAN",
829 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST WEIGHTED MEANERR",
830 weighted_mean.
error);
832 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MEDIAN",
834 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MEDIANERR",
837 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST STD",
839 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MIN",
841 cpl_propertylist_append_double(qclist,
"ESO QC PERSIST MAX",
#define HDRL_OMP(x)
Definition hdrl_cat_def.h:35
cpl_error_code hdrl_image_sub_image(hdrl_image *self, const hdrl_image *other)
Subtract two images, store the result in the first image.
Definition hdrl_image_math.c:137
hdrl_value hdrl_image_get_median(const hdrl_image *self)
computes the median and associated error of an image.
Definition hdrl_image_math.c:501
cpl_error_code hdrl_image_add_image(hdrl_image *self, const hdrl_image *other)
Add two images, store the result in the first image.
Definition hdrl_image_math.c:59
cpl_error_code hdrl_image_mul_scalar(hdrl_image *self, hdrl_value value)
Elementwise multiplication of an image with a scalar.
Definition hdrl_image_math.c:245
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
Definition hdrl_image.c:391
hdrl_value hdrl_image_get_minmax_mean(const hdrl_image *self, double nlow, double nhigh)
computes the minmax rejected mean and the associated error of an image.
Definition hdrl_image_math.c:448
hdrl_value hdrl_image_get_sigclip_mean(const hdrl_image *self, double kappa_low, double kappa_high, int niter)
computes the sigma-clipped mean and associated error of an image.
Definition hdrl_image_math.c:425
double hdrl_image_get_stdev(const hdrl_image *self)
computes the standard deviation of the data of an image
Definition hdrl_image_math.c:540
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition hdrl_image.c:157
cpl_error_code hdrl_image_mul_image(hdrl_image *self, const hdrl_image *other)
Multiply two images, store the result in the first image.
Definition hdrl_image_math.c:201
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition hdrl_image.c:131
hdrl_value hdrl_image_get_mean(const hdrl_image *self)
computes mean pixel value and associated error of an image.
Definition hdrl_image_math.c:402
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
Definition hdrl_image.c:540
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
Definition hdrl_image.c:525
hdrl_value hdrl_image_get_weighted_mean(const hdrl_image *self)
computes the weighted mean and associated error of an image.
Definition hdrl_image_math.c:520
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition hdrl_image.c:295
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition hdrl_image.c:118
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
Definition hdrl_image.c:311
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition hdrl_image.c:379
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
Definition hdrl_imagelist_io.c:274
cpl_size hdrl_imagelist_get_size_y(const hdrl_imagelist *himlist)
Get number of rows of images in the imagelist.
Definition hdrl_imagelist_io.c:188
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
Definition hdrl_imagelist_io.c:384
const hdrl_image * hdrl_imagelist_get_const(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
Definition hdrl_imagelist_io.c:229
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
Definition hdrl_imagelist_io.c:148
cpl_error_code hdrl_imagelist_mul_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise multiplication of a scalar to each image in the himlist.
Definition hdrl_imagelist_basic.c:330
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
Definition hdrl_imagelist_io.c:84
cpl_size hdrl_imagelist_get_size_x(const hdrl_imagelist *himlist)
Get number of colums of images in the imagelist.
Definition hdrl_imagelist_io.c:168
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
Definition hdrl_imagelist_io.c:210
cpl_error_code hdrl_persistence_compute(const double gain, const double turnover, const double mean_trim, const cpl_boolean cleanQ, const cpl_array *dateobs, const cpl_array *exptimes, hdrl_imagelist *ilist_persistence, const cpl_imagelist *ilist_obj, const cpl_image *maximum, const cpl_image *density, const cpl_image *fullwell, const cpl_table *frac, hdrl_image **persistence, cpl_propertylist **persistence_qc)
generate the persistence map
Definition hdrl_persistence.c:332
Definition hdrl_image_defs.h:40
Definition hdrl_imagelist_defs.h:42
Definition hdrl_types.h:77
hdrl_error_t error
Definition hdrl_types.h:79
hdrl_data_t data
Definition hdrl_types.h:78