37#include <gsl/gsl_interp.h>
38#include "eris_ifu_dfs.h"
39#include "eris_ifu_debug.h"
40#include "eris_ifu_utils.h"
41#include "eris_ifu_functions.h"
42#include "eris_ifu_distortion_static.h"
43#include "eris_ifu_flat_static.h"
44#include "eris_ifu_error.h"
45#include "eris_utils.h"
80 ifsInstrument instrument = UNSET_INSTRUMENT;
81 const char *filename = NULL;
82 cpl_propertylist *header = NULL;
90 filename = cpl_frame_get_filename(frame);
91 header = cpl_propertylist_load(filename, 0);
97 instrument = UNSET_INSTRUMENT;
118 hdrl_image *image = NULL;
119 cpl_image *noiseImage = NULL;
121 cpl_ensure(cplImage, CPL_ERROR_NULL_INPUT, NULL);
132 cpl_image_delete(noiseImage);
153 cpl_image *noiseImg = NULL;
155 cpl_ensure(data, CPL_ERROR_NULL_INPUT, NULL);
156 cpl_ensure(gain > 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
157 cpl_ensure(readnoise >= 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
162 noiseImg = cpl_image_duplicate(data));
166 cpl_image_abs(noiseImg);
167 cpl_image_multiply_scalar(noiseImg, gain);
168 cpl_image_add_scalar(noiseImg, readnoise * readnoise);
169 cpl_image_power(noiseImg, 0.5);
170 cpl_image_divide_scalar(noiseImg, gain);
197 int exposureCorrectionMode) {
198 hdrl_imagelist *imageList = NULL;
199 hdrl_image *tmpImage = NULL;
200 const cpl_frame *frame = NULL;
202 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
206 frameCnt = cpl_frameset_get_size(frameset);
208 for (cpl_size ix=0; ix<frameCnt; ix++) {
209 frame = cpl_frameset_get_position_const(frameset, ix);
211 frame, exposureCorrectionMode, NULL);
238 int exposureCorrectionMode,
241 hdrl_image *image = NULL;
242 const char *filename;
244 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, NULL);
247 filename = cpl_frame_get_filename(frame);
249 filename, exposureCorrectionMode, dqi);
277 cpl_error_code err = CPL_ERROR_NONE;
281 cpl_ensure_code(data, CPL_ERROR_NULL_INPUT);
285 for (x = 1; x <= ERIS_IFU_DETECTOR_SIZE_X; x++) {
288 y <= ERIS_IFU_DETECTOR_BP_BORDER;
291 cpl_image_reject(data, x, y);
293 cpl_image_set(dqi, x, y, ERIS_DQI_BP);
296 cpl_image_set(data, x, y, 1.);
301 for (y = ERIS_IFU_DETECTOR_SIZE_Y-ERIS_IFU_DETECTOR_BP_BORDER+1;
302 y <= ERIS_IFU_DETECTOR_SIZE_Y;
305 cpl_image_reject(data, x, y);
307 cpl_image_set(dqi, x, y, ERIS_DQI_BP);
310 cpl_image_set(data, x, y, 1.);
316 for (y = ERIS_IFU_DETECTOR_BP_BORDER+1; y <= ERIS_IFU_DETECTOR_SIZE_Y-ERIS_IFU_DETECTOR_BP_BORDER; y++) {
318 for (x = 1; x <= ERIS_IFU_DETECTOR_BP_BORDER; x++) {
319 cpl_image_reject(data, x, y);
321 cpl_image_set(dqi, x, y, ERIS_DQI_BP);
324 cpl_image_set(data, x, y, 1.);
329 for (x = ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER+1;
330 x <= ERIS_IFU_DETECTOR_SIZE_X;
333 cpl_image_reject(data, x, y);
335 cpl_image_set(dqi, x, y, ERIS_DQI_BP);
338 cpl_image_set(data, x, y, 1.);
346 err = cpl_error_get_code();
382 int exposureCorrectionMode,
385 hdrl_image *image = NULL;
386 cpl_image *dataImage = NULL;
387 cpl_image *noiseImage = NULL;
388 cpl_propertylist *header = NULL;
389 ifsInstrument instrument = UNSET_INSTRUMENT;
393 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
397 dataImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, 0);
404 if ((exposureCorrectionMode & LINE_EXPOSURE_CORRECTION) != 0) {
409 if ((exposureCorrectionMode & COLUMN_EXPOSURE_CORRECTION) != 0) {
419 header = cpl_propertylist_load(filename, 0);
422 if (instrument == SPIFFI) {
426 }
else if (instrument == SPIFFIER) {
427 gain = cpl_propertylist_get_double(header, FHDR_DET_CHIP_GAIN);
428 ron = cpl_propertylist_get_double(header, FHDR_DET_CHIP_RON);
467 int exposureCorrectionMode,
468 hdrl_parameter *laCosmicParams,
471 cpl_mask *mask = NULL;
472 cpl_ensure(image, CPL_ERROR_NULL_INPUT, NULL);
473 cpl_ensure(laCosmicParams, CPL_ERROR_NULL_INPUT, NULL);
478 if ((exposureCorrectionMode & COSMIC_RAY_EXPOSURE_DETECTION) &&
479 (laCosmicParams != NULL)) {
481 cpl_msg_info(__func__,
"CRH count %lld",cpl_mask_count(mask));
523 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
525 const float satPixelValue = -45727232.0f;
526 const float satPixelLevel = satPixelValue + 1.f;
528 cpl_size nx = cpl_image_get_size_x(image);
529 cpl_size ny = cpl_image_get_size_y(image);
530 const float *data = cpl_image_get_data_float_const(image);
531 cpl_mask *mask = cpl_image_get_bpm(image);
532 cpl_binary *bp = cpl_mask_get_data(mask);
536 for (cpl_size iy=0; iy<ny; iy++) {
537 for (cpl_size ix=0; ix<nx; ix++) {
538 if (data[ix+iy*nx] <= satPixelLevel) {
540 cpl_image_add_scalar(dqi, ERIS_DQI_SAT);
543 bp[ix+iy*nx] = BAD_PIX;
548 cpl_image_set_bpm(image, mask);
551 cpl_msg_debug(__FUNCTION__,
"Found %d saturated pixels", nSat);
554 return CPL_ERROR_NONE;
580 cpl_error_code retVal = CPL_ERROR_NONE;
584 double *dData = NULL;
586 cpl_vector *borderVector = NULL;
592 const int borderSize = 4;
594 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
598 borderVector = cpl_vector_new(borderSize*2));
599 borderData = cpl_vector_get_data(borderVector);
601 nCols = cpl_image_get_size_x(image);
602 nRows = cpl_image_get_size_y(image);
603 if (nCols < borderSize*2) {
605 "CPL image too small");
607 type = cpl_image_get_type(image);
608 if (type == CPL_TYPE_FLOAT) {
609 fData = cpl_image_get_data_float(image);
610 }
else if (type == CPL_TYPE_DOUBLE) {
611 dData = cpl_image_get_data_double(image);
614 "CPL image type must be float or double");
617 for (cpl_size row=0; row<nRows; row++) {
618 for (
int col=0; col<4; col++) {
619 if (type == CPL_TYPE_FLOAT) {
620 borderData[col] = fData[col+row*nCols];
621 borderData[col+borderSize] = fData[nCols-1-col+row*nCols];
623 borderData[col] = dData[col+row*nCols];
624 borderData[col+borderSize] = dData[nCols-1-col+row*nCols];
627 borderMean = cpl_vector_get_mean(borderVector);
628 borderMedian = cpl_vector_get_median(borderVector);
629 if (type == CPL_TYPE_FLOAT) {
630 fborderMean = (float) borderMean;
631 fborderMedian = (float) borderMedian;
633 for (
int col=borderSize; col<(nCols-borderSize); col++) {
634 if (type == CPL_TYPE_FLOAT) {
635 fData[col+row*nCols] = fData[col+row*nCols] +
636 fborderMean - fborderMedian;
638 dData[col+row*nCols] = dData[col+row*nCols] +
639 borderMean - borderMedian;
647 retVal = cpl_error_get_code();
677 cpl_error_code retVal = CPL_ERROR_NONE;
681 double *dData = NULL;
683 cpl_vector *borderVector = NULL;
687 const int borderSize = 4;
689 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
693 borderVector = cpl_vector_new(borderSize*2));
694 borderData = cpl_vector_get_data(borderVector);
696 nCols = cpl_image_get_size_x(image);
697 nRows = cpl_image_get_size_y(image);
698 if (nRows < borderSize*2) {
700 "CPL image too small");
702 type = cpl_image_get_type(image);
703 if (type == CPL_TYPE_FLOAT) {
704 fData = cpl_image_get_data_float(image);
705 }
else if (type == CPL_TYPE_DOUBLE) {
706 dData = cpl_image_get_data_double(image);
709 "CPL image type must be float or double");
712 for (cpl_size col=0; col<nCols; col++) {
713 for (
int row=0; row<4; row++) {
714 if (type == CPL_TYPE_FLOAT) {
715 borderData[row] = fData[col + row*nCols];
716 borderData[row+borderSize] = fData[col+(nRows-row-1)*nCols];
718 borderData[row] = dData[col + row*nCols];
719 borderData[row+borderSize] = dData[col+(nRows-row-1)*nCols];
722 borderMedian = cpl_vector_get_median(borderVector);
723 if (type == CPL_TYPE_FLOAT) {
724 fborderMedian = (float) borderMedian;
726 for (
int row=borderSize; row<(nCols-borderSize); row++) {
727 if (type == CPL_TYPE_FLOAT) {
728 fData[col + row*nCols] = fData[col + row*nCols] -
731 dData[col + row*nCols] = dData[col + row*nCols] -
740 retVal = cpl_error_get_code();
772 const char *recipe_name,
773 hdrl_image *master_img,
774 const hdrl_imagelist *imglist_on,
775 cpl_mask **bpm2dMask,
776 cpl_mask **bpm3dMask)
778 cpl_error_code err = CPL_ERROR_NONE;
779 const cpl_parameter *param = NULL;
780 const char *bpmMethod = NULL;
781 char *parName = NULL;
782 hdrl_parameter *bpm2dParam = NULL;
783 hdrl_parameter *bpm3dParam = NULL;
784 cpl_image *bpm3dImg = NULL;
785 cpl_imagelist *bpm3dImageList = NULL;
786 cpl_mask *masterBpmMask = NULL;
788 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
789 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
790 cpl_ensure_code(master_img, CPL_ERROR_NULL_INPUT);
791 cpl_ensure_code(imglist_on, CPL_ERROR_NULL_INPUT);
792 cpl_ensure_code(bpm2dMask, CPL_ERROR_NULL_INPUT);
793 cpl_ensure_code(bpm3dMask, CPL_ERROR_NULL_INPUT);
797 parName = cpl_sprintf(
"eris.%s.bpm.method", recipe_name);
798 param = cpl_parameterlist_find_const(pl, parName);
801 bpmMethod = cpl_parameter_get_string(param);
803 if (strpbrk(bpmMethod,
"2") != NULL)
805 parName = cpl_sprintf(
"eris.%s.2dBadPix", recipe_name);
809 cpl_msg_info(cpl_func,
"Generating 2D bad pixel mask...");
814 if (strpbrk(bpmMethod,
"3") != NULL)
816 parName = cpl_sprintf(
"eris.%s.3dBadPix", recipe_name);
820 cpl_msg_info(cpl_func,
"Generate 3D bad pixel mask");
824 int imglistCnt = (int) cpl_imagelist_get_size(bpm3dImageList);
825 cpl_image *patternImg = NULL;
827 bpm3dImg = cpl_image_duplicate(cpl_imagelist_get(bpm3dImageList, 0));
828 patternImg = cpl_image_duplicate(
829 cpl_imagelist_get(bpm3dImageList, 0));
830 for (
int i = 1; i < imglistCnt; i++) {
831 cpl_image *tmpImg = cpl_imagelist_get(bpm3dImageList, i);
832 cpl_image_add(bpm3dImg, tmpImg);
833 cpl_image_multiply_scalar(tmpImg, (
double) (1<<i));
834 cpl_image_add(patternImg, tmpImg);
837 parName = cpl_sprintf(
"eris.%s.product_depth", recipe_name);
838 param = cpl_parameterlist_find_const(pl, parName);
840 int pd = cpl_parameter_get_int(param);
842 if (pd >= PD_DEBUG) {
843 cpl_image_save(bpm3dImg,ERIS_IFU_PRO_DARK_DBG_3D_FN
".fits",
844 CPL_TYPE_INT, NULL, CPL_IO_CREATE);
845 cpl_image_save(patternImg,ERIS_IFU_PRO_DARK_DBG_3D_FN
".fits",
846 CPL_TYPE_INT, NULL, CPL_IO_EXTEND);
849 *bpm3dMask = cpl_mask_threshold_image_create(bpm3dImg,
850 1.5, imglistCnt + 0.5);
851 cpl_image_delete(patternImg);
854 cpl_msg_info(cpl_func,
"Generating master bad pixel mask...");
856 if (*bpm2dMask != NULL) {
857 cpl_msg_info(cpl_func,
"Number of 2D bad pixels: %lld",
858 cpl_mask_count(*bpm2dMask));
859 cpl_mask_or(masterBpmMask, *bpm2dMask);
861 if (bpm3dImg != NULL) {
862 cpl_msg_info(cpl_func,
"Number of 3D bad pixels: %lld",
863 cpl_mask_count(*bpm3dMask));
864 cpl_mask_or(masterBpmMask, *bpm3dMask);
870 err = cpl_error_get_code();
872 cpl_mask_delete(*bpm2dMask);
873 cpl_mask_delete(*bpm3dMask);
907 const cpl_polynomial *poly_u,
908 const cpl_polynomial *poly_v)
910 hdrl_image *hdrlOutImg = NULL;
911 const cpl_image *inImgData = NULL,
913 cpl_image *outImgData = NULL,
915 cpl_vector *profile = NULL;
918 cpl_type type = CPL_TYPE_UNSPECIFIED;
920 cpl_ensure(hdrlInImg, CPL_ERROR_NULL_INPUT, NULL);
921 cpl_ensure(poly_u, CPL_ERROR_NULL_INPUT, NULL);
922 cpl_ensure(poly_v, CPL_ERROR_NULL_INPUT, NULL);
929 nx = cpl_image_get_size_x(inImgData);
930 ny = cpl_image_get_size_y(inImgData);
931 type = cpl_image_get_type(inImgData);
934 outImgData = cpl_image_new(nx, ny, type);
935 outImgErr = cpl_image_new(nx, ny, type);
936 profile = cpl_vector_new(CPL_KERNEL_DEF_SAMPLES);
937 cpl_vector_fill_kernel_profile(profile, CPL_KERNEL_TANH,
938 CPL_KERNEL_DEF_WIDTH);
939 cpl_image_warp_polynomial(outImgData, inImgData,
941 profile, CPL_KERNEL_DEF_WIDTH,
942 profile, CPL_KERNEL_DEF_WIDTH);
943 cpl_image_warp_polynomial(outImgErr, inImgErr,
945 profile, CPL_KERNEL_DEF_WIDTH,
946 profile, CPL_KERNEL_DEF_WIDTH);
954 double *ptmpImg = cpl_image_get_data(tmpImg);
955 cpl_mask *tmpMask = cpl_image_get_bpm(tmpImg);
956 cpl_binary *ptmpMask = cpl_mask_get_data(tmpMask);
961 for (
int y = 0; y < h; y++) {
962 for (
int x = 0; x < w; x++) {
963 if (ptmpImg[y*w+x] == 0) {
964 ptmpMask[y*w+x] = BAD_PIX;
1003 cpl_parameterlist *pl,
1004 const char *recipename)
1007 cpl_ensure_code(pl,CPL_ERROR_NULL_INPUT);
1008 cpl_ensure_code(recipename,CPL_ERROR_NULL_INPUT);
1011 cpl_error_code retVal = CPL_ERROR_NONE;
1012 cpl_parameter *p = NULL;
1014 char *recname_full = NULL;
1015 hdrl_parameter *p_hdrl = NULL;
1016 cpl_parameterlist *tmp_pl = NULL;
1020 recname_full = cpl_sprintf(
"eris.%s", recipename);
1021 pName = cpl_sprintf(
"%s.%s", recname_full,
"instrument");
1022 p = cpl_parameter_new_enum(pName, CPL_TYPE_STRING,
1023 "Specifies the VLT instrument "
1024 "{ERIS,SINFONI,NONE}",
1025 recname_full,
"ERIS", 3,
"ERIS",
1027 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"instrument");
1028 cpl_parameterlist_append(pl, p);
1034 pName = cpl_sprintf(
"%s.%s", recname_full,
"product_depth");
1035 p = cpl_parameter_new_value(pName, CPL_TYPE_INT,
1036 "Specifies the product output depth "
1037 "(>0 for auxiliary products)",
1039 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1041 cpl_parameterlist_append(pl, p);
1045 pName = cpl_sprintf(
"%s.%s", recname_full,
"line_corr");
1046 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
1047 "If TRUE raw exposure image line "
1048 "correction will be applied",
1049 recname_full, CPL_FALSE);
1050 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1052 cpl_parameterlist_append(pl, p);
1056 pName = cpl_sprintf(
"%s.%s", recname_full,
"col_corr");
1057 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
1058 "If TRUE raw exposure image column "
1059 "correction will be applied",
1060 recname_full, CPL_FALSE);
1061 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1063 cpl_parameterlist_append(pl, p);
1067 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_corr");
1068 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
1069 "If TRUE raw exposure image cosmic ray "
1070 "hit correction will be applied",
1071 recname_full, CPL_FALSE);
1072 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1074 cpl_parameterlist_append(pl, p);
1078 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_detection");
1079 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
1080 "If TRUE raw exposure image cosmic ray "
1081 "hit detection will be applied",
1082 recname_full, CPL_FALSE);
1083 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1085 cpl_parameterlist_append(pl, p);
1095 pName = cpl_sprintf(
"%s.%s", recname_full,
"pixel_saturation");
1096 p = cpl_parameter_new_value(pName, CPL_TYPE_DOUBLE,
1097 "Pixel saturation level ",
1098 recname_full, 18000.);
1099 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pixel_saturation");
1101 cpl_parameterlist_append(pl, p);
1110 retVal = cpl_error_get_code();
1134 if(stdParams != NULL) {
1170 const cpl_parameterlist *parlist,
1171 const char *recipename,
1172 struct stdParamStruct *stdParams)
1175 cpl_ensure_code(parlist,CPL_ERROR_NULL_INPUT);
1176 cpl_ensure_code(recipename,CPL_ERROR_NULL_INPUT);
1177 cpl_ensure_code(stdParams,CPL_ERROR_NULL_INPUT);
1179 cpl_error_code retVal = CPL_ERROR_NONE;
1180 cpl_parameter *p = NULL;
1182 const char *instrument = NULL;
1183 char *recname_full = NULL;
1184 char *tmp_str = NULL;
1188 recname_full = cpl_sprintf(
"eris.%s", recipename);
1189 pName = cpl_sprintf(
"%s.%s", recname_full,
"product_depth");
1190 stdParams->productDepth = cpl_parameter_get_int(
1191 cpl_parameterlist_find_const(parlist, pName));
1195 stdParams->rawImageCorrectionMask = 0;
1196 pName = cpl_sprintf(
"%s.%s", recname_full,
"line_corr");
1197 stdParams->line_corr = cpl_parameter_get_bool(
1198 cpl_parameterlist_find_const(parlist, pName));
1201 if (stdParams->line_corr) {
1202 stdParams->rawImageCorrectionMask |= LINE_EXPOSURE_CORRECTION;
1205 pName = cpl_sprintf(
"%s.%s", recname_full,
"col_corr");
1206 stdParams->col_corr = cpl_parameter_get_bool(
1207 cpl_parameterlist_find_const(parlist, pName));
1210 if (stdParams->col_corr) {
1211 stdParams->rawImageCorrectionMask |= COLUMN_EXPOSURE_CORRECTION;
1214 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_corr");
1215 stdParams->crh_corr = cpl_parameter_get_bool(
1216 cpl_parameterlist_find_const(parlist, pName));
1219 if (stdParams->crh_corr) {
1220 stdParams->rawImageCorrectionMask |= COSMIC_RAY_EXPOSURE_CORRECTION;
1223 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_detection");
1224 stdParams->crh_det = cpl_parameter_get_bool(
1225 cpl_parameterlist_find_const(parlist, pName));
1228 if (stdParams->crh_det) {
1229 stdParams->rawImageCorrectionMask |= COSMIC_RAY_EXPOSURE_DETECTION;
1232 tmp_str = cpl_sprintf(
"%s.crh", recname_full);
1234 stdParams->crh_detection =
1239 pName = cpl_sprintf(
"%s.%s", recname_full,
"instrument");
1240 instrument = cpl_parameter_get_string(
1241 cpl_parameterlist_find_const(parlist, pName));
1244 if (strcmp(instrument,
"SINFONI") == 0) {
1245 stdParams->instrument = SPIFFI;
1247 else if(strcmp(instrument,
"ERIS") == 0) {
1248 stdParams->instrument = SPIFFIER;
1251 stdParams->instrument = OTHER_INSTRUMENT;
1257 retVal = cpl_error_get_code();
1283 const char* recname)
1285 char *tmp_str1 = NULL,
1286 *recname_full = NULL;
1287 cpl_error_code err = CPL_ERROR_NONE;
1288 cpl_parameter *p = NULL;
1290 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1291 cpl_ensure_code(recname, CPL_ERROR_NULL_INPUT);
1295 recname_full = cpl_sprintf(
"eris.%s", recname);
1298 tmp_str1 = cpl_sprintf(
"%s%s%s",
"eris.", recname,
".instrument");
1299 p = cpl_parameter_new_enum(tmp_str1, CPL_TYPE_STRING,
1300 "Specifies the VLT instrument {ERIS,SINFONI,NONE}",
1301 recname_full, INSTRUMENT_ERIS, 3,
1302 INSTRUMENT_ERIS, INSTRUMENT_SINFONI, INSTRUMENT_NONE);
1303 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"instrument");
1304 cpl_parameterlist_append(pl, p);
1308 tmp_str1 = cpl_sprintf(
"%s%s%s",
"eris.", recname,
".product_depth");
1309 p = cpl_parameter_new_value(tmp_str1, CPL_TYPE_INT,
1310 "Specifies the product output depth "
1311 "instrument (>0 for auxillary products)",
1313 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1315 cpl_parameterlist_append(pl, p);
1321 err = cpl_error_get_code();
1351 const char* recname)
1353 char *tmp_str = NULL,
1354 *recname_full = NULL,
1355 *tmp_def_meth = NULL,
1357 cpl_error_code err = CPL_ERROR_NONE;
1358 cpl_parameterlist *tmp_pl = NULL;
1359 cpl_parameter *p = NULL;
1360 hdrl_parameter *p_hdrl1 = NULL;
1361 hdrl_parameter *p_hdrl2 = NULL;
1363 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1364 cpl_ensure_code(recname, CPL_ERROR_NULL_INPUT);
1368 recname_full = cpl_sprintf(
"eris.%s", recname);
1371 if (strcmp(recname, REC_NAME_DISTORTION) == 0) {
1372 tmp_def_meth = cpl_sprintf(
"2d");
1373 tmp_meth = cpl_sprintf(
"LEGENDRE");
1375 tmp_def_meth = cpl_sprintf(
"2d3d");
1376 tmp_meth = cpl_sprintf(
"FILTER");
1380 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".bpm.method");
1381 p = cpl_parameter_new_enum(tmp_str, CPL_TYPE_STRING,
1382 "Specifies the VLT instrument {2d,3d,2d3d}",
1383 recname_full, tmp_def_meth, 4,
1384 "2d",
"3d",
"2d3d",
"none");
1385 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"bpm.method");
1386 cpl_parameterlist_append(pl, p);
1390 hdrl_parameter * mode_def =
1395 "collapse",
"MEDIAN",
1396 p_hdrl1, p_hdrl2, mode_def);
1404 10., 10., 3, CPL_FILTER_MEDIAN, CPL_BORDER_NOP, 5, 5);
1408 10., 10., 3, 20, 20, 10, 10, 2, 2);
1412 "2dBadPix", tmp_meth,
1421 HDRL_BPM_3D_THRESHOLD_RELATIVE);
1423 "3dBadPix", p_hdrl1);
1431 err = cpl_error_get_code();
1466 const char* recname)
1468 cpl_error_code err = CPL_ERROR_NONE;
1469 hdrl_parameter *p_hdrl = NULL;
1470 cpl_parameterlist *tmp_pl = NULL;
1471 cpl_parameter *p = NULL;
1472 char *tmp_str = NULL,
1473 *recname_full = NULL;
1475 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1479 recname_full = cpl_sprintf(
"eris.%s", recname);
1482 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".mode");
1484 p = cpl_parameter_new_enum(tmp_str,
1486 "Mode of flat-calculation",
1489 flatModes[0],flatModes[1],flatModes[2]);
1510 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"flat.mode");
1511 cpl_parameterlist_append(pl, p);
1531 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmin1");
1532 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1533 "Fixed Pattern Noise: qc_fpn_xmin1",
1535 512, 1, ERIS_IFU_DETECTOR_SIZE_X);
1536 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmin1");
1537 cpl_parameterlist_append(pl, p);
1540 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmax1");
1541 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1542 "Fixed Pattern Noise: qc_fpn_xmax1",
1544 1536, 1, ERIS_IFU_DETECTOR_SIZE_X);
1545 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmax1");
1546 cpl_parameterlist_append(pl, p);
1549 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymin1");
1550 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1551 "Fixed Pattern Noise: qc_fpn_ymin1",
1553 512, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1554 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymin1");
1555 cpl_parameterlist_append(pl, p);
1558 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymax1");
1559 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1560 "Fixed Pattern Noise: qc_fpn_ymax1",
1562 1536, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1563 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymax1");
1564 cpl_parameterlist_append(pl, p);
1567 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmin2");
1568 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1569 "Fixed Pattern Noise: qc_fpn_xmin2",
1571 1350, 1, ERIS_IFU_DETECTOR_SIZE_X);
1572 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmin2");
1573 cpl_parameterlist_append(pl, p);
1576 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmax2");
1577 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1578 "Fixed Pattern Noise: qc_fpn_xmax2",
1580 1390, 1, ERIS_IFU_DETECTOR_SIZE_X);
1581 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmax2");
1582 cpl_parameterlist_append(pl, p);
1585 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymin2");
1586 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1587 "Fixed Pattern Noise: qc_fpn_ymin2",
1589 1000, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1590 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymin2");
1591 cpl_parameterlist_append(pl, p);
1594 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymax2");
1595 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1596 "Fixed Pattern Noise: qc_fpn_ymax2",
1598 1200, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1599 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymax2");
1600 cpl_parameterlist_append(pl, p);
1606 err = cpl_error_get_code();
1633 const cpl_vector *y,
1636 cpl_vector *fit_par = NULL;
1637 cpl_polynomial *poly_coeff = NULL;
1638 cpl_matrix *x_matrix = NULL;
1639 double *pfit_par = NULL,
1645 const cpl_boolean sampsym = CPL_FALSE;
1647 cpl_ensure(x, CPL_ERROR_NULL_INPUT, NULL);
1648 cpl_ensure(y, CPL_ERROR_NULL_INPUT, NULL);
1649 cpl_ensure(degree > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1657 poly_coeff = cpl_polynomial_new(1);
1661 px = cpl_vector_get_data((cpl_vector*)x);
1662 x_matrix = cpl_matrix_wrap(1, cpl_vector_get_size(x), px);
1667 cpl_polynomial_fit(poly_coeff,
1676 cpl_matrix_unwrap(x_matrix); x_matrix = NULL;
1682 fit_par = cpl_vector_new(degree + 1);
1683 pfit_par = cpl_vector_get_data(fit_par);
1685 for(k = 0; k <= degree; k++) {
1686 pfit_par[k] = cpl_polynomial_get_coeff(poly_coeff, &k);
1694 cpl_matrix_unwrap(x_matrix); x_matrix = NULL;
1695 cpl_polynomial_delete(poly_coeff); poly_coeff = NULL;
1722 cpl_polynomial *fit = NULL;
1723 cpl_matrix *x_pos = NULL;
1724 cpl_vector *values = NULL;
1727 fit = cpl_polynomial_new(1);
1728 if (nPoints < degree+1) {
1729 for (cpl_size power=0; power<=degree; power++) {
1730 cpl_polynomial_set_coeff(fit, &power, 0.0);
1734 x_pos = cpl_matrix_wrap(1, nPoints, xdata);
1735 values = cpl_vector_wrap(nPoints, ydata);
1736 const cpl_boolean sampsym = CPL_FALSE;
1737 const cpl_size maxdeg1d = degree;
1738 cpl_polynomial_fit(fit, x_pos, &sampsym, values, NULL,
1739 CPL_FALSE, NULL, &maxdeg1d);
1746 cpl_polynomial_delete(fit);
1751 if (x_pos != NULL) {
1752 cpl_matrix_unwrap(x_pos);
1754 if (values != NULL) {
1755 cpl_vector_unwrap(values);
1795 const int interType)
1797 cpl_error_code retVal = CPL_ERROR_NONE;
1798 const gsl_interp_type *interp;
1800 gsl_error_handler_t * gslErrorHandler;
1801 double *xIn2 = NULL;
1802 double *yIn2 = NULL;
1807 gslErrorHandler = gsl_set_error_handler_off();
1809 ASSURE((xIn != NULL && yIn != NULL && xOut != NULL &&
1811 CPL_ERROR_NULL_INPUT,
"no NULL pointer allowed for arrays");
1812 ASSURE((nIn > 0 && nOut > 0),
1813 CPL_ERROR_ILLEGAL_INPUT,
"invalid array size input");
1815 remove_2nans(nIn, xIn, yIn, &nIn2, &xIn2, &yIn2);
1817 gsl_interp_accel *acc = gsl_interp_accel_alloc();
1819 const int nPoints = interType;
1820 interp = gsl_interp_polynomial;
1821 gsl_interp *workspace = gsl_interp_alloc(interp, nPoints);
1823 for (
int rx = 0; rx < nOut; rx++) {
1824 sx = gsl_interp_accel_find(acc, xIn2, nIn2,
1826 if (sx > (
size_t) (nPoints / 2)) {
1829 if (((cpl_size) sx + nPoints) > nIn2) {
1830 sx = nIn2 - nPoints;
1832 gsl_status = gsl_interp_init(workspace, &xIn2[sx], &yIn2[sx],
1834 if (gsl_status != 0) {
1836 "GSL interpolation routine returned error %d: %s",
1837 gsl_status, gsl_strerror(gsl_status));
1839 gsl_status = gsl_interp_eval_e(workspace, &xIn2[sx],
1840 &yIn2[sx], xOut[rx], acc, &yOut[rx]);
1841 if (gsl_status != 0 && gsl_status != GSL_EDOM) {
1843 "GSL interpolation routine returned error %d: %s",
1844 gsl_status, gsl_strerror(gsl_status));
1847 gsl_interp_free(workspace);
1849 switch (interType) {
1851 interp = gsl_interp_linear;
1854 interp = gsl_interp_cspline;
1857 interp = gsl_interp_cspline_periodic;
1860 interp = gsl_interp_akima;
1863 interp = gsl_interp_akima_periodic;
1866 interp = gsl_interp_steffen;
1870 "unknown interpolation type %d", interType);
1872 gsl_interp *workspace = gsl_interp_alloc(interp, nIn2);
1874 gsl_status = gsl_interp_init(workspace, xIn2, yIn2, nIn2);
1875 if (gsl_status != 0) {
1877 "GSL interpolation routine returned error %d: %s",
1878 gsl_status, gsl_strerror(gsl_status));
1880 for (
int rx = 0; rx < nOut; rx++) {
1881 gsl_status = gsl_interp_eval_e(workspace, xIn2, yIn2,
1882 xOut[rx], acc, &yOut[rx]);
1883 if (gsl_status != 0 && gsl_status != GSL_EDOM) {
1885 "GSL interpolation routine returned error %d: %s",
1886 gsl_status, gsl_strerror(gsl_status));
1889 gsl_interp_free(workspace);
1891 gsl_interp_accel_free(acc);
1896 retVal = cpl_error_get_code();
1898 gsl_set_error_handler(gslErrorHandler);
1905void remove_2nans(
int size_in,
double *xin,
double *yin,
int *size_out,
double **xout,
double **yout) {
1909 for (
int i = 0; i < size_in; i++) {
1916 *size_out = no_valids;
1917 *xout = (
double *) cpl_calloc(no_valids,
sizeof(
double));
1918 *yout = (
double *) cpl_calloc(no_valids,
sizeof(
double));
1921 for (
int i = 0; i < size_in; i++) {
1924 (*xout)[ox] = xin[i];
1925 (*yout)[ox] = yin[i];
1981 cpl_size nr_valid_pix = 0,
1984 const double *pimg = NULL;
1988 cpl_ensure(image != NULL, CPL_ERROR_NULL_INPUT, -1.0);
1992 nx = cpl_image_get_size_x(image);
1993 ny = cpl_image_get_size_y(image);
1994 nr_valid_pix = nx * ny;
1997 pimg = cpl_image_get_data_double_const(image));
1999 for (
int j = 0; j < ny; j++) {
2000 for (
int i = 0; i < nx; i++) {
2001 if (isnan(pimg[i+j*nx]))
2004 sum += pimg[i+j*nx];
2007 mean = sum / (double) nr_valid_pix;
2018cpl_error_code eris_ifu_line_gauss_fit(
2019 const cpl_vector *yIn,
2022 struct gaussParStruct *gaussPar)
2024 cpl_error_code retVal = CPL_ERROR_NONE;
2033 const double *pyVec = NULL;
2034 cpl_vector *xVec = NULL,
2038 cpl_ensure_code(yIn, CPL_ERROR_NULL_INPUT);
2039 cpl_ensure_code(cpl_vector_get_size(yIn) == ERIS_IFU_DETECTOR_SIZE,
2040 CPL_ERROR_ILLEGAL_INPUT);
2046 yIn2 = cpl_vector_duplicate(yIn));
2048 yInData = cpl_vector_get_data(yIn2));
2051 start = center-(range/2);
2057 }
else if (start >= ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER) {
2058 start = ERIS_IFU_DETECTOR_SIZE_X - ERIS_IFU_DETECTOR_BP_BORDER - 1;
2060 if (start + range >= ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER) {
2063 range = ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER - start;
2066 yVec = cpl_vector_wrap(range, &yInData[start]));
2069 pyVec = cpl_vector_get_data_const(yVec));
2072 xVec = cpl_vector_new(range));
2074 pxVec = cpl_vector_get_data(xVec));
2075 for (
int ix=0; ix<range; ix++) {
2076 pxVec[ix] = start + ix;
2079 cpl_errorstate prestate = cpl_errorstate_get();
2080 retVal = cpl_vector_fit_gaussian(xVec, NULL, yVec, NULL, CPL_FIT_ALL,
2081 &pos, &sigma, &area, &offset, &mserr, NULL, NULL);
2083 gaussPar->errorCode = (int) retVal;
2084 if ((retVal == CPL_ERROR_NONE) ||
2085 ((retVal == CPL_ERROR_CONTINUE) &&
2086 (((pos > 2042) && (pos < ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER)) ||
2087 ((pos < 7) && (pos >= ERIS_IFU_DETECTOR_BP_BORDER)))))
2090 if (retVal == CPL_ERROR_CONTINUE) {
2091 cpl_errorstate_set(prestate);
2092 retVal = CPL_ERROR_NONE;
2096 gaussPar->sigma = sigma;
2097 gaussPar->area = area;
2098 gaussPar->offset = offset;
2099 gaussPar->peak = area / sqrt(2 * CPL_MATH_PI * sigma * sigma);
2100 gaussPar->mse = mserr;
2104 gaussPar->sigma = 0.;
2105 gaussPar->area = 0.;
2106 gaussPar->offset = 0.;
2107 gaussPar->peak = 0.;
2111 if (retVal == CPL_ERROR_CONTINUE) {
2113 cpl_errorstate_set(prestate);
2114 retVal = CPL_ERROR_NONE;
2116 const int decrement = 9;
2117 if ((range - decrement) > 2) {
2120 cpl_vector_unwrap(yVec); yVec = NULL;
2128 if (range <= GAUSS_PAR_RANGE_MAX) {
2129 for (
int ix=0; ix<range; ix++) {
2130 gaussPar->range = range;
2131 gaussPar->xdata[ix] = pxVec[ix];
2132 gaussPar->ydata[ix] = pyVec[ix];
2139 gaussPar->sigma = 0.;
2140 gaussPar->area = 0.;
2141 gaussPar->offset = 0.;
2142 gaussPar->peak = 0.;
2144 if (range <= GAUSS_PAR_RANGE_MAX) {
2145 for (
int ix=0; ix<range; ix++) {
2146 gaussPar->range = 0.;
2147 gaussPar->xdata[ix] = 0.;
2148 gaussPar->ydata[ix] = 0.;
2155 cpl_vector_unwrap(yVec); yVec = NULL;
2172 double *pcollapsed_img = NULL;
2173 cpl_vector *tmp = NULL,
2175 cpl_image *collapsed_img = NULL;
2177 cpl_ensure(chunk_center > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2178 cpl_ensure(height > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2180 cpl_ensure(height % 2 == 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2181 cpl_ensure(img, CPL_ERROR_NULL_INPUT, NULL);
2185 nx = cpl_image_get_size_x(img);
2186 ny = cpl_image_get_size_y(img);
2189 chunk_bottom = chunk_center - height/2;
2190 chunk_top = ny - (chunk_center + height/2);
2193 collapsed_img = cpl_image_collapse_median_create(img, 0,
2196 pcollapsed_img = cpl_image_get_data_double(collapsed_img);
2197 tmp = cpl_vector_wrap(nx, pcollapsed_img);
2198 collapsed = cpl_vector_duplicate(tmp);
2206 cpl_vector_unwrap(tmp); tmp = NULL;
2218 double *pcollapsed_img = NULL;
2219 cpl_vector *tmp = NULL,
2221 cpl_image *collapsed_img = NULL;
2223 cpl_ensure(img, CPL_ERROR_NULL_INPUT, NULL);
2227 nx = cpl_image_get_size_x(img);
2230 collapsed_img = cpl_image_collapse_median_create(img, 0, 0, 0);
2231 pcollapsed_img = cpl_image_get_data_double(collapsed_img);
2232 tmp = cpl_vector_wrap(nx, pcollapsed_img);
2233 collapsed = cpl_vector_duplicate(tmp);
2241 cpl_vector_unwrap(tmp); tmp = NULL;
2261 double *left_edge_pos,
2262 double *right_edge_pos,
2266 cpl_error_code err = CPL_ERROR_NONE;
2267 cpl_vector *x_left = NULL,
2273 *profile_x_v = NULL,
2277 double *px_left = NULL,
2286 const double *pprofile_x = NULL;
2287 cpl_size maxpos = 0,
2291 cpl_ensure_code(profile_x, CPL_ERROR_NULL_INPUT);
2292 cpl_ensure_code(left_edge_pos, CPL_ERROR_NULL_INPUT);
2293 cpl_ensure_code(right_edge_pos, CPL_ERROR_NULL_INPUT);
2297 s = (int) cpl_image_get_size_x(profile_x);
2304 x_left = cpl_vector_new(s2);
2305 x_right = cpl_vector_new(s2);
2306 y_left = cpl_vector_new(s2);
2307 y_right = cpl_vector_new(s2);
2309 px_left = cpl_vector_get_data(x_left);
2310 px_right = cpl_vector_get_data(x_right);
2311 py_left = cpl_vector_get_data(y_left);
2312 py_right = cpl_vector_get_data(y_right);
2317 tmpVector = cpl_vector_new_from_image_row(profile_x,1);
2318 profile_x_v = cpl_vector_filter_lowpass_create(
2319 tmpVector, CPL_LOWPASS_LINEAR, 2);
2321 pprofile_x = cpl_vector_get_data(profile_x_v);
2323 for (
int i = 0; i < s2; i++) {
2326 py_left[i] = pprofile_x[i+1] - pprofile_x[i];
2327 px_right[i] = i+s2+1;
2329 py_right[i] = pprofile_x[i+s2-1] - pprofile_x[i+s2];
2332 y_left2 = cpl_vector_filter_lowpass_create(y_left,
2333 CPL_LOWPASS_GAUSSIAN, 5);
2334 y_right2 = cpl_vector_filter_lowpass_create(y_right,
2335 CPL_LOWPASS_GAUSSIAN, 5);
2338 if (cpl_vector_get_min(y_left2) < -100) {
2339 maxpos = cpl_vector_get_maxpos(y_left2);
2340 cpl_vector *tmpVec = cpl_vector_extract(y_left2,0,maxpos,1);
2341 minpos = cpl_vector_get_minpos(tmpVec);
2342 cpl_vector_delete(tmpVec);
2343 median = cpl_vector_get_median_const(y_left2);
2344 if (cpl_vector_get(y_left2,minpos < median-10.)) {
2347 inBetween = (cpl_size) (((
double) (maxpos + minpos) / 2.) + .5);
2348 for (cpl_size vx = 0; vx < inBetween; vx++) {
2349 if (cpl_vector_get(y_left2, vx) < -10.) {
2350 cpl_vector_set(y_left2, vx, median);
2353 minpos = cpl_vector_get_minpos(y_left2);
2354 if (cpl_vector_get(y_left2,minpos < median-10.)) {
2355 inBetween = (cpl_size) (((
double)(maxpos + minpos) / 2.) + .5);
2356 for (cpl_size vx = inBetween;
2357 vx < cpl_vector_get_size(y_left2); vx++) {
2358 if (cpl_vector_get(y_left2, vx) < -10.) {
2359 cpl_vector_set(y_left2, vx, median);
2367 cpl_size vSize = cpl_vector_get_size(y_left2);
2368 for (cpl_size vx = maxpos; vx < vSize; vx++) {
2369 if (cpl_vector_get(y_left2, vx) < -10.) {
2370 cpl_vector_set(y_left2, vx, median);
2378 if (cpl_vector_get_min(y_right2) < -100.) {
2379 maxpos = cpl_vector_get_maxpos(y_right2);
2380 minpos = cpl_vector_get_minpos(y_right2);
2381 median = cpl_vector_get_median_const(y_right2);
2382 if (maxpos < minpos) {
2384 cpl_size vSize = cpl_vector_get_size(y_right2);
2385 inBetween = (cpl_size) (((
double) (maxpos + minpos) / 2.) + .5);
2386 for (cpl_size vx = inBetween; vx < vSize; vx++) {
2387 if (cpl_vector_get(y_left2, vx) < -10.) {
2388 cpl_vector_set(y_left2, vx, median);
2393 for (cpl_size vx=maxpos; vx<=0; vx--) {
2394 if (cpl_vector_get(y_right2, vx) < -10.) {
2395 cpl_vector_set(y_right2, vx, median);
2402 if (productDepth & 8) {
2403 cpl_propertylist *pl = NULL;
2405 "eris_ifu_distortion_dbg_slitpos_profile_left_xy.fits",
2408 "eris_ifu_distortion_dbg_slitpos_profile_left_xy.fits",
2411 "eris_ifu_distortion_dbg_slitpos_profile_right_xy.fits",
2414 "eris_ifu_distortion_dbg_slitpos_profile_right_xy.fits",
2417 "eris_ifu_distortion_dbg_slitpos_profile_left_xy.fits",
2420 "eris_ifu_distortion_dbg_slitpos_profile_right_xy.fits",
2429 if (err != CPL_ERROR_NONE) {
2445 *left_edge_pos = llx + x0 + 0.5;
2450 if (err != CPL_ERROR_NONE) {
2466 *right_edge_pos = llx + x0 - 0.5;
2473 err = cpl_error_get_code();
2474 if (err != CPL_ERROR_EOL) {
2500 hdrl_image *badPixelMaskImg)
2502 cpl_mask *m = cpl_mask_new(3,3);
2503 cpl_mask_set(m, 1,2, BAD_PIX);
2504 cpl_mask_set(m, 2,1, BAD_PIX);
2505 cpl_mask_set(m, 2,3, BAD_PIX);
2506 cpl_mask_set(m, 3,2, BAD_PIX);
2514 cpl_image_power(variance, 2.0);
2515 cpl_image_filter_mask(filterd_error, variance, m, CPL_FILTER_AVERAGE,
2517 cpl_image_delete(variance);
2519 cpl_image_divide_scalar(filterd_error, (
double) cpl_mask_count(m));
2520 cpl_image_power(filterd_error, 0.5);
2522 if (badPixelMaskImg == NULL){
2524 cpl_image *badPixelMaskImg2 = cpl_image_cast(badPixelMaskImg1, CPL_TYPE_DOUBLE);
2525 cpl_image_delete(badPixelMaskImg1);
2531 badPixelMaskImg2, NULL, 1, 1);
2533 cpl_image_delete(badPixelMaskImg2);
2538 cpl_mask_set(m, 2, 2, GOOD_PIX);
2544 double corr_thesh = 0.4;
2545 cpl_image_threshold(filtered_mask, corr_thesh, corr_thesh, 1.0, 0.0);
2547 cpl_mask *corr_mask_t = cpl_mask_threshold_image_create(filtered_mask,-0.1, 0.1);
2548 cpl_mask_not(corr_mask_t);
2549 cpl_msg_info(__func__,
"No. bad pixels to be corrected: %d", (
int)cpl_mask_count(corr_mask_t));
2550 cpl_image_multiply(filterd_image, filtered_mask);
2551 cpl_image_multiply(filterd_error, filtered_mask);
2553 cpl_image_subtract_scalar(filtered_mask, 1);
2554 cpl_image_multiply_scalar(filtered_mask, -1);
2555 cpl_mask *corr_mask = cpl_mask_threshold_image_create(filtered_mask,-0.1, 0.1);
2556 cpl_mask_not(corr_mask);
2557 cpl_msg_debug(__func__,
"No. bad pixels before correction: %d", (
int)cpl_image_count_rejected(
hdrl_image_get_image(himg)));
2560 cpl_msg_debug(__func__,
"No. bad pixels after correction: %d", (
int)cpl_image_count_rejected(
hdrl_image_get_image(himg)));
2570 cpl_image_delete(filtered_mask);
2571 cpl_image_delete(filterd_error);
2572 cpl_image_delete(filterd_image);
2573 cpl_mask_delete(corr_mask);
2574 cpl_mask_delete(corr_mask_t);
2576 return cpl_error_get_code();
2579double eris_ifu_auto_derot_corr(
double alt,
double rot)
2581 double za = 90.0-alt;
2582 double offset = -0.000379*za + 0.000500*pow(za,2);
2583 double alt_scale = 0.0467*za - 0.000265*pow(za,2);
2584 double pi = 3.14159265359;
2585 double derot_corr = offset + alt_scale * cos((rot - 15.4)*pi/180);
ifsInstrument eris_ifu_get_instrument(const cpl_propertylist *header)
Return the used instrument of the FITS file header.
cpl_error_code eris_ifu_fit_gauss(const cpl_vector *x, const cpl_vector *y, double *x0, double *sigma, double *area, double *offset)
Fit Gaussian to find peak center and width.
#define BRK_WITH_ERROR(code)
Set a new CPL error, and exit the try-block.
#define ASSURE(condition, error,...)
error handling macro (from fors-pipeline)
#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 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_exposure_line_correction(cpl_image *image)
Perform line/row correction on raw detector image.
cpl_polynomial * eris_ifu_1d_polynomial_fit(int nPoints, double *xdata, double *ydata, int degree)
Fit a 1D polynomial to arrays of data points.
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_error_code eris_ifu_1d_interpolation(double *xIn, double *yIn, int nIn, double *xOut, double *yOut, int nOut, const int interType)
Perform 1D interpolation using GSL routines.
cpl_error_code eris_ifu_bpm_correction(hdrl_image *himg, hdrl_image *badPixelMaskImg)
eris_ifu_bpm_correction
cpl_error_code eris_parlist_config_add_flat(cpl_parameterlist *pl, const char *recname)
Add flat field configuration parameters to parameter list.
cpl_error_code eris_parlist_config_add_all_recipes(cpl_parameterlist *pl, const char *recname)
Add common configuration parameters for all recipes.
cpl_vector * eris_ifu_calc_centers_collapse_chunk(const cpl_image *img, int chunk_center, int height)
hdrl_image * eris_ifu_load_exposure_frame(const cpl_frame *frame, int exposureCorrectionMode, cpl_image *dqi)
Load a raw detector exposure from a frame with optional corrections.
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_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 2D and/or 3D bad pixel masks using HDRL algorithms.
cpl_error_code eris_ifu_add_badpix_border(cpl_image *data, cpl_boolean add_ones, cpl_image *dqi)
Flag detector border pixels as bad in image and optionally set to 1.
cpl_error_code eris_ifu_add_std_params(cpl_parameterlist *pl, const char *recipename)
Add standard recipe parameters to a parameter list.
cpl_error_code eris_ifu_exposure_column_correction(cpl_image *image)
Perform column correction on raw detector image.
cpl_vector * eris_ifu_polyfit_1d(const cpl_vector *x, const cpl_vector *y, const int degree)
Fit a 1D polynomial to vector data.
cpl_mask * eris_ifu_detect_crh(hdrl_image *image, int exposureCorrectionMode, hdrl_parameter *laCosmicParams, bool maskImage)
Detect cosmic ray hits using LA-cosmic algorithm.
cpl_error_code eris_parlist_config_add_bpm(cpl_parameterlist *pl, const char *recname)
Add bad pixel mask configuration parameters to parameter list.
cpl_image * eris_ifu_calc_noise_map(const cpl_image *data, double gain, double readnoise)
Calculate a noise map for detector data.
cpl_error_code eris_ifu_saturation_detection(cpl_image *image, cpl_image *dqi)
Detect saturated pixels and flag them as bad.
hdrl_image * eris_ifu_load_exposure_file(const char *filename, int exposureCorrectionMode, cpl_image *dqi)
Load a raw detector exposure from file with corrections and noise.
hdrl_imagelist * eris_ifu_load_exposure_frameset(const cpl_frameset *frameset, int exposureCorrectionMode)
Load raw exposures from a frameset with optional corrections.
ifsInstrument eris_ifu_get_instrument_frame(cpl_frame *frame)
Get instrument identifier from a CPL frame.
double eris_ifu_image_get_mean(const cpl_image *image)
**
hdrl_image * eris_ifu_raw_hdrl_image(const cpl_image *cplImage)
Create an HDRL image from a CPL image with calculated noise.
hdrl_image * eris_ifu_warp_polynomial_image(const hdrl_image *hdrlInImg, const cpl_polynomial *poly_u, const cpl_polynomial *poly_v)
Warp an HDRL image using 2D polynomial transformations.
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.
cpl_error_code eris_ifu_parameterlist_append_list(cpl_parameterlist *p1, const cpl_parameterlist *p2)
Append a parameterlist to another one.
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_vector_dbg(const cpl_vector *vec, const char *filename, int create, const cpl_propertylist *pl)
Save vector for debugging (quick, no DFS overhead)
void eris_ifu_free_parameter(cpl_parameter **item)
Free memory and set pointer to null.
void eris_ifu_free_imagelist(cpl_imagelist **item)
Free memory and set pointer to null.
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.
void eris_ifu_free_parameterlist(cpl_parameterlist **item)
Free memory and set pointer to null.
void eris_ifu_free_hdrl_parameter(hdrl_parameter **item)
Free memory and set pointer to null.
int eris_ifu_is_nan_or_inf(double A)
Checks if a value is nan, inf or -inf.
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
cpl_mask * hdrl_bpm_2d_compute(const hdrl_image *img_in, const hdrl_parameter *params)
Detect bad pixels on a single image with an iterative process.
hdrl_parameter * hdrl_bpm_2d_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse parameter list to create input parameters for the BPM_2D.
hdrl_parameter * hdrl_bpm_2d_parameter_create_filtersmooth(double kappa_low, double kappa_high, int maxiter, cpl_filter_mode filter, cpl_border_mode border, int smooth_x, int smooth_y)
Creates BPM_2D Parameters object for HDRL_BPM_2D_FILTERSMOOTH.
cpl_parameterlist * hdrl_bpm_2d_parameter_create_parlist(const char *base_context, const char *prefix, const char *method_def, const hdrl_parameter *filtersmooth_def, const hdrl_parameter *legendresmooth_def)
Create parameter list for the BPM_2D computation.
hdrl_parameter * hdrl_bpm_2d_parameter_create_legendresmooth(double kappa_low, double kappa_high, int maxiter, int steps_x, int steps_y, int filter_size_x, int filter_size_y, int order_x, int order_y)
Creates BPM_2D Parameters object for HDRL_BPM_2D_LEGENDRESMOOTH.
cpl_parameterlist * hdrl_bpm_3d_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create a parameter list for the BPM_3D computation.
cpl_imagelist * hdrl_bpm_3d_compute(const hdrl_imagelist *imglist, const hdrl_parameter *params)
detect bad pixels on a stack of identical images
hdrl_parameter * hdrl_bpm_3d_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse a parameterlist to create input parameters for the BPM_3D.
hdrl_parameter * hdrl_bpm_3d_parameter_create(double kappa_low, double kappa_high, hdrl_bpm_3d_method method)
Creates BPM Parameters object for the imagelist method.
hdrl_parameter * hdrl_collapse_sigclip_parameter_create(double kappa_low, double kappa_high, int niter)
create a parameter object for sigclipped mean
hdrl_parameter * hdrl_collapse_mode_parameter_create(double histo_min, double histo_max, double bin_size, hdrl_mode_type mode_method, cpl_size error_niter)
create a parameter object for the mode
cpl_parameterlist * hdrl_collapse_parameter_create_parlist(const char *base_context, const char *prefix, const char *method_def, hdrl_parameter *sigclip_def, hdrl_parameter *minmax_def, hdrl_parameter *mode_def)
Create parameters for the collapse.
hdrl_parameter * hdrl_collapse_minmax_parameter_create(double nlow, double nhigh)
create a parameter object for min-max rejected mean
hdrl_parameter * hdrl_flat_parameter_create(cpl_size filter_size_x, cpl_size filter_size_y, hdrl_flat_method method)
Creates FLAT Parameters object.
cpl_parameterlist * hdrl_flat_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create a parameter list for the FLAT computation.
cpl_error_code hdrl_image_reject_from_mask(hdrl_image *self, const cpl_mask *map)
set bpm of hdrl_image
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl 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
const cpl_image * hdrl_image_get_error_const(const hdrl_image *himg)
get error as cpl image
cpl_error_code hdrl_image_insert(hdrl_image *self, const cpl_image *image, const cpl_image *error, cpl_size xpos, cpl_size ypos)
Copy cpl images into an hdrl 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
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
cpl_parameterlist * hdrl_lacosmic_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create parameter list for the LaCosmic computation.
cpl_mask * hdrl_lacosmic_edgedetect(const hdrl_image *ima_in, const hdrl_parameter *params)
Detect bad-pixels / cosmic-rays on a single image.
hdrl_parameter * hdrl_lacosmic_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse parameterlist to create input parameters for the LaCosmic.
hdrl_parameter * hdrl_lacosmic_parameter_create(double sigma_lim, double f_lim, int max_iter)
Creates LaCosmic parameters object.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter