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_p2vmred.h" 49 #include "gravi_eop.h" 50 #include "gravi_metrology.h" 52 #include "gravi_vis.h" 55 #include "gravi_preproc.h" 61 static int gravity_postprocess_create(cpl_plugin *);
62 static int gravity_postprocess_exec(cpl_plugin *);
63 static int gravity_postprocess_destroy(cpl_plugin *);
64 static int gravity_postprocess(cpl_frameset *,
const cpl_parameterlist *);
70 static char gravity_postprocess_short[] =
"Post-process the products, to fine-tune their content.";
71 static char gravity_postprocess_description[] =
72 "The recipe allows to manipulate the product of the GRAVITY pipeline, mostly the VIS. It permits to merge several files together into a single VIS file with all observations; to average the observations of one or several VIS file to increse the SNR; to remove some data (FT, SC); and to resample the SC observation with spectral bining.\n" 74 "The list of input file can be P2VMRED, VIS, VIS_CALIBRATED (or even RAW for some parameters). However they should all be compatible in term of setup and observed objet !! Note that the recipe performs only little checks of the input file content and structure. Thus the user shall ensure the input files are conformable (same polarisation and spectral mode for instante)";
91 int cpl_plugin_get_info(cpl_pluginlist * list)
93 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
94 cpl_plugin * plugin = &recipe->interface;
96 if (cpl_plugin_init(plugin,
99 CPL_PLUGIN_TYPE_RECIPE,
100 "gravity_postprocess",
101 gravity_postprocess_short,
102 gravity_postprocess_description,
103 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
106 gravity_postprocess_create,
107 gravity_postprocess_exec,
108 gravity_postprocess_destroy)) {
109 cpl_msg_error(cpl_func,
"Plugin initialization failed");
110 (void)cpl_error_set_where(cpl_func);
114 if (cpl_pluginlist_append(list, plugin)) {
115 cpl_msg_error(cpl_func,
"Error adding plugin to list");
116 (void)cpl_error_set_where(cpl_func);
132 static int gravity_postprocess_create(cpl_plugin * plugin)
138 if (cpl_error_get_code() != CPL_ERROR_NONE) {
139 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
140 cpl_func, __LINE__, cpl_error_get_where());
141 return (
int)cpl_error_get_code();
144 if (plugin == NULL) {
145 cpl_msg_error(cpl_func,
"Null plugin");
146 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
150 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
151 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
152 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
156 recipe = (cpl_recipe *)plugin;
159 recipe->parameters = cpl_parameterlist_new();
160 if (recipe->parameters == NULL) {
161 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
162 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
168 gravi_parameter_add_average_vis (recipe->parameters);
171 p = cpl_parameter_new_value (
"gravity.postprocess.force-merge", CPL_TYPE_BOOL,
172 "Force merging even if inconsistent data",
173 "gravity.postprocess", FALSE);
174 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"force-merge");
175 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
176 cpl_parameterlist_append (recipe->parameters, p);
179 p = cpl_parameter_new_value (
"gravity.postprocess.remove-ft", CPL_TYPE_BOOL,
180 "Remove FT extensions",
181 "gravity.postprocess", FALSE);
182 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"remove-ft");
183 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
184 cpl_parameterlist_append (recipe->parameters, p);
187 p = cpl_parameter_new_value (
"gravity.postprocess.remove-sc", CPL_TYPE_BOOL,
188 "Remove SC extensions",
189 "gravity.postprocess", FALSE);
190 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"remove-sc");
191 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
192 cpl_parameterlist_append (recipe->parameters, p);
195 p = cpl_parameter_new_value (
"gravity.postprocess.remove-opdc", CPL_TYPE_BOOL,
196 "Remove OPDC extensions",
197 "gravity.postprocess", FALSE);
198 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"remove-opdc");
199 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
200 cpl_parameterlist_append (recipe->parameters, p);
203 p = cpl_parameter_new_value (
"gravity.postprocess.remove-met", CPL_TYPE_BOOL,
204 "Remove METROLOGY related extensions",
205 "gravity.postprocess", FALSE);
206 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"remove-met");
207 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
208 cpl_parameterlist_append (recipe->parameters, p);
211 p = cpl_parameter_new_value (
"gravity.postprocess.nbin-lambda-sc", CPL_TYPE_INT,
212 "Bin SC extensions in spectral dimension",
213 "gravity.postprocess", 0);
214 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"nbin-lambda-sc");
215 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
216 cpl_parameterlist_append (recipe->parameters, p);
228 static int gravity_postprocess_exec(cpl_plugin * plugin)
233 cpl_errorstate initial_errorstate = cpl_errorstate_get();
237 if (cpl_error_get_code() != CPL_ERROR_NONE) {
238 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
239 cpl_func, __LINE__, cpl_error_get_where());
240 return (
int)cpl_error_get_code();
243 if (plugin == NULL) {
244 cpl_msg_error(cpl_func,
"Null plugin");
245 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
249 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
250 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
251 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
255 recipe = (cpl_recipe *)plugin;
258 if (recipe->parameters == NULL) {
259 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
260 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
262 if (recipe->frames == NULL) {
263 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
264 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
268 recipe_status = gravity_postprocess(recipe->frames, recipe->parameters);
271 if (cpl_dfs_update_product_header(recipe->frames)) {
273 recipe_status = (int)cpl_error_get_code();
277 if (!cpl_errorstate_is_equal(initial_errorstate)) {
280 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
283 return recipe_status;
293 static int gravity_postprocess_destroy(cpl_plugin * plugin)
297 if (plugin == NULL) {
298 cpl_msg_error(cpl_func,
"Null plugin");
299 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
303 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
304 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
305 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
309 recipe = (cpl_recipe *)plugin;
311 cpl_parameterlist_delete(recipe->parameters);
326 static int gravity_postprocess(cpl_frameset * frameset,
327 const cpl_parameterlist * parlist)
329 cpl_frameset *used_frameset=NULL;
330 cpl_frame * frame=NULL, *frame_merged=NULL;
332 gravi_data * data_merged=NULL, * data=NULL;
335 gravity_print_banner ();
336 cpl_msg_set_time_on();
337 cpl_msg_set_component_on();
338 gravi_msg_function_start(1);
340 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
341 cpl_error_get_code());
343 cpl_size nframe = cpl_frameset_get_size (frameset);
344 int force = gravi_param_get_bool (parlist,
"gravity.postprocess.force-merge");
349 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
350 "Not enough frames in the frameset");
358 used_frameset = cpl_frameset_new();
362 for (cpl_size f = 0; f < nframe ; f++ ) {
365 frame = cpl_frameset_get_position (frameset, f);
366 data = gravi_data_load_frame (frame, used_frameset);
369 if (gravi_param_get_bool (parlist,
"gravity.postprocess.remove-ft")) {
370 cpl_msg_info (cpl_func,
"Erase FT");
371 gravi_data_erase_type (data,
"_FT");
372 CPLCHECK_CLEAN (
"Cannot delete FT");
376 if (gravi_param_get_bool (parlist,
"gravity.postprocess.remove-sc")) {
377 cpl_msg_info (cpl_func,
"Erase SC");
378 gravi_data_erase_type (data,
"_SC");
379 CPLCHECK_CLEAN (
"Cannot delete SC");
382 if (gravi_param_get_bool (parlist,
"gravity.postprocess.remove-opdc")) {
383 cpl_msg_info (cpl_func,
"Erase OPDC");
384 gravi_data_erase_type (data,
"OPDC");
385 CPLCHECK_CLEAN (
"Cannot delete OPDC");
388 if (gravi_param_get_bool (parlist,
"gravity.postprocess.remove-met")) {
389 cpl_msg_info (cpl_func,
"Erase MET");
390 gravi_data_erase_type (data,
"METROLOGY");
391 gravi_data_erase_type (data,
"VIS_MET");
392 CPLCHECK_CLEAN (
"Cannot delete MET");
397 frame_merged = frame;
398 data_merged = data; data = NULL;
402 gravi_data_append (data_merged, data, force);
403 FREE (gravi_data_delete, data);
406 CPLCHECK_CLEAN (
"Cannot append frames");
411 if (gravi_param_get_bool (parlist,
"gravity.postprocess.average-vis")) {
413 gravi_msg_warning (
"FIXME",
"Average the different observations = EXPERIMENTAL");
414 gravi_average_vis (data_merged);
416 CPLCHECK_CLEAN (
"Cannot average VIS");
420 cpl_size resamp_sc = gravi_param_get_int (parlist,
"gravity.postprocess.nbin-lambda-sc");
421 if ( resamp_sc > 1) {
423 gravi_msg_warning (
"FIXME",
"Resamp the SC data = EXPERIMENTAL");
424 gravi_vis_resamp (data_merged, resamp_sc);
426 CPLCHECK_CLEAN (
"Cannot resamp SC");
432 gravi_vis_mjd_to_time (data_merged);
435 gravi_data_save_new (data_merged, frameset, NULL, parlist,
436 used_frameset, frame_merged,
"gravity_postprocess",
437 NULL,
"POSTPROCESSED");
439 CPLCHECK_CLEAN (
"Cannot save the POSTPROCESSED product");
446 cpl_msg_info(cpl_func,
"Memory cleanup");
448 FREE (gravi_data_delete,data);
449 FREE (gravi_data_delete,data_merged);
450 FREE (cpl_frameset_delete,used_frameset);
452 gravi_msg_function_exit(1);
453 return (
int)cpl_error_get_code();