34 #include "gravi_data.h" 35 #include "gravi_pfits.h" 36 #include "gravi_dfs.h" 38 #include "gravi_utils.h" 40 #include "gravi_calib.h" 41 #include "gravi_p2vmred.h" 42 #include "gravi_eop.h" 43 #include "gravi_metrology.h" 45 #include "gravi_signal.h" 46 #include "gravi_vis.h" 49 #include "gravi_preproc.h" 56 static int gravity_vis_from_p2vmred_create(cpl_plugin *);
57 static int gravity_vis_from_p2vmred_exec(cpl_plugin *);
58 static int gravity_vis_from_p2vmred_destroy(cpl_plugin *);
59 static int gravity_vis_from_p2vmred(cpl_frameset *,
const cpl_parameterlist *);
64 static char gravity_vis_from_p2vmred_short[] =
"Compute the visibilities from P2VMRED intermediate product.";
65 static char gravity_vis_from_p2vmred_description[] =
66 "This recipe average the real-time data from input P2VMRED files into a VIS product. It allows to run the reduction with different parameters (for instance for SNR thresholding) without having to re-reduce the files from scratch. Typically the reduction is 4x faster when started from this intermediate product.\n" 68 "The tag in the DO category can be SINGLE/DUAL and CAL/SCI. They should reflect the mode (SINGLE or DUAL) and the DPR.CATG of the observation (SCIENCE or CALIB). The tag in the PRO.CATG category will be SINGLE/DUAL and CAL/SCI depending on the input tag.\n" 69 GRAVI_RECIPE_INPUT
"\n" 70 GRAVI_P2VMRED_SINGLE_SCIENCE
" : Input intermediate product\n" 71 GRAVI_RECIPE_OUTPUT
"\n" 72 GRAVI_VIS_SINGLE_SCIENCE
" : OIFITS with uncalibrated visibilities\n" 90 int cpl_plugin_get_info(cpl_pluginlist * list)
92 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
93 cpl_plugin * plugin = &recipe->interface;
95 if (cpl_plugin_init(plugin,
98 CPL_PLUGIN_TYPE_RECIPE,
99 "gravity_vis_from_p2vmred",
100 gravity_vis_from_p2vmred_short,
101 gravity_vis_from_p2vmred_description,
102 "JB. Le Bouquin, Vincent Lapeyrere, ",
105 gravity_vis_from_p2vmred_create,
106 gravity_vis_from_p2vmred_exec,
107 gravity_vis_from_p2vmred_destroy)) {
108 cpl_msg_error(cpl_func,
"Plugin initialization failed");
109 (void)cpl_error_set_where(cpl_func);
113 if (cpl_pluginlist_append(list, plugin)) {
114 cpl_msg_error(cpl_func,
"Error adding plugin to list");
115 (void)cpl_error_set_where(cpl_func);
131 static int gravity_vis_from_p2vmred_create(cpl_plugin * plugin)
137 if (cpl_error_get_code() != CPL_ERROR_NONE) {
138 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
139 cpl_func, __LINE__, cpl_error_get_where());
140 return (
int)cpl_error_get_code();
143 if (plugin == NULL) {
144 cpl_msg_error(cpl_func,
"Null plugin");
145 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
149 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
150 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
151 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
155 recipe = (cpl_recipe *)plugin;
158 recipe->parameters = cpl_parameterlist_new();
159 if (recipe->parameters == NULL) {
160 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
161 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
168 gravi_parameter_add_static_name (recipe->parameters);
171 gravi_parameter_add_average_vis (recipe->parameters);
174 gravi_parameter_add_compute_snr (recipe->parameters, isCalib);
177 gravi_parameter_add_rejection (recipe->parameters, isCalib);
180 gravi_parameter_add_compute_vis (recipe->parameters, isCalib);
183 p = cpl_parameter_new_value (
"gravity.signal.use-existing-rejection", CPL_TYPE_BOOL,
184 "Use existing rejection flags (ignore related options)",
185 "gravity.signal", FALSE);
186 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"use-existing-rejection");
187 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
188 cpl_parameterlist_append (recipe->parameters, p);
200 static int gravity_vis_from_p2vmred_exec(cpl_plugin * plugin)
205 cpl_errorstate initial_errorstate = cpl_errorstate_get();
209 if (cpl_error_get_code() != CPL_ERROR_NONE) {
210 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
211 cpl_func, __LINE__, cpl_error_get_where());
212 return (
int)cpl_error_get_code();
215 if (plugin == NULL) {
216 cpl_msg_error(cpl_func,
"Null plugin");
217 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
221 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
222 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
223 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
227 recipe = (cpl_recipe *)plugin;
230 if (recipe->parameters == NULL) {
231 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
232 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
234 if (recipe->frames == NULL) {
235 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
236 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
240 recipe_status = gravity_vis_from_p2vmred(recipe->frames, recipe->parameters);
243 if (cpl_dfs_update_product_header(recipe->frames)) {
245 recipe_status = (int)cpl_error_get_code();
249 if (!cpl_errorstate_is_equal(initial_errorstate)) {
252 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
255 return recipe_status;
265 static int gravity_vis_from_p2vmred_destroy(cpl_plugin * plugin)
269 if (plugin == NULL) {
270 cpl_msg_error(cpl_func,
"Null plugin");
271 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
275 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
276 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
277 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
281 recipe = (cpl_recipe *)plugin;
283 cpl_parameterlist_delete(recipe->parameters);
298 static int gravity_vis_from_p2vmred(cpl_frameset * frameset,
299 const cpl_parameterlist * parlist)
301 cpl_frameset * recipe_frameset=NULL, *used_frameset=NULL;
303 cpl_frame * frame=NULL;
305 const char * frame_tag=NULL;
306 char * proCatg = NULL, * mode=NULL;
308 gravi_data * p2vmred_data=NULL, * vis_data=NULL, * tmpvis_data=NULL;
313 gravity_print_banner ();
314 cpl_msg_set_time_on();
315 cpl_msg_set_component_on();
316 gravi_msg_function_start(1);
318 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
319 cpl_error_get_code()) ;
322 recipe_frameset = gravi_frameset_extract_p2vmred_data (frameset);
325 if ( cpl_frameset_get_size (recipe_frameset) < 1 ) {
326 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
327 "Illegal number of P2VMREDUCED file on the frameset");
332 used_frameset = cpl_frameset_new();
338 frame_tag = cpl_frame_get_tag (cpl_frameset_get_position (recipe_frameset, 0));
340 if ((strcmp(frame_tag, GRAVI_P2VMRED_DUAL_CALIB) == 0)) {
341 proCatg = cpl_sprintf (GRAVI_VIS_DUAL_CALIB);
342 mode = cpl_sprintf (
"gravi_dual");
344 else if ((strcmp(frame_tag, GRAVI_P2VMRED_DUAL_SCIENCE) == 0)) {
345 proCatg = cpl_sprintf (GRAVI_VIS_DUAL_SCIENCE);
346 mode = cpl_sprintf (
"gravi_dual");
348 else if ((strcmp(frame_tag, GRAVI_P2VMRED_SINGLE_CALIB) == 0)) {
349 proCatg = cpl_sprintf (GRAVI_VIS_SINGLE_CALIB);
350 mode = cpl_sprintf (
"gravi_single");
352 else if ((strcmp(frame_tag, GRAVI_P2VMRED_SINGLE_SCIENCE) == 0)) {
353 proCatg = cpl_sprintf (GRAVI_VIS_SINGLE_SCIENCE);
354 mode = cpl_sprintf (
"gravi_single");
357 proCatg = cpl_sprintf (
"UNKNOWN");
358 mode = cpl_sprintf (
"gravi_single");
361 cpl_msg_info (cpl_func,
"Mode of the first frame is: %s (will be used for all frames)", mode);
367 nb_frame = cpl_frameset_get_size (recipe_frameset);
368 for (
int ivis = 0; ivis < nb_frame; ivis++){
370 cpl_msg_info (cpl_func,
" ***** P2VMREDUCED %d over %d ***** ", ivis+1, nb_frame);
372 frame = cpl_frameset_get_position (recipe_frameset, ivis);
373 p2vmred_data = gravi_data_load_frame (frame, used_frameset);
376 if (gravi_param_get_bool (parlist,
"gravity.signal.use-existing-rejection")) {
377 cpl_msg_info (cpl_func,
"Don't recompute SNR and selection, use the existing one");
380 gravi_compute_snr (p2vmred_data, parlist);
381 CPLCHECK_MSG (
"Cannot compute SNR");
384 gravi_compute_rejection (p2vmred_data, parlist);
385 CPLCHECK_MSG (
"Cannot recompute rejection flags signals");
390 tmpvis_data = gravi_compute_vis (p2vmred_data, parlist);
391 CPLCHECK_CLEAN (
"Cannot average the P2VMRED frames into VIS");
395 vis_data = tmpvis_data; tmpvis_data = NULL;
398 cpl_msg_info (cpl_func,
"Merge with previous OI_VIS");
399 gravi_data_append (vis_data, tmpvis_data, 1);
400 FREE (gravi_data_delete, tmpvis_data);
402 CPLCHECK_CLEAN (
"Cannot merge the visibilities");
405 cpl_msg_info (cpl_func,
"Free the p2vmreduced");
406 FREE (gravi_data_delete, p2vmred_data);
412 if (!strcmp (gravi_param_get_string (parlist,
"gravity.vis.vis-correction"),
"FORCE")) {
414 cpl_msg_info (cpl_func,
"Align the SC visibilities on the FT");
415 gravi_normalize_sc_to_ft (vis_data);
418 cpl_msg_info (cpl_func,
"Don't align the SC visibilities on the FT");
422 if (gravi_param_get_bool (parlist,
"gravity.postprocess.average-vis")) {
424 cpl_msg_warning (cpl_func,
"Average the different observation (if any) = EXPERIMENTAL");
425 gravi_average_vis (vis_data);
428 cpl_msg_info (cpl_func,
"Don't average the different observation (if any)");
433 gravi_vis_mjd_to_time (vis_data);
436 frame = cpl_frameset_get_position (recipe_frameset, 0);
438 gravi_data_save_new (vis_data, frameset, NULL, parlist,
439 used_frameset, frame,
"gravity_vis_from_p2vmred",
442 CPLCHECK_CLEAN (
"Cannot save the VIS product");
449 cpl_msg_info(cpl_func,
"Memory cleanup");
451 FREE (gravi_data_delete,p2vmred_data);
452 FREE (gravi_data_delete,vis_data);
453 FREE (gravi_data_delete,tmpvis_data);
454 FREE (cpl_frameset_delete,recipe_frameset);
455 FREE (cpl_frameset_delete,used_frameset);
456 FREE (cpl_free,proCatg);
457 FREE (cpl_free,mode);
459 gravi_msg_function_exit(1);
460 return (
int)cpl_error_get_code();