00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include "visir_recipe.h"
00037 #include "visir_spectro.h"
00038 #include "visir_spc_distortion.h"
00039 #include "visir_spc_photom.h"
00040
00041
00042
00043
00044
00045
00046 #define RECIPE_STRING "visir_old_spc_obs_ech"
00047
00048
00049 #define RECIPE_KEYS_REGEXP_ALL \
00050 VISIR_PFITS_REGEXP_IMG_RECOMBINE \
00051 "|" VISIR_PFITS_REGEXP_SPC_GET_RES_WL
00052
00053
00054 #define RECIPE_KEYS_REGEXP \
00055 RECIPE_KEYS_REGEXP_ALL \
00056 "|" VISIR_PFITS_REGEXP_CAPA \
00057 "|" VISIR_PFITS_REGEXP_SPC_WCAL_PAF
00058
00059
00060 #define RECIPE_KEYS_REGEXP_WCS \
00061 RECIPE_KEYS_REGEXP \
00062 "|" IRPLIB_PFITS_WCS_REGEXP
00063
00064 #define RAW_INPUT 0
00065 #define PREPROCESSED_INPUT 1
00066
00067
00068
00069
00070 static cpl_error_code visir_spc_ech_save(cpl_frameset *,
00071 const cpl_parameterlist *,
00072 const cpl_propertylist *,
00073 const cpl_propertylist *,
00074 const cpl_image *,
00075 const cpl_image *,
00076 const cpl_table *,
00077 const char *,
00078 const char *,
00079 const char *);
00080
00081 VISIR_RECIPE_DEFINE(visir_old_spc_obs_ech,
00082 VISIR_PARAM_ZERODIST | VISIR_PARAM_ORDEROFF |
00083 VISIR_PARAM_EMIS_TOL |
00084 VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
00085 VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
00086 VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00087 VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
00088 VISIR_PARAM_UNION | VISIR_PARAM_REJECT |
00089 VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
00090 VISIR_PARAM_PLOT | VISIR_PARAM_SLITSKEW |
00091 VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
00092 VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
00093 VISIR_PARAM_HORIARC | VISIR_PARAM_FIXCOMBI |
00094 VISIR_PARAM_BKG_CORRECT,
00095 "Old DRS detector: Spectroscopic Observation recipe in Echelle mode",
00096 "This recipe estimates the dispersion relation using the "
00097 "atmospheric spectrum\n"
00098 "in a grism spectroscopy half-cycle frame.\n"
00099 "It also extracts the spectrum of an observed object using "
00100 "a combined frame.\n"
00101 "The files listed in the Set Of Frames (sof-file) "
00102 "must be tagged:\n"
00103 "VISIR-Echelle-Spectroscopy-file.fits "
00104 VISIR_SPC_OBS_ECH_RAW "\n"
00105 "VISIR-Quantum-Efficiency-Calibration-file.fits "
00106 VISIR_CALIB_QEFF_SPC "\n"
00107 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
00108 VISIR_CALIB_LINES_SPC
00109 "\n"
00110 MAN_VISIR_CALIB_BPM_SPC);
00111
00112
00113
00114
00115
00116
00120
00121
00122
00123
00124
00125
00126
00133
00134 static int visir_old_spc_obs_ech(cpl_frameset * framelist,
00135 const cpl_parameterlist * parlist)
00136 {
00137 irplib_framelist * allframes = NULL;
00138 irplib_framelist * rawframes = NULL;
00139 irplib_framelist * skyframes = NULL;
00140 cpl_propertylist * qclist = cpl_propertylist_new();
00141 cpl_propertylist * paflist = cpl_propertylist_new();
00142 const char * badpix;
00143 const char * star_cat;
00144 const char * spc_cal_qeff;
00145 const char * spc_cal_lines;
00146 const char * flat;
00147 cpl_image * combined = NULL;
00148 cpl_image * flipped = NULL;
00149 cpl_image * comnarrow = NULL;
00150 cpl_imagelist * hcycle = NULL;
00151 cpl_table * spc_table = NULL;
00152 cpl_image * weight2d = NULL;
00153 cpl_image * imhcycle = NULL;
00154 cpl_image * imhcyclenarrow = NULL;
00155 cpl_image ** combinedpair;
00156 double wlen, slitw, temp, fwhm;
00157 visir_spc_resol resol;
00158 cpl_boolean drop_wcs;
00159 int input_mode;
00160 cpl_boolean bkgcorrect;
00161 cpl_errorstate cleanstate = cpl_errorstate_get();
00162 visir_spc_config spc_config;
00163 const char * dit_key = VISIR_PFITS_DOUBLE_DIT;
00164 cpl_boolean has_phot = CPL_FALSE;
00165
00166 spc_config.recipename = RECIPE_STRING;
00167 spc_config.parlist = parlist;
00168 spc_config.orderoffset = 0;
00169 spc_config.phot_emis_tol = 1.0;
00170
00171
00172 spc_config.plot =
00173 visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_PLOT);
00174
00175 spc_config.phi =
00176 visir_parameterlist_get_double(parlist,RECIPE_STRING,VISIR_PARAM_SLITSKEW);
00177 spc_config.ksi =
00178 visir_parameterlist_get_double(parlist,RECIPE_STRING,VISIR_PARAM_SPECSKEW);
00179 spc_config.eps =
00180 visir_parameterlist_get_double(parlist, RECIPE_STRING,VISIR_PARAM_VERTARC);
00181 spc_config.delta =
00182 visir_parameterlist_get_double(parlist, RECIPE_STRING,VISIR_PARAM_HORIARC);
00183
00184 spc_config.orderoffset =
00185 visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_ORDEROFF);
00186
00187 spc_config.do_fixcombi = visir_parameterlist_get_bool(parlist, RECIPE_STRING,
00188 VISIR_PARAM_FIXCOMBI);
00189 spc_config.phot_emis_tol =
00190 visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_EMIS_TOL);
00191
00192 bkgcorrect = visir_parameterlist_get_bool(parlist, RECIPE_STRING,
00193 VISIR_PARAM_BKG_CORRECT);
00194 skip_if (0);
00195
00196
00197 skip_if (visir_dfs_set_groups(framelist));
00198
00199
00200 allframes = irplib_framelist_cast(framelist);
00201 skip_if(allframes == NULL);
00202 rawframes = irplib_framelist_extract(allframes, VISIR_SPC_OBS_ECH_RAW);
00203 if (rawframes == NULL) {
00204 cpl_errorstate_set(cleanstate);
00205 rawframes = irplib_framelist_extract(allframes, VISIR_SPC_OBS_ECH_PP);
00206 skip_if (rawframes == NULL);
00207 skyframes = irplib_framelist_extract(allframes, VISIR_SPC_OBS_SKYFRAME);
00208 skip_if (skyframes == NULL);
00209 input_mode = PREPROCESSED_INPUT;
00210 cpl_msg_info(cpl_func, "Input: preprocessed data");
00211 } else {
00212 cpl_msg_info(cpl_func, "Input: raw data");
00213 input_mode = RAW_INPUT;
00214 }
00215 skip_if (rawframes == NULL);
00216
00217 irplib_framelist_empty(allframes);
00218
00219 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
00220 VISIR_PFITS_REGEXP_SPC_SENSIT
00221 "|" RECIPE_KEYS_REGEXP_ALL
00222 "|" RECIPE_KEYS_REGEXP_WCS
00223 "|" VISIR_PFITS_REGEXP_DIT
00224 "|" "ESO DRS .*"
00225 "|" "ESO QC EXPTIME"
00226 "|" "ESO QC BACKGD MEAN"
00227 ")$", CPL_FALSE));
00228
00229 skip_if(visir_dfs_check_framelist_tag(rawframes));
00230
00231 if (cpl_propertylist_has(irplib_framelist_get_propertylist(rawframes, 0),
00232 VISIR_PFITS_DOUBLE_SEQ1_DIT))
00233 dit_key = VISIR_PFITS_DOUBLE_SEQ1_DIT;
00234 skip_if(0);
00235
00236 visir_data_type data_type;
00237 const cpl_frame * frm = irplib_framelist_get_const(rawframes, 0);
00238 skip_if(visir_get_data_type(frm,
00239 irplib_framelist_get_propertylist(rawframes, 0), &data_type, NULL));
00240
00241
00242 star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_SPC);
00243
00244
00245 spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00246
00247
00248 spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00249
00250
00251 badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00252
00253
00254 flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00255
00256
00257 resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm,
00258 visir_data_is_aqu(data_type));
00259
00260 skip_if (0);
00261
00262 if (resol != VISIR_SPC_R_GHR) {
00263 if (spc_config.orderoffset == 0) {
00264 cpl_msg_warning(cpl_func,"Reducing non-HR Grism data as main order");
00265 } else {
00266 cpl_msg_error(cpl_func, "This recipe cannot reduce non-HR Grism "
00267 "data with an order-offset of %d",
00268 spc_config.orderoffset);
00269 visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00270 skip_if(1);
00271 }
00272 }
00273
00274 if (input_mode == PREPROCESSED_INPUT) {
00275 cpl_imagelist * imagelist = cpl_imagelist_new();
00276 cpl_size nframes = irplib_framelist_get_size(rawframes);
00277 const cpl_propertylist ** plists =
00278 cpl_malloc(nframes * sizeof(cpl_propertylist *));
00279 double bg_sum = 0;
00280
00281 for (cpl_size i = 0; i < nframes; i++) {
00282 cpl_frame * frame = irplib_framelist_get(rawframes, i);
00283 const char * fname = cpl_frame_get_filename(frame);
00284 const cpl_size next = cpl_fits_count_extensions(fname);
00285 cpl_imagelist * tmplist = cpl_imagelist_new();
00286 cpl_errorstate prestate = cpl_errorstate_get();
00287 cpl_image * tmp;
00288
00289 plists[i] = irplib_framelist_get_propertylist_const(rawframes, i);
00290 if (cpl_propertylist_has(plists[i], "ESO QC BACKGD MEAN"))
00291 bg_sum += cpl_propertylist_get_double(plists[i],
00292 "ESO QC BACKGD MEAN");
00293 skip_if(0);
00294
00295 for (cpl_size j = 0; j < 1 + next; j++) {
00296 cpl_image * img =
00297 cpl_image_load(fname, CPL_TYPE_UNSPECIFIED, 0, j);
00298 if (img == NULL) {
00299 cpl_errorstate_set(prestate);
00300 cpl_msg_info(cpl_func, "No image in extension %d", (int)j);
00301 continue;
00302 }
00303 cpl_imagelist_set(tmplist, img, cpl_imagelist_get_size(tmplist));
00304 skip_if(0);
00305 }
00306 tmp = cpl_imagelist_collapse_create(tmplist);
00307
00308 skip_if (visir_spc_det_fix(&tmp, 1, CPL_TRUE,
00309 wlen, resol,
00310 spc_config.phi,
00311 spc_config.ksi,
00312 spc_config.eps,
00313 spc_config.delta,
00314 spc_config.plot));
00315 cpl_imagelist_set(imagelist, tmp,
00316 cpl_imagelist_get_size(imagelist));
00317 cpl_imagelist_delete(tmplist);
00318 }
00319 combinedpair =
00320 visir_img_recombine_list(RECIPE_STRING, parlist, imagelist,
00321 plists, CPL_GEOM_FIRST, &drop_wcs);
00322
00323
00324 cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN",
00325 bg_sum / nframes);
00326 cpl_imagelist_delete(imagelist);
00327 cpl_free(plists);
00328 bug_if(0);
00329
00330
00331 {
00332 cpl_frame * frame = irplib_framelist_get(skyframes, 0);
00333 const char * fname = cpl_frame_get_filename(frame);
00334 imhcycle = cpl_image_load(fname, CPL_TYPE_UNSPECIFIED, 0, 0);
00335 skip_if(imhcycle == NULL);
00336 }
00337
00338 skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_TRUE,
00339 wlen, resol,
00340 spc_config.phi,
00341 spc_config.ksi,
00342 spc_config.eps,
00343 spc_config.delta,
00344 spc_config.plot));
00345
00346 } else if (input_mode == RAW_INPUT) {
00347 combinedpair = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix,
00348 flat, CPL_GEOM_FIRST, &drop_wcs,
00349 !spc_config.do_fixcombi, wlen, resol);
00350
00351
00352 hcycle = visir_load_hcycle(rawframes, 0);
00353 skip_if (0);
00354
00355 imhcycle = cpl_imagelist_unset(hcycle, 0);
00356
00357 skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
00358 wlen, resol,
00359 spc_config.phi,
00360 spc_config.ksi,
00361 spc_config.eps,
00362 spc_config.delta,
00363 spc_config.plot));
00364
00365 skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
00366 }
00367 else
00368 bug_if(1);
00369
00370 if (combinedpair == NULL) {
00371 cpl_msg_error(cpl_func, "Could not combine the input frames");
00372 skip_if (1);
00373 }
00374
00375 cpl_image_delete(combinedpair[1]);
00376 combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
00377 cpl_image_delete(combinedpair[0]);
00378 cpl_free(combinedpair);
00379
00380
00381
00382 flipped = visir_spc_flip(combined, wlen, resol, VISIR_DATA_CUBE2);
00383 skip_if (0);
00384 cpl_image_delete(combined);
00385 combined = flipped;
00386 flipped = NULL;
00387
00388
00389
00390 flipped = visir_spc_flip(imhcycle, wlen, resol, VISIR_DATA_CUBE2);
00391 skip_if (0);
00392 cpl_image_delete(imhcycle);
00393 imhcycle = flipped;
00394 flipped = NULL;
00395
00396 skip_if (0);
00397
00398 skip_if (spc_config.do_fixcombi && input_mode == RAW_INPUT &&
00399 visir_spc_det_fix(&combined, 1, CPL_TRUE,
00400 wlen, resol,
00401 spc_config.phi,
00402 spc_config.ksi,
00403 spc_config.eps,
00404 spc_config.delta,
00405 spc_config.plot));
00406
00407 skip_if(visir_spc_extract_order(&imhcyclenarrow, &comnarrow,
00408 combined, imhcycle, wlen,
00409 &spc_config, CPL_TRUE,
00410 visir_data_is_aqu(data_type)));
00411
00412 skip_if (visir_spc_extract_wcal(comnarrow, imhcyclenarrow,
00413 wlen, slitw, temp, fwhm,
00414 resol, spc_config.orderoffset,
00415 spc_cal_lines, spc_cal_qeff,
00416 &spc_table, &weight2d, qclist,
00417 spc_config.plot, bkgcorrect,
00418 visir_data_is_aqu(data_type)));
00419
00420
00421 bug_if (visir_spectro_qc(qclist, paflist, drop_wcs, rawframes, NULL,
00422 "^(" VISIR_PFITS_REGEXP_SPC_WCAL_PAF ")$"));
00423
00424 if (star_cat) {
00425 cpl_errorstate prestate = cpl_errorstate_get();
00426 const cpl_propertylist * plist =
00427 irplib_framelist_get_propertylist_const(rawframes, 0);
00428 skip_if(0);
00429 if (visir_spc_phot_sensit(rawframes, &spc_config, plist, star_cat,
00430 &weight2d, qclist, spc_table, resol,
00431 dit_key)) {
00432 irplib_error_recover(prestate, "Could not perform photometric "
00433 "calibration");
00434 } else {
00435 has_phot = CPL_TRUE;
00436 }
00437 } else {
00438
00439 skip_if (cpl_table_erase_column(spc_table, "SPC_EMISSIVITY"));
00440 }
00441
00442 irplib_framelist_empty(rawframes);
00443
00444
00445 cpl_msg_info(cpl_func, "Saving the produced spectrum");
00446
00447
00448 bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00449 VISIR_SPC_OBS_ECH_COMBINED_PROCATG));
00450
00451 if (has_phot)
00452 skip_if (visir_spc_ech_save(framelist, parlist, qclist, paflist,
00453 combined, weight2d, spc_table,
00454 VISIR_SPC_PHOT_ECH_TAB_PROCATG,
00455 VISIR_SPC_PHOT_ECH_COMBINED_PROCATG,
00456 VISIR_SPC_PHOT_ECH_WEIGHT_PROCATG));
00457 else
00458 skip_if (visir_spc_ech_save(framelist, parlist, qclist, paflist,
00459 combined, weight2d, spc_table,
00460 VISIR_SPC_OBS_ECH_TAB_PROCATG,
00461 VISIR_SPC_OBS_ECH_COMBINED_PROCATG,
00462 VISIR_SPC_OBS_ECH_WEIGHT_PROCATG));
00463
00464 end_skip;
00465
00466 cpl_propertylist_delete(qclist);
00467 cpl_propertylist_delete(paflist);
00468 irplib_framelist_delete(allframes);
00469 irplib_framelist_delete(rawframes);
00470 irplib_framelist_delete(skyframes);
00471 cpl_image_delete(flipped);
00472 cpl_image_delete(combined);
00473 cpl_image_delete(comnarrow);
00474 cpl_image_delete(imhcyclenarrow);
00475 cpl_table_delete(spc_table);
00476 cpl_image_delete(weight2d);
00477 cpl_image_delete(imhcycle);
00478 cpl_imagelist_delete(hcycle);
00479
00480 return cpl_error_get_code();
00481 }
00482
00483
00484
00499
00500 static cpl_error_code visir_spc_ech_save(cpl_frameset * set,
00501 const cpl_parameterlist * parlist,
00502 const cpl_propertylist * qclist,
00503 const cpl_propertylist * paflist,
00504 const cpl_image * combined,
00505 const cpl_image * weight2d,
00506 const cpl_table * table,
00507 const char * tab_procatg,
00508 const char * com_procatg,
00509 const char * wgt_procatg)
00510 {
00511
00512 bug_if (0);
00513
00514
00515 skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
00516 tab_procatg,
00517 qclist, NULL, visir_pipe_id,
00518 RECIPE_STRING "_tab" CPL_DFS_FITS));
00519
00520
00521 skip_if (irplib_dfs_save_image(set, parlist, set, combined, CPL_BPP_IEEE_FLOAT,
00522 RECIPE_STRING, com_procatg,
00523 qclist, NULL, visir_pipe_id,
00524 RECIPE_STRING CPL_DFS_FITS));
00525
00526
00527 skip_if (irplib_dfs_save_image(set, parlist, set, weight2d, CPL_BPP_IEEE_FLOAT,
00528 RECIPE_STRING, wgt_procatg,
00529 qclist, NULL, visir_pipe_id,
00530 RECIPE_STRING "_weight" CPL_DFS_FITS));
00531
00532 #ifdef VISIR_SAVE_PAF
00533
00534
00535 skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
00536 RECIPE_STRING CPL_DFS_PAF));
00537 #else
00538 bug_if(paflist == NULL);
00539 #endif
00540
00541 end_skip;
00542
00543 return cpl_error_get_code();
00544
00545 }