30#include <hdrl_download.h>
31#include <hdrl_utils.h>
47static int gravity_eop(cpl_frameset *,
const cpl_parameterlist *);
50 double * mjd_lastfinal);
56static const char gravity_eop_short[] =
"Download the last values of the Earth Orientation Parameters and DUT from IERS.";
58"This recipe downloads the latest version of the Earth Orientation Parameter \n"
59"and DUT from the IERS site. File is created in the current directory. A web connection is required.\n"
61 "* Download the IERS data\n"
62 "* Convert into CPL table\n"
67 GRAVI_EOP_MAP" : EOP calibration file (gravity_eop_calib.fits)\n"
89 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
90 cpl_plugin * plugin = &recipe->interface;
92 if (cpl_plugin_init(plugin,
95 CPL_PLUGIN_TYPE_RECIPE,
99 "Cesar Enrique Garcia Dabo",
105 cpl_msg_error(cpl_func,
"Plugin initialization failed");
106 (void)cpl_error_set_where(cpl_func);
110 if (cpl_pluginlist_append(list, plugin)) {
111 cpl_msg_error(cpl_func,
"Error adding plugin to list");
112 (void)cpl_error_set_where(cpl_func);
134 if (cpl_error_get_code() != CPL_ERROR_NONE) {
135 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
136 cpl_func, __LINE__, cpl_error_get_where());
137 return (
int)cpl_error_get_code();
140 if (plugin == NULL) {
141 cpl_msg_error(cpl_func,
"Null plugin");
142 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
146 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
147 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
148 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
152 recipe = (cpl_recipe *)plugin;
155 recipe->parameters = cpl_parameterlist_new();
156 if (recipe->parameters == NULL) {
157 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
158 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
162 p = cpl_parameter_new_value (
"gravity.eop.eop_host",
164 "FTP Host to retrieve the EOP from",
166 "https://datacenter.iers.org");
167 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"eop_host");
168 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
169 cpl_parameterlist_append (recipe->parameters, p);
172 p = cpl_parameter_new_value (
"gravity.eop.eop_urlpath",
174 "FTP URL path of the EOP file to retrieve",
176 "/data/latestVersion/finals.data.iau2000.txt");
177 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"eop_urlpath");
178 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
179 cpl_parameterlist_append (recipe->parameters, p);
196 cpl_errorstate initial_errorstate = cpl_errorstate_get();
199 if (cpl_error_get_code() != CPL_ERROR_NONE) {
200 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
201 cpl_func, __LINE__, cpl_error_get_where());
202 return (
int)cpl_error_get_code();
205 if (plugin == NULL) {
206 cpl_msg_error(cpl_func,
"Null plugin");
207 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
211 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
212 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
213 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
217 recipe = (cpl_recipe *)plugin;
220 if (recipe->parameters == NULL) {
221 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
222 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
224 if (recipe->frames == NULL) {
225 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
226 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
230 recipe_status =
gravity_eop(recipe->frames, recipe->parameters);
232 if (!cpl_errorstate_is_equal(initial_errorstate)) {
235 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
238 return recipe_status;
252 if (plugin == NULL) {
253 cpl_msg_error(cpl_func,
"Null plugin");
254 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
258 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
259 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
260 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
264 recipe = (cpl_recipe *)plugin;
266 cpl_parameterlist_delete(recipe->parameters);
273 double * mjd_lastfinal)
276 double mjd_lastprediction;
279 mjd_start = cpl_table_get_double (eop_table,
"MJD", 0, &null);
280 for(
int i = 0 ; i < cpl_table_get_nrow(eop_table); i++)
282 const char * flag = cpl_table_get_string(eop_table,
"FLAG", i);
283 if(!strncmp(flag,
"I", 1))
284 *mjd_lastfinal = cpl_table_get_double(eop_table,
"MJD", i, &null);
285 if(!strncmp(flag,
"P", 1))
286 mjd_lastprediction = cpl_table_get_double(eop_table,
"MJD", i, &null);
289 cpl_msg_info (cpl_func,
"QC EOP MJD START = %.3f", mjd_start);
290 cpl_msg_info (cpl_func,
"QC EOP MJD LAST FINAL = %.3f", *mjd_lastfinal);
291 cpl_msg_info (cpl_func,
"QC EOP MJD LAST PREDICTION = %.3f", mjd_lastprediction);
293 cpl_propertylist_append_double (
header,
"ESO QC EOP MJD START", mjd_start);
294 cpl_propertylist_append_double (
header,
"ESO QC EOP MJD LAST FINAL", *mjd_lastfinal);
295 cpl_propertylist_append_double (
header,
"ESO QC EOP MJD LAST PREDICTION", mjd_lastprediction);
296 cpl_propertylist_append_double (
header,
"MJD-OBS", *mjd_lastfinal);
298 return CPL_ERROR_NONE;
309 const cpl_parameterlist * parlist)
311 const char * eop_host;
312 const char * eop_urlpath;
313 cpl_propertylist * applist;
314 char * timestamp_lastfinal;
315 double mjd_lastfinal;
324 cpl_errorstate prestate = cpl_errorstate_get();
333 if (!cpl_errorstate_is_equal(prestate)) {
334 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
335 "Could not retrieve the input "
346 const char * url = cpl_sprintf(
"%s%s", eop_host, eop_urlpath);
347 eop_data = hdrl_download_url_to_buffer (url, &data_length);
348 cpl_free((
char *)url);
350 if (eop_data == NULL || !cpl_errorstate_is_equal(prestate)) {
351 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
352 "Could not download data from server");
356 cpl_msg_info (cpl_func,
"Convert EOP data to cpl_table");
357 cpl_table * eop_table = hdrl_eop_data_totable (eop_data, (cpl_size)data_length);
360 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
362 applist = cpl_propertylist_new();
365 cpl_propertylist_append_string (applist, CPL_DFS_PRO_CATG,
GRAVI_EOP_MAP);
366 cpl_propertylist_append_string (applist,
"ESO PRO TECH",
"CATALOG");
367 cpl_propertylist_append_string (applist,
"ESO PRO TYPE",
"STATIC");
375 filename = cpl_sprintf(
"GRAVI_EOP_PARAM.%s.fits", timestamp_lastfinal);
376 cpl_table_save (eop_table, applist, NULL, filename, CPL_IO_CREATE);
381 cpl_frame * product_frame = cpl_frame_new();
382 cpl_frame_set_filename (product_frame, filename);
383 cpl_frame_set_tag (product_frame, CPL_DFS_PRO_CATG);
384 cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_TABLE);
385 cpl_frame_set_group (product_frame, CPL_FRAME_GROUP_PRODUCT);
386 cpl_frame_set_level (product_frame, CPL_FRAME_LEVEL_FINAL);
387 cpl_frameset_insert (frameset, product_frame);
388 cpl_ensure_code (cpl_errorstate_is_equal(prestate), cpl_error_get_code());
391 FREE (cpl_propertylist_delete, applist);
392 FREE (cpl_table_delete, eop_table);
393 FREE (free, eop_data);
394 FREE (cpl_free, filename);
395 FREE (cpl_free, timestamp_lastfinal);
398 return (
int)cpl_error_get_code();
#define GRAVI_RECIPE_OUTPUT
#define GRAVI_RECIPE_FLOW
#define GRAVI_RECIPE_INPUT
cpl_table_save(vis_met, NULL, NULL, "vismet.fits", CPL_IO_CREATE)
cpl_propertylist * header
cpl_msg_info(cpl_func, "Compute WAVE_SCAN for %s", GRAVI_TYPE(type_data))
#define gravi_msg_function_exit(flag)
#define FREE(function, variable)
#define gravi_msg_function_start(flag)
static const char gravity_eop_name[]
static int gravity_eop(cpl_frameset *, const cpl_parameterlist *)
Implement the recipe functionality.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static const char gravity_eop_short[]
cpl_error_code gravity_eop_compute_qc(cpl_table *eop_table, cpl_propertylist *header, double *mjd_lastfinal)
static int gravity_eop_create(cpl_plugin *)
Setup the recipe options
static int gravity_eop_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static const char gravity_eop_description[]
static int gravity_eop_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
const char * gravi_param_get_string(const cpl_parameterlist *parlist, const char *name)
void gravity_print_banner(void)
char * gravi_convert_to_timestamp(double mjd)