36#include "eris_utils.h"
37#include "eris_pfits.h"
43#include <eris_ifu_dfs.h>
44#include <eris_nix_dfs.h>
48typedef enum {SLOW=0, FAST=1, UP_TO_RAMP=2} read_out_mode;
49static char eris_persistence_monitor_description[] =
50"This recipe counts the number of pixel above user defined thresholds. \n"
51"Inputs are ERIS SPIFFIER or NIX PERSISTENCE cubes and their corresponding\n"
52"The recipe applies the check only on the 1st input raw frame, thus the set\n"
53"of frames should contain only two input frames, the raw data and the \n"
54"Master dark (same DIT). The corresponding associated tags are: \n"
56"MASTER_DARK_IFU (or MASTER_DARK_IMA for NIX data) \n"
58"The number of pixels above saturation or threshold values are stored as QC\n"
59"In the output product that has PRO.CATG PERSISTENCE_HDR. Optional product \n"
60"when the user sets debug to true is the image on which the pixel are computed"
67cpl_recipe_define(eris_persistence_monitor, ERIS_BINARY_VERSION,
"Firstname Lastname",
68 PACKAGE_BUGREPORT,
"2017",
69 "Monitor PERSISTENCE image sat. & threshold pixels",
70 eris_persistence_monitor_description);
87cpl_error_code eris_persistence_monitor_fill_parameterlist(cpl_parameterlist *self)
89 cpl_errorstate prestate = cpl_errorstate_get();
94 p = cpl_parameter_new_value(
"eris.eris_persistence_monitor.saturation",
95 CPL_TYPE_DOUBLE,
"saturation level",
96 "eris.eris_persistence_monitor", 60000.);
97 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"saturation");
98 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
99 cpl_parameterlist_append(self, p);
101 p = cpl_parameter_new_value(
"eris.eris_persistence_monitor.saturation_neg",
102 CPL_TYPE_DOUBLE,
"negative saturation level",
103 "eris.eris_persistence_monitor", -4.5e7);
104 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"saturation_neg");
105 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
106 cpl_parameterlist_append(self, p);
108 p = cpl_parameter_new_value(
"eris.eris_persistence_monitor.threshold",
109 CPL_TYPE_DOUBLE,
"threshold level",
110 "eris.eris_persistence_monitor",300.);
111 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"threshold");
112 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
113 cpl_parameterlist_append(self, p);
116 p = cpl_parameter_new_value(
"eris.eris_persistence_monitor.debug",
117 CPL_TYPE_BOOL,
"If set to 'true' extra products are created",
118 "eris.eris_persistence_monitor", CPL_FALSE);
119 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"debug");
120 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
121 cpl_parameterlist_append(self, p);
123 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
124 : cpl_error_set_where(cpl_func);
135static int eris_persistence_monitor(cpl_frameset * frameset,
136 const cpl_parameterlist * parameters)
138 cpl_parameter * param = NULL;
139 cpl_parameterlist * parlist = (cpl_parameterlist *) parameters;
140 cpl_boolean debug = CPL_FALSE;
141 cpl_frameset * rawframes = NULL;
142 const cpl_frame * firstframe = NULL;
144 cpl_propertylist * plist = NULL;
145 cpl_propertylist * applist = NULL;
149 double saturation = 0;
150 double threshold = 0;
151 double saturation_negative = - 4.5e7;
153 const char* recipe =
"eris_persistence_monitor";
156 cpl_errorstate prestate = cpl_errorstate_get();
160 param = cpl_parameterlist_find(parlist,
161 "eris.eris_persistence_monitor.saturation");
162 int saturation_sw_set = cpl_parameter_get_default_flag(param);
163 saturation = cpl_parameter_get_double(param);
166 param = cpl_parameterlist_find(parlist,
167 "eris.eris_persistence_monitor.saturation_neg");
169 saturation_negative = cpl_parameter_get_double(param);
171 param = cpl_parameterlist_find(parlist,
172 "eris.eris_persistence_monitor.threshold");
173 int threshold_sw_set = cpl_parameter_get_default_flag(param);
174 threshold = cpl_parameter_get_double(param);
176 param = cpl_parameterlist_find(parlist,
177 "eris.eris_persistence_monitor.debug");
178 debug = cpl_parameter_get_bool(param);
180 if (!cpl_errorstate_is_equal(prestate)) {
181 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
182 "Could not retrieve the input "
194 cpl_error_get_code());
199 rawframes = cpl_frameset_new();
202 for (cpl_size j = 0; j<cpl_frameset_get_size(frameset); j++) {
203 const cpl_frame * current_frame;
204 current_frame = cpl_frameset_get_position_const(frameset, j);
205 if(!strcmp(cpl_frame_get_tag(current_frame), ERIS_PERSISTENCE_IMA) ||
206 !strcmp(cpl_frame_get_tag(current_frame), ERIS_PERSISTENCE_CUBE)) {
207 cpl_frame * new_frame = cpl_frame_duplicate(current_frame);
208 cpl_frameset_insert(rawframes, new_frame);
214 cpl_frameset_delete(rawframes);
215 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
216 "SOF does not have any file tagged "
217 "with %s or %s", ERIS_PERSISTENCE_IMA,
218 ERIS_PERSISTENCE_CUBE);
221 cpl_propertylist_delete(plist);
225 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
229 cpl_size nframes = cpl_frameset_get_size(rawframes);
231 firstframe = cpl_frameset_get_position_const(rawframes, 0);
232 const char* fname = cpl_frame_get_filename(firstframe);
233 plist = cpl_propertylist_load(fname, 0);
234 const char* arm = cpl_propertylist_get_string(plist,FHDR_E_ARM);
235 int naxis = cpl_propertylist_get_int(plist,
"NAXIS");
237 cpl_frame* frm_mdark = NULL;
238 const char* read_curname = cpl_propertylist_get_string(plist,
239 "ESO DET READ CURNAME");
240 read_out_mode rom = SLOW;
242 if (read_curname == NULL) {
243 cpl_propertylist_delete(plist);
244 cpl_frameset_delete(rawframes);
245 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
246 "No 'ESO DET READ CURNAME' in raw "
249 if (strcmp(read_curname,
"FAST_UNCORR") == 0) {
251 }
else if (strcmp(read_curname,
"SLOW_GR_UTR") == 0) {
253 }
else if (strcmp(read_curname,
"Normal_UpTheRamp") == 0) {
256 if(strcmp(arm,
"SPIFFIER") == 0) {
257 if(saturation_sw_set == 1) {
262 if(threshold_sw_set == 1) {
267 frm_mdark = cpl_frameset_find(frameset,ERIS_IFU_PRO_DARK);
269 if(saturation_sw_set == 1) {
278 if(threshold_sw_set == 1) {
283 frm_mdark = cpl_frameset_find(frameset,ERIS_NIX_MASTER_DARK_IMG_PRO_CATG);
285 cpl_propertylist_delete(plist);
288 param = cpl_parameterlist_find(parlist,
289 "eris.eris_persistence_monitor.saturation");
290 cpl_parameter_set_double(param, saturation);
291 param = cpl_parameterlist_find(parlist,
292 "eris.eris_persistence_monitor.threshold");
293 cpl_parameter_set_double(param, threshold);
294 cpl_image* img_persistence = NULL;
295 cpl_imagelist* iml_persistence = NULL;
298 applist = cpl_propertylist_new();
299 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
300 ERIS_PERSISTENCE_HDR);
302 nframes = cpl_frameset_get_size(rawframes);
309 int frame_nsat_max = 0;
315 int frame_nthresh_max = 0;
318 double nsat_avg_tmp = 0;
319 double nsat_frac_tmp = 0;
320 int nsat_max_tmp = 0;
321 int nsat_tot_tmp = 0;
325 double nthresh_avg_tmp = 0;
326 double nthresh_frac_tmp = 0;
327 int nthresh_max_tmp = 0;
328 int nthresh_tot_tmp = 0;
333 for(cpl_size i = 0; i < nframes; i++) {
335 const cpl_frame * frame = cpl_frameset_get_position_const(rawframes, i);
336 fname = cpl_frame_get_filename(frame);
337 plist = cpl_propertylist_load(fname, 0);
338 arm = cpl_propertylist_get_string(plist,FHDR_E_ARM);
339 naxis = cpl_propertylist_get_int(plist,
"NAXIS");
340 cpl_propertylist_delete(plist);
341 cpl_msg_info(cpl_func,
"process frame: %lld %s", i, fname);
345 eris_get_sat_qc_for_image(fname, frm_mdark, saturation,
346 saturation_negative, threshold, i, applist);
349 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 NSAT MAX",i);
350 nsat_max_tmp = cpl_propertylist_get_int(applist, keyname);
353 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 NSAT TOT",i);
354 nsat_tot_tmp += cpl_propertylist_get_int(applist, keyname);
357 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 NSAT AVG",i);
358 nsat_avg_tmp += cpl_propertylist_get_int(applist, keyname);
361 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 BPSAT FRAC", i);
362 nsat_frac_tmp += cpl_propertylist_get_double(applist, keyname);
365 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 NTHRESH MAX",i);
366 nthresh_max_tmp = cpl_propertylist_get_int(applist, keyname);
369 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 NTHRESH TOT",i);
370 nthresh_tot_tmp += cpl_propertylist_get_int(applist, keyname);
373 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 NTHRESH AVG",i);
374 nthresh_avg_tmp += cpl_propertylist_get_int(applist, keyname);
377 keyname = cpl_sprintf(
"ESO QC FRM%lld SLIC0 BPTHRESH FRAC", i);
378 nthresh_frac_tmp += cpl_propertylist_get_double(applist, keyname);
381 if(nsat_max_tmp > nsat_max) {
382 nsat_max = nsat_max_tmp;
386 if(nthresh_max_tmp > nthresh_max) {
387 nthresh_max = nthresh_max_tmp;
388 frame_nthresh_max = i;
394 eris_get_sat_qc_for_cube(fname, frm_mdark, saturation, saturation_negative,
395 threshold, i, applist);
397 keyname = cpl_sprintf(
"ESO QC FRM%lld NSAT MAX", i);
398 nsat_max_tmp = cpl_propertylist_get_int(applist, keyname);
401 keyname = cpl_sprintf(
"ESO QC FRM%lld NSAT TOT", i);
402 nsat_tot_tmp += cpl_propertylist_get_int(applist, keyname);
405 keyname = cpl_sprintf(
"ESO QC FRM%lld NSAT AVG", i);
406 nsat_avg_tmp += cpl_propertylist_get_double(applist, keyname);
409 keyname = cpl_sprintf(
"ESO QC FRM%lld BPSAT FRAC", i);
410 nsat_frac_tmp += cpl_propertylist_get_double(applist, keyname);
413 keyname = cpl_sprintf(
"ESO QC FRM%lld NTHRESH MAX", i);
414 nthresh_max_tmp = cpl_propertylist_get_int(applist, keyname);
417 keyname = cpl_sprintf(
"ESO QC FRM%lld NTHRESH TOT", i);
418 nthresh_tot_tmp += cpl_propertylist_get_int(applist, keyname);
421 keyname = cpl_sprintf(
"ESO QC FRM%lld NTHRESH AVG", i);
422 nthresh_avg_tmp += cpl_propertylist_get_double(applist, keyname);
425 keyname = cpl_sprintf(
"ESO QC FRM%lld BPTHRESH FRAC", i);
426 nthresh_frac_tmp += cpl_propertylist_get_double(applist, keyname);
429 if(nsat_max_tmp > nsat_max) {
430 nsat_max = nsat_max_tmp;
434 if(nthresh_max_tmp > nthresh_max) {
435 nthresh_max = nthresh_max_tmp;
436 frame_nthresh_max = i;
444 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG,
445 ERIS_PERSISTENCE_DBG);
447 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL,
448 img_persistence, CPL_BPP_IEEE_FLOAT, ERIS_PERSISTENCE_DBG,
449 applist, NULL, PACKAGE
"/" PACKAGE_VERSION,
450 ERIS_PERSISTENCE_DBG_FN)) {
452 (void)cpl_error_set_where(cpl_func);
455 if (cpl_dfs_save_imagelist(frameset, NULL, parlist, frameset, NULL,
456 iml_persistence, CPL_BPP_IEEE_FLOAT, ERIS_PERSISTENCE_DBG,
457 applist, NULL, PACKAGE
"/" PACKAGE_VERSION,
458 ERIS_PERSISTENCE_DBG_FN)) {
460 (void)cpl_error_set_where(cpl_func);
466 cpl_image_delete(img_persistence);
468 cpl_imagelist_delete(iml_persistence);
472 keyname = cpl_sprintf(
"ESO QC NSAT MAX");
473 cpl_propertylist_append_int(applist, keyname, nsat_max);
476 keyname = cpl_sprintf(
"ESO QC NSAT TOT");
477 cpl_propertylist_append_int(applist, keyname, nsat_tot_tmp);
480 keyname = cpl_sprintf(
"ESO QC NSAT AVG");
481 cpl_propertylist_append_double(applist, keyname, (nsat_avg_tmp / nframes));
484 keyname = cpl_sprintf(
"ESO QC FRM NSAT MAX");
485 cpl_propertylist_append_int(applist, keyname, frame_nsat_max);
488 keyname = cpl_sprintf(
"ESO QC BPSAT FRAC");
489 cpl_propertylist_append_double(applist, keyname,
490 (
double)(nsat_frac_tmp / nframes));
493 keyname = cpl_sprintf(
"ESO QC NTHRESH MAX");
494 cpl_propertylist_append_int(applist, keyname, nthresh_max);
497 keyname = cpl_sprintf(
"ESO QC NTHRESH TOT");
498 cpl_propertylist_append_int(applist, keyname, nthresh_tot_tmp);
501 keyname = cpl_sprintf(
"ESO QC NTHRESH AVG");
502 cpl_propertylist_append_double(applist, keyname, (nthresh_avg_tmp / nframes));
505 keyname = cpl_sprintf(
"ESO QC FRM NTHRESH MAX");
506 cpl_propertylist_append_int(applist, keyname, frame_nthresh_max);
509 keyname = cpl_sprintf(
"ESO QC BPTHRESH FRAC");
510 cpl_propertylist_append_double(applist, keyname, (nthresh_frac_tmp / nframes));
513 cpl_dfs_save_propertylist(frameset, NULL, parlist, frameset, NULL,
514 recipe, applist, NULL,
515 PACKAGE
"/" PACKAGE_VERSION, ERIS_PERSISTENCE_HDR_FN);
518 cpl_propertylist_delete(applist);
519 cpl_frameset_delete(rawframes);
522 return (
int)cpl_error_get_code();
cpl_error_code eris_nix_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.