147 if (cpl_error_get_code() != CPL_ERROR_NONE) {
148 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
149 cpl_func, __LINE__, cpl_error_get_where());
150 return (
int)cpl_error_get_code();
153 if (plugin == NULL) {
154 cpl_msg_error(cpl_func,
"Null plugin");
155 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
159 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
160 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
161 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
165 recipe = (cpl_recipe *)plugin;
168 recipe->parameters = cpl_parameterlist_new();
169 if (recipe->parameters == NULL) {
170 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
171 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
178 p = cpl_parameter_new_value (
"gravity.viscal.delta-time-calib", CPL_TYPE_DOUBLE,
179 "Delta time to interpolate the TF [s]",
180 "gravity.viscal", 3600.0);
181 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"delta-time-calib");
182 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
183 cpl_parameterlist_append (recipe->parameters, p);
186 p = cpl_parameter_new_value (
"gravity.viscal.force-calib", CPL_TYPE_BOOL,
187 "Force the calibration, don't check setup",
188 "gravity.viscal", FALSE);
189 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"force-calib");
190 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
191 cpl_parameterlist_append (recipe->parameters, p);
194 p = cpl_parameter_new_value (
"gravity.viscal.nsmooth-tfvis-sc", CPL_TYPE_INT,
195 "Smooth the TF spectrally by this number of "
196 "spectral bin, to enhance SNR (only "
197 "apply to VIS2, VISPHI, VISAMP, T3PHI, T3AMP). "
198 "This parameter is ignored in spectral mode LOW.",
199 "gravity.viscal", 0);
200 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"nsmooth-tfvis-sc");
201 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
202 cpl_parameterlist_append (recipe->parameters, p);
204 p = cpl_parameter_new_value (
"gravity.viscal.nsmooth-tfflux-sc", CPL_TYPE_INT,
205 "Smooth the TF spectrally by this number of "
206 "spectral bin, to enhance SNR (only "
207 "apply to FLUX, RVIS, IVIS). "
208 "This parameter is ignored in spectral mode LOW.",
209 "gravity.viscal", 0);
210 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"nsmooth-tfflux-sc");
211 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
212 cpl_parameterlist_append (recipe->parameters, p);
214 p = cpl_parameter_new_value (
"gravity.viscal.maxdeg-tfvis-sc", CPL_TYPE_INT,
215 "Fit the TF spectrally by a polynomial to enhance SNR "
216 "(only apply to VIS2, VISPHI, VISAMP, T3PHI, T3AMP). "
217 "This parameter is ignored in spectral mode LOW.",
218 "gravity.viscal", 5);
219 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"maxdeg-tfvis-sc");
220 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
221 cpl_parameterlist_append (recipe->parameters, p);
223 p = cpl_parameter_new_value (
"gravity.viscal.calib-flux", CPL_TYPE_BOOL,
224 "Normalize the FLUX by the calibrator.",
225 "gravity.viscal", FALSE);
226 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"calib-flux");
227 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
228 cpl_parameterlist_append (recipe->parameters, p);
231 p = cpl_parameter_new_value (
"gravity.viscal.smoothing", CPL_TYPE_BOOL,
232 "control smoothing of transfer function (TF)"
233 "TRUE do smoothing - FALSE Skip smoothing",
234 "gravity.viscal", TRUE);
235 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"smoothing");
236 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
237 cpl_parameterlist_append (recipe->parameters, p);
239 p = cpl_parameter_new_value (
"gravity.viscal.separate-phase-calib", CPL_TYPE_BOOL,
240 "enable use of distinct calibrator for VISPHI",
241 "gravity.viscal", FALSE);
242 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"separate-phase-calib");
243 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
244 cpl_parameterlist_append (recipe->parameters, p);
261 cpl_errorstate initial_errorstate = cpl_errorstate_get();
264 if (cpl_error_get_code() != CPL_ERROR_NONE) {
265 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
266 cpl_func, __LINE__, cpl_error_get_where());
267 return (
int)cpl_error_get_code();
270 if (plugin == NULL) {
271 cpl_msg_error(cpl_func,
"Null plugin");
272 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
276 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
277 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
278 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
282 recipe = (cpl_recipe *)plugin;
285 if (recipe->parameters == NULL) {
286 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
287 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
289 if (recipe->frames == NULL) {
290 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
291 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
295 recipe_status =
gravity_viscal(recipe->frames, recipe->parameters);
299 if (cpl_dfs_update_product_header(recipe->frames)) {
301 recipe_status = (int)cpl_error_get_code();
305 if (!cpl_errorstate_is_equal(initial_errorstate)) {
308 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
311 return recipe_status;
355 const cpl_parameterlist * parlist)
357 cpl_frameset * vis_calib_frameset = NULL, * vis_sci_frameset = NULL, *current_frameset = NULL;
358 cpl_frameset * tf_calib_frameset = NULL, * used_frameset = NULL, * diamcat_frameset = NULL;
359 cpl_frame * frame = NULL;
361 cpl_propertylist * applist = NULL;
363 gravi_data ** vis_calibs = NULL, * vis_calib = NULL, * visphi_calib = NULL, * zero_data = NULL, * tf_science = NULL;
364 gravi_data * calibrated = NULL, * vis_data = NULL, * diamcat_data = NULL;
366 int data_mode, nb_frame_tf = 0, nb_frame_calib = 0, nb_frame_sci, i, j, nb_calib = 0;
374 cpl_error_get_code());
384 if ( cpl_frameset_is_empty (vis_calib_frameset) &&
385 cpl_frameset_is_empty (tf_calib_frameset) ) {
386 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
387 "No VIS or TF file on the frameset") ;
392 nb_frame_tf = cpl_frameset_get_size (tf_calib_frameset);
393 nb_frame_calib = cpl_frameset_get_size (vis_calib_frameset);
394 nb_frame_sci = cpl_frameset_get_size (vis_sci_frameset);
397 vis_calibs = cpl_malloc ( (nb_frame_calib + nb_frame_tf) *
sizeof (
gravi_data *));
398 for (j = 0; j < (nb_frame_calib + nb_frame_tf); j++) vis_calibs[j] = NULL;
406 if ( !cpl_frameset_is_empty (diamcat_frameset) ) {
407 frame = cpl_frameset_get_position (diamcat_frameset, 0);
412 used_frameset = cpl_frameset_new();
415 for (j = 0; j < nb_frame_calib; j++) {
416 cpl_msg_info (cpl_func,
"*** Compute TF %i over %i ***", j+1, nb_frame_calib);
419 frame = cpl_frameset_get_position (vis_calib_frameset, j);
428 cpl_msg_info (cpl_func,
"LOW spectral resolution -> don't smooth the TF");
429 }
else if (!smoothing) {
430 cpl_msg_info (cpl_func,
"smoothing parameter == FALSE -> don't smooth the TF");
433 cpl_size smooth_flx_sc =
gravi_param_get_int (parlist,
"gravity.viscal.nsmooth-tfflux-sc");
439 if (vis_calib == NULL) {
440 cpl_msg_error (cpl_func,
"Cannot compute this TF... continue");
453 cpl_msg_warning (cpl_func,
"visphi calib provided but --separate-phase-calib not set, will be ignored");
454 }
else if (visphi_calib) {
455 cpl_msg_warning (cpl_func,
"multiple visphi calib provided, will be ignored");
457 cpl_msg_info (cpl_func,
"*** TF %i over %i to be used for visphi ***", j+1, nb_frame_calib);
460 NULL, frame,
"gravity_viscal",
464 visphi_calib = vis_calib;
469 NULL, frame,
"gravity_viscal",
473 vis_calibs[nb_calib] = vis_calib;
479 frame = cpl_frame_duplicate (frame);
480 cpl_frame_set_group (frame, CPL_FRAME_GROUP_CALIB);
481 cpl_frameset_insert (used_frameset, frame);
490 cpl_msg_info (cpl_func,
"*** Load already computed TF ***");
493 for (j = 0; j < nb_frame_tf; j++) {
494 cpl_msg_info (cpl_func,
" %i over %i", j+1, nb_frame_tf);
496 frame = cpl_frameset_get_position (tf_calib_frameset, j);
505 cpl_msg_warning (cpl_func,
"visphi calib provided but --separate-phase-calib not set, will be ignored");
506 }
else if (visphi_calib) {
507 cpl_msg_warning (cpl_func,
"multiple visphi calib provided, will be ignored");
509 cpl_msg_info (cpl_func,
"*** TF %i over %i to be used for visphi ***", j+1, nb_frame_tf);
510 visphi_calib = vis_calib;
514 vis_calibs[nb_calib] = vis_calib;
524 cpl_msg_info (cpl_func,
"*** All TF computed or loaded ***");
526 cpl_msg_info (cpl_func,
"Load or create successfully %i TF over %i input CAL files", nb_calib, nb_frame_calib + nb_frame_tf);
532 if ( nb_calib > 1 ) {
533 cpl_msg_info (cpl_func,
"*** Compute the zero of the metrology -- FIXME: to be done ***");
539 used_frameset, NULL,
"gravity_viscal",
554 for (i = 0; i < nb_frame_sci; i++){
555 current_frameset = cpl_frameset_duplicate (used_frameset);
557 cpl_msg_info (cpl_func,
"*** Calibration of file %i over %i ***", i+1, nb_frame_sci);
559 frame = cpl_frameset_get_position (vis_sci_frameset, i);
563 calibrated =
gravi_calibrate_vis (vis_data, vis_calibs, nb_calib, visphi_calib, tf_science, parlist);
564 CPLCHECK_GOTO(
"Cannot calibrate the visibility", cleanup_calib);
570 cpl_msg_info (cpl_func,
"Computing QC parameters for calibrated visibilities");
571 char * input_data_type = cpl_sprintf (
"vis_science");
574 current_frameset, input_data_type);
575 cpl_free(input_data_type);
578 cpl_propertylist_update_int (idp_hdr,
"NCOMBINE", 1);
581 cpl_propertylist_append(extra_header, idp_hdr);
582 cpl_propertylist_delete(idp_hdr);
585 current_frameset, frame,
"gravity_viscal",
588 CPLCHECK_GOTO(
"Cannot save the calibrated visibility", cleanup_calib);
593 cpl_propertylist_update_string (hdr,
"PRODCATG",
"ANCILLARY.TRANSFERFUNC");
594 cpl_propertylist_set_comment (hdr,
"PRODCATG",
"Data product category");
597 current_frameset, frame,
"gravity_viscal", NULL,
600 CPLCHECK_GOTO(
"Cannot save the TF interpolated for this visibility", cleanup_calib);
606 FREE (cpl_propertylist_delete,applist);
607 FREE (cpl_frameset_delete,current_frameset);
620 FREE (cpl_frameset_delete,tf_calib_frameset);
621 FREE (cpl_frameset_delete,vis_calib_frameset);
622 FREE (cpl_frameset_delete,vis_sci_frameset);
623 FREE (cpl_frameset_delete,diamcat_frameset);
624 FREE (cpl_frameset_delete,current_frameset);
625 FREE (cpl_frameset_delete,used_frameset);
630 return (
int)cpl_error_get_code();