25#include "eris_ifu_error.h"
26#include "eris_ifu_utils.h"
27#include "eris_ifu_functions.h"
28#include "eris_ifu_dark_static.h"
29#include "eris_ifu_dfs.h"
30#include "eris_utils.h"
44cpl_error_code eris_ifu_dark_static(
const cpl_parameterlist *parlist,
45 hdrl_imagelist *darkImageList,
46 hdrl_parameter *pdarkcollapse,
47 hdrl_image **masterDarkHdrlImg,
48 cpl_image **qualityImage,
49 cpl_image **masterBpm,
50 cpl_image **contribMap,
53 cpl_propertylist *qcParams)
55 cpl_error_code retVal = CPL_ERROR_NONE;
56 cpl_image *noiseImage = NULL;
57 cpl_image *masterBpmImg = NULL;
58 cpl_mask *borderSaturatedMask = NULL;
59 const cpl_mask *masterBpmMask = NULL;
60 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
61 cpl_ensure_code(darkImageList, CPL_ERROR_NULL_INPUT);
62 cpl_ensure_code(pdarkcollapse, CPL_ERROR_NULL_INPUT);
63 cpl_ensure_code(masterDarkHdrlImg, CPL_ERROR_NULL_INPUT);
64 cpl_ensure_code(qualityImage, CPL_ERROR_NULL_INPUT);
65 cpl_ensure_code(masterBpm, CPL_ERROR_NULL_INPUT);
66 cpl_ensure_code(contribMap, CPL_ERROR_NULL_INPUT);
67 cpl_ensure_code(bpm2dMask, CPL_ERROR_NULL_INPUT);
68 cpl_ensure_code(bpm3dMask, CPL_ERROR_NULL_INPUT);
69 cpl_ensure_code(qcParams, CPL_ERROR_NULL_INPUT);
73 cpl_msg_info(cpl_func,
"Generate master dark image");
74 hdrl_image *tmpHdrlImg = NULL;
76 &tmpHdrlImg, contribMap);
78 cpl_msg_info(cpl_func,
"Generate noise image");
79 noiseImage = eris_ifu_dark_noise(darkImageList);
86 borderSaturatedMask = cpl_mask_duplicate(
95 bpm2dMask, bpm3dMask);
98 (*qualityImage) = eris_ifu_dark_get_dqi(
99 borderSaturatedMask, *bpm2dMask, *bpm3dMask);
104 masterBpmImg = cpl_image_new_from_mask(masterBpmMask);
106 if (qcParams != NULL) {
107 cpl_msg_info(cpl_func,
"Generate QC parameters");
108 eris_ifu_dark_qc(parlist, *masterDarkHdrlImg, darkImageList,
109 masterBpmMask, *qualityImage, qcParams);
114 retVal = cpl_error_get_code();
119 (*masterBpm) = masterBpmImg;
131cpl_image *eris_ifu_dark_noise(hdrl_imagelist* imageList)
133 cpl_image *noiseImage = NULL;
134 cpl_size xDim,yDim,zDim;
143 noiseImage = cpl_image_new(xDim, yDim, CPL_TYPE_DOUBLE);
144 data = cpl_vector_new(zDim);
145 for (
int x = 1; x <= xDim; x++)
147 for (
int y = 1; y <= yDim; y++)
149 for (
int z = 0; z < zDim; z++)
151 cpl_vector_set(data, z, cpl_image_get(
155 cpl_image_set(noiseImage, x, y, cpl_vector_get_stdev(data));
175cpl_error_code eris_ifu_dark_qc(
176 const cpl_parameterlist *parlist,
177 hdrl_image *masterDarkHdrlImg,
178 hdrl_imagelist* darkImageList,
179 const cpl_mask *masterBpm,
180 const cpl_image *qualityImage,
181 cpl_propertylist *qcParams)
183 cpl_error_code retVal = CPL_ERROR_NONE;
184 hdrl_image *qcMasterDarkHdrlImg = NULL;
185 const cpl_image *masterDarkImage = NULL;
186 cpl_image *tmpImg = NULL;
187 cpl_vector *qcMedian = NULL;
188 cpl_size nBadPixels = 0;
192 cpl_size zone_def[4];
193 cpl_size hsize, nsamp;
194 double qc_ron_val, qc_ron_err, qc_fpn_val;
199 double fracBadPix = 0;
200 const int *qiData = NULL;
206 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
207 cpl_ensure_code(masterDarkHdrlImg, CPL_ERROR_NULL_INPUT);
208 cpl_ensure_code(darkImageList, CPL_ERROR_NULL_INPUT);
209 cpl_ensure_code(masterBpm, CPL_ERROR_NULL_INPUT);
210 cpl_ensure_code(qcParams, CPL_ERROR_NULL_INPUT);
216 nBadPixels = cpl_mask_count(masterBpm) - (2048*8 + 2040*8);
219 sx = cpl_mask_get_size_x(masterBpm);
220 sy = cpl_mask_get_size_y(masterBpm);
223 fracBadPix = (double) ((
int) nBadPixels) / (
int) npix;
226 "Total number of bad pixels but border pixels");
228 "Fraction of bad pixels to total");
230 qiData = cpl_image_get_data_const(qualityImage);
231 imgDim = cpl_image_get_size_x(qualityImage) *
232 cpl_image_get_size_y(qualityImage);
236 for (cpl_size ix=0; ix<imgDim; ix++) {
237 if (qiData[ix] & ERIS_DQI_SAT) {
240 if (qiData[ix] & ERIS_DQI_BP_BPM2D) {
243 if (qiData[ix] & ERIS_DQI_BP_BPM3D) {
250 "Dark 2D bad pixels");
253 "Dark 3D bad pixels");
255 (
double) ((
int) bp2dCnt) / (
int) npix,
256 "Fraction of 2D bad pixels to total");
259 (
double) ((
int) bp3dCnt) / (
int) npix,
260 "Fraction of 3D bad pixels to total");
272 "[ADU] Clean mean of master dark image");
277 "[ADU] Clean stdev of master dark image");
281 qcMedian = cpl_vector_new(frameCnt);
282 for (
int i=0; i<frameCnt; i++) {
283 cpl_vector_set(qcMedian, i,
284 cpl_image_get_median(
290 cpl_vector_get_mean(qcMedian),
"[ADU] Average of raw darks medians");
293 cpl_vector_get_stdev(qcMedian),
"[ADU] Stdev Read Out Noise");
298 zone_def[0] = cpl_parameter_get_int(
299 cpl_parameterlist_find_const(parlist,
300 "eris.eris_ifu_dark.qc_fpn_xmin"));
301 zone_def[1] = cpl_parameter_get_int(
302 cpl_parameterlist_find_const(parlist,
303 "eris.eris_ifu_dark.qc_fpn_xmax"));
304 zone_def[2] = cpl_parameter_get_int(
305 cpl_parameterlist_find_const(parlist,
306 "eris.eris_ifu_dark.qc_fpn_ymin"));
307 zone_def[3] = cpl_parameter_get_int(
308 cpl_parameterlist_find_const(parlist,
309 "eris.eris_ifu_dark.qc_fpn_ymax"));
310 hsize = cpl_parameter_get_int(
311 cpl_parameterlist_find_const(parlist,
312 "eris.eris_ifu_dark.qc_fpn_hsize"));
313 nsamp = cpl_parameter_get_int(
314 cpl_parameterlist_find_const(parlist,
315 "eris.eris_ifu_dark.qc_fpn_nsamp"));
318 cpl_flux_get_noise_window(masterDarkImage, zone_def, hsize, nsamp,
321 "Fixed Pattern Noise of combined frames");
324 zone_def[0] = cpl_parameter_get_int(
325 cpl_parameterlist_find_const(parlist,
326 "eris.eris_ifu_dark.qc_ron_xmin"));
327 zone_def[1] = cpl_parameter_get_int(
328 cpl_parameterlist_find_const(parlist,
329 "eris.eris_ifu_dark.qc_ron_xmax"));
330 zone_def[2] = cpl_parameter_get_int(
331 cpl_parameterlist_find_const(parlist,
332 "eris.eris_ifu_dark.qc_ron_ymin"));
333 zone_def[3] = cpl_parameter_get_int(
334 cpl_parameterlist_find_const(parlist,
335 "eris.eris_ifu_dark.qc_ron_ymax"));
336 hsize = cpl_parameter_get_int(
337 cpl_parameterlist_find_const(parlist,
338 "eris.eris_ifu_dark.qc_ron_hsize"));
339 nsamp = cpl_parameter_get_int(
340 cpl_parameterlist_find_const(parlist,
341 "eris.eris_ifu_dark.qc_ron_nsamp"));
344 cpl_flux_get_noise_window(masterDarkImage, zone_def, hsize,
345 nsamp, &qc_ron_val, &qc_ron_err);
347 "[ADU] Read Out Noise");
349 "[ADU] Error of Read Out Noise");
351 for (
int i=0 ; i<frameCnt-1 ; i++) {
352 tmpImg = cpl_image_subtract_create(
358 cpl_flux_get_noise_window(tmpImg, zone_def, hsize, nsamp,
360 qc_ron_val *= sqrt(1./2.);
362 paramName = cpl_sprintf(
"RON%d",i+1);
364 "[ADU] Read Out Noise");
370 "Number of RON frames");
375 retVal = cpl_error_get_code();
381cpl_image* eris_ifu_dark_get_dqi(
382 const cpl_mask* masterBpmMask,
383 const cpl_mask* bpm2dMask,
384 const cpl_mask* bpm3dMask ) {
386 cpl_image *dqi = NULL;
387 cpl_image *tmpImg = NULL;
390 dqi = cpl_image_new_from_mask(masterBpmMask);
391 cpl_image_multiply_scalar(dqi, ERIS_DQI_SAT);
392 for (cpl_size ix=1; ix <= ERIS_IFU_DETECTOR_SIZE; ix++) {
393 for (cpl_size iy=1; iy <= 4; iy ++) {
394 cpl_image_set(dqi,ix, iy, ERIS_DQI_BP);
396 for (cpl_size iy=ERIS_IFU_DETECTOR_SIZE-3;
397 iy <= ERIS_IFU_DETECTOR_SIZE; iy ++) {
398 cpl_image_set(dqi,ix, iy, ERIS_DQI_BP);
401 for (cpl_size iy=1; iy <= ERIS_IFU_DETECTOR_SIZE; iy++) {
402 for (cpl_size ix=1; ix <= 4; ix ++) {
403 cpl_image_set(dqi,ix, iy, ERIS_DQI_BP);
405 for (cpl_size ix=ERIS_IFU_DETECTOR_SIZE-3;
406 ix <= ERIS_IFU_DETECTOR_SIZE; ix ++) {
407 cpl_image_set(dqi,ix, iy, ERIS_DQI_BP);
411 tmpImg = cpl_image_new_from_mask(bpm2dMask);
412 cpl_image_multiply_scalar(tmpImg, ERIS_DQI_BP_BPM2D);
413 cpl_image_add(dqi, tmpImg);
416 tmpImg = cpl_image_new_from_mask(bpm3dMask);
417 cpl_image_multiply_scalar(tmpImg, ERIS_DQI_BP_BPM3D);
418 cpl_image_add(dqi, tmpImg);
cpl_error_code eris_ifu_append_qc_float(cpl_propertylist *pl, const char *name, float val, const char *comment)
Append a QC parameter of type FLOAT to a property list.
cpl_error_code eris_ifu_append_qc_double(cpl_propertylist *pl, const char *name, double val, const char *comment)
Append a QC parameter of type DOUBLE to a property list.
cpl_error_code eris_ifu_append_qc_int(cpl_propertylist *pl, const char *name, int val, const char *comment)
Append a QC parameter of type INT to a property list.
#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.
cpl_error_code eris_ifu_calc_bpm(const cpl_parameterlist *pl, const char *recipe_name, hdrl_image *master_img, const hdrl_imagelist *imglist_on, cpl_mask **bpm2dMask, cpl_mask **bpm3dMask)
Create and apply 2D and/or 3D Badpixel-Mask based on parameter.
void eris_ifu_free_string(char **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_hdrl_image_reject_mask(hdrl_image *img, const cpl_mask *mask)
'Add' the mask to the image
void eris_ifu_free_hdrl_image(hdrl_image **item)
free memory and set pointer to null
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
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
double hdrl_image_get_stdev(const hdrl_image *self)
computes the standard deviation of the data of an image
hdrl_value hdrl_image_get_mean(const hdrl_image *self)
computes mean pixel value and associated error of an image.
const cpl_mask * hdrl_image_get_mask_const(const hdrl_image *himg)
get cpl bad pixel mask from image
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
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
cpl_size hdrl_imagelist_get_size_y(const hdrl_imagelist *himlist)
Get number of rows of images in the imagelist.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
cpl_error_code hdrl_imagelist_collapse(const hdrl_imagelist *himlist, const hdrl_parameter *param, hdrl_image **out, cpl_image **contrib)
collapsing of image list
cpl_size hdrl_imagelist_get_size_x(const hdrl_imagelist *himlist)
Get number of colums 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.