36 #include "crires_recipe.h"
38 #include "crires_combine.h"
39 #include "crires_wlcalib.h"
40 #include "crires_extract.h"
41 #include "crires_photom.h"
47 #define RECIPE_STRING "crires_util_combine"
53 static int crires_util_combine_save(
const cpl_imagelist **,
54 const cpl_parameterlist *, cpl_frameset *) ;
56 static char crires_util_combine_description[] =
57 "crires_util_combine -- Images Combination tool\n"
58 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
59 "raw-file.fits "CRIRES_SPEC_JITTER_RAW
" or\n"
60 "raw-file.fits "CRIRES_SPEC_JITTER_J_RAW
" or\n"
61 "raw-file.fits "CRIRES_SPEC_JITTER_STD_RAW
" or\n"
62 "raw-file.fits "CRIRES_SPEC_JITTER_J_STD_RAW
" or\n"
63 "raw-file.fits "CRIRES_SPEC_NODDING_OBJECT_RAW
" or\n"
64 "raw-file.fits "CRIRES_SPEC_NODDING_SKY_RAW
" or\n"
65 "raw-file.fits "CRIRES_SPEC_GENERIC_OBJECT_RAW
" or\n"
66 "raw-file.fits "CRIRES_SPEC_GENERIC_SKY_RAW
" or\n"
67 "raw-file.fits "CRIRES_SPEC_NODDING_RAW
" or\n"
68 "raw-file.fits "CRIRES_SPEC_NODDING_J_RAW
" or\n"
69 "raw-file.fits "CRIRES_SPEC_NODDING_STD_RAW
" or\n"
70 "raw-file.fits "CRIRES_SPEC_NODDING_J_STD_RAW
" or\n"
71 "raw-file.fits "CRIRES_WIN_NODDING_OBJECT_RAW
" or\n"
72 "raw-file.fits "CRIRES_WIN_NODDING_SKY_RAW
" or\n"
73 "raw-file.fits "CRIRES_WIN_NODDING_RAW
" or\n"
74 "raw-file.fits "CRIRES_WIN_NODDING_J_RAW
" or\n"
75 "raw-file.fits "CRIRES_WIN_NODDING_STD_RAW
" or\n"
76 "raw-file.fits "CRIRES_WIN_NODDING_J_STD_RAW
" or\n"
77 "flat-file.fits "CRIRES_CALPRO_FLAT
" or\n"
78 "flat-file.fits "CRIRES_CALPRO_FLAT_WIN
" or\n"
79 "bpm-file.fits "CRIRES_CALPRO_BPM
" or\n"
80 "bpm-file.fits "CRIRES_CALPRO_BPM_WIN
" or\n"
81 "dark-file.fits "CRIRES_CALPRO_DARK
" or\n"
82 "dark-file.fits "CRIRES_CALPRO_DARK_WIN
" or\n"
83 "detlin-file.fits "CRIRES_CALPRO_COEFFS_CUBE
" or\n"
85 "In the case of a nodding observation, in order not to degrade the \n"
86 " instrument high resolution, the combined images using only NODA\n"
87 " or NODB nodding positions can be produced on request. (see --onlyA/B)\n"
89 "This recipe produces 2 files:\n"
90 "First product: the combined image\n"
91 " (PRO TYPE = "CRIRES_PROTYPE_COMBINED
")\n"
92 "Second product: the contribution map\n"
93 " (PRO TYPE = "CRIRES_PROTYPE_CONTRIB
")\n" ;
95 CRIRES_RECIPE_DEFINE(crires_util_combine,
100 "Images Combination tool",
101 crires_util_combine_description) ;
116 crires_illum_period period ;
117 } crires_util_combine_config ;
131 static int crires_util_combine(
132 cpl_frameset * frameset,
133 const cpl_parameterlist * parlist)
135 cpl_frameset * rawframes ;
136 cpl_frameset * skyframes ;
141 const char * flat_win ;
142 const char * dark_win ;
143 const char * bpm_win ;
144 const char * detlin ;
145 cpl_imagelist ** comblist ;
146 cpl_propertylist * plist ;
154 crires_util_combine_config.refine = crires_parameterlist_get_bool(
155 parlist, RECIPE_STRING, CRIRES_PARAM_REFINE) ;
156 crires_util_combine_config.onlyA = crires_parameterlist_get_bool(
157 parlist, RECIPE_STRING, CRIRES_PARAM_ONLYA) ;
158 crires_util_combine_config.onlyB = crires_parameterlist_get_bool(
159 parlist, RECIPE_STRING, CRIRES_PARAM_ONLYB) ;
160 crires_util_combine_config.blind = crires_parameterlist_get_bool(
161 parlist, RECIPE_STRING, CRIRES_PARAM_BLIND) ;
164 if (crires_dfs_set_groups(frameset, NULL)) {
165 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
170 flat = crires_extract_filename(frameset, CRIRES_CALPRO_FLAT) ;
171 dark = crires_extract_filename(frameset, CRIRES_CALPRO_DARK) ;
172 bpm = crires_extract_filename(frameset, CRIRES_CALPRO_BPM) ;
173 flat_win = crires_extract_filename(frameset, CRIRES_CALPRO_FLAT_WIN) ;
174 dark_win = crires_extract_filename(frameset, CRIRES_CALPRO_DARK_WIN) ;
175 bpm_win = crires_extract_filename(frameset, CRIRES_CALPRO_BPM_WIN) ;
176 detlin = crires_extract_filename(frameset, CRIRES_CALPRO_COEFFS_CUBE) ;
179 if ((rawframes = crires_extract_frameset(frameset,
180 CRIRES_SPEC_JITTER_RAW)) != NULL) {
181 crires_util_combine_config.nodding = 0 ;
182 }
else if ((rawframes = crires_extract_frameset(frameset,
183 CRIRES_SPEC_JITTER_J_RAW)) != NULL) {
184 crires_util_combine_config.nodding = 0 ;
185 }
else if ((rawframes = crires_extract_frameset(frameset,
186 CRIRES_SPEC_NODDING_RAW)) != NULL) {
187 crires_util_combine_config.nodding = 1 ;
188 }
else if ((rawframes = crires_extract_frameset(frameset,
189 CRIRES_SPEC_NODDING_OBJECT_RAW)) != NULL) {
190 crires_util_combine_config.nodding = 1 ;
191 }
else if ((rawframes = crires_extract_frameset(frameset,
192 CRIRES_SPEC_NODDING_J_RAW)) != NULL) {
193 crires_util_combine_config.nodding = 1 ;
194 }
else if ((rawframes = crires_extract_frameset(frameset,
195 CRIRES_SPEC_GENERIC_OBJECT_RAW)) != NULL) {
196 crires_util_combine_config.nodding = 0 ;
197 }
else if ((rawframes = crires_extract_frameset(frameset,
198 CRIRES_SPEC_JITTER_STD_RAW)) != NULL) {
199 crires_util_combine_config.nodding = 0 ;
200 }
else if ((rawframes = crires_extract_frameset(frameset,
201 CRIRES_SPEC_JITTER_J_STD_RAW)) != NULL) {
202 crires_util_combine_config.nodding = 0 ;
203 }
else if ((rawframes = crires_extract_frameset(frameset,
204 CRIRES_SPEC_NODDING_STD_RAW)) != NULL) {
205 crires_util_combine_config.nodding = 1 ;
206 }
else if ((rawframes = crires_extract_frameset(frameset,
207 CRIRES_SPEC_NODDING_J_STD_RAW)) != NULL) {
208 crires_util_combine_config.nodding = 1 ;
209 }
else if ((rawframes = crires_extract_frameset(frameset,
210 CRIRES_WIN_NODDING_OBJECT_RAW)) != NULL) {
211 crires_util_combine_config.nodding = 1 ;
212 }
else if ((rawframes = crires_extract_frameset(frameset,
213 CRIRES_WIN_NODDING_RAW)) != NULL) {
214 crires_util_combine_config.nodding = 1 ;
215 }
else if ((rawframes = crires_extract_frameset(frameset,
216 CRIRES_WIN_NODDING_J_RAW)) != NULL) {
217 crires_util_combine_config.nodding = 1 ;
218 }
else if ((rawframes = crires_extract_frameset(frameset,
219 CRIRES_WIN_NODDING_STD_RAW)) != NULL) {
220 crires_util_combine_config.nodding = 1 ;
221 }
else if ((rawframes = crires_extract_frameset(frameset,
222 CRIRES_WIN_NODDING_J_STD_RAW)) != NULL) {
223 crires_util_combine_config.nodding = 1 ;
225 cpl_msg_error(__func__,
"No raw frame in input") ;
226 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
231 if (crires_util_combine_config.nodding == 0) {
232 if (crires_util_combine_config.onlyA) {
233 cpl_msg_warning(__func__,
"onlyA only possible in nodding mode") ;
234 crires_util_combine_config.onlyA = 0 ;
236 if (crires_util_combine_config.onlyB) {
237 cpl_msg_warning(__func__,
"onlyB only possible in nodding mode") ;
238 crires_util_combine_config.onlyB = 0 ;
243 skyframes = crires_extract_frameset(frameset, CRIRES_SPEC_NODDING_SKY_RAW) ;
244 if (skyframes == NULL) {
245 skyframes = crires_extract_frameset(frameset,
246 CRIRES_WIN_NODDING_SKY_RAW) ;
248 if (skyframes == NULL) {
249 skyframes = crires_extract_frameset(frameset,
250 CRIRES_SPEC_GENERIC_SKY_RAW) ;
254 crires_util_combine_config.period =
255 crires_get_detector_illum_period(cpl_frame_get_filename(
256 cpl_frameset_get_position(rawframes, 0))) ;
257 if (crires_util_combine_config.period == CRIRES_ILLUM_UNKNOWN) {
258 cpl_msg_error(__func__,
259 "Cannot determine the detector illumination period") ;
260 cpl_frameset_delete(rawframes) ;
261 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
266 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(
267 cpl_frameset_get_position(rawframes, 0)), 0)) == NULL)
269 sval = crires_pfits_get_ncorrs(plist) ;
270 if (!strcmp(sval,
"FowlerNsampGRstWin")) {
271 crires_util_combine_config.period = CRIRES_ILLUM_FULL_DETECTOR ;
272 crires_util_combine_config.win_mode = 1 ;
274 crires_util_combine_config.win_mode = 0 ;
276 cpl_propertylist_delete(plist) ;
279 crires_display_detector_illum(crires_util_combine_config.period) ;
282 if (crires_util_combine_config.win_mode == 1) {
283 fname = cpl_frame_get_filename(cpl_frameset_get_position(rawframes,0)) ;
284 if (flat_win != NULL) {
285 if (crire_stripe_keys_mismatch(fname, flat_win)) {
286 cpl_msg_error(__func__,
287 "Mismatch of STRIPE keys with the flat frame") ;
288 cpl_frameset_delete(rawframes) ;
289 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
290 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
294 if (bpm_win != NULL) {
295 if (crire_stripe_keys_mismatch(fname, bpm_win)) {
296 cpl_msg_error(__func__,
297 "Mismatch of STRIPE keys with the bpm frame") ;
298 cpl_frameset_delete(rawframes) ;
299 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
300 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
304 if (dark_win != NULL) {
305 if (crire_stripe_keys_mismatch(fname, dark_win)) {
306 cpl_msg_error(__func__,
307 "Mismatch of STRIPE keys with the dark frame") ;
308 cpl_frameset_delete(rawframes) ;
309 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
310 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
317 cpl_msg_info(__func__,
"Images combination") ;
318 cpl_msg_indent_more() ;
319 if (crires_util_combine_config.win_mode == 0) {
320 comblist = crires_combine_imagelist(rawframes, skyframes,
321 crires_util_combine_config.period,
322 flat, dark, bpm, detlin,
323 crires_util_combine_config.nodding,
324 crires_util_combine_config.blind,
325 crires_util_combine_config.refine,
326 crires_util_combine_config.onlyA,
327 crires_util_combine_config.onlyB) ;
329 comblist = crires_combine_imagelist_win(rawframes,
330 flat_win, dark_win, bpm_win, detlin,
331 crires_util_combine_config.refine,
332 crires_util_combine_config.onlyA,
333 crires_util_combine_config.onlyB) ;
335 if (comblist == NULL) {
336 cpl_msg_error(__func__,
"Cannot combine the images") ;
337 cpl_frameset_delete(rawframes) ;
338 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
339 cpl_msg_indent_less() ;
342 cpl_frameset_delete(rawframes) ;
343 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
344 cpl_msg_indent_less() ;
347 cpl_msg_info(__func__,
"Save the product") ;
348 cpl_msg_indent_more() ;
349 if (crires_util_combine_save((
const cpl_imagelist **)comblist, parlist,
351 cpl_msg_error(__func__,
"Cannot save the product") ;
352 cpl_imagelist_delete(comblist[0]) ;
353 cpl_imagelist_delete(comblist[1]) ;
354 if (crires_util_combine_config.onlyA) {
355 cpl_imagelist_delete(comblist[2]) ;
356 cpl_imagelist_delete(comblist[3]) ;
358 if (crires_util_combine_config.onlyB) {
359 cpl_imagelist_delete(comblist[4]) ;
360 cpl_imagelist_delete(comblist[5]) ;
363 cpl_msg_indent_less() ;
366 cpl_imagelist_delete(comblist[0]) ;
367 cpl_imagelist_delete(comblist[1]) ;
368 if (crires_util_combine_config.onlyA) {
369 cpl_imagelist_delete(comblist[2]) ;
370 cpl_imagelist_delete(comblist[3]) ;
372 if (crires_util_combine_config.onlyB) {
373 cpl_imagelist_delete(comblist[4]) ;
374 cpl_imagelist_delete(comblist[5]) ;
377 cpl_msg_indent_less() ;
380 if (cpl_error_get_code())
return -1 ;
394 static int crires_util_combine_save(
395 const cpl_imagelist ** images,
396 const cpl_parameterlist * parlist,
399 const char * recipe_name =
"crires_util_combine" ;
402 crires_image_save(set,
407 CRIRES_OBS_COMBINED_IMA,
408 CRIRES_PROTYPE_COMBINED,
409 crires_util_combine_config.period,
412 PACKAGE
"/" PACKAGE_VERSION,
413 "crires_util_combine_comb.fits") ;
416 crires_image_save(set,
421 CRIRES_OBS_CONTRIBUTION_IMA,
422 CRIRES_PROTYPE_CONTRIB,
423 crires_util_combine_config.period,
426 PACKAGE
"/" PACKAGE_VERSION,
427 "crires_util_combine_contrib.fits") ;
430 if (crires_util_combine_config.onlyA) {
432 crires_image_save(set,
437 CRIRES_OBS_COMBINED_NA_IMA,
438 CRIRES_PROTYPE_COMBINED,
439 crires_util_combine_config.period,
442 PACKAGE
"/" PACKAGE_VERSION,
443 "crires_util_combine_comb_noddedA.fits") ;
446 crires_image_save(set,
451 CRIRES_OBS_CONTRIBUTION_NA_IMA,
452 CRIRES_PROTYPE_CONTRIB,
453 crires_util_combine_config.period,
456 PACKAGE
"/" PACKAGE_VERSION,
457 "crires_util_combine_contrib_noddedA.fits") ;
461 if (crires_util_combine_config.onlyB) {
463 crires_image_save(set,
468 CRIRES_OBS_COMBINED_NB_IMA,
469 CRIRES_PROTYPE_COMBINED,
470 crires_util_combine_config.period,
473 PACKAGE
"/" PACKAGE_VERSION,
474 "crires_util_combine_comb_noddedB.fits") ;
477 crires_image_save(set,
482 CRIRES_OBS_CONTRIBUTION_NB_IMA,
483 CRIRES_PROTYPE_CONTRIB,
484 crires_util_combine_config.period,
487 PACKAGE
"/" PACKAGE_VERSION,
488 "crires_util_combine_contrib_noddedB.fits") ;