44#include "eris_ifu_dfs.h"
45#include "eris_ifu_error.h"
46#include "eris_ifu_functions.h"
47#include "eris_ifu_utils.h"
48#include "eris_ifu_detlin_static.h"
53static char eris_ifu_detlin_description[] =
"\
54This recipe computes detector non linearities and a bad pixel map.\n\
55The input files are increasing intensity raw flats\n\
57-----------------------------------------------------------------------------\n\
59 DO CATG Explanation Required #Frames\n\
60 ------- ----------- -------- -------\n\
61 LINEARITY_LAMP Lamp exposures Y 3-n \n\
64 DO CATG Explanation\n\
65 ------- -----------\n\
66 BPM_DETLIN Badpixel frame\n\
67 BPM_DETLIN_FILT Filtered badpixel frame\n\
68-----------------------------------------------------------------------------\n\
70Information on relevant parameters may be found with\n\
71 esorex --params "REC_NAME_DETLIN
"\n\
72 esorex --help "REC_NAME_DETLIN
"\n";
77cpl_recipe_define(eris_ifu_detlin, ERIS_BINARY_VERSION,
"Alex Agudo Berbel",
78 PACKAGE_BUGREPORT,
"2020",
79 "Detector's linearity & non linear bad pixels determination.",
80 eris_ifu_detlin_description);
95static cpl_error_code eris_ifu_detlin_fill_parameterlist(cpl_parameterlist *pl)
97 cpl_error_code err = CPL_ERROR_NONE;
98 cpl_parameter *p = NULL;
99 hdrl_parameter *hp = NULL;
100 cpl_parameterlist *pl_tmp = NULL;
102 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
116 for (p = cpl_parameterlist_get_first(pl_tmp);
118 p = cpl_parameterlist_get_next(pl_tmp))
121 cpl_parameterlist_append(pl, cpl_parameter_duplicate(p)));
126 p = cpl_parameter_new_value(REC_NAME_DETLIN
".post-filter-x",
128 "X Size of the post filtering kernel",
132 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pfx"));
134 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV));
136 cpl_parameterlist_append(pl, p));
139 p = cpl_parameter_new_value(REC_NAME_DETLIN
".post-filter-y",
141 "Y Size of the post filtering kernel",
145 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pfy"));
147 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV));
149 cpl_parameterlist_append(pl, p));
152 p = cpl_parameter_new_enum(REC_NAME_DETLIN
".post-filter-mode",
154 "Post filtering mode",
157 "closing",
"dilation"));
159 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pfm"));
161 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV));
163 cpl_parameterlist_append(pl, p));
168 err = cpl_error_get_code();
183eris_ifu_detlin(cpl_frameset *frameset,
const cpl_parameterlist *parlist)
187 cpl_vector *vec_dit_on = NULL,
189 cpl_image *bpm = NULL,
190 *bpm_filtered = NULL;
191 cpl_mask *bpm_mask = NULL,
192 *bpm_mask_filtered = NULL;
193 cpl_propertylist *qc_list = NULL;
194 hdrl_imagelist *hdrl_imglist_on = NULL,
195 *hdrl_imglist_off = NULL;
197 struct stdParamStruct stdParams = stdParamStructInit;
199 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
200 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
201 cpl_ensure_code(cpl_frameset_is_empty(frameset) == FALSE,
202 CPL_ERROR_NULL_INPUT);
203 cpl_ensure_code(cpl_frameset_count_tags(frameset, ERIS_IFU_RAW_LIN) > 2,
204 CPL_ERROR_ILLEGAL_INPUT);
221 qc_list = cpl_propertylist_new();
223 char* pipe_id = cpl_sprintf(
"%s%s%s", PACKAGE,
"/", PACKAGE_VERSION);
224 cpl_propertylist *applist = cpl_propertylist_new();
225 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG,
226 ERIS_IFU_PRO_DETLIN_GAIN_INFO);
228 cpl_dfs_save_propertylist(frameset, NULL, parlist, frameset, NULL,
229 REC_NAME_DETLIN, applist, NULL,
230 pipe_id, ERIS_IFU_PRO_DETLIN_GAIN_INFO_FN);
231 cpl_table_save(gain_table, applist, applist,
232 ERIS_IFU_PRO_DETLIN_GAIN_INFO_FN, CPL_IO_EXTEND);
233 cpl_table_delete(gain_table);
234 cpl_propertylist_delete(applist);
250 cpl_msg_info(cpl_func,
"Loading frames...");
252 &hdrl_imglist_on, &hdrl_imglist_off, &vec_dit_on, &vec_dit_off);
255 if ((stdParams.productDepth & 2) != 0) {
256 cpl_msg_info(cpl_func,
"Save additional products ...");
258 ERIS_IFU_PRO_DETLIN_DBG_CUBE_ON_FN, TRUE);
260 ERIS_IFU_PRO_DETLIN_DBG_CUBE_OFF_FN, TRUE);
265 for (
int ix=0; ix < onSize; ix++) {
266 dit_on = cpl_vector_get(vec_dit_on, ix);
268 for (
int ox=0; ox < offSize; ox++) {
269 if (dit_on == cpl_vector_get(vec_dit_off, ox)) {
277 cpl_msg_error(cpl_func,
"No matching off-frame DIT found for"
288 for (
int ix=0; ix < onSize; ix++) {
293 cpl_mask * dbpm = cpl_image_get_bpm(cimage_data);
294 cpl_mask * ebpm = cpl_image_get_bpm(cimage_error);
295 cpl_mask_or(dbpm, ebpm);
296 cpl_mask_or(ebpm, dbpm);
300 if ((stdParams.productDepth & 2) != 0) {
301 cpl_msg_info(cpl_func,
"Save additional products ...");
303 ERIS_IFU_PRO_DETLIN_DBG_CUBE_SUB_FN, TRUE);
306 cpl_msg_info(cpl_func,
"Generating bad pixel map...");
308 vec_dit_on, qc_list);
311 bpm_mask = cpl_mask_threshold_image_create(bpm, 0, INT_MAX);
312 n = cpl_mask_count(bpm_mask);
314 (
double) (cpl_mask_get_size_x(bpm_mask) * cpl_mask_get_size_y(bpm_mask));
316 cpl_msg_info(cpl_func,
" %lld bad pixels (%g%%)", n, p * 100.);
318 cpl_msg_info(cpl_func,
"Filtering bad pixel map...");
321 cpl_msg_info(cpl_func,
"Saving bad pixel maps...");
323 ERIS_IFU_PRO_DETLIN_BPM, ERIS_IFU_PRO_DETLIN_BPM_FN,
324 CPL_TYPE_USHORT, bpm);
325 cpl_propertylist_delete(qc_list);
326 qc_list = cpl_propertylist_new();
327 bpm_filtered = cpl_image_new_from_mask(bpm_mask_filtered);
330 ERIS_IFU_PRO_DETLIN_BPM_FILT,
331 ERIS_IFU_PRO_DETLIN_BPM_FILT_FN, CPL_TYPE_USHORT,
348 if (cpl_memory_is_empty() == 0) {
352 return (
int) cpl_error_get_code();
cpl_mask * eris_ifu_detlin_filter_mask(const cpl_mask *bpm, const cpl_parameterlist *parlist)
Apply morphological filtering to a bad pixel mask.
cpl_error_code eris_ifu_detlin_load_frames(const cpl_frameset *frameset, int exposureCorrectionMode, hdrl_imagelist **hdrl_imglist_on, hdrl_imagelist **hdrl_imglist_off, cpl_vector **vec_dit_on, cpl_vector **vec_dit_off)
Load linearity calibration frames from a frameset.
cpl_table * eris_compute_gain(cpl_frameset *frameset)
Compute detector gain from linearity frames.
cpl_image * eris_ifu_detlin_compute_linearity(const cpl_parameterlist *parlist, const hdrl_imagelist *imglist, const cpl_vector *vec_dit, cpl_propertylist *qclog)
Compute detector linearity bad pixel map.
cpl_error_code eris_ifu_dfs_set_groups(cpl_frameset *self)
Set the frame group (RAW, CALIB, or PRODUCT) for all frames in a frameset.
#define BRK_IF_ERROR(function)
If function is or returns an error != CPL_ERROR_NONE, then the try-block is exited.
#define CHECK_ERROR_STATE(void)
Check the CPL error state, and exit the try-block if not CPL_ERROR_NONE.
#define TRY
Beginning of a TRY-block.
#define CATCH
End of a TRY-block, beginning of a CATCH-block.
#define BRK_IF_NULL(function)
If function is or returns a NULL pointer, then the try-block is exited.
#define CATCH_MSGS()
Displays an error message stack.
cpl_error_code eris_ifu_fetch_std_param(const cpl_parameterlist *parlist, const char *recipename, struct stdParamStruct *stdParams)
Fetch standard parameters from parameter list into structure.
void eris_ifu_free_std_param(struct stdParamStruct *stdParams)
Free memory allocated for stdParamStruct.
cpl_error_code eris_ifu_add_std_params(cpl_parameterlist *pl, const char *recipename)
Add standard recipe parameters to a parameter list.
void eris_ifu_free_propertylist(cpl_propertylist **item)
Free memory and set pointer to null.
void eris_ifu_free_vector(cpl_vector **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_get_badpix_qc_from_ima(const cpl_image *image, cpl_propertylist *qc_list, const char *prefix)
compute QC keyword with number of bad pixels and fraction to total
void eris_ifu_free_hdrl_imagelist(hdrl_imagelist **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_save_hdrl_imagelist_dbg(const hdrl_imagelist *hdrl_img_list, const char *filename, int singlefile)
Save HDRL imagelist for debugging (data + error + mask planes)
void eris_ifu_free_image(cpl_image **item)
Free memory and set pointer to null.
void eris_ifu_free_mask(cpl_mask **item)
Free memory and set pointer to null.
void eris_ifu_free_parameterlist(cpl_parameterlist **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_save_image(cpl_frameset *fs, const cpl_propertylist *plist, const cpl_parameterlist *parlist, const char *recipe, const char *procatg, const char *filename, cpl_type type, const cpl_image *image)
Save image with DFS compliance.
void eris_ifu_free_hdrl_parameter(hdrl_parameter **item)
Free memory and set pointer to null.
cpl_parameterlist * hdrl_bpm_fit_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create a parameter list for the BPM_FIT computation.
hdrl_parameter * hdrl_bpm_fit_parameter_create_rel_chi(int degree, double rel_chi_low, double rel_chi_high)
create bpm_fit parameter with relative chi bpm treshold
cpl_error_code hdrl_image_sub_image(hdrl_image *self, const hdrl_image *other)
Subtract two images, store the result in the first image.
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
const hdrl_image * hdrl_imagelist_get_const(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.