00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024
00025
00026
00027
00028
00029 #ifndef _XOPEN_SOURCE
00030 #define _XOPEN_SOURCE 600
00031 #endif
00032
00033 #ifndef ESO_COVERAGE_BUILD
00034 #define ESO_COVERAGE_BUILD 0
00035 #endif
00036
00037 #include "visir_recipe.h"
00038
00039 #include "visir_utils.h"
00040 #include "irplib_utils.h"
00041 #include <cxlist.h>
00042 #include <string.h>
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045
00046 int visir_util_repack_get_info(cpl_pluginlist *);
00047 int visir_util_undistort_get_info(cpl_pluginlist *);
00048 int visir_old_util_destripe_get_info(cpl_pluginlist *);
00049 int visir_old_spc_obs_get_info(cpl_pluginlist *);
00050
00051
00052
00053
00054
00055 #define RECIPE_STRING "visir_spc_reduce"
00056
00057
00058
00059
00060
00061
00062
00063 static cpl_error_code visir_spc_reduce_fill_parameterlist(cpl_parameterlist *);
00064 static int visir_spc_reduce(cpl_frameset *, const cpl_parameterlist *);
00065
00066 static int visir_spc_reduce_create(cpl_plugin * plugin)
00067 {
00068 cpl_recipe * recipe = (cpl_recipe *)plugin;
00069 cpl_errorstate prestate = cpl_errorstate_get();
00070
00071
00072
00073 return cpl_recipedefine_create(plugin)
00074 || cpl_recipedefine_create_is_ok(prestate,
00075 visir_spc_reduce_fill_parameterlist(recipe->parameters))
00076 ? (int)cpl_error_set_where(cpl_func) : 0;
00077 }
00078
00079
00080
00093
00094 static int visir_spc_reduce_exec(cpl_plugin * plugin)
00095 {
00096 char * progname = getenv("_");
00097 char * classpath = getenv("CLASSPATH");
00098 cpl_msg_debug(cpl_func, "Program name: %s", progname);
00099 cpl_msg_debug(cpl_func, "CLASSPATH: %s", classpath);
00100 if ((progname && strstr(progname, "gasgano")) ||
00101 (classpath && strstr(classpath, "gasgano.jar"))) {
00102 cpl_msg_info(cpl_func, "Running under gasgano, disabling OpenMP");
00103 setenv("OMP_NUM_THREADS", "0", 1);
00104 return visir_tmpdir_exec(RECIPE_STRING, plugin, visir_spc_reduce);
00105 }
00106 else if (ESO_COVERAGE_BUILD || getenv("VISIR_NO_FORK") != NULL) {
00107
00108 return cpl_recipedefine_exec(plugin, visir_spc_reduce)
00109 ? (int)cpl_error_set_where(cpl_func) : 0;
00110 }
00111 else
00112 return visir_forking_exec(RECIPE_STRING, plugin, visir_spc_reduce);
00113 }
00114
00115
00116 static int visir_spc_reduce_destroy(cpl_plugin * plugin)
00117 {
00118
00119 return cpl_recipedefine_destroy(plugin)
00120 ? (int)cpl_error_set_where(cpl_func) : 0;
00121 }
00122
00123 int cpl_plugin_get_info(cpl_pluginlist * list)
00124 {
00125
00126 return cpl_recipedefine_init(list, CPL_VERSION_CODE,
00127 VISIR_BINARY_VERSION,
00128 "visir_spc_reduce",
00129 "Spectroscopic Observation recipe",
00130 "This recipe performs a wavelength calibration "
00131 "followed by spectrum extraction from a combined image. "
00132 "It can also compute sensitivities for standard star "
00133 "observations.\n"
00134 "It works for low and high resolution including echelle "
00135 "mode.\n"
00136
00137 "The files listed in the Set Of Frames (sof-file) "
00138 "must be tagged:\n"
00139 "VISIR-Long-Slit-Spectroscopy-file.fits "
00140 VISIR_SPC_OBS_RAW "\n"
00141 "VISIR-Quantum-Efficiency-Calibration-file.fits "
00142 VISIR_CALIB_QEFF_SPC "\n"
00143 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
00144 VISIR_CALIB_LINES_SPC "\n"
00145 "VISIR-Standard-Star-Flux-Catalog.fits (optional)"
00146 VISIR_CALIB_STDSTAR_SPC"\n"
00147 "VISIR-linearty-table.fits "VISIR_CALIB_LIN" (optional)"
00148 MAN_VISIR_CALIB_BPM_IMG,
00149 "Julian Taylor",
00150 "jtaylor@partner.eso.org",
00151 cpl_get_license(PACKAGE_NAME, "2015"),
00152 visir_spc_reduce_create,
00153 visir_spc_reduce_exec,
00154 visir_spc_reduce_destroy)
00155 ? ((void)cpl_error_set_where(cpl_func), 1) : 0;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00173
00174
00175
00176
00177
00178
00179
00187
00188 static cpl_error_code
00189 visir_spc_reduce_fill_parameterlist(cpl_parameterlist * self)
00190 {
00191 cpl_pluginlist * plugins = cpl_pluginlist_new();
00192 const char * context = PACKAGE ".visir_spc_reduce";
00193
00194 cpl_recipe * repack = visir_init_recipe("visir_util_repack",
00195 &visir_util_repack_get_info,
00196 plugins);
00197
00198 cpl_recipe * undist = visir_init_recipe("visir_util_undistort",
00199 &visir_util_undistort_get_info,
00200 plugins);
00201
00202 cpl_recipe * destripe = visir_init_recipe("visir_old_util_destripe",
00203 &visir_old_util_destripe_get_info,
00204 plugins);
00205
00206 cpl_recipe * spcobs = visir_init_recipe("visir_old_spc_obs",
00207 &visir_old_spc_obs_get_info,
00208 plugins);
00209
00210 cpl_plugin_get_init(&repack->interface)(&repack->interface);
00211 cpl_plugin_get_init(&undist->interface)(&undist->interface);
00212 cpl_plugin_get_init(&destripe->interface)(&destripe->interface);
00213 cpl_plugin_get_init(&spcobs->interface)(&spcobs->interface);
00214 cpl_parameterlist * repack_par = repack->parameters;
00215 cpl_parameterlist * undist_par = undist->parameters;
00216 cpl_parameterlist * destripe_par = destripe->parameters;
00217 cpl_parameterlist * spcobs_par = spcobs->parameters;
00218 skip_if(0);
00219
00220 for (cpl_parameter * p = cpl_parameterlist_get_first(repack_par);
00221 p != NULL; p = cpl_parameterlist_get_next(repack_par)) {
00222 const char * name = cpl_parameter_get_name(p);
00223 if (strstr(name, "bkgcorrect"))
00224 continue;
00225 if (strstr(name, "normalize"))
00226 continue;
00227 if (strstr(name, "compress"))
00228 continue;
00229 if (strstr(name, "ncycles"))
00230 continue;
00231 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
00232 }
00233
00234 skip_if(0);
00235
00236 for (cpl_parameter * p = cpl_parameterlist_get_first(undist_par);
00237 p != NULL; p = cpl_parameterlist_get_next(undist_par)) {
00238 const char * name = cpl_parameter_get_name(p);
00239 if (strstr(name, "bkgcorrect"))
00240 continue;
00241 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
00242 }
00243
00244 skip_if(0);
00245
00246 for (cpl_parameter * p = cpl_parameterlist_get_first(destripe_par);
00247 p != NULL; p = cpl_parameterlist_get_next(destripe_par)) {
00248 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
00249 }
00250
00251 skip_if(0);
00252
00253 for (cpl_parameter * p = cpl_parameterlist_get_first(spcobs_par);
00254 p != NULL; p = cpl_parameterlist_get_next(spcobs_par)) {
00255 const char * name = cpl_parameter_get_name(p);
00256 if (strstr(name, "auto_bpm") ||
00257 strstr(name, "rem_glitch") ||
00258 strstr(name, "purge_bad") ||
00259 strstr(name, "union") ||
00260 strstr(name, "refine") ||
00261 strstr(name, "xcorr") ||
00262 strstr(name, "objects") ||
00263 strstr(name, "nodding") ||
00264 strstr(name, "offsets")) {
00265 continue;
00266 }
00267 cpl_parameterlist_append(self, visir_parameter_duplicate(p));
00268 }
00269
00270 skip_if(0);
00271
00272
00273 skip_if(irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
00274 "delete-temp", CPL_TRUE, NULL,
00275 context, "Delete temporary files "
00276 "created during processing"));
00277
00278 skip_if(irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
00279 "destripe", CPL_FALSE, NULL, context,
00280 "Attempt to remove stripes"));
00281
00282 end_skip;
00283
00284 cpl_parameterlist_delete(repack->parameters);
00285 cpl_parameterlist_delete(undist->parameters);
00286 cpl_parameterlist_delete(destripe->parameters);
00287 cpl_parameterlist_delete(spcobs->parameters);
00288 cpl_plugin_delete(&repack->interface);
00289 cpl_plugin_delete(&undist->interface);
00290 cpl_plugin_delete(&destripe->interface);
00291 cpl_plugin_delete(&spcobs->interface);
00292 cpl_pluginlist_delete(plugins);
00293
00294 return cpl_error_get_code();
00295 }
00296
00297 static cpl_error_code
00298 util_repack_set_parameters(cpl_parameterlist * rec_pars,
00299 const cpl_parameterlist * chain_pars)
00300 {
00301 cpl_parameter * par;
00302
00303 skip_if(visir_copy_parameters(rec_pars, chain_pars));
00304
00305 par = cpl_parameterlist_find(rec_pars, PACKAGE".visir_util_repack.bkgcorrect");
00306
00307 if (par)
00308 cpl_parameter_set_string(par, "none");
00309
00310 par = cpl_parameterlist_find(rec_pars, PACKAGE".visir_util_repack.normalize");
00311
00312 if (par)
00313 cpl_parameter_set_bool(par, CPL_TRUE);
00314
00315 par = cpl_parameterlist_find(rec_pars, PACKAGE".visir_util_repack.compress");
00316
00317 if (par)
00318 cpl_parameter_set_bool(par, CPL_TRUE);
00319
00320 end_skip;
00321
00322 return cpl_error_get_code();
00323 }
00324
00325 static cpl_error_code
00326 util_undistort_set_parameters(cpl_parameterlist * rec_pars,
00327 const cpl_parameterlist * chain_pars)
00328 {
00329 cpl_parameter * par;
00330
00331 skip_if(visir_copy_parameters(rec_pars, chain_pars));
00332
00333 par = cpl_parameterlist_find(rec_pars, PACKAGE".visir_util_repack.bkgcorrect");
00334
00335 if (par)
00336 cpl_parameter_set_bool(par, CPL_TRUE);
00337
00338 end_skip;
00339
00340 return cpl_error_get_code();
00341 }
00342
00343
00350
00351 static int visir_spc_reduce(cpl_frameset * framelist,
00352 const cpl_parameterlist * parlist)
00353 {
00354 cpl_pluginlist * plugins = cpl_pluginlist_new();
00355 cpl_frameset * usedframes = cpl_frameset_new();
00356
00357 cpl_recipe * repack = visir_init_recipe("visir_util_repack",
00358 &visir_util_repack_get_info,
00359 plugins);
00360 cpl_recipe * undist = visir_init_recipe("visir_util_undistort",
00361 &visir_util_undistort_get_info,
00362 plugins);
00363 cpl_recipe * destripe = visir_init_recipe("visir_old_util_destripe",
00364 &visir_old_util_destripe_get_info,
00365 plugins);
00366 cpl_recipe * spcobs = visir_init_recipe("visir_old_spc_obs",
00367 &visir_old_spc_obs_get_info,
00368 plugins);
00369 const cpl_boolean do_destripe =
00370 irplib_parameterlist_get_bool(parlist, PACKAGE,
00371 RECIPE_STRING, "destripe");
00372
00373 cpl_frameset * repackset = cpl_frameset_new();
00374 cpl_frameset * obsset = cpl_frameset_new();
00375 cpl_size nrep;
00376 const char * input_tag;
00377
00378 visir_dfs_set_groups(framelist);
00379 cpl_frame * linframe = NULL;
00380 FOR_EACH_FRAMESET(frame_, framelist) {
00381 cpl_frame * frame = cpl_frame_duplicate(frame_);
00382 cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
00383 if (!strcmp(cpl_frame_get_tag(frame), VISIR_SPEC_STD_CAT_PROCATG) ||
00384 !strcmp(cpl_frame_get_tag(frame), VISIR_SPEC_CAL_LINES_PROCATG) ||
00385 !strcmp(cpl_frame_get_tag(frame), VISIR_SPEC_CAL_QEFF_PROCATG))
00386 cpl_frameset_insert(obsset, frame);
00387 else {
00388
00389 if (!strcmp(cpl_frame_get_tag(frame), VISIR_CALIB_LIN)) {
00390 linframe = cpl_frame_duplicate(frame);
00391 }
00392 cpl_frameset_insert(repackset, frame);
00393 }
00394 }
00395 nrep = cpl_frameset_get_size(repackset);
00396 if (cpl_frameset_find(repackset, VISIR_CALIB_LIN)) {
00397 nrep--;
00398 }
00399 if (cpl_frameset_find(repackset, VISIR_CALIB_STATIC_MASK)) {
00400 nrep--;
00401 }
00402 if (cpl_frameset_count_tags(repackset, VISIR_SPC_OBS_ECH_RAW) == nrep) {
00403 input_tag = VISIR_SPC_OBS_ECH_PP;
00404 }
00405 else if (cpl_frameset_count_tags(repackset,
00406 VISIR_SPC_PHOT_ECH_RAW) == nrep) {
00407 input_tag = VISIR_SPC_PHOT_ECH_PP;
00408 }
00409 else if (cpl_frameset_count_tags(repackset, VISIR_SPC_OBS_RAW) == nrep) {
00410 input_tag = VISIR_SPC_OBS_PP;
00411 }
00412 else if (cpl_frameset_count_tags(repackset, VISIR_SPC_PHOT_RAW) == nrep) {
00413 input_tag = VISIR_SPC_PHOT_PP;
00414 }
00415 else {
00416 error_if(1, CPL_ERROR_ILLEGAL_INPUT, "Invalid input tags");
00417 }
00418 cpl_msg_info(cpl_func, "Working on %s", input_tag);
00419 skip_if(visir_run_recipe(repack, repackset, parlist,
00420 &util_repack_set_parameters));
00421
00422 cpl_frameset * undistset = visir_prepare_frameset(repackset, NULL, 0,
00423 CPL_TRUE);
00424
00425
00426 undistset = visir_remove_modified_calib(undistset);
00427
00428 if (linframe) {
00429 cpl_frameset_insert(undistset, linframe);
00430 }
00431 cpl_frameset_delete(repackset);
00432 repackset = NULL;
00433 skip_if(visir_run_recipe(undist, undistset, parlist,
00434 &util_undistort_set_parameters));
00435
00436 cpl_frameset * destripeset = undistset;
00437 if (do_destripe) {
00438 const char * tagmap[] = {"UNDISTORTED", VISIR_UTIL_CORRECTED};
00439 destripeset =
00440 visir_prepare_frameset(undistset, tagmap, ARRAY_LEN(tagmap),
00441 CPL_FALSE);
00442 skip_if(visir_run_recipe(destripe, destripeset, parlist,
00443 &visir_copy_parameters));
00444 }
00445
00446 const char * obstagmap[] = {do_destripe ? "DESTRIPED" : "UNDISTORTED",
00447 input_tag};
00448 cpl_frameset * tmp_obs =
00449 visir_prepare_frameset(destripeset, obstagmap, ARRAY_LEN(obstagmap),
00450 CPL_FALSE);
00451
00452 FOR_EACH_FRAMESET_C(frm, tmp_obs)
00453 cpl_frameset_insert(obsset, cpl_frame_duplicate(frm));
00454 cpl_frameset_delete(tmp_obs);
00455
00456 cpl_frame * sky = cpl_frameset_find(undistset, "SPEC_OBS_LMR_SKYFRAME");
00457 skip_if(sky == NULL);
00458 sky = cpl_frame_duplicate(sky);
00459 cpl_frame_set_group(sky, CPL_FRAME_GROUP_CALIB);
00460 cpl_frameset_insert(obsset, sky);
00461
00462 cpl_frameset_delete(undistset);
00463
00464 skip_if(visir_run_recipe(spcobs, obsset, parlist,
00465 &visir_copy_parameters));
00466
00467 FOR_EACH_FRAMESET(frm, obsset)
00468 if (cpl_frame_get_group(frm) == CPL_FRAME_GROUP_PRODUCT)
00469 cpl_frameset_insert(framelist, cpl_frame_duplicate(frm));
00470 skip_if(0);
00471
00472 end_skip;
00473 {
00474 cpl_recipe * recipes[] = {repack, undist, destripe, spcobs};
00475 for (size_t i = 0; i < ARRAY_LEN(recipes); i++)
00476 if (recipes[i])
00477 cpl_plugin_delete(&(recipes[i]->interface));
00478 }
00479 cpl_pluginlist_delete(plugins);
00480
00481 cpl_frameset_delete(obsset);
00482 cpl_frameset_delete(usedframes);
00483
00484 return cpl_error_get_code();
00485 }