35 #include "muse_basicproc.h" 37 #include "muse_combine.h" 38 #include "muse_pfits.h" 39 #include "muse_quadrants.h" 40 #include "muse_quality.h" 41 #include "muse_utils.h" 42 #include "muse_data_format_z.h" 48 #define GENERATE_TEST_IMAGES 0 90 bpars->
overscan = cpl_strdup(cpl_parameter_get_string(param));
92 bpars->
rejection = cpl_strdup(cpl_parameter_get_string(param));
94 cpl_errorstate state = cpl_errorstate_get();
95 bpars->
ovscsigma = cpl_parameter_get_double(param);
96 if (!cpl_errorstate_is_equal(state)) {
97 cpl_errorstate_set(state);
98 bpars->
ovscsigma = cpl_parameter_get_int(param);
101 bpars->
ovscignore = cpl_parameter_get_int(param);
103 if (strstr(aPrefix,
"muse_scibasic")) {
105 bpars->
crmethod = cpl_strdup(cpl_parameter_get_string(param));
107 bpars->
dcrxbox = cpl_parameter_get_int(param);
109 bpars->
dcrybox = cpl_parameter_get_int(param);
111 bpars->
dcrpasses = cpl_parameter_get_int(param);
113 state = cpl_errorstate_get();
114 bpars->
dcrthres = cpl_parameter_get_double(param);
115 if (!cpl_errorstate_is_equal(state)) {
116 cpl_errorstate_set(state);
117 bpars->
dcrthres = cpl_parameter_get_int(param);
145 cpl_ensure(aHeader, CPL_ERROR_NULL_INPUT, NULL);
150 cpl_ensure(parlist, CPL_ERROR_ILLEGAL_INPUT, NULL);
151 const char *recipe = cpl_propertylist_get_string(aHeader,
"ESO PRO REC1 ID");
152 char *prefix = cpl_sprintf(
"muse.%s", recipe);
154 cpl_parameterlist_delete(parlist);
202 static cpl_error_code
205 cpl_ensure_code(aImage && aRef, CPL_ERROR_NULL_INPUT);
206 cpl_ensure_code(aImage->
header && aRef->
header, CPL_ERROR_NULL_INPUT);
208 cpl_propertylist *him = aImage->
header,
211 const char *fn1 = cpl_propertylist_get_string(him, MUSE_HDR_TMP_FN),
212 *fn2 = cpl_propertylist_get_string(href, MUSE_HDR_TMP_FN),
215 cpl_msg_error(__func__,
"\"%s\" does not contain a category (ESO.PRO.CATG)!",
217 return CPL_ERROR_ILLEGAL_INPUT;
220 cpl_boolean ignoreread = getenv(
"MUSE_DEBUG_IGNORE_READOUT")
221 && atoi(getenv(
"MUSE_DEBUG_IGNORE_READOUT")) > 0,
222 ignoremode = getenv(
"MUSE_DEBUG_IGNORE_INSMODE")
223 && atoi(getenv(
"MUSE_DEBUG_IGNORE_INSMODE")) > 0;
229 *rawtag = cpl_propertylist_get_string(him, MUSE_HDR_TMP_INTAG);
242 cpl_boolean chipinfo = chipname1 && chipid1
243 && chipname2 && chipid2;
245 cpl_msg_warning(__func__,
"CHIP information is missing (ESO.DET.CHIP." 246 "{NAME,ID}) from \"%s\" (%s, %s) or \"%s\" (%s, %s)",
247 fn1, chipname1, chipid1, fn2, chipname2, chipid2);
250 if (binx1 != binx2 || biny1 != biny2) {
251 cpl_msg_error(__func__,
"Binning of \"%s\" (%dx%d) and \"%s\" (%dx%d) does " 252 "not match", fn1, binx1, biny1, fn2, binx2, biny2);
253 return CPL_ERROR_TYPE_MISMATCH;
259 if (!strncmp(catg,
"MASTER_BIAS", 12)) {
260 if (!ignoreread && (readid1 != readid2)) {
261 cpl_msg_error(__func__,
"Read-out mode of \"%s\" (%d: %s) and \"%s\" (%d:" 262 " %s) does not match", fn1, readid1, readname1, fn2,
264 return CPL_ERROR_TYPE_MISMATCH;
266 if (chipinfo && (strcmp(chipname1, chipname2) || strcmp(chipid1, chipid2))) {
267 cpl_msg_error(__func__,
"CHIP information (ESO.DET.CHIP.{NAME,ID}) " 268 "does not match for \"%s\" (%s, %s) and \"%s\" (%s, %s)",
269 fn1, chipname1, chipid1, fn2, chipname2, chipid2);
270 return CPL_ERROR_TYPE_MISMATCH;
276 if (!strncmp(catg,
"MASTER_FLAT", 12)) {
277 if (!ignoremode && (mode1 != mode2)) {
278 if (rawtag && !strncmp(rawtag, MUSE_TAG_ILLUM, strlen(MUSE_TAG_ILLUM) + 1)) {
280 cpl_msg_debug(__func__,
"Instrument modes for \"%s\" (%s, is %s) and \"%s\"" 281 " (%s) do not match", fn1, modestr1, rawtag, fn2, modestr2);
283 cpl_msg_error(__func__,
"Instrument modes for \"%s\" (%s) and \"%s\" (%s)" 284 " do not match", fn1, modestr1, fn2, modestr2);
285 return CPL_ERROR_TYPE_MISMATCH;
295 if (!ignoreread && (readid1 != readid2)) {
296 cpl_msg_warning(__func__,
"Read-out mode of \"%s\" (%d: %s) and \"%s\" (%d:" 297 " %s) does not match", fn1, readid1, readname1, fn2,
300 if (chipinfo && (strcmp(chipname1, chipname2) || strcmp(chipid1, chipid2))) {
301 cpl_msg_warning(__func__,
"CHIP information (ESO.DET.CHIP.{NAME,ID,DATE}) " 302 "does not match for \"%s\" (%s, %s) and \"%s\" (%s, %s)",
303 fn1, chipname1, chipid1, fn2, chipname2, chipid2);
306 return CPL_ERROR_NONE;
325 static cpl_error_code
329 cpl_ensure_code(aList, CPL_ERROR_NULL_INPUT);
331 for (k = 0; k < aList->
size; k++) {
336 cpl_msg_warning(__func__,
"Could not compute overscan statistics in IFU " 338 k+1, cpl_error_get_message());
341 return CPL_ERROR_NONE;
358 static cpl_error_code
362 cpl_ensure_code(aList, CPL_ERROR_NULL_INPUT);
363 cpl_boolean ovscvpoly = aBPars && aBPars->
overscan 364 && !strncmp(aBPars->
overscan,
"vpoly", 5);
366 cpl_msg_debug(__func__,
"not vpoly: %s!", aBPars ? aBPars->
overscan :
"");
367 return CPL_ERROR_NONE;
370 unsigned char ovscvorder = 5;
373 char *rest = strchr(aBPars->
overscan,
':');
374 if (strlen(aBPars->
overscan) > 6 && rest++) {
375 ovscvorder = strtol(rest, &rest, 10);
376 if (strlen(rest++) > 0) {
377 frms = strtod(rest, &rest);
378 if (strlen(rest++) > 0) {
379 fchisq = strtod(rest, &rest);
383 cpl_msg_debug(__func__,
"vpoly: %s (vorder=%hhu, frms=%f, fchisq=%f, ignore=%u," 384 " sigma=%.3f)", aBPars->
overscan, ovscvorder, frms, fchisq,
387 cpl_error_code rc = CPL_ERROR_NONE;
389 for (k = 0; k < aList->
size; k++) {
394 if (rc != CPL_ERROR_NONE) {
396 cpl_msg_error(__func__,
"Could not correct quadrants levels using vertical" 397 " overscan fit in IFU %hhu: %s", ifu, cpl_error_get_message());
418 static cpl_error_code
421 cpl_ensure_code(aList, CPL_ERROR_NULL_INPUT);
424 for (k = 0; k < aList->
size; k++) {
427 cpl_ensure_code(trimmed, cpl_error_get_code());
433 : CPL_ERROR_ILLEGAL_OUTPUT;
455 static cpl_error_code
460 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
463 return CPL_ERROR_NONE;
465 cpl_boolean ovscoffset = aBPars && aBPars->
overscan 466 && !strncmp(aBPars->
overscan,
"offset", 7);
468 return CPL_ERROR_NONE;
471 cpl_msg_info(__func__,
"Running overscan correction using %u %s images in IFU" 472 " %hhu", aList->
size, MUSE_TAG_BIAS, ifu);
475 for (k = 1; k < aList->
size; k++) {
478 return CPL_ERROR_NONE;
507 static cpl_error_code
512 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
516 return CPL_ERROR_NONE;
518 if (aList->
size < 2) {
519 return CPL_ERROR_NONE;
521 cpl_boolean ovscnone = aBPars && aBPars->
overscan 522 && !strncmp(aBPars->
overscan,
"none", 5);
524 return CPL_ERROR_NONE;
526 double sigma = aBPars ? aBPars->
ovscsigma : 1.;
530 cpl_propertylist *refheader = refimage->
header;
531 cpl_error_code rc = CPL_ERROR_NONE;
533 for (n = 1; n <= 4; n++) {
535 char *keywordmean = cpl_sprintf(MUSE_HDR_OVSC_MEAN, n),
536 *keywordstdev = cpl_sprintf(MUSE_HDR_OVSC_STDEV, n);
539 const char *reffn = cpl_propertylist_get_string(refheader, MUSE_HDR_TMP_FN);
540 float refmean = cpl_propertylist_get_float(refheader, keywordmean),
541 refstdev = cpl_propertylist_get_float(refheader, keywordstdev),
542 hilimit = refmean + sigma * refstdev,
543 lolimit = refmean - sigma * refstdev;
545 double summean = refmean,
546 sumstdev = pow(refstdev, 2.);
550 for (k = 1; k < aList->
size; k++) {
552 float mean = cpl_propertylist_get_float(h, keywordmean),
553 stdev = cpl_propertylist_get_float(h, keywordstdev);
555 sumstdev += pow(stdev, 2.) + pow(mean - refmean, 2.);
556 const char *fn = cpl_propertylist_get_string(h, MUSE_HDR_TMP_FN);
557 if (mean > hilimit || mean < lolimit) {
558 cpl_msg_error(__func__,
"Overscan of IFU %hhu, quadrant %1hhu of image %u [%s] " 559 "(%.3f+/-%.3f) differs from first image [%s] (%.3f+/-%.3f)!",
560 ifu, n, k+1, fn, mean, stdev, reffn, refmean, refstdev);
561 rc = cpl_error_set(__func__, CPL_ERROR_INCOMPATIBLE_INPUT);
564 cpl_msg_debug(__func__,
"Overscan of IFU %hhu, quadrant %1hhu of image %u [%s] " 565 "%.3f+/-%.3f (first image [%s] %.3f+/-%.3f)",
566 ifu, n, k+1, fn, mean, stdev, reffn, refmean, refstdev);
571 summean /= aList->
size;
572 sumstdev = sqrt(sumstdev) / aList->
size;
573 cpl_msg_debug(__func__,
"Averaged overscan values in IFU %hhu, quadrant %1hhu: " 574 "%.3f +/- %.3f (%d images)", ifu, n, summean, sumstdev, aList->
size);
575 cpl_propertylist_update_float(refheader, keywordmean, summean);
576 cpl_propertylist_update_float(refheader, keywordstdev, sumstdev);
578 cpl_free(keywordmean);
579 cpl_free(keywordstdev);
605 static cpl_error_code
609 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
611 MUSE_TAG_BADPIX_TABLE, aIFU,
614 return CPL_ERROR_NONE;
618 cpl_errorstate prestate = cpl_errorstate_get();
619 cpl_size iframe, nframes = cpl_frameset_get_size(frames);
620 for (iframe = 0; iframe < nframes; iframe++) {
621 cpl_frame *frame = cpl_frameset_get_position(frames, iframe);
622 const char *fn = cpl_frame_get_filename(frame);
623 char *extname = cpl_sprintf(
"CHAN%02hhu", aIFU);
632 int nrow = cpl_table_get_nrow(table);
633 unsigned int k, nbadpix = 0,
635 for (k = 0; k < aList->
size; k++) {
639 for (i = 0; i < nrow; i++) {
640 int x = cpl_table_get(table, MUSE_BADPIX_X, i, NULL),
641 y = cpl_table_get(table, MUSE_BADPIX_Y, i, NULL);
645 uint32_t dq = cpl_table_get(table, MUSE_BADPIX_DQ, i, NULL);
646 cpl_errorstate state = cpl_errorstate_get();
648 uint32_t value = cpl_image_get(image->
dq, x, y, &err);
649 if (err != 0 || !cpl_errorstate_is_equal(state)) {
650 cpl_errorstate_set(state);
658 cpl_image_set(image->
dq, x, y, dq | value);
664 cpl_table_delete(table);
666 cpl_msg_debug(__func__,
"Applied %u bad pixels from %s \"%s\" in IFU %hhu.",
667 nbadpix, MUSE_TAG_BADPIX_TABLE, fn, aIFU);
670 cpl_msg_warning(__func__,
"\"%s\" contained %u entries outside the CCD in" 671 " IFU %hhu!", fn, nbadpos, aIFU);
674 cpl_frameset_delete(frames);
675 return cpl_errorstate_is_equal(prestate) ? CPL_ERROR_NONE
676 : cpl_error_get_code();
689 static cpl_error_code
692 cpl_ensure_code(aList, CPL_ERROR_NULL_INPUT);
694 for (k = 0; k < aList->
size; k++) {
699 int npix = cpl_image_get_size_x(image->
data)
700 * cpl_image_get_size_y(image->
data);
701 if (nsaturated > (0.01 * npix)) {
702 const char *fn = cpl_propertylist_get_string(image->
header,
704 cpl_msg_error(__func__,
"Raw exposure %u [%s] is strongly saturated in " 705 "IFU %hhu (%d of %d pixels)!", k+1, fn, ifu, nsaturated,
707 }
else if (nsaturated > (0.001 * npix)) {
708 const char *fn = cpl_propertylist_get_string(image->
header,
710 cpl_msg_warning(__func__,
"Raw exposure %u [%s] is probably saturated in " 711 "IFU %hhu (%d of %d pixels)!", k+1, fn, ifu, nsaturated,
714 cpl_msg_debug(__func__,
"Raw exposure %u in IFU %hhu (%d of %d pixels " 715 "saturated)!", k+1, ifu, nsaturated, npix);
717 cpl_propertylist_update_int(image->
header, MUSE_HDR_TMP_NSAT, nsaturated);
719 return CPL_ERROR_NONE;
736 static cpl_error_code
740 cpl_ensure_code(aList, CPL_ERROR_NULL_INPUT);
743 return CPL_ERROR_NONE;
746 cpl_msg_info(__func__,
"Computing per-quadrant medians for %u exposures in " 747 "IFU %hhu", aList->
size, ifu);
749 for (k = 0; k < aList->
size; k++) {
752 for (n = 1; n <= 4; n++) {
754 float median = cpl_image_get_median_window(image->
data, w[0], w[2],
757 char *kw = cpl_sprintf(MUSE_HDR_TMP_QUADnMED, n);
758 cpl_propertylist_append_float(image->
header, kw, median);
762 return CPL_ERROR_NONE;
788 static cpl_error_code
792 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
794 MUSE_TAG_MASTER_BIAS, aIFU);
795 cpl_errorstate prestate = cpl_errorstate_get();
796 if (!biasframe)
return CPL_ERROR_NONE;
797 cpl_errorstate_set(prestate);
798 const char *biasname = cpl_frame_get_filename(biasframe);
803 char *errmsg = cpl_strdup(cpl_error_get_message());
804 cpl_errorstate_set(prestate);
809 cpl_msg_error(__func__,
"%s", errmsg);
810 cpl_msg_error(__func__,
"%s", cpl_error_get_message());
812 cpl_frame_delete(biasframe);
813 return cpl_error_get_code();
816 cpl_msg_info(__func__,
"Bias correction in IFU %hhu using \"%s[CHAN%02hhu." 817 "DATA]\"", aIFU, biasname, aIFU);
819 cpl_msg_info(__func__,
"Bias correction in IFU %hhu using \"%s[DATA]\"",
823 cpl_propertylist_append_string(biasimage->
header, MUSE_HDR_TMP_FN, biasname);
828 cpl_boolean parmatch = mbpars && aBPars;
830 parmatch = parmatch && mbpars->
overscan && aBPars->overscan
831 && !strncmp(aBPars->overscan, mbpars->
overscan,
832 CPL_MIN(strlen(aBPars->overscan), strlen(mbpars->
overscan) + 1));
833 parmatch = parmatch && mbpars->
rejection && aBPars->rejection
834 && !strncmp(aBPars->rejection, mbpars->
rejection,
835 CPL_MIN(strlen(aBPars->rejection), strlen(mbpars->
rejection) + 1));
836 parmatch = parmatch && fabs(aBPars->ovscsigma - mbpars->
ovscsigma)
838 parmatch = parmatch && aBPars->ovscignore == mbpars->
ovscignore;
841 cpl_msg_warning(__func__,
"overscan parameters differ between %s and recipe" 842 " parameters!", MUSE_TAG_MASTER_BIAS);
844 cpl_msg_debug(__func__,
"overscan parameters between %s and given " 845 "parameters nicely match", MUSE_TAG_MASTER_BIAS);
849 cpl_boolean ovscoffset = aBPars && aBPars->overscan
850 && !strncmp(aBPars->overscan,
"offset", 7),
851 ovscvpoly = aBPars && aBPars->overscan
852 && !strncmp(aBPars->overscan,
"vpoly", 5);
854 cpl_error_code rc = CPL_ERROR_NONE;
856 for (k = 0; k < aList->
size && rc == CPL_ERROR_NONE; k++) {
858 rc = muse_basicproc_verify_setup(image, biasimage);
859 if (rc != CPL_ERROR_NONE) {
865 }
else if (ovscvpoly) {
871 cpl_msg_error(__func__,
"Very different overscan levels found between " 872 "%s and raw exposure %u in IFU %hhu: incompatible overscan" 873 " parameters?", MUSE_TAG_MASTER_BIAS, k + 1, aIFU);
874 rc = cpl_error_set(__func__, CPL_ERROR_INCOMPATIBLE_INPUT);
880 aBPars ? aBPars->ovscsigma : 1.);
883 #if GENERATE_TEST_IMAGES 915 static cpl_error_code
919 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
922 return CPL_ERROR_NONE;
925 CPL_ERROR_INCOMPATIBLE_INPUT);
928 MUSE_TAG_MASTER_BIAS, aIFU);
931 return CPL_ERROR_NONE;
933 const char *biasname = cpl_frame_get_filename(fbias);
934 cpl_propertylist *hbias = cpl_propertylist_load(biasname, 0);
935 if (!cpl_propertylist_has(hbias, QC_BIAS_MASTER_RON)) {
936 cpl_propertylist_delete(hbias);
938 char *extname = cpl_sprintf(
"CHAN%02hhu.%s", aIFU, EXTNAME_DATA);
939 int extension = cpl_fits_find_extension(biasname, extname);
940 hbias = cpl_propertylist_load(biasname, extension);
943 cpl_frame_delete(fbias);
947 cpl_image *diff = cpl_image_subtract_create(f1, f2);
950 for (n = 1; n <= 4; n++) {
952 double m1 = cpl_image_get_mean_window(f1, w[0], w[2], w[1], w[3]),
953 m2 = cpl_image_get_mean_window(f2, w[0], w[2], w[1], w[3]),
954 sf = cpl_image_get_stdev_window(diff, w[0], w[2], w[1], w[3]);
955 char *keyword = cpl_sprintf(QC_BIAS_MASTERn_PREFIX
" MEAN", n);
956 float mb = cpl_propertylist_get_float(hbias, keyword);
958 keyword = cpl_sprintf(QC_BIAS_MASTER_RON, n);
961 sb = cpl_propertylist_get_float(hbias, keyword) * sqrt(2.)
964 gain = (m1 + m2 - 2*mb) / (sf*sf - sb*sb),
965 dgain = 1. - gain / gainheader;
968 cpl_msg_warning(__func__,
"IFU %hhu, quadrant %1hhu: estimated gain %.3f " 969 "count/adu but header gives %.3f!", aIFU, n, gain,
972 cpl_msg_info(__func__,
"IFU %hhu, quadrant %1hhu: estimated gain %.3f " 973 "count/adu (header %.3f, delta %.3f)", aIFU, n, gain,
980 cpl_image_delete(diff);
981 cpl_propertylist_delete(hbias);
983 return CPL_ERROR_NONE;
1006 static cpl_error_code
1010 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
1015 return CPL_ERROR_NONE;
1019 MUSE_TAG_NONLINGAIN, aIFU);
1022 return CPL_ERROR_NONE;
1024 if (getenv(
"MUSE_BASICPROC_SKIP_GAIN_OVERRIDE") &&
1025 atoi(getenv(
"MUSE_BASICPROC_SKIP_GAIN_OVERRIDE")) > 0) {
1026 cpl_msg_info(__func__,
"Skipping gain override, although %s is given",
1027 MUSE_TAG_NONLINGAIN);
1028 return CPL_ERROR_NONE;
1030 cpl_errorstate state = cpl_errorstate_get();
1031 const char *fn = cpl_frame_get_filename(fnonlingain);
1033 char *extname = cpl_sprintf(
"CHAN%02hhu", aIFU);
1034 int extension = cpl_fits_find_extension(fn, extname);
1035 cpl_propertylist *header = cpl_propertylist_load(fn, extension);
1036 cpl_msg_info(__func__,
"Overriding gain in IFU %hhu using \"%s[%s]\"",
1042 for (k = 0; k < aList->
size; k++) {
1046 for (n = 1; n <= 4; n++) {
1049 char *kw = cpl_sprintf(
"ESO DET OUT%d GAIN", n);
1050 cpl_propertylist_update_double(image->
header, kw, gain);
1054 cpl_propertylist_delete(header);
1055 return cpl_errorstate_is_equal(state) ? CPL_ERROR_NONE : cpl_error_get_code();
1073 static cpl_error_code
1076 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
1082 return CPL_ERROR_NONE;
1086 cpl_msg_info(__func__,
"Converting %u exposures from adu to count (= electron)" 1087 " units in IFU %hhu", aList->
size, ifu);
1088 cpl_error_code rc = CPL_ERROR_NONE;
1090 for (k = 0; k < aList->
size; k++) {
1119 static cpl_error_code
1124 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
1131 return CPL_ERROR_NONE;
1135 MUSE_TAG_NONLINGAIN, aIFU);
1138 return CPL_ERROR_NONE;
1140 if (getenv(
"MUSE_BASICPROC_SKIP_NONLIN_CORR") &&
1141 atoi(getenv(
"MUSE_BASICPROC_SKIP_NONLIN_CORR")) > 0) {
1142 cpl_msg_debug(__func__,
"Skipping nonlinearity correction, although %s is " 1143 "given", MUSE_TAG_NONLINGAIN);
1144 return CPL_ERROR_NONE;
1146 const char *fn = cpl_frame_get_filename(fnonlingain);
1148 char *extname = cpl_sprintf(
"CHAN%02hhu", aIFU);
1149 int extension = cpl_fits_find_extension(fn, extname);
1150 cpl_propertylist *header = cpl_propertylist_load(fn, extension);
1151 cpl_msg_info(__func__,
"Correcting nonlinearity in IFU %hhu using \"%s[%s]\"",
1156 cpl_error_code rc = CPL_ERROR_NONE;
1158 for (k = 0; k < aList->
size; k++) {
1160 int nx = cpl_image_get_size_x(image->
data);
1161 float *data = cpl_image_get_data_float(image->
data),
1162 *stat = cpl_image_get_data_float(image->
stat);
1167 for (n = 1; n <= 4; n++) {
1170 cpl_polynomial *poly = cpl_polynomial_new(1);
1171 char *kw = cpl_sprintf(MUSE_HDR_NONLINn_ORDER, n);
1172 unsigned char o, order = cpl_propertylist_get_int(header, kw);
1174 for (o = 0; o <= order; o++) {
1175 kw = cpl_sprintf(MUSE_HDR_NONLINn_COEFFo, n, o);
1177 cpl_polynomial_set_coeff(poly, &pows,
1178 cpl_propertylist_get_double(header, kw));
1182 kw = cpl_sprintf(MUSE_HDR_NONLINn_LLO, n);
1183 double lolim = cpl_propertylist_get_double(header, kw);
1185 kw = cpl_sprintf(MUSE_HDR_NONLINn_LHI, n);
1186 double hilim = cpl_propertylist_get_double(header, kw);
1189 double p1lo, p0lo = cpl_polynomial_eval_1d(poly, lolim, &p1lo),
1190 p1hi, p0hi = cpl_polynomial_eval_1d(poly, hilim, &p1hi);
1192 lolim = pow(10, lolim);
1193 hilim = pow(10, hilim);
1195 double values[] = { 1., 20., 200., 2000., 20000., 65000.,
1196 lolim, hilim, -1. };
1198 while (values[idx] > 0) {
1199 double v = values[idx],
1201 cpl_msg_debug(__func__,
"%f adu -> %f log10(adu) ==> poly = %f ==> x %f",
1202 v, logv, cpl_polynomial_eval_1d(poly, logv, NULL),
1203 1. / (1. + cpl_polynomial_eval_1d(poly, logv, NULL)));
1206 cpl_polynomial_dump(poly, stdout);
1208 cpl_msg_debug(__func__,
"beyond low limit (%f adu): %f + (%f) * (log10(adu) - %f)",
1209 lolim, p0lo, p1lo, log10(lolim));
1210 cpl_msg_debug(__func__,
"beyond high limit (%f adu): %f + (%f) * (log10(adu) - %f)",
1211 hilim, p0hi, p1hi, log10(hilim));
1217 for (i = w[0] - 1; i < w[1]; i++) {
1219 for (j = w[2] - 1; j < w[3]; j++) {
1220 if (data[i + j*nx] <= 0) {
1225 if (data[i + j*nx] < lolim) {
1226 pcor = p0lo + p1lo * (log10(data[i + j*nx]) - log10(lolim));
1227 }
else if (data[i + j*nx] > hilim) {
1228 pcor = p0hi + p1hi * (log10(data[i + j*nx]) - log10(hilim));
1230 pcor = cpl_polynomial_eval_1d(poly, log10(data[i + j*nx]), NULL);
1233 double fcor = 1. / (1. + pcor);
1234 data[i + j*nx] *= fcor;
1235 stat[i + j*nx] *= fcor*fcor;
1239 cpl_polynomial_delete(poly);
1242 cpl_propertylist_delete(header);
1264 static cpl_error_code
1268 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
1271 MUSE_TAG_MASTER_DARK, aIFU);
1272 cpl_errorstate prestate = cpl_errorstate_get();
1273 if (!darkframe)
return CPL_ERROR_NONE;
1274 cpl_errorstate_set(prestate);
1275 const char *darkname = cpl_frame_get_filename(darkframe);
1278 char *errmsg = cpl_strdup(cpl_error_get_message());
1279 cpl_errorstate_set(prestate);
1282 cpl_msg_error(__func__,
"%s", errmsg);
1283 cpl_msg_error(__func__,
"%s", cpl_error_get_message());
1285 cpl_frame_delete(darkframe);
1286 return cpl_error_get_code();
1289 cpl_msg_info(__func__,
"Dark correction in IFU %hhu using \"%s[CHAN%02hhu." 1290 "DATA]\"", aIFU, darkname, aIFU);
1292 cpl_msg_info(__func__,
"Dark correction in IFU %hhu using \"%s[DATA]\"",
1295 cpl_propertylist_append_string(darkimage->
header, MUSE_HDR_TMP_FN, darkname);
1298 cpl_error_code rc = CPL_ERROR_NONE;
1300 for (k = 0; k < aList->
size; k++) {
1302 rc = muse_basicproc_verify_setup(image, darkimage);
1303 if (rc != CPL_ERROR_NONE) {
1341 static cpl_error_code
1345 cpl_ensure_code(aList, CPL_ERROR_NULL_INPUT);
1349 cpl_boolean isdcr = aBPars && aBPars->
crmethod 1350 && !strncmp(aBPars->
crmethod,
"dcr", 4);
1352 return CPL_ERROR_NONE;
1355 cpl_error_code rc = CPL_ERROR_NONE;
1357 for (k = 0; k < aList->
size; k++) {
1364 cpl_msg_error(__func__,
"Cosmic ray rejection using DCR in IFU %hhu failed" 1365 " for image %u (ncr = %d): %s", ifu, k+1, ncr,
1366 cpl_error_get_message());
1367 rc = cpl_error_get_code();
1369 cpl_msg_info(__func__,
"Cosmic ray rejection using DCR in IFU %hhu found " 1370 "%d affected pixels in image %u", ifu, ncr, k+1);
1394 static cpl_error_code
1398 cpl_ensure_code(aList && aProcessing, CPL_ERROR_NULL_INPUT);
1401 MUSE_TAG_MASTER_FLAT, aIFU);
1402 cpl_errorstate prestate = cpl_errorstate_get();
1403 if (!flatframe)
return CPL_ERROR_NONE;
1404 cpl_errorstate_set(prestate);
1405 const char *flatname = cpl_frame_get_filename(flatframe);
1406 prestate = cpl_errorstate_get();
1409 char *errmsg = cpl_strdup(cpl_error_get_message());
1410 cpl_errorstate_set(prestate);
1413 cpl_msg_error(__func__,
"%s", errmsg);
1414 cpl_msg_error(__func__,
"%s", cpl_error_get_message());
1416 cpl_frame_delete(flatframe);
1417 return cpl_error_get_code();
1420 cpl_msg_info(__func__,
"Flat-field correction in IFU %hhu using \"%s[CHAN%02hhu." 1421 "DATA]\"", aIFU, flatname, aIFU);
1423 cpl_msg_info(__func__,
"Flat-field correction in IFU %hhu using \"%s[DATA]\"",
1427 double fflux = cpl_propertylist_get_double(flatimage->
header,
1428 QC_FLAT_MASTER_INTFLUX);
1429 cpl_propertylist_append_string(flatimage->
header, MUSE_HDR_TMP_FN, flatname);
1432 cpl_error_code rc = CPL_ERROR_NONE;
1434 for (k = 0; k < aList->
size; k++) {
1435 cpl_msg_debug(__func__,
"Flat-field division in IFU %hhu of image %u of %u",
1436 aIFU, k+1, aList->
size);
1438 rc = muse_basicproc_verify_setup(image, flatimage);
1439 if (rc != CPL_ERROR_NONE) {
1443 cpl_propertylist_update_double(image->
header, MUSE_HDR_FLAT_FLUX_LAMP, fflux);
1477 muse_basicproc_load_raw(
muse_processing *aProcessing,
unsigned char aIFU)
1479 cpl_ensure(aProcessing, CPL_ERROR_NULL_INPUT, NULL);
1483 aProcessing->
intags, aIFU);
1484 cpl_ensure(rawframes, CPL_ERROR_NULL_INPUT, NULL);
1488 cpl_size iframe, nframes = cpl_frameset_get_size(rawframes);
1489 for (iframe = 0; iframe < nframes; iframe++) {
1490 cpl_frame *frame = cpl_frameset_get_position(rawframes, iframe);
1491 const char *fileName = cpl_frame_get_filename(frame);
1493 if (extension == -1) {
1494 cpl_msg_error(__func__,
"\"%s\" does not contain data from IFU %hhu",
1503 cpl_propertylist_append_string(raw->
header, MUSE_HDR_TMP_FN, fileName);
1505 cpl_propertylist_append_string(raw->
header, MUSE_HDR_TMP_INTAG,
1506 cpl_frame_get_tag(frame));
1512 cpl_frameset_delete(rawframes);
1515 if (!images->
size) {
1516 if ((
int)cpl_error_get_code() != MUSE_ERROR_CHIP_NOT_LIVE) {
1517 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_OUTPUT);
1518 cpl_msg_error(__func__,
"No raw images loaded for IFU %hhu", aIFU);
1524 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_OUTPUT);
1525 cpl_msg_error(__func__,
"Non-uniform imagelist for IFU %hhu", aIFU);
1584 muse_imagelist *images = muse_basicproc_load_raw(aProcessing, aIFU);
1588 cpl_errorstate prestate = cpl_errorstate_get();
1589 if (muse_basicproc_apply_badpix(images, aProcessing, aIFU) != CPL_ERROR_NONE) {
1590 cpl_msg_warning(__func__,
"Applying bad pixel table to IFU %hhu failed: %s",
1591 aIFU, cpl_error_get_message());
1592 cpl_errorstate_set(prestate);
1594 if (muse_basicproc_check_saturation(images) != CPL_ERROR_NONE) {
1598 muse_basicproc_quadrant_statistics(images, aProcessing);
1599 if (muse_basicproc_overscans_compute_stats(images, aBPars)
1600 != CPL_ERROR_NONE) {
1604 if (muse_basicproc_correct_overscans_vpoly(images, aBPars)
1605 != CPL_ERROR_NONE) {
1609 if (muse_basicproc_trim_images(images) != CPL_ERROR_NONE) {
1613 if (muse_basicproc_correct_overscans_offset(images, aProcessing, aBPars)
1614 != CPL_ERROR_NONE) {
1615 cpl_msg_warning(__func__,
"Bias-level correction using overscans failed in " 1616 "IFU %hhu: %s", aIFU, cpl_error_get_message());
1618 if (muse_basicproc_check_overscans(images, aProcessing, aBPars)
1619 != CPL_ERROR_NONE) {
1623 muse_basicproc_gain_override(images, aProcessing, aIFU);
1624 muse_basicproc_check_gain(images, aProcessing, aIFU);
1625 if (muse_basicproc_apply_bias(images, aProcessing, aIFU, aBPars)
1626 != CPL_ERROR_NONE) {
1630 if (muse_basicproc_corr_nonlinearity(images, aProcessing, aIFU) != CPL_ERROR_NONE) {
1634 if (muse_basicproc_adu_to_count(images, aProcessing) != CPL_ERROR_NONE) {
1638 if (muse_basicproc_apply_dark(images, aProcessing, aIFU) != CPL_ERROR_NONE) {
1642 if (muse_basicproc_apply_cr(images, aBPars) != CPL_ERROR_NONE) {
1646 if (muse_basicproc_apply_flat(images, aProcessing, aBPars, aIFU) != CPL_ERROR_NONE) {
1674 cpl_size iframe, nframes = cpl_frameset_get_size(redframes);
1675 for (iframe = 0; iframe < nframes; iframe++) {
1676 cpl_frame *frame = cpl_frameset_get_position(redframes, iframe);
1677 cpl_errorstate prestate = cpl_errorstate_get();
1678 const char *imagename = cpl_frame_get_filename(frame);
1681 cpl_errorstate_set(prestate);
1687 cpl_frameset_delete(redframes);
1711 CPL_ERROR_NULL_INPUT, NULL);
1720 cpl_msg_info(__func__,
"Preparing %s flat: %d slices in the data of IFU %hhu " 1721 "found.", MUSE_TAG_ILLUM, npt, ifu);
1722 cpl_table *tattached = cpl_table_new(npt);
1723 cpl_table_new_column(tattached,
"slice", CPL_TYPE_INT);
1724 cpl_table_new_column(tattached,
"fflat", CPL_TYPE_DOUBLE);
1725 for (ipt = 0; ipt < npt; ipt++) {
1728 double median = cpl_table_get_column_median(pts[ipt]->table,
1730 cpl_msg_debug(__func__,
"Found median of %f in slice %d of IFU %hhu " 1731 "of illum flat.", median, slice, ifu);
1732 cpl_table_set_int(tattached,
"slice", ipt, slice);
1733 cpl_table_set_double(tattached,
"fflat", ipt, 1. / median);
1737 double mean = cpl_table_get_column_mean(tattached,
"fflat");
1738 cpl_msg_debug(__func__,
"Normalizing whole illum-flat table if IFU %hhu to " 1740 cpl_table_multiply_scalar(tattached,
"fflat", 1. / mean);
1741 cpl_table_set_column_format(tattached,
"fflat",
"%.6f");
1766 cpl_table *aWave, cpl_table *aGeo)
1768 cpl_ensure(aImages && aTrace && aWave && aGeo, CPL_ERROR_NULL_INPUT, NULL);
1770 cpl_table *tattached = NULL;
1773 cpl_boolean *isillum = cpl_calloc(nimages,
sizeof(cpl_boolean));
1774 for (k = 0; k < nimages; k++) {
1775 isillum[k] = CPL_FALSE;
1777 const char *tag = cpl_propertylist_get_string(image->
header,
1778 MUSE_HDR_TMP_INTAG);
1779 if (tag && !strncmp(tag, MUSE_TAG_ILLUM, strlen(MUSE_TAG_ILLUM) + 1)) {
1780 isillum[k] = CPL_TRUE;
1782 if (cpl_propertylist_has(image->
header,
"ESO TPL ID")) {
1783 const char *tplid = cpl_propertylist_get_string(image->
header,
1785 *fn = cpl_propertylist_get_string(image->
header,
1787 *tplatt =
"MUSE_wfm_cal_specflatatt",
1788 *tplill =
"MUSE_wfm_cal_illum",
1789 *tpliln =
"MUSE_nfm_cal_illum";
1790 if (strncmp(tplid, tplatt, strlen(tplatt) + 1) &&
1791 strncmp(tplid, tplill, strlen(tplill) + 1) &&
1792 strncmp(tplid, tpliln, strlen(tpliln) + 1)) {
1793 cpl_msg_warning(__func__,
"%s input (\"%s\") was taken with neither" 1794 " %s nor %s template, but %s!", MUSE_TAG_ILLUM, fn,
1795 tplatt, tplill, tplid);
1797 cpl_msg_debug(__func__,
"%s input (\"%s\") was taken with template " 1798 "%s", MUSE_TAG_ILLUM, fn, tplid);
1805 cpl_msg_warning(__func__,
"Image %u of %u of IFU %hhu is illum flat, " 1806 "but not the first; not using it!", k + 1, nimages, ifu);
1809 cpl_msg_debug(__func__,
"Image %u of %u of IFU %hhu is illum flat.",
1810 k + 1, nimages, ifu);
1812 tattached = muse_basicproc_prepare_illum(pt);
1815 cpl_msg_debug(__func__,
"Image %u of %u of IFU %hhu is not an illum flat.",
1816 k + 1, nimages, ifu);
1822 for (k = 0, k2 = 0; k < nimages; k++) {
1852 cpl_ensure_code(aPT && aPT->
header && aPT->
table && aAttached,
1853 CPL_ERROR_NULL_INPUT);
1859 cpl_msg_info(__func__,
"Applying %s flat-field in IFU %hhu (%d slices)",
1860 MUSE_TAG_ILLUM, ifu, npt);
1861 cpl_array *afactors = cpl_array_new(npt, CPL_TYPE_DOUBLE);
1862 for (ipt = 0; ipt < npt; ipt++) {
1865 fslice = cpl_table_get_int(aAttached,
"slice", ipt, NULL);
1867 double fflat = cpl_table_get_double(aAttached,
"fflat", ipt, &err);
1868 if (!err && slice == fslice) {
1871 cpl_array_set_double(afactors, ipt, fflat);
1873 cpl_propertylist_update_double(aPT->
header, kw, fflat);
1876 cpl_msg_warning(__func__,
"some error (%d) occurred when applying illum-" 1877 "flat correction to slice %hu/%hu of IFU %hhu: %s", err,
1878 slice, fslice, ifu, cpl_error_get_message());
1883 cpl_array_get_mean(afactors));
1885 cpl_array_get_stdev(afactors));
1886 cpl_array_delete(afactors);
1887 return CPL_ERROR_NONE;
1911 cpl_ensure_code(aPT && aPT->
header && aPT->
table && aTwilight,
1912 CPL_ERROR_NULL_INPUT);
1919 char *kw = cpl_sprintf(MUSE_HDR_FLAT_FLUX_SKY
"%hhu", ifu);
1920 double flux = cpl_propertylist_get_double(aTwilight->
header, kw);
1922 cpl_propertylist_update_double(aPT->
header, MUSE_HDR_FLAT_FLUX_SKY, flux);
1925 int nx = cpl_image_get_size_x(cpl_imagelist_get(aTwilight->
data, 0)),
1926 ny = cpl_image_get_size_y(cpl_imagelist_get(aTwilight->
data, 0)),
1927 nz = cpl_imagelist_get_size(aTwilight->
data);
1928 cpl_msg_debug(__func__,
"Working with %d planes in twilight cube", nz);
1931 if (cd12 > DBL_EPSILON || cd21 > DBL_EPSILON) {
1932 cpl_msg_warning(__func__,
"Twilight cube contains WCS cross-terms (CD1_2" 1933 "=%e, CD2_1=%e), results will be inaccurate!", cd12, cd21);
1954 for (irow = 0; irow < nrow; irow++) {
1957 int x = lround((xpos[irow] - crval1) / cd11 + crpix1),
1958 y = lround((ypos[irow] - crval2) / cd22 + crpix2);
1972 double z = (lbda[irow] - crval3) / cd33 + crpix3;
1974 cpl_msg_debug(__func__,
"%"CPL_SIZE_FORMAT
": %.3f %.3f %.3f => %d %d %.3f",
1975 irow, xpos[irow], ypos[irow], lbda[irow], x, y, z);
1978 int zp1 = floor(z) - 1,
1994 double v1 = cpl_image_get(cpl_imagelist_get(aTwilight->
data, zp1),
1996 v2 = cpl_image_get(cpl_imagelist_get(aTwilight->
data, zp2),
2012 f = fabs(z - 1 - zp1);
2013 villum = v1 * (1. - f) + v2 * f;
2015 double fillum = 1. / villum;
2017 cpl_msg_debug(__func__,
"%d/%d, %f/%f -> %f -> %f => %f", zp1, zp2, v1, v2,
2022 data[irow] *= fillum;
2024 stat[irow] *= fillum*fillum;
2027 cpl_msg_warning(__func__,
"Failed to correct twilight in %"CPL_SIZE_FORMAT
2028 " of %"CPL_SIZE_FORMAT
", pixels in IFU %hhu!", nfailed,
2031 cpl_msg_debug(__func__,
"No failures during twilight correction of %" 2032 CPL_SIZE_FORMAT
" pixels in IFU %hhu", nrow, ifu);
2035 return CPL_ERROR_NONE;
2060 cpl_ensure_code(aPT && aPT->
header && aPT->
table, CPL_ERROR_NULL_INPUT);
2063 float lmin = kMuseNa2LambdaMin,
2064 lmax = kMuseNa2LambdaMax;
2067 if (insmode == MUSE_MODE_WFM_AO_N) {
2069 }
else if (insmode == MUSE_MODE_WFM_AO_E) {
2070 lmin = kMuseNaLambdaMin;
2071 lmax = kMuseNaLambdaMax;
2072 }
else if (insmode == MUSE_MODE_NFM_AO_N) {
2073 lmin = kMuseNaGLambdaMin;
2074 lmax = kMuseNaGLambdaMax;
2076 cpl_msg_warning(__func__,
"No notch filter for mode %s!", mode);
2077 return CPL_ERROR_ILLEGAL_INPUT;
2079 cpl_msg_info(__func__,
"%s mode: marking NaD region (%.1f..%.1f Angstrom) of " 2080 "IFU %d as 0x%08lx", mode, lmin, lmax, aIFU, EURO3D_NOTCH_NAD);
2081 cpl_table_unselect_all(aPT->
table);
2083 CPL_GREATER_THAN, lmin);
2085 CPL_LESS_THAN, lmax);
2086 cpl_array *asel = cpl_table_where_selected(aPT->
table);
2087 cpl_size isel, nsel = cpl_array_get_size(asel);
2088 const cpl_size *sel = cpl_array_get_data_cplsize_const(asel);
2090 for (isel = 0; isel < nsel; isel++) {
2091 dq[sel[isel]] = EURO3D_NOTCH_NAD;
2093 cpl_array_delete(asel);
2095 return CPL_ERROR_NONE;
2126 muse_basicproc_combine_compare_lamp(
const cpl_frame *aFrame1,
const cpl_frame *aFrame2)
2128 cpl_ensure(aFrame1 && aFrame2, CPL_ERROR_NULL_INPUT, -1);
2129 const char *fn1 = cpl_frame_get_filename(aFrame1),
2130 *fn2 = cpl_frame_get_filename(aFrame2);
2131 cpl_propertylist *head1 = cpl_propertylist_load(fn1, 0),
2132 *head2 = cpl_propertylist_load(fn2, 0);
2133 if (!head1 || !head2) {
2134 cpl_propertylist_delete(head1);
2135 cpl_propertylist_delete(head2);
2142 int nlamp = 1, status1, status2;
2143 cpl_errorstate prestate = cpl_errorstate_get();
2148 cpl_errorstate_set(prestate);
2149 if (name1 && name2 && strncmp(name1, name2, strlen(name1) + 1)) {
2150 cpl_error_set_message(__func__, CPL_ERROR_INCOMPATIBLE_INPUT,
2151 "Files \"%s\" and \"%s\" have incompatible lamp " 2152 "setups", fn1, fn2);
2153 cpl_propertylist_delete(head1);
2154 cpl_propertylist_delete(head2);
2159 if (name1 && name2 && strncmp(name1, name2, strlen(name1) + 1)) {
2160 cpl_error_set_message(__func__, CPL_ERROR_INCOMPATIBLE_INPUT,
2161 "Files \"%s\" and \"%s\" have incompatible shutter " 2162 "setups", fn1, fn2);
2163 cpl_propertylist_delete(head1);
2164 cpl_propertylist_delete(head2);
2170 cpl_errorstate_set(prestate);
2171 if (status1 != status2) {
2176 if (status1 != status2) {
2180 }
while (cpl_errorstate_is_equal(prestate));
2181 cpl_errorstate_set(prestate);
2183 cpl_propertylist_delete(head1);
2184 cpl_propertylist_delete(head2);
2185 return status1 == status2;
2219 cpl_frameset ***aLabeledFrames)
2221 if (aLabeledFrames) {
2222 *aLabeledFrames = NULL;
2224 cpl_ensure(aProcessing, CPL_ERROR_NULL_INPUT, NULL);
2228 aProcessing->
intags, aIFU,
2230 char *prefix = cpl_sprintf(
"muse.%s", aProcessing->
name);
2235 printf(
"rawframes\n");
2236 cpl_frameset_dump(rawframes, stdout);
2240 *labels = cpl_frameset_labelise(rawframes,
2241 muse_basicproc_combine_compare_lamp,
2243 if (!labels || nlabels <= 1) {
2246 cpl_frameset_delete(rawframes);
2253 if (aLabeledFrames) {
2254 *aLabeledFrames = cpl_calloc(1,
sizeof(cpl_frameset *));
2255 (*aLabeledFrames)[0] = cpl_frameset_duplicate(aProcessing->
usedframes);
2264 cpl_array *alabels = cpl_array_wrap_int(labels, cpl_frameset_get_size(rawframes));
2265 cpl_array_dump(alabels, 0, 1000, stdout);
2267 cpl_array_unwrap(alabels);
2271 if (aLabeledFrames) {
2272 *aLabeledFrames = cpl_calloc(nlabels,
sizeof(cpl_frameset *));
2279 cpl_frameset *inframes = proc->
inframes;
2284 int ilabel, ipos = 0;
2285 for (ilabel = 0; ilabel < nlabels; ilabel++) {
2287 cpl_frameset *frames = cpl_frameset_extract(rawframes, labels, ilabel);
2289 cpl_frameset_join(frames, auxframes);
2298 cpl_frameset_delete(frames);
2303 if (aLabeledFrames) {
2304 cpl_free(*aLabeledFrames);
2305 *aLabeledFrames = NULL;
2312 cpl_msg_error(__func__,
"Image combination failed for IFU %hhu for lamp " 2313 "with label %d of %"CPL_SIZE_FORMAT, aIFU, ilabel + 1, nlabels);
2315 cpl_frameset_delete(frames);
2319 if (aLabeledFrames) {
2321 cpl_size iframe, nframes = cpl_frameset_get_size(frames);
2322 for (iframe = 0; iframe < nframes; iframe++) {
2323 cpl_frame *frame = cpl_frameset_get_position(frames, iframe);
2324 const char *fn = cpl_frame_get_filename(frame),
2325 *tag = cpl_frame_get_tag(frame);
2326 cpl_size iuframe, nuframes = cpl_frameset_get_size(aProcessing->
usedframes);
2328 (iuframe < nuframes) && fn && tag;
2330 cpl_frame *uframe = cpl_frameset_get_position(aProcessing->
usedframes,
2332 const char *fnu = cpl_frame_get_filename(uframe),
2333 *tagu = cpl_frame_get_tag(uframe);
2334 if (fnu && !strncmp(fn, fnu, strlen(fn) + 1) &&
2335 tagu && !strncmp(tag, tagu, strlen(tag) + 1)) {
2336 cpl_frame_set_group(frame, cpl_frame_get_group(uframe));
2341 (*aLabeledFrames)[ipos] = frames;
2343 cpl_frameset_delete(frames);
2349 char *keyword = cpl_sprintf(QC_WAVECAL_PREFIXi
" "QC_BASIC_NSATURATED, k+1);
2352 cpl_propertylist_update_int(lampimage->
header, keyword, nsaturated);
2362 cpl_frameset_delete(rawframes);
2363 cpl_frameset_delete(auxframes);
2368 if (aLabeledFrames) {
2369 cpl_free(*aLabeledFrames);
2370 *aLabeledFrames = NULL;
2402 double aHalfWidth,
double aBinWidth,
2403 float aLo,
float aHi,
unsigned char aIter)
2405 cpl_ensure_code(aPt && aLines, CPL_ERROR_NULL_INPUT);
2406 cpl_ensure_code(cpl_array_get_type(aLines) == CPL_TYPE_DOUBLE ||
2407 cpl_array_get_type(aLines) == CPL_TYPE_FLOAT,
2408 CPL_ERROR_ILLEGAL_INPUT);
2412 double shift = 0., wshift = 0.;
2413 cpl_array *errors = cpl_array_new(4, CPL_TYPE_DOUBLE);
2414 int i, n = cpl_array_get_size(aLines), nvalid = 0;
2415 for (i = 0; i < n; i++) {
2417 double lambdasign = cpl_array_get(aLines, i, &err),
2418 lambda = fabs(lambdasign);
2419 if (err || lambda >= lmax || lambda <= lmin) {
2420 cpl_msg_debug(__func__,
"Invalid wavelength at position %d of %d in " 2421 "skylines", i + 1, n);
2426 aBinWidth, aLo, aHi, aIter,
2428 cerr = cpl_array_get_double(errors, 0, NULL);
2429 shift += (lambda - center) / cerr;
2430 wshift += 1. / cerr;
2431 cpl_msg_debug(__func__,
"dlambda = %.4f +/- %.4f (for skyline at %.4f " 2432 "Angstrom)", lambda - center, cerr, lambda);
2434 cpl_array_delete(errors);
2436 if (nvalid > 0 && isfinite(shift)) {
2437 cpl_msg_info(__func__,
"Skyline correction (%d lines): shifting data of IFU " 2441 cpl_propertylist_update_float(aPt->
header, QC_SCIBASIC_SHIFT, shift);
2443 cpl_propertylist_update_float(aPt->
header, QC_SCIBASIC_SHIFT, 0.);
2445 return CPL_ERROR_NONE;
2463 const char *aPrefix,
unsigned aStats)
2465 cpl_ensure_code(aImage, CPL_ERROR_NULL_INPUT);
2467 int nx = cpl_image_get_size_x(aImage),
2468 ny = cpl_image_get_size_y(aImage);
2470 aStats, 1, 1, nx, ny);
2493 cpl_propertylist *aHeader,
2494 const char *aPrefix,
unsigned aStats,
2495 int aX1,
int aY1,
int aX2,
int aY2)
2497 cpl_ensure_code(aImage && aHeader, CPL_ERROR_NULL_INPUT);
2498 cpl_ensure_code(aPrefix, CPL_ERROR_ILLEGAL_INPUT);
2499 cpl_ensure_code(aPrefix, CPL_ERROR_ILLEGAL_INPUT);
2501 cpl_stats *stats = cpl_stats_new_from_image_window(aImage, aStats,
2502 aX1, aY1, aX2, aY2);
2504 return cpl_error_get_code();
2507 char keyword[KEYWORD_LENGTH];
2508 if (aStats & CPL_STATS_MEDIAN) {
2509 snprintf(keyword, KEYWORD_LENGTH,
"%s MEDIAN", aPrefix);
2510 cpl_propertylist_append_float(aHeader, keyword,
2511 cpl_stats_get_median(stats));
2513 if (aStats & CPL_STATS_MEAN) {
2514 snprintf(keyword, KEYWORD_LENGTH,
"%s MEAN", aPrefix);
2515 cpl_propertylist_append_float(aHeader, keyword, cpl_stats_get_mean(stats));
2517 if (aStats & CPL_STATS_STDEV) {
2518 snprintf(keyword, KEYWORD_LENGTH,
"%s STDEV", aPrefix);
2519 cpl_propertylist_append_float(aHeader, keyword, cpl_stats_get_stdev(stats));
2521 if (aStats & CPL_STATS_MIN) {
2522 snprintf(keyword, KEYWORD_LENGTH,
"%s MIN", aPrefix);
2523 cpl_propertylist_append_float(aHeader, keyword, cpl_stats_get_min(stats));
2525 if (aStats & CPL_STATS_MAX) {
2526 snprintf(keyword, KEYWORD_LENGTH,
"%s MAX", aPrefix);
2527 cpl_propertylist_append_float(aHeader, keyword, cpl_stats_get_max(stats));
2529 if (aStats & CPL_STATS_FLUX) {
2530 snprintf(keyword, KEYWORD_LENGTH,
"%s INTFLUX", aPrefix);
2531 cpl_propertylist_append_float(aHeader, keyword, cpl_stats_get_flux(stats));
2534 cpl_stats_delete(stats);
2536 return CPL_ERROR_NONE;
2558 cpl_ensure_code(aImage && aImage->
dq && aImage->
header && aPrefix,
2559 CPL_ERROR_NULL_INPUT);
2561 cpl_mask *mask = cpl_mask_threshold_image_create(aImage->
dq,
2562 EURO3D_SATURATED - 0.1,
2563 EURO3D_SATURATED + 0.1);
2564 int nsaturated = cpl_mask_count(mask);
2565 cpl_mask_delete(mask);
2567 char *keyword = NULL;
2568 if (aPrefix[strlen(aPrefix)-1] ==
' ') {
2569 keyword = cpl_sprintf(
"%s%s", aPrefix, QC_BASIC_NSATURATED);
2571 keyword = cpl_sprintf(
"%s %s", aPrefix, QC_BASIC_NSATURATED);
2573 cpl_error_code rc = cpl_propertylist_update_int(aImage->
header, keyword,
cpl_error_code muse_quadrants_overscan_correct(muse_image *aImage, muse_image *aRefImage)
Adapt bias level to reference image using overscan statistics.
muse_imagelist * muse_basicproc_load(muse_processing *aProcessing, unsigned char aIFU, muse_basicproc_params *aBPars)
Load the raw input files from disk and do basic processing.
#define MUSE_PIXTABLE_XPOS
Structure definition of a MUSE datacube.
Structure definition for a collection of muse_images.
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
void muse_pixtable_extracted_delete(muse_pixtable **aPixtables)
Delete a pixel table array.
double muse_pfits_get_cd(const cpl_propertylist *aHeaders, unsigned int aAxisI, unsigned int aAxisJ)
find out the WCS coordinate at the reference point
int muse_image_divide(muse_image *aImage, muse_image *aDivisor)
Divide a muse_image by another with correct treatment of bad pixels and variance. ...
const char * muse_pfits_get_insmode(const cpl_propertylist *aHeaders)
find out the observation mode
double muse_utils_pixtable_fit_line_gaussian(muse_pixtable *aPixtable, double aLambda, double aHalfWidth, double aBinSize, float aLo, float aHi, unsigned char aIter, cpl_array *aResults, cpl_array *aErrors)
Fit a 1D Gaussian to a given wavelength range in a pixel table.
unsigned short muse_pixtable_origin_get_slice(uint32_t aOrigin)
Get the slice number from the encoded 32bit origin number.
cpl_size * muse_quadrants_get_window(const muse_image *aImage, unsigned char aQuadrant)
Determine the data window of a given quadrant on the CCD.
int muse_image_scale(muse_image *aImage, double aScale)
Scale a muse_image with correct treatment of variance.
int muse_pfits_get_read_id(const cpl_propertylist *aHeaders)
find out the readout mode id
int muse_utils_get_extension_for_ifu(const char *aFilename, unsigned char aIFU)
Return extension number that corresponds to this IFU/channel number.
cpl_size muse_pixtable_extracted_get_size(muse_pixtable **aPixtables)
Get the size of an array of extracted pixel tables.
double muse_pfits_get_crval(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the WCS coordinate at the reference point
unsigned char muse_utils_get_ifu(const cpl_propertylist *aHeaders)
Find out the IFU/channel from which this header originated.
muse_imagelist * muse_basicproc_combine_images_lampwise(muse_processing *aProcessing, unsigned char aIFU, muse_basicproc_params *aBPars, cpl_frameset ***aLabeledFrames)
Combine several images into a lampwise image list.
muse_pixtable ** muse_pixtable_extracted_get_slices(muse_pixtable *aPixtable)
Extract one pixel table per IFU and slice.
double muse_pfits_get_gain(const cpl_propertylist *aHeaders, unsigned char aQuadrant)
find the detector gain (in units of count/adu)
cpl_image * data
the data extension
muse_image * muse_image_load_from_raw(const char *aFilename, int aExtension)
Load raw image into the data extension of a MUSE image.
cpl_size muse_pixtable_get_nrow(const muse_pixtable *aPixtable)
get the number of rows within the pixel table
int muse_pfits_get_shut_status(const cpl_propertylist *aHeaders, int aShutter)
query the status of one shutter
#define MUSE_HDR_PT_LHI
FITS header keyword contains the upper limit of the data in spectral direction.
int muse_cosmics_dcr(muse_image *aImage, unsigned int aXBox, unsigned int aYBox, unsigned int aPasses, float aThres)
Quickly mark cosmic rays in an image using the DCR algorithm.
muse_image * muse_image_duplicate(const muse_image *aImage)
Duplicate the three image extensions and the FITS headers of a MUSE image.
#define MUSE_HDR_PT_ILLUMi
cpl_image * stat
the statistics extension
int muse_image_subtract(muse_image *aImage, muse_image *aSubtract)
Subtract a muse_image from another with correct treatment of bad pixels and variance.
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
#define MUSE_HDR_PT_ILLUM_MEAN
muse_imagelist * muse_basicproc_load_reduced(muse_processing *aProcessing, unsigned char aIFU)
Load reduced input files from disk.
muse_basicproc_params * muse_basicproc_params_new(cpl_parameterlist *aParameters, const char *aPrefix)
Create a new structure of basic processing parameters.
muse_image * muse_combine_images(muse_combinepar *aCPars, muse_imagelist *aImages)
Combine several images into one.
Structure definition of MUSE three extension FITS file.
cpl_error_code muse_quadrants_overscan_polyfit_vertical(muse_image *aImage, unsigned aIgnore, unsigned char aOrder, double aSigma, const double aFRMS, double aFChiSq)
Correct quadrants by polynomial representation of vertical overscan.
const char * muse_pfits_get_shut_name(const cpl_propertylist *aHeaders, int aShutter)
query the name of one shutter
cpl_table * table
The pixel table.
void muse_basicproc_params_delete(muse_basicproc_params *aBPars)
Free a structure of basic processing parameters.
cpl_propertylist * header
the FITS header
cpl_boolean muse_processing_check_intags(muse_processing *aProcessing, const char *aTag, int aNChars)
Check that a tag is part of the input tags of a processing structure.
int muse_image_variance_create(muse_image *aImage, muse_image *aBias)
Create the photon noise-based variance in the stat extension.
cpl_table * muse_cpltable_load(const char *aFile, const char *aExtension, const muse_cpltable_def aDefinition[])
Load a table from disk (and check against definition).
#define MUSE_PIXTABLE_DATA
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
cpl_frameset * usedframes
void muse_combinepar_delete(muse_combinepar *aCPars)
Clear the combination parameters.
cpl_image * dq
the data quality extension
cpl_error_code muse_pixtable_restrict_wavelength(muse_pixtable *aPixtable, double aLow, double aHigh)
Restrict a pixel table to a certain wavelength range.
double muse_pfits_get_crpix(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the WCS reference point
int muse_pfits_get_biny(const cpl_propertylist *aHeaders)
find out the binning factor in y direction
Structure definition of MUSE pixel table.
cpl_boolean muse_quadrants_overscan_check(muse_image *aImage, muse_image *aRefImage, double aSigma)
Compare overscan statistics of all quadrants to those of reference image.
cpl_error_code muse_basicproc_shift_pixtable(muse_pixtable *aPt, cpl_array *aLines, double aHalfWidth, double aBinWidth, float aLo, float aHi, unsigned char aIter)
Compute wavelength corrections for science data based on reference sky lines.
const char * muse_pfits_get_lamp_name(const cpl_propertylist *aHeaders, int aLamp)
query the name of one lamp
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
cpl_error_code muse_basicproc_apply_twilight(muse_pixtable *aPT, muse_datacube *aTwilight)
Apply an attached flat-field to a pixel table.
cpl_error_code muse_basicproc_stats_append_header_window(cpl_image *aImage, cpl_propertylist *aHeader, const char *aPrefix, unsigned aStats, int aX1, int aY1, int aX2, int aY2)
Compute image statistics of an image window and add them to a header.
int muse_quality_set_saturated(muse_image *aImage)
Set all pixels above the saturation limit in the bad pixel image.
const char * muse_pfits_get_read_name(const cpl_propertylist *aHeaders)
find out the readout mode name
int muse_imagelist_is_uniform(muse_imagelist *aList)
Check that all images in the muse_imagelist have the same size.
muse_combinepar * muse_combinepar_new(cpl_parameterlist *aParameters, const char *aPrefix)
Create a new set of combination parameters.
cpl_imagelist * data
the cube containing the actual data values
void muse_processing_append_used(muse_processing *aProcessing, cpl_frame *aFrame, cpl_frame_group aGroup, int aDuplicate)
Add a frame to the set of used frames.
muse_pixtable * muse_pixtable_create(muse_image *aImage, cpl_table *aTrace, cpl_table *aWave, cpl_table *aGeoTable)
Create the pixel table for one CCD.
#define MUSE_PIXTABLE_ORIGIN
#define MUSE_HDR_PT_LLO
FITS header keyword contains the lower limit of the data in spectral direction.
const char * muse_pfits_get_chip_id(const cpl_propertylist *aHeaders)
find out the chip id
cpl_error_code muse_quadrants_overscan_stats(muse_image *aImage, const char *aRejection, unsigned int aIgnore)
Compute overscan statistics of all quadrants and save in FITS header.
cpl_error_code muse_basicproc_stats_append_header(cpl_image *aImage, cpl_propertylist *aHeader, const char *aPrefix, unsigned aStats)
Compute image statistics of an image and add them to a header.
cpl_table * muse_basicproc_get_illum(muse_imagelist *aImages, cpl_table *aTrace, cpl_table *aWave, cpl_table *aGeo)
Get an illum/attached flat-field from an imagelist and prepare it for use.
int muse_pfits_get_binx(const cpl_propertylist *aHeaders)
find out the binning factor in x direction
cpl_propertylist * header
the FITS header
#define MUSE_PIXTABLE_STAT
cpl_error_code muse_image_save(muse_image *aImage, const char *aFilename)
Save the three image extensions and the FITS headers of a MUSE image to a file.
muse_image * muse_image_load(const char *aFilename)
Load the three extensions and the FITS headers of a MUSE image from a file.
int muse_pfits_get_lamp_status(const cpl_propertylist *aHeaders, int aLamp)
query the status of one lamp
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
#define MUSE_PIXTABLE_LAMBDA
cpl_error_code muse_basicproc_mask_notch_filter(muse_pixtable *aPT, unsigned char aIFU)
Mask the range of the NaD notch filter in the given pixel table.
muse_basicproc_params * muse_basicproc_params_new_from_propertylist(const cpl_propertylist *aHeader)
Create a structure of basic processing parameters from a FITS header.
double muse_pfits_get_exptime(const cpl_propertylist *aHeaders)
find out the exposure time
cpl_error_code muse_processing_check_input(muse_processing *aProcessing, unsigned char aIFU)
Check the input files for completeness.
Structure of basic processing parameters.
const char * muse_pfits_get_pro_catg(const cpl_propertylist *aHeaders)
find out the PRO category
#define MUSE_HDR_PT_ILLUM_STDEV
cpl_error_code muse_basicproc_apply_illum(muse_pixtable *aPT, cpl_table *aAttached)
Apply an illum/attached flat-field to a pixel table.
cpl_frameset * muse_frameset_check_raw(const cpl_frameset *aFrames, const cpl_array *aTags, unsigned char aIFU)
return frameset containing good raw input data
cpl_error_code muse_image_adu_to_count(muse_image *aImage)
Convert the data units from raw adu to count (= electron) units.
cpl_frameset * muse_frameset_find(const cpl_frameset *aFrames, const char *aTag, unsigned char aIFU, cpl_boolean aInvert)
return frameset containing data from an IFU/channel with a certain tag
cpl_parameter * muse_cplparamerterlist_find_prefix(cpl_parameterlist *aParameters, const char *aPrefix, const char *aName)
Return the full recipe parameter belonging to prefix and shortname.
muse_image * muse_image_load_from_extensions(const char *aFilename, unsigned char aIFU)
Load the three extensions and the FITS headers of a MUSE image from extensions of a merged file...
#define MUSE_PIXTABLE_YPOS
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
cpl_error_code muse_imagelist_set(muse_imagelist *aList, muse_image *aImage, unsigned int aIdx)
Set the muse_image of given list index.
const char * muse_pfits_get_chip_name(const cpl_propertylist *aHeaders)
find out the chip name
cpl_frame * muse_frameset_find_master(const cpl_frameset *aFrames, const char *aTag, unsigned char aIFU)
find the master frame according to its CCD number and tag
muse_ins_mode muse_pfits_get_mode(const cpl_propertylist *aHeaders)
find out the observation mode
muse_image * muse_imagelist_unset(muse_imagelist *aList, unsigned int aIdx)
Unset the muse_image at given list index from the list.
cpl_parameterlist * parameters
muse_image * muse_quadrants_trim_image(muse_image *aImage)
Trim the input image of pre- and over-scan regions of all quadrants.
cpl_parameterlist * muse_cplparameterlist_from_propertylist(const cpl_propertylist *aHeader, int aRecNum)
Recreate a cpl_parameterlist from the RECi headers of an output MUSE product.
cpl_propertylist * header
The FITS header.
cpl_frameset * muse_frameset_find_tags(const cpl_frameset *aFrames, const cpl_array *aTags, unsigned char aIFU, cpl_boolean aInvert)
return frameset containing data from an IFU/channel with the given tag(s)
cpl_error_code muse_basicproc_qc_saturated(muse_image *aImage, const char *aPrefix)
Add QC parameter about saturated pixels to a muse_image.