39#include "casu_utils.h"
42#include "eris_utils.h"
43#include "eris_nix_utils.h"
44#include "eris_pfits.h"
46#include "eris_nix_dfs.h"
47#include "eris_nix_master_bpm.h"
48#include "eris_nix_master_dark.h"
49#include "eris_nix_gain_linearity.h"
58static const char eris_nix_spec_cal_and_stack_description[] =
59"This recipe calibrates a set of OBJECT_SPEC_JITTER frames to \n"
61"- OBJECT_SPEC_JITTER_CALIB frames.\n"
64"- a series of tagged OBJECT_SPEC_JITTER frames.\n"
65"- a tagged DETMON coefficients cube file, e.g.\n"
66"-- detmon_ir_lg_coeffs_cube.fits "ERIS_NIX_COEFFS_CUBE_PRO_CATG
"\n"
67"- a tagged MASTER_DARK for matching detector configuration.\n"
68"- a tagged MASTER_FLAT_HIFREQ for matching detector configuration.\n"
69"The output will be fits files containing:\n"
70"- OBJECT_SPEC_JITTER_CALIBRATED frames.\n"
74#define RECIPE_NAME "eris.eris_nix_spec_cal_and_stack"
80cpl_recipe_define(eris_nix_spec_cal_and_stack, ERIS_BINARY_VERSION,
82 PACKAGE_BUGREPORT,
"2017",
83 "Calibrate OBJECT_SPEC_JITTER frames",
84 eris_nix_spec_cal_and_stack_description);
100static cpl_error_code eris_nix_spec_cal_and_stack_fill_parameterlist(
101 cpl_parameterlist * self) {
103 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
105 cpl_parameter * p = NULL;
107 p = cpl_parameter_new_value(RECIPE_NAME
".debug_data",
109 "write additional data for debugging",
110 RECIPE_NAME, CPL_TRUE);
111 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"debug_data");
112 cpl_parameterlist_append(self, p);
114 p = cpl_parameter_new_value(RECIPE_NAME
".zero_rejected",
116 "set rejected pixels to 0",
117 RECIPE_NAME, CPL_TRUE);
118 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"zero_rejected");
119 cpl_parameterlist_append(self, p);
121 p = cpl_parameter_new_value(RECIPE_NAME
".premask_objects",
123 "create object masks before background estimation",
124 RECIPE_NAME, CPL_TRUE);
125 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"premask_objects");
126 cpl_parameterlist_append(self, p);
128 p = cpl_parameter_new_value(RECIPE_NAME
".premask_nsigma",
130 "sigma cutoff for premask object detection",
132 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"premask_nsigma");
133 cpl_parameterlist_append(self, p);
149static int eris_nix_spec_cal_and_stack(cpl_frameset * frameset,
150 const cpl_parameterlist * parlist) {
152 cpl_table * astrom_ref_cat = NULL;
153 located_imagelist * cal_sky_jitters1 = NULL;
154 located_imagelist * cal_sky_jitters2 = NULL;
155 located_imagelist * cal_jitters1 = NULL;
156 located_imagelist * cal_jitters2 = NULL;
158 hdrl_parameter * cat_params = NULL;
159 const gain_linearity * gain_lin = NULL;
160 master_bpm * master_bpm = NULL;
161 master_dark * master_dark = NULL;
162 master_flat * master_flat_hifreq = NULL;
163 master_flat * master_flat_lofreq = NULL;
164 mef_extension_list * mefs = NULL;
165 hdrl_image * object_photom = NULL;
166 const cpl_parameter * p = NULL;
167 cpl_table * photom_ref_cat = NULL;
168 cpl_vector * profile = NULL;
169 located_imagelist * raw_sky_jitters = NULL;
170 located_imagelist * raw_jitters = NULL;
171 located_image * sky_stack = NULL;
172 located_image * sky_stack2 = NULL;
173 cpl_polynomial * startrace_poly = NULL;
174 located_image * target_stack = NULL;
175 located_image * target_stack2 = NULL;
176 cpl_frameset * used_frameset = NULL;
177 cpl_polynomial * wave_poly = NULL;
179 enu_check_error_code(
"%s():%d: An error is already set: %s",
180 cpl_func, __LINE__, cpl_error_get_where());
184 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
185 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
189 cpl_msg_set_level_from_env();
190 cpl_msg_severity severity = cpl_msg_get_level();
191 cpl_msg_info(cpl_func,
"level %d", (
int) severity);
195 p = cpl_parameterlist_find_const(parlist, RECIPE_NAME
".debug_data");
196 int debug_data = cpl_parameter_get_bool(p);
197 cpl_msg_info(cpl_func,
"debug_data %d", debug_data);
199 p = cpl_parameterlist_find_const(parlist, RECIPE_NAME
".zero_rejected");
200 int zero_rejected = cpl_parameter_get_bool(p);
202 p = cpl_parameterlist_find_const(parlist, RECIPE_NAME
".premask_objects");
203 int premask_objects = cpl_parameter_get_bool(p);
205 p = cpl_parameterlist_find_const(parlist, RECIPE_NAME
".premask_nsigma");
206 double premask_nsigma = cpl_parameter_get_double(p);
208 cpl_size x_probe = 1024;
209 cpl_size y_probe = 1024;
211 enu_check_error_code(
"Could not retrieve input parameters");
216 enu_check_error_code(
"Could not identify RAW and CALIB frames");
217 used_frameset = cpl_frameset_new();
221 gain_lin = engl_gain_linearity_load_from_frameset(frameset,
222 ERIS_NIX_GAIN_PRO_CATG, ERIS_NIX_COEFFS_CUBE_PRO_CATG,
223 ERIS_NIX_NL_BPM_PRO_CATG, 0, used_frameset);
224 enu_check_error_code(
"failed to read gain/linearity information from SoF");
229 ERIS_NIX_MASTER_DARK_IMG_PRO_CATG, used_frameset);
230 enu_check_error_code(
"failed to read master dark from SoF");
234 master_bpm = en_master_bpm_load_from_frameset(frameset,
235 ERIS_NIX_MASTER_BPM_PRO_CATG,
237 enu_check_error_code(
"failed to read master BPM from SoF");
241 master_flat_hifreq = en_master_flat_load_from_frameset(
242 frameset, ERIS_NIX_MASTER_FLAT_HIFREQ_PRO_CATG,
243 used_frameset, CPL_FALSE);
248 wave_poly = cpl_polynomial_new(2);
249 cpl_size pows[] = {0, 1};
250 cpl_polynomial_set_coeff(wave_poly, pows, 1.0);
251 cpl_polynomial_dump(wave_poly, stdout);
253 startrace_poly = cpl_polynomial_new(2);
256 cpl_polynomial_set_coeff(startrace_poly, pows, 1.0);
257 cpl_polynomial_dump(startrace_poly, stdout);
262 ERIS_NIX_RAW_OBJECT_LSS_JITTER_DO_CATG, used_frameset);
263 cpl_msg_info(cpl_func,
"read %d OBJECT_LSS_JITTER frames",
264 (
int) raw_jitters->size);
269 int flag_mask = ~(int)0;
271 NULL, NULL, flag_mask, zero_rejected, x_probe,
273 enu_check_error_code(
"error performing basic calibration of frames");
275 RECIPE_NAME, frameset, parlist, used_frameset);
284 cpl_msg_info(cpl_func,
"Estimating sky background:");
286 if (premask_objects) {
287 enu_opm_spectrum_limlist(cal_jitters1, premask_nsigma);
290 -1, -1, cal_jitters1);
291 enu_check_error_code(
"error in sky background first iteration: sky-subtract");
292 cpl_msg_info(cpl_func,
"here0 %s", cpl_error_get_message());
297 RECIPE_NAME, frameset, parlist, used_frameset);
307 cpl_msg_info(cpl_func,
"here0 %s", cpl_error_get_message());
309 cpl_msg_info(cpl_func,
"here1 %s", cpl_error_get_message());
311 cpl_msg_info(cpl_func,
"note: check warping profile when have real data");
312 cpl_msg_info(cpl_func,
"here0 %s", cpl_error_get_message());
313 profile = cpl_vector_new(CPL_KERNEL_DEF_SAMPLES);
314 cpl_vector_fill_kernel_profile(profile, CPL_KERNEL_LANCZOS,
315 CPL_KERNEL_DEF_WIDTH);
316 for (cpl_size j = 0; j < cal_jitters2->size; j++) {
317 cpl_msg_info(cpl_func,
"here0 %s", cpl_error_get_message());
319 cal_jitters2->limages[j]->himage),
321 cal_jitters1->limages[j]->himage),
322 startrace_poly, wave_poly,
323 profile, CPL_KERNEL_DEF_WIDTH,
324 profile, CPL_KERNEL_DEF_WIDTH);
330 RECIPE_NAME, frameset, parlist, used_frameset);
448 cpl_table_delete(astrom_ref_cat);
454 engl_gain_linearity_delete((gain_linearity *) gain_lin);
457 en_master_flat_delete(master_flat_hifreq);
458 en_master_flat_delete(master_flat_lofreq);
461 cpl_table_delete(photom_ref_cat);
462 cpl_vector_delete(profile);
467 cpl_polynomial_delete(startrace_poly);
470 cpl_frameset_delete(used_frameset);
471 cpl_polynomial_delete(wave_poly);
473 return (
int) cpl_error_get_code();
cpl_error_code eris_nix_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
void en_master_bpm_delete(master_bpm *target)
Delete a 'master_bpm' struct.
master_dark * en_master_dark_load_from_frameset(const cpl_frameset *frameset, const char *tag, cpl_frameset *used)
Load a 'master_dark' struct from a frameset.
void en_master_dark_delete(master_dark *target)
Delete a 'master_dark' struct.
void enu_located_imagelist_delete(located_imagelist *limlist)
Delete a located_imagelist and its contents.
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_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_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_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.
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.
void enu_mef_extension_list_delete(mef_extension_list *list)
Delete a mef_extension_list and its contents.
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter