38#include "irplib_plugin.h"
39#include "irplib_utils.h"
50detmon_lg_dfs_set_groups(cpl_frameset * set,
51 const char *tag_on,
const char *tag_off);
53static int check_regions(
const cpl_size *,
const cpl_size *,
54 const cpl_size *,
const cpl_size *,
55 cpl_size) CPL_ATTR_NONNULL;
57static int compare_tags(
const cpl_frame *,
const cpl_frame *) CPL_ATTR_NONNULL;
59static const cpl_parameter *
60get_parameter_const(
const cpl_parameterlist * parlist,
61 const char * recipe,
const char * name)
63 char * parname = cpl_sprintf(
"detmon.%s.%s", recipe, name);
64 const cpl_parameter * p = cpl_parameterlist_find_const(parlist, parname);
70get_parameter(cpl_parameterlist * parlist,
71 const char * recipe,
const char * name)
73 char * parname = cpl_sprintf(
"detmon.%s.%s", recipe, name);
74 cpl_parameter * p = cpl_parameterlist_find(parlist, parname);
79static int compare_tags(
const cpl_frame * a,
const cpl_frame * b)
81 return strcmp(cpl_frame_get_tag(a), cpl_frame_get_tag(b)) == 0;
98static cpl_error_code merge_keys(cpl_propertylist * mplist,
99 const cpl_propertylist * plist_,
101 const char * rmregexp)
105 cpl_propertylist * plist = cpl_propertylist_duplicate(plist_);
108 cpl_propertylist_erase_regexp(plist, rmregexp, 0);
109 cpl_propertylist_erase_regexp(mplist, rmregexp, 0);
112 for (cpl_size i = 0; i < cpl_propertylist_get_size(plist); i++) {
113 cpl_property * p = cpl_propertylist_get(plist, i);
114 const char * key = cpl_property_get_name(p);
116 if (!strncmp(key,
"ESO QC ", 7)) {
118 sprintf(buffer,
"ESO QC REG%d %s", pos, key + 7);
119 cpl_msg_debug(cpl_func,
"merging %s to %s", key, buffer);
120 if (cpl_property_get_type(p) == CPL_TYPE_INT) {
121 int val = cpl_property_get_int(p);
122 cpl_propertylist_append_int(mplist, buffer, val);
123 }
else if (cpl_property_get_type(p) == CPL_TYPE_DOUBLE) {
124 double val = cpl_property_get_double(p);
125 cpl_propertylist_append_double(mplist, buffer, val);
126 }
else if (cpl_property_get_type(p) == CPL_TYPE_STRING) {
127 const char * val = cpl_property_get_string(p);
128 cpl_propertylist_append_string(mplist, buffer, val);
132 com = cpl_property_get_comment(p);
133 cpl_propertylist_set_comment(mplist, buffer, com);
136 if (cpl_propertylist_has(mplist, key))
137 cpl_propertylist_erase(mplist, key);
143 cpl_propertylist_delete(plist);
145 return cpl_error_get_code();
159merge_images(cpl_imagelist * mimgl,
const cpl_imagelist * img)
161 cpl_ensure_code(cpl_imagelist_get_size(img) ==
162 cpl_imagelist_get_size(mimgl), CPL_ERROR_ILLEGAL_INPUT);
163 for (
int i = 0; i < cpl_imagelist_get_size(img); i++) {
164 cpl_image * mimg = cpl_imagelist_get(mimgl, i);
165 const cpl_image * part = cpl_imagelist_get_const(img, i);
166 cpl_image_add(mimg, part);
169 return cpl_error_get_code();
183merge_table(cpl_table * mtab, cpl_table * tab,
const int pos)
185 cpl_table_new_column(tab,
"REGION", CPL_TYPE_INT);
186 cpl_table_fill_column_window(tab,
"REGION", 0, cpl_table_get_nrow(tab), pos);
187 skip_if(cpl_table_insert(mtab, tab, cpl_table_get_nrow(mtab)));
191 return cpl_error_get_code();
196parse_token(
const char * token, cpl_size * llx, cpl_size * lly,
197 cpl_size * urx, cpl_size * ury)
199 assert(
sizeof(*llx) == 8);
200 if (sscanf(token,
" %lld , %lld , %lld , %lld ", llx, lly, urx, ury) == 4) {
201 return CPL_ERROR_NONE;
203 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
204 "Data not formated correctly: %s; "
205 "expecting: name,llx,lly,urx,ury",
225read_regions_file(
const char * filename, cpl_size ** rllx,
226 cpl_size ** rlly, cpl_size ** rurx, cpl_size ** rury,
229 FILE * f = fopen(filename,
"r");
234 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO,
235 "Could not open file %s: %s", filename,
238 cpl_msg_info(cpl_func,
"Reading %s", filename);
241 *rllx = cpl_malloc(
sizeof(**rllx) * nmax);
242 *rlly = cpl_malloc(
sizeof(**rllx) * nmax);
243 *rurx = cpl_malloc(
sizeof(**rllx) * nmax);
244 *rury = cpl_malloc(
sizeof(**rllx) * nmax);
246 while (fgets(line, 1024, f) != NULL) {
247 const size_t nline = strlen(line);
248 if (line[nline - 1] !=
'\n') {
249 cpl_error_set_message(cpl_func, CPL_ERROR_FILE_IO,
250 "File %s contained too long line",
258 skip_if(parse_token(line, &(*rllx)[i], &(*rlly)[i],
259 &(*rurx)[i], &(*rury)[i]));
263 *rllx = cpl_realloc(*rllx,
sizeof(**rllx) * nmax);
264 *rlly = cpl_realloc(*rlly,
sizeof(**rllx) * nmax);
265 *rurx = cpl_realloc(*rurx,
sizeof(**rllx) * nmax);
266 *rury = cpl_realloc(*rury,
sizeof(**rllx) * nmax);
274 return CPL_ERROR_NONE;
293read_regions_str(
const char * istr, cpl_size ** rllx,
294 cpl_size ** rlly, cpl_size ** rurx, cpl_size ** rury,
298 char * str_ = cpl_strdup(istr);
301 *rllx = cpl_malloc(
sizeof(**rllx) * nmax);
302 *rlly = cpl_malloc(
sizeof(**rllx) * nmax);
303 *rurx = cpl_malloc(
sizeof(**rllx) * nmax);
304 *rury = cpl_malloc(
sizeof(**rllx) * nmax);
306 for (
int i = 0; ;i++) {
307 const char * token = strtok(str,
":");
311 skip_if(parse_token(token, &(*rllx)[i], &(*rlly)[i],
312 &(*rurx)[i], &(*rury)[i]));
316 *rllx = cpl_realloc(*rllx,
sizeof(**rllx) * nmax);
317 *rlly = cpl_realloc(*rlly,
sizeof(**rllx) * nmax);
318 *rurx = cpl_realloc(*rurx,
sizeof(**rllx) * nmax);
319 *rury = cpl_realloc(*rury,
sizeof(**rllx) * nmax);
328 return CPL_ERROR_NONE;
331static int check_regions(
const cpl_size * llx,
const cpl_size * lly,
332 const cpl_size * urx,
const cpl_size * ury,
335 for (cpl_size i = 0; i < nregions; i++) {
336 if (llx[i] < 1 || lly[i] < 1 ||
337 urx[i] < 1 || ury[i] < 1) {
338 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
339 "Region %lld (%lld,%lld,%lld,%lld) has entry smaller one",
340 i, llx[i], lly[i], urx[i], ury[i]);
342 if ((urx[i] - llx[i] < 1) || (ury[i] - lly[i] < 1)) {
343 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
344 "Region %lld (%lld,%lld,%lld,%lld) has size zero",
345 i, llx[i], lly[i], urx[i], ury[i]);
347 for (cpl_size j = i + 1; j < nregions; j++) {
348 if (!(llx[i] > urx[j] || urx[i] < llx[j] ||
349 lly[i] > ury[j] || ury[i] < lly[j])) {
350 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
351 "Regions %lld (%lld,%lld,%lld,%lld) and "
352 "%lld (%lld,%lld,%lld,%lld) overlap (llx,lly,urx,ury)",
353 i, llx[i], lly[i], urx[i], ury[i],
354 j, llx[j], lly[j], urx[j], ury[j]);
359 return CPL_ERROR_NONE;
378merge_lingain(cpl_propertylist * mplist[], cpl_table * mtab[],
379 const char * fn,
const cpl_size next,
380 const char * rmregexp,
int pos)
382 cpl_errorstate cleanstate = cpl_errorstate_get();
385 cpl_propertylist * plist = cpl_propertylist_load(fn, 0);
386 skip_if(plist == NULL);
387 if (mplist[0] == NULL) {
389 merge_keys(mplist[0], plist, pos, rmregexp);
392 merge_keys(mplist[0], plist, pos, rmregexp);
393 cpl_propertylist_delete(plist);
398 for (
int e = 1; e < next + 1; e++) {
399 cpl_propertylist * plist = cpl_propertylist_load(fn, e);
400 cpl_table * tab = cpl_table_load(fn, e, 0);
403 cpl_errorstate_set(cleanstate);
404 cpl_propertylist_delete(plist);
407 skip_if(plist == NULL);
409 if (mplist[e] == NULL) {
410 merge_keys(plist, plist, pos, rmregexp);
411 cpl_table_new_column(tab,
"REGION", CPL_TYPE_INT);
412 cpl_table_fill_column_window(tab,
"REGION", 0,
413 cpl_table_get_nrow(tab), 1);
419 merge_keys(mplist[e], plist, pos, rmregexp);
420 merge_table(mtab[e], tab, pos);
422 cpl_propertylist_delete(plist);
423 cpl_table_delete(tab);
430 return cpl_error_get_code();
449merge_bpmcoeff(cpl_propertylist * mplist[], cpl_imagelist * mimg[],
450 const char * fn,
const cpl_size next,
451 const char * rmregexp,
int pos)
453 cpl_errorstate cleanstate = cpl_errorstate_get();
455 for (
int e = 0; e < next + 1; e++) {
456 cpl_propertylist * plist = cpl_propertylist_load(fn, e);
457 cpl_imagelist * img = cpl_imagelist_load(fn, CPL_TYPE_UNSPECIFIED, e);
460 cpl_errorstate_set(cleanstate);
461 cpl_propertylist_delete(plist);
464 skip_if(plist == NULL);
466 if (mimg[e] == NULL) {
467 merge_keys(plist, plist, pos, rmregexp);
472 merge_keys(mplist[e], plist, pos, rmregexp);
473 merge_images(mimg[e], img);
475 cpl_propertylist_delete(plist);
476 cpl_imagelist_delete(img);
483 return cpl_error_get_code();
487 DETMON_BP_MAP_NL_IDX = 0,
488 DETMON_LIN_INFO_IDX = 1,
489 DETMON_GAIN_INFO_IDX = 2,
491 DETMON_COEFF_IDX = 4,
495static cpl_error_code disable_contamination(cpl_parameterlist * parlist,
500 for (
int i = 1; i < 6; i++) {
502 sprintf(buffer,
"detmon.%s.llx%d", recipe, i);
503 par = cpl_parameterlist_find(parlist, buffer);
504 cpl_parameter_set_int(par, 1);
505 sprintf(buffer,
"detmon.%s.lly%d", recipe, i);
506 par = cpl_parameterlist_find(parlist, buffer);
507 cpl_parameter_set_int(par, 1);
508 sprintf(buffer,
"detmon.%s.urx%d", recipe, i);
509 par = cpl_parameterlist_find(parlist, buffer);
510 cpl_parameter_set_int(par, 2);
511 sprintf(buffer,
"detmon.%s.ury%d", recipe, i);
512 par = cpl_parameterlist_find(parlist, buffer);
513 cpl_parameter_set_int(par, 2);
516 return cpl_error_get_code();
519static cpl_error_code detmon_lg_mr_phu_qc_reg_kw(
520 cpl_propertylist** mplist[],
524 const char * kw_segment,
530 char * kw_input = NULL;
531 char* kw_output_avg = NULL ;
532 char* kw_output_rms = NULL;
533 cpl_array* kw_vals = cpl_array_new(next, kw_type);
538 if (mplist[idx][0] == NULL) {
539 mplist[idx][0] = cpl_propertylist_new();
543 for (
int i=0; i < nregions; i++) {
545 kw_input = cpl_sprintf(
"ESO QC REG%d %s", i+1, kw_segment);
546 cpl_msg_info(cpl_func,
"Look for header kw %s", kw_input);
547 kw_output_avg = cpl_sprintf(
"ESO QC REG%d %s AVG", i+1, kw_segment);
548 kw_output_rms = cpl_sprintf(
"ESO QC REG%d %s RMS", i+1, kw_segment);
549 cpl_array_fill_window_invalid(kw_vals, 0, next);
555 for (cpl_size e = 1 ; e < next + 1 ; ++e) {
556 if (cpl_propertylist_has(mplist[idx][e], kw_input)) {
559 " Found value %s in ext %" CPL_SIZE_FORMAT
"",
561 if (kw_type == CPL_TYPE_DOUBLE) {
562 cpl_array_set_double(kw_vals, e-1,
563 cpl_propertylist_get_double(
564 mplist[idx][e], kw_input));
565 }
else if (kw_type == CPL_TYPE_INT) {
566 cpl_array_set_int(kw_vals, e-1,
567 cpl_propertylist_get_int(
568 mplist[idx][e], kw_input));
572 cpl_msg_info(cpl_func,
" Array populated");
574 cpl_propertylist_append_double(mplist[idx][0],
576 cpl_array_get_mean(kw_vals));
577 cpl_msg_info(cpl_func,
" Set mean");
578 cpl_propertylist_set_comment(
581 cpl_sprintf(
"Avg of QC REG%d %s over exts",
583 cpl_msg_info(cpl_func,
" Set mean comment");
584 cpl_propertylist_append_double(mplist[idx][0],
586 cpl_array_get_stdev(kw_vals));
587 cpl_msg_info(cpl_func,
" Set RMS");
588 cpl_propertylist_set_comment(
591 cpl_sprintf(
"RMS of QC REG%d %s over exts",
595 if (cpl_error_get_code() != CPL_ERROR_NONE) {
596 cpl_msg_error(__func__,
"Error found in detmon_lg_mr_phu_qc_reg_kw!");
597 cpl_msg_error(__func__,
"In %s, L%u", cpl_error_get_file(),
598 cpl_error_get_line());
599 cpl_msg_error(__func__,
"%s", cpl_error_get_message());
600 cpl_msg_error(__func__,
"Values ingested:");
601 cpl_array_dump(kw_vals, 0, next, stdout);
602 cpl_msg_error(__func__,
"There were %d extensions", next);
603 cpl_msg_error(__func__,
"There were %d regions detected", nregions);
612 cpl_free(kw_output_rms);
613 cpl_free(kw_output_avg);
614 cpl_array_delete(kw_vals);
617 return cpl_error_get_code();
631int detmon_lg_mr(cpl_frameset * frameset,
632 const cpl_parameterlist * parlist_,
636 const char * tag_on=NULL;
637 const char * tag_off=NULL;
638 IRPLIB_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
639 cpl_parameterlist * parlist = (cpl_parameterlist *)parlist_;
640 IRPLIB_DIAG_PRAGMA_POP;
641 cpl_propertylist ** mplist[DETMON_MAX_IDX] = {NULL};
642 cpl_imagelist ** mimg[DETMON_MAX_IDX] = {NULL};
643 cpl_table ** mtab[DETMON_MAX_IDX] = {NULL};
644 cpl_size next[DETMON_MAX_IDX] = {0};
645 char * outname[DETMON_MAX_IDX] = {NULL};
646 char * outtag[DETMON_MAX_IDX] = {NULL};
647 cpl_size nregions = 0;
649 cpl_frameset * frameset_copy = NULL;
651 cpl_propertylist * lintbl =
652 detmon_fill_prolist(
"DET_LIN_INFO",
"REDUCED",
"TECH", CPL_FALSE);
654 cpl_propertylist * gaintbl =
655 detmon_fill_prolist(
"GAIN_INFO",
"REDUCED",
"TECH", CPL_FALSE);
657 cpl_propertylist * coeffscube =
658 detmon_fill_prolist(
"COEFFS_CUBE",
"REDUCED",
"TECH", CPL_FALSE);
660 cpl_propertylist * bpm =
661 detmon_fill_prolist(
"BP_MAP_NL",
"REDUCED",
"TECH", CPL_FALSE);
663 cpl_propertylist * corr =
664 detmon_fill_prolist(
"AUTOCORR",
"REDUCED",
"TECH", CPL_FALSE);
666 cpl_propertylist * diff_flat =
667 detmon_fill_prolist(
"DIFF_FLAT",
"REDUCED",
"TECH", CPL_FALSE);
668 const char * regions_file = NULL;
669 const char * regions_str = NULL;
670 cpl_error_code error = CPL_ERROR_NONE;
671 cpl_size * llx = NULL, * lly = NULL, * urx = NULL, * ury = NULL;
672 const cpl_parameter * p =
673 get_parameter_const(parlist, recipe,
"regions-file");
675 regions_file = cpl_parameter_get_string(p);
676 if (strlen(regions_file) == 0)
680 p = get_parameter_const(parlist, recipe,
"regions");
682 regions_str = cpl_parameter_get_string(p);
683 if (strlen(regions_str) == 0)
687 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
688 "Please only provide either "
690 "--regions-file, not both");
695 p = get_parameter(parlist, recipe,
"intermediate");
697 error_if(cpl_parameter_get_bool(p), CPL_ERROR_UNSUPPORTED_MODE,
698 "--intermediate not supported by multi region recipe");
701 skip_if (detmon_lg_set_tag(frameset, &tag_on, &tag_off));
702 skip_if (detmon_lg_dfs_set_groups(frameset, tag_on, tag_off));
705 read_regions_file(regions_file, &llx, &lly, &urx, &ury,
708 else if (regions_str) {
709 read_regions_str(regions_str, &llx, &lly, &urx, &ury,
713 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
714 "Empty value in --regions or "
719 skip_if(check_regions(llx, lly, urx, ury, nregions));
723 skip_if(disable_contamination(parlist, recipe));
729 for (
int i = 0; i < nregions; i++) {
731 cpl_size * selection;
733 par = get_parameter(parlist, recipe,
"llx");
734 cpl_parameter_set_int(par, llx[i]);
735 par = get_parameter(parlist, recipe,
"lly");
736 cpl_parameter_set_int(par, lly[i]);
737 par = get_parameter(parlist, recipe,
"urx");
738 cpl_parameter_set_int(par, urx[i]);
739 par = get_parameter(parlist, recipe,
"ury");
740 cpl_parameter_set_int(par, ury[i]);
741 cpl_msg_info(cpl_func,
"working on region %d/%d: %lld %lld, "
742 "%lld %lld", i + 1, (
int)nregions,
743 llx[i], lly[i], urx[i], ury[i]);
747 cpl_frameset_delete(frameset_copy);
748 frameset_copy = cpl_frameset_duplicate(frameset);
749 error = detmon_lg(frameset_copy,
756 lintbl, gaintbl, coeffscube,
757 bpm, corr, diff_flat,
758 PACKAGE
"/" PACKAGE_VERSION,
762 selection = cpl_frameset_labelise(frameset_copy, compare_tags,
765 for (cpl_size j = 0; j < nlabels; j++) {
766 cpl_frameset * a = cpl_frameset_extract(frameset_copy, selection, j);
767 cpl_frame * frm = cpl_frameset_get_position(a, 0);
768 const char * fn = cpl_frame_get_filename(frm);
771 if (strcmp(cpl_frame_get_tag(frm),
"DET_LIN_INFO") == 0)
772 idx = DETMON_LIN_INFO_IDX;
773 else if (strcmp(cpl_frame_get_tag(frm),
"GAIN_INFO") == 0)
774 idx = DETMON_GAIN_INFO_IDX;
775 else if (strcmp(cpl_frame_get_tag(frm),
"BP_MAP_NL") == 0)
776 idx = DETMON_BPM_IDX;
777 else if (strcmp(cpl_frame_get_tag(frm),
"COEFFS_CUBE") == 0)
778 idx = DETMON_COEFF_IDX;
780 cpl_frameset_delete(a);
785 if (mplist[idx] == NULL) {
786 next[idx] = cpl_frame_get_nextensions(frm);
787 outname[idx] = cpl_strdup(cpl_frame_get_filename(frm));
788 outtag[idx] = cpl_strdup(cpl_frame_get_tag(frm));
789 mplist[idx] = cpl_calloc(
sizeof(cpl_propertylist*), next[idx] + 1);
790 mimg[idx] = cpl_calloc(
sizeof(cpl_imagelist*), next[idx] + 1);
791 mtab[idx] = cpl_calloc(
sizeof(cpl_table*), next[idx] + 1);
794 if (idx == DETMON_LIN_INFO_IDX) {
795 const char * rmregexp =
"ESO QC CONTAM.*";
796 merge_lingain(mplist[idx], mtab[idx], fn, next[idx],
799 else if (idx == DETMON_GAIN_INFO_IDX) {
800 merge_lingain(mplist[idx], mtab[idx], fn, next[idx],
803 else if (idx == DETMON_BPM_IDX) {
804 merge_bpmcoeff(mplist[idx], mimg[idx], fn, next[idx],
807 else if (idx == DETMON_COEFF_IDX) {
808 merge_bpmcoeff(mplist[idx], mimg[idx], fn, next[idx],
813 for (
int e = 0; e < next[idx] + 1; e++) {
815 cpl_propertylist * copy_plist;
821 copy_plist = cpl_propertylist_duplicate(mplist[idx][e]);
822 cpl_propertylist_erase_regexp(copy_plist,
"ESO QC.*", 1);
823 len = cpl_propertylist_get_size(copy_plist);
824 cpl_propertylist_delete(copy_plist);
829 sprintf(buffer,
"ESO QC REG%d LLX", i + 1);
830 cpl_propertylist_append_int(mplist[idx][e], buffer, llx[i]);
831 cpl_propertylist_set_comment(mplist[idx][e], buffer,
832 "Lower left X of region");
833 sprintf(buffer,
"ESO QC REG%d LLY", i + 1);
834 cpl_propertylist_append_int(mplist[idx][e], buffer, lly[i]);
835 cpl_propertylist_set_comment(mplist[idx][e], buffer,
836 "Lower left Y of region");
837 sprintf(buffer,
"ESO QC REG%d URX", i + 1);
838 cpl_propertylist_append_int(mplist[idx][e], buffer, urx[i]);
839 cpl_propertylist_set_comment(mplist[idx][e], buffer,
840 "Upper right X of region");
841 sprintf(buffer,
"ESO QC REG%d URY", i + 1);
842 cpl_propertylist_append_int(mplist[idx][e], buffer, ury[i]);
843 cpl_propertylist_set_comment(mplist[idx][e], buffer,
844 "Upper right Y of region");
847 cpl_frameset_delete(a);
852 cpl_msg_info(__func__,
"Completed looping over regions");
856 cpl_frameset * usedframes = cpl_frameset_new();
857 const cpl_size nframes = cpl_frameset_get_size(frameset);
859 cpl_frameset * rawframes = cpl_frameset_new();
861 for (cpl_size idx = 0; idx < nframes; idx++) {
862 const cpl_frame * frm = cpl_frameset_get_position_const(frameset,
864 if (cpl_frame_get_group(frm) == CPL_FRAME_GROUP_RAW ||
865 cpl_frame_get_group(frm) == CPL_FRAME_GROUP_CALIB) {
866 cpl_frameset_insert(usedframes, cpl_frame_duplicate(frm));
868 if (cpl_frame_get_group(frm) == CPL_FRAME_GROUP_RAW) {
869 cpl_frameset_insert(rawframes, cpl_frame_duplicate(frm));
873 cpl_array* reg_values = NULL;
874 char * var_part = NULL;
878 for (
int idx = 0; idx < DETMON_MAX_IDX; idx++) {
879 cpl_msg_info(__func__,
"Doing idx loop %d of %d", idx,
884 if ((idx == DETMON_LIN_INFO_IDX || idx == DETMON_GAIN_INFO_IDX)) {
888 double exptime = 0.0 ;
889 reg_values = cpl_array_new(nregions,
892 if (!is_nir && next[idx] > 0) {
893 cpl_msg_info(cpl_func,
"Doing PIPE-10123 lin/gain kw for %s",
898 r < cpl_frameset_get_size(rawframes);
900 const cpl_frame * frm = cpl_frameset_get_position_const(
903 const char * rfn = cpl_frame_get_filename(frm);
904 const cpl_propertylist * rpl = cpl_propertylist_load(
908 cpl_propertylist_has(rpl,
"EXPTIME")) {
909 if (cpl_propertylist_get_double(rpl,
"EXPTIME") >
911 exptime = cpl_propertylist_get_double(
916 cpl_propertylist_append_double(mplist[idx][0],
917 "ESO QC EXPTIME MAX",
919 cpl_propertylist_set_comment(
921 "ESO QC EXPTIME MAX",
922 "Max. EXPTIME from input raws");
924 if (idx == DETMON_GAIN_INFO_IDX) {
926 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
929 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
933 var_part =
"LIN EFF";
934 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
935 nregions,
"GAIN ERR",
937 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
940 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
941 nregions,
"COUNTS MIN",
943 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
944 nregions,
"COUNTS MAX",
946 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
949 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
950 nregions,
"LIN EFF FLUX",
952 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
955 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
958 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
961 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
966 for (cpl_size e = 1; e < next[idx] + 1; e++) {
968 for (
int i=0 ; i < nregions ; i++) {
969 if (cpl_propertylist_has(mplist[idx][e],
970 cpl_sprintf(
"ESO QC REG%d %s",
972 cpl_array_set_double(
974 cpl_propertylist_get_double(mplist[idx][e],
975 cpl_sprintf(
"ESO QC REG%d %s",
982 cpl_propertylist_append_double(mplist[idx][e],
983 cpl_sprintf(
"ESO QC %s AVG",
987 cpl_propertylist_set_comment(mplist[idx][e],
988 cpl_sprintf(
"ESO QC %s AVG",
993 cpl_propertylist_append_double(mplist[idx][e],
994 cpl_sprintf(
"ESO QC %s RMS",
998 cpl_propertylist_set_comment(mplist[idx][e],
999 cpl_sprintf(
"ESO QC %s RMS",
1002 "RMS of QC.REGn.%s",
1004 cpl_msg_info(__func__,
"Wrote ESO QC RMS");
1008 cpl_msg_info(__func__,
"Doing a GAIN/LIN save (var_part=%s)",
1010 irplib_dfs_save_propertylist(frameset, parlist, usedframes,
1011 recipe, outtag[idx],
1012 mplist[idx][0], NULL, PACKAGE
"/"
1013 PACKAGE_VERSION, outname[idx]);
1016 for (cpl_size e = 1; e < next[idx] + 1; e++) {
1018 cpl_table_save(mtab[idx][e], NULL, mplist[idx][e],
1019 outname[idx], CPL_IO_EXTEND);
1022 cpl_propertylist_save(mplist[idx][e], outname[idx],
1029 else if (idx == DETMON_BPM_IDX) {
1030 cpl_msg_info(__func__,
"Doing a BPM save");
1031 if (!mimg[idx][0]) {
1032 irplib_dfs_save_propertylist(frameset, parlist, usedframes,
1033 recipe, outtag[idx],
1034 mplist[idx][0], NULL, PACKAGE
1035 "/" PACKAGE_VERSION,
1039 irplib_dfs_save_image(frameset, parlist, usedframes,
1040 cpl_imagelist_get(mimg[idx][0], 0),
1041 CPL_TYPE_UNSPECIFIED, recipe,
1042 outtag[idx], mplist[idx][0], NULL,
1043 PACKAGE
"/" PACKAGE_VERSION,
1045 cpl_imagelist_delete(mimg[idx][0]);
1046 mimg[idx][0] = NULL;
1049 for (cpl_size e = 1; e < next[idx] + 1; e++) {
1051 cpl_image * img = cpl_imagelist_get(mimg[idx][e], 0);
1052 cpl_image_save(img, outname[idx], CPL_TYPE_UNSPECIFIED,
1055 cpl_imagelist_delete(mimg[idx][e]);
1056 mimg[idx][e] = NULL;
1059 cpl_propertylist_save(mplist[idx][e], outname[idx],
1065 else if (idx == DETMON_COEFF_IDX) {
1067 if (!is_nir && next[idx] > 0) {
1068 cpl_msg_info(cpl_func,
"Doing PIPE-10123 COEFF kw for %s",
1070 for (
int j=0; j < 4 ; ++j ) {
1071 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
1073 cpl_sprintf(
"LIN COEF%d", j),
1076 detmon_lg_mr_phu_qc_reg_kw(mplist, idx, next[idx],
1081 cpl_msg_info(__func__,
"Doing a COEFFS save");
1082 if (!mimg[idx][0]) {
1083 irplib_dfs_save_propertylist(frameset, parlist, usedframes,
1084 recipe, outtag[idx],
1085 mplist[idx][0], NULL, PACKAGE
1086 "/" PACKAGE_VERSION,
1090 irplib_dfs_save_imagelist(frameset, parlist, usedframes,
1092 CPL_TYPE_UNSPECIFIED,
1093 recipe, outtag[idx],
1094 mplist[idx][0], NULL, PACKAGE
"/"
1095 PACKAGE_VERSION, outname[idx]);
1096 cpl_imagelist_delete(mimg[idx][0]);
1097 mimg[idx][0] = NULL;
1100 for (cpl_size e = 1; e < next[idx] + 1; e++) {
1102 cpl_imagelist_save(mimg[idx][e], outname[idx],
1103 CPL_TYPE_UNSPECIFIED,
1104 mplist[idx][e], CPL_IO_EXTEND);
1105 cpl_imagelist_delete(mimg[idx][e]);
1106 mimg[idx][e] = NULL;
1109 cpl_propertylist_save(mplist[idx][e], outname[idx],
1117 cpl_array_delete(reg_values);
1119 cpl_frameset_delete(usedframes);
1120 cpl_frameset_delete(rawframes);
1125 cpl_frameset_delete(frameset_copy);
1126 cpl_propertylist_delete(lintbl);
1127 cpl_propertylist_delete(gaintbl);
1128 cpl_propertylist_delete(coeffscube);
1129 cpl_propertylist_delete(bpm);
1130 cpl_propertylist_delete(corr);
1131 cpl_propertylist_delete(diff_flat);
1132 for (
int i = 0; i < DETMON_MAX_IDX; i++) {
1133 for (
int e = 0; e < next[i] + 1; e++) {
1135 cpl_propertylist_delete(mplist[i][e]);
1137 cpl_imagelist_delete(mimg[i][e]);
1139 cpl_table_delete(mtab[i][e]);
1141 cpl_free(mplist[i]);
1144 cpl_free(outname[i]);
1145 cpl_free(outtag[i]);
1152 return cpl_error_get_code();