40 #include "gravi_utils.h"
41 #include "gravi_pfits.h"
42 #include "gravi_dfs.h"
43 #include "gravi_calib.h"
44 #include "gravi_vis.h"
45 #include "gravi_data.h"
54 static int gravi_single_create(cpl_plugin *);
55 static int gravi_single_exec(cpl_plugin *);
56 static int gravi_single_destroy(cpl_plugin *);
57 static int gravi_single(cpl_frameset *,
const cpl_parameterlist *);
63 static char gravi_single_description[] =
64 "This recipe is associated to the observing template in single field mode.\n"
65 "Its aim is to reduce the raw interferometric data acquired on calibrator or \n"
66 "science targets and to compute the intrumental visibility.\n"
67 "The output OIFITS file contains the values of the complex visibilities, \n"
68 "squared visibilities and the closure phases.\n"
69 "Description DO category\n"
71 " Master dark " DARK
"\n"
72 " Master flat " FLAT
"\n"
73 " Bad pixel map " BAD
"\n"
74 " Master wave " WAVE
"\n"
75 " Master p2vm " P2VM
"\n"
76 " Raw observation file " GRAVI_SINGLE_CALIB
" or "
77 GRAVI_SINGLE_SCIENCE
"\n"
79 " Sky file (one or more) " SINGLE_SCI_SKY_RAW
" or "
80 SINGLE_CAL_SKY_RAW
"\n"
83 " Master sky (one per input sky) " SKY
"\n"
84 " Raw Visibilities " VIS_SINGLE_CALIB
" or " VIS_SINGLE_SCIENCE
"\n"
86 " Preprocessed files (--preproc-file) " PREPROC
"\n"
87 " Non averaged visibilities (--p2vmreduced-file) " P2VMREDUCED
"\n"
105 int cpl_plugin_get_info(cpl_pluginlist * list)
107 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
108 cpl_plugin * plugin = &recipe->interface;
110 if (cpl_plugin_init(plugin,
112 GRAVI_BINARY_VERSION,
113 CPL_PLUGIN_TYPE_RECIPE,
115 "This recipe is used to compute the squared visibility",
116 gravi_single_description,
117 "Firstname Lastname",
122 gravi_single_destroy)) {
123 cpl_msg_error(cpl_func,
"Plugin initialization failed");
124 (void)cpl_error_set_where(cpl_func);
128 if (cpl_pluginlist_append(list, plugin)) {
129 cpl_msg_error(cpl_func,
"Error adding plugin to list");
130 (void)cpl_error_set_where(cpl_func);
146 static int gravi_single_create(cpl_plugin * plugin)
152 if (cpl_error_get_code() != CPL_ERROR_NONE) {
153 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
154 cpl_func, __LINE__, cpl_error_get_where());
155 return (
int)cpl_error_get_code();
158 if (plugin == NULL) {
159 cpl_msg_error(cpl_func,
"Null plugin");
160 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
164 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
165 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
166 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
170 recipe = (cpl_recipe *)plugin;
173 recipe->parameters = cpl_parameterlist_new();
174 if (recipe->parameters == NULL) {
175 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
176 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
182 p = cpl_parameter_new_value(
"gravi.preproc_param.preproc_file",
183 CPL_TYPE_BOOL,
"Save the preprocessed file",
"gravi.preproc_param", FALSE);
184 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"preproc-file");
185 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
186 cpl_parameterlist_append(recipe->parameters, p);
189 p = cpl_parameter_new_value(
"gravi.preproc_param.p2vmreduced_file",
190 CPL_TYPE_BOOL,
"Save the file with non averaged visibilities",
"gravi.preproc_param", FALSE);
191 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"p2vmreduced-file");
192 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
193 cpl_parameterlist_append(recipe->parameters, p);
196 p = cpl_parameter_new_value(
"gravi.vis_reduce.vis_correction",
197 CPL_TYPE_BOOL,
"Correction of visibility losses",
"gravi.vis_reduce", FALSE);
198 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"vis-correction");
199 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
200 cpl_parameterlist_append(recipe->parameters, p);
203 p = cpl_parameter_new_value(
"gravi.snr_threshold_ft",
204 CPL_TYPE_DOUBLE,
"SNR threshold for fringe detection in FT (>0)",
"gravi.vis_reduce", 3.0);
205 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"snr-threshold-ft");
206 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
207 cpl_parameterlist_append(recipe->parameters, p);
210 p = cpl_parameter_new_value(
"gravi.state_threshold_ft",
211 CPL_TYPE_DOUBLE,
"STATE threshold for fringe detection in FT (>=0)",
"gravi.vis_reduce", 1.0);
212 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"state-threshold-ft");
213 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
214 cpl_parameterlist_append(recipe->parameters, p);
217 p = cpl_parameter_new_value(
"gravi.fringedet_threshold_sc",
218 CPL_TYPE_DOUBLE,
"Fringe-detection ratio threshold to accept SC frame (0..1)",
"gravi.vis_reduce", 0.8);
219 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"fringedet-threshold-sc");
220 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
221 cpl_parameterlist_append(recipe->parameters, p);
224 p = cpl_parameter_new_value(
"gravi.vfactor_threshold_sc",
225 CPL_TYPE_DOUBLE,
"vFactor threshold to accept SC frame (0..1)",
"gravi.vis_reduce", 0.1);
226 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"vfactor-threshold-sc");
227 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
228 cpl_parameterlist_append(recipe->parameters, p);
231 p = cpl_parameter_new_value(
"gravi.debias_sc",
232 CPL_TYPE_BOOL,
"Subtract the V2 bias from SC",
"gravi", TRUE);
233 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"debias-sc");
234 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
235 cpl_parameterlist_append(recipe->parameters, p);
238 p = cpl_parameter_new_value(
"gravi.debias_ft",
239 CPL_TYPE_BOOL,
"Subtract the V2 bias from FT",
"gravi", TRUE);
240 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"debias-ft");
241 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
242 cpl_parameterlist_append(recipe->parameters, p);
245 p = cpl_parameter_new_value(
"gravi.use_met",
246 CPL_TYPE_BOOL,
"Use the metrology to rephase the SC data in real-time",
"gravi", FALSE);
247 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"use-met");
248 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
249 cpl_parameterlist_append(recipe->parameters, p);
252 p = cpl_parameter_new_value(
"gravi.vis_flat",
253 CPL_TYPE_BOOL,
"Flat the OIVIS by the VIS_FLAT file",
"gravi", TRUE);
254 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"vis-flat");
255 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
256 cpl_parameterlist_append(recipe->parameters, p);
268 static int gravi_single_exec(cpl_plugin * plugin)
273 cpl_errorstate initial_errorstate = cpl_errorstate_get();
276 if (cpl_error_get_code() != CPL_ERROR_NONE) {
277 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
278 cpl_func, __LINE__, cpl_error_get_where());
279 return (
int)cpl_error_get_code();
282 if (plugin == NULL) {
283 cpl_msg_error(cpl_func,
"Null plugin");
284 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
288 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
289 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
290 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
294 recipe = (cpl_recipe *)plugin;
297 if (recipe->parameters == NULL) {
298 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
299 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
301 if (recipe->frames == NULL) {
302 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
303 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
307 recipe_status = gravi_single(recipe->frames, recipe->parameters);
311 if (cpl_dfs_update_product_header(recipe->frames)) {
313 recipe_status = (int)cpl_error_get_code();
317 if (!cpl_errorstate_is_equal(initial_errorstate)) {
320 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
323 return recipe_status;
333 static int gravi_single_destroy(cpl_plugin * plugin)
337 if (plugin == NULL) {
338 cpl_msg_error(cpl_func,
"Null plugin");
339 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
343 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
344 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
345 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
349 recipe = (cpl_recipe *)plugin;
351 cpl_parameterlist_delete(recipe->parameters);
366 static int gravi_single(cpl_frameset * frameset,
367 const cpl_parameterlist * parlist)
369 cpl_frameset * recipe_frameset=NULL, * wave_frameset=NULL, * dark_frameset=NULL,
370 * darkcalib_frameset=NULL, * skyframeset=NULL,
371 * gain_frameset=NULL, * p2vm_frameset=NULL, * badpix_frameset=NULL, *used_frameset=NULL,
372 * visflat_frameset=NULL;
373 cpl_propertylist * applist=NULL, * primary_hdr=NULL, * primary_hdr_dark=NULL, * wave_plist=NULL, *bad_primary_hdr=NULL;
374 cpl_table * detector_table=NULL;
375 cpl_frame * frame=NULL;
376 const cpl_parameter * p=NULL, * p_=NULL;
377 cpl_parameter * par=NULL;
378 char * out_dark=NULL, * preproc_name=NULL;
379 const char * frame_tag=NULL, * filename=NULL, * temp=NULL;
380 gravi_data * p2vm_map=NULL, * data=NULL, * darkOrSky_map=NULL, * wave_map=NULL, * dark_map=NULL,
381 * profile_map=NULL, * badpix_map=NULL, * vis_flat=NULL;
382 gravi_data ** calib_datas=NULL;
383 gravi_data * preproc_data=NULL;
384 gravi_data ** raw_data=NULL, * p2vm_reduce=NULL;
385 gravi_data * oi_vis=NULL;
386 darkOrSky_map = NULL, wave_map = NULL;
387 int nb_frame, i, j, nwave_ft, nwave_sc, n_region;
391 char * vis_name=NULL;
394 cpl_msg_set_time_on();
395 cpl_msg_set_component_on();
396 cpl_msg_info(cpl_func,
"Start function -- cleanup");
403 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
404 cpl_error_get_code()) ;
406 p2vm_frameset = gravi_frameset_extract_p2vm_file(frameset);
409 recipe_frameset = gravi_frameset_extract_single_data(frameset);
410 darkcalib_frameset = gravi_frameset_extract_dark_file(frameset);
411 wave_frameset = gravi_frameset_extract_wave_file(frameset);
412 dark_frameset = gravi_frameset_extract_dark(frameset);
413 gain_frameset = gravi_frameset_extract_flat_file(frameset);
414 badpix_frameset = gravi_frameset_extract_badpix(frameset);
415 skyframeset = gravi_frameset_extract_single_sky_data (frameset);
416 visflat_frameset = gravi_frameset_extract_visflat(frameset);
421 if (cpl_frameset_is_empty(recipe_frameset)) {
422 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
423 "No SINGLE frame on the frameset");
429 if ((cpl_frameset_is_empty(p2vm_frameset)) ||
430 (cpl_frameset_is_empty(wave_frameset)) ||
431 (cpl_frameset_is_empty(gain_frameset))) {
432 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
433 "No P2VM or FLAT or WAVE file on the frameset") ;
438 nb_frame = cpl_frameset_get_size(recipe_frameset);
444 if (!cpl_frameset_is_empty(dark_frameset)) {
446 frame = cpl_frameset_get_position(dark_frameset, 0);
447 filename = cpl_frame_get_filename(frame);
449 cpl_msg_info (cpl_func,
"File %s is a raw DARK file", FILESHORT(filename));
450 data = gravi_data_load(filename);
453 primary_hdr = gravi_data_get_propertylist(data, GRAVI_PRIMARY_HDR_NAME_EXT);
454 shutter = gravi_shutters_check(primary_hdr);
455 if (!((shutter[0] == 0) && (shutter[1] == 0) &&
456 (shutter[2] == 0) && (shutter[3] == 0))){
457 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
"Shutter problem in the DARK");
462 dark_map = gravi_compute_dark(data);
464 CPLCHECK_CLEAN(
"Could not compute the DARK map");
467 primary_hdr_dark = gravi_data_get_propertylist(dark_map,
468 GRAVI_PRIMARY_HDR_NAME_EXT);
469 applist = gravi_propertylist_get_qc (primary_hdr_dark);
470 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, DARK);
473 out_dark = gravi_data_product_name (filename,
"dark");
475 gravi_data_save(dark_map, frameset, out_dark, parlist,
476 dark_frameset, frame,
"gravi_single", applist);
478 CPLCHECK_CLEAN(
"Could not save the DARK map");
480 FREE (cpl_propertylist_delete,applist);
481 FREE (gravi_data_delete,data);
482 FREE (cpl_free,shutter);
486 else if (!cpl_frameset_is_empty(darkcalib_frameset)) {
488 frame = cpl_frameset_get_position(darkcalib_frameset, 0);
489 filename = cpl_frame_get_filename(frame);
491 cpl_msg_info (cpl_func,
"File %s is a master DARK file already computed", FILESHORT(filename));
492 dark_map = gravi_data_load(filename);
493 cpl_frameset_insert (dark_frameset, cpl_frame_duplicate (frame));
498 cpl_msg_info (cpl_func,
"There is no DARK in the frame set");
504 if (!cpl_frameset_is_empty(skyframeset)) {
507 frame = cpl_frameset_get_position(skyframeset, 0);
508 filename = cpl_frame_get_filename(frame);
510 cpl_msg_info (cpl_func,
"File %s is a raw SKY file", FILESHORT(filename));
512 data = gravi_data_load(filename);
513 primary_hdr = gravi_data_get_propertylist(data, GRAVI_PRIMARY_HDR_NAME_EXT);
516 darkOrSky_map = gravi_compute_dark(data);
517 FREE (gravi_data_delete,data);
519 CPLCHECK_CLEAN(
"Error while computing the darkOrSky_map");
522 cpl_frameset_insert (dark_frameset, cpl_frame_duplicate (frame));
524 primary_hdr_dark = gravi_data_get_propertylist(darkOrSky_map,
525 GRAVI_PRIMARY_HDR_NAME_EXT);
527 applist = gravi_propertylist_get_qc (primary_hdr_dark);
529 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, SKY);
532 out_dark = gravi_data_product_name (filename,
"sky");
534 gravi_data_save(darkOrSky_map, frameset, out_dark, parlist,
535 skyframeset, frame,
"gravi_single", applist);
537 CPLCHECK_CLEAN(
"Could not save the darkOrSky_map");
539 FREE (cpl_propertylist_delete,applist);
540 FREE (cpl_free,out_dark);
542 else if (testDarkSky) {
543 darkOrSky_map = dark_map;
546 cpl_msg_info (cpl_func,
"There is no sky in the frame set");
549 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
"No dark or sky frames in the frameset");
553 FREE (cpl_frameset_delete,skyframeset);
559 if (!cpl_frameset_is_empty(badpix_frameset)){
562 frame = cpl_frameset_get_position(badpix_frameset, 0);
563 filename = cpl_frame_get_filename(frame);
565 cpl_msg_info (cpl_func,
"File %s is a BAD pixel map", FILESHORT(filename));
566 badpix_map = gravi_data_load(filename);
570 badpix_map = gravi_compute_badpix(darkOrSky_map, parlist);
572 CPLCHECK_CLEAN(
"Could not compute the BAD pixel map");
575 bad_primary_hdr = gravi_data_get_propertylist (badpix_map, GRAVI_PRIMARY_HDR_NAME_EXT);
576 applist = gravi_propertylist_get_qc (bad_primary_hdr);
578 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, BAD);
580 frame = cpl_frameset_get_position (dark_frameset, 0);
582 gravi_data_save (badpix_map, frameset,
"gravi_bad_map.fits", parlist,
583 dark_frameset, frame,
"gravi_single", applist);
585 CPLCHECK_CLEAN(
"Could not save the BAD pixel map");
587 FREE (cpl_propertylist_delete, applist);
594 frame = cpl_frameset_get_position(gain_frameset, 0);
595 filename = cpl_frame_get_filename(frame);
597 cpl_msg_info (cpl_func,
"File %s is a master FLAT already computed", FILESHORT(filename));
598 profile_map = gravi_data_load(filename);
604 frame = cpl_frameset_get_position(wave_frameset, 0);
605 filename = cpl_frame_get_filename(frame);
607 cpl_msg_info (cpl_func,
"File %s is a master WAVE map already computed", FILESHORT(filename));
608 wave_map = gravi_data_load(filename);
611 if ((wave_map == NULL) || (profile_map == NULL)) {
612 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
"No WAVE or FLAT frames in the frameset");
620 frame = cpl_frameset_get_position(p2vm_frameset, 0);
621 filename = cpl_frame_get_filename(frame);
623 cpl_msg_info (cpl_func,
"File %s is a P2VM file already computed", FILESHORT(filename));
624 p2vm_map = gravi_data_load (filename);
626 CPLCHECK_CLEAN(
"Error while loading the P2VM map");
631 if (!cpl_frameset_is_empty(visflat_frameset))
633 frame = cpl_frameset_get_position(visflat_frameset, 0);
634 filename = cpl_frame_get_filename(frame);
635 cpl_msg_info (cpl_func,
"File %s is a VIS_FLAT already computed", FILESHORT(filename));
636 vis_flat = gravi_data_load(filename);
644 detector_table = gravi_data_get_table (p2vm_map, GRAVI_IMAGING_DETECTOR_FT_EXT);
645 n_region = cpl_table_get_nrow(detector_table);
647 wave_plist = gravi_data_get_oi_propertylist
648 (p2vm_map, GRAVI_OI_WAVE_SWITCH(GRAVI_TYPE_SC),
649 GRAVI_SPECTRO_SWITCH(GRAVI_TYPE_SC,0,2));
650 nwave_sc = gravi_pfits_get_nwave (wave_plist);
651 wave_plist = gravi_data_get_oi_propertylist (p2vm_map,
652 GRAVI_OI_WAVE_SWITCH(GRAVI_TYPE_FT),
653 GRAVI_SPECTRO_SWITCH(GRAVI_TYPE_FT,0,2));
654 nwave_ft = gravi_pfits_get_nwave (wave_plist);
657 wave_plist = gravi_data_get_oi_propertylist
658 (p2vm_map, GRAVI_OI_WAVE_SWITCH(GRAVI_TYPE_SC), GRAVI_SPECTRO_SWITCH(GRAVI_TYPE_SC,0,1));
659 nwave_sc = gravi_pfits_get_nwave (wave_plist);
660 wave_plist = gravi_data_get_oi_propertylist (p2vm_map,
661 GRAVI_OI_WAVE_SWITCH(GRAVI_TYPE_FT), GRAVI_SPECTRO_SWITCH(GRAVI_TYPE_FT,0,1));
662 nwave_ft = gravi_pfits_get_nwave (wave_plist);
665 primary_hdr = gravi_data_get_propertylist (wave_map, GRAVI_PRIMARY_HDR_NAME_EXT);
667 CPLCHECK_CLEAN(
"Missed one the preproc parameters");
676 calib_datas = cpl_malloc(nb_calfile *
sizeof(gravi_data *));
678 calib_datas[0] = darkOrSky_map;
679 calib_datas[1] = wave_map;
680 calib_datas[2] = profile_map;
681 calib_datas[3] = badpix_map;
683 calib_datas[4] = dark_map;
686 used_frameset=cpl_frameset_new();
687 cpl_frameset_join(used_frameset, cpl_frameset_duplicate(dark_frameset));
688 cpl_frameset_join(used_frameset, cpl_frameset_duplicate(badpix_frameset));
689 cpl_frameset_join(used_frameset, cpl_frameset_duplicate(wave_frameset));
690 cpl_frameset_join(used_frameset, cpl_frameset_duplicate(gain_frameset));
691 cpl_frameset_join(used_frameset, cpl_frameset_duplicate(p2vm_frameset));
693 p = cpl_parameterlist_find_const(parlist,
"gravi.preproc_param.preproc_file");
695 p_ = cpl_parameterlist_find_const(parlist,
"gravi.preproc_param.p2vmreduced_file");
701 oi_vis = gravi_data_new(0);
702 primary_hdr = gravi_data_get_propertylist (p2vm_map,
703 GRAVI_PRIMARY_HDR_NAME_EXT);
705 const char * resol_p2vm = gravi_pfits_get_resolution (primary_hdr);
706 const char * resol_file;
714 for (thread = 0; thread < nb_frame; thread++){
716 frame = cpl_frameset_get_position (recipe_frameset, thread);
717 frame_tag = cpl_frame_get_tag (frame);
718 filename = cpl_frame_get_filename (frame);
720 data = gravi_data_load(filename);
721 primary_hdr = gravi_data_get_propertylist (data,
722 GRAVI_PRIMARY_HDR_NAME_EXT);
726 shutter = gravi_shutters_check(primary_hdr);
727 resol_file = gravi_pfits_get_resolution (primary_hdr);
730 if ((shutter [0] != 1) || (shutter [1] != 1) ||
731 (shutter [2] != 1) || (shutter [3] != 1)) {
732 cpl_msg_warning( cpl_func,
"RAW data shutters: %d-%d-%d-%d",
733 shutter[0], shutter[1], shutter[2], shutter[3] );
736 cpl_msg_info (cpl_func,
"Preprocessing of the single"
737 " field file %s", FILESHORT(filename));
739 preproc_data = gravi_preproc(data, calib_datas, nb_calfile, p2vm_map, parlist);
741 FREE (gravi_data_delete,data);
742 FREE (cpl_free,shutter);
744 CPLCHECK_CLEAN(
"Cannot preproc the data");
747 if (cpl_parameter_get_bool(p)){
749 preproc_name = gravi_data_product_name (filename,
"preproc");
750 cpl_frameset * preproc_frame = cpl_frameset_new ();
751 cpl_frameset_insert (preproc_frame, cpl_frame_duplicate (frame));
752 cpl_frameset_join(preproc_frame, cpl_frameset_duplicate(used_frameset));
754 applist = gravi_propertylist_get_qc (gravi_data_get_propertylist
755 (preproc_data, GRAVI_PRIMARY_HDR_NAME_EXT));
757 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, PREPROC);
759 gravi_data_save(preproc_data, frameset, preproc_name, parlist,
760 preproc_frame, frame,
"gravi_single", applist);
762 CPLCHECK_CLEAN(
"Cannot save the PREPROC product");
764 FREE (cpl_free,preproc_name);
765 FREE (cpl_propertylist_delete,applist);
766 FREE (cpl_frameset_delete,preproc_frame);
771 cpl_msg_info (cpl_func,
"Application of the p2vm to file %s", FILESHORT(filename));
772 p2vm_reduce = gravi_p2vm_reduce (preproc_data, p2vm_map,
"gravi_single", parlist);
773 FREE (gravi_data_delete,preproc_data);
775 CPLCHECK_CLEAN(
"Cannot apply p2vm to the preproc data");
780 gravi_compute_tau0 (p2vm_reduce);
784 cpl_msg_info (cpl_func,
"Computing the average visibilities of file %s", FILESHORT(filename));
785 gravi_vis_reduce (oi_vis, p2vm_reduce, nb_vis, parlist,
"gravi_single");
787 CPLCHECK_CLEAN(
"Cannot average the visibilities");
791 cpl_msg_info( cpl_func,
"Compute the QC TF parameters" );
792 cpl_errorstate prestate = cpl_errorstate_get();
794 gravi_data * oi_tf = gravi_compute_tf( oi_vis );
796 cpl_propertylist_copy_property_regexp( gravi_data_get_propertylist (oi_vis, GRAVI_PRIMARY_HDR_NAME_EXT),
797 gravi_data_get_propertylist (oi_tf, GRAVI_PRIMARY_HDR_NAME_EXT),
799 FREE (gravi_data_delete,oi_tf);
802 if( cpl_error_get_code() ) {
803 cpl_msg_warning(cpl_func,
"Cannot compute the QC TF parameters for this observation");
804 cpl_errorstate_dump (prestate, 0, NULL);
809 if (cpl_parameter_get_bool(p_)){
810 cpl_msg_info( cpl_func,
"Save the p2vmreduced file" );
813 char * p2vm_name = gravi_data_product_name (filename,
"p2vmreduced");
815 cpl_frameset * p2vmreduced_frame = cpl_frameset_new ();
816 cpl_frameset_insert (p2vmreduced_frame, cpl_frame_duplicate (frame));
817 cpl_frameset_join(p2vmreduced_frame, cpl_frameset_duplicate(used_frameset));
818 applist = gravi_propertylist_get_qc (gravi_data_get_propertylist
819 (p2vm_reduce, GRAVI_PRIMARY_HDR_NAME_EXT));
821 if ((strcmp(frame_tag, SINGLE_CAL_SKY_RAW) == 0) ||
822 (strcmp(frame_tag, SINGLE_SCI_SKY_RAW) == 0))
823 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, SKYREDUCED);
825 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, P2VMREDUCED);
827 gravi_data_save(p2vm_reduce, frameset, p2vm_name, parlist,
828 p2vmreduced_frame, frame,
"gravi_single", applist);
830 CPLCHECK_CLEAN(
"Cannot save the P2VMREDUCED product");
832 FREE (cpl_free,p2vm_name);
833 FREE (cpl_propertylist_delete,applist);
834 FREE (cpl_frameset_delete,p2vmreduced_frame);
838 cpl_msg_info(cpl_func,
"Free the p2vmreduced");
839 FREE (gravi_data_delete,p2vm_reduce);
845 if (gravi_param_get_bool(parlist,
"gravi.vis_flat")) {
848 cpl_msg_info (cpl_func,
"Perform the visibility flat with the provided VIS_FLAT file");
849 gravi_flat_vis (oi_vis, vis_flat);
850 CPLCHECK_CLEAN(
"Cannot flat the OI_VIS");
854 cpl_msg_warning (cpl_func,
"vis-flat option requested but no VIS_FLAT in the frameset");
861 frame = cpl_frameset_get_position (recipe_frameset, 0);
862 frame_tag = cpl_frame_get_tag (frame);
863 primary_hdr = gravi_data_get_propertylist(oi_vis,
864 GRAVI_PRIMARY_HDR_NAME_EXT);
865 applist = gravi_propertylist_get_qc (primary_hdr);
867 cpl_propertylist_append_int(applist, GRAVI_NIGHT_OBS,
868 cpl_propertylist_get_int (primary_hdr, GRAVI_NIGHT_OBS));
869 cpl_propertylist_append_string(applist,
"DATE-OBS",
870 cpl_propertylist_get_string (primary_hdr,
"DATE-OBS"));
873 if ((strcmp(frame_tag, GRAVI_SINGLE_CALIB) == 0)) {
874 vis_name = gravi_data_product_name (filename,
"singlecal");
875 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, VIS_SINGLE_CALIB);
877 else if ((strcmp(frame_tag, GRAVI_SINGLE_SCIENCE) == 0)) {
878 vis_name = gravi_data_product_name (filename,
"singlesci");
879 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, VIS_SINGLE_SCIENCE);
882 vis_name = gravi_data_product_name (filename,
"singlesci");
883 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, VIS_SINGLE_SCIENCE);
886 cpl_frameset_join (recipe_frameset, cpl_frameset_duplicate (used_frameset));
888 gravi_data_save(oi_vis, frameset, vis_name, parlist,
889 recipe_frameset, frame,
"gravi_single", applist);
891 CPLCHECK_CLEAN(
"Cannot save the VIS product");
899 cpl_msg_info(cpl_func,
"Memory cleanup");
901 FREE (gravi_data_delete,dark_map);
902 FREE (gravi_data_delete,data);
903 FREE (cpl_free,shutter);
904 FREE (cpl_free,preproc_name);
905 FREE (gravi_data_delete,preproc_data);
906 FREE (gravi_data_delete,profile_map);
907 FREE (gravi_data_delete,darkOrSky_map);
908 FREE (gravi_data_delete,wave_map);
909 FREE (gravi_data_delete,badpix_map);
910 FREE (gravi_data_delete,p2vm_map);
911 FREE (gravi_data_delete,vis_flat);
912 FREE (cpl_frameset_delete,darkcalib_frameset);
913 FREE (cpl_frameset_delete,wave_frameset);
914 FREE (cpl_frameset_delete,dark_frameset);
915 FREE (cpl_frameset_delete,gain_frameset);
916 FREE (cpl_frameset_delete,badpix_frameset);
917 FREE (gravi_data_delete,p2vm_reduce);
918 FREE (cpl_free,calib_datas);
919 FREE (cpl_free,vis_name);
920 FREE (cpl_propertylist_delete,applist);
921 FREE (gravi_data_delete,oi_vis);
922 FREE (cpl_frameset_delete,p2vm_frameset);
923 FREE (cpl_frameset_delete,visflat_frameset);
924 FREE (cpl_frameset_delete,recipe_frameset);
925 FREE (cpl_frameset_delete,used_frameset);
927 cpl_msg_info(cpl_func,
"Exit function");
928 return (
int)cpl_error_get_code();