41 #include "gravi_data.h" 42 #include "gravi_pfits.h" 43 #include "gravi_dfs.h" 45 #include "gravi_utils.h" 47 #include "gravi_calib.h" 48 #include "gravi_preproc.h" 49 #include "gravi_p2vmred.h" 50 #include "gravi_signal.h" 51 #include "gravi_vis.h" 52 #include "gravi_metrology.h" 53 #include "gravi_disp.h" 63 static int gravity_disp_create(cpl_plugin *);
64 static int gravity_disp_exec(cpl_plugin *);
65 static int gravity_disp_destroy(cpl_plugin *);
66 static int gravity_disp(cpl_frameset *,
const cpl_parameterlist *);
72 static char gravity_disp_short[] =
"Calibrate the linearity and the dispersion of the differential delay lines.";
73 static char gravity_disp_description[] =
74 "This recipe is associated to the template GRAVI_all_disp. It measure the phases obtained on the internal source at the position of the Argon lines and various stretch of the FDDL. It deduces the linearity model and the dispersion model of the differential delay lines. These models are stored as polynomials versus wavelength.\n" 75 GRAVI_RECIPE_INPUT
"\n" 76 GRAVI_FLAT_MAP
" : flat calibration (PRO.CATG="GRAVI_FLAT_MAP
")\n" 77 GRAVI_BAD_MAP
" : badpixel calibration (PRO.CATG="GRAVI_BAD_MAP
") \n" 78 GRAVI_WAVE_MAP
" : wave calibration (PRO.CATG="GRAVI_WAVE_MAP
")\n" 79 GRAVI_P2VM_MAP
" : p2vm calibration (PRO.CATG="GRAVI_P2VM_MAP
")\n" 80 GRAVI_DARK_MAP
" : dark calibration\n" 81 GRAVI_DISP_RAW
" >10 : raw dispersion\n" 82 GRAVI_RECIPE_OUTPUT
"\n" 83 GRAVI_DISP_MODEL
" : dispersion model of FDDL\n" 101 int cpl_plugin_get_info(cpl_pluginlist * list)
103 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
104 cpl_plugin * plugin = &recipe->interface;
106 if (cpl_plugin_init(plugin,
108 GRAVI_BINARY_VERSION,
109 CPL_PLUGIN_TYPE_RECIPE,
112 gravity_disp_description,
113 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
118 gravity_disp_destroy)) {
119 cpl_msg_error(cpl_func,
"Plugin initialization failed");
120 (void)cpl_error_set_where(cpl_func);
124 if (cpl_pluginlist_append(list, plugin)) {
125 cpl_msg_error(cpl_func,
"Error adding plugin to list");
126 (void)cpl_error_set_where(cpl_func);
142 static int gravity_disp_create(cpl_plugin * plugin)
148 if (cpl_error_get_code() != CPL_ERROR_NONE) {
149 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
150 cpl_func, __LINE__, cpl_error_get_where());
151 return (
int)cpl_error_get_code();
154 if (plugin == NULL) {
155 cpl_msg_error(cpl_func,
"Null plugin");
156 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
160 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
161 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
162 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
166 recipe = (cpl_recipe *)plugin;
169 recipe->parameters = cpl_parameterlist_new();
170 if (recipe->parameters == NULL) {
171 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
172 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
179 gravi_parameter_add_static_name (recipe->parameters);
182 gravi_parameter_add_preproc_file (recipe->parameters);
183 gravi_parameter_add_p2vmred_file (recipe->parameters);
184 gravi_parameter_add_vis_file (recipe->parameters);
187 gravi_parameter_add_compute_snr (recipe->parameters, isCalib);
190 gravi_parameter_add_rejection (recipe->parameters, isCalib);
193 gravi_parameter_add_compute_vis (recipe->parameters, isCalib);
205 static int gravity_disp_exec(cpl_plugin * plugin)
210 cpl_errorstate initial_errorstate = cpl_errorstate_get();
213 if (cpl_error_get_code() != CPL_ERROR_NONE) {
214 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
215 cpl_func, __LINE__, cpl_error_get_where());
216 return (
int)cpl_error_get_code();
219 if (plugin == NULL) {
220 cpl_msg_error(cpl_func,
"Null plugin");
221 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
225 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
226 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
227 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
231 recipe = (cpl_recipe *)plugin;
234 if (recipe->parameters == NULL) {
235 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
236 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
238 if (recipe->frames == NULL) {
239 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
240 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
244 recipe_status = gravity_disp(recipe->frames, recipe->parameters);
248 if (cpl_dfs_update_product_header(recipe->frames)) {
250 recipe_status = (int)cpl_error_get_code();
254 if (!cpl_errorstate_is_equal(initial_errorstate)) {
257 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
260 return recipe_status;
270 static int gravity_disp_destroy(cpl_plugin * plugin)
274 if (plugin == NULL) {
275 cpl_msg_error(cpl_func,
"Null plugin");
276 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
280 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
281 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
282 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
286 recipe = (cpl_recipe *)plugin;
288 cpl_parameterlist_delete(recipe->parameters);
303 static int gravity_disp(cpl_frameset * frameset,
304 const cpl_parameterlist * parlist)
306 cpl_frameset * disp_frameset=NULL, * darkcalib_frameset=NULL,
307 * dark_frameset=NULL, * wavecalib_frameset=NULL,
308 * badcalib_frameset=NULL, * flatcalib_frameset=NULL, * dispvis_frameset=NULL,
309 * p2vmcalib_frameset=NULL, * wavelampcalib_frameset=NULL, *used_frameset=NULL,
310 * current_frameset=NULL;
312 cpl_frame * frame=NULL;
314 gravi_data * disp_map = NULL, * data = NULL, * dark_map = NULL, * wave_map = NULL,
315 * profile_map = NULL, * badpix_map = NULL, * p2vmred_data = NULL, * preproc_data = NULL,
316 * p2vm_map = NULL, * vis_data = NULL, * tmpvis_data=NULL, * argon_data=NULL;
321 gravity_print_banner ();
322 cpl_msg_set_time_on();
323 cpl_msg_set_component_on();
324 gravi_msg_function_start(1);
328 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
329 cpl_error_get_code()) ;
332 dispvis_frameset = gravi_frameset_extract_dispvis_data (frameset);
335 used_frameset = cpl_frameset_new();
338 if (cpl_frameset_is_empty (dispvis_frameset)){
341 wavelampcalib_frameset = gravi_frameset_extract_wavelamp_map (frameset);
342 p2vmcalib_frameset = gravi_frameset_extract_p2vm_map (frameset);
343 wavecalib_frameset = gravi_frameset_extract_wave_map (frameset);
344 flatcalib_frameset = gravi_frameset_extract_flat_map (frameset);
345 badcalib_frameset = gravi_frameset_extract_bad_map (frameset);
347 darkcalib_frameset = gravi_frameset_extract_dark_map (frameset);
348 dark_frameset = gravi_frameset_extract_dark_data (frameset);
350 disp_frameset = gravi_frameset_extract_disp_data (frameset);
353 if ( cpl_frameset_is_empty(p2vmcalib_frameset) ||
354 cpl_frameset_is_empty(wavecalib_frameset) ||
355 cpl_frameset_is_empty(flatcalib_frameset) ||
356 cpl_frameset_is_empty(badcalib_frameset) ||
357 ( cpl_frameset_is_empty(dark_frameset) &&
358 cpl_frameset_is_empty(darkcalib_frameset) ) ||
359 cpl_frameset_is_empty(disp_frameset) ||
360 cpl_frameset_is_empty(wavelampcalib_frameset) ) {
362 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
363 "Missing P2VM, DARK, BAD, WAVE, WAVELAMP, FLAT or DISP frames") ;
371 if (!cpl_frameset_is_empty (dark_frameset)) {
373 frame = cpl_frameset_get_position (dark_frameset, 0);
374 data = gravi_data_load_rawframe (frame, used_frameset);
375 gravi_data_detector_cleanup (data, parlist);
378 dark_map = gravi_compute_dark (data);
379 FREE (gravi_data_delete, data);
381 CPLCHECK_CLEAN (
"Could not compute the DARK map");
384 gravi_data_save_new (dark_map, frameset, NULL, parlist,
385 NULL, frame,
"gravi_single",
386 NULL, GRAVI_DARK_MAP);
388 CPLCHECK_CLEAN (
"Could not save the DARK map");
390 else if (!cpl_frameset_is_empty (darkcalib_frameset)) {
392 frame = cpl_frameset_get_position (darkcalib_frameset, 0);
393 dark_map = gravi_data_load_frame (frame, used_frameset);
395 CPLCHECK_CLEAN (
"Could not load the DARK map");
398 cpl_msg_error (cpl_func,
"There is no DARK in the frame set");
401 frame = cpl_frameset_get_position (badcalib_frameset, 0);
402 badpix_map = gravi_data_load_frame (frame, used_frameset);
405 frame = cpl_frameset_get_position (flatcalib_frameset, 0);
406 profile_map = gravi_data_load_frame (frame, used_frameset);
409 frame = cpl_frameset_get_position (wavecalib_frameset, 0);
410 wave_map = gravi_data_load_frame (frame, used_frameset);
413 frame = cpl_frameset_get_position (p2vmcalib_frameset, 0);
414 p2vm_map = gravi_data_load_frame (frame, used_frameset);
416 CPLCHECK_CLEAN (
"Error while loading the calibration map");
423 nb_frame = cpl_frameset_get_size (disp_frameset);
424 for (
int ivis = 0; ivis < nb_frame; ivis++) {
425 current_frameset = cpl_frameset_duplicate (used_frameset);
427 cpl_msg_info (cpl_func,
"*** Processing file %d over %d *** ", ivis+1, nb_frame);
429 frame = cpl_frameset_get_position (disp_frameset, ivis);
430 data = gravi_data_load_rawframe (frame, current_frameset);
431 gravi_data_detector_cleanup (data, parlist);
434 preproc_data = gravi_extract_spectrum (data, profile_map, dark_map,
435 badpix_map, NULL, parlist);
436 CPLCHECK_CLEAN (
"Cannot extract spectrum");
439 gravi_align_spectrum (preproc_data, wave_map, p2vm_map);
440 CPLCHECK_CLEAN (
"Cannot re-interpolate spectrum");
443 gravi_data_move_ext (preproc_data, data, GRAVI_ARRAY_GEOMETRY_EXT);
444 gravi_data_move_ext (preproc_data, data, GRAVI_OPTICAL_TRAIN_EXT);
445 gravi_data_move_ext (preproc_data, data, GRAVI_OPDC_EXT);
446 gravi_data_move_ext (preproc_data, data, GRAVI_FDDL_EXT);
447 gravi_data_move_ext (preproc_data, data, GRAVI_METROLOGY_EXT);
448 CPLCHECK_CLEAN (
"Cannot move ext");
450 FREE (gravi_data_delete,data);
453 if (gravi_param_get_bool (parlist,
"gravity.dfs.preproc-file")) {
455 gravi_data_save_new (preproc_data, frameset, NULL, parlist,
456 current_frameset, frame,
"gravity_disp",
457 NULL, GRAVI_PREPROC);
459 CPLCHECK_CLEAN (
"Could not save the preproc data");
464 p2vmred_data = gravi_compute_p2vmred (preproc_data, p2vm_map,
"gravi_single", parlist);
465 CPLCHECK_CLEAN (
"Cannot apply p2vm to the preproc data");
468 gravi_data_move_ext (p2vmred_data, preproc_data, GRAVI_METROLOGY_EXT);
469 gravi_data_move_ext (p2vmred_data, preproc_data, GRAVI_FDDL_EXT);
470 gravi_data_move_ext (p2vmred_data, preproc_data, GRAVI_OPDC_EXT);
471 FREE (gravi_data_delete, preproc_data);
472 CPLCHECK_CLEAN (
"Cannot delete preproc");
475 gravi_compute_opdc_state (p2vmred_data);
476 CPLCHECK_CLEAN (
"Cannot reduce OPDC");
479 gravi_metrology_reduce (p2vmred_data);
480 CPLCHECK_CLEAN (
"Cannot reduce metrology");
483 gravi_compute_snr (p2vmred_data, parlist);
484 CPLCHECK_MSG (
"Cannot compute SNR");
487 gravi_compute_signals (p2vmred_data, disp_map, parlist);
488 CPLCHECK_MSG (
"Cannot compute signals");
491 gravi_compute_rejection (p2vmred_data, parlist);
492 CPLCHECK_MSG (
"Cannot rejection flags signals");
496 tmpvis_data = gravi_compute_vis (p2vmred_data, parlist);
497 CPLCHECK_CLEAN (
"Cannot average the visibilities");
500 if (gravi_param_get_bool (parlist,
"gravity.dfs.vis-file")) {
502 gravi_data_save_new (tmpvis_data, frameset, NULL, parlist,
503 current_frameset, frame,
"gravity_disp",
504 NULL, GRAVI_VIS_SINGLE_CALIB);
506 CPLCHECK_CLEAN (
"Cannot save the VIS product");
510 if (gravi_param_get_bool (parlist,
"gravity.dfs.p2vmred-file")) {
512 gravi_data_save_new (p2vmred_data, frameset, NULL, parlist,
513 current_frameset, frame,
"gravity_disp",
514 NULL, GRAVI_P2VMRED_SINGLE_CALIB);
516 CPLCHECK_CLEAN (
"Cannot save the P2VMREDUCED product");
522 vis_data = tmpvis_data; tmpvis_data = NULL;
525 cpl_msg_info (cpl_func,
"Merge with previous OI_VIS");
526 gravi_data_append (vis_data, tmpvis_data, 1);
527 FREE (gravi_data_delete, tmpvis_data);
529 CPLCHECK_CLEAN (
"Cannot merge the visibilities");
531 cpl_msg_info (cpl_func,
"Free the p2vmreduced");
532 FREE (cpl_frameset_delete, current_frameset);
533 FREE (gravi_data_delete, p2vmred_data);
539 gravi_vis_mjd_to_time (vis_data);
542 frame = cpl_frameset_get_position (wavelampcalib_frameset, 0);
543 argon_data = gravi_data_load_frame (frame, used_frameset);
546 gravi_data_copy_ext (vis_data, argon_data,
"POS_ARGON");
549 cpl_frameset_join (used_frameset, disp_frameset);
550 frame = cpl_frameset_get_position (disp_frameset, 0);
552 gravi_data_save_new (vis_data, frameset, NULL, parlist,
553 used_frameset, frame,
"gravity_disp",
554 NULL, GRAVI_DISP_VIS);
556 CPLCHECK_CLEAN(
"Could not save the VIS_SINGLE product");
558 FREE (gravi_data_delete, profile_map);
559 FREE (gravi_data_delete, dark_map);
560 FREE (gravi_data_delete, wave_map);
561 FREE (gravi_data_delete, badpix_map);
562 FREE (gravi_data_delete, p2vm_map);
563 FREE (cpl_frameset_delete, darkcalib_frameset);
564 FREE (cpl_frameset_delete, wavecalib_frameset);
565 FREE (cpl_frameset_delete, dark_frameset);
566 FREE (cpl_frameset_delete, flatcalib_frameset);
567 FREE (cpl_frameset_delete, badcalib_frameset);
568 FREE (cpl_frameset_delete, p2vmcalib_frameset);
569 FREE (cpl_frameset_delete, wavelampcalib_frameset);
574 frame = cpl_frameset_get_position (dispvis_frameset, 0);
575 vis_data = gravi_data_load_frame (frame, used_frameset);
577 CPLCHECK_CLEAN (
"Cannot load the DISP_VIS file");
579 disp_frameset = cpl_frameset_duplicate (dispvis_frameset);
586 gravi_disp_cleanup (vis_data);
588 disp_map = gravi_compute_disp (vis_data);
590 CPLCHECK_CLEAN (
"Error while computing the disp_map");
593 frame = cpl_frameset_get_position (disp_frameset, 0);
596 gravi_data_save_new (disp_map, frameset, NULL, parlist,
597 used_frameset, frame,
"gravity_disp",
598 NULL, GRAVI_DISP_MODEL);
600 CPLCHECK_CLEAN(
"Could not save the DISP_MODEL product");
606 cpl_msg_info(cpl_func,
"Memory cleanup");
607 FREE (gravi_data_delete, data);
608 FREE (gravi_data_delete, tmpvis_data);
609 FREE (gravi_data_delete, vis_data);
610 FREE (gravi_data_delete, disp_map);
611 FREE (gravi_data_delete, dark_map);
612 FREE (gravi_data_delete, wave_map);
613 FREE (gravi_data_delete, profile_map);
614 FREE (gravi_data_delete, badpix_map);
615 FREE (gravi_data_delete, p2vmred_data);
616 FREE (gravi_data_delete, p2vm_map);
617 FREE (gravi_data_delete, preproc_data);
618 FREE (cpl_frameset_delete, disp_frameset);
619 FREE (cpl_frameset_delete, dispvis_frameset);
620 FREE (cpl_frameset_delete, darkcalib_frameset);
621 FREE (cpl_frameset_delete, dark_frameset);
622 FREE (cpl_frameset_delete, wavecalib_frameset);
623 FREE (cpl_frameset_delete, badcalib_frameset);
624 FREE (cpl_frameset_delete, flatcalib_frameset);
625 FREE (cpl_frameset_delete, p2vmcalib_frameset);
626 FREE (cpl_frameset_delete, wavelampcalib_frameset);
627 FREE (cpl_frameset_delete, used_frameset);
628 FREE (cpl_frameset_delete, current_frameset);
630 gravi_msg_function_exit(1);
631 return (
int)cpl_error_get_code();