29#ifndef ESO_COVERAGE_BUILD
30#define ESO_COVERAGE_BUILD 0
33#include "visir_recipe.h"
35#include "visir_utils.h"
36#include "irplib_utils.h"
42int visir_util_repack_get_info(cpl_pluginlist *);
43int visir_util_undistort_get_info(cpl_pluginlist *);
44int visir_old_util_destripe_get_info(cpl_pluginlist *);
45int visir_old_spc_obs_get_info(cpl_pluginlist *);
51#define RECIPE_STRING "visir_spc_reduce"
59static cpl_error_code visir_spc_reduce_fill_parameterlist(cpl_parameterlist *);
60static int visir_spc_reduce(cpl_frameset *,
const cpl_parameterlist *);
62static int visir_spc_reduce_create(cpl_plugin * plugin)
64 cpl_recipe * recipe = (cpl_recipe *)plugin;
65 cpl_errorstate prestate = cpl_errorstate_get();
69 return cpl_recipedefine_create(plugin)
70 || cpl_recipedefine_create_is_ok(prestate,
71 visir_spc_reduce_fill_parameterlist(recipe->parameters))
72 ? (int)cpl_error_set_where(cpl_func) : 0;
90static int visir_spc_reduce_exec(cpl_plugin * plugin)
92 char * progname = getenv(
"_");
93 char * classpath = getenv(
"CLASSPATH");
94 cpl_msg_debug(cpl_func,
"Program name: %s", progname);
95 cpl_msg_debug(cpl_func,
"CLASSPATH: %s", classpath);
96 if ((progname && strstr(progname,
"gasgano")) ||
97 (classpath && strstr(classpath,
"gasgano.jar"))) {
98 cpl_msg_info(cpl_func,
"Running under gasgano, disabling OpenMP");
99 setenv(
"OMP_NUM_THREADS",
"0", 1);
100 return visir_tmpdir_exec(RECIPE_STRING, plugin, visir_spc_reduce);
102 else if (ESO_COVERAGE_BUILD || getenv(
"VISIR_NO_FORK") != NULL) {
104 return cpl_recipedefine_exec(plugin, visir_spc_reduce)
105 ? (int)cpl_error_set_where(cpl_func) : 0;
108 return visir_forking_exec(RECIPE_STRING, plugin, visir_spc_reduce);
112static int visir_spc_reduce_destroy(cpl_plugin * plugin)
115 return cpl_recipedefine_destroy(plugin)
116 ? (int)cpl_error_set_where(cpl_func) : 0;
119int cpl_plugin_get_info(cpl_pluginlist * list)
122 return cpl_recipedefine_init(list, CPL_VERSION_CODE,
123 VISIR_BINARY_VERSION,
125 "Spectroscopic Observation recipe",
126 "This recipe performs a wavelength calibration "
127 "followed by spectrum extraction from a combined image. "
128 "It can also compute sensitivities for standard star "
130 "It works for low and high resolution including echelle "
133 "The files listed in the Set Of Frames (sof-file) "
135 "VISIR-Long-Slit-Spectroscopy-file.fits "
136 VISIR_SPC_OBS_RAW
"\n"
137 "VISIR-Quantum-Efficiency-Calibration-file.fits "
138 VISIR_CALIB_QEFF_SPC
"\n"
139 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
140 VISIR_CALIB_LINES_SPC
"\n"
141 "VISIR-Standard-Star-Flux-Catalog.fits (optional)"
142 VISIR_CALIB_STDSTAR_SPC
"\n"
143 "VISIR-linearty-table.fits "VISIR_CALIB_LIN
" (optional)"
144 MAN_VISIR_CALIB_BPM_IMG,
146 "jtaylor@partner.eso.org",
147 cpl_get_license(PACKAGE_NAME,
"2015"),
148 visir_spc_reduce_create,
149 visir_spc_reduce_exec,
150 visir_spc_reduce_destroy)
151 ? ((void)cpl_error_set_where(cpl_func), 1) : 0;
185visir_spc_reduce_fill_parameterlist(cpl_parameterlist * self)
187 cpl_pluginlist * plugins = cpl_pluginlist_new();
188 const char * context = PACKAGE
".visir_spc_reduce";
190 cpl_recipe * repack = visir_init_recipe(
"visir_util_repack",
191 &visir_util_repack_get_info,
194 cpl_recipe * undist = visir_init_recipe(
"visir_util_undistort",
195 &visir_util_undistort_get_info,
198 cpl_recipe * destripe = visir_init_recipe(
"visir_old_util_destripe",
199 &visir_old_util_destripe_get_info,
202 cpl_recipe * spcobs = visir_init_recipe(
"visir_old_spc_obs",
203 &visir_old_spc_obs_get_info,
206 cpl_plugin_get_init(&repack->interface)(&repack->interface);
207 cpl_plugin_get_init(&undist->interface)(&undist->interface);
208 cpl_plugin_get_init(&destripe->interface)(&destripe->interface);
209 cpl_plugin_get_init(&spcobs->interface)(&spcobs->interface);
210 cpl_parameterlist * repack_par = repack->parameters;
211 cpl_parameterlist * undist_par = undist->parameters;
212 cpl_parameterlist * destripe_par = destripe->parameters;
213 cpl_parameterlist * spcobs_par = spcobs->parameters;
216 for (cpl_parameter * p = cpl_parameterlist_get_first(repack_par);
217 p != NULL; p = cpl_parameterlist_get_next(repack_par)) {
218 const char * name = cpl_parameter_get_name(p);
219 if (strstr(name,
"bkgcorrect"))
221 if (strstr(name,
"normalize"))
223 if (strstr(name,
"compress"))
225 if (strstr(name,
"ncycles"))
227 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
232 for (cpl_parameter * p = cpl_parameterlist_get_first(undist_par);
233 p != NULL; p = cpl_parameterlist_get_next(undist_par)) {
234 const char * name = cpl_parameter_get_name(p);
235 if (strstr(name,
"bkgcorrect"))
237 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
242 for (cpl_parameter * p = cpl_parameterlist_get_first(destripe_par);
243 p != NULL; p = cpl_parameterlist_get_next(destripe_par)) {
244 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
249 for (cpl_parameter * p = cpl_parameterlist_get_first(spcobs_par);
250 p != NULL; p = cpl_parameterlist_get_next(spcobs_par)) {
251 const char * name = cpl_parameter_get_name(p);
252 if (strstr(name,
"auto_bpm") ||
253 strstr(name,
"rem_glitch") ||
254 strstr(name,
"purge_bad") ||
255 strstr(name,
"union") ||
256 strstr(name,
"refine") ||
257 strstr(name,
"xcorr") ||
258 strstr(name,
"objects") ||
259 strstr(name,
"nodding") ||
260 strstr(name,
"offsets")) {
263 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
269 skip_if(irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
270 "delete-temp", CPL_TRUE, NULL,
271 context,
"Delete temporary files "
272 "created during processing"));
274 skip_if(irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
275 "destripe", CPL_FALSE, NULL, context,
276 "Attempt to remove stripes"));
280 cpl_parameterlist_delete(repack->parameters);
281 cpl_parameterlist_delete(undist->parameters);
282 cpl_parameterlist_delete(destripe->parameters);
283 cpl_parameterlist_delete(spcobs->parameters);
284 cpl_plugin_delete(&repack->interface);
285 cpl_plugin_delete(&undist->interface);
286 cpl_plugin_delete(&destripe->interface);
287 cpl_plugin_delete(&spcobs->interface);
288 cpl_pluginlist_delete(plugins);
290 return cpl_error_get_code();
294util_repack_set_parameters(cpl_parameterlist * rec_pars,
295 const cpl_parameterlist * chain_pars)
299 skip_if(visir_copy_parameters(rec_pars, chain_pars));
301 par = cpl_parameterlist_find(rec_pars, PACKAGE
".visir_util_repack.bkgcorrect");
304 cpl_parameter_set_string(par,
"none");
306 par = cpl_parameterlist_find(rec_pars, PACKAGE
".visir_util_repack.normalize");
309 cpl_parameter_set_bool(par, CPL_TRUE);
311 par = cpl_parameterlist_find(rec_pars, PACKAGE
".visir_util_repack.compress");
314 cpl_parameter_set_bool(par, CPL_TRUE);
318 return cpl_error_get_code();
322util_undistort_set_parameters(cpl_parameterlist * rec_pars,
323 const cpl_parameterlist * chain_pars)
327 skip_if(visir_copy_parameters(rec_pars, chain_pars));
329 par = cpl_parameterlist_find(rec_pars, PACKAGE
".visir_util_repack.bkgcorrect");
332 cpl_parameter_set_bool(par, CPL_TRUE);
336 return cpl_error_get_code();
347static int visir_spc_reduce(cpl_frameset * framelist,
348 const cpl_parameterlist * parlist)
350 cpl_pluginlist * plugins = cpl_pluginlist_new();
351 cpl_frameset * usedframes = cpl_frameset_new();
353 cpl_recipe * repack = visir_init_recipe(
"visir_util_repack",
354 &visir_util_repack_get_info,
356 cpl_recipe * undist = visir_init_recipe(
"visir_util_undistort",
357 &visir_util_undistort_get_info,
359 cpl_recipe * destripe = visir_init_recipe(
"visir_old_util_destripe",
360 &visir_old_util_destripe_get_info,
362 cpl_recipe * spcobs = visir_init_recipe(
"visir_old_spc_obs",
363 &visir_old_spc_obs_get_info,
365 const cpl_boolean do_destripe =
366 irplib_parameterlist_get_bool(parlist, PACKAGE,
367 RECIPE_STRING,
"destripe");
369 cpl_frameset * repackset = cpl_frameset_new();
370 cpl_frameset * obsset = cpl_frameset_new();
372 const char * input_tag;
375 cpl_frame * linframe = NULL;
376 FOR_EACH_FRAMESET(frame_, framelist) {
377 cpl_frame * frame = cpl_frame_duplicate(frame_);
378 cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
379 if (!strcmp(cpl_frame_get_tag(frame), VISIR_SPEC_STD_CAT_PROCATG) ||
380 !strcmp(cpl_frame_get_tag(frame), VISIR_SPEC_CAL_LINES_PROCATG) ||
381 !strcmp(cpl_frame_get_tag(frame), VISIR_SPEC_CAL_QEFF_PROCATG))
382 cpl_frameset_insert(obsset, frame);
385 if (!strcmp(cpl_frame_get_tag(frame), VISIR_CALIB_LIN)) {
386 linframe = cpl_frame_duplicate(frame);
388 cpl_frameset_insert(repackset, frame);
391 nrep = cpl_frameset_get_size(repackset);
392 if (cpl_frameset_find(repackset, VISIR_CALIB_LIN)) {
395 if (cpl_frameset_find(repackset, VISIR_CALIB_STATIC_MASK)) {
398 if (cpl_frameset_count_tags(repackset, VISIR_SPC_OBS_ECH_RAW) == nrep) {
399 input_tag = VISIR_SPC_OBS_ECH_PP;
401 else if (cpl_frameset_count_tags(repackset,
402 VISIR_SPC_PHOT_ECH_RAW) == nrep) {
403 input_tag = VISIR_SPC_PHOT_ECH_PP;
405 else if (cpl_frameset_count_tags(repackset, VISIR_SPC_OBS_RAW) == nrep) {
406 input_tag = VISIR_SPC_OBS_PP;
408 else if (cpl_frameset_count_tags(repackset, VISIR_SPC_PHOT_RAW) == nrep) {
409 input_tag = VISIR_SPC_PHOT_PP;
412 error_if(1, CPL_ERROR_ILLEGAL_INPUT,
"Invalid input tags");
414 cpl_msg_info(cpl_func,
"Working on %s", input_tag);
415 skip_if(visir_run_recipe(repack, repackset, parlist,
416 &util_repack_set_parameters));
418 cpl_frameset * undistset = visir_prepare_frameset(repackset, NULL, 0,
422 undistset = visir_remove_modified_calib(undistset);
425 cpl_frameset_insert(undistset, linframe);
427 cpl_frameset_delete(repackset);
429 skip_if(visir_run_recipe(undist, undistset, parlist,
430 &util_undistort_set_parameters));
432 cpl_frameset * destripeset = undistset;
434 const char * tagmap[] = {
"UNDISTORTED", VISIR_UTIL_CORRECTED};
436 visir_prepare_frameset(undistset, tagmap, ARRAY_LEN(tagmap),
438 skip_if(visir_run_recipe(destripe, destripeset, parlist,
439 &visir_copy_parameters));
442 const char * obstagmap[] = {do_destripe ?
"DESTRIPED" :
"UNDISTORTED",
444 cpl_frameset * tmp_obs =
445 visir_prepare_frameset(destripeset, obstagmap, ARRAY_LEN(obstagmap),
448 FOR_EACH_FRAMESET_C(frm, tmp_obs)
449 cpl_frameset_insert(obsset, cpl_frame_duplicate(frm));
450 cpl_frameset_delete(tmp_obs);
452 cpl_frame * sky = cpl_frameset_find(undistset,
"SPEC_OBS_LMR_SKYFRAME");
453 skip_if(sky == NULL);
454 sky = cpl_frame_duplicate(sky);
455 cpl_frame_set_group(sky, CPL_FRAME_GROUP_CALIB);
456 cpl_frameset_insert(obsset, sky);
458 cpl_frameset_delete(undistset);
460 skip_if(visir_run_recipe(spcobs, obsset, parlist,
461 &visir_copy_parameters));
463 FOR_EACH_FRAMESET(frm, obsset)
464 if (cpl_frame_get_group(frm) == CPL_FRAME_GROUP_PRODUCT)
465 cpl_frameset_insert(framelist, cpl_frame_duplicate(frm));
470 cpl_recipe * recipes[] = {repack, undist, destripe, spcobs};
471 for (
size_t i = 0; i < ARRAY_LEN(recipes); i++)
473 cpl_plugin_delete(&(recipes[i]->interface));
475 cpl_pluginlist_delete(plugins);
477 cpl_frameset_delete(obsset);
478 cpl_frameset_delete(usedframes);
480 return cpl_error_get_code();
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.