187 cpl_errorstate initial_errorstate = cpl_errorstate_get();
190 if (cpl_error_get_code() != CPL_ERROR_NONE) {
191 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
192 cpl_func, __LINE__, cpl_error_get_where());
193 return (
int)cpl_error_get_code();
196 if (plugin == NULL) {
197 cpl_msg_error(cpl_func,
"Null plugin");
198 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
202 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
203 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
204 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
208 recipe = (cpl_recipe *)plugin;
211 if (recipe->parameters == NULL) {
212 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
213 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
215 if (recipe->frames == NULL) {
216 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
217 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
221 recipe_status =
gravity_pcacal(recipe->frames, recipe->parameters);
224 if (cpl_dfs_update_product_header(recipe->frames)) {
225 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
228 if (!cpl_errorstate_is_equal(initial_errorstate)) {
231 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
234 return recipe_status;
309 const cpl_parameterlist * parlist)
311 cpl_frameset *vis_cal_frameset = NULL;
312 cpl_frameset *used_frameset = cpl_frameset_new();
313 cpl_frame *frame = NULL;
315 gravi_data *data_tmp = NULL, **data_accepted = NULL, *pca_data = NULL;
316 cpl_propertylist *hdr = NULL, *header_first = NULL, *wave_plist = NULL, *wv_plisti = NULL;
317 const char *telescope = NULL, *pola_mode = NULL, *spec_res = NULL;
318 int nframes, nwave, nwavei, npol, naccept;
320 char product_filename[100];
326 int min_tracking_ratio = cpl_parameter_get_int(
327 cpl_parameterlist_find_const(parlist,
"gravity.calib.pca-tracking-ratio"));
331 cpl_error_get_code());
334 if (cpl_frameset_is_empty(vis_cal_frameset)) {
335 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
336 "No VIS_CAL on the frameset");
341 frame = cpl_frameset_get_position(vis_cal_frameset, 0);
345 telescope = cpl_propertylist_get_string(header_first,
"TELESCOP");
351 double time_mjd_obs = cpl_propertylist_get_double(header_first,
"MJD-OBS");
354 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
355 "First frame is too old\n");
357 for (
int i = 0; i <
N_EPOCH; i++) {
365 nwave = cpl_propertylist_get_int(wave_plist,
"NWAVE");
369 cpl_msg_info(cpl_func,
"INS.NAME=%s POLA.MODE=%s SPEC.RES=%s NPOL=%d NWAVE=%d",
370 telescope, pola_mode, spec_res, npol, nwave);
374 nframes = cpl_frameset_get_size(vis_cal_frameset);
375 data_accepted = cpl_malloc(nframes *
sizeof(
gravi_data *));
376 for (
int n = 0; n < nframes; n++) {
377 frame = cpl_frameset_get_position(vis_cal_frameset, n);
384 nwavei = cpl_propertylist_get_int(wv_plisti,
"NWAVE");
386 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
387 "Input files have inconsistent wavelength axes");
390 if (strcmp(telescope, cpl_propertylist_get_string(hdr,
"TELESCOP"))) {
391 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
392 "Input files have multiple TELESCOP");
397 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
398 "Input files have multiple INS.POLA.MODE");
403 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
404 "Input files have multiple INS.SPEC.RES");
408 time_mjd_obs = cpl_propertylist_get_double(hdr,
"MJD-OBS");
411 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
412 "Input files span multiple epochs");
416 cpl_boolean skip = CPL_FALSE;
418 for (
int i = 0; i < nbase; i++) {
419 const cpl_array *vis_arr = cpl_table_get_array(vis_tmp,
"VISPHI", i);
420 if ((cpl_array_get_max(vis_arr) < 1e-2) || (cpl_array_get_min(vis_arr) > -1e-2)) {
421 cpl_msg_warning(cpl_func,
"Input file %s has invalid data and will be skipped [range=%f--%f].",
422 cpl_frame_get_filename(frame), cpl_array_get_max(vis_arr), cpl_array_get_min(vis_arr));
430 data_accepted[naccept] = data_tmp;
431 cpl_frameset_insert(used_frameset, frame);
438 cpl_msg_info(cpl_func,
"Accepting %d of %d frames", naccept, nframes);
439 data_accepted = cpl_realloc(data_accepted, naccept *
sizeof(
gravi_data *));
442 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
443 "None of the input files satisfy the minimum tracking ratio criterion");
446 cpl_msg_warning(cpl_func,
"Fewer than %lld valid frames provided, calibration may be inaccurate",
MIN_CALIB_FRAMES);
448 if (cpl_error_get_code())
459 cpl_errorstate e_state = cpl_errorstate_get();
462 cpl_table_new_column(table,
"FILES", CPL_TYPE_STRING);
463 for (
int i = 0; i < naccept; i++) {
464 const cpl_frame *frame = cpl_frameset_get_position_const(used_frameset, i);
465 cpl_table_set_string(table,
"FILES", i, cpl_frame_get_filename(frame));
469 cpl_errorstate_set(e_state);
473 sprintf(product_filename,
"GRAVI.%s.%s_%s_%s.", mjd_str, spec_res, pola_mode, telescope);
475 used_frameset, NULL,
"gravity_phase_pca",
483 FREE(cpl_propertylist_delete, header_first);
484 FREE(cpl_frameset_delete, vis_cal_frameset);
489 return (
int)cpl_error_get_code();