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"
60 ifsInstrument instrument = UNSET_INSTRUMENT;
61 const char *filename = NULL;
62 cpl_propertylist *header = NULL;
70 filename = cpl_frame_get_filename(frame);
71 header = cpl_propertylist_load(filename, 0);
77 instrument = UNSET_INSTRUMENT;
98 hdrl_image *image = NULL;
99 cpl_image *noiseImage = NULL;
101 cpl_ensure(cplImage, CPL_ERROR_NULL_INPUT, NULL);
112 cpl_image_delete(noiseImage);
134 cpl_image *noiseImg = NULL;
136 cpl_ensure(data, CPL_ERROR_NULL_INPUT, NULL);
137 cpl_ensure(gain > 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
138 cpl_ensure(readnoise >= 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
143 noiseImg = cpl_image_duplicate(data));
147 cpl_image_abs(noiseImg);
148 cpl_image_multiply_scalar(noiseImg, gain);
149 cpl_image_add_scalar(noiseImg, readnoise * readnoise);
150 cpl_image_power(noiseImg, 0.5);
151 cpl_image_divide_scalar(noiseImg, gain);
174 int exposureCorrectionMode) {
175 hdrl_imagelist *imageList = NULL;
176 hdrl_image *tmpImage = NULL;
177 const cpl_frame *frame = NULL;
179 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
183 frameCnt = cpl_frameset_get_size(frameset);
185 for (cpl_size ix=0; ix<frameCnt; ix++) {
186 frame = cpl_frameset_get_position_const(frameset, ix);
188 frame, exposureCorrectionMode, NULL);
214 int exposureCorrectionMode,
217 hdrl_image *image = NULL;
218 const char *filename;
220 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, NULL);
223 filename = cpl_frame_get_filename(frame);
225 filename, exposureCorrectionMode, dqi);
249 cpl_error_code err = CPL_ERROR_NONE;
253 cpl_ensure_code(data, CPL_ERROR_NULL_INPUT);
257 for (x = 1; x <= ERIS_IFU_DETECTOR_SIZE_X; x++) {
260 y <= ERIS_IFU_DETECTOR_BP_BORDER;
263 cpl_image_reject(data, x, y);
265 cpl_image_set(dqi, x, y, ERIS_DQI_BP);
268 cpl_image_set(data, x, y, 1.);
273 for (y = ERIS_IFU_DETECTOR_SIZE_Y-ERIS_IFU_DETECTOR_BP_BORDER+1;
274 y <= ERIS_IFU_DETECTOR_SIZE_Y;
277 cpl_image_reject(data, x, y);
279 cpl_image_set(dqi, x, y, ERIS_DQI_BP);
282 cpl_image_set(data, x, y, 1.);
288 for (y = ERIS_IFU_DETECTOR_BP_BORDER+1; y <= ERIS_IFU_DETECTOR_SIZE_Y-ERIS_IFU_DETECTOR_BP_BORDER; y++) {
290 for (x = 1; x <= ERIS_IFU_DETECTOR_BP_BORDER; x++) {
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 (x = ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER+1;
302 x <= ERIS_IFU_DETECTOR_SIZE_X;
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.);
318 err = cpl_error_get_code();
363 int exposureCorrectionMode,
366 hdrl_image *image = NULL;
367 cpl_image *dataImage = NULL;
368 cpl_image *noiseImage = NULL;
369 cpl_propertylist *header = NULL;
370 ifsInstrument instrument = UNSET_INSTRUMENT;
374 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
378 dataImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, 0);
385 if ((exposureCorrectionMode & LINE_EXPOSURE_CORRECTION) != 0) {
390 if ((exposureCorrectionMode & COLUMN_EXPOSURE_CORRECTION) != 0) {
400 header = cpl_propertylist_load(filename, 0);
403 if (instrument == SPIFFI) {
407 }
else if (instrument == SPIFFIER) {
408 gain = cpl_propertylist_get_double(header, FHDR_DET_CHIP_GAIN);
409 ron = cpl_propertylist_get_double(header, FHDR_DET_CHIP_RON);
447 int exposureCorrectionMode,
448 hdrl_parameter *laCosmicParams,
451 cpl_mask *mask = NULL;
452 cpl_ensure(image, CPL_ERROR_NULL_INPUT, NULL);
453 cpl_ensure(laCosmicParams, CPL_ERROR_NULL_INPUT, NULL);
458 if ((exposureCorrectionMode & COSMIC_RAY_EXPOSURE_DETECTION) &&
459 (laCosmicParams != NULL)) {
461 cpl_msg_info(__func__,
"CRH count %lld",cpl_mask_count(mask));
496 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
498 const float satPixelValue = -45727232.0f;
499 const float satPixelLevel = satPixelValue + 1.f;
501 cpl_size nx = cpl_image_get_size_x(image);
502 cpl_size ny = cpl_image_get_size_y(image);
503 const float *data = cpl_image_get_data_float_const(image);
504 cpl_mask *mask = cpl_image_get_bpm(image);
505 cpl_binary *bp = cpl_mask_get_data(mask);
509 for (cpl_size iy=0; iy<ny; iy++) {
510 for (cpl_size ix=0; ix<nx; ix++) {
511 if (data[ix+iy*nx] <= satPixelLevel) {
513 cpl_image_add_scalar(dqi, ERIS_DQI_SAT);
516 bp[ix+iy*nx] = BAD_PIX;
521 cpl_image_set_bpm(image, mask);
524 cpl_msg_debug(__FUNCTION__,
"Found %d saturated pixels", nSat);
527 return CPL_ERROR_NONE;
550 cpl_error_code retVal = CPL_ERROR_NONE;
554 double *dData = NULL;
556 cpl_vector *borderVector = NULL;
562 const int borderSize = 4;
564 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
568 borderVector = cpl_vector_new(borderSize*2));
569 borderData = cpl_vector_get_data(borderVector);
571 nCols = cpl_image_get_size_x(image);
572 nRows = cpl_image_get_size_y(image);
573 if (nCols < borderSize*2) {
575 "CPL image too small");
577 type = cpl_image_get_type(image);
578 if (type == CPL_TYPE_FLOAT) {
579 fData = cpl_image_get_data_float(image);
580 }
else if (type == CPL_TYPE_DOUBLE) {
581 dData = cpl_image_get_data_double(image);
584 "CPL image type must be float or double");
587 for (cpl_size row=0; row<nRows; row++) {
588 for (
int col=0; col<4; col++) {
589 if (type == CPL_TYPE_FLOAT) {
590 borderData[col] = fData[col+row*nCols];
591 borderData[col+borderSize] = fData[nCols-1-col+row*nCols];
593 borderData[col] = dData[col+row*nCols];
594 borderData[col+borderSize] = dData[nCols-1-col+row*nCols];
597 borderMean = cpl_vector_get_mean(borderVector);
598 borderMedian = cpl_vector_get_median(borderVector);
599 if (type == CPL_TYPE_FLOAT) {
600 fborderMean = (float) borderMean;
601 fborderMedian = (float) borderMedian;
603 for (
int col=borderSize; col<(nCols-borderSize); col++) {
604 if (type == CPL_TYPE_FLOAT) {
605 fData[col+row*nCols] = fData[col+row*nCols] +
606 fborderMean - fborderMedian;
608 dData[col+row*nCols] = dData[col+row*nCols] +
609 borderMean - borderMedian;
617 retVal = cpl_error_get_code();
646 cpl_error_code retVal = CPL_ERROR_NONE;
650 double *dData = NULL;
652 cpl_vector *borderVector = NULL;
656 const int borderSize = 4;
658 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
662 borderVector = cpl_vector_new(borderSize*2));
663 borderData = cpl_vector_get_data(borderVector);
665 nCols = cpl_image_get_size_x(image);
666 nRows = cpl_image_get_size_y(image);
667 if (nRows < borderSize*2) {
669 "CPL image too small");
671 type = cpl_image_get_type(image);
672 if (type == CPL_TYPE_FLOAT) {
673 fData = cpl_image_get_data_float(image);
674 }
else if (type == CPL_TYPE_DOUBLE) {
675 dData = cpl_image_get_data_double(image);
678 "CPL image type must be float or double");
681 for (cpl_size col=0; col<nCols; col++) {
682 for (
int row=0; row<4; row++) {
683 if (type == CPL_TYPE_FLOAT) {
684 borderData[row] = fData[col + row*nCols];
685 borderData[row+borderSize] = fData[col+(nRows-row-1)*nCols];
687 borderData[row] = dData[col + row*nCols];
688 borderData[row+borderSize] = dData[col+(nRows-row-1)*nCols];
691 borderMedian = cpl_vector_get_median(borderVector);
692 if (type == CPL_TYPE_FLOAT) {
693 fborderMedian = (float) borderMedian;
695 for (
int row=borderSize; row<(nCols-borderSize); row++) {
696 if (type == CPL_TYPE_FLOAT) {
697 fData[col + row*nCols] = fData[col + row*nCols] -
700 dData[col + row*nCols] = dData[col + row*nCols] -
709 retVal = cpl_error_get_code();
728 const char *recipe_name,
729 hdrl_image *master_img,
730 const hdrl_imagelist *imglist_on,
731 cpl_mask **bpm2dMask,
732 cpl_mask **bpm3dMask)
734 cpl_error_code err = CPL_ERROR_NONE;
735 const cpl_parameter *param = NULL;
736 const char *bpmMethod = NULL;
737 char *parName = NULL;
738 hdrl_parameter *bpm2dParam = NULL;
739 hdrl_parameter *bpm3dParam = NULL;
740 cpl_image *bpm3dImg = NULL;
741 cpl_imagelist *bpm3dImageList = NULL;
742 cpl_mask *masterBpmMask = NULL;
744 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
745 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
746 cpl_ensure_code(master_img, CPL_ERROR_NULL_INPUT);
747 cpl_ensure_code(imglist_on, CPL_ERROR_NULL_INPUT);
748 cpl_ensure_code(bpm2dMask, CPL_ERROR_NULL_INPUT);
749 cpl_ensure_code(bpm3dMask, CPL_ERROR_NULL_INPUT);
753 parName = cpl_sprintf(
"eris.%s.bpm.method", recipe_name);
754 param = cpl_parameterlist_find_const(pl, parName);
757 bpmMethod = cpl_parameter_get_string(param);
759 if (strpbrk(bpmMethod,
"2") != NULL)
761 parName = cpl_sprintf(
"eris.%s.2dBadPix", recipe_name);
765 cpl_msg_info(cpl_func,
"Generating 2D bad pixel mask...");
770 if (strpbrk(bpmMethod,
"3") != NULL)
772 parName = cpl_sprintf(
"eris.%s.3dBadPix", recipe_name);
776 cpl_msg_info(cpl_func,
"Generate 3D bad pixel mask");
780 int imglistCnt = (int) cpl_imagelist_get_size(bpm3dImageList);
781 cpl_image *patternImg = NULL;
783 bpm3dImg = cpl_image_duplicate(cpl_imagelist_get(bpm3dImageList, 0));
784 patternImg = cpl_image_duplicate(
785 cpl_imagelist_get(bpm3dImageList, 0));
786 for (
int i = 1; i < imglistCnt; i++) {
787 cpl_image *tmpImg = cpl_imagelist_get(bpm3dImageList, i);
788 cpl_image_add(bpm3dImg, tmpImg);
789 cpl_image_multiply_scalar(tmpImg, (
double) (1<<i));
790 cpl_image_add(patternImg, tmpImg);
793 parName = cpl_sprintf(
"eris.%s.product_depth", recipe_name);
794 param = cpl_parameterlist_find_const(pl, parName);
796 int pd = cpl_parameter_get_int(param);
798 if (pd >= PD_DEBUG) {
799 cpl_image_save(bpm3dImg,ERIS_IFU_PRO_DARK_DBG_3D_FN
".fits",
800 CPL_TYPE_INT, NULL, CPL_IO_CREATE);
801 cpl_image_save(patternImg,ERIS_IFU_PRO_DARK_DBG_3D_FN
".fits",
802 CPL_TYPE_INT, NULL, CPL_IO_EXTEND);
805 *bpm3dMask = cpl_mask_threshold_image_create(bpm3dImg,
806 1.5, imglistCnt + 0.5);
807 cpl_image_delete(patternImg);
810 cpl_msg_info(cpl_func,
"Generating master bad pixel mask...");
812 if (*bpm2dMask != NULL) {
813 cpl_msg_info(cpl_func,
"Number of 2D bad pixels: %lld",
814 cpl_mask_count(*bpm2dMask));
815 cpl_mask_or(masterBpmMask, *bpm2dMask);
817 if (bpm3dImg != NULL) {
818 cpl_msg_info(cpl_func,
"Number of 3D bad pixels: %lld",
819 cpl_mask_count(*bpm3dMask));
820 cpl_mask_or(masterBpmMask, *bpm3dMask);
826 err = cpl_error_get_code();
828 cpl_mask_delete(*bpm2dMask);
829 cpl_mask_delete(*bpm3dMask);
854 const cpl_polynomial *poly_u,
855 const cpl_polynomial *poly_v)
857 hdrl_image *hdrlOutImg = NULL;
858 const cpl_image *inImgData = NULL,
860 cpl_image *outImgData = NULL,
862 cpl_vector *profile = NULL;
865 cpl_type type = CPL_TYPE_UNSPECIFIED;
867 cpl_ensure(hdrlInImg, CPL_ERROR_NULL_INPUT, NULL);
868 cpl_ensure(poly_u, CPL_ERROR_NULL_INPUT, NULL);
869 cpl_ensure(poly_v, CPL_ERROR_NULL_INPUT, NULL);
876 nx = cpl_image_get_size_x(inImgData);
877 ny = cpl_image_get_size_y(inImgData);
878 type = cpl_image_get_type(inImgData);
881 outImgData = cpl_image_new(nx, ny, type);
882 outImgErr = cpl_image_new(nx, ny, type);
883 profile = cpl_vector_new(CPL_KERNEL_DEF_SAMPLES);
884 cpl_vector_fill_kernel_profile(profile, CPL_KERNEL_TANH,
885 CPL_KERNEL_DEF_WIDTH);
886 cpl_image_warp_polynomial(outImgData, inImgData,
888 profile, CPL_KERNEL_DEF_WIDTH,
889 profile, CPL_KERNEL_DEF_WIDTH);
890 cpl_image_warp_polynomial(outImgErr, inImgErr,
892 profile, CPL_KERNEL_DEF_WIDTH,
893 profile, CPL_KERNEL_DEF_WIDTH);
901 double *ptmpImg = cpl_image_get_data(tmpImg);
902 cpl_mask *tmpMask = cpl_image_get_bpm(tmpImg);
903 cpl_binary *ptmpMask = cpl_mask_get_data(tmpMask);
908 for (
int y = 0; y < h; y++) {
909 for (
int x = 0; x < w; x++) {
910 if (ptmpImg[y*w+x] == 0) {
911 ptmpMask[y*w+x] = BAD_PIX;
927cpl_error_code eris_ifu_add_std_params(
928 cpl_parameterlist *pl,
929 const char *recipename)
932 cpl_ensure_code(pl,CPL_ERROR_NULL_INPUT);
933 cpl_ensure_code(recipename,CPL_ERROR_NULL_INPUT);
936 cpl_error_code retVal = CPL_ERROR_NONE;
937 cpl_parameter *p = NULL;
939 char *recname_full = NULL;
940 hdrl_parameter *p_hdrl = NULL;
941 cpl_parameterlist *tmp_pl = NULL;
945 recname_full = cpl_sprintf(
"eris.%s", recipename);
946 pName = cpl_sprintf(
"%s.%s", recname_full,
"instrument");
947 p = cpl_parameter_new_enum(pName, CPL_TYPE_STRING,
948 "Specifies the VLT instrument "
949 "{ERIS,SINFONI,NONE}",
950 recname_full,
"ERIS", 3,
"ERIS",
952 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"instrument");
953 cpl_parameterlist_append(pl, p);
959 pName = cpl_sprintf(
"%s.%s", recname_full,
"product_depth");
960 p = cpl_parameter_new_value(pName, CPL_TYPE_INT,
961 "Specifies the product output depth "
962 "(>0 for auxiliary products)",
964 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
966 cpl_parameterlist_append(pl, p);
970 pName = cpl_sprintf(
"%s.%s", recname_full,
"line_corr");
971 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
972 "If TRUE raw exposure image line "
973 "correction will be applied",
974 recname_full, CPL_FALSE);
975 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
977 cpl_parameterlist_append(pl, p);
981 pName = cpl_sprintf(
"%s.%s", recname_full,
"col_corr");
982 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
983 "If TRUE raw exposure image column "
984 "correction will be applied",
985 recname_full, CPL_FALSE);
986 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
988 cpl_parameterlist_append(pl, p);
992 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_corr");
993 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
994 "If TRUE raw exposure image cosmic ray "
995 "hit correction will be applied",
996 recname_full, CPL_FALSE);
997 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
999 cpl_parameterlist_append(pl, p);
1003 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_detection");
1004 p = cpl_parameter_new_value(pName, CPL_TYPE_BOOL,
1005 "If TRUE raw exposure image cosmic ray "
1006 "hit detection will be applied",
1007 recname_full, CPL_FALSE);
1008 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1010 cpl_parameterlist_append(pl, p);
1020 pName = cpl_sprintf(
"%s.%s", recname_full,
"pixel_saturation");
1021 p = cpl_parameter_new_value(pName, CPL_TYPE_DOUBLE,
1022 "Pixel saturation level ",
1023 recname_full, 18000.);
1024 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pixel_saturation");
1026 cpl_parameterlist_append(pl, p);
1035 retVal = cpl_error_get_code();
1046void eris_ifu_free_std_param(
struct stdParamStruct * stdParams) {
1048 if(stdParams != NULL) {
1057cpl_error_code eris_ifu_fetch_std_param(
1058 const cpl_parameterlist *parlist,
1059 const char *recipename,
1060 struct stdParamStruct *stdParams)
1063 cpl_ensure_code(parlist,CPL_ERROR_NULL_INPUT);
1064 cpl_ensure_code(recipename,CPL_ERROR_NULL_INPUT);
1065 cpl_ensure_code(stdParams,CPL_ERROR_NULL_INPUT);
1067 cpl_error_code retVal = CPL_ERROR_NONE;
1068 cpl_parameter *p = NULL;
1070 const char *instrument = NULL;
1071 char *recname_full = NULL;
1072 char *tmp_str = NULL;
1076 recname_full = cpl_sprintf(
"eris.%s", recipename);
1077 pName = cpl_sprintf(
"%s.%s", recname_full,
"product_depth");
1078 stdParams->productDepth = cpl_parameter_get_int(
1079 cpl_parameterlist_find_const(parlist, pName));
1083 stdParams->rawImageCorrectionMask = 0;
1084 pName = cpl_sprintf(
"%s.%s", recname_full,
"line_corr");
1085 stdParams->line_corr = cpl_parameter_get_bool(
1086 cpl_parameterlist_find_const(parlist, pName));
1089 if (stdParams->line_corr) {
1090 stdParams->rawImageCorrectionMask |= LINE_EXPOSURE_CORRECTION;
1093 pName = cpl_sprintf(
"%s.%s", recname_full,
"col_corr");
1094 stdParams->col_corr = cpl_parameter_get_bool(
1095 cpl_parameterlist_find_const(parlist, pName));
1098 if (stdParams->col_corr) {
1099 stdParams->rawImageCorrectionMask |= COLUMN_EXPOSURE_CORRECTION;
1102 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_corr");
1103 stdParams->crh_corr = cpl_parameter_get_bool(
1104 cpl_parameterlist_find_const(parlist, pName));
1107 if (stdParams->crh_corr) {
1108 stdParams->rawImageCorrectionMask |= COSMIC_RAY_EXPOSURE_CORRECTION;
1111 pName = cpl_sprintf(
"%s.%s", recname_full,
"crh_detection");
1112 stdParams->crh_det = cpl_parameter_get_bool(
1113 cpl_parameterlist_find_const(parlist, pName));
1116 if (stdParams->crh_det) {
1117 stdParams->rawImageCorrectionMask |= COSMIC_RAY_EXPOSURE_DETECTION;
1120 tmp_str = cpl_sprintf(
"%s.crh", recname_full);
1122 stdParams->crh_detection =
1127 pName = cpl_sprintf(
"%s.%s", recname_full,
"instrument");
1128 instrument = cpl_parameter_get_string(
1129 cpl_parameterlist_find_const(parlist, pName));
1132 if (strcmp(instrument,
"SINFONI") == 0) {
1133 stdParams->instrument = SPIFFI;
1135 else if(strcmp(instrument,
"ERIS") == 0) {
1136 stdParams->instrument = SPIFFIER;
1139 stdParams->instrument = OTHER_INSTRUMENT;
1145 retVal = cpl_error_get_code();
1156cpl_error_code eris_parlist_config_add_all_recipes(cpl_parameterlist *pl,
1157 const char* recname)
1159 char *tmp_str1 = NULL,
1160 *recname_full = NULL;
1161 cpl_error_code err = CPL_ERROR_NONE;
1162 cpl_parameter *p = NULL;
1164 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1165 cpl_ensure_code(recname, CPL_ERROR_NULL_INPUT);
1169 recname_full = cpl_sprintf(
"eris.%s", recname);
1172 tmp_str1 = cpl_sprintf(
"%s%s%s",
"eris.", recname,
".instrument");
1173 p = cpl_parameter_new_enum(tmp_str1, CPL_TYPE_STRING,
1174 "Specifies the VLT instrument {ERIS,SINFONI,NONE}",
1175 recname_full, INSTRUMENT_ERIS, 3,
1176 INSTRUMENT_ERIS, INSTRUMENT_SINFONI, INSTRUMENT_NONE);
1177 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"instrument");
1178 cpl_parameterlist_append(pl, p);
1182 tmp_str1 = cpl_sprintf(
"%s%s%s",
"eris.", recname,
".product_depth");
1183 p = cpl_parameter_new_value(tmp_str1, CPL_TYPE_INT,
1184 "Specifies the product output depth "
1185 "instrument (>0 for auxillary products)",
1187 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
1189 cpl_parameterlist_append(pl, p);
1195 err = cpl_error_get_code();
1205cpl_error_code eris_parlist_config_add_bpm(cpl_parameterlist *pl,
1206 const char* recname)
1208 char *tmp_str = NULL,
1209 *recname_full = NULL,
1210 *tmp_def_meth = NULL,
1212 cpl_error_code err = CPL_ERROR_NONE;
1213 cpl_parameterlist *tmp_pl = NULL;
1214 cpl_parameter *p = NULL;
1215 hdrl_parameter *p_hdrl1 = NULL;
1216 hdrl_parameter *p_hdrl2 = NULL;
1218 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1219 cpl_ensure_code(recname, CPL_ERROR_NULL_INPUT);
1223 recname_full = cpl_sprintf(
"eris.%s", recname);
1226 if (strcmp(recname, REC_NAME_DISTORTION) == 0) {
1227 tmp_def_meth = cpl_sprintf(
"2d");
1228 tmp_meth = cpl_sprintf(
"LEGENDRE");
1230 tmp_def_meth = cpl_sprintf(
"2d3d");
1231 tmp_meth = cpl_sprintf(
"FILTER");
1235 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".bpm.method");
1236 p = cpl_parameter_new_enum(tmp_str, CPL_TYPE_STRING,
1237 "Specifies the VLT instrument {2d,3d,2d3d}",
1238 recname_full, tmp_def_meth, 4,
1239 "2d",
"3d",
"2d3d",
"none");
1240 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"bpm.method");
1241 cpl_parameterlist_append(pl, p);
1245 hdrl_parameter * mode_def =
1250 "collapse",
"MEDIAN",
1251 p_hdrl1, p_hdrl2, mode_def);
1259 10., 10., 3, CPL_FILTER_MEDIAN, CPL_BORDER_NOP, 5, 5);
1263 10., 10., 3, 20, 20, 10, 10, 2, 2);
1267 "2dBadPix", tmp_meth,
1276 HDRL_BPM_3D_THRESHOLD_RELATIVE);
1278 "3dBadPix", p_hdrl1);
1286 err = cpl_error_get_code();
1301cpl_error_code eris_parlist_config_add_flat(cpl_parameterlist *pl,
1302 const char* recname)
1304 cpl_error_code err = CPL_ERROR_NONE;
1305 hdrl_parameter *p_hdrl = NULL;
1306 cpl_parameterlist *tmp_pl = NULL;
1307 cpl_parameter *p = NULL;
1308 char *tmp_str = NULL,
1309 *recname_full = NULL;
1311 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1315 recname_full = cpl_sprintf(
"eris.%s", recname);
1318 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".mode");
1320 p = cpl_parameter_new_enum(tmp_str,
1322 "Mode of flat-calculation",
1325 flatModes[0],flatModes[1],flatModes[2]);
1346 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"flat.mode");
1347 cpl_parameterlist_append(pl, p);
1367 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmin1");
1368 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1369 "Fixed Pattern Noise: qc_fpn_xmin1",
1371 512, 1, ERIS_IFU_DETECTOR_SIZE_X);
1372 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmin1");
1373 cpl_parameterlist_append(pl, p);
1376 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmax1");
1377 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1378 "Fixed Pattern Noise: qc_fpn_xmax1",
1380 1536, 1, ERIS_IFU_DETECTOR_SIZE_X);
1381 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmax1");
1382 cpl_parameterlist_append(pl, p);
1385 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymin1");
1386 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1387 "Fixed Pattern Noise: qc_fpn_ymin1",
1389 512, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1390 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymin1");
1391 cpl_parameterlist_append(pl, p);
1394 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymax1");
1395 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1396 "Fixed Pattern Noise: qc_fpn_ymax1",
1398 1536, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1399 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymax1");
1400 cpl_parameterlist_append(pl, p);
1403 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmin2");
1404 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1405 "Fixed Pattern Noise: qc_fpn_xmin2",
1407 1350, 1, ERIS_IFU_DETECTOR_SIZE_X);
1408 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmin2");
1409 cpl_parameterlist_append(pl, p);
1412 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.xmax2");
1413 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1414 "Fixed Pattern Noise: qc_fpn_xmax2",
1416 1390, 1, ERIS_IFU_DETECTOR_SIZE_X);
1417 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_xmax2");
1418 cpl_parameterlist_append(pl, p);
1421 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymin2");
1422 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1423 "Fixed Pattern Noise: qc_fpn_ymin2",
1425 1000, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1426 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymin2");
1427 cpl_parameterlist_append(pl, p);
1430 tmp_str = cpl_sprintf(
"%s%s", recname_full,
".qc.fpn.ymax2");
1431 p = cpl_parameter_new_range(tmp_str, CPL_TYPE_INT,
1432 "Fixed Pattern Noise: qc_fpn_ymax2",
1434 1200, 1, ERIS_IFU_DETECTOR_SIZE_Y);
1435 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"qc_fpn_ymax2");
1436 cpl_parameterlist_append(pl, p);
1442 err = cpl_error_get_code();
1475 const cpl_vector *y,
1478 cpl_vector *fit_par = NULL;
1479 cpl_polynomial *poly_coeff = NULL;
1480 cpl_matrix *x_matrix = NULL;
1481 double *pfit_par = NULL,
1487 const cpl_boolean sampsym = CPL_FALSE;
1489 cpl_ensure(x, CPL_ERROR_NULL_INPUT, NULL);
1490 cpl_ensure(y, CPL_ERROR_NULL_INPUT, NULL);
1491 cpl_ensure(degree > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1499 poly_coeff = cpl_polynomial_new(1);
1503 px = cpl_vector_get_data((cpl_vector*)x);
1504 x_matrix = cpl_matrix_wrap(1, cpl_vector_get_size(x), px);
1509 cpl_polynomial_fit(poly_coeff,
1518 cpl_matrix_unwrap(x_matrix); x_matrix = NULL;
1524 fit_par = cpl_vector_new(degree + 1);
1525 pfit_par = cpl_vector_get_data(fit_par);
1527 for(k = 0; k <= degree; k++) {
1528 pfit_par[k] = cpl_polynomial_get_coeff(poly_coeff, &k);
1536 cpl_matrix_unwrap(x_matrix); x_matrix = NULL;
1537 cpl_polynomial_delete(poly_coeff); poly_coeff = NULL;
1542cpl_polynomial * eris_ifu_1d_polynomial_fit(
1548 cpl_polynomial *fit = NULL;
1549 cpl_matrix *x_pos = NULL;
1550 cpl_vector *values = NULL;
1553 fit = cpl_polynomial_new(1);
1554 if (nPoints < degree+1) {
1555 for (cpl_size power=0; power<=degree; power++) {
1556 cpl_polynomial_set_coeff(fit, &power, 0.0);
1560 x_pos = cpl_matrix_wrap(1, nPoints, xdata);
1561 values = cpl_vector_wrap(nPoints, ydata);
1562 const cpl_boolean sampsym = CPL_FALSE;
1563 const cpl_size maxdeg1d = degree;
1564 cpl_polynomial_fit(fit, x_pos, &sampsym, values, NULL,
1565 CPL_FALSE, NULL, &maxdeg1d);
1572 cpl_polynomial_delete(fit);
1577 if (x_pos != NULL) {
1578 cpl_matrix_unwrap(x_pos);
1580 if (values != NULL) {
1581 cpl_vector_unwrap(values);
1643 const int interType)
1645 cpl_error_code retVal = CPL_ERROR_NONE;
1646 const gsl_interp_type *interp;
1648 gsl_error_handler_t * gslErrorHandler;
1649 double *xIn2 = NULL;
1650 double *yIn2 = NULL;
1655 gslErrorHandler = gsl_set_error_handler_off();
1657 ASSURE((xIn != NULL && yIn != NULL && xOut != NULL &&
1659 CPL_ERROR_NULL_INPUT,
"no NULL pointer allowed for arrays");
1660 ASSURE((nIn > 0 && nOut > 0),
1661 CPL_ERROR_ILLEGAL_INPUT,
"invalid array size input");
1663 remove_2nans(nIn, xIn, yIn, &nIn2, &xIn2, &yIn2);
1665 gsl_interp_accel *acc = gsl_interp_accel_alloc();
1667 const int nPoints = interType;
1668 interp = gsl_interp_polynomial;
1669 gsl_interp *workspace = gsl_interp_alloc(interp, nPoints);
1671 for (
int rx = 0; rx < nOut; rx++) {
1672 sx = gsl_interp_accel_find(acc, xIn2, nIn2,
1674 if (sx > (
size_t) (nPoints / 2)) {
1677 if (((cpl_size) sx + nPoints) > nIn2) {
1678 sx = nIn2 - nPoints;
1680 gsl_status = gsl_interp_init(workspace, &xIn2[sx], &yIn2[sx],
1682 if (gsl_status != 0) {
1684 "GSL interpolation routine returned error %d: %s",
1685 gsl_status, gsl_strerror(gsl_status));
1687 gsl_status = gsl_interp_eval_e(workspace, &xIn2[sx],
1688 &yIn2[sx], xOut[rx], acc, &yOut[rx]);
1689 if (gsl_status != 0 && gsl_status != GSL_EDOM) {
1691 "GSL interpolation routine returned error %d: %s",
1692 gsl_status, gsl_strerror(gsl_status));
1695 gsl_interp_free(workspace);
1697 switch (interType) {
1699 interp = gsl_interp_linear;
1702 interp = gsl_interp_cspline;
1705 interp = gsl_interp_cspline_periodic;
1708 interp = gsl_interp_akima;
1711 interp = gsl_interp_akima_periodic;
1714 interp = gsl_interp_steffen;
1718 "unknown interpolation type %d", interType);
1720 gsl_interp *workspace = gsl_interp_alloc(interp, nIn2);
1722 gsl_status = gsl_interp_init(workspace, xIn2, yIn2, nIn2);
1723 if (gsl_status != 0) {
1725 "GSL interpolation routine returned error %d: %s",
1726 gsl_status, gsl_strerror(gsl_status));
1728 for (
int rx = 0; rx < nOut; rx++) {
1729 gsl_status = gsl_interp_eval_e(workspace, xIn2, yIn2,
1730 xOut[rx], acc, &yOut[rx]);
1731 if (gsl_status != 0 && gsl_status != GSL_EDOM) {
1733 "GSL interpolation routine returned error %d: %s",
1734 gsl_status, gsl_strerror(gsl_status));
1737 gsl_interp_free(workspace);
1739 gsl_interp_accel_free(acc);
1744 retVal = cpl_error_get_code();
1746 gsl_set_error_handler(gslErrorHandler);
1753void remove_2nans(
int size_in,
double *xin,
double *yin,
int *size_out,
double **xout,
double **yout) {
1757 for (
int i = 0; i < size_in; i++) {
1764 *size_out = no_valids;
1765 *xout = (
double *) cpl_calloc(no_valids,
sizeof(
double));
1766 *yout = (
double *) cpl_calloc(no_valids,
sizeof(
double));
1769 for (
int i = 0; i < size_in; i++) {
1772 (*xout)[ox] = xin[i];
1773 (*yout)[ox] = yin[i];
1829 cpl_size nr_valid_pix = 0,
1832 const double *pimg = NULL;
1836 cpl_ensure(image != NULL, CPL_ERROR_NULL_INPUT, -1.0);
1840 nx = cpl_image_get_size_x(image);
1841 ny = cpl_image_get_size_y(image);
1842 nr_valid_pix = nx * ny;
1845 pimg = cpl_image_get_data_double_const(image));
1847 for (
int j = 0; j < ny; j++) {
1848 for (
int i = 0; i < nx; i++) {
1849 if (isnan(pimg[i+j*nx]))
1852 sum += pimg[i+j*nx];
1855 mean = sum / (double) nr_valid_pix;
1866cpl_error_code eris_ifu_line_gauss_fit(
1867 const cpl_vector *yIn,
1870 struct gaussParStruct *gaussPar)
1872 cpl_error_code retVal = CPL_ERROR_NONE;
1881 const double *pyVec = NULL;
1882 cpl_vector *xVec = NULL,
1886 cpl_ensure_code(yIn, CPL_ERROR_NULL_INPUT);
1887 cpl_ensure_code(cpl_vector_get_size(yIn) == ERIS_IFU_DETECTOR_SIZE,
1888 CPL_ERROR_ILLEGAL_INPUT);
1894 yIn2 = cpl_vector_duplicate(yIn));
1896 yInData = cpl_vector_get_data(yIn2));
1899 start = center-(range/2);
1905 }
else if (start >= ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER) {
1906 start = ERIS_IFU_DETECTOR_SIZE_X - ERIS_IFU_DETECTOR_BP_BORDER - 1;
1908 if (start + range >= ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER) {
1911 range = ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER - start;
1914 yVec = cpl_vector_wrap(range, &yInData[start]));
1917 pyVec = cpl_vector_get_data_const(yVec));
1920 xVec = cpl_vector_new(range));
1922 pxVec = cpl_vector_get_data(xVec));
1923 for (
int ix=0; ix<range; ix++) {
1924 pxVec[ix] = start + ix;
1927 cpl_errorstate prestate = cpl_errorstate_get();
1928 retVal = cpl_vector_fit_gaussian(xVec, NULL, yVec, NULL, CPL_FIT_ALL,
1929 &pos, &sigma, &area, &offset, &mserr, NULL, NULL);
1931 gaussPar->errorCode = (int) retVal;
1932 if ((retVal == CPL_ERROR_NONE) ||
1933 ((retVal == CPL_ERROR_CONTINUE) &&
1934 (((pos > 2042) && (pos < ERIS_IFU_DETECTOR_SIZE_X-ERIS_IFU_DETECTOR_BP_BORDER)) ||
1935 ((pos < 7) && (pos >= ERIS_IFU_DETECTOR_BP_BORDER)))))
1938 if (retVal == CPL_ERROR_CONTINUE) {
1939 cpl_errorstate_set(prestate);
1940 retVal = CPL_ERROR_NONE;
1944 gaussPar->sigma = sigma;
1945 gaussPar->area = area;
1946 gaussPar->offset = offset;
1947 gaussPar->peak = area / sqrt(2 * CPL_MATH_PI * sigma * sigma);
1948 gaussPar->mse = mserr;
1952 gaussPar->sigma = 0.;
1953 gaussPar->area = 0.;
1954 gaussPar->offset = 0.;
1955 gaussPar->peak = 0.;
1959 if (retVal == CPL_ERROR_CONTINUE) {
1961 cpl_errorstate_set(prestate);
1962 retVal = CPL_ERROR_NONE;
1964 const int decrement = 9;
1965 if ((range - decrement) > 2) {
1968 cpl_vector_unwrap(yVec); yVec = NULL;
1976 if (range <= GAUSS_PAR_RANGE_MAX) {
1977 for (
int ix=0; ix<range; ix++) {
1978 gaussPar->range = range;
1979 gaussPar->xdata[ix] = pxVec[ix];
1980 gaussPar->ydata[ix] = pyVec[ix];
1987 gaussPar->sigma = 0.;
1988 gaussPar->area = 0.;
1989 gaussPar->offset = 0.;
1990 gaussPar->peak = 0.;
1992 if (range <= GAUSS_PAR_RANGE_MAX) {
1993 for (
int ix=0; ix<range; ix++) {
1994 gaussPar->range = 0.;
1995 gaussPar->xdata[ix] = 0.;
1996 gaussPar->ydata[ix] = 0.;
2003 cpl_vector_unwrap(yVec); yVec = NULL;
2020 double *pcollapsed_img = NULL;
2021 cpl_vector *tmp = NULL,
2023 cpl_image *collapsed_img = NULL;
2025 cpl_ensure(chunk_center > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2026 cpl_ensure(height > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2028 cpl_ensure(height % 2 == 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2029 cpl_ensure(img, CPL_ERROR_NULL_INPUT, NULL);
2033 nx = cpl_image_get_size_x(img);
2034 ny = cpl_image_get_size_y(img);
2037 chunk_bottom = chunk_center - height/2;
2038 chunk_top = ny - (chunk_center + height/2);
2041 collapsed_img = cpl_image_collapse_median_create(img, 0,
2044 pcollapsed_img = cpl_image_get_data_double(collapsed_img);
2045 tmp = cpl_vector_wrap(nx, pcollapsed_img);
2046 collapsed = cpl_vector_duplicate(tmp);
2054 cpl_vector_unwrap(tmp); tmp = NULL;
2066 double *pcollapsed_img = NULL;
2067 cpl_vector *tmp = NULL,
2069 cpl_image *collapsed_img = NULL;
2071 cpl_ensure(img, CPL_ERROR_NULL_INPUT, NULL);
2075 nx = cpl_image_get_size_x(img);
2078 collapsed_img = cpl_image_collapse_median_create(img, 0, 0, 0);
2079 pcollapsed_img = cpl_image_get_data_double(collapsed_img);
2080 tmp = cpl_vector_wrap(nx, pcollapsed_img);
2081 collapsed = cpl_vector_duplicate(tmp);
2089 cpl_vector_unwrap(tmp); tmp = NULL;
2109 double *left_edge_pos,
2110 double *right_edge_pos,
2114 cpl_error_code err = CPL_ERROR_NONE;
2115 cpl_vector *x_left = NULL,
2121 *profile_x_v = NULL,
2125 double *px_left = NULL,
2134 const double *pprofile_x = NULL;
2135 cpl_size maxpos = 0,
2139 cpl_ensure_code(profile_x, CPL_ERROR_NULL_INPUT);
2140 cpl_ensure_code(left_edge_pos, CPL_ERROR_NULL_INPUT);
2141 cpl_ensure_code(right_edge_pos, CPL_ERROR_NULL_INPUT);
2145 s = (int) cpl_image_get_size_x(profile_x);
2152 x_left = cpl_vector_new(s2);
2153 x_right = cpl_vector_new(s2);
2154 y_left = cpl_vector_new(s2);
2155 y_right = cpl_vector_new(s2);
2157 px_left = cpl_vector_get_data(x_left);
2158 px_right = cpl_vector_get_data(x_right);
2159 py_left = cpl_vector_get_data(y_left);
2160 py_right = cpl_vector_get_data(y_right);
2165 tmpVector = cpl_vector_new_from_image_row(profile_x,1);
2166 profile_x_v = cpl_vector_filter_lowpass_create(
2167 tmpVector, CPL_LOWPASS_LINEAR, 2);
2169 pprofile_x = cpl_vector_get_data(profile_x_v);
2171 for (
int i = 0; i < s2; i++) {
2174 py_left[i] = pprofile_x[i+1] - pprofile_x[i];
2175 px_right[i] = i+s2+1;
2177 py_right[i] = pprofile_x[i+s2-1] - pprofile_x[i+s2];
2180 y_left2 = cpl_vector_filter_lowpass_create(y_left,
2181 CPL_LOWPASS_GAUSSIAN, 5);
2182 y_right2 = cpl_vector_filter_lowpass_create(y_right,
2183 CPL_LOWPASS_GAUSSIAN, 5);
2186 if (cpl_vector_get_min(y_left2) < -100) {
2187 maxpos = cpl_vector_get_maxpos(y_left2);
2188 cpl_vector *tmpVec = cpl_vector_extract(y_left2,0,maxpos,1);
2189 minpos = cpl_vector_get_minpos(tmpVec);
2190 cpl_vector_delete(tmpVec);
2191 median = cpl_vector_get_median_const(y_left2);
2192 if (cpl_vector_get(y_left2,minpos < median-10.)) {
2195 inBetween = (cpl_size) (((
double) (maxpos + minpos) / 2.) + .5);
2196 for (cpl_size vx = 0; vx < inBetween; vx++) {
2197 if (cpl_vector_get(y_left2, vx) < -10.) {
2198 cpl_vector_set(y_left2, vx, median);
2201 minpos = cpl_vector_get_minpos(y_left2);
2202 if (cpl_vector_get(y_left2,minpos < median-10.)) {
2203 inBetween = (cpl_size) (((
double)(maxpos + minpos) / 2.) + .5);
2204 for (cpl_size vx = inBetween;
2205 vx < cpl_vector_get_size(y_left2); vx++) {
2206 if (cpl_vector_get(y_left2, vx) < -10.) {
2207 cpl_vector_set(y_left2, vx, median);
2215 cpl_size vSize = cpl_vector_get_size(y_left2);
2216 for (cpl_size vx = maxpos; vx < vSize; vx++) {
2217 if (cpl_vector_get(y_left2, vx) < -10.) {
2218 cpl_vector_set(y_left2, vx, median);
2226 if (cpl_vector_get_min(y_right2) < -100.) {
2227 maxpos = cpl_vector_get_maxpos(y_right2);
2228 minpos = cpl_vector_get_minpos(y_right2);
2229 median = cpl_vector_get_median_const(y_right2);
2230 if (maxpos < minpos) {
2232 cpl_size vSize = cpl_vector_get_size(y_right2);
2233 inBetween = (cpl_size) (((
double) (maxpos + minpos) / 2.) + .5);
2234 for (cpl_size vx = inBetween; vx < vSize; vx++) {
2235 if (cpl_vector_get(y_left2, vx) < -10.) {
2236 cpl_vector_set(y_left2, vx, median);
2241 for (cpl_size vx=maxpos; vx<=0; vx--) {
2242 if (cpl_vector_get(y_right2, vx) < -10.) {
2243 cpl_vector_set(y_right2, vx, median);
2250 if (productDepth & 8) {
2251 cpl_propertylist *pl = NULL;
2253 "eris_ifu_distortion_dbg_slitpos_profile_left_xy.fits",
2256 "eris_ifu_distortion_dbg_slitpos_profile_left_xy.fits",
2259 "eris_ifu_distortion_dbg_slitpos_profile_right_xy.fits",
2262 "eris_ifu_distortion_dbg_slitpos_profile_right_xy.fits",
2265 "eris_ifu_distortion_dbg_slitpos_profile_left_xy.fits",
2268 "eris_ifu_distortion_dbg_slitpos_profile_right_xy.fits",
2276 err = eris_ifu_fit_gauss(x_left, y_left2, &x0, &sigma, &area, &offset);
2277 if (err != CPL_ERROR_NONE) {
2293 *left_edge_pos = llx + x0 + 0.5;
2297 err = eris_ifu_fit_gauss(x_right, y_right2, &x0, &sigma, &area, &offset);
2298 if (err != CPL_ERROR_NONE) {
2314 *right_edge_pos = llx + x0 - 0.5;
2321 err = cpl_error_get_code();
2322 if (err != CPL_ERROR_EOL) {
2348 hdrl_image *badPixelMaskImg)
2350 cpl_mask *m = cpl_mask_new(3,3);
2351 cpl_mask_set(m, 1,2, BAD_PIX);
2352 cpl_mask_set(m, 2,1, BAD_PIX);
2353 cpl_mask_set(m, 2,3, BAD_PIX);
2354 cpl_mask_set(m, 3,2, BAD_PIX);
2362 cpl_image_power(variance, 2.0);
2363 cpl_image_filter_mask(filterd_error, variance, m, CPL_FILTER_AVERAGE,
2365 cpl_image_delete(variance);
2367 cpl_image_divide_scalar(filterd_error, (
double) cpl_mask_count(m));
2368 cpl_image_power(filterd_error, 0.5);
2370 if (badPixelMaskImg == NULL){
2372 cpl_image *badPixelMaskImg2 = cpl_image_cast(badPixelMaskImg1, CPL_TYPE_DOUBLE);
2373 cpl_image_delete(badPixelMaskImg1);
2379 badPixelMaskImg2, NULL, 1, 1);
2381 cpl_image_delete(badPixelMaskImg2);
2386 cpl_mask_set(m, 2, 2, GOOD_PIX);
2392 double corr_thesh = 0.4;
2393 cpl_image_threshold(filtered_mask, corr_thesh, corr_thesh, 1.0, 0.0);
2395 cpl_mask *corr_mask_t = cpl_mask_threshold_image_create(filtered_mask,-0.1, 0.1);
2396 cpl_mask_not(corr_mask_t);
2397 cpl_msg_info(__func__,
"No. bad pixels to be corrected: %d", (
int)cpl_mask_count(corr_mask_t));
2398 cpl_image_multiply(filterd_image, filtered_mask);
2399 cpl_image_multiply(filterd_error, filtered_mask);
2401 cpl_image_subtract_scalar(filtered_mask, 1);
2402 cpl_image_multiply_scalar(filtered_mask, -1);
2403 cpl_mask *corr_mask = cpl_mask_threshold_image_create(filtered_mask,-0.1, 0.1);
2404 cpl_mask_not(corr_mask);
2405 cpl_msg_debug(__func__,
"No. bad pixels before correction: %d", (
int)cpl_image_count_rejected(
hdrl_image_get_image(himg)));
2408 cpl_msg_debug(__func__,
"No. bad pixels after correction: %d", (
int)cpl_image_count_rejected(
hdrl_image_get_image(himg)));
2418 cpl_image_delete(filtered_mask);
2419 cpl_image_delete(filterd_error);
2420 cpl_image_delete(filterd_image);
2421 cpl_mask_delete(corr_mask);
2422 cpl_mask_delete(corr_mask_t);
2424 return cpl_error_get_code();
2427double eris_ifu_auto_derot_corr(
double alt,
double rot)
2429 double za = 90.0-alt;
2430 double offset = -0.000379*za + 0.000500*pow(za,2);
2431 double alt_scale = 0.0467*za - 0.000265*pow(za,2);
2432 double pi = 3.14159265359;
2433 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.
#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 correction on a raw detector image.
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_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)
Read a raw detector exposure, perform some correction, add noise data.
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_error_code eris_ifu_exposure_column_correction(cpl_image *image)
Perform column correction on a raw detector image.
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.
cpl_mask * eris_ifu_detect_crh(hdrl_image *image, int exposureCorrectionMode, hdrl_parameter *laCosmicParams, bool maskImage)
Detect Cosmic Ray Hits.
cpl_image * eris_ifu_calc_noise_map(const cpl_image *data, double gain, double readnoise)
Return an HDRL image containing a noise image.
cpl_error_code eris_ifu_saturation_detection(cpl_image *image, cpl_image *dqi)
Detect saturated pixel and mask them as bad pixels.
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.
hdrl_imagelist * eris_ifu_load_exposure_frameset(const cpl_frameset *frameset, int exposureCorrectionMode)
Read a raw detector exposure, perform some correction, add noise data.
ifsInstrument eris_ifu_get_instrument_frame(cpl_frame *frame)
Return value of INSTRUME from a given input frame.
double eris_ifu_image_get_mean(const cpl_image *image)
**
hdrl_image * eris_ifu_raw_hdrl_image(const cpl_image *cplImage)
Return an HDRL image containing a noise image as well.
hdrl_image * eris_ifu_warp_polynomial_image(const hdrl_image *hdrlInImg, const cpl_polynomial *poly_u, const cpl_polynomial *poly_v)
eris_ifu_warp_polynomial_image
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
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