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");
451 snprintf(fsuffix, 16,
"%d", j+1);
455 cpl_msg_warning (cpl_func,
"visphi calib provided but --separate-phase-calib not set, will be ignored");
456 }
else if (visphi_calib) {
457 cpl_msg_warning (cpl_func,
"multiple visphi calib provided, will be ignored");
459 cpl_msg_info (cpl_func,
"*** TF %i over %i to be used for visphi ***", j+1, nb_frame_calib);
462 NULL, frame,
"gravity_viscal",
466 visphi_calib = vis_calib;
471 NULL, frame,
"gravity_viscal",
475 vis_calibs[nb_calib] = vis_calib;
481 frame = cpl_frame_duplicate (frame);
482 cpl_frame_set_group (frame, CPL_FRAME_GROUP_CALIB);
483 cpl_frameset_insert (used_frameset, frame);
492 cpl_msg_info (cpl_func,
"*** Load already computed TF ***");
495 for (j = 0; j < nb_frame_tf; j++) {
496 cpl_msg_info (cpl_func,
" %i over %i", j+1, nb_frame_tf);
498 frame = cpl_frameset_get_position (tf_calib_frameset, j);
507 cpl_msg_warning (cpl_func,
"visphi calib provided but --separate-phase-calib not set, will be ignored");
508 }
else if (visphi_calib) {
509 cpl_msg_warning (cpl_func,
"multiple visphi calib provided, will be ignored");
511 cpl_msg_info (cpl_func,
"*** TF %i over %i to be used for visphi ***", j+1, nb_frame_tf);
512 visphi_calib = vis_calib;
516 vis_calibs[nb_calib] = vis_calib;
526 cpl_msg_info (cpl_func,
"*** All TF computed or loaded ***");
528 cpl_msg_info (cpl_func,
"Load or create successfully %i TF over %i input CAL files", nb_calib, nb_frame_calib + nb_frame_tf);
534 if ( nb_calib > 1 ) {
535 cpl_msg_info (cpl_func,
"*** Compute the zero of the metrology -- FIXME: to be done ***");
541 used_frameset, NULL,
"gravity_viscal",
556 for (i = 0; i < nb_frame_sci; i++){
557 current_frameset = cpl_frameset_duplicate (used_frameset);
559 cpl_msg_info (cpl_func,
"*** Calibration of file %i over %i ***", i+1, nb_frame_sci);
561 frame = cpl_frameset_get_position (vis_sci_frameset, i);
565 calibrated =
gravi_calibrate_vis (vis_data, vis_calibs, nb_calib, visphi_calib, tf_science, parlist);
566 CPLCHECK_GOTO(
"Cannot calibrate the visibility", cleanup_calib);
572 cpl_msg_info (cpl_func,
"Computing QC parameters for calibrated visibilities");
573 char * input_data_type = cpl_sprintf (
"vis_science");
576 current_frameset, input_data_type);
577 cpl_free(input_data_type);
580 cpl_propertylist_update_int (idp_hdr,
"NCOMBINE", 1);
583 cpl_propertylist_append(extra_header, idp_hdr);
584 cpl_propertylist_delete(idp_hdr);
587 snprintf(fsuffix, 16,
"%d", i+1);
589 current_frameset, frame,
"gravity_viscal",
592 CPLCHECK_GOTO(
"Cannot save the calibrated visibility", cleanup_calib);
597 cpl_propertylist_update_string (hdr,
"PRODCATG",
"ANCILLARY.TRANSFERFUNC");
598 cpl_propertylist_set_comment (hdr,
"PRODCATG",
"Data product category");
601 current_frameset, frame,
"gravity_viscal", NULL,
604 CPLCHECK_GOTO(
"Cannot save the TF interpolated for this visibility", cleanup_calib);
610 FREE (cpl_propertylist_delete,applist);
611 FREE (cpl_frameset_delete,current_frameset);
624 FREE (cpl_frameset_delete,tf_calib_frameset);
625 FREE (cpl_frameset_delete,vis_calib_frameset);
626 FREE (cpl_frameset_delete,vis_sci_frameset);
627 FREE (cpl_frameset_delete,diamcat_frameset);
628 FREE (cpl_frameset_delete,current_frameset);
629 FREE (cpl_frameset_delete,used_frameset);
634 return (
int)cpl_error_get_code();