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 averages the real-time data of 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" 70 "* Load the input file (loop on input files)\n" 71 "* Update the selection flag\n" 72 "* Average the real-time visibilities\n" 73 "* Write the product\n" 74 GRAVI_RECIPE_INPUT
"\n" 75 GRAVI_P2VMRED_SINGLE_SCIENCE
" : Input intermediate product\n" 76 GRAVI_RECIPE_OUTPUT
"\n" 77 GRAVI_VIS_SINGLE_SCIENCE
" : OIFITS with uncalibrated visibilities\n" 95 int cpl_plugin_get_info(cpl_pluginlist * list)
97 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
98 cpl_plugin * plugin = &recipe->interface;
100 if (cpl_plugin_init(plugin,
102 GRAVI_BINARY_VERSION,
103 CPL_PLUGIN_TYPE_RECIPE,
104 "gravity_vis_from_p2vmred",
105 gravity_vis_from_p2vmred_short,
106 gravity_vis_from_p2vmred_description,
107 "JB. Le Bouquin, Vincent Lapeyrere, ",
110 gravity_vis_from_p2vmred_create,
111 gravity_vis_from_p2vmred_exec,
112 gravity_vis_from_p2vmred_destroy)) {
113 cpl_msg_error(cpl_func,
"Plugin initialization failed");
114 (void)cpl_error_set_where(cpl_func);
118 if (cpl_pluginlist_append(list, plugin)) {
119 cpl_msg_error(cpl_func,
"Error adding plugin to list");
120 (void)cpl_error_set_where(cpl_func);
136 static int gravity_vis_from_p2vmred_create(cpl_plugin * plugin)
142 if (cpl_error_get_code() != CPL_ERROR_NONE) {
143 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
144 cpl_func, __LINE__, cpl_error_get_where());
145 return (
int)cpl_error_get_code();
148 if (plugin == NULL) {
149 cpl_msg_error(cpl_func,
"Null plugin");
150 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
154 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
155 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
156 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
160 recipe = (cpl_recipe *)plugin;
163 recipe->parameters = cpl_parameterlist_new();
164 if (recipe->parameters == NULL) {
165 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
166 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
173 gravi_parameter_add_static_name (recipe->parameters);
176 gravi_parameter_add_average_vis (recipe->parameters);
179 gravi_parameter_add_compute_snr (recipe->parameters, isCalib);
185 gravi_parameter_add_compute_vis (recipe->parameters, isCalib);
188 p = cpl_parameter_new_value (
"gravity.signal.use-existing-rejection", CPL_TYPE_BOOL,
189 "Use existing rejection flags (ignore related options)",
190 "gravity.signal", FALSE);
191 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"use-existing-rejection");
192 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
193 cpl_parameterlist_append (recipe->parameters, p);
205 static int gravity_vis_from_p2vmred_exec(cpl_plugin * plugin)
210 cpl_errorstate initial_errorstate = cpl_errorstate_get();
214 if (cpl_error_get_code() != CPL_ERROR_NONE) {
215 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
216 cpl_func, __LINE__, cpl_error_get_where());
217 return (
int)cpl_error_get_code();
220 if (plugin == NULL) {
221 cpl_msg_error(cpl_func,
"Null plugin");
222 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
226 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
227 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
228 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
232 recipe = (cpl_recipe *)plugin;
235 if (recipe->parameters == NULL) {
236 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
237 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
239 if (recipe->frames == NULL) {
240 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
241 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
245 recipe_status = gravity_vis_from_p2vmred(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_vis_from_p2vmred_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_vis_from_p2vmred(cpl_frameset * frameset,
304 const cpl_parameterlist * parlist)
306 cpl_frameset * recipe_frameset=NULL, *used_frameset=NULL;
308 cpl_frame * frame=NULL;
310 const char * frame_tag=NULL;
311 char * proCatg = NULL, * mode=NULL;
313 gravi_data * p2vmred_data=NULL, * vis_data=NULL, * tmpvis_data=NULL;
318 gravity_print_banner ();
319 cpl_msg_set_time_on();
320 cpl_msg_set_component_on();
321 gravi_msg_function_start(1);
324 cpl_error_get_code()) ;
327 recipe_frameset = gravi_frameset_extract_p2vmred_data (frameset);
330 if ( cpl_frameset_get_size (recipe_frameset) < 1 ) {
331 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
332 "Illegal number of P2VMREDUCED file on the frameset");
337 used_frameset = cpl_frameset_new();
343 frame_tag = cpl_frame_get_tag (cpl_frameset_get_position (recipe_frameset, 0));
345 if ((strcmp(frame_tag, GRAVI_P2VMRED_DUAL_CALIB) == 0)) {
346 proCatg = cpl_sprintf (GRAVI_VIS_DUAL_CALIB);
347 mode = cpl_sprintf (
"gravi_dual");
349 else if ((strcmp(frame_tag, GRAVI_P2VMRED_DUAL_SCIENCE) == 0)) {
350 proCatg = cpl_sprintf (GRAVI_VIS_DUAL_SCIENCE);
351 mode = cpl_sprintf (
"gravi_dual");
353 else if ((strcmp(frame_tag, GRAVI_P2VMRED_SINGLE_CALIB) == 0)) {
354 proCatg = cpl_sprintf (GRAVI_VIS_SINGLE_CALIB);
355 mode = cpl_sprintf (
"gravi_single");
357 else if ((strcmp(frame_tag, GRAVI_P2VMRED_SINGLE_SCIENCE) == 0)) {
358 proCatg = cpl_sprintf (GRAVI_VIS_SINGLE_SCIENCE);
359 mode = cpl_sprintf (
"gravi_single");
362 proCatg = cpl_sprintf (
"UNKNOWN");
363 mode = cpl_sprintf (
"gravi_single");
366 cpl_msg_info (cpl_func,
"Mode of the first frame is: %s (will be used for all frames)", mode);
372 nb_frame = cpl_frameset_get_size (recipe_frameset);
373 for (
int ivis = 0; ivis < nb_frame; ivis++){
375 cpl_msg_info (cpl_func,
" ***** P2VMREDUCED %d over %d ***** ", ivis+1, nb_frame);
377 frame = cpl_frameset_get_position (recipe_frameset, ivis);
381 if (gravi_param_get_bool (parlist,
"gravity.signal.use-existing-rejection")) {
382 cpl_msg_info (cpl_func,
"Don't recompute SNR and selection, use the existing one");
386 CPLCHECK_MSG (
"Cannot compute SNR");
390 CPLCHECK_MSG (
"Cannot recompute rejection flags signals");
394 cpl_size current_frame = 0;
395 while (current_frame >= 0)
401 CPLCHECK_CLEAN (
"Cannot average the P2VMRED frames into VIS");
404 if (gravi_param_get_bool (parlist,
"gravity.vis.force-same-time") ) {
405 cpl_msg_info (cpl_func,
"Force same time for all quantities/baselines");
407 CPLCHECK_CLEAN (
"Cannot average the TIME in OI_VIS");
411 if (vis_data == NULL) {
412 vis_data = tmpvis_data; tmpvis_data = NULL;
415 cpl_msg_info (cpl_func,
"Merge with previous OI_VIS");
419 CPLCHECK_CLEAN (
"Cannot merge the visibilities");
423 cpl_msg_info (cpl_func,
"Free the p2vmreduced");
433 if (!strcmp (gravi_param_get_string (parlist,
"gravity.vis.vis-correction-sc"),
"FORCE")) {
435 cpl_msg_info (cpl_func,
"Align the SC visibilities on the FT");
439 cpl_msg_info (cpl_func,
"Don't align the SC visibilities on the FT");
443 if (gravi_param_get_bool (parlist,
"gravity.postprocess.average-vis")) {
445 cpl_msg_warning (cpl_func,
"Average the different observation (if any) = EXPERIMENTAL");
449 cpl_msg_info (cpl_func,
"Don't average the different observation (if any)");
457 frame = cpl_frameset_get_position (recipe_frameset, 0);
460 used_frameset, frame,
"gravity_vis_from_p2vmred",
463 CPLCHECK_CLEAN (
"Cannot save the VIS product");
470 cpl_msg_info(cpl_func,
"Memory cleanup");
475 FREE (cpl_frameset_delete,recipe_frameset);
476 FREE (cpl_frameset_delete,used_frameset);
477 FREE (cpl_free,proCatg);
478 FREE (cpl_free,mode);
480 gravi_msg_function_exit(1);
481 return (
int)cpl_error_get_code();
gravi_data * gravi_data_load_frame(cpl_frame *frame, cpl_frameset *used_frameset)
Load a FITS file and create a gravi_data.
cpl_error_code gravi_compute_vis_qc(gravi_data *vis_data)
The function compute the QC parameters for a VIS (averaged) data.
cpl_error_code gravi_compute_snr(gravi_data *p2vmred_data, const cpl_parameterlist *parlist)
Compute real-time SNR and Group-Delay of the observation.
gravi_data * gravi_compute_vis(gravi_data *p2vmred_data, const cpl_parameterlist *parlist, cpl_size *current_frame)
The function average the individual frames of a P2VMREDUCED file into a final, single observation per...
cpl_error_code gravi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
cpl_error_code gravi_vis_mjd_to_time(gravi_data *vis_data)
Recompute the TIME column of all OIFITS extension from the MJD column, following the OIFITS standard ...
cpl_error_code gravi_compute_rejection(gravi_data *p2vmred_data, const cpl_parameterlist *parlist)
Create rejection flags P2VMREDUCED file.
const char * gravi_get_license(void)
Get the pipeline copyright and license.
cpl_error_code gravi_parameter_add_rejection(cpl_parameterlist *self, int isCalib)
Add rejection parameters to the input parameter list.
cpl_error_code gravi_data_save_new(gravi_data *self, cpl_frameset *allframes, const char *filename, const char *suffix, const cpl_parameterlist *parlist, cpl_frameset *usedframes, cpl_frame *frame, const char *recipe, cpl_propertylist *applist, const char *proCatg)
Save a gravi data in a CPL-complian FITS file.
cpl_error_code gravi_average_vis(gravi_data *oi_data)
Coadd the observations together.
cpl_error_code gravi_vis_force_time(gravi_data *oi_data)
Force all data in OI_TABLE to have the same TIME and MJD.
cpl_error_code gravi_normalize_sc_to_ft(gravi_data *vis_data)
Align the SC visibilities on the FT visibilities.
void gravi_data_delete(gravi_data *self)
Delete a gravi data.
cpl_error_code gravi_data_append(gravi_data *first, const gravi_data *second, int force)
Append a gravi_data into another existing one.