36 #include "visir_recipe.h"
37 #include "visir_spectro.h"
38 #include "visir_spc_distortion.h"
39 #include "visir_spc_photom.h"
46 #define RECIPE_STRING "visir_old_spc_obs_ech"
49 #define RECIPE_KEYS_REGEXP_ALL \
50 VISIR_PFITS_REGEXP_IMG_RECOMBINE \
51 "|" VISIR_PFITS_REGEXP_SPC_GET_RES_WL
54 #define RECIPE_KEYS_REGEXP \
55 RECIPE_KEYS_REGEXP_ALL \
56 "|" VISIR_PFITS_REGEXP_CAPA \
57 "|" VISIR_PFITS_REGEXP_SPC_WCAL_PAF
60 #define RECIPE_KEYS_REGEXP_WCS \
62 "|" IRPLIB_PFITS_WCS_REGEXP
65 #define PREPROCESSED_INPUT 1
70 static cpl_error_code visir_spc_ech_save(cpl_frameset *,
71 const cpl_parameterlist *,
72 const cpl_propertylist *,
73 const cpl_propertylist *,
81 VISIR_RECIPE_DEFINE(visir_old_spc_obs_ech,
82 VISIR_PARAM_ZERODIST | VISIR_PARAM_ORDEROFF |
83 VISIR_PARAM_EMIS_TOL |
84 VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
85 VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
86 VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
87 VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
88 VISIR_PARAM_UNION | VISIR_PARAM_REJECT |
89 VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
90 VISIR_PARAM_PLOT | VISIR_PARAM_SLITSKEW |
91 VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
92 VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
93 VISIR_PARAM_HORIARC | VISIR_PARAM_FIXCOMBI |
94 VISIR_PARAM_BKG_CORRECT,
95 "Old DRS detector: Spectroscopic Observation recipe in Echelle mode",
96 "This recipe estimates the dispersion relation using the "
97 "atmospheric spectrum\n"
98 "in a grism spectroscopy half-cycle frame.\n"
99 "It also extracts the spectrum of an observed object using "
100 "a combined frame.\n"
101 "The files listed in the Set Of Frames (sof-file) "
103 "VISIR-Echelle-Spectroscopy-file.fits "
104 VISIR_SPC_OBS_ECH_RAW
"\n"
105 "VISIR-Quantum-Efficiency-Calibration-file.fits "
106 VISIR_CALIB_QEFF_SPC
"\n"
107 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
108 VISIR_CALIB_LINES_SPC
110 MAN_VISIR_CALIB_BPM_SPC);
134 static int visir_old_spc_obs_ech(cpl_frameset * framelist,
135 const cpl_parameterlist * parlist)
137 irplib_framelist * allframes = NULL;
138 irplib_framelist * rawframes = NULL;
139 irplib_framelist * skyframes = NULL;
140 cpl_propertylist * qclist = cpl_propertylist_new();
141 cpl_propertylist * paflist = cpl_propertylist_new();
143 const char * star_cat;
144 const char * spc_cal_qeff;
145 const char * spc_cal_lines;
147 cpl_image * combined = NULL;
148 cpl_image * flipped = NULL;
149 cpl_image * comnarrow = NULL;
150 cpl_imagelist * hcycle = NULL;
151 cpl_table * spc_table = NULL;
152 cpl_image * weight2d = NULL;
153 cpl_image * imhcycle = NULL;
154 cpl_image * imhcyclenarrow = NULL;
155 cpl_image ** combinedpair;
156 double wlen, slitw, temp, fwhm;
157 visir_spc_resol resol;
158 cpl_boolean drop_wcs;
160 cpl_boolean bkgcorrect;
161 cpl_errorstate cleanstate = cpl_errorstate_get();
163 const char * dit_key = VISIR_PFITS_DOUBLE_DIT;
164 cpl_boolean has_phot = CPL_FALSE;
166 spc_config.recipename = RECIPE_STRING;
167 spc_config.parlist = parlist;
168 spc_config.orderoffset = 0;
169 spc_config.phot_emis_tol = 1.0;
184 spc_config.orderoffset =
188 VISIR_PARAM_FIXCOMBI);
189 spc_config.phot_emis_tol =
193 VISIR_PARAM_BKG_CORRECT);
201 skip_if(allframes == NULL);
203 if (rawframes == NULL) {
204 cpl_errorstate_set(cleanstate);
206 skip_if (rawframes == NULL);
208 skip_if (skyframes == NULL);
209 input_mode = PREPROCESSED_INPUT;
210 cpl_msg_info(cpl_func,
"Input: preprocessed data");
212 cpl_msg_info(cpl_func,
"Input: raw data");
213 input_mode = RAW_INPUT;
215 skip_if (rawframes == NULL);
220 VISIR_PFITS_REGEXP_SPC_SENSIT
221 "|" RECIPE_KEYS_REGEXP_ALL
222 "|" RECIPE_KEYS_REGEXP_WCS
223 "|" VISIR_PFITS_REGEXP_DIT
226 "|" "ESO QC BACKGD MEAN"
232 VISIR_PFITS_DOUBLE_SEQ1_DIT))
233 dit_key = VISIR_PFITS_DOUBLE_SEQ1_DIT;
236 visir_data_type data_type;
239 skip_if(visir_get_data_type(frm, plist, &data_type, NULL));
257 resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm,
258 visir_data_is_aqu(data_type));
262 if (resol != VISIR_SPC_R_GHR) {
263 if (spc_config.orderoffset == 0) {
264 cpl_msg_warning(cpl_func,
"Reducing non-HR Grism data as main order");
266 cpl_msg_error(cpl_func,
"This recipe cannot reduce non-HR Grism "
267 "data with an order-offset of %d",
268 spc_config.orderoffset);
269 visir_error_set(CPL_ERROR_TYPE_MISMATCH);
274 if (input_mode == PREPROCESSED_INPUT) {
275 cpl_imagelist * imagelist = cpl_imagelist_new();
277 const cpl_propertylist ** plists =
278 cpl_malloc(nframes *
sizeof(cpl_propertylist *));
281 for (cpl_size i = 0; i < nframes; i++) {
283 const char * fname = cpl_frame_get_filename(frame);
284 const cpl_size next = cpl_fits_count_extensions(fname);
285 cpl_imagelist * tmplist = cpl_imagelist_new();
286 cpl_errorstate prestate = cpl_errorstate_get();
290 if (cpl_propertylist_has(plists[i],
"ESO QC BACKGD MEAN"))
291 bg_sum += cpl_propertylist_get_double(plists[i],
292 "ESO QC BACKGD MEAN");
295 for (cpl_size j = 0; j < 1 + next; j++) {
297 cpl_image_load(fname, CPL_TYPE_UNSPECIFIED, 0, j);
299 cpl_errorstate_set(prestate);
300 cpl_msg_info(cpl_func,
"No image in extension %d", (
int)j);
303 cpl_imagelist_set(tmplist, img, cpl_imagelist_get_size(tmplist));
306 tmp = cpl_imagelist_collapse_create(tmplist);
308 skip_if (visir_spc_det_fix(&tmp, 1, CPL_TRUE,
315 cpl_imagelist_set(imagelist, tmp,
316 cpl_imagelist_get_size(imagelist));
317 cpl_imagelist_delete(tmplist);
321 plists, CPL_GEOM_FIRST, &drop_wcs);
324 cpl_propertylist_append_double(qclist,
"ESO QC BACKGD MEAN",
326 cpl_imagelist_delete(imagelist);
333 const char * fname = cpl_frame_get_filename(frame);
334 imhcycle = cpl_image_load(fname, CPL_TYPE_UNSPECIFIED, 0, 0);
335 skip_if(imhcycle == NULL);
338 skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_TRUE,
346 }
else if (input_mode == RAW_INPUT) {
348 flat, CPL_GEOM_FIRST, &drop_wcs,
349 !spc_config.do_fixcombi, wlen, resol);
355 imhcycle = cpl_imagelist_unset(hcycle, 0);
357 skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
365 skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
370 if (combinedpair == NULL) {
371 cpl_msg_error(cpl_func,
"Could not combine the input frames");
375 cpl_image_delete(combinedpair[1]);
376 combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
377 cpl_image_delete(combinedpair[0]);
378 cpl_free(combinedpair);
382 flipped = visir_spc_flip(combined, wlen, resol, VISIR_DATA_CUBE2);
384 cpl_image_delete(combined);
390 flipped = visir_spc_flip(imhcycle, wlen, resol, VISIR_DATA_CUBE2);
392 cpl_image_delete(imhcycle);
398 skip_if (spc_config.do_fixcombi && input_mode == RAW_INPUT &&
399 visir_spc_det_fix(&combined, 1, CPL_TRUE,
407 skip_if(visir_spc_extract_order(&imhcyclenarrow, &comnarrow,
408 combined, imhcycle, wlen,
409 &spc_config, CPL_TRUE,
410 visir_data_is_aqu(data_type)));
412 skip_if (visir_spc_extract_wcal(comnarrow, imhcyclenarrow,
413 wlen, slitw, temp, fwhm,
414 resol, spc_config.orderoffset,
415 spc_cal_lines, spc_cal_qeff,
416 &spc_table, &weight2d, qclist,
417 spc_config.plot, bkgcorrect,
418 visir_data_is_aqu(data_type)));
421 bug_if (visir_spectro_qc(qclist, paflist, drop_wcs, rawframes, NULL,
422 "^(" VISIR_PFITS_REGEXP_SPC_WCAL_PAF
")$"));
425 cpl_errorstate prestate = cpl_errorstate_get();
426 const cpl_propertylist * plist =
430 &weight2d, qclist, spc_table, dit_key)) {
431 irplib_error_recover(prestate,
"Could not perform photometric "
438 skip_if (cpl_table_erase_column(spc_table,
"SPC_EMISSIVITY"));
444 cpl_msg_info(cpl_func,
"Saving the produced spectrum");
447 bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
448 VISIR_SPC_OBS_ECH_COMBINED_PROCATG));
451 skip_if (visir_spc_ech_save(framelist, parlist, qclist, paflist,
452 combined, weight2d, spc_table,
453 VISIR_SPC_PHOT_ECH_TAB_PROCATG,
454 VISIR_SPC_PHOT_ECH_COMBINED_PROCATG,
455 VISIR_SPC_PHOT_ECH_WEIGHT_PROCATG));
457 skip_if (visir_spc_ech_save(framelist, parlist, qclist, paflist,
458 combined, weight2d, spc_table,
459 VISIR_SPC_OBS_ECH_TAB_PROCATG,
460 VISIR_SPC_OBS_ECH_COMBINED_PROCATG,
461 VISIR_SPC_OBS_ECH_WEIGHT_PROCATG));
465 cpl_propertylist_delete(qclist);
466 cpl_propertylist_delete(paflist);
470 cpl_image_delete(flipped);
471 cpl_image_delete(combined);
472 cpl_image_delete(comnarrow);
473 cpl_image_delete(imhcyclenarrow);
474 cpl_table_delete(spc_table);
475 cpl_image_delete(weight2d);
476 cpl_image_delete(imhcycle);
477 cpl_imagelist_delete(hcycle);
479 return cpl_error_get_code();
499 static cpl_error_code visir_spc_ech_save(cpl_frameset * set,
500 const cpl_parameterlist * parlist,
501 const cpl_propertylist * qclist,
502 const cpl_propertylist * paflist,
503 const cpl_image * combined,
504 const cpl_image * weight2d,
505 const cpl_table * table,
506 const char * tab_procatg,
507 const char * com_procatg,
508 const char * wgt_procatg)
516 qclist, NULL, visir_pipe_id,
517 RECIPE_STRING
"_tab" CPL_DFS_FITS));
521 RECIPE_STRING, com_procatg,
522 qclist, NULL, visir_pipe_id,
523 RECIPE_STRING CPL_DFS_FITS));
527 RECIPE_STRING, wgt_procatg,
528 qclist, NULL, visir_pipe_id,
529 RECIPE_STRING
"_weight" CPL_DFS_FITS));
531 #ifdef VISIR_SAVE_PAF
534 skip_if (cpl_dfs_save_paf(
"VISIR", RECIPE_STRING, paflist,
535 RECIPE_STRING CPL_DFS_PAF));
537 bug_if(paflist == NULL);
542 return cpl_error_get_code();
double visir_parameterlist_get_double(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR parameter of type double.
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
cpl_error_code visir_spc_phot_sensit(const irplib_framelist *rawframes, const visir_spc_config *pconfig, const cpl_propertylist *plist, const char *star_cat, cpl_image **pweight2d, cpl_propertylist *qclist, cpl_table *spc_table, const char *dit_key)
Compute the sensitivity from an extracted spectrum.
int visir_parameterlist_get_int(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR integer parameter.
cpl_boolean visir_parameterlist_get_bool(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR boolean parameter.
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
cpl_frame * irplib_framelist_get(irplib_framelist *self, int pos)
Get the specified frame from the framelist.
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
cpl_propertylist * irplib_framelist_get_propertylist(irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.