29#include "eris_ifu_error.h"
30#include "eris_ifu_utils.h"
31#include "eris_ifu_dfs.h"
32#include "eris_ifu_debug.h"
33#include "eris_ifu_flat_static.h"
34#include "eris_ifu_dark_static.h"
35#include "eris_ifu_functions.h"
36#include "eris_utils.h"
50cpl_error_code eris_ifu_flat_static(cpl_frameset *fs,
51 const cpl_parameterlist *parlist,
55 const char *recipe_name,
56 const char *procatg_prefix,
57 const char *instrument,
58 cpl_propertylist **qc_list,
59 hdrl_image **masterFlatHdrlImg_lo,
60 cpl_image **qualityImage)
62 cpl_error_code ret = CPL_ERROR_NONE;
63 cpl_image *contrib_map = NULL;
64 cpl_mask *bpm2dMask = NULL,
66 hdrl_image *masterFlatHdrlImg_hi = NULL;
67 hdrl_imagelist *hdrl_imglist_on = NULL,
68 *hdrl_imglist_off = NULL;
69 const char *fn = NULL;
72 double cleanmean = 0.,
75 cpl_ensure_code(fs, CPL_ERROR_NULL_INPUT);
76 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
77 cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
78 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
79 cpl_ensure_code(qc_list, CPL_ERROR_NULL_INPUT);
80 cpl_ensure_code(masterFlatHdrlImg_lo, CPL_ERROR_NULL_INPUT);
81 cpl_ensure_code(qualityImage, CPL_ERROR_NULL_INPUT);
86 double gain = 0., exptime = 0.;
87 eris_ifu_flat_load_frames(fs,
92 *qc_list = cpl_propertylist_new();
94 cpl_propertylist_append_string(*qc_list, CPL_DFS_PRO_CATG,
98 eris_ifu_flat_calc_qc_pre(parlist,
108 cpl_frame* raw_flat_lamp = NULL;
110 if (NULL != cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_LAMP)){
111 raw_flat_lamp = cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_LAMP);
113 }
else if (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_LAMP_ON) > 0) {
115 raw_flat_lamp = cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_LAMP_ON);
117 }
else if (NULL != cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_LAMP_ON)){
118 raw_flat_lamp = cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_LAMP_ON);
120 }
else if (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_NS_ON) > 0) {
122 raw_flat_lamp = cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_NS_ON);
124 raw_flat_lamp = cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_NS);
126 const char* fname = cpl_frame_get_filename(raw_flat_lamp);
127 cpl_propertylist* header = cpl_propertylist_load(fname, 0);
129 cpl_propertylist_delete(header);
130 eris_ifu_calc_flat(parlist,
137 masterFlatHdrlImg_lo,
138 &masterFlatHdrlImg_hi,
141 if (productDepth & 4) {
143 tmp_str = cpl_sprintf(
"%s_dbg_flat_masterflat.fits", recipe_name));
145 "eris_ifu_distortion_dbg_flat_02_masterflat",
151 if (mode != FLAT_MODE_SEGMENT) {
176 if (strcmp(recipe_name, REC_NAME_DISTORTION) == 0)
179 double maxcut = 50000.,
181 char *band_name = NULL;
182 cpl_frame *fr = NULL;
184 cpl_propertylist *plist = NULL;
186 fr = cpl_frameset_find(fs, ERIS_IFU_RAW_FLAT_NS);
187 fn = cpl_frame_get_filename(fr);
188 plist = cpl_propertylist_load(fn, 0);
190 if (strcmp(instrument, INSTRUMENT_ERIS) == 0) {
192 const char *tmp_band = NULL;
193 tmp_band = cpl_propertylist_get_string(plist,
195 band_name = cpl_sprintf(
"%c", tmp_band[0]);
198 band_name = cpl_sprintf(
"%s", cpl_propertylist_get_string(plist,
199 FHDR_S_FILTER_NAME));
212 hdrl_image **med = NULL,
225 mjd_obs = cpl_propertylist_get_double(plist, FHDR_MJD_OBS);
230 if ((strcmp(band_name,
"J") == 0) && (mjd_obs > 58150.)) {
236 if (productDepth & 4) {
238 "eris_ifu_distortion_dbg_flat_03_bad_medIm",
245 eris_ifu_flat_thresh_mask(*masterFlatHdrlImg_lo,
248 if (productDepth & 4) {
250 "eris_ifu_distortion_dbg_flat_04_bad_medIm_cut",
254 eris_ifu_column_tilt(*masterFlatHdrlImg_lo, sigma);
256 if (productDepth & 4) {
258 "eris_ifu_distortion_dbg_flat_05_bad_colImage",
261 eris_ifu_flat_stats_rectangle(*masterFlatHdrlImg_lo,
268 eris_ifu_flat_thresh_mask(*masterFlatHdrlImg_lo,
269 cleanmean - meanfactor*cleanstdev,
270 cleanmean + meanfactor*cleanstdev);
271 if (productDepth & 4) {
273 "eris_ifu_distortion_dbg_flat_06_bad_threshIm",
277 med = (hdrl_image**)cpl_calloc(n_iter,
sizeof(hdrl_image*));
279 med[0] = eris_ifu_flat_median_image(*masterFlatHdrlImg_lo,
280 -meanfactor*cleanstdev);
282 for (
int i = 0; i< n_iter-1; i++) {
283 med[i+1] = eris_ifu_flat_median_image(med[i],
284 -meanfactor*cleanstdev);
288 eris_ifu_sinfo_compare_images(*masterFlatHdrlImg_lo,
291 if (productDepth & 4) {
293 "eris_ifu_distortion_dbg_flat_07_bad_compImage",
298 *masterFlatHdrlImg_lo = original;
300 for (
int i = 0; i< n_iter; i++) {
303 cpl_free(med); med = NULL;
310 *masterFlatHdrlImg_lo,
312 &bpm2dMask, &bpm3dMask);
315 eris_ifu_flat_thresh_mask(*masterFlatHdrlImg_lo,
326 *masterFlatHdrlImg_lo,
328 &bpm2dMask, &bpm3dMask);
333 eris_ifu_flat_calc_qc_post(parlist,
337 masterFlatHdrlImg_hi,
338 *masterFlatHdrlImg_lo,
352 ret = cpl_error_get_code();
374double eris_ifu_getThreshold(hdrl_image *img) {
375 cpl_ensure(img != NULL, CPL_ERROR_ILLEGAL_INPUT, NAN);
387int eris_ifu_matchRow(
int blocksize,
int rx) {
388 cpl_ensure(blocksize > 0, CPL_ERROR_ILLEGAL_INPUT, -1);
389 cpl_ensure(rx >= 0, CPL_ERROR_ILLEGAL_INPUT, -1);
390 cpl_ensure(rx < ERIS_IFU_DETECTOR_SIZE_X, CPL_ERROR_ILLEGAL_INPUT, -1);
394 val = (int)((rx-4)/blocksize + 0.5);
404void eris_ifu_free_structFlatData(
struct structFlatData* sfd) {
407 if(sfd->ry != NULL) cpl_vector_delete(sfd->ry);
408 if(sfd->borderAleft != NULL) cpl_vector_delete(sfd->borderAleft);
409 if(sfd->borderAright != NULL) cpl_vector_delete(sfd->borderAright);
410 if(sfd->borderBleft != NULL) cpl_vector_delete(sfd->borderBleft);
411 if(sfd->borderBright != NULL) cpl_vector_delete(sfd->borderBright);
412 if(sfd->borderCleft != NULL) cpl_vector_delete(sfd->borderCleft);
413 if(sfd->borderCright != NULL) cpl_vector_delete(sfd->borderCright);
414 if(sfd->borderDleft != NULL) cpl_vector_delete(sfd->borderDleft);
415 if(sfd->borderDright != NULL) cpl_vector_delete(sfd->borderDright);
443cpl_error_code eris_ifu_calc_flat(
const cpl_parameterlist *parlist,
444 const char *recipe_name,
446 const hdrl_imagelist *hdrl_imglist_on,
447 const hdrl_imagelist *hdrl_imglist_off,
450 hdrl_image **masterFlatHdrlImg_lo,
451 hdrl_image **masterFlatHdrlImg_hi,
452 cpl_image **contrib_map)
454 cpl_error_code ret = CPL_ERROR_NONE;
455 cpl_image *noiseImage = NULL;
456 hdrl_image *fastFlatImg = NULL;
457 hdrl_parameter *pHdrlCollapse = NULL,
458 *pHdrlFlat_lo = NULL,
459 *pHdrlFlat_hi = NULL;
460 hdrl_imagelist *tmp_hdrl_imglist = NULL,
461 *tmp_hdrl_imglist2 = NULL;
462 char *tmp_str = NULL;
464 cpl_ensure_code(hdrl_imglist_on, CPL_ERROR_NULL_INPUT);
465 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
466 cpl_ensure_code(hdrl_imglist_off, CPL_ERROR_NULL_INPUT);
467 cpl_ensure_code(masterFlatHdrlImg_lo, CPL_ERROR_NULL_INPUT);
468 cpl_ensure_code(contrib_map, CPL_ERROR_NULL_INPUT);
472 cpl_msg_info(cpl_func,
"Generating master flat image...");
475 tmp_str = cpl_sprintf(
"eris.%s.collapse", recipe_name);
480 if (mode == FLAT_MODE_FAST) {
482 cpl_msg_info(cpl_func,
" Fast-mode");
493 masterFlatHdrlImg_lo,
499 cpl_image *tmpFilteredImg = cpl_image_new(
504 cpl_matrix *filterKernel = cpl_matrix_new(5,3);
505 cpl_matrix_set(filterKernel, 0, 1 ,1.0);
506 cpl_matrix_set(filterKernel, 1, 1 ,2.0);
507 cpl_matrix_set(filterKernel, 2, 1 ,3.0);
508 cpl_matrix_set(filterKernel, 3, 1 ,2.0);
509 cpl_matrix_set(filterKernel, 4, 1 ,1.0);
510 cpl_matrix_set(filterKernel, 2, 0 ,1.5);
511 cpl_matrix_set(filterKernel, 2, 2 ,1.5);
512 cpl_image_filter(tmpFilteredImg,
514 filterKernel, CPL_FILTER_LINEAR, CPL_BORDER_FILTER);
515 cpl_image_reject_from_mask(tmpFilteredImg,
522 *masterFlatHdrlImg_lo = fastFlatImg;
524 if (productDepth & 4) {
525 tmp_str = cpl_sprintf(
"%s_dbg_flat_fast.fits", recipe_name);
535 }
else if (mode == FLAT_MODE_HDRL) {
538 cpl_msg_info(cpl_func,
" HDRL-mode");
541 tmp_str = cpl_sprintf(
"eris.%s.flat_lo", recipe_name);
555 cpl_msg_warning(cpl_func,
"#off and #on frames isn't the same! "
556 "So no off-frames are subtracted at all.");
559 if (productDepth & 1) {
561 tmp_str = cpl_sprintf(
"eris.%s.flat_hi", recipe_name);
568 pHdrlCollapse, pHdrlFlat_hi,
569 masterFlatHdrlImg_hi, contrib_map);
574 if (productDepth & 4) {
575 tmp_str = cpl_sprintf(
"eris_ifu_%s_dbg_flat_01_sub", recipe_name);
583 cpl_msg_severity level = cpl_msg_get_level();
584 cpl_msg_set_level(CPL_MSG_ERROR);
588 pHdrlCollapse, pHdrlFlat_lo,
589 masterFlatHdrlImg_lo, contrib_map);
590 cpl_msg_set_level(level);
596 }
else if (mode == FLAT_MODE_SEGMENT) {
598 cpl_msg_info(cpl_func,
" Segment-mode");
600 hdrl_image *masterFlat = NULL,
619 double columnThreshold = .70,
623 *pdnewcleanRowBC = NULL,
626 const double *pcleanRow = NULL;
627 cpl_vector *cleanRow = NULL,
628 *dnewcleanRowBC = NULL,
634 struct structFlatData Data;
639 columnThreshold = .65;
650 eris_ifu_flat_data_init(&Data, blockSize);
656 for(
int ry = ERIS_IFU_DETECTOR_BP_BORDER;
657 ry < ERIS_IFU_DETECTOR_SIZE_Y-ERIS_IFU_DETECTOR_BP_BORDER;
662 ERIS_IFU_DETECTOR_SIZE_X,
666 pcleanRow = cpl_vector_get_data_const(cleanRow);
677 thresholdA = medianA.data * columnThreshold;
679 for (
int cx = 920; cx <= 1100; cx++) {
680 if (pcleanRow[cx] < thresholdA) {
681 if (nLower == 0) rightEdgeA = cx;
683 if (nLower > 1)
break;
692 for (
int cx = 60; cx >= 4; cx--) {
693 if (pcleanRow[cx] < thresholdA) {
694 if (nLower == 0) leftEdgeA = cx;
696 if (nLower > 3)
break;
707 Dright+1, blockSize);
710 thresholdD = medianD.data * columnThreshold;
713 for (
int cx = 1980; cx <= 2047; cx++) {
714 if (pcleanRow[cx] < thresholdD) {
715 if (nLower == 0) rightEdgeD = cx;
717 if (nLower > 6)
break;
726 for (
int cx = 1140; cx >= 1000; cx--) {
727 if (pcleanRow[cx] < thresholdD) {
728 if (nLower == 0) leftEdgeD = cx;
730 if (nLower > 3)
break;
740 dnewcleanRowBC = cpl_vector_new(ERIS_IFU_DETECTOR_SIZE_X);
741 pdnewcleanRowBC = cpl_vector_get_data(dnewcleanRowBC);
742 for (
int i = 0; i < ERIS_IFU_DETECTOR_SIZE_X-1; i++) {
743 pdnewcleanRowBC[i] = pcleanRow[i+1]- pcleanRow[i];
746 int start = rightEdgeA + 1;
747 sample = cpl_vector_extract(dnewcleanRowBC, start, leftEdgeD-1, 1);
748 sample2 = cpl_vector_extract(sample, 40, cpl_vector_get_size(sample)-1, 1);
749 leftEdgeC = cpl_vector_get_maxpos(sample2) + offset + 1;
752 sample2 = cpl_vector_extract(sample, 0, leftEdgeC-10-1, 1);
753 leftEdgeB = cpl_vector_get_maxpos(sample2) - 4;
755 rightEdgeC = cpl_vector_get_minpos(sample);
756 rightEdgeB = leftEdgeC - 2;
762 eris_ifu_flat_data_setBorders(&Data, ry,
763 leftEdgeA, rightEdgeA,
764 leftEdgeB, rightEdgeB,
765 leftEdgeC, rightEdgeC,
766 leftEdgeD, rightEdgeD);
779 for(
int ry = ERIS_IFU_DETECTOR_BP_BORDER;
780 ry < ERIS_IFU_DETECTOR_SIZE_Y-ERIS_IFU_DETECTOR_BP_BORDER;
785 ERIS_IFU_DETECTOR_SIZE_X,
789 borders = eris_ifu_flat_data_getBorderInterpolated(&Data, ry);
790 pborders = cpl_vector_get_data(borders);
792 leftEdgeA = pborders[0];
793 rightEdgeA = pborders[1];
794 leftEdgeB = pborders[2];
795 rightEdgeB = pborders[3];
796 leftEdgeC = pborders[4];
797 rightEdgeC = pborders[5];
798 leftEdgeD = pborders[6];
799 rightEdgeD = pborders[7];
804 if (4+1 <= leftEdgeA+1) {
805 for (
int x = 4; x <= leftEdgeA; x++) {
806 for (
int y = 0; y < sy; y++) {
813 for (
int x = rightEdgeA; x < leftEdgeB; x++) {
814 for (
int y = 0; y < sy; y++) {
820 for (
int x = rightEdgeB; x < leftEdgeC; x++) {
821 for (
int y = 0; y < sy; y++) {
827 for (
int x = rightEdgeC; x < leftEdgeD; x++) {
828 for (
int y = 0; y < sy; y++) {
834 if (rightEdgeD+1<=2044) {
835 for (
int x = rightEdgeD; x < 2044; x++) {
836 for (
int y = 0; y < sy; y++) {
846 coldThreshold = eris_ifu_getThreshold(tmpImg);
847 for (
int x = leftEdgeA; x < rightEdgeA+1; x++) {
848 for (
int y = 0; y < sy; y++) {
849 if (psubImg[x+y*sx] < coldThreshold) {
858 coldThreshold = eris_ifu_getThreshold(tmpImg);
859 for (
int x = leftEdgeB; x < rightEdgeB+1; x++) {
860 for (
int y = 0; y < sy; y++) {
861 if (psubImg[x+y*sx] < coldThreshold) {
870 coldThreshold = eris_ifu_getThreshold(tmpImg);
871 for (
int x = leftEdgeC; x < rightEdgeC+1; x++) {
872 for (
int y = 0; y < sy; y++) {
873 if (psubImg[x+y*sx] < coldThreshold) {
882 coldThreshold = eris_ifu_getThreshold(tmpImg);
883 for (
int x = leftEdgeD; x < rightEdgeD+1; x++) {
884 for (
int y = 0; y < sy; y++) {
885 if (psubImg[x+y*sx] < coldThreshold) {
894 for (
int x = 1; x <= ERIS_IFU_DETECTOR_SIZE_X; x++) {
896 for (
int y = ry+1; y <= ry+1+blockSize-1; y++) {
913 const double *pleftA = cpl_vector_get_data_const(Data.borderAleft),
914 *prightD = cpl_vector_get_data_const(Data.borderDright);
915 for (
int rx = 4; rx < 2044; rx++) {
916 bool lineCheck =
false;
919 rowIndex = eris_ifu_matchRow(blockSize, rx);
921 for (
int colx = pleftA[rowIndex]; colx < prightD[rowIndex]+1; colx++) {
923 if (rx_left > 2044) {
934 if (lineCheck ==
false) {
946 if ((fabs(cPix - cPixBefore) < thresh) &&
947 (fabs(cPix - cPixAfter) < thresh))
952 for (
int ax = startPos; ax < colx; ax++) {
970 *masterFlatHdrlImg_lo = masterFlat;
971 eris_ifu_free_structFlatData(&Data);
980 if (productDepth & 1) {
983 ret = cpl_error_get_code();
1016cpl_error_code eris_ifu_flat_load_frames(
const cpl_frameset *fs,
1019 hdrl_imagelist **hdrl_imglist_on,
1020 hdrl_imagelist **hdrl_imglist_off)
1022 cpl_error_code err = CPL_ERROR_NONE;
1023 cpl_frameset *fs_extracted = NULL;
1024 cpl_frameset *fs_off = NULL;
1025 hdrl_image *tmp_img = NULL;
1026 cpl_propertylist *plist = NULL;
1027 cpl_mask *bpm_dark = NULL,
1030 const cpl_frame *fr = NULL;
1031 const char *fn = NULL;
1033 bool first_on =
false;
1037 cpl_ensure_code(fs, CPL_ERROR_NULL_INPUT);
1038 cpl_ensure_code(hdrl_imglist_on, CPL_ERROR_NULL_INPUT);
1039 cpl_ensure_code(hdrl_imglist_off, CPL_ERROR_NULL_INPUT);
1040 cpl_ensure_code(gain, CPL_ERROR_NULL_INPUT);
1041 cpl_ensure_code(exptime, CPL_ERROR_NULL_INPUT);
1051 if (bpm_dark != NULL) {
1052 if (cpl_frameset_count_tags(fs, ERIS_IFU_PRO_DIST_BPM) == 1) {
1054 ERIS_IFU_PRO_DIST_BPM, 0);
1055 cpl_mask_or(bpm_dark, bpm_dist);
1058 if (cpl_frameset_count_tags(fs, ERIS_IFU_PRO_DETLIN_BPM) == 1) {
1060 ERIS_IFU_PRO_DETLIN_BPM, 0);
1061 cpl_mask_or(bpm_dark, bpm_detlin);
1065 cpl_msg_info(cpl_func,
"Loading all flat frames...");
1068 if (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_LAMP) > 0) {
1069 tag = cpl_sprintf(
"%s", ERIS_IFU_RAW_FLAT_LAMP);
1071 }
else if ( (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_LAMP_ON) > 0) &&
1072 (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_LAMP_OFF) > 0) ) {
1078 cpl_frameset_join(fs_extracted, fs_off);
1079 tag = cpl_sprintf(
"%s", ERIS_IFU_RAW_FLAT_LAMP_ON);
1080 }
else if (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_NS) > 0) {
1081 tag = cpl_sprintf(
"%s", ERIS_IFU_RAW_FLAT_NS);
1083 }
else if ( (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_NS_ON) > 0) &&
1084 (cpl_frameset_count_tags(fs, ERIS_IFU_RAW_FLAT_NS_OFF) > 0) ) {
1090 cpl_frameset_join(fs_extracted, fs_off);
1091 tag = cpl_sprintf(
"%s", ERIS_IFU_RAW_FLAT_NS_ON);
1096 "Neither FLAT_LAMP or FLAT_NS "
1100 if (cpl_frameset_get_size(fs_extracted) < 1) {
1102 "Not enough FLAT RAW frames!");
1112 fr = cpl_frameset_find_const(fs_extracted, tag);
1113 cpl_size frameCnt = cpl_frameset_get_size(fs_extracted);
1116 for (cpl_size i = 0 ; i < frameCnt ; i++) {
1117 fr = cpl_frameset_get_position_const(fs_extracted, i) ;
1118 fn = cpl_frame_get_filename(fr);
1119 eris_ifu_file_exists(fn);
1123 if (cpl_frame_get_tag(fr) != NULL) {
1125 if (bpm_dark != NULL) {
1139 plist = cpl_propertylist_load(fn, 0));
1140 if (cpl_propertylist_has(plist, FHDR_DET_CHIP_GAIN)) {
1141 *gain = cpl_propertylist_get_double(plist,
1142 FHDR_DET_CHIP_GAIN);
1146 if (cpl_propertylist_has(plist, FHDR_EXPTIME)) {
1147 *exptime = cpl_propertylist_get_double(plist,
1162 cpl_msg_info(cpl_func,
"nr_on: %d nr_off: %d",nr_on, nr_off);
1163 if (nr_on != nr_off) {
1164 cpl_msg_warning(cpl_func,
1165 "Number obj/off frames differs (#obj= %d #off= %d)!",
1172 "No ON raw frame in input, something wrong!");
1177 "input object list's size 0. Check"
1178 " your input data!");
1187 err = cpl_error_get_code();
1193 cpl_mask_delete(bpm_dark);
1194 cpl_mask_delete(bpm_dist);
1195 cpl_mask_delete(bpm_detlin);
1217cpl_error_code eris_ifu_flat_calc_qc_pre(
1218 const cpl_parameterlist *parlist,
1219 const char *recipe_name,
1220 const hdrl_imagelist *hdrl_imglist_on,
1221 const hdrl_imagelist *hdrl_imglist_off,
1222 cpl_propertylist *qc_list)
1224 cpl_error_code err = CPL_ERROR_NONE;
1226 cpl_vector *vec_off = NULL;
1227 cpl_vector *vec_diff = NULL;
1228 const cpl_parameter *p =NULL;
1229 hdrl_parameter *param = NULL;
1230 const hdrl_image *img_on_const = NULL;
1231 const hdrl_image *img_off_const = NULL;
1232 hdrl_image *img_on = NULL;
1233 hdrl_image *img_off = NULL;
1234 hdrl_image *img = NULL;
1235 cpl_image *contrib = NULL;
1247 double fpn_stdev1 = 0.;
1248 double fpn_stdev2 = 0.;
1249 char *tmp_str = NULL;
1251 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1252 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
1253 cpl_ensure_code(hdrl_imglist_on, CPL_ERROR_NULL_INPUT);
1254 cpl_ensure_code(hdrl_imglist_off, CPL_ERROR_NULL_INPUT);
1255 cpl_ensure_code(qc_list, CPL_ERROR_NULL_INPUT);
1260 qc_sat = eris_ifu_get_qc_saturated(hdrl_imglist_on);
1264 "nr. saturated pixels of master flat");
1270 nr_fr = (nr_on <= nr_off) ? nr_on : nr_off;
1272 if (nr_on != nr_off) {
1273 cpl_msg_warning(cpl_func,
1274 "Number obj/off frames differs (#obj= %d #off= %d)!",
1278 vec_off = cpl_vector_new(nr_fr);
1279 vec_diff = cpl_vector_new(nr_fr);
1281 for (
int i = 0; i < nr_fr; i++) {
1296 cpl_vector_get_mean(vec_off),
1297 "[ADU] average flux off frames");
1299 cpl_vector_get_mean(vec_diff),
1300 "[ADU] average counts");
1303 cpl_vector_get_stdev(vec_diff),
1304 "[ADU] stdev counts");
1308 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.xmin1", recipe_name);
1309 p = cpl_parameterlist_find_const(parlist, tmp_str);
1310 xmin1 = cpl_parameter_get_int(p);
1314 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.xmax1", recipe_name);
1315 p = cpl_parameterlist_find_const(parlist, tmp_str);
1316 xmax1 = cpl_parameter_get_int(p);
1320 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.ymin1", recipe_name);
1321 p = cpl_parameterlist_find_const(parlist, tmp_str);
1322 ymin1 = cpl_parameter_get_int(p);
1326 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.ymax1", recipe_name);
1327 p = cpl_parameterlist_find_const(parlist, tmp_str);
1328 ymax1 = cpl_parameter_get_int(p);
1332 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.xmin2", recipe_name);
1333 p = cpl_parameterlist_find_const(parlist, tmp_str);
1334 xmin2 = cpl_parameter_get_int(p);
1338 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.xmax2", recipe_name);
1339 p = cpl_parameterlist_find_const(parlist, tmp_str);
1340 xmax2 = cpl_parameter_get_int(p);
1344 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.ymin2", recipe_name);
1345 p = cpl_parameterlist_find_const(parlist, tmp_str);
1346 ymin2 = cpl_parameter_get_int(p);
1350 tmp_str = cpl_sprintf(
"eris.%s.qc.fpn.ymax2", recipe_name);
1351 p = cpl_parameterlist_find_const(parlist, tmp_str);
1352 ymax2 = cpl_parameter_get_int(p);
1356 tmp_str = cpl_sprintf(
"eris.%s.collapse", recipe_name);
1376 xmin1, ymin1, xmax1, ymax1);
1380 xmin2, ymin2, xmax2, ymax2);
1386 "[ADU] Fixed Pattern Noise of combined frames");
1389 "[ADU] Fixed Pattern Noise of combined frames");
1394 err = cpl_error_get_code();
1425cpl_error_code eris_ifu_flat_calc_qc_post(
const cpl_parameterlist *parlist,
1426 const char *recipe_name,
1427 const int productDepth,
1428 const char *procatg_prefix,
1429 const hdrl_image *masterFlatHdrlImg_hi,
1430 const hdrl_image *masterFlatHdrlImg_lo,
1431 const hdrl_imagelist *hdrl_imglist_on,
1432 const hdrl_imagelist *hdrl_imglist_off,
1433 const cpl_image *contrib_map,
1436 const cpl_mask *bpm2dMask,
1437 const cpl_mask *bpm3dMask,
1439 cpl_propertylist *qc_list,
1440 cpl_image **qualityImage)
1442 cpl_error_code err = CPL_ERROR_NONE;
1448 const cpl_mask *bp_mask_flat = NULL;
1449 cpl_image *tmpImg = NULL,
1456 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1457 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
1458 cpl_ensure_code(procatg_prefix, CPL_ERROR_NULL_INPUT);
1460 cpl_ensure_code(masterFlatHdrlImg_lo, CPL_ERROR_NULL_INPUT);
1461 cpl_ensure_code(hdrl_imglist_on, CPL_ERROR_NULL_INPUT);
1462 cpl_ensure_code(hdrl_imglist_off, CPL_ERROR_NULL_INPUT);
1463 cpl_ensure_code(contrib_map, CPL_ERROR_NULL_INPUT);
1468 cpl_ensure_code(fs, CPL_ERROR_NULL_INPUT);
1469 cpl_ensure_code(qc_list, CPL_ERROR_NULL_INPUT);
1470 cpl_ensure_code(qualityImage, CPL_ERROR_NULL_INPUT);
1478 *qualityImage = cpl_image_new_from_mask(bp_mask_flat);
1481 cpl_image_multiply_scalar(*qualityImage, ERIS_DQI_BP);
1486 double stdev = cpl_image_get_stdev(ima);
1487 double mean = cpl_image_get_mean(ima);
1488 double median = cpl_image_get_median(ima);
1493 key_name = cpl_sprintf(
"ESO QC FLAT STDDEV");
1494 key_comm = cpl_sprintf(
"[ADU] Std deviation on flat image");
1495 cpl_propertylist_append_double(qc_list,key_name, stdev);
1496 cpl_propertylist_set_comment(qc_list, key_name, key_comm);
1501 key_name = cpl_sprintf(
"ESO QC FLAT MEAN");
1502 key_comm = cpl_sprintf(
"[ADU] Mean value on flat image");
1503 cpl_propertylist_append_double(qc_list,key_name, mean);
1504 cpl_propertylist_set_comment(qc_list, key_name, key_comm);
1508 key_name = cpl_sprintf(
"ESO QC FLAT MEDIAN");
1509 key_comm = cpl_sprintf(
"[ADU] Median value on flat image");
1510 cpl_propertylist_append_double(qc_list,key_name, median);
1511 cpl_propertylist_set_comment(qc_list, key_name, key_comm);
1516 if (bpm2dMask != NULL) {
1517 bpm2dImg = cpl_image_new_from_mask(bpm2dMask);
1518 tmpImg = cpl_image_duplicate(bpm2dImg);
1520 cpl_image_multiply_scalar(tmpImg, ERIS_DQI_BP_BPM2D);
1521 cpl_image_add(*qualityImage, tmpImg);
1524 if (bpm3dMask != NULL) {
1525 bpm3dImg = cpl_image_new_from_mask(bpm3dMask);
1526 tmpImg = cpl_image_duplicate(bpm3dImg);
1528 cpl_image_multiply_scalar(tmpImg, ERIS_DQI_BP_BPM3D);
1529 cpl_image_add(*qualityImage, tmpImg);
1534 if ((hdrl_imglist_on != NULL) && ((productDepth & 2) != 0)) {
1535 fn = cpl_sprintf(
"%s%s", recipe_name,
1536 ERIS_IFU_PRO_FLAT_DBG_CUBE_ON_FN);
1541 if ((hdrl_imglist_off != NULL) && ((productDepth & 2) != 0)) {
1542 fn = cpl_sprintf(
"%s%s", recipe_name,
1543 ERIS_IFU_PRO_FLAT_DBG_CUBE_OFF_FN);
1548 if ((contrib_map != NULL) && ((productDepth & 1) != 0)) {
1549 fn = cpl_sprintf(
"%s%s", recipe_name,
1550 ERIS_IFU_PRO_FLAT_DBG_CONTRIBMAP_FN);
1551 pc = cpl_sprintf(
"%s%s", procatg_prefix,
1552 ERIS_IFU_PRO_FLAT_DBG_CONTRIBMAP);
1554 pc, fn, CPL_TYPE_USHORT, contrib_map);
1559 if ((masterFlatHdrlImg_hi != NULL) && ((productDepth & 1) != 0)) {
1560 fn = cpl_sprintf(
"%s%s", recipe_name,
1561 ERIS_IFU_PRO_FLAT_DBG_HI_FN);
1562 pc = cpl_sprintf(
"%s%s", procatg_prefix,
1563 ERIS_IFU_PRO_FLAT_DBG_HI);
1565 pc, fn, CPL_TYPE_DOUBLE,
1571 if ((bpm2dMask != NULL) && ((productDepth & 1) != 0)) {
1574 fn = cpl_sprintf(
"%s%s", recipe_name,
1575 ERIS_IFU_PRO_FLAT_DBG_BPM2D_FN);
1576 pc = cpl_sprintf(
"%s%s", procatg_prefix,
1577 ERIS_IFU_PRO_FLAT_DBG_BPM2D);
1579 pc, fn, CPL_TYPE_USHORT, bpm2dImg);
1584 if ((bpm3dMask != NULL) && ((productDepth & 1) != 0)) {
1587 fn = cpl_sprintf(
"%s%s", recipe_name,
1588 ERIS_IFU_PRO_FLAT_DBG_BPM3D_FN);
1589 pc = cpl_sprintf(
"%s%s", procatg_prefix,
1590 ERIS_IFU_PRO_FLAT_DBG_BPM3D);
1592 pc, fn, CPL_TYPE_USHORT, bpm3dImg);
1599 cpl_propertylist_update_string(qc_list, CPL_DFS_PRO_CATG,
"");
1604 err = cpl_error_get_code();
1627int eris_ifu_get_qc_saturated(
const hdrl_imagelist *dataHdrl)
1629 int saturated_pixels = 0,
1634 sat_min = ERIS_IFU_FLAT_SAT_MIN;
1635 double threshold = ERIS_IFU_FLAT_SATURATED;
1636 const hdrl_image *curHdrlImg = NULL;
1637 const cpl_image *curImg = NULL;
1638 const double *pcur_img = NULL;
1640 cpl_ensure_code(dataHdrl, CPL_ERROR_NULL_INPUT);
1644 ASSURE((threshold > 0.0) &&
1646 CPL_ERROR_ILLEGAL_INPUT,
1647 "threshold and sat_min must be greater than zero!");
1656 if (nz >= sat_min) {
1658 int ix = 0, iy = 0, iz = 0;
1659 for (iy = 0; iy < ny; iy++) {
1660 for (ix = 0; ix < nx; ix++) {
1662 for (iz = 0; iz < nz; iz++) {
1665 pcur_img = cpl_image_get_data_double_const(curImg);
1667 if (!cpl_image_is_rejected(curImg, ix+1, iy+1) &&
1668 (pcur_img[ix+iy*nx] > threshold)) {
1673 if (tmp_sat >= sat_min) {
1683 saturated_pixels = -1;
1686 return saturated_pixels;
1715cpl_error_code eris_ifu_column_tilt(hdrl_image *hdrl_img,
double sigma)
1717 cpl_error_code err = CPL_ERROR_NONE;
1718 cpl_image *img = NULL;
1719 cpl_binary *pmask = NULL;
1720 cpl_mask *mask = NULL;
1744 cpl_ensure_code(hdrl_img, CPL_ERROR_NULL_INPUT);
1745 cpl_ensure_code(sigma > 0., CPL_ERROR_ILLEGAL_INPUT);
1756 pimg = cpl_image_get_data_double(img);
1757 pmask = cpl_mask_get_data(mask);
1760 for (
int i = ERIS_IFU_DETECTOR_BP_BORDER;
1761 i < ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER ;
1766 column = (
double*)cpl_calloc(ny,
sizeof(
double*));
1767 sig = (
double*)cpl_calloc(ny,
sizeof(
double*));
1768 dat = (
double*)cpl_calloc(ny,
sizeof(
double*));
1771 for(
int j = 0; j < ny; j++) {
1772 if (pmask[i + j*nx] != BAD_PIX) {
1773 column[j] = pimg[i + j*nx];
1781 for(
int j = 0; j < ny; j++) {
1782 pmask[i + j*nx] = BAD_PIX;
1789 eris_ifu_flat_pixel_qsort(column, col_nr);
1791 for (
int j = 0.1*col_nr + 1; j <= 0.9*col_nr; j++) {
1793 sum2 += column[j] * column[j];
1798 noise = sigma * 1000.;
1800 mean = sum/(double)npix;
1801 noise = sqrt((sum2 - sum*mean)/(
double)(npix -1));
1810 if ((col_nr % 2 == 1) || (col_nr == 0)) {
1811 sinfo_median = column[col_nr/2];
1813 sinfo_median = (column[col_nr/2 - 1] + column[col_nr/2])/2.;
1818 for (
int j = 0; j < ny; j++) {
1819 if ((pmask[i + j*nx] != BAD_PIX) &&
1820 (fabs((pimg[i+j*nx])-sinfo_median) <= noise))
1822 column[col_nr] = pimg[i+j*nx];
1823 dat[col_nr] = (double)j;
1834 eris_ifu_my_fit(dat, column, col_nr, sig, mwt, &a,
1835 &b, &siga, &sigb, &chi2, &q);
1838 if ((fabs(b) >= slope) || (fabs(a) >= saturation) ||
1839 isnan(b) || isnan(a))
1841 cpl_msg_warning(cpl_func,
"linear fit: slope is greater than "
1842 "limit: %f saturation level is "
1843 "reached: %f in column number %d ",
1848 for (
int j = 0; j < ny; j++) {
1849 if ((pmask[i + j*nx] != BAD_PIX) &&
1850 (fabs(b) < slope) &&
1851 (fabs(a) < saturation))
1853 pimg[i+j*nx] = pimg[i+j*nx] - (a + b*(double)j);
1854 }
else if (pmask[i + j*nx] == BAD_PIX) {
1855 pimg[i+j*nx] = NAN ;
1856 }
else if (((fabs(b) >= slope) ||
1857 (fabs(a) >= saturation) ||
1858 isnan(a) || isnan(b)) &&
1859 (pmask[i + j*nx] != BAD_PIX))
1861 pimg[i+j*nx] -= sinfo_median;
1863 cpl_msg_error(cpl_func,
" case is not possible! %f %f",
1876 err = cpl_error_get_code();
1895cpl_error_code eris_ifu_flat_thresh_mask(hdrl_image* hdrl_img,
1901 const double *pimg = NULL;
1902 cpl_image *img = NULL;
1903 cpl_mask *mask = NULL;
1904 cpl_binary *pmask = NULL;
1905 cpl_error_code err = CPL_ERROR_NONE;
1907 cpl_ensure_code(hdrl_img, CPL_ERROR_NULL_INPUT);
1918 pimg = cpl_image_get_data_double_const(img);
1919 pmask = cpl_mask_get_data(mask);
1921 for (
int x = ERIS_IFU_DETECTOR_BP_BORDER; x < nx-ERIS_IFU_DETECTOR_BP_BORDER; x++) {
1922 for (
int y = ERIS_IFU_DETECTOR_BP_BORDER; y < ny-ERIS_IFU_DETECTOR_BP_BORDER; y++) {
1923 if ((pmask[x+nx*y] == GOOD_PIX) &&
1924 ((pimg[x+nx*y] > hi_cut) || (pimg[x+nx*y] < lo_cut)))
1926 pmask[x+nx*y] = BAD_PIX;
1934 err = cpl_error_get_code();
1953#define PIX_SWAP(a,b) { double temp=(a); (a)=(b); (b)=temp; }
1954#define PIX_STACK_SIZE 50
1956cpl_error_code eris_ifu_flat_pixel_qsort(
double *pix_arr,
int npix)
1958 int i, ir, j, k, l, j_stack;
1959 int i_stack[PIX_STACK_SIZE *
sizeof(double)];
1962 cpl_error_code ret = CPL_ERROR_NONE;
1964 cpl_ensure_code(pix_arr, CPL_ERROR_NULL_INPUT);
1973 for (j = l + 1; j <= ir; j++) {
1975 for (i = j - 1; i >= 1; i--) {
1976 if (pix_arr[i - 1] <= a)
1978 pix_arr[i] = pix_arr[i - 1];
1984 ir = i_stack[j_stack-- - 1];
1985 l = i_stack[j_stack-- - 1];
1988 PIX_SWAP(pix_arr[k - 1], pix_arr[l])
1989 if (pix_arr[l] > pix_arr[ir - 1]) {
1990 PIX_SWAP(pix_arr[l], pix_arr[ir - 1])
1992 if (pix_arr[l - 1] > pix_arr[ir - 1]) {
1993 PIX_SWAP(pix_arr[l - 1], pix_arr[ir - 1])
1995 if (pix_arr[l] > pix_arr[l - 1]) {
1996 PIX_SWAP(pix_arr[l], pix_arr[l - 1])
2004 while (pix_arr[i - 1] < a);
2007 while (pix_arr[j - 1] > a);
2010 PIX_SWAP(pix_arr[i - 1], pix_arr[j - 1]);
2012 pix_arr[l - 1] = pix_arr[j - 1];
2015 if (j_stack > PIX_STACK_SIZE) {
2017 "stack too small : aborting");
2019 if (ir - i + 1 >= j - l) {
2020 i_stack[j_stack - 1] = ir;
2021 i_stack[j_stack - 2] = i;
2024 i_stack[j_stack - 1] = j - 1;
2025 i_stack[j_stack - 2] = l;
2035 ret = cpl_error_get_code();
2040#undef PIX_STACK_SIZE
2061void eris_ifu_my_fit(
double x[],
double y[],
int ndata,
double sig[],
int mwt,
2062 double *a,
double *b,
double *siga,
double *sigb,
double *chi2,
2075 for (
int i = 0; i < ndata; i++) {
2076 double wt = 1./pow(sig[i], 2);
2082 for (
int i = 0; i < ndata; i++) {
2091 for (
int i = 0; i < ndata; i++) {
2092 t = (x[i] - sxoss)/sig[i];
2094 *b += t*y[i]/sig[i];
2097 for (
int i = 0; i < ndata; i++) {
2105 *a = (sy - sx*(*b))/ss;
2106 *siga = sqrt ((1.0 + sx*sx/(ss*st2))/ss);
2107 *sigb = sqrt (1.0/st2);
2110 for (
int i = 0 ; i < ndata ; i++) {
2111 *chi2 += pow((y[i] - (*a) - (*b)*x[i]), 2);
2118 double sigdat = sqrt ((*chi2)/(ndata - 2));
2122 for (
int i = 0; i < ndata; i++) {
2123 *chi2 += pow(((y[i] - (*a) - (*b) * x[i])/sig[i]),2);
2146cpl_error_code eris_ifu_flat_stats_rectangle(
const hdrl_image *hdrl_img,
2156 cpl_error_code ret = CPL_ERROR_NONE;
2157 const cpl_image *img = NULL;
2164 const double *pimg = NULL;
2165 double *pix_array = NULL,
2169 cpl_ensure_code(hdrl_img, CPL_ERROR_NULL_INPUT);
2174 cpl_ensure_code(loReject+hiReject < 100., CPL_ERROR_ILLEGAL_INPUT);
2175 cpl_ensure_code(loReject >= 0., CPL_ERROR_ILLEGAL_INPUT);
2176 cpl_ensure_code(loReject < 100., CPL_ERROR_ILLEGAL_INPUT);
2177 cpl_ensure_code(hiReject >= 0., CPL_ERROR_ILLEGAL_INPUT);
2178 cpl_ensure_code(hiReject < 100., CPL_ERROR_ILLEGAL_INPUT);
2179 cpl_ensure_code((llx >= 0) && (lly >= 0), CPL_ERROR_ILLEGAL_INPUT);
2180 cpl_ensure_code((urx >= 0) && (ury >= 0), CPL_ERROR_ILLEGAL_INPUT);
2181 cpl_ensure_code((llx < nx) && (lly < ny), CPL_ERROR_ILLEGAL_INPUT);
2182 cpl_ensure_code((urx < nx) && (ury < ny), CPL_ERROR_ILLEGAL_INPUT);
2183 cpl_ensure_code((ury > lly) && (urx > llx), CPL_ERROR_ILLEGAL_INPUT);
2188 npix = (urx-llx+1) * (ury-lly+1);
2189 pix_array = (
double*)cpl_calloc(npix,
sizeof(
double));
2193 pimg = cpl_image_get_data_double_const(img);
2195 for (
int row = lly; row <= ury; row++) {
2196 for (
int col = llx; col <= urx; col++) {
2197 if (!isnan(pimg[col + row*nx])) {
2198 pix_array[n] = pimg[col + row*nx];
2207 eris_ifu_flat_clean_mean(pix_array, npix, loReject, hiReject,
2212 lo_n = (int)(loReject / 100. * (
double)npix);
2213 hi_n = (int)(hiReject / 100. * (
double)npix);
2217 for (
int i = lo_n; i <= npix - hi_n; i++) {
2218 pix_sum += (double)pix_array[i];
2219 sqr_sum += ((double)pix_array[i] * (
double)pix_array[i]);
2226 "number of clean pixels is zero!");
2229 pix_sum /= (double)n;
2230 sqr_sum /= (double)n;
2231 *cleanstdev = sqrt(sqr_sum - pix_sum*pix_sum);
2237 ret = cpl_error_get_code();
2239 *cleanmean = -DBL_MAX;
2240 *cleanstdev = -DBL_MAX;
2243 cpl_free(pix_array);
2264cpl_error_code eris_ifu_flat_clean_mean(
double *array,
2266 double throwaway_low,
2267 double throwaway_high,
2270 cpl_error_code ret = CPL_ERROR_NONE;
2276 cpl_ensure_code(array, CPL_ERROR_NULL_INPUT);
2277 cpl_ensure_code(n_elements > 0, CPL_ERROR_ILLEGAL_INPUT);
2278 cpl_ensure_code(throwaway_low > 0., CPL_ERROR_ILLEGAL_INPUT);
2279 cpl_ensure_code(throwaway_high > 0., CPL_ERROR_ILLEGAL_INPUT);
2280 cpl_ensure_code(throwaway_low + throwaway_high < 100.,
2281 CPL_ERROR_ILLEGAL_INPUT);
2285 lo_n = (int)(throwaway_low * (
double)n_elements / 100.);
2286 hi_n = (int)(throwaway_high * (
double)n_elements / 100.);
2289 eris_ifu_flat_pixel_qsort(array, n_elements);
2291 for (
int i = lo_n; i < n_elements - hi_n; i++) {
2292 if (!isnan(array[i])) {
2301 *cleanmean = sum/(double)n;
2308 ret = cpl_error_get_code();
2310 *cleanmean = -DBL_MAX;
2339hdrl_image* eris_ifu_flat_median_image(
const hdrl_image *hdrl_img_in,
2342 hdrl_image *hdrl_img_out = NULL;
2343 const cpl_image *img_in = NULL;
2344 cpl_image *img_out = NULL;
2345 const cpl_mask *mask_in = NULL;
2346 cpl_mask *mask_out = NULL;
2347 const cpl_binary *pmask_in = NULL;
2348 cpl_binary *pmask_out = NULL;
2349 int *position = NULL,
2354 double *value = NULL,
2357 const double *pimg_in = NULL;
2359 cpl_ensure(hdrl_img_in, CPL_ERROR_NULL_INPUT, NULL);
2370 pimg_in = cpl_image_get_data_double_const(img_in);
2371 pmask_in = cpl_mask_get_data_const(mask_in);
2376 pimg_out = cpl_image_get_data_double(img_out);
2377 pmask_out = cpl_mask_get_data(mask_out);
2380 for (
int i = 0; i < im_size; i++) {
2382 if (pmask_in[i] == BAD_PIX) {
2387 value = (
double*)cpl_calloc(8,
sizeof(
double*));
2388 position = (
int*)cpl_calloc(8,
sizeof(
int*));
2392 position[0] = i + nx - 1 ;
2393 position[1] = i + nx ;
2394 position[2] = i + nx + 1 ;
2395 position[3] = i + 1 ;
2396 position[4] = i - nx + 1 ;
2397 position[5] = i - nx ;
2398 position[6] = i - nx - 1 ;
2399 position[7] = i - 1 ;
2405 if ((i >= 0) && (i < nx)) {
2407 position[4] += 2*nx;
2408 position[5] += 2*nx;
2409 position[6] += 2*nx;
2410 }
else if ((i >= ((
int)nx*ny - nx)) && (i < (int) nx*ny)) {
2412 position[0] -= 2*nx;
2413 position[1] -= 2*nx;
2414 position[2] -= 2*nx;
2415 }
else if (i % nx == 0) {
2420 }
else if ((i % nx) == (nx - 1)) {
2432 for (
int j = 0; j < nposition; j++) {
2433 if((position[j] > -1) && (position[j] < im_size)) {
2434 if (pmask_in[position[j]] == GOOD_PIX) {
2435 value[n] = pimg_in[position[j]];
2444 pmask_out[i] = BAD_PIX;
2452 eris_ifu_flat_pixel_qsort(value, nposition));
2454 if (nposition % 2 == 1) {
2455 my_med = value[nposition/2];
2457 my_med = (value [nposition/2 - 1] + value [nposition/2]) / 2.;
2473 pimg_out[i] = my_med;
2474 }
else if ((fmedian < 0) && (fabs(my_med-pimg_in[i]) >= -fmedian)) {
2475 pimg_out[i] = my_med;
2476 }
else if ((fmedian > 0) &&
2477 (fabs(my_med-pimg_in[i]) >=
2478 fmedian*sqrt(fabs(my_med))))
2480 pimg_out[i] = my_med;
2499 return hdrl_img_out;
2515cpl_error_code eris_ifu_sinfo_compare_images(
const hdrl_image *hdrl_img1,
2516 const hdrl_image *hdrl_img2,
2517 hdrl_image *hdrl_img_orig)
2519 const cpl_image *img1 = NULL,
2521 const double *pimg1 = NULL;
2522 const double *pimg2 = NULL;
2523 const cpl_mask *mask_img1 = NULL,
2525 cpl_mask *mask_orig = NULL;
2526 const cpl_binary *pmask_img1 = NULL,
2528 cpl_binary *pmask_orig = NULL;
2529 cpl_error_code err = CPL_ERROR_NONE;
2535 cpl_ensure_code(hdrl_img1, CPL_ERROR_NULL_INPUT);
2536 cpl_ensure_code(hdrl_img2, CPL_ERROR_NULL_INPUT);
2537 cpl_ensure_code(hdrl_img_orig, CPL_ERROR_NULL_INPUT);
2544 cpl_ensure_code(nx1 == nx2, CPL_ERROR_ILLEGAL_INPUT);
2545 cpl_ensure_code(ny1 == ny2, CPL_ERROR_ILLEGAL_INPUT);
2551 pimg1=cpl_image_get_data_double_const(img1);
2552 pimg2=cpl_image_get_data_double_const(img2);
2557 pmask_img1 = cpl_mask_get_data_const(mask_img1);
2558 pmask_img2 = cpl_mask_get_data_const(mask_img2);
2559 pmask_orig = cpl_mask_get_data(mask_orig);
2561 for (
int i = 0; i < nx1*ny1; i++) {
2562 if ((pmask_img1[i] == BAD_PIX) ||
2563 (pmask_img2[i] == BAD_PIX) ||
2564 (pimg1[i] != pimg2[i]))
2566 pmask_orig[i] = BAD_PIX;
2573 err = cpl_error_get_code();
2588cpl_error_code eris_ifu_flat_fast_bpm(hdrl_image *master_flat,
2589 const cpl_parameterlist *parlist,
2596 cpl_error_code err = CPL_ERROR_NONE;
2597 cpl_mask *bpm = NULL;
2598 cpl_image *img = NULL;
2599 const cpl_image *data = NULL;
2600 cpl_binary *pbpm = NULL;
2602 cpl_ensure_code(master_flat, CPL_ERROR_NULL_INPUT);
2603 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
2610 pbpm = cpl_mask_get_data(bpm);
2616 urx = ERIS_IFU_DETECTOR_BP_BORDER+SLITLET_WIDTH,
2617 ury = ERIS_IFU_DETECTOR_SIZE_Y;
2620 img = cpl_image_extract(data, llx, lly, urx, ury));
2626 cpl_vector *profile_x = NULL;
2627 double left_edge_pos = 0.,
2628 right_edge_pos = 0.,
2635 for (
int i = 0; i < nr_values; i++) {
2637 center_y = ERIS_IFU_DETECTOR_BP_BORDER + i*height + height/2;
2639 img, center_y, height);
2643 pprofile_x = cpl_vector_get_data(profile_x);
2645 cpl_image *imgg=NULL;
2646 imgg = cpl_image_wrap_double(cpl_vector_get_size(profile_x),1,
2649 imgg = cpl_image_wrap_double(cpl_image_get_size_x(img), 1,
2658 if (status == CPL_ERROR_EOL) {
2661 }
else if (status != CPL_ERROR_NONE) {
2666 for (
int y = center_y-height/2; y < center_y+height/2; y++) {
2667 for (
int x = 0; x <= (int)(left_edge_pos+.5); x++) {
2668 pbpm[x+y*ERIS_IFU_DETECTOR_SIZE_X] = BAD_PIX;
2676 for (
int y = 2020; y < 2045; y++) {
2677 for (
int x = 0; x <= (int)(left_edge_pos+.5); x++) {
2678 pbpm[x+y*ERIS_IFU_DETECTOR_SIZE_X] = BAD_PIX;
2687 err = cpl_error_get_code();
2694cpl_error_code eris_ifu_flat_data_init(
struct structFlatData *self,
int blocksize) {
2695 cpl_error_code err = CPL_ERROR_NONE;
2697 cpl_ensure_code(blocksize > 0, CPL_ERROR_ILLEGAL_INPUT);
2702 self->blocksize = blocksize;
2703 int nr = ERIS_IFU_DETECTOR_SIZE_Y/blocksize;
2704 self->ry = cpl_vector_new(nr);
2705 self->borderAleft = cpl_vector_new(nr);
2706 self->borderAright = cpl_vector_new(nr);
2707 self->borderBleft = cpl_vector_new(nr);
2708 self->borderBright = cpl_vector_new(nr);
2709 self->borderCleft = cpl_vector_new(nr);
2710 self->borderCright = cpl_vector_new(nr);
2711 self->borderDleft = cpl_vector_new(nr);
2712 self->borderDright = cpl_vector_new(nr);
2717 err = cpl_error_get_code();
2723cpl_error_code eris_ifu_flat_data_setBorders(
struct structFlatData *self,
int ry,
2724 int leftEdgeA,
int rightEdgeA,
2725 int leftEdgeB,
int rightEdgeB,
2726 int leftEdgeC,
int rightEdgeC,
2727 int leftEdgeD,
int rightEdgeD)
2729 cpl_error_code err = CPL_ERROR_NONE;
2733 cpl_vector_set(self->ry, self->cnt, ry);
2734 cpl_vector_set(self->borderAleft, self->cnt, leftEdgeA);
2735 cpl_vector_set(self->borderAright, self->cnt, rightEdgeA);
2736 cpl_vector_set(self->borderBleft, self->cnt, leftEdgeB);
2737 cpl_vector_set(self->borderBright, self->cnt, rightEdgeB);
2738 cpl_vector_set(self->borderCleft, self->cnt, leftEdgeC);
2739 cpl_vector_set(self->borderCright, self->cnt, rightEdgeC);
2740 cpl_vector_set(self->borderDleft, self->cnt, leftEdgeD);
2741 cpl_vector_set(self->borderDright, self->cnt, rightEdgeD);
2747 err = cpl_error_get_code();
2753cpl_vector* eris_ifu_flat_data_getBorderInterpolated(
struct structFlatData *self,
2756 cpl_vector *borders = NULL,
2760 double *pfit_par = NULL,
2764 rowx = row + self->blocksize/2.;
2766 borders = cpl_vector_new(8);
2767 pborders = cpl_vector_get_data(borders);
2770 pfit_par = cpl_vector_get_data(fit_par);
2771 pborders[0] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2775 pfit_par = cpl_vector_get_data(fit_par);
2776 pborders[1] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2780 pfit_par = cpl_vector_get_data(fit_par);
2781 pborders[2] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2785 pfit_par = cpl_vector_get_data(fit_par);
2786 pborders[3] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2790 pfit_par = cpl_vector_get_data(fit_par);
2791 pborders[4] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2795 pfit_par = cpl_vector_get_data(fit_par);
2796 pborders[5] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2800 pfit_par = cpl_vector_get_data(fit_par);
2801 pborders[6] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
2805 pfit_par = cpl_vector_get_data(fit_par);
2806 pborders[7] = (int)(pfit_par[0] + pfit_par[1] * rowx + pfit_par[2] * pow(rowx, 2) + 0.5);
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.
bool eris_ifu_frame_is_sky(const cpl_frame *fr)
Determine if a frame is a Sky frame or not.
ifsBand eris_ifu_get_band(const cpl_propertylist *header)
Determine preoptic band.
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.
bool eris_ifu_frame_is_on(const cpl_frame *fr)
Determine if a frame is obtained with lamps ON or OFF.
#define ASSURE(condition, error,...)
error handling macro (from fors-pipeline)
#define ERIS_IFU_TRY_EXIT(void)
The try-block is exited.
#define BRK_IF_ERROR(function)
If function is or returns an error != CPL_ERROR_NONE, then the try-block is exited.
#define RECOVER(void)
Recover the error state which was present during TRY (at the beginning of the try-block).
#define CHECK_ERROR_STATE(void)
Check the CPL error state, and exit the try-block if not CPL_ERROR_NONE.
#define BRK_WITH_ERROR_MSG(code,...)
Set a new CPL error, and exit the try-block.
#define CATCH_MSG()
Displays an error message.
#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_slitpos_gauss(const cpl_image *profile_x, double *left_edge_pos, double *right_edge_pos, int llx, int productDepth)
eris_ifu_dist_slitpos_gauss
cpl_vector * eris_ifu_calc_centers_collapse_chunk(const cpl_image *img, int chunk_center, int height)
cpl_vector * eris_ifu_image_collapse(const cpl_image *img)
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.
cpl_error_code eris_ifu_add_badpix_border(cpl_image *data, cpl_boolean add_ones, cpl_image *dqi)
Add image border frame to bad pixel map.
cpl_vector * eris_ifu_polyfit_1d(const cpl_vector *x, const cpl_vector *y, const int degree)
An easy-to-handle wrapper to cpl_polynomial_fit() to fit a vector.
hdrl_image * eris_ifu_load_exposure_file(const char *filename, int exposureCorrectionMode, cpl_image *dqi)
Read a raw detector exposure, perform some correction, add noise data.
void eris_ifu_free_frameset(cpl_frameset **item)
free memory and set pointer to null
void eris_ifu_free_propertylist(cpl_propertylist **item)
free memory and set pointer to null
void eris_ifu_free_string(char **item)
free memory and set pointer to null
void eris_ifu_free_double_array(double **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_save_image_dbg(const cpl_image *img, const char *filename, int create, const cpl_propertylist *pl)
eris_ifu_save_image_dbg
cpl_mask * eris_ifu_load_badpixel_mask(const cpl_frameset *frameset, const char *category, int ext)
**
void eris_ifu_free_int_array(int **item)
free memory and set pointer to null
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)
eris_ifu_save_hdrl_imagelist_dbg
cpl_error_code eris_ifu_save_hdrl_image_dbg(const hdrl_image *hdrl_img, const char *filename, int singlefile, const cpl_propertylist *pl)
save HDRL imagelist for debugging purposes
void eris_ifu_free_hdrl_image(hdrl_image **item)
free memory and set pointer to null
cpl_error_code eris_ifu_get_badpix_qc_from_mask(const cpl_mask *bp_mask, cpl_propertylist *qc_list, const char *prefix)
compute QC keyword with number of bad pixels and fraction to total
void eris_ifu_free_image(cpl_image **item)
free memory and set pointer to null
void eris_ifu_free_matrix(cpl_matrix **item)
free memory and set pointer to null
void eris_ifu_free_mask(cpl_mask **item)
free memory and set pointer to null
cpl_frameset * eris_ifu_get_frameset_by_tag(const cpl_frameset *frameset, const char *tag)
Get frames with given tag from frameset.
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
void eris_ifu_free_hdrl_parameter(hdrl_parameter **item)
free memory and set pointer to null
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
hdrl_parameter * hdrl_collapse_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
parse parameterlist for imagelist reduction method
cpl_error_code hdrl_flat_compute(hdrl_imagelist *hdrl_data, const cpl_mask *stat_mask, const hdrl_parameter *collapse_params, hdrl_parameter *flat_params, hdrl_image **master, cpl_image **contrib_map)
compute high or low frequency master flat with median filtering
hdrl_parameter * hdrl_flat_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse a parameterlist to create input parameters for the FLAT.
hdrl_value hdrl_image_get_pixel(const hdrl_image *self, cpl_size xpos, cpl_size ypos, int *pis_rejected)
get pixel values of hdrl_image
int hdrl_image_is_rejected(hdrl_image *self, cpl_size xpos, cpl_size ypos)
return if pixel is marked bad
cpl_error_code hdrl_image_set_pixel(hdrl_image *self, cpl_size xpos, cpl_size ypos, hdrl_value value)
set pixel values of hdrl_image
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_error_code hdrl_image_reject_from_mask(hdrl_image *self, const cpl_mask *map)
set bpm of hdrl_image
cpl_error_code hdrl_image_div_scalar(hdrl_image *self, hdrl_value value)
Elementwise division of an image with a scalar.
hdrl_value hdrl_image_get_median(const hdrl_image *self)
computes the median and associated error of an image.
hdrl_image * hdrl_image_sub_image_create(const hdrl_image *self, const hdrl_image *other)
Subtract two images.
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
hdrl_image * hdrl_image_extract(const hdrl_image *self, cpl_size llx, cpl_size lly, cpl_size urx, cpl_size ury)
extract copy of window from image
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
hdrl_value hdrl_image_get_mean(const hdrl_image *self)
computes mean pixel value and associated error of an image.
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
const cpl_mask * hdrl_image_get_mask_const(const hdrl_image *himg)
get cpl bad pixel mask from image
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of 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
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
cpl_error_code hdrl_image_reject(hdrl_image *self, cpl_size xpos, cpl_size ypos)
mark pixel as bad
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
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.
cpl_error_code hdrl_imagelist_collapse(const hdrl_imagelist *himlist, const hdrl_parameter *param, hdrl_image **out, cpl_image **contrib)
collapsing of image list
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
cpl_error_code hdrl_imagelist_sub_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Subtract two image lists, the first one is replaced by the result.
hdrl_imagelist * hdrl_imagelist_duplicate(const hdrl_imagelist *himlist)
Duplicate an image list.