35#include "eris_nix_utils.h"
36#include "eris_utils.h"
37#include "eris_nix_casu_utils.h"
38#include "eris_nix_dfs.h"
39#include "eris_nix_match.h"
42#include <casu_stats.h>
100 const int read_offsets,
101 const cpl_table * refine_wcs,
102 const master_dark * mdark,
103 const gain_linearity * gain_lin,
104 const master_flat * flatfield_1,
105 const master_flat * flatfield_2,
106 const master_bpm * mbad_pix_map,
108 const char * fill_rejected,
109 const double fill_value,
110 const cpl_size x_probe,
111 const cpl_size y_probe) {
113 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
115 if (limage == NULL)
return CPL_ERROR_NONE;
124 if (limage->himage) {
141 enu_check_error_code(
"failure in basic calibration");
143 }
else if (limage->himagelist) {
145 cpl_error_code didfail = CPL_ERROR_NONE;
148 cpl_errorstate cleanstate = cpl_errorstate_get();
150#pragma omp parallel for
152 for (cpl_size plane = 0; plane < nplanes; plane++) {
154 cpl_error_code errori = cpl_error_get_code();
162 if (didfail)
continue;
166 cpl_msg_info(cpl_func,
"basic calibrate cube: plane %d",
189 errori = cpl_error_set_message(cpl_func, cpl_error_get_code(),
190 "Cannot process plane %d/%d",
191 (
int)plane+1, (
int)nplanes);
200 cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
201 cpl_errorstate_set(cleanstate);
203#pragma omp critical(enu_basic_calibrate)
208 if (didfail) (void)cpl_error_set_message(cpl_func, didfail,
209 "Cannot process %d plane(s)",
215 return cpl_error_get_code();
258 cpl_image * confidence,
259 cpl_propertylist * plist,
260 const cpl_frame * frame,
261 const int read_offsets,
262 const master_dark * mdark,
263 const gain_linearity * gain_lin,
264 const master_flat * flatfield_1,
265 const master_flat * flatfield_2,
266 const master_bpm * mbad_pix_map,
267 const int set_confidence,
269 const char * fill_rejected,
270 const double fill_value,
271 const cpl_size x_probe,
272 const cpl_size y_probe) {
274 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
276 cpl_ensure_code(himage, CPL_ERROR_NULL_INPUT);
285 cpl_size nx_chip = 0;
286 cpl_size ny_chip = 0;
287 cpl_boolean windowed = 0;
297 enu_check_error_code(
"failed to read detector window information");
299 cpl_msg_info(cpl_func,
"detector is windowed!");
304 const char * instrume = cpl_propertylist_get_string(plist,
"INSTRUME");
305 const cpl_boolean eris = !strcmp(instrume,
"ERIS");
309 int probe = CPL_FALSE;
310 if ((x_probe >= 0) && (y_probe >= 0)) {
311 if (x_probe < nx && y_probe < ny) {
322 cpl_msg_info(cpl_func,
323 "..basic_calibrate probe (%d,%d) entry(v,e,r)=%5.3e %5.3e %d",
324 (
int) x_probe, (
int)y_probe, val.data, val.error,
327 cpl_msg_info(cpl_func,
328 "..basic_calibrate probe (median) entry(v,e)=%5.3e %5.3e",
329 val.data, val.error);
337 cpl_msg_info(cpl_func,
"basic calibrate: correcting wcs for windowed data");
338 const double crpix1 = cpl_propertylist_get_double(plist,
"CRPIX1");
339 cpl_propertylist_update_double(plist,
"CRPIX1",
340 crpix1 - (
double)strx + 1.0);
341 const double crpix2 = cpl_propertylist_get_double(plist,
"CRPIX2");
342 cpl_propertylist_update_double(plist,
"CRPIX2",
343 crpix2 - (
double)stry + 1.0);
344 cpl_msg_info(cpl_func,
"..crpix %f %f -> %f %f", crpix1, crpix2,
345 crpix1 - (
double)strx + 1.0,
346 crpix2 - (
double)stry + 1.0);
351 if (read_offsets && eris) {
352 cpl_msg_info(cpl_func,
"basic calibrate: removing read-offsets and "
355 enu_check_error_code(
"error removing read-offsets");
363 cpl_msg_info(cpl_func,
364 "..basic_calibrate probe (%d,%d) after "
365 "read_offsets(v,e,r)=%5.3e %5.3e %d",
366 (
int) x_probe, (
int)y_probe,
367 val.data, val.error, reject);
369 cpl_msg_info(cpl_func,
370 "..basic_calibrate probe (median) after "
371 "read_offsets(v,e)=%5.3e %5.3e", val.data, val.error);
376 cpl_msg_info(cpl_func,
"basic calibrate: subtracting master dark");
384 "^ESO DET SEQ1 DIT$|"
385 "^ESO DET READ CURNAME$|"
386 "^ESO DET SEQ1 WIN NX$|"
387 "^ESO DET SEQ1 WIN NY$|"
388 "^ESO DET SEQ1 WIN ROT$|"
389 "^ESO DET SEQ1 WIN STRX$|"
390 "^ESO DET SEQ1 WIN STRY$|"
392 "^ESO DET NCORRS NAME$|"
395 "^ESO DET WIN STARTX$|"
396 "^ESO DET WIN STARTY$");
397 const char * filename = NULL;
399 filename = cpl_frame_get_filename(frame);
401 if (filename == NULL) {
402 enu_check(compatible, CPL_ERROR_INCOMPATIBLE_INPUT,
403 "MASTER_DARK does not match frame");
405 enu_check(compatible, CPL_ERROR_INCOMPATIBLE_INPUT,
406 "MASTER_DARK does not match %s", filename);
412 double gain = cpl_propertylist_get_double(mdark->plist,
414 cpl_propertylist_update_double(plist,
"ESO DARK GAIN", gain);
415 cpl_propertylist_set_comment(plist,
"ESO DARK GAIN",
416 "GAIN from master_dark [e-/ADU]");
417 enu_check_error_code(
"error subtracting DARK");
430 cpl_msg_info(cpl_func,
431 "..basic_calibrate probe (%d,%d) after dark_subtract "
432 "dark(v,e,r)=%5.3e %5.3e %d "
433 "data(v,e,r)=%5.3e %5.3e %d",
434 (
int) x_probe, (
int)y_probe,
435 dark.data, dark.error, reject2,
436 val.data, val.error, reject);
439 cpl_msg_info(cpl_func,
440 "..basic_calibrate probe (median) after dark_subtract "
441 "dark(v,e)=%5.3e %5.3e data(v,e)=%5.3e %5.3e",
442 dark.data, dark.error,
443 val.data, val.error);
448 hdrl_image * himage_copy = NULL;
452 cpl_msg_info(cpl_func,
"basic calibrate: linearizing and estimating "
455 himage_copy = end_linearize_and_variance_detmon(gain_lin,
463 enu_check_error_code(
"error estimating variance");
468 cpl_propertylist_update_double(plist,
469 "ESO DETMON SATURATION",
470 gain_lin->saturation_limit);
471 cpl_propertylist_set_comment(plist,
472 "ESO DETMON SATURATION",
473 "saturation level from "
474 "linearisation [ADU]");
479 cpl_propertylist_update_string(plist,
"BUNIT",
"adu/s");
487 cpl_msg_info(cpl_func,
488 "..basic_calibrate probe (%d,%d) "
489 "after linearize/calc_variance "
490 "data(v,e,r)=%5.3e %5.3e %d",
491 (
int) x_probe, (
int)y_probe, val.data, val.error,
494 cpl_msg_info(cpl_func,
495 "..basic_calibrate probe (median) "
496 "after linearize/calc_variance "
497 "data(v,e)=%5.3e %5.3e", val.data, val.error);
502 cpl_msg_info(cpl_func,
"basic calibrate: dividing by flatfield 1");
509 hdrl_image * flat_window = !windowed ? NULL :
514 const hdrl_image * flat_use = !windowed ? flatfield_1->flat
517 enu_check_error_code(
"error extracting window from flatfield 1");
525 if (set_confidence) {
532 cpl_image * flat_conf_window = !windowed ? NULL :
533 cpl_image_extract(flatfield_1->confidence,
537 const cpl_image * flat_conf_use = !windowed ? flatfield_1->confidence
540 enu_check_error_code(
"error extracting window from flatfield 1 "
542 cpl_image_copy(confidence, flat_conf_use, 1, 1);
543 cpl_image_delete(flat_conf_window);
547 cpl_mask * old_mask =
548 cpl_image_set_bpm(confidence,
550 cpl_mask_delete(old_mask);
551 cpl_image_fill_rejected(confidence, 0.0);
552 (void)cpl_image_unset_bpm(confidence);
554 cpl_image_accept_all(confidence);
558 enu_check_error_code(
"error dividing by flatfield 1");
571 cpl_msg_info(cpl_func,
572 "..basic_calibrate probe (%d,%d) after flatfield1 "
573 "flat(v,e,r)=%5.3e %5.3e %d "
574 "data(v,e,r)=%5.3e %5.3e %d",
575 (
int) x_probe, (
int)y_probe,
576 flat.data, flat.error, reject2,
577 val.data, val.error, reject);
580 cpl_msg_info(cpl_func,
581 "..basic_calibrate probe (median) after flatfield1 "
582 "flat(v,e)=%5.3e %5.3e data(v,e)=%5.3e %5.3e",
583 flat.data, flat.error,
584 val.data, val.error);
591 cpl_msg_info(cpl_func,
"basic calibrate: dividing by flatfield 2");
593 hdrl_image * flat_window = !windowed ? NULL :
598 const hdrl_image * flat_use = !windowed ? flatfield_2->flat
601 enu_check_error_code(
"error extracting window from flatfield 2");
605 if (set_confidence) {
606 cpl_image * flat_conf_window = !windowed ? NULL :
607 cpl_image_extract(flatfield_2->confidence,
611 const cpl_image * flat_conf_use = !windowed ? flatfield_2->confidence
614 enu_check_error_code(
"error extracting window from flatfield 1 "
617 cpl_image_copy(confidence, flat_conf_use, 1, 1);
618 cpl_image_delete(flat_conf_window);
622 cpl_mask * old_mask =
623 cpl_image_set_bpm(confidence,
625 cpl_mask_delete(old_mask);
626 cpl_image_fill_rejected(confidence, 0.0);
627 (void)cpl_image_unset_bpm(confidence);
629 cpl_image_accept_all(confidence);
633 enu_check_error_code(
"error dividing by flatfield 2");
646 cpl_msg_info(cpl_func,
647 "..basic_calibrate probe (%d,%d) after flatfield2 "
648 "flat(v,e,r)=%5.3e %5.3e %d "
649 "data(v,e,r)=%5.3e %5.3e %d",
650 (
int) x_probe, (
int)y_probe,
651 flat.data, flat.error, reject2,
652 val.data, val.error, reject);
655 cpl_msg_info(cpl_func,
656 "..basic_calibrate probe (median) after flatfield2 "
657 "flat(v,e)=%5.3e %5.3e data(v,e)=%5.3e %5.3e",
658 flat.data, flat.error,
659 val.data, val.error);
665 cpl_msg_info(cpl_func,
"basic calibrate: associating with master_bpm");
666 cpl_mask * new_mask = en_master_bpm_get_mask(mbad_pix_map, flag_mask);
670 cpl_mask * new_mask_window = !windowed ? NULL :
671 cpl_mask_extract(new_mask,
675 const cpl_mask * new_mask_use = !windowed ? new_mask : new_mask_window;
677 enu_check_error_code(
"error extracting window from bpm mask");
682 cpl_mask_or(current_mask, new_mask_use);
683 enu_check_error_code(
"error setting new mask");
685 if (set_confidence) {
689 double * conf_data = cpl_image_get_data(confidence);
690 cpl_binary * mask_data = cpl_mask_get_data(current_mask);
691 cpl_size nx_mask = cpl_mask_get_size_x(current_mask);
692 cpl_size ny_mask = cpl_mask_get_size_y(current_mask);
693 for (cpl_size i = 0; i < nx_mask * ny_mask; i++) {
694 if (mask_data[i] == CPL_BINARY_1) {
707 double conf = cpl_image_get(confidence, x_probe, y_probe, &ignore);
708 cpl_msg_info(cpl_func,
"..basic_calibrate probe (%d,%d) "
709 "after associate_mask(v,e,r,c)=%5.3e %5.3e %d %5.3e",
710 (
int) x_probe, (
int)y_probe, val.data, val.error,
713 cpl_mask_delete(new_mask);
714 cpl_mask_delete(new_mask_window);
717 if (!strcmp(fill_rejected,
"noop")) {
718 cpl_msg_info(cpl_func,
"not modifying rejected pixels");
721 if (!strcmp(fill_rejected,
"set_NaN")) {
723 cpl_msg_info(cpl_func,
"basic calibrate: setting rejected pixels "
725 }
else if (!strcmp(fill_rejected,
"set_value")) {
727 cpl_msg_info(cpl_func,
"basic calibrate: setting rejected pixels "
730 enu_check(CPL_FALSE, CPL_ERROR_UNSPECIFIED,
731 "bad fill-rejected value: programming error");
735 enu_check_error_code(
"error setting rejected pixels to %f",
745 double conf = cpl_image_get(confidence, x_probe, y_probe, &ignore);
746 cpl_msg_info(cpl_func,
747 "..basic_calibrate probe (%d,%d) after "
748 "fill-rejected(v,e,r,c)=%5.3e %5.3e %d %5.3e",
749 (
int) x_probe, (
int)y_probe, val.data, val.error,
756 return cpl_error_get_code();;
774 const int min_coadds,
775 const hdrl_parameter * collapse_params,
776 const cpl_size filter_size_x,
777 const cpl_size filter_size_y,
778 const hdrl_flat_method method) {
782 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
783 cpl_ensure(himlist, CPL_ERROR_NULL_INPUT, NULL);
784 cpl_ensure(collapse_params, CPL_ERROR_NULL_INPUT, NULL);
786 cpl_image * contribution = NULL;
787 hdrl_parameter * flat_params = NULL;
788 hdrl_image * result = NULL;
793 enu_check(size >= min_coadds, CPL_ERROR_ILLEGAL_INPUT,
794 "insufficient frames for flat (%d<min:%d)",
795 (
int) size, min_coadds);
802 if (method == HDRL_FLAT_FREQ_HIGH) {
803 cpl_msg_info(cpl_func,
"Calculating high freq flatfield");
804 }
else if (method == HDRL_FLAT_FREQ_LOW) {
805 cpl_msg_info(cpl_func,
"Calculating low freq flatfield");
807 cpl_msg_info(cpl_func,
"..smoothing filter sizes %d %d",
808 (
int)filter_size_x, (
int)filter_size_y);
810 flat_params, &result, &contribution);
811 enu_check_error_code(
"error computing flat-field");
814 cpl_image_delete(contribution);
816 if (cpl_error_get_code() != CPL_ERROR_NONE) {
840 const double fwhm_pix,
843 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
844 cpl_ensure_code(limage, CPL_ERROR_NULL_INPUT);
845 cpl_ensure_code(abmaglim, CPL_ERROR_NULL_INPUT);
848 if (fwhm_pix <= 0.0) {
850 return CPL_ERROR_NONE;
856 double histo_min = 10.;
857 double histo_max = 1.;
860 double bin_size = 0.;
861 cpl_size error_niter = 0;
862 hdrl_mode_type mode_method = HDRL_MODE_MEDIAN;
863 hdrl_parameter * mode_parameter = NULL;
871 double* pconf = cpl_image_get_data_double(limage->confidence);
876 cpl_binary* pbpm_data = cpl_mask_get_data(mask_data);
877 cpl_binary* pbpm_errs = cpl_mask_get_data(mask_errs);
878 for(cpl_size indexj = 0; indexj < sy; indexj++) {
879 for(cpl_size indexi = 0; indexi < sx; indexi++) {
880 if(!isfinite(pdata[indexi+indexj*sx]) ) {
881 pbpm_data[indexi+indexj*sx] = CPL_BINARY_1;
882 pbpm_errs[indexi+indexj*sx] = CPL_BINARY_1;
883 pconf[indexi+indexj*sx] = CPL_BINARY_1;
893 ((
int) (3.0 * fwhm_pix) / 2) * 2 + 1,
894 ((
int) (3.0 * fwhm_pix) / 2) * 2 + 1,
895 HDRL_IMAGE_EXTEND_MIRROR,
901 return cpl_error_get_code();
919 const cpl_propertylist * wcs_plist) {
921 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
922 cpl_ensure_code(catalogue, CPL_ERROR_NULL_INPUT);
923 cpl_ensure_code(wcs_plist, CPL_ERROR_NULL_INPUT);
925 cpl_array * conv_status = NULL;
926 cpl_matrix * ref_phys = NULL;
927 cpl_matrix * ref_world = NULL;
928 cpl_wcs * wcs = NULL;
940 cpl_size nobj = cpl_table_get_nrow(catalogue);
941 const double * ra = cpl_table_get_data_double_const(catalogue,
"RA");
942 const double * dec = NULL;
946 if (cpl_table_has_column(catalogue,
"DEC")) {
947 dec = cpl_table_get_data_double_const(catalogue,
"DEC");
949 ref_world = cpl_matrix_new(nobj, 2);
950 for (cpl_size iobj = 0; iobj < nobj; iobj++) {
951 cpl_matrix_set(ref_world, iobj, 0, ra[iobj]);
952 cpl_matrix_set(ref_world, iobj, 1, dec[iobj]);
955 wcs = cpl_wcs_new_from_propertylist(wcs_plist);
957 cpl_wcs_convert(wcs, ref_world, &ref_phys, &conv_status,
959 enu_check_error_code(
"failure calculating predicted positions");
964 if (!cpl_table_has_column(catalogue,
"X_coordinate")) {
965 cpl_table_new_column(catalogue,
"X_coordinate", CPL_TYPE_DOUBLE);
967 if (!cpl_table_has_column(catalogue,
"Y_coordinate")) {
968 cpl_table_new_column(catalogue,
"Y_coordinate", CPL_TYPE_DOUBLE);
971 for (cpl_size iobj = 0; iobj < nobj; iobj++) {
972 cpl_table_set_double(catalogue,
"X_coordinate", iobj,
973 cpl_matrix_get(ref_phys, iobj, 0));
974 cpl_table_set_double(catalogue,
"Y_coordinate", iobj,
975 cpl_matrix_get(ref_phys, iobj, 1));
985 cpl_array_delete(conv_status);
986 cpl_matrix_delete(ref_phys);
987 cpl_matrix_delete(ref_world);
990 return cpl_error_get_code();
1012 const hdrl_image * himage,
1013 const cpl_image * confidence,
1014 const cpl_wcs * wcs,
1015 hdrl_parameter * params) {
1017 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
1019 cpl_ensure(himage, CPL_ERROR_NULL_INPUT, NULL);
1023 cpl_ensure(wcs, CPL_ERROR_NULL_INPUT, NULL);
1024 cpl_ensure(params, CPL_ERROR_NULL_INPUT, NULL);
1026 hdrl_catalogue_result * result = NULL;
1034 cpl_ensure(cpl_image_get_max(confidence) > 0.0, CPL_ERROR_ILLEGAL_INPUT,
1039 confidence, wcs, params);
1057 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1067 const char * message = cpl_error_get_message();
1068 cpl_msg_info(cpl_func,
"catalogue %s", message);
1070 if (strstr(message,
"No objects found in image")) {
1071 cpl_msg_warning(cpl_func,
"No objects found in image");
1092 const double timerange,
1093 const located_imagelist * pool,
1096 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
1098 cpl_ensure(target, CPL_ERROR_NULL_INPUT, NULL);
1099 cpl_ensure(timerange > 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1100 cpl_ensure(pool, CPL_ERROR_NULL_INPUT, NULL);
1102 cpl_vector * result = NULL;
1103 cpl_vector * result_diff = NULL;
1105 double target_mjd = cpl_propertylist_get_double(target->plist,
"MJD-OBS");
1107 for (cpl_size j = 0; j < pool->size; j++) {
1108 double mjd_j = cpl_propertylist_get_double((pool->limages[j])->
1110 double diff = (target_mjd - mjd_j) * 3600.0 * 24.0;
1112 if (fabs(diff) < timerange/2.0) {
1113 if (result == NULL) {
1114 result = cpl_vector_new(1);
1115 result_diff = cpl_vector_new(1);
1116 cpl_vector_set(result, 0, (
double)j);
1117 cpl_vector_set(result_diff, 0, diff);
1119 cpl_size size = cpl_vector_get_size(result);
1120 cpl_vector_set_size(result, size + 1);
1121 cpl_vector_set_size(result_diff, size + 1);
1122 cpl_vector_set(result, size, (
double)j);
1123 cpl_vector_set(result_diff, size, diff);
1127 enu_check_error_code(
"error in enu_bracket_skys: %s",
1128 cpl_error_get_message());
1129 enu_check(result && cpl_vector_get_size(result) > 0,
1130 CPL_ERROR_INCOMPATIBLE_INPUT,
1131 "no sky frames found within %f sec of target frame", timerange/2.0);
1134 cpl_size size = cpl_vector_get_size(result);
1136 cpl_msg_info(
" ",
".. enu_bracket_skys: timerange %5.1f", timerange);
1137 cpl_msg_info(
" ",
".. j# diff(sec)");
1139 for (cpl_size i = 0; i < size; i++) {
1140 int j = cpl_vector_get(result, i);
1141 double diff = cpl_vector_get(result_diff, i);
1142 cpl_msg_info(
" ",
".. %3d %7.1f", j, diff);
1147 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1148 cpl_vector_delete(result);
1151 cpl_vector_delete(result_diff);
1168 hdrl_parameter * params) {
1170 cpl_ensure_code(cpl_error_get_code() == CPL_ERROR_NONE,
1171 cpl_error_get_code());
1172 cpl_ensure_code(limlist, CPL_ERROR_NULL_INPUT);
1173 cpl_ensure_code(params, CPL_ERROR_NULL_INPUT);
1175 cpl_size njitters = limlist->size;
1176 cpl_msg_info(cpl_func,
"cataloguing %d images", (
int) njitters);
1178 for (cpl_size i = 0; i < njitters; i++) {
1179 cpl_wcs * wcs = cpl_wcs_new_from_propertylist
1180 (limlist->limages[i]->plist);
1181 cpl_msg_info(cpl_func,
"cataloguing image %d", (
int) i);
1183 double* pdata = cpl_image_get_data_double(
hdrl_image_get_image((limlist->limages[i])->himage));
1189 double* pconf = cpl_image_get_data_double(limlist->limages[i]->confidence);
1190 cpl_binary* pbpm_data = cpl_mask_get_data(mask);
1191 cpl_binary* pbpm_errs = cpl_mask_get_data(mskerr);
1192 for(cpl_size indexj = 0; indexj < sy; indexj++) {
1193 for(cpl_size indexi = 0; indexi < sx; indexi++) {
1194 if(!isfinite(pdata[indexi+indexj*sx]) ) {
1195 pbpm_data[indexi+indexj*sx] = CPL_BINARY_1;
1196 pbpm_errs[indexi+indexj*sx] = CPL_BINARY_1;
1197 pconf[indexi+indexj*sx] = 0;
1204 limlist->limages[i]->himage,
1205 limlist->limages[i]->confidence,
1208 cpl_wcs_delete(wcs);
1210 if (cpl_error_get_code() != CPL_ERROR_NONE)
break;
1213 return cpl_error_get_code();
1235 const cpl_propertylist * plist2,
1236 const char * regexp) {
1238 if (cpl_error_get_code() != CPL_ERROR_NONE)
return 0;
1242 cpl_ensure(plist1 && plist2 && regexp, CPL_ERROR_NULL_INPUT, 0);
1244 int result = CPL_TRUE;
1245 char * reason = NULL;
1249 cpl_propertylist * subset1 = cpl_propertylist_new();
1250 cpl_propertylist_copy_property_regexp(subset1, plist1, regexp, 0);
1251 cpl_propertylist * subset2 = cpl_propertylist_new();
1252 cpl_propertylist_copy_property_regexp(subset2, plist2, regexp, 0);
1253 enu_check_error_code(
"error deriving subset of propertylist");
1257 cpl_size size1 = cpl_propertylist_get_size(subset1);
1258 cpl_size size2 = cpl_propertylist_get_size(subset2);
1259 if (size1 != size2) {
1261 reason = cpl_sprintf(
"propertylist sizes do not match %s",
" ");
1263 for (cpl_size i=0; i<size1; i++) {
1264 const cpl_property * prop1 = cpl_propertylist_get(subset1, i);
1265 const char * name = cpl_property_get_name(prop1);
1269 if (!cpl_propertylist_has(subset2, name)) {
1271 reason = cpl_sprintf(
"name mismatch: %s", name);
1277 const cpl_property * prop2 = cpl_propertylist_get_property_const(
1280 const cpl_type type1 = cpl_property_get_type(prop1);
1281 const cpl_type type2 = cpl_property_get_type(prop2);
1282 if (type1 != type2) {
1284 reason = cpl_sprintf(
"type mismatch: name %s: types %s and %s",
1286 cpl_type_get_name(cpl_property_get_type(prop1)),
1287 cpl_type_get_name(cpl_property_get_type(prop2)));
1295 const char * sval1 = NULL;
1296 const char * sval2 = NULL;
1300 ival1 = cpl_property_get_int(prop1);
1301 ival2 = cpl_property_get_int(prop2);
1302 if (ival1 != ival2) {
1304 reason = cpl_sprintf(
"value mismatch: name %s: values "
1306 name, ival1, ival2);
1310 case CPL_TYPE_DOUBLE:
1311 dval1 = cpl_property_get_double(prop1);
1312 dval2 = cpl_property_get_double(prop2);
1313 if (dval1 != dval2) {
1315 reason = cpl_sprintf(
"value mismatch: name %s: values "
1317 name, dval1, dval2);
1321 case CPL_TYPE_STRING:
1322 sval1 = cpl_property_get_string(prop1);
1323 sval2 = cpl_property_get_string(prop2);
1324 if (strcmp(sval1, sval2)) {
1326 reason = cpl_sprintf(
"value mismatch: name %s: values "
1327 "%s, %s", name, sval1, sval2);
1331 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
1332 "name %s, type not handled %s",
1333 name, cpl_type_get_name(
1334 cpl_property_get_type(prop1)));
1337 if (result == CPL_FALSE)
break;
1344 cpl_propertylist_delete(subset1);
1345 cpl_propertylist_delete(subset2);
1350 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1353 if (result == CPL_FALSE) {
1354 cpl_msg_info(cpl_func,
"propertylist match failed: %s", reason);
1379 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
1380 cpl_ensure_code(limage, CPL_ERROR_NULL_INPUT);
1382 cpl_boolean bad = CPL_FALSE;
1384 if (!cpl_propertylist_has(limage->plist,
"CTYPE1")) {
1388 const char * ctype1 = cpl_propertylist_get_string(
1389 limage->plist,
"CTYPE1");
1390 if (strstr(ctype1,
"PIXEL")) {
1396 if (!cpl_propertylist_has(limage->plist,
"CTYPE2")) {
1401 const char * ctype2 = cpl_propertylist_get_string(limage->plist,
1403 if (strstr(ctype2,
"PIXEL")) {
1408 cpl_error_code result = cpl_error_get_code();
1409 if (result == CPL_ERROR_NONE && bad) {
1410 result = cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1411 "missing or bad WCS keywords in "
1413 cpl_frame_get_filename(limage->frame));
1436 const located_imagelist * limlist,
1437 const char * nameroot,
1438 const char * recipename,
1439 cpl_frameset * frameset,
1440 const cpl_parameterlist * parlist,
1441 const cpl_frameset * used) {
1443 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
1444 if (!debug)
return CPL_ERROR_NONE;
1445 cpl_ensure_code(limlist, CPL_ERROR_NULL_INPUT);
1446 cpl_ensure_code(nameroot, CPL_ERROR_NULL_INPUT);
1447 cpl_ensure_code(recipename, CPL_ERROR_NULL_INPUT);
1448 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
1449 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1450 cpl_ensure_code(used, CPL_ERROR_NULL_INPUT);
1452 char * fname_copy = NULL;
1453 char * out_fname = NULL;
1457 cpl_size nimages = limlist->size;
1458 for (cpl_size j = 0; j < nimages; j++) {
1462 if (limlist->limages[j]->frame) {
1463 const char * fname = cpl_frame_get_filename(
1464 limlist->limages[j]->frame);
1465 fname_copy = cpl_strdup(fname);
1467 const char * fname =
"debug.fits";
1468 fname_copy = cpl_strdup(fname);
1470 out_fname = cpl_sprintf(
"%s_%s", nameroot, basename(fname_copy));
1472 cpl_msg_info(cpl_func,
"..(debug) writing %s", out_fname);
1476 cpl_propertylist * applist = cpl_propertylist_new();
1477 cpl_propertylist_update_string(applist,
1480 cpl_propertylist_update_string(applist,
"PRODCATG",
"ANCILLARY.IMAGE");
1482 cpl_frameset * provenance = cpl_frameset_new();
1483 cpl_frameset_insert(provenance, cpl_frame_duplicate(
1484 limlist->limages[j]->frame));
1490 limlist->limages[j],
1492 limlist->limages[j]->frame,
1497 cpl_frameset_delete(provenance);
1498 cpl_propertylist_delete(applist);
1503 cpl_free(fname_copy);
1504 cpl_free(out_fname);
1506 return cpl_error_get_code();
1526 const hdrl_image * flat,
1527 const cpl_image * confidence,
1528 const cpl_mask * cold_bpm,
1529 cpl_frameset * frameset,
1530 const cpl_parameterlist * parlist,
1531 const char * filename,
1532 const char * recipe_name,
1533 const cpl_propertylist* qclog) {
1537 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
1538 cpl_ensure_code(pro_catg, CPL_ERROR_NULL_INPUT);
1539 cpl_ensure_code(flat, CPL_ERROR_NULL_INPUT);
1540 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
1541 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1542 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
1544 mef_extension_list * mefs = NULL;
1545 cpl_propertylist * plist = NULL;
1549 plist = cpl_propertylist_new();
1550 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, pro_catg);
1551 cpl_propertylist_update_string(plist,
"PRODCATG",
"ANCILLARY.IMAGE");
1556 cpl_propertylist_append_double(plist,
"ESO QC FLAT MED",
1557 (
double) flat_median.data);
1558 cpl_propertylist_set_comment(plist,
"ESO QC FLAT MED",
1559 "[ADU] Median flat value");
1561 cpl_propertylist_append_double(plist,
"ESO QC FLAT MEAN",
1562 (
double) flat_mean.data);
1563 cpl_propertylist_set_comment(plist,
"ESO QC FLAT MEAN",
1564 "[ADU] Mean flat value");
1566 cpl_propertylist_append_double(plist,
"ESO QC FLAT RMS", flat_rms);
1567 cpl_propertylist_set_comment(plist,
"ESO QC FLAT RMS",
1568 "[ADU] RMS flat value");
1572 cpl_msg_warning(cpl_func,
"TBD: calculate normalisation value");
1574 cpl_propertylist_append_double(plist,
"ESO QC FLAT NORM", norm);
1575 cpl_propertylist_set_comment(plist,
"ESO QC FLAT NORM",
1576 "Flat normalisation value");
1578 cpl_size ncold = cpl_mask_count(cold_bpm);
1579 cpl_propertylist_append_double(plist,
"ESO QC NUMBER COLD PIXELS",
1581 cpl_propertylist_set_comment(plist,
"ESO QC NUMBER COLD PIXELS",
1582 "Number of cold pixels");
1584 enu_check_error_code(
"error constructing output propertylist");
1597 cpl_propertylist_append(plist, qclog);
1610 PACKAGE
"/" PACKAGE_VERSION,
1615 cpl_propertylist_delete(plist);
1617 return cpl_error_get_code();
1638 if (cpl_error_get_code() != CPL_ERROR_NONE)
return 0;
1640 double airmass = 0.0;
1641 if (cpl_propertylist_has(plist,
"AIRMASS")) {
1643 airmass = cpl_propertylist_get_double(plist,
"AIRMASS");
1644 }
else if (cpl_propertylist_has(plist,
"ESO TEL AIRM START")) {
1646 const double astart = cpl_propertylist_get_double(plist,
1647 "ESO TEL AIRM START");
1648 const double aend = cpl_propertylist_get_double(plist,
1649 "ESO TEL AIRM END");
1650 airmass = (astart + aend) / 2.0;
1653 const double alt = cpl_propertylist_get_double(plist,
1655 double airmass_alt = 1.0 /cos ((90.0 - alt) * CPL_MATH_PI / 180.0);
1656 if (fabs(airmass - airmass_alt) > 1.0e-5) {
1657 cpl_msg_warning(cpl_func,
"discrepency - airmass %f -> %f", airmass,
1659 airmass = airmass_alt;
1662 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1663 "unable to find airmass information");
1692 const cpl_parameterlist * parlist,
1693 const cpl_propertylist * plist,
1694 double * obj_core_radius,
1695 int * bkg_mesh_size) {
1697 char * param_name = NULL;
1698 const cpl_parameter * p = NULL;
1699 cpl_parameter * par = NULL;
1701 cpl_error_code error_code = CPL_ERROR_NONE;
1703 if (cpl_error_get_code() != CPL_ERROR_NONE)
return CPL_ERROR_NONE;
1704 cpl_ensure_code(context, CPL_ERROR_NULL_INPUT);
1705 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1706 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
1707 cpl_ensure_code(obj_core_radius, CPL_ERROR_NULL_INPUT);
1708 cpl_ensure_code(bkg_mesh_size, CPL_ERROR_NULL_INPUT);
1710 *obj_core_radius = -1.0;
1711 *bkg_mesh_size = -1;
1716 param_name = cpl_sprintf(
"%s.catalogue.ao-params", context);
1717 cpl_msg_info(cpl_func,
"%s", param_name);
1718 p = cpl_parameterlist_find_const(parlist, param_name);
1719 const char * ao_params = cpl_parameter_get_string(p);
1720 cpl_free(param_name);
1721 enu_check_error_code(
"failed to read ao-params");
1723 if (!strcmp(ao_params,
"auto") &&
1724 cpl_propertylist_has(plist,
"ESO OBS AOMODE")) {
1725 const char * aomode = cpl_propertylist_get_string(plist,
1727 cpl_msg_info(cpl_func,
"AOMODE: %s", aomode);
1729 if (!strcmp(aomode,
"FULL_AO")) {
1730 *obj_core_radius = 10.0;
1731 *bkg_mesh_size = 64;
1732 if(!strstr(context,
"skysub")) {
1734 param_name = cpl_sprintf(
"%s.catalogue.obj.core-radius", context);
1735 par = (cpl_parameter*) cpl_parameterlist_find_const(parlist, param_name);
1736 cpl_parameter_set_double(par, *obj_core_radius);
1737 cpl_free(param_name);
1741 param_name = cpl_sprintf(
"%s.catalogue.bkg.mesh-size", context);
1742 par = (cpl_parameter*) cpl_parameterlist_find_const(parlist, param_name);
1743 cpl_parameter_set_int(par, *bkg_mesh_size);
1744 cpl_free(param_name);
1746 }
else if (!strcmp(aomode,
"NO_AO")) {
1747 *obj_core_radius = 25.0;
1748 *bkg_mesh_size = 128;
1749 if(!strstr(context,
"skysub")) {
1751 param_name = cpl_sprintf(
"%s.catalogue.obj.core-radius", context);
1752 par = (cpl_parameter*) cpl_parameterlist_find_const(parlist, param_name);
1753 cpl_parameter_set_double(par, *obj_core_radius);
1754 cpl_free(param_name);
1757 param_name = cpl_sprintf(
"%s.catalogue.bkg.mesh-size", context);
1758 par = (cpl_parameter*) cpl_parameterlist_find_const(parlist, param_name);
1759 cpl_parameter_set_int(par, *bkg_mesh_size);
1760 cpl_free(param_name);
1762 error_code = CPL_ERROR_DATA_NOT_FOUND;
1766 if(!strstr(context,
"skysub")) {
1767 param_name = cpl_sprintf(
"%s.catalogue.obj.core-radius", context);
1768 p = cpl_parameterlist_find_const(parlist, param_name);
1769 *obj_core_radius = cpl_parameter_get_double(p);
1770 cpl_free(param_name);
1773 param_name = cpl_sprintf(
"%s.catalogue.bkg.mesh-size", context);
1774 p = cpl_parameterlist_find_const(parlist, param_name);
1775 *bkg_mesh_size = cpl_parameter_get_int(p);
1776 cpl_free(param_name);
1778 cpl_msg_info(cpl_func,
"catalogue AO-related params: %s %f %d",
1779 ao_params, *obj_core_radius, *bkg_mesh_size);
1801 if (cpl_error_get_code() != CPL_ERROR_NONE)
return 0;
1803 double tel_alt = 0.0;
1804 if (cpl_propertylist_has(plist,
"ESO TEL ALT")) {
1806 tel_alt = cpl_propertylist_get_double(plist,
"ESO TEL ALT");
1808 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1809 "unable to find ESO TEL ALT information");
1831 if (cpl_error_get_code() != CPL_ERROR_NONE)
return 0;
1834 if (cpl_propertylist_has(plist,
"ESO DET DIT")) {
1835 dit = cpl_propertylist_get_double(plist,
"ESO DET DIT");
1836 }
else if (cpl_propertylist_has(plist,
"ESO DET SEQ1 DIT")) {
1837 dit = cpl_propertylist_get_double(plist,
"ESO DET SEQ1 DIT");
1839 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1840 "no keyword ESO.DET.DIT or ESO.DET.SEQ1.DIT");
1862 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
1864 const char * result = NULL;
1865 if (cpl_propertylist_has(plist,
"ESO DET NCORRS NAME")) {
1866 result = cpl_propertylist_get_string(plist,
"ESO DET NCORRS NAME");
1867 }
else if (cpl_propertylist_has(plist,
"ESO DET READ CURNAME")) {
1868 result = cpl_propertylist_get_string(plist,
"ESO DET READ CURNAME");
1870 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1871 "no keyword ESO.DET.NCORRS.NAME or "
1872 "ESO.DET.READ.CURNAME");
1898 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
1899 cpl_ensure(plist, CPL_ERROR_NULL_INPUT, NULL);
1901 const char * filter = NULL;
1903 if (cpl_propertylist_has(plist,
"FILTER")) {
1904 filter = cpl_propertylist_get_string(plist,
"FILTER");
1905 }
else if (cpl_propertylist_has(plist,
"INSTRUME")) {
1906 const char * instrume = cpl_propertylist_get_string(plist,
"INSTRUME");
1908 if (!strcmp(instrume,
"NAOS+CONICA")) {
1909 filter = cpl_propertylist_get_string(plist,
"ESO INS OPTI4 NAME");
1910 cpl_msg_info(cpl_func,
"OPTI4 %s", filter);
1911 if (!strcmp(filter,
"clear") || !strcmp(filter,
"empty")) {
1912 filter = cpl_propertylist_get_string(plist,
"ESO INS OPTI5 NAME");
1913 cpl_msg_info(cpl_func,
"OPTI5 %s", filter);
1915 if (!strcmp(filter,
"clear") || !strcmp(filter,
"empty")) {
1916 filter = cpl_propertylist_get_string(plist,
"ESO INS OPTI6 NAME");
1917 cpl_msg_info(cpl_func,
"OPTI6 %s", filter);
1919 }
else if (!strcmp(instrume,
"ERIS")) {
1920 filter = cpl_propertylist_get_string(plist,
"ESO INS2 NXFW NAME");
1922 cpl_error_set(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT);
1949 if (cpl_error_get_code() != CPL_ERROR_NONE)
return -1.0;
1950 cpl_ensure(filter, CPL_ERROR_NULL_INPUT, -1.0);
1952 double lambda = 1.0;
1953 if (!strcmp(filter,
"J")) {
1955 }
else if (!strcmp(filter,
"H")) {
1957 }
else if (!strcmp(filter,
"Ks")) {
1959 }
else if (!strcmp(filter,
"Short-Lp")) {
1961 }
else if (!strcmp(filter,
"L-Broad")) {
1963 }
else if (!strcmp(filter,
"Lp")) {
1965 }
else if (!strcmp(filter,
"Mp")) {
1967 }
else if (!strcmp(filter,
"Pa-b")) {
1969 }
else if (!strcmp(filter,
"Fe-II")) {
1971 }
else if (!strcmp(filter,
"H2-cont")) {
1973 }
else if (!strcmp(filter,
"H2-1-0S")) {
1975 }
else if (!strcmp(filter,
"Br-g")) {
1977 }
else if (!strcmp(filter,
"K-peak")) {
1979 }
else if (!strcmp(filter,
"IB-2.42")) {
1981 }
else if (!strcmp(filter,
"IB-2.48")) {
1983 }
else if (!strcmp(filter,
"Br-a-cont")) {
1985 }
else if (!strcmp(filter,
"Br-a")) {
1988 cpl_msg_warning(cpl_func,
"filter %s not recognized", filter);
2006 const char * eris_nix_license =
2007 "This file is part of the ERIS/NIX Instrument Pipeline\n"
2008 "Copyright (C) 2017 European Southern Observatory\n"
2010 "This program is free software; you can redistribute it and/or modify\n"
2011 "it under the terms of the GNU General Public License as published by\n"
2012 "the Free Software Foundation; either version 2 of the License, or\n"
2013 "(at your option) any later version.\n"
2015 "This program is distributed in the hope that it will be useful,\n"
2016 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2017 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
2018 "GNU General Public License for more details.\n"
2020 "You should have received a copy of the GNU General Public License\n"
2021 "along with this program; if not, write to the Free Software\n"
2022 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, \n"
2023 "MA 02110-1301 USA";
2024 return eris_nix_license;
2039 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
2040 cpl_ensure(wcs, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2041 cpl_ensure(ra, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2042 cpl_ensure(dec, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2046 const cpl_array * dims = cpl_wcs_get_image_dims(wcs);
2047 cpl_matrix * from = cpl_matrix_new(1, 2);
2049 double dim = cpl_array_get(dims, 0, &ignore);
2050 cpl_matrix_set(from, 0, 0, dim / 2.0);
2051 dim = cpl_array_get(dims, 1, &ignore);
2052 cpl_matrix_set(from, 0, 1, dim / 2.0);
2056 cpl_matrix * to = NULL;
2057 cpl_array * status = NULL;
2058 cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_PHYS2WORLD);
2060 *ra = cpl_matrix_get(to, 0, 0);
2061 *dec = cpl_matrix_get(to, 0, 1);
2065 cpl_matrix_delete(from);
2066 cpl_matrix_delete(to);
2067 cpl_array_delete(status);
2069 return cpl_error_get_code();
2093 cpl_size * strx, cpl_size * stry,
2094 cpl_size * nx_chip, cpl_size * ny_chip,
2095 cpl_boolean * windowed,
2096 const cpl_propertylist * plist) {
2098 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
2100 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
2101 enu_check(cpl_propertylist_has(plist,
"INSTRUME") == 1,
2102 CPL_ERROR_DATA_NOT_FOUND,
2103 "propertylist does not contain INSTRUME keyword");
2107 const char * instrume = cpl_propertylist_get_string(plist,
"INSTRUME");
2115 if (!strcmp(instrume,
"ERIS")) {
2116 nxw = cpl_propertylist_get_int(plist,
"ESO DET SEQ1 WIN NX");
2117 nyw = cpl_propertylist_get_int(plist,
"ESO DET SEQ1 WIN NY");
2118 rotw = cpl_propertylist_get_int(plist,
"ESO DET SEQ1 WIN ROT");
2119 strxw = cpl_propertylist_get_int(plist,
"ESO DET SEQ1 WIN STRX");
2120 stryw = cpl_propertylist_get_int(plist,
"ESO DET SEQ1 WIN STRY");
2124 nxw = cpl_propertylist_get_int(plist,
"ESO DET WIN NX");
2125 nyw = cpl_propertylist_get_int(plist,
"ESO DET WIN NY");
2126 if (cpl_propertylist_get_type(plist,
"ESO DET WIN STARTX") ==
2128 strxw = (int) cpl_propertylist_get_double(plist,
2129 "ESO DET WIN STARTX");
2130 stryw = (int) cpl_propertylist_get_double(plist,
2131 "ESO DET WIN STARTY");
2133 strxw = cpl_propertylist_get_int(plist,
"ESO DET WIN STARTX");
2134 stryw = cpl_propertylist_get_int(plist,
"ESO DET WIN STARTY");
2138 cpl_size nx_chipw = 2048;
2139 cpl_size ny_chipw = 2048;
2141 if(cpl_propertylist_has(plist,
"ESO DET CHIP NX")) {
2142 nx_chipw = cpl_propertylist_get_int(plist,
"ESO DET CHIP NX");
2144 nx_chipw = cpl_propertylist_get_int(plist,
"ESO DET CHIP1 NX");
2147 if(cpl_propertylist_has(plist,
"ESO DET CHIP NY")) {
2148 ny_chipw = cpl_propertylist_get_int(plist,
"ESO DET CHIP NY");
2150 ny_chipw = cpl_propertylist_get_int(plist,
"ESO DET CHIP1 NY");
2153 enu_check_error_code(
"failed to read detector mode information");
2154 enu_check(rotw == 0, CPL_ERROR_UNSUPPORTED_MODE,
2155 "detector window rot must be 0");
2157 cpl_boolean windowedw = (rotw != 0) ||
2160 (nxw != nx_chipw) ||
2162 cpl_msg_info(cpl_func,
"flatfield rot=%d strx=%d stry=%d nx=%d ny=%d",
2163 (
int)rotw, (
int)strxw, (
int)stryw, (
int)nxw, (
int)nyw);
2173 *nx_chip = nx_chipw;
2174 *ny_chip = ny_chipw;
2175 *windowed = windowedw;
2179 return cpl_error_get_code();
2191 const hdrl_catalogue_result * target) {
2192 cpl_ensure(target, CPL_ERROR_NULL_INPUT, NULL);
2194 hdrl_catalogue_result * result = cpl_malloc(
sizeof(hdrl_catalogue_result));
2196 result->catalogue = cpl_table_duplicate(target->catalogue);
2197 result->background = cpl_image_duplicate(target->background);
2198 result->segmentation_map = cpl_image_duplicate(target->segmentation_map);
2199 result->qclist = cpl_propertylist_duplicate(target->qclist);
2219 hdrl_image ** result,
2220 mef_extension_list ** mef_extensions,
2221 cpl_propertylist ** plist) {
2223 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
2225 cpl_propertylist * plist1 = NULL;
2226 cpl_propertylist * plist2 = NULL;
2227 cpl_propertylist * plist3 = NULL;
2228 cpl_image * data = NULL;
2229 cpl_image * error = NULL;
2230 cpl_mask * bpm = NULL;
2231 cpl_mask * old_bpm = NULL;
2235 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
2239 cpl_size size = cpl_fits_count_extensions(filename);
2240 cpl_ensure_code(size >= 3, CPL_ERROR_BAD_FILE_FORMAT);
2244 *plist = cpl_propertylist_load(filename, 0);
2245 enu_check_error_code(
"accessing HDU 0 header");
2249 plist1 = cpl_propertylist_load(filename, 1);
2250 enu_check_error_code(
"accessing HDU 1 header");
2252 const char * extname = cpl_propertylist_get_string(plist1,
"EXTNAME");
2253 const char * hduclass = cpl_propertylist_get_string(plist1,
"HDUCLASS");
2254 const char * hdudoc = cpl_propertylist_get_string(plist1,
"HDUDOC");
2256 const char * hduclas1 = cpl_propertylist_get_string(plist1,
"HDUCLAS1");
2257 const char * hduclas2 = cpl_propertylist_get_string(plist1,
"HDUCLAS2");
2258 enu_check_error_code(
"accessing HDU 1 propertylist");
2259 enu_check(strstr(extname,
"DATA") != NULL &&
2260 strstr(hduclass,
"ESO") != NULL &&
2261 strstr(hdudoc,
"SDP") != NULL &&
2262 strstr(hduclas1,
"IMAGE") != NULL &&
2263 strstr(hduclas2,
"DATA") != NULL,
2264 CPL_ERROR_BAD_FILE_FORMAT,
"bad HD1 format");
2265 data = cpl_image_load(filename, HDRL_TYPE_DATA, 0, 1);
2266 enu_check_error_code(
"accessing HDU 1 data");
2273 const char * wcs_items =
"NAXIS WCSAXES CRPIX1 CRPIX2 "
2274 "CD1_1 CD1_2 CD2_1 CD2_2 CUNIT1 CUNIT2 "
2275 "CTYPE1 CTYPE2 CRVAL1 CRVAL2 LONPOLE "
2276 "LATPOLE CSYER1 CSYER2 EQUINOX MJD-OBS "
2277 "DATE-OBS NAXIS2 NAXIS1";
2278 cpl_size plist1_size = cpl_propertylist_get_size(plist1);
2279 for (cpl_size ip=plist1_size; ip>0; ip--) {
2280 const cpl_property * newprop = cpl_propertylist_get_const(plist1, ip-1);
2281 const char * name = cpl_property_get_name(newprop);
2282 if (strstr(wcs_items, name) != NULL) {
2283 if (cpl_propertylist_has(*plist, name)) {
2284 cpl_propertylist_copy_property(*plist, plist1, name);
2286 cpl_propertylist_insert_after_property(*plist,
"NAXIS",
2294 plist2 = cpl_propertylist_load(filename, 2);
2295 enu_check_error_code(
"accessing HDU 2");
2296 extname = cpl_propertylist_get_string(plist2,
"EXTNAME");
2297 hduclass = cpl_propertylist_get_string(plist2,
"HDUCLASS");
2298 hdudoc = cpl_propertylist_get_string(plist2,
"HDUDOC");
2300 hduclas1 = cpl_propertylist_get_string(plist2,
"HDUCLAS1");
2301 hduclas2 = cpl_propertylist_get_string(plist2,
"HDUCLAS2");
2302 enu_check_error_code(
"accessing HDU 2 propertylist");
2303 enu_check(strstr(extname,
"ERR") != NULL &&
2304 strstr(hduclass,
"ESO") != NULL &&
2305 strstr(hdudoc,
"SDP") != NULL &&
2306 strstr(hduclas1,
"IMAGE") != NULL &&
2307 strstr(hduclas2,
"ERROR") != NULL,
2308 CPL_ERROR_BAD_FILE_FORMAT,
"bad HD2 format");
2309 error = cpl_image_load(filename, HDRL_TYPE_ERROR, 0, 2);
2310 enu_check_error_code(
"accessing HDU 1 data error");
2314 plist3 = cpl_propertylist_load(filename, 3);
2315 enu_check_error_code(
"accessing HDU 3");
2316 extname = cpl_propertylist_get_string(plist3,
"EXTNAME");
2317 hduclass = cpl_propertylist_get_string(plist3,
"HDUCLASS");
2318 hdudoc = cpl_propertylist_get_string(plist3,
"HDUDOC");
2320 hduclas1 = cpl_propertylist_get_string(plist3,
"HDUCLAS1");
2321 hduclas2 = cpl_propertylist_get_string(plist3,
"HDUCLAS2");
2322 enu_check_error_code(
"accessing HDU 3 propertylist");
2323 enu_check(strstr(extname,
"DQ") != NULL &&
2324 strstr(hduclass,
"ESO") != NULL &&
2325 strstr(hdudoc,
"SDP") != NULL &&
2326 strstr(hduclas1,
"IMAGE") != NULL &&
2327 strstr(hduclas2,
"QUALITY") != NULL,
2328 CPL_ERROR_BAD_FILE_FORMAT,
"bad HD3 format");
2332 bpm = cpl_mask_load(filename, 0, 3);
2333 enu_check_error_code(
"accessing HDU 3 data mask");
2334 old_bpm = cpl_image_set_bpm(data, bpm);
2335 cpl_mask_delete(old_bpm); old_bpm = NULL;
2340 int n_ext = size - 3;
2342 for (
int ext=0; ext<n_ext; ext++) {
2346 cpl_propertylist * ext_plist = cpl_propertylist_load(filename, ext+4);
2347 const char * name = cpl_propertylist_get_string(ext_plist,
"EXTNAME");
2348 const char * mef_type = cpl_propertylist_get_string(ext_plist,
2349 "ERIS_NIX_MEF_TYPE");
2353 if (!strcmp(mef_type, MEF_EXTENSION_CONTAINING_MASK)) {
2354 cpl_mask * mask = cpl_mask_load(filename, 0, ext+4);
2356 (*mef_extensions)->mef[ext] =
new;
2357 cpl_mask_delete(mask);
2358 }
else if (!strcmp(mef_type, MEF_EXTENSION_CONTAINING_IMAGE)) {
2359 cpl_image * image = cpl_image_load(filename, HDRL_TYPE_DATA, 0,
2362 (*mef_extensions)->mef[ext] =
new;
2363 cpl_image_delete(image);
2364 }
else if (!strcmp(mef_type, MEF_EXTENSION_CONTAINING_TABLE)) {
2365 cpl_table * table = cpl_table_load(filename, ext+4, 0);
2367 (*mef_extensions)->mef[ext] =
new;
2368 cpl_table_delete(table);
2370 cpl_error_set_message(
"enu_himage_load_from_fits",
2371 CPL_ERROR_UNSUPPORTED_MODE,
2372 "unsupported extension name: %s", name);
2375 cpl_propertylist_delete(ext_plist);
2379 cpl_propertylist_delete(plist1);
2380 cpl_propertylist_delete(plist2);
2381 cpl_propertylist_delete(plist3);
2382 cpl_image_delete(data);
2383 cpl_image_delete(error);
2384 cpl_mask_delete(old_bpm);
2385 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2386 cpl_propertylist_delete(*plist);
2391 *mef_extensions = NULL;
2394 return cpl_error_get_code();
2416 cpl_image ** pcopyconf,
2417 const cpl_boolean collapse_cube) {
2419 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
2421 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, NULL);
2422 cpl_ensure(cpl_frame_get_filename(frame), CPL_ERROR_NULL_INPUT, NULL);
2424 located_image * result = NULL;
2425 mef_extension_list * mefs = NULL;
2426 cpl_image * data = NULL;
2427 cpl_image * err = NULL;
2428 cpl_image * qual = NULL;
2429 cpl_image * conf = NULL;
2430 cpl_image * bkg = NULL;
2431 cpl_image * bkg_err = NULL;
2432 cpl_image * bkg_conf = NULL;
2434 const char * filename = cpl_frame_get_filename(frame);
2435 cpl_msg_info(cpl_func,
"..reading %s", filename);
2441 cpl_size next = cpl_fits_count_extensions(filename);
2443 cpl_propertylist * plist = cpl_propertylist_load(filename, 0);
2446 const char * instrume = cpl_propertylist_get_string(plist,
"INSTRUME");
2447 const cpl_boolean eris = !strcmp(instrume,
"ERIS");
2449 char * first_ext = NULL;
2450 if (eris && (next > 0)) {
2451 cpl_propertylist * plist1 = cpl_propertylist_load(filename, 1);
2452 if (cpl_propertylist_has(plist1,
"EXTNAME")) {
2453 first_ext = cpl_sprintf(
"%s", cpl_propertylist_get_string(plist1,
"EXTNAME"));
2455 cpl_propertylist_delete(plist1);
2459 char * frame_format = NULL;
2460 if (cpl_propertylist_has(plist,
"ESO DET FRAM FORMAT")) {
2461 frame_format = cpl_sprintf(
"%s", cpl_propertylist_get_string(plist,
"ESO DET FRAM FORMAT"));
2462 cpl_msg_info(cpl_func,
"..format %s", frame_format);
2468 cpl_msg_info(cpl_func,
"NACO data");
2473 data = cpl_image_load(filename, HDRL_TYPE_DATA, 0, 0);
2474 enu_check_error_code(
"failed to read file: %s", filename);
2476 cpl_image_delete(data); data = NULL;
2482 cpl_image * confidence;
2483 if (pcopyconf != NULL && *pcopyconf != NULL &&
2484 cpl_image_get_size_x(*pcopyconf) == nx &&
2485 cpl_image_get_size_y(*pcopyconf) == ny) {
2486 confidence = *pcopyconf;
2489 confidence = cpl_image_new(nx, ny, HDRL_TYPE_DATA);
2490 cpl_image_fill_window(confidence, 1, 1, nx, ny, 100.0);
2493 cpl_frame_dump(frame, NULL);
2504 cpl_frame_duplicate(frame));
2507 }
else if ((next == 0) ||
2510 !strcmp(first_ext,
"ASM_DATA"))) {
2517 cpl_table * asm_table = cpl_table_load(filename, 1, CPL_TRUE);
2518 cpl_size nrow = cpl_table_get_nrow(asm_table);
2519 if(cpl_table_has_column(asm_table,
"IA_FWHM")) {
2520 double * ia_fwhm_data = cpl_table_get_data_double(asm_table,
2522 cpl_vector * ia_fwhm = cpl_vector_wrap(nrow, ia_fwhm_data);
2523 double ia_fwhm_mean = cpl_vector_get_mean(ia_fwhm);
2531 double ia_fwhm_corr = ia_fwhm_mean * pow(0.5/lambda, 1./5.);
2533 cpl_propertylist_update_double(plist,
"PSF_FWHM", ia_fwhm_corr);
2534 cpl_propertylist_set_comment(plist,
"PSF_FWHM",
"PSF fwhm derived "
2535 "from AO ASM table [arcsec]");
2537 cpl_vector_unwrap(ia_fwhm);
2539 cpl_msg_warning(cpl_func,
"ASM table miss IA_FWHM column. PSF_FWHM not computed");
2541 cpl_table_delete(asm_table);
2548 if (cpl_propertylist_has(plist,
"ESO TEL AMBI FWHM START") &&
2549 cpl_propertylist_has(plist,
"ESO TEL AMBI FWHM END") &&
2550 cpl_propertylist_has(plist,
"ESO TEL AIRM START") &&
2551 cpl_propertylist_has(plist,
"ESO TEL AIRM END")) {
2553 double dimm1 = cpl_propertylist_get_double(plist,
2554 "ESO TEL AMBI FWHM START");
2555 double dimm2 = cpl_propertylist_get_double(plist,
2556 "ESO TEL AMBI FWHM END");
2557 double airmass1 = cpl_propertylist_get_double(plist,
2558 "ESO TEL AIRM START");
2559 double airmass2 = cpl_propertylist_get_double(plist,
2560 "ESO TEL AIRM END");
2568 pow(0.5/lambda, 1./5.) *
2569 pow(airmass1, 3./5.) *
2571 (pow(lambda/1.e6, 2./5.) *
2572 pow(airmass1, -1./5.) /
2577 pow(0.5/lambda, 1./5.) *
2578 pow(airmass2, 3./5.) *
2580 (pow(lambda/1.e6, 2./5.) *
2581 pow(airmass2, -1./5.) /
2586 cpl_propertylist_update_double(plist,
"PSF_FWHM",
2587 (dimm1 + dimm2) / 2.0);
2588 cpl_propertylist_set_comment(plist,
"PSF_FWHM",
2590 "from DIMM [arcsec]");
2594 if (!strcmp(frame_format,
"single")) {
2598 data = cpl_image_load(filename, HDRL_TYPE_DATA, 0, 0);
2599 enu_check_error_code(
"failed to read file: %s", filename);
2601 cpl_image_delete(data); data = NULL;
2607 cpl_image * confidence;
2608 if (pcopyconf != NULL && *pcopyconf != NULL &&
2609 cpl_image_get_size_x(*pcopyconf) == nx &&
2610 cpl_image_get_size_y(*pcopyconf) == ny) {
2611 confidence = *pcopyconf;
2614 confidence = cpl_image_new(nx, ny, HDRL_TYPE_DATA);
2615 cpl_image_fill_window(confidence, 1, 1, nx, ny, 100.0);
2628 cpl_frame_duplicate(frame));
2631 }
else if (!strcmp(frame_format,
"cube")) {
2635 cpl_imagelist * datalist = cpl_imagelist_load(filename,
2637 enu_check_error_code(
"failed to read file: %s", filename);
2640 cpl_imagelist_delete(datalist); datalist = NULL;
2646 cpl_image * confidence;
2647 if (pcopyconf != NULL && *pcopyconf != NULL &&
2648 cpl_image_get_size_x(*pcopyconf) == nx &&
2649 cpl_image_get_size_y(*pcopyconf) == ny) {
2650 confidence = *pcopyconf;
2653 confidence = cpl_image_new(nx, ny, HDRL_TYPE_DATA);
2654 cpl_image_fill_window(confidence, 1, 1, nx, ny, 100.0);
2658 hdrl_image * himage = NULL;
2659 cpl_image* conf_ima = NULL;
2660 if(collapse_cube == 1) {
2662 }
else if(collapse_cube == 2) {
2664 }
else if(collapse_cube == 3) {
2667 cpl_image_delete(conf_ima);
2678 cpl_frame_duplicate(frame));
2690 cpl_frame_duplicate(frame));
2698 cpl_propertylist_delete(plist);
2701 enu_check_error_code(
"Unable to load components for file");
2705 const char * hduclass = cpl_propertylist_get_string(
2706 mefs->mef[0]->plist,
"HDUCLASS");
2707 const char * hdudoc = cpl_propertylist_get_string(
2708 mefs->mef[0]->plist,
"HDUDOC");
2709 const char * hduvers = cpl_propertylist_get_string(
2710 mefs->mef[0]->plist,
"HDUVERS");
2711 enu_check_error_code(
"Unable to read ESO DPS keywords from header");
2713 enu_check(strstr(hduclass,
"ESO") != NULL &&
2714 strstr(hdudoc,
"SDP") != NULL &&
2715 strstr(hduvers,
"SDP version") != NULL,
2716 CPL_ERROR_BAD_FILE_FORMAT,
"file not in ESO DPS format");
2721 const char * scidata = NULL;
2722 if (mefs->size > 0 &&
2723 cpl_propertylist_has(mefs->mef[1]->plist,
"SCIDATA")){
2724 scidata = cpl_propertylist_get_string(mefs->mef[1]->plist,
2727 const char * errdata = NULL;
2728 if (cpl_propertylist_has(mefs->mef[0]->plist,
"ERRDATA")){
2729 errdata = cpl_propertylist_get_string(mefs->mef[0]->plist,
2732 const char * qualdata = NULL;
2733 if (cpl_propertylist_has(mefs->mef[0]->plist,
"QUALDATA")){
2734 qualdata = cpl_propertylist_get_string(mefs->mef[0]->plist,
2737 const char * confdata = NULL;
2738 if (cpl_propertylist_has(mefs->mef[0]->plist,
"CONFDATA")){
2739 confdata = cpl_propertylist_get_string(mefs->mef[0]->plist,
2742 const char * bkgdata = NULL;
2743 if (cpl_propertylist_has(mefs->mef[0]->plist,
"BKGDATA")){
2744 bkgdata = cpl_propertylist_get_string(mefs->mef[0]->plist,
2747 const char * bkgerr = NULL;
2748 if (cpl_propertylist_has(mefs->mef[0]->plist,
"BKGERR")){
2749 bkgerr = cpl_propertylist_get_string(mefs->mef[0]->plist,
2752 const char * bkgconf = NULL;
2753 if (cpl_propertylist_has(mefs->mef[0]->plist,
"BKGCONF")){
2754 bkgconf = cpl_propertylist_get_string(mefs->mef[0]->plist,
2757 enu_check_error_code(
"accessing DPS standard extensions");
2761 for (cpl_size i=0; i<next; i++) {
2762 const char * extname = cpl_propertylist_get_string(
2763 mefs->mef[i]->plist,
"EXTNAME");
2768 if (scidata && !strcmp(scidata, extname)) {
2771 enu_check_error_code(
"accessing SCIDATA extension");
2776 cpl_propertylist * primary_plist = plist;
2783 plist = cpl_propertylist_duplicate(mefs->mef[i]->plist);
2784 const char * sdp7_items =
" EXTNAME HDUCLAS1 HDUCLAS2"
2786 cpl_size psize = cpl_propertylist_get_size(plist);
2787 for (cpl_size ip = psize; ip > 0; ip--) {
2788 const cpl_property * prop = cpl_propertylist_get_const(
2790 const char * name = cpl_property_get_name(prop);
2796 char * padded_name = cpl_sprintf(
" %s ", name);
2797 if (strstr(sdp7_items, padded_name) != NULL) {
2798 cpl_propertylist_erase(plist, name);
2800 cpl_free(padded_name);
2805 psize = cpl_propertylist_get_size(primary_plist);
2806 for (cpl_size ip = psize; ip > 0; ip--) {
2807 const cpl_property * prop = cpl_propertylist_get_const(
2808 primary_plist, ip-1);
2809 const char * name = cpl_property_get_name(prop);
2810 if (!cpl_propertylist_has(plist, name)) {
2811 cpl_propertylist_copy_property(plist, primary_plist,
2815 cpl_propertylist_delete(primary_plist);
2817 }
else if (errdata && !strcmp(errdata, extname)) {
2820 enu_check_error_code(
"accessing SCIERR extension");
2821 }
else if (qualdata && !strcmp(qualdata, extname)) {
2823 "IMAGE",
"QUALITY");
2824 enu_check_error_code(
"accessing QUALDATA extension");
2825 }
else if (confdata && !strcmp(confdata, extname)) {
2828 enu_check_error_code(
"accessing CONFDATA extension");
2829 }
else if (bkgdata && !strcmp(bkgdata, extname)) {
2831 "IMAGE",
"BKG_DATA");
2832 enu_check_error_code(
"accessing BKGDATA extension");
2833 }
else if (bkgerr && !strcmp(bkgerr, extname)) {
2835 "IMAGE",
"BKG_ERR");
2836 enu_check_error_code(
"accessing BKGERR extension");
2837 }
else if (bkgconf && !strcmp(bkgconf, extname)) {
2839 "IMAGE",
"BKG_CONF");
2840 enu_check_error_code(
"accessing BKGCONF extension");
2846 enu_check(data != NULL, CPL_ERROR_BAD_FILE_FORMAT,
"no data in file");
2847 cpl_mask * bpm = cpl_image_get_bpm(data);
2848 cpl_mask_threshold_image(bpm, qual, 0.9, 1.1, CPL_BINARY_1);
2849 bpm = cpl_image_get_bpm(err);
2850 cpl_mask_threshold_image(bpm, qual, 0.9, 1.1, CPL_BINARY_1);
2852 hdrl_image * hbkg = NULL;
2867 cpl_frame_duplicate(frame));
2870 cpl_image_delete(data);
2871 cpl_image_delete(err);
2872 cpl_image_delete(qual);
2873 cpl_image_delete(bkg);
2874 cpl_image_delete(bkg_err);
2885 cpl_free(frame_format);
2886 cpl_free(first_ext);
2887 cpl_propertylist_delete(plist);
2888 cpl_image_delete(data);
2889 cpl_image_delete(err);
2890 cpl_image_delete(qual);
2891 cpl_image_delete(conf);
2892 cpl_image_delete(bkg);
2893 cpl_image_delete(bkg_err);
2894 cpl_image_delete(bkg_conf);
2896 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2919 cpl_propertylist ** plist) {
2921 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
2923 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
2928 cpl_size next = cpl_fits_count_extensions(filename);
2930 mef_extension *
new = NULL;
2931 cpl_propertylist * eplist = NULL;
2935 *plist = cpl_propertylist_load(filename, 0);
2936 enu_check_error_code(
"accessing HDU 0 header");
2940 for (
int i=0; i<next; i++) {
2945 cpl_propertylist_delete(eplist);
2946 eplist = cpl_propertylist_load(filename, hdu);
2947 const char * name = cpl_propertylist_get_string(eplist,
"EXTNAME");
2948 const char * hduclas1 = cpl_propertylist_get_string(eplist,
2950 enu_check_error_code(
"error accessing keywords in HDU %d", hdu);
2954 if (!strcmp(hduclas1, MEF_EXTENSION_CONTAINING_MASK)) {
2955 cpl_mask * mask = cpl_mask_load(filename, 0, hdu);
2957 cpl_mask_delete(mask);
2958 }
else if (!strcmp(hduclas1,
"IMAGE")) {
2959 cpl_image * image = cpl_image_load(filename, HDRL_TYPE_DATA, 0, hdu);
2961 cpl_image_delete(image);
2962 }
else if (!strcmp(hduclas1, MEF_EXTENSION_CONTAINING_TABLE)) {
2963 cpl_table * table = cpl_table_load(filename, hdu, 0);
2965 cpl_table_delete(table);
2967 cpl_error_set_message(
"enu_load_mef_components",
2968 CPL_ERROR_UNSUPPORTED_MODE,
2969 "extension has unsupported HDUCLAS1: %s %s", name, hduclas1);
2971 enu_check_error_code(
"error accessing HDU %d", hdu);
2973 result->mef[i] =
new;
2979 cpl_propertylist_delete(eplist);
2980 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2983 cpl_propertylist_delete(*plist);
3005 cpl_image_delete(limage->confidence);
3007 cpl_image_delete(limage->bkg_confidence);
3008 cpl_propertylist_delete(limage->plist);
3010 cpl_mask_delete(limage->object_mask);
3013 cpl_frame_delete(limage->frame);
3031 cpl_ensure(limage, CPL_ERROR_NULL_INPUT, NULL);
3035 hdrl_image * himage = NULL;
3036 if (limage->himage) {
3039 hdrl_imagelist * himagelist = NULL;
3040 if (limage->himagelist) {
3043 cpl_image * confidence = NULL;
3044 if (limage->confidence) {
3045 confidence = cpl_image_duplicate(limage->confidence);
3047 hdrl_image * bkg = NULL;
3051 cpl_image * bkg_confidence = NULL;
3052 if (limage->bkg_confidence) {
3053 bkg_confidence = cpl_image_duplicate(limage->bkg_confidence);
3055 cpl_propertylist * plist = NULL;
3056 if (limage->plist) {
3057 plist = cpl_propertylist_duplicate(limage->plist);
3059 hdrl_catalogue_result * objects = NULL;
3060 if (limage->objects) {
3063 cpl_mask * object_mask = NULL;
3064 if (limage->object_mask) {
3065 object_mask = cpl_mask_duplicate(limage->object_mask);
3067 hdrl_catalogue_result * matchstd_wcs = NULL;
3068 if (limage->matchstd_wcs) {
3070 limage->matchstd_wcs);
3072 hdrl_catalogue_result * matchstd_phot = NULL;
3073 if (limage->matchstd_phot) {
3075 limage->matchstd_phot);
3077 cpl_frame * frame = NULL;
3078 if (limage->frame) {
3079 frame = cpl_frame_duplicate(limage->frame);
3122 hdrl_imagelist * himagelist,
3123 cpl_image * confidence,
3125 cpl_image * bkg_confidence,
3126 cpl_propertylist * plist,
3127 hdrl_catalogue_result * objects,
3128 cpl_mask * object_mask,
3129 hdrl_catalogue_result * wcs,
3130 hdrl_catalogue_result * photom,
3131 cpl_frame * frame) {
3133 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3136 cpl_ensure(!(himage && himagelist), CPL_ERROR_ILLEGAL_INPUT, NULL);
3138 located_image * result = cpl_malloc(
sizeof(located_image));
3139 result->himage = himage;
3140 result->himagelist = himagelist;
3141 result->confidence = confidence;
3143 result->bkg_confidence = bkg_confidence;
3144 result->plist = plist;
3145 result->objects = objects;
3146 result->object_mask = object_mask;
3147 result->matchstd_wcs = wcs;
3148 result->matchstd_phot = photom;
3149 result->frame = frame;
3169 cpl_frameset * frameset,
3171 cpl_frameset * used) {
3173 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3174 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
3175 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
3176 cpl_ensure(used, CPL_ERROR_NULL_INPUT, NULL);
3178 located_imagelist * result = NULL;
3179 cpl_frameset_iterator * frameset_iter = NULL;
3180 mef_extension_list * mefs = NULL;
3181 cpl_image * data = NULL;
3182 cpl_image * err = NULL;
3183 cpl_image * qual = NULL;
3184 cpl_image * conf = NULL;
3185 cpl_image * bkg = NULL;
3186 cpl_image * bkg_err = NULL;
3187 cpl_image * bkg_conf = NULL;
3188 cpl_propertylist * plist = NULL;
3192 frameset_iter = cpl_frameset_iterator_new(frameset);
3195 for (cpl_frame * frame = NULL;
3196 (frame = cpl_frameset_iterator_get(frameset_iter)) &&
3197 (cpl_error_get_code() == CPL_ERROR_NONE);
3198 cpl_frameset_iterator_advance(frameset_iter, 1)) {
3200 const char * frametag = cpl_frame_get_tag(frame);
3201 if (!strcmp(tag, frametag)) {
3209 cpl_frameset_iterator_reset(frameset_iter);
3210 cpl_size position = 0;
3211 for (cpl_frame * frame = NULL;
3212 (frame = cpl_frameset_iterator_get(frameset_iter)) &&
3213 (cpl_error_get_code() == CPL_ERROR_NONE);
3214 cpl_frameset_iterator_advance(frameset_iter, 1)) {
3219 const char * frametag = cpl_frame_get_tag(frame);
3221 if (!strcmp(tag, frametag)) {
3464 cpl_frame * dup_frame = cpl_frame_duplicate(frame);
3465 cpl_frameset_insert(used, dup_frame);
3473 cpl_propertylist_delete(plist);
3474 cpl_image_delete(data);
3475 cpl_image_delete(err);
3476 cpl_image_delete(qual);
3477 cpl_image_delete(conf);
3478 cpl_image_delete(bkg);
3479 cpl_image_delete(bkg_err);
3480 cpl_image_delete(bkg_conf);
3482 cpl_frameset_iterator_delete(frameset_iter);
3483 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3507 const char * dps_name,
3508 const char * hduclas1,
3509 const char * hduclas2) {
3511 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3512 cpl_ensure(mef, CPL_ERROR_NULL_INPUT, NULL);
3513 cpl_ensure(dps_name, CPL_ERROR_NULL_INPUT, NULL);
3514 cpl_ensure(hduclas1, CPL_ERROR_NULL_INPUT, NULL);
3515 cpl_ensure(hduclas2, CPL_ERROR_NULL_INPUT, NULL);
3517 cpl_image * result = NULL;
3519 cpl_propertylist * plist = mef->plist;
3520 const char * hduclass = cpl_propertylist_get_string(plist,
"HDUCLASS");
3521 const char * hdudoc = cpl_propertylist_get_string(plist,
"HDUDOC");
3523 const char * my_hduclas1 = cpl_propertylist_get_string(plist,
"HDUCLAS1");
3524 const char * my_hduclas2 = cpl_propertylist_get_string(plist,
"HDUCLAS2");
3526 enu_check(strstr(hduclass,
"ESO") != NULL &&
3527 strstr(hdudoc,
"SDP") != NULL &&
3529 strstr(my_hduclas1, hduclas1) != NULL &&
3530 strstr(my_hduclas2, hduclas2) != NULL,
3531 CPL_ERROR_BAD_FILE_FORMAT,
"bad %s format", dps_name);
3536 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3539 if (result != NULL) mef->data = NULL;
3558 for (cpl_size i = 0; i < limlist->size; i++) {
3561 cpl_free(limlist->limages);
3578 const located_imagelist * limlist) {
3580 cpl_ensure(limlist, CPL_ERROR_NULL_INPUT, NULL);
3583 for (cpl_size i = 0; i < limlist->size; i++) {
3586 limlist->limages[i]), i);
3607 located_image * limage,
3608 cpl_size position) {
3610 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
3614 cpl_ensure_code(position >= 0 && position < limlist->size,
3615 CPL_ERROR_ACCESS_OUT_OF_RANGE);
3616 limlist->limages[position] = limage;
3618 return cpl_error_get_code();
3635 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3637 located_imagelist * limlist = cpl_malloc(
sizeof(located_imagelist));
3638 limlist->size = size;
3639 limlist->limages = cpl_calloc(size,
sizeof(located_image));
3640 for (cpl_size i = 0; i < limlist->size; i++) {
3641 limlist->limages[i] = NULL;
3660 if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_IMAGE)) {
3661 cpl_image_delete((cpl_image *) (mef->data));
3662 }
else if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_MASK)) {
3663 cpl_mask_delete((cpl_mask *) mef->data);
3664 }
else if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_TABLE)) {
3665 cpl_table_delete((cpl_table *) mef->data);
3666 }
else if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_VECTOR)) {
3667 cpl_vector_delete((cpl_vector *) mef->data);
3669 cpl_error_set(
"enu_mef_extension_delete",
3670 CPL_ERROR_UNSUPPORTED_MODE);
3672 cpl_propertylist_delete(mef->plist);
3673 cpl_free(mef->name);
3692 for (cpl_size i = 0; i < list->size; i++) {
3695 cpl_free(list->mef);
3714 const char * target) {
3716 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3718 cpl_ensure(meflist, CPL_ERROR_NULL_INPUT, NULL);
3719 cpl_ensure(target, CPL_ERROR_NULL_INPUT, NULL);
3723 cpl_mask * result = NULL;
3724 for (
int i=0; i<meflist->size; i++) {
3725 if (!strcmp(meflist->mef[i]->name, target)) {
3726 cpl_ensure(!strcmp(meflist->mef[i]->data_type,
3727 MEF_EXTENSION_CONTAINING_MASK),
3728 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
3729 result = cpl_mask_duplicate((
const cpl_mask *) meflist->mef[i]->data);
3736 cpl_ensure(result, CPL_ERROR_DATA_NOT_FOUND, NULL);
3755 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3757 mef_extension_list * mef_list = cpl_malloc(
sizeof(mef_extension_list));
3758 mef_list->size = size;
3759 mef_list->mef = cpl_calloc(size,
sizeof(mef_extension));
3760 for (cpl_size i = 0; i < size; i++) {
3761 mef_list->mef[i] = NULL;
3785 const cpl_image * data,
3786 const cpl_propertylist * plist) {
3788 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3792 cpl_ensure(name && data, CPL_ERROR_NULL_INPUT, NULL);
3794 mef_extension * result = cpl_malloc(
sizeof(mef_extension));
3795 result->name = cpl_strdup(name);
3796 result->data = (
void *) cpl_image_duplicate(data);
3797 result->data_type = MEF_EXTENSION_CONTAINING_IMAGE;
3799 result->plist = cpl_propertylist_duplicate(plist);
3801 result->plist = NULL;
3824 const cpl_mask * data,
3825 const cpl_propertylist * plist) {
3827 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3831 cpl_ensure(name && data, CPL_ERROR_NULL_INPUT, NULL);
3833 mef_extension * result = cpl_malloc(
sizeof(mef_extension));
3834 result->name = cpl_strdup(name);
3835 result->data = (
void *) cpl_mask_duplicate(data);
3836 result->data_type = MEF_EXTENSION_CONTAINING_MASK;
3838 result->plist = cpl_propertylist_duplicate(plist);
3840 result->plist = NULL;
3863 const cpl_table * table,
3864 const cpl_propertylist * plist) {
3866 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3867 cpl_ensure(name, CPL_ERROR_NULL_INPUT, NULL);
3868 cpl_ensure(table, CPL_ERROR_NULL_INPUT, NULL);
3870 mef_extension * result = cpl_malloc(
sizeof(mef_extension));
3871 result->name = cpl_strdup(name);
3872 result->data = (
void *) cpl_table_duplicate(table);
3873 result->plist = NULL;
3875 result->plist = cpl_propertylist_duplicate(plist);
3877 result->plist = NULL;
3879 result->data_type = MEF_EXTENSION_CONTAINING_TABLE;
3902 const cpl_vector * vector,
3903 const cpl_propertylist * plist) {
3905 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
3906 cpl_ensure(name, CPL_ERROR_NULL_INPUT, NULL);
3907 cpl_ensure(vector, CPL_ERROR_NULL_INPUT, NULL);
3909 mef_extension * result = cpl_malloc(
sizeof(mef_extension));
3910 result->name = cpl_strdup(name);
3911 result->data = (
void *) cpl_vector_duplicate(vector);
3912 result->plist = NULL;
3914 result->plist = cpl_propertylist_duplicate(plist);
3916 result->plist = NULL;
3918 result->data_type = MEF_EXTENSION_CONTAINING_VECTOR;
3939 const char * filename,
3940 const cpl_propertylist * plist,
3943 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
3945 cpl_msg_debug(cpl_func,
"enu_mef_extension_save entry");
3947 cpl_propertylist * plist_copy = NULL;
3951 cpl_ensure_code(mef, CPL_ERROR_NULL_INPUT);
3952 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
3953 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
3955 plist_copy = cpl_propertylist_duplicate(plist);
3956 cpl_propertylist_update_string(plist_copy,
"ERIS_NIX_MEF_TYPE",
3959 if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_IMAGE)) {
3960 if (cpl_propertylist_has(plist,
"CD3_3")) {
3971 cpl_msg_info(cpl_func,
"..saving data with NDIM=3");
3973 cpl_imagelist * imlist = cpl_imagelist_new();
3974 cpl_imagelist_set(imlist, mef->data, 0);
3975 cpl_imagelist_save(imlist,
3977 CPL_TYPE_UNSPECIFIED,
3981 cpl_imagelist_unset(imlist, 0);
3982 cpl_imagelist_delete(imlist);
3985 cpl_image_save((
const cpl_image *) (mef->data),
3987 CPL_TYPE_UNSPECIFIED,
3991 }
else if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_MASK)) {
3992 cpl_mask_save((
const cpl_mask *) (mef->data),
3996 }
else if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_TABLE)) {
3997 cpl_table_save((
const cpl_table *) (mef->data),
4002 }
else if (!strcmp(mef->data_type, MEF_EXTENSION_CONTAINING_VECTOR)) {
4003 cpl_vector_save((
const cpl_vector *) (mef->data),
4009 cpl_error_set_message(
"enu_mef_extension_save",
4010 CPL_ERROR_UNSUPPORTED_MODE,
4011 "unsupported extension type: %s", mef->data_type);
4014 cpl_propertylist_delete(plist_copy);
4016 return cpl_error_get_code();
4037 const double obj_threshold,
4038 const int bkg_mesh_size,
4039 const double bkg_smooth_fwhm,
4040 located_imagelist * limlist) {
4042 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4046 cpl_ensure_code(limlist, CPL_ERROR_NULL_INPUT);
4048 casu_fits ** indata = NULL;
4049 casu_fits ** inconf = NULL;
4050 casu_fits ** invar = NULL;
4059 int casu_status = CASU_OK;
4062 cpl_msg_info(cpl_func,
".. calculating object masks");
4063 for (cpl_size j = 0; j < limlist->size; j++) {
4064 casu_opm(indata[j], inconf[j], obj_min_pixels, obj_threshold,
4065 bkg_mesh_size, bkg_smooth_fwhm, niter, &casu_status);
4066 enu_check(casu_status == CASU_OK, CPL_ERROR_UNSPECIFIED,
4067 "CASU error calculating object mask");
4068 limlist->limages[j]->object_mask = cpl_mask_duplicate(
4069 cpl_image_get_bpm(indata[j]->image));
4074 for (cpl_size j = 0; j < limlist->size; j++) {
4075 casu_fits_delete(indata[j]);
4076 casu_fits_delete(inconf[j]);
4077 casu_fits_delete(invar[j]);
4083 return cpl_error_get_code();
4106 const int nsigma_cut) {
4108 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4109 cpl_ensure_code(limlist, CPL_ERROR_NULL_INPUT);
4111 cpl_msg_info(cpl_func,
"..calculating LSS object pixel mask");
4116 for (cpl_size j = 0; j < limlist->size; j++) {
4119 cpl_image * collapsed = cpl_image_collapse_median_create(
4121 limlist->limages[j]->himage), 0, 0, 0);
4125 double csigma = 0.0;
4126 double cmedian = cpl_image_get_median_dev(collapsed, &csigma);
4127 double maxval = cpl_image_get_max(collapsed);
4128 cpl_mask * collapsed_opm = cpl_mask_threshold_image_create(
4129 collapsed, cmedian + nsigma_cut * csigma,
4134 cpl_mask * kernel = cpl_mask_new(11, 1);
4135 cpl_mask_not(kernel);
4136 cpl_mask * dilated_collapsed_opm = cpl_mask_new(nx, 1);
4137 cpl_mask_filter(dilated_collapsed_opm, collapsed_opm, kernel,
4138 CPL_FILTER_DILATION, CPL_BORDER_NOP);
4139 cpl_mask_delete(kernel);
4143 const cpl_binary * collapsed_opm_data = cpl_mask_get_data_const(
4144 dilated_collapsed_opm);
4145 cpl_mask * opm = cpl_mask_new(nx, ny);
4146 cpl_binary * opm_data = cpl_mask_get_data(opm);
4147 for (cpl_size iy=0; iy < ny; iy++) {
4148 memcpy(opm_data + iy * (nx *
sizeof(*opm_data)),
4150 nx *
sizeof(*opm_data));
4155 cpl_mask_delete(limlist->limages[j]->object_mask);
4156 limlist->limages[j]->object_mask = opm;
4158 cpl_image_delete(collapsed);
4159 cpl_mask_delete(collapsed_opm);
4160 cpl_mask_delete(dilated_collapsed_opm);
4163 return cpl_error_get_code();
4183 const char * wcs_method,
4184 const char * catalogue,
4185 located_image * limage,
4186 const double match_rad,
4187 cpl_table ** matched_stds,
4188 cpl_matrix ** xy_shift) {
4190 cpl_matrix * celestial = NULL;
4191 cpl_array * colnames = NULL;
4192 cpl_array * conv_status = NULL;
4193 cpl_table * refcat_copy = NULL;
4194 cpl_matrix * ref_phys = NULL;
4195 cpl_matrix * ref_world = NULL;
4196 cpl_matrix * xy = NULL;
4197 cpl_wcs * wcs = NULL;
4199 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4201 cpl_msg_info(cpl_func,
"correcting %s", cpl_frame_get_filename(limage->frame));
4205 cpl_msg_info(cpl_func,
" ..%s", cpl_frame_get_filename(limage->frame));
4209 cpl_size nref = cpl_table_get_nrow(refcat);
4210 const double * ra = cpl_table_get_data_double_const(refcat,
"RA");
4211 const double * dec = NULL;
4212 if (cpl_table_has_column(refcat,
"DEC")) {
4213 dec = cpl_table_get_data_double_const(refcat,
"DEC");
4215 ref_world = cpl_matrix_new(nref, 2);
4216 for (cpl_size iobj = 0; iobj < nref; iobj++) {
4217 cpl_matrix_set(ref_world, iobj, 0, ra[iobj]);
4218 cpl_matrix_set(ref_world, iobj, 1, dec[iobj]);
4220 wcs = cpl_wcs_new_from_propertylist(limage->plist);
4221 cpl_wcs_convert(wcs, ref_world, &ref_phys, &conv_status,
4222 CPL_WCS_WORLD2PHYS);
4223 enu_check_error_code(
"failure calculating predicted positions");
4229 refcat_copy = cpl_table_duplicate(refcat);
4230 colnames = cpl_table_get_column_names(refcat_copy);
4231 cpl_size ncol = cpl_array_get_size(colnames);
4232 for (cpl_size ic = 0; ic < ncol; ic++) {
4233 const char * colname = cpl_array_get_string(colnames, ic);
4234 if (strcmp(colname,
"RA") &&
4235 strcmp(colname,
"DEC") &&
4236 strcmp(colname,
"Aper_flux_3") &&
4237 strcmp(colname,
"Std index")) {
4238 cpl_table_erase_column(refcat_copy, colname);
4244 cpl_table_new_column(refcat_copy,
"xpredict", CPL_TYPE_DOUBLE);
4245 cpl_table_new_column(refcat_copy,
"ypredict", CPL_TYPE_DOUBLE);
4246 for (cpl_size iobj = 0; iobj < nref; iobj++) {
4247 cpl_table_set_double(refcat_copy,
"xpredict", iobj,
4248 cpl_matrix_get(ref_phys, iobj, 0));
4249 cpl_table_set_double(refcat_copy,
"ypredict", iobj,
4250 cpl_matrix_get(ref_phys, iobj, 1));
4252 enu_check_error_code(
"failure while creating predicted positions");
4256 cpl_msg_info(cpl_func,
" ..matching sources");
4258 if (limage->objects && limage->objects->catalogue) {
4259 nobj = cpl_table_get_nrow(limage->objects->catalogue);
4262 if (min(nref, nobj) > 0) {
4264 cpl_msg_info(cpl_func,
" ..calling enm_try_all_associations");
4270 enu_check_error_code(
"enu_correct_wcs failure at "
4271 "try_all_associations");
4278 enu_check_error_code(
"failure to correct wcs");
4281 cpl_msg_info(cpl_func,
" ..no catalogued objects, cannot correct wcs");
4285 cpl_matrix_delete(celestial);
4286 cpl_array_delete(colnames);
4287 cpl_array_delete(conv_status);
4288 cpl_table_delete(refcat_copy);
4289 cpl_matrix_delete(ref_phys);
4290 cpl_matrix_delete(ref_world);
4291 cpl_matrix_delete(xy);
4292 cpl_wcs_delete(wcs);
4294 return cpl_error_get_code();
4311 const cpl_table * refine_wcs) {
4313 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4314 cpl_ensure_code(limage, CPL_ERROR_NULL_INPUT);
4316 if (!refine_wcs)
return CPL_ERROR_NONE;
4318 const char * camera = cpl_propertylist_get_string(limage->plist,
4319 "ESO INS2 NXCW NAME");
4320 enu_check(camera != NULL,
4321 CPL_ERROR_ILLEGAL_INPUT,
4322 "failed to read NIX camera from ESO.INS2.NXCW.NAME");
4324 cpl_size nrow = cpl_table_get_nrow(refine_wcs);
4325 cpl_boolean match = CPL_FALSE;
4332 for (cpl_size row = 0; row < nrow; row++) {
4333 const char * row_cam = cpl_table_get_string(refine_wcs,
"camera", row);
4335 if (!strcmp(camera, row_cam)) {
4339 cd1_1 = cpl_table_get_double(refine_wcs,
"CD1_1", row, &ignore);
4340 cd1_2 = cpl_table_get_double(refine_wcs,
"CD1_2", row, &ignore);
4341 cd2_1 = cpl_table_get_double(refine_wcs,
"CD2_1", row, &ignore);
4342 cd2_2 = cpl_table_get_double(refine_wcs,
"CD2_2", row, &ignore);
4348 cpl_propertylist_update_double(limage->plist,
"CD1_1", cd1_1);
4349 cpl_propertylist_update_double(limage->plist,
"CD1_2", cd1_2);
4350 cpl_propertylist_update_double(limage->plist,
"CD2_1", cd2_1);
4351 cpl_propertylist_update_double(limage->plist,
"CD2_2", cd2_2);
4356 cpl_error_set_message(cpl_func,
4357 CPL_ERROR_ILLEGAL_INPUT,
4358 "camera name not matched: %s",
4361 cd1_1 = cpl_propertylist_get_double(limage->plist,
"CD1_1");
4362 cd1_2 = cpl_propertylist_get_double(limage->plist,
"CD1_2");
4363 cd2_1 = cpl_propertylist_get_double(limage->plist,
"CD2_1");
4364 cd2_2 = cpl_propertylist_get_double(limage->plist,
"CD2_2");
4365 cpl_msg_info(cpl_func,
"..set CD matrix cd1_1=%10.8e cd1_2=%10.8e "
4366 "cd_2_1=%10.8e cd2_2=%10.8e",
4367 cd1_1, cd1_2, cd2_1, cd2_2);
4372 return cpl_error_get_code();
4390 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4391 cpl_ensure_code(confidence, CPL_ERROR_NULL_INPUT);
4393 const cpl_size nx = cpl_image_get_size_x(confidence);
4394 const cpl_size ny = cpl_image_get_size_y(confidence);
4395 double * data = cpl_image_get_data_double(confidence);
4397 const cpl_mask * bpm = cpl_image_get_bpm_const(confidence);
4398 cpl_mask * mask = bpm ? cpl_image_get_bpm(confidence) : NULL;
4399 cpl_binary * mask_data = bpm ? cpl_mask_get_data(mask) : NULL;
4404 for (cpl_size i = 0; i < nx*ny; i++) {
4405 if (mask_data != NULL && mask_data[i] == CPL_TRUE) {
4406 mask_data[i] = CPL_FALSE;
4409 if (data[i] < 0.0) data[i] = 0.0;
4411 if (data[i] > 0.0) nonzero++;
4415 cpl_image_multiply_scalar(confidence, 100.0 * (
double)nonzero / sum);
4418 return cpl_error_get_code();
4460 const cpl_propertylist * plist,
4461 cpl_image * confidence,
4462 const int set_confidence) {
4466 double * hdata = cpl_image_get_data_double(himg);
4467 double * edata = cpl_image_get_data_double(eimg);
4468 cpl_binary* hbpm = cpl_mask_get_data(cpl_image_get_bpm(himg));
4469 cpl_binary* ebpm = cpl_mask_get_data(cpl_image_get_bpm(eimg));
4470 const int mx = cpl_image_get_size_x(himg);
4471 cpl_boolean is_fast_uncorr = CPL_FALSE;
4472 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4475 cpl_ensure_code(hdata != NULL, CPL_ERROR_UNSUPPORTED_MODE);
4476 cpl_ensure_code(edata != NULL, CPL_ERROR_UNSUPPORTED_MODE);
4477 cpl_ensure_code(hbpm != NULL, CPL_ERROR_UNSUPPORTED_MODE);
4478 cpl_ensure_code(ebpm != NULL, CPL_ERROR_UNSUPPORTED_MODE);
4480 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
4489 cpl_size nx_chip = 0;
4490 cpl_size ny_chip = 0;
4491 cpl_boolean windowed = 0;
4501 enu_check_error_code(
"failed to read detector window information");
4507 double mjd = cpl_propertylist_get_double(plist,
"MJD-OBS");
4508 enu_check_error_code(
"failed to read MJD-OBS");
4510 cpl_msg_info(cpl_func,
"PIPE-10417 DITDELAY bug, "
4511 "remove_read_offsets will ignore bottom 4 rows "
4514 const char * curr_name = cpl_propertylist_get_string(plist,
"ESO DET READ CURNAME");
4515 if (strcmp(curr_name,
"FAST_UNCORR") == 0) {
4516 is_fast_uncorr = CPL_TRUE;
4518 cpl_size top_width = 4;
4519 cpl_size bottom_width = 4;
4520 cpl_msg_debug(cpl_func,
4521 "..offsets from %d top rows and %d bottom rows of chip",
4522 (
int) top_width, (
int) bottom_width);
4524 cpl_size nread_chan = nx_chip / 64;
4528 cpl_vector* voff = cpl_vector_new(nread_chan);
4530 double* pvoff = cpl_vector_get_data(voff);
4531 for (cpl_size read_chan = 0; read_chan < nread_chan; read_chan++) {
4535 cpl_array * offset_data = cpl_array_new(0, CPL_TYPE_DOUBLE);
4538 for (cpl_size x = 64 * read_chan; x < 64 * (read_chan+1); x++) {
4543 if (x+1 >= strx && x+1 < strx+nx) {
4545 for (cpl_size y = 0; y < bottom_width; y++) {
4549 if (y+1 >= stry && y+1 < stry+ny) {
4550 const int ipos = x-strx+1 + mx * (y-stry+1);
4554 const double pixval = hdata[ipos];
4558 cpl_array_set_size(offset_data, ndata);
4559 cpl_array_set(offset_data, ndata-1, pixval);
4562 hbpm[ipos] = CPL_BINARY_1;
4563 ebpm[ipos] = CPL_BINARY_1;
4565 if (set_confidence && confidence) {
4566 cpl_image_set(confidence,
4575 for (cpl_size y = ny_chip-top_width; y < ny_chip; y++) {
4576 if (y+1 >= stry && y+1 < stry+ny) {
4577 const int ipos = x-strx+1 + mx * (y-stry+1);
4580 const double pixval = hdata[ipos];
4582 cpl_array_set_size(offset_data, ndata);
4583 cpl_array_set(offset_data, ndata-1, pixval);
4584 hbpm[ipos] = CPL_BINARY_1;
4585 ebpm[ipos] = CPL_BINARY_1;
4586 if (set_confidence && confidence) {
4587 cpl_image_set(confidence,
4600 hdrl_value offset = {0.0, 0.0};
4602 cpl_msg_debug(cpl_func,
"read_chan %d", (
int)read_chan);
4603 cpl_msg_debug(cpl_func,
"ndata %d", (
int)ndata);
4604 cpl_msg_debug(cpl_func,
"median %f", cpl_array_get_median(offset_data));
4605 cpl_msg_debug(cpl_func,
"mean %f", cpl_array_get_mean(offset_data));
4606 cpl_msg_debug(cpl_func,
"std %f", cpl_array_get_stdev(offset_data));
4611 double mean = cpl_array_get_mean(offset_data);
4612 double stdev = cpl_array_get_stdev(offset_data);
4614 for (cpl_size i = 0; i < cpl_array_get_size(offset_data); i++) {
4616 double datum = cpl_array_get_double(offset_data, i, &invalid);
4617 if (!invalid && fabs(datum - mean) > 5 * stdev) {
4618 cpl_array_set_invalid(offset_data, i);
4623 offset.data = cpl_array_get_mean(offset_data);
4624 offset.error = cpl_array_get_stdev(offset_data);
4625 pvoff[read_chan] = offset.data;
4627 cpl_msg_debug(cpl_func,
"..removing offset %f (%f) for read "
4628 "channel %d", offset.data, offset.error,
4631 for (cpl_size x = 64 * read_chan; x < 64 * (read_chan+1); x++) {
4632 for (cpl_size y = 0; y < ny_chip; y++) {
4634 if (x+1 >= strx && x+1 < strx+nx &&
4635 y+1 >= stry && y+1 < stry+ny) {
4636 const int ipos = x-strx+1 + mx * (y-stry+1);
4639 hdata[ipos] -= offset.data;
4640 edata[ipos] = hypot(edata[ipos], offset.error);
4646 cpl_array_delete(offset_data);
4651 if(is_fast_uncorr && ndata > 0) {
4652 cpl_image_add_scalar(himg,cpl_vector_get_mean(voff));
4657 for (cpl_size y = 0; y < ny_chip; y++) {
4658 for (cpl_size x = 0; x < 4; x++) {
4660 if (x+1 >= strx && x+1 < strx+nx &&
4661 y+1 >= stry && y+1 < stry+ny) {
4662 const int ipos = x-strx+1 + mx * (y-stry+1);
4664 hbpm[ipos] = CPL_BINARY_1;
4665 ebpm[ipos] = CPL_BINARY_1;
4667 if (set_confidence && confidence) {
4668 cpl_image_set(confidence,
4677 for (cpl_size y = 0; y < ny_chip; y++) {
4678 for (cpl_size x = nx_chip-4; x < nx_chip; x++) {
4680 if (x+1 >= strx && x+1 < strx+nx &&
4681 y+1 >= stry && y+1 < stry+ny) {
4686 if (set_confidence && confidence) {
4687 cpl_image_set(confidence,
4696 cpl_vector_delete(voff);
4699 if (set_confidence && confidence) {
4705 return cpl_error_get_code();
4719 const char * preface) {
4721 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
4723 char * in_name = cpl_strdup(filename);
4726 char * pch = strstr(filename,
"ERIS.20");
4728 pch = strstr(filename,
"NACO.20");
4731 char * outname = cpl_sprintf(
"%s.%s", preface, pch);
4757 const char * select_method,
4758 const double timerange,
4759 const located_imagelist * target_list,
4760 const located_imagelist * sky_list,
4761 const cpl_size x_probe,
4762 const cpl_size y_probe,
4763 hdrl_imagelist ** sky_himagelist,
4764 cpl_imagelist ** sky_confidence_list) {
4766 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
4768 cpl_image * contrib = NULL;
4769 hdrl_imagelist * skylist = NULL;
4770 cpl_image * sky_confidence = NULL;
4771 hdrl_image * sky_himage = NULL;
4772 cpl_vector * sky_indeces = NULL;
4774 cpl_msg_debug(cpl_func,
"enu_sky_backgrounds entry");
4778 cpl_ensure_code(method, CPL_ERROR_NULL_INPUT);
4779 cpl_ensure_code(select_method, CPL_ERROR_NULL_INPUT);
4780 cpl_ensure_code(target_list, CPL_ERROR_NULL_INPUT);
4781 cpl_ensure_code(sky_list, CPL_ERROR_NULL_INPUT);
4782 cpl_ensure_code(!strcmp(select_method,
"bracket") &&
4783 (timerange > 0.0), CPL_ERROR_ILLEGAL_INPUT);
4786 *sky_confidence_list = cpl_imagelist_new();
4790 int debug = CPL_FALSE;
4791 cpl_vector * data = NULL;
4792 cpl_vector * error = NULL;
4793 cpl_vector * reject = NULL;
4794 cpl_vector * confidence = NULL;
4796 cpl_msg_info(cpl_func,
"..calculating skys");
4800 cpl_size njitters = target_list->size;
4801 for (cpl_size j = 0; j < njitters; j++) {
4805 if (strstr(select_method,
"bracket") != NULL) {
4807 sky_list, x_probe > -1);
4809 enu_check_error_code(
"failure in enu_bracket_skys");
4811 cpl_size skysize = cpl_vector_get_size(sky_indeces);
4813 if (x_probe > 0 && skysize > 0) {
4815 sky_list->limages[0]->himage);
4817 sky_list->limages[0]->himage);
4818 if (x_probe > 0 && x_probe <= x_size &&
4819 y_probe > 0 && y_probe <= y_size) {
4822 data = cpl_vector_new(skysize);
4823 error = cpl_vector_new(skysize);
4824 reject = cpl_vector_new(skysize);
4825 confidence = cpl_vector_new(skysize);
4830 if (strstr(method,
"collapse-median") != NULL) {
4837 for (
int k = 0; k < (int)skysize; k++) {
4838 int i = (int)cpl_vector_get(sky_indeces, k);
4840 limages[i]->himage);
4841 cpl_image * confcopy = cpl_image_duplicate(sky_list->
4842 limages[i]->confidence);
4843 if (sky_list->limages[i]->object_mask != NULL) {
4845 sky_list->limages[i]->object_mask);
4846 cpl_mask_or(cpl_image_get_bpm(confcopy),
4847 sky_list->limages[i]->object_mask);
4848 cpl_image_fill_rejected(confcopy, 0.0);
4849 cpl_image_accept_all(confcopy);
4855 x_probe, y_probe, &rejected);
4857 double conf = cpl_image_get(confcopy,
4860 cpl_vector_set(data, k, himval.data);
4861 cpl_vector_set(error, k, himval.error);
4862 cpl_vector_set(reject, k, (
double)rejected);
4863 cpl_vector_set(confidence, k, conf);
4869 if (sky_confidence == NULL) {
4870 sky_confidence = confcopy;
4872 cpl_image_add(sky_confidence, confcopy);
4873 cpl_image_delete(confcopy);
4876 cpl_msg_debug(cpl_func,
"after skylist %s",
4877 cpl_error_get_message());
4883 cpl_image_delete(contrib);
4885 cpl_msg_debug(cpl_func,
"after collapse %s",
4886 cpl_error_get_message());
4894 ".. enu_sky_backgrounds: probe pixel %d,%d",
4895 (
int)x_probe, (
int)y_probe);
4896 cpl_msg_info(
" ",
".. jitter %d" , (
int)j);
4897 cpl_msg_info(
" ",
"... j# q c d (e)");
4899 for (cpl_size k = 0; k < skysize; k++) {
4900 cpl_msg_info(
" ",
"... %3d %3d %3d %8.2f(%4.2f)",
4901 (
int)cpl_vector_get(sky_indeces, k),
4902 (
int)cpl_vector_get(reject, k),
4903 (
int)cpl_vector_get(confidence, k),
4904 cpl_vector_get(data, k),
4905 cpl_vector_get(error, k));
4910 x_probe, y_probe, &rejected);
4911 double cval = cpl_image_get(sky_confidence, x_probe,
4913 cpl_msg_info(
" ",
"... -> %3d %3d %8.2f(%4.2f)",
4914 rejected, (
int)cval, hval.data, hval.error);
4916 cpl_vector_delete(data);
4917 cpl_vector_delete(error);
4918 cpl_vector_delete(reject);
4919 cpl_vector_delete(confidence);
4923 enu_check_error_code(
"failure in collapse-median");
4925 }
else if (strstr(method,
"median-median") != NULL) {
4930 cpl_array * sky_medians = cpl_array_new(0, CPL_TYPE_DOUBLE);
4932 for (
int k = 0; k < (int)skysize; k++) {
4933 int i = (int)cpl_vector_get(sky_indeces, k);
4938 sky_list->limages[i]->himage);
4939 if (sky_list->limages[i]->object_mask != NULL) {
4941 sky_list->limages[i]->object_mask);
4946 cpl_size asize = cpl_array_get_size(sky_medians);
4947 cpl_array_set_size(sky_medians, asize+1);
4948 cpl_array_set_double(sky_medians, asize, bkg.data);
4951 cpl_vector_set(data, k, bkg.data);
4952 cpl_vector_set(error, k, bkg.error);
4953 cpl_vector_set(confidence, k, 100.0);
4954 cpl_vector_set(reject, k, 0.0);
4961 double sky_med_med = cpl_array_get_median(sky_medians);
4962 double sky_med_stdev = cpl_array_get_stdev(sky_medians);
4965 cpl_image * sky_data = cpl_image_new(nx, ny, HDRL_TYPE_DATA);
4966 cpl_image_fill_window(sky_data, 1, 1, nx, ny, sky_med_med);
4967 cpl_image * sky_error = cpl_image_new(nx, ny, HDRL_TYPE_ERROR);
4968 cpl_image_fill_window(sky_error, 1, 1, nx, ny, sky_med_stdev);
4970 cpl_image_delete(sky_data);
4971 cpl_image_delete(sky_error);
4973 sky_confidence = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
4974 cpl_image_fill_window(sky_confidence, 1, 1, nx, ny, 100.0);
4976 cpl_array_delete(sky_medians);
4980 ".. enu_sky_backgrounds: median of images");
4981 cpl_msg_info(
" ",
".. jitter %d" , (
int)j);
4982 cpl_msg_info(
" ",
"... j# c d (e)");
4984 for (cpl_size k = 0; k < skysize; k++) {
4985 cpl_msg_info(
" ",
"... %3d %3d %8.2f(%4.2f)",
4986 (
int)cpl_vector_get(sky_indeces, k),
4987 (
int)cpl_vector_get(confidence, k),
4988 cpl_vector_get(data, k),
4989 cpl_vector_get(error, k));
4995 double cval = cpl_image_get(sky_confidence, 1, 1, &ignore);
4996 cpl_msg_info(
" ",
"... -> %3d %3d %8.2f(%4.2f)",
4997 rejected, (
int)cval, hval.data, hval.error);
4999 cpl_vector_delete(data);
5000 cpl_vector_delete(error);
5001 cpl_vector_delete(confidence);
5003 enu_check_error_code(
"failure in median-median");
5010 cpl_imagelist_set(*sky_confidence_list, sky_confidence,
5011 cpl_imagelist_get_size(*sky_confidence_list));
5012 cpl_vector_delete(sky_indeces); sky_indeces = NULL;
5017 cpl_vector_delete(sky_indeces);
5018 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5020 *sky_himagelist = NULL;
5021 cpl_imagelist_delete(*sky_confidence_list);
5022 *sky_confidence_list = NULL;
5025 return cpl_error_get_code();
5044 const double stk_lthr,
const double stk_hthr,
5045 const char * stk_method,
const int stk_fast,
5046 located_image ** result) {
5048 cpl_ensure_code(cpl_error_get_code() == CPL_ERROR_NONE,
5049 cpl_error_get_code());
5051 casu_fits ** indata = NULL;
5052 casu_fits ** inconf = NULL;
5053 casu_fits ** invar = NULL;
5054 casu_fits * out = NULL;
5055 casu_fits * outc = NULL;
5056 casu_fits * outv = NULL;
5060 cpl_ensure_code(limages, CPL_ERROR_NULL_INPUT);
5061 cpl_ensure_code((stk_fast == 0) || (stk_fast == 1),
5062 CPL_ERROR_ILLEGAL_INPUT);
5066 const int casu_stk_method = (!strcmp(stk_method,
"nearest") ? 0 : 1);
5074 int njitters = (int) limages->size;
5075 int casu_status = CASU_OK;
5076 casu_imstack(indata, inconf, invar, NULL, (
int)njitters, (int)njitters,
5077 stk_lthr, stk_hthr, casu_stk_method, 0, stk_fast, 0,
5078 "ESO DET DIT", &out, &outc, &outv, &casu_status);
5081 cpl_image_duplicate(outc->image),
5084 NULL, NULL, NULL, NULL, NULL);
5089 return cpl_error_get_code();
5109 const char * select_method,
5110 const double timerange,
5111 const located_imagelist * sky_data,
5112 const cpl_size x_probe,
5113 const cpl_size y_probe,
5114 located_imagelist * target_data) {
5116 cpl_ensure_code(cpl_error_get_code() == CPL_ERROR_NONE,
5117 cpl_error_get_code());
5118 cpl_ensure_code(method, CPL_ERROR_NULL_INPUT);
5119 cpl_ensure_code(select_method, CPL_ERROR_NULL_INPUT);
5120 cpl_ensure_code(target_data, CPL_ERROR_NULL_INPUT);
5122 cpl_imagelist * sky_conf_list = NULL;
5123 hdrl_imagelist * sky_himlist = NULL;
5125 cpl_msg_info(cpl_func,
5126 "enu_sky_subtract called: method=%s, sky_selector=%s, "
5128 method, select_method, timerange);
5129 cpl_size njitters = target_data->size;
5130 cpl_msg_info(cpl_func,
"number of jitters to be reduced %d",
5136 sky_data, x_probe, y_probe, &sky_himlist,
5138 enu_check_error_code(
"failure in enu_sky_backgrounds");
5142 hdrl_value before = {0.0, 0.0};
5144 hdrl_value bkg = {0.0, 0.0};
5145 double bkgconf = 0.0;
5146 hdrl_value after = {0.0, 0.0};
5149 int bkgrejected = 1;
5152 int header_printed = CPL_FALSE;
5154 for (cpl_size i = 0; i < njitters; i++) {
5159 target_data->limages[i]->himage);
5161 target_data->limages[i]->himage);
5162 if (x_probe > 0 && x_probe <= x_size &&
5163 y_probe > 0 && y_probe <= y_size) {
5165 x_probe, y_probe, &brejected);
5166 bconf = cpl_image_get(target_data->limages[i]->confidence,
5167 x_probe, y_probe, &ignore);
5176 target_data->limages[i]->bkg_confidence = cpl_image_duplicate(
5177 cpl_imagelist_get(sky_conf_list, i));
5196 cpl_image_fill_rejected(target_data->limages[i]->confidence, 0.0);
5197 cpl_image_accept_all(target_data->limages[i]->confidence);
5202 if (x_probe > 0 && x_probe <= x_size &&
5203 y_probe > 0 && y_probe <= y_size) {
5205 x_probe, y_probe, &arejected);
5206 aconf = cpl_image_get(target_data->limages[i]->confidence,
5207 x_probe, y_probe, &ignore);
5209 x_probe, y_probe, &bkgrejected);
5210 bkgconf = cpl_image_get(target_data->limages[i]->bkg_confidence,
5211 x_probe, y_probe, &ignore);
5213 if (!header_printed) {
5214 header_printed = CPL_TRUE;
5215 cpl_msg_info(
" ",
"..Subtracting sky backgrounds: probe pixel %d,%d",
5216 (
int)x_probe, (
int)y_probe);
5217 cpl_msg_info(
" ",
" j# before sky after");
5218 cpl_msg_info(
" ",
" q c d e q c d e q c d e");
5221 "..%3d %d %3.0f %4.2e(%4.2e)) %d %3.0f %4.2e(%4.2e) %d %3.0f %10.2e(%4.2e)",
5223 brejected, bconf, before.data, before.error,
5224 bkgrejected, bkgconf, bkg.data, bkg.error,
5225 arejected, aconf, after.data, after.error);
5230 cpl_imagelist_delete(sky_conf_list);
5233 return cpl_error_get_code();
5250 cpl_propertylist* qc_list,
5253 cpl_ensure_code(mbad_pix_map != NULL, CPL_ERROR_NULL_INPUT);
5254 cpl_ensure_code(qc_list != NULL, CPL_ERROR_NULL_INPUT);
5255 cpl_ensure_code(prefix != NULL, CPL_ERROR_NULL_INPUT);
5258 cpl_mask * bpm_mask = NULL;
5260 flag_mask = ~flag_mask;
5261 bpm_mask = en_master_bpm_get_mask(mbad_pix_map, flag_mask);
5262 int nbad = cpl_mask_count(bpm_mask);
5263 cpl_propertylist_append_int(qc_list,
"ESO QC NUMBER BAD PIXELS", nbad);
5264 cpl_size sx = cpl_mask_get_size_x(bpm_mask);
5265 cpl_size sy = cpl_mask_get_size_y(bpm_mask);
5266 double fraction = (double) nbad / (sx * sy);
5267 cpl_propertylist_append_double(qc_list,
"ESO QC FRACTION BAD PIXELS",
fraction);
5268 return cpl_error_get_code();
5288 cpl_mask* bp_map_nl_mask,
5289 const cpl_parameterlist* parlist,
5290 const char* context,
5291 const double threshold_pos,
5292 const cpl_boolean verbose,
5293 const cpl_boolean rescale_by_dit){
5295 cpl_ensure(lamp_on_limlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
5296 cpl_ensure(bp_map_nl_mask != NULL, CPL_ERROR_NULL_INPUT, NULL);
5297 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
5298 cpl_ensure(context != NULL, CPL_ERROR_NULL_INPUT, NULL);
5301 cpl_propertylist* qc;
5310 qc = cpl_propertylist_new();
5311 double threshold_neg = - 4.3e7;
5312 threshold_neg = 300.;
5313 long nthresh_pos = 0;
5314 long nthresh_neg = 0;
5316 const cpl_parameter* param;
5320 pname = cpl_sprintf(
"%s%s",context,
".saturation_neg");
5321 cpl_msg_warning(cpl_func,
"pname: %s",pname);
5322 param = cpl_parameterlist_find_const(parlist,pname);
5323 threshold_neg = cpl_parameter_get_double(param);
5329 double mean_min = DBL_MAX;
5330 double median_min = DBL_MAX;
5332 double mean_max = -DBL_MIN;
5333 double median_max = -DBL_MIN;
5334 double nthresh_pos_max = 0;
5335 double nthresh_neg_max = 0;
5343 cpl_propertylist* plist = NULL;
5344 for(cpl_size i = 0; i < lamp_on_limlist->size; i++) {
5352 cpl_mask_or(mask, bp_map_nl_mask);
5356 plist = lamp_on_limlist->limages[i]->plist;
5357 if(cpl_propertylist_has(plist,
"ESO DET SEQ1 DIT")) {
5358 dit = cpl_propertylist_get_double(plist,
"ESO DET SEQ1 DIT");
5360 nthresh_neg = eris_image_get_threshpix(ima, threshold_neg, CPL_FALSE);
5361 nthresh_pos = eris_image_get_threshpix(ima, threshold_pos, CPL_TRUE);
5363 eris_image_flag_threshpix(&ima, threshold_neg, CPL_FALSE);
5364 mean = cpl_image_get_mean(ima);
5365 median = cpl_image_get_median(ima);
5366 rms = cpl_image_get_stdev(ima);
5371 if(rescale_by_dit) {
5377 key_name = cpl_sprintf(
"ESO QC FLAT_ON%lld NPOSSAT",i);
5378 key_comm = cpl_sprintf(
"Number of flat pixels above threshold");
5379 cpl_propertylist_append_int(qc, key_name, nthresh_pos);
5380 cpl_propertylist_set_comment(qc, key_name, key_comm);
5384 key_name = cpl_sprintf(
"ESO QC FLAT_ON%lld NNEGSAT",i);
5385 key_comm = cpl_sprintf(
"Number of flat pixels below neg threshold");
5386 cpl_propertylist_append_int(qc, key_name, nthresh_neg);
5387 cpl_propertylist_set_comment(qc, key_name, key_comm);
5391 key_name = cpl_sprintf(
"ESO QC FLAT_ON%lld MEAN",i);
5392 key_comm = cpl_sprintf(
"[ADU] Mean of flat");
5393 cpl_propertylist_append_double(qc, key_name, mean);
5394 cpl_propertylist_set_comment(qc,key_name,key_comm);
5398 key_name = cpl_sprintf(
"ESO QC FLAT_ON%lld MEDIAN",i);
5399 key_comm = cpl_sprintf(
"[ADU] Median of flat");
5400 cpl_propertylist_append_double(qc, key_name, median);
5401 cpl_propertylist_set_comment(qc,key_name,key_comm);
5405 key_name = cpl_sprintf(
"ESO QC FLAT_ON%lld STDEV",i);
5406 key_comm = cpl_sprintf(
"[ADU] Stdev of flat");
5407 cpl_propertylist_append_double(qc, key_name, rms);
5408 cpl_propertylist_set_comment(qc,key_name,key_comm);
5413 if(mean > mean_max) {
5416 if(median > median_max) {
5417 median_max = median;
5421 if(mean < mean_min) {
5424 if(median < median_min) {
5425 median_min = median;
5429 if(nthresh_pos > nthresh_pos_max) {
5430 nthresh_pos_max = nthresh_pos;
5432 if(nthresh_neg > nthresh_neg_max) {
5433 nthresh_neg_max = nthresh_neg;
5436 cpl_msg_info(cpl_func,
"mean_min: %g",mean_min);
5437 cpl_msg_info(cpl_func,
"mean_max: %g",mean_max);
5438 cpl_msg_info(cpl_func,
"median_min: %g",median_min);
5439 cpl_msg_info(cpl_func,
"median_max: %g",median_max);
5440 cpl_msg_info(cpl_func,
"f1: %g",mean_min / mean_max);
5441 cpl_msg_info(cpl_func,
"f2: %g",median_min / median_max);
5443 key_name = cpl_sprintf(
"ESO QC FLAT_ON MEAN MIN");
5444 key_comm = cpl_sprintf(
"[ADU] Min of Means of flat");
5445 cpl_propertylist_append_double(qc, key_name, mean_min);
5446 cpl_propertylist_set_comment(qc,key_name,key_comm);
5450 key_name = cpl_sprintf(
"ESO QC FLAT_ON MEDIAN MIN");
5451 key_comm = cpl_sprintf(
"[ADU] Min of Medians of flat");
5452 cpl_propertylist_append_double(qc, key_name, median_min);
5453 cpl_propertylist_set_comment(qc,key_name,key_comm);
5457 key_name = cpl_sprintf(
"ESO QC FLAT_ON MEAN MAX");
5458 key_comm = cpl_sprintf(
"[ADU] Max of Means of flat");
5459 cpl_propertylist_append_double(qc, key_name, mean_max);
5460 cpl_propertylist_set_comment(qc,key_name,key_comm);
5464 key_name = cpl_sprintf(
"ESO QC FLAT_ON MEDIAN MAX");
5465 key_comm = cpl_sprintf(
"[ADU] Max of Medians of flat");
5466 cpl_propertylist_append_double(qc, key_name, median_max);
5467 cpl_propertylist_set_comment(qc,key_name,key_comm);
5472 key_name = cpl_sprintf(
"ESO QC FLAT_ON MEAN FRAC");
5473 key_comm = cpl_sprintf(
"Min/Max of Means of flat");
5474 cpl_propertylist_append_double(qc, key_name, mean_min / mean_max);
5475 cpl_propertylist_set_comment(qc,key_name,key_comm);
5480 key_name = cpl_sprintf(
"ESO QC FLAT_ON MEDIAN FRAC");
5481 key_comm = cpl_sprintf(
"Min/Max of Medians of flat");
5482 cpl_propertylist_append_double(qc, key_name, median_min / median_max);
5483 cpl_propertylist_set_comment(qc,key_name,key_comm);
5489 key_name = cpl_sprintf(
"ESO QC FLAT_ON NPOSSAT MAX");
5490 key_comm = cpl_sprintf(
"Max of Number of flat pixels above threshold");
5491 cpl_propertylist_append_int(qc, key_name, nthresh_pos_max);
5492 cpl_propertylist_set_comment(qc, key_name, key_comm);
5496 key_name = cpl_sprintf(
"ESO QC FLAT_ON NNEGSAT MAX");
5497 key_comm = cpl_sprintf(
"MAX Number of flat pixels below neg threshold");
5498 cpl_propertylist_append_int(qc, key_name, nthresh_neg_max);
5499 cpl_propertylist_set_comment(qc, key_name, key_comm);
cpl_error_code encu_limlist_to_casu_fits(located_imagelist *limlist, casu_fits ***indata, casu_fits ***inconf, casu_fits ***invar)
Translate a located_imagelist to arrays of casu_fits structs.
cpl_error_code enu_dfs_save_limage(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *provenance, const cpl_boolean prov_raw, const located_image *limage, const char *recipe, const cpl_frame *inherit, cpl_propertylist *applist, const char *pipe_id, const char *filename)
Save a located image structure to a MEF.
cpl_error_code enu_dfs_save_himage(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *provenance, const cpl_boolean prov_raw, const hdrl_image *image, const hdrl_imagelist *imagelist, const mef_extension_list *mefs, const char *recipe, const cpl_frame *inherit_frame, const cpl_propertylist *applist, const cpl_propertylist *wcs_plist, const char *pipe_id, const char *filename)
Save an hdrl_image/imagelist as a DFS-compliant MEF pipeline product.
cpl_error_code enm_correct_crpix(const char *wcs_method, const char *catalogue, cpl_table *matched_stds, located_image *limage, cpl_matrix **xy_shift)
Apply median offsets from matched standards table to image.
cpl_error_code enm_try_all_associations(cpl_table *objtab, cpl_table *stdtab, const double match_rad, cpl_table **matchtab)
Match standards with objects by trying all possibilities and selecting best.
located_image * enu_located_image_duplicate(const located_image *limage)
Make a deep copy of a located_image and its contents.
cpl_mask * enu_mef_extension_list_get_mask(mef_extension_list *meflist, const char *target)
Get a cpl_mask from a named mef_extension in a list.
cpl_vector * enu_bracket_skys(const located_image *target, const double timerange, const located_imagelist *pool, const int debug)
Find images taken within a given time of a target image.
cpl_error_code enu_modify_CD_matrix(located_image *limage, const cpl_table *refine_wcs)
Update the CD matrix to reduce image distortion and rotation.
void enu_located_imagelist_delete(located_imagelist *limlist)
Delete a located_imagelist and its contents.
cpl_error_code enu_located_imagelist_insert(located_imagelist *limlist, located_image *limage, cpl_size position)
Insert a located_image at a specified point in a located_imagelist.
void enu_mef_extension_delete(mef_extension *mef)
Delete a mef_extension and its contents.
cpl_error_code eris_nix_get_badpix_qc_from_ima(const master_bpm *mbad_pix_map, cpl_propertylist *qc_list, const char *prefix)
Compute QC on input master bpm.
mef_extension * enu_mef_new_mask(const char *name, const cpl_mask *data, const cpl_propertylist *plist)
Create a mef_extension struct holding a cpl_mask.
cpl_propertylist * enu_raw_flats_qc(located_imagelist *lamp_on_limlist, cpl_mask *bp_map_nl_mask, const cpl_parameterlist *parlist, const char *context, const double threshold_pos, const cpl_boolean verbose, const cpl_boolean rescale_by_dit)
Compute QC on input raw flats.
cpl_error_code enu_flat_save(const char *pro_catg, const hdrl_image *flat, const cpl_image *confidence, const cpl_mask *cold_bpm, cpl_frameset *frameset, const cpl_parameterlist *parlist, const char *filename, const char *recipe_name, const cpl_propertylist *qclog)
Save a flatfield result.
int enu_check_conformance(const cpl_propertylist *plist1, const cpl_propertylist *plist2, const char *regexp)
Check that the specified subset of two propertylists match.
located_image * enu_load_limage_from_frame(const cpl_frame *frame, cpl_image **pcopyconf, const cpl_boolean collapse_cube)
Load components of a located_image from a frame.
hdrl_catalogue_result * enu_hdrl_catalogue_result_duplicate(const hdrl_catalogue_result *target)
Return a deep copy of the given hdrl_catalogue_result object.
located_imagelist * enu_located_imagelist_duplicate(const located_imagelist *limlist)
Make a deep copy of a located_imagelist and its contents.
cpl_error_code enu_calc_maglim(const located_image *limage, const double photzp, const double fwhm_pix, double *abmaglim)
Calculate magnitude limit of image.
cpl_error_code enu_opm_lss_limlist(located_imagelist *limlist, const int nsigma_cut)
Calculate object masks for LSS images in a located_imagelist.
double enu_get_tel_alt(const cpl_propertylist *plist)
Get telescope altitude of an observation.
mef_extension * enu_mef_new_image(const char *name, const cpl_image *data, const cpl_propertylist *plist)
Create a mef_extension to hold a cpl_image.
cpl_error_code enu_get_ra_dec(const cpl_wcs *wcs, double *ra, double *dec)
Get RA and Dec at centre of image with given wcs.
hdrl_image * enu_calc_flat(hdrl_imagelist *himlist, const int min_coadds, const hdrl_parameter *collapse_params, const cpl_size filter_size_x, const cpl_size filter_size_y, const hdrl_flat_method method)
Calculate a flatfield result.
cpl_error_code enu_get_window_info(cpl_size *nx, cpl_size *ny, int *rot, cpl_size *strx, cpl_size *stry, cpl_size *nx_chip, cpl_size *ny_chip, cpl_boolean *windowed, const cpl_propertylist *plist)
Get the detector 'window' information.
cpl_error_code enu_basic_calibrate(located_image *limage, const int read_offsets, const cpl_table *refine_wcs, const master_dark *mdark, const gain_linearity *gain_lin, const master_flat *flatfield_1, const master_flat *flatfield_2, const master_bpm *mbad_pix_map, const int flag_mask, const char *fill_rejected, const double fill_value, const cpl_size x_probe, const cpl_size y_probe)
Do basic calibration of located_image (single or cube)
cpl_error_code enu_catalogue_limlist(located_imagelist *limlist, hdrl_parameter *params)
Calculate object catalogues for a list of images.
cpl_error_code enu_sky_subtract_limlist(const char *method, const char *select_method, const double timerange, const located_imagelist *sky_data, const cpl_size x_probe, const cpl_size y_probe, located_imagelist *target_data)
Estimate and subtract sky backgrounds for a list of target images.
cpl_error_code enu_himage_load_from_fits(const char *filename, hdrl_image **result, mef_extension_list **mef_extensions, cpl_propertylist **plist)
Load an hdrl_image from a multi-extension FITS file.
cpl_error_code enu_debug_limlist_save(const int debug, const located_imagelist *limlist, const char *nameroot, const char *recipename, cpl_frameset *frameset, const cpl_parameterlist *parlist, const cpl_frameset *used)
Save a list of intermediate image results for use in debugging.
mef_extension * enu_mef_new_table(const char *name, const cpl_table *table, const cpl_propertylist *plist)
Create a mef_extension struct holding a cpl_table.
mef_extension_list * enu_mef_extension_list_new(cpl_size size)
Construct a new mef_extension_list.
located_image * enu_located_image_new(hdrl_image *himage, hdrl_imagelist *himagelist, cpl_image *confidence, hdrl_image *bkg, cpl_image *bkg_confidence, cpl_propertylist *plist, hdrl_catalogue_result *objects, cpl_mask *object_mask, hdrl_catalogue_result *wcs, hdrl_catalogue_result *photom, cpl_frame *frame)
Create a located_image structure and initialise the contents.
double enu_get_airmass(const cpl_propertylist *plist)
Get the mean airmass of an observation.
cpl_error_code enu_calc_pixel_coords(cpl_table *catalogue, const cpl_propertylist *wcs_plist)
Calculate predicted positions of catalogue objects for given wcs.
located_imagelist * enu_limlist_load_from_frameset(cpl_frameset *frameset, const char *tag, cpl_frameset *used)
Load tagged data from a frameset into a located_imagelist.
void enu_located_image_delete(located_image *limage)
Delete a located_image and its contents.
mef_extension * enu_mef_new_vector(const char *name, const cpl_vector *vector, const cpl_propertylist *plist)
Create a mef_extension struct holding a cpl_vector.
cpl_error_code enu_remove_read_offsets(hdrl_image *himage, const cpl_propertylist *plist, cpl_image *confidence, const int set_confidence)
Function to remove read offsets from an himage and set blank pixels
cpl_error_code enu_normalise_confidence(cpl_image *confidence)
Normalise confidence array so that mean of good pixels is 100.
cpl_error_code enu_stack(located_imagelist *limages, const double stk_lthr, const double stk_hthr, const char *stk_method, const int stk_fast, located_image **result)
Stack a set of images.
cpl_error_code enu_correct_wcs(const cpl_table *refcat, const char *wcs_method, const char *catalogue, located_image *limage, const double match_rad, cpl_table **matched_stds, cpl_matrix **xy_shift)
Correct the wcs of an image.
cpl_error_code enu_basic_calibrate_himage(hdrl_image *himage, cpl_image *confidence, cpl_propertylist *plist, const cpl_frame *frame, const int read_offsets, const master_dark *mdark, const gain_linearity *gain_lin, const master_flat *flatfield_1, const master_flat *flatfield_2, const master_bpm *mbad_pix_map, const int set_confidence, const int flag_mask, const char *fill_rejected, const double fill_value, const cpl_size x_probe, const cpl_size y_probe)
Do basic calibration of an hdrl_image.
cpl_error_code enu_mef_extension_save(const mef_extension *mef, const char *filename, const cpl_propertylist *plist, unsigned mode)
Save a mef_extension struct to FITS file.
char * enu_repreface(const char *filename, const char *preface)
Preface a raw filename with a string.
const char * enu_get_license(void)
Get the pipeline copyright and license.
void enu_mef_extension_list_delete(mef_extension_list *list)
Delete a mef_extension_list and its contents.
cpl_image * enu_load_component(mef_extension *mef, const char *dps_name, const char *hduclas1, const char *hduclas2)
Load data from a MEF component of specified type.
const char * enu_get_filter(const cpl_propertylist *plist)
Get the filter used in an observation.
cpl_error_code enu_check_wcs(const located_image *limage)
Check that the image has valid WCS keywords.
cpl_error_code enu_get_rcore_and_mesh_size(const char *context, const cpl_parameterlist *parlist, const cpl_propertylist *plist, double *obj_core_radius, int *bkg_mesh_size)
Get catalogue core-radius and mesh-size appropriate to AO mode.
located_imagelist * enu_located_imagelist_new(cpl_size size)
Return a pointer to a new located_imagelist.
hdrl_catalogue_result * enu_catalogue_compute(const hdrl_image *himage, const cpl_image *confidence, const cpl_wcs *wcs, hdrl_parameter *params)
Wrapper for hdrl_catalogue_compute.
cpl_error_code enu_opm_limlist(const int obj_min_pixels, const double obj_threshold, const int bkg_mesh_size, const double bkg_smooth_fwhm, located_imagelist *limlist)
Calculate object masks for images in a located_imagelist.
const char * enu_get_det_mode(const cpl_propertylist *plist)
Get the detector mode of an integration.
cpl_error_code enu_sky_backgrounds(const char *method, const char *select_method, const double timerange, const located_imagelist *target_list, const located_imagelist *sky_list, const cpl_size x_probe, const cpl_size y_probe, hdrl_imagelist **sky_himagelist, cpl_imagelist **sky_confidence_list)
Get sky backgrounds for a list of target images.
double enu_get_dit(const cpl_propertylist *plist)
Get the DIT of an integration.
double enu_get_filter_wavelength(const char *filter)
Get the effective wavelength of a filter.
mef_extension_list * enu_load_mef_components(const char *filename, cpl_propertylist **plist)
Load components of a multi-extension FITS file.
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
hdrl_catalogue_result * hdrl_catalogue_compute(const cpl_image *image_, const cpl_image *confidence_map, const cpl_wcs *wcs, hdrl_parameter *param_)
build object catalog
void hdrl_catalogue_result_delete(hdrl_catalogue_result *result)
delete hdrl parameter result object
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
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_error_code hdrl_flat_compute(hdrl_imagelist *hdrl_data, const cpl_mask *stat_mask, const hdrl_parameter *collapse_params, hdrl_parameter *flat_params, hdrl_image **master, cpl_image **contrib_map)
compute high or low frequency master flat with median filtering
hdrl_value hdrl_image_get_pixel(const hdrl_image *self, cpl_size xpos, cpl_size ypos, int *pis_rejected)
get pixel values of hdrl_image
cpl_error_code hdrl_image_sub_image(hdrl_image *self, const hdrl_image *other)
Subtract two images, store the result in the first image.
cpl_error_code hdrl_image_reject_from_mask(hdrl_image *self, const cpl_mask *map)
set bpm of hdrl_image
hdrl_value hdrl_image_get_median(const hdrl_image *self)
computes the median and associated error of an image.
cpl_error_code hdrl_image_div_image(hdrl_image *self, const hdrl_image *other)
Divide two images, store the result in the first image.
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
double hdrl_image_get_stdev(const hdrl_image *self)
computes the standard deviation of the data of an image
cpl_error_code hdrl_image_copy(hdrl_image *dst, const hdrl_image *src, cpl_size xpos, cpl_size ypos)
Copy one image into another.
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
hdrl_image * hdrl_image_extract(const hdrl_image *self, cpl_size llx, cpl_size lly, cpl_size urx, cpl_size ury)
extract copy of window from image
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
hdrl_value hdrl_image_get_mean(const hdrl_image *self)
computes mean pixel value and associated error of an image.
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
cpl_error_code hdrl_image_reject(hdrl_image *self, cpl_size xpos, cpl_size ypos)
mark pixel as bad
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
cpl_error_code hdrl_imagelist_collapse_mean(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Mean collapsing of image list.
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_y(const hdrl_imagelist *himlist)
Get number of rows of images in the imagelist.
hdrl_imagelist * hdrl_imagelist_create(cpl_imagelist *imlist, cpl_imagelist *errlist)
Create an hdrl_imagelist out of 2 cpl_imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
const hdrl_image * hdrl_imagelist_get_const(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
cpl_error_code hdrl_imagelist_collapse(const hdrl_imagelist *himlist, const hdrl_parameter *param, hdrl_image **out, cpl_image **contrib)
collapsing of image list
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
cpl_error_code hdrl_imagelist_collapse_median(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Median collapsing of image list.
hdrl_imagelist * hdrl_imagelist_duplicate(const hdrl_imagelist *himlist)
Duplicate an image list.
cpl_size hdrl_imagelist_get_size_x(const hdrl_imagelist *himlist)
Get number of colums of images in the imagelist.
cpl_error_code hdrl_imagelist_collapse_weighted_mean(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Weighted Mean collapsing of image list.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
cpl_error_code hdrl_maglim_compute(const cpl_image *image, const double zeropoint, const double fwhm, const cpl_size kernel_size_x, const cpl_size kernel_size_y, const hdrl_image_extend_method image_extend_method, const hdrl_parameter *mode_parameter, double *limiting_magnitude)
Computes the limiting magnitude of an image.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter
double fraction(double x, double y, double r_out)
Fraction of pixel bounded.