31 #include "gravi_data.h" 32 #include "gravi_dfs.h" 33 #include "gravi_pfits.h" 35 #include "gravi_utils.h" 37 #include "gravi_eop.h" 43 static int gravity_eop_create(cpl_plugin *);
44 static int gravity_eop_exec(cpl_plugin *);
45 static int gravity_eop_destroy(cpl_plugin *);
46 static int gravity_eop(cpl_frameset *,
const cpl_parameterlist *);
47 cpl_error_code gravity_eop_compute_qc(cpl_table * eop_table,
48 cpl_propertylist* header,
49 double * mjd_lastfinal);
55 static const char gravity_eop_short[] =
"Download the last values of the Earth Orientation Parameters and DUT from IERS.";
56 static const char gravity_eop_description[] =
57 "This recipe downloads the latest version of the Earth Orientation Parameter \n" 58 "and DUT from the IERS site. File is created in the current directory. A web connection is required.\n" 60 "* Download the IERS data\n" 61 "* Convert into CPL table\n" 63 GRAVI_RECIPE_INPUT
"\n" 65 GRAVI_RECIPE_OUTPUT
"\n" 66 GRAVI_EOP_MAP
" : EOP calibration file (gravity_eop_calib.fits)\n" 69 static const char gravity_eop_name[] =
"gravity_eop";
86 int cpl_plugin_get_info(cpl_pluginlist * list)
88 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
89 cpl_plugin * plugin = &recipe->interface;
91 if (cpl_plugin_init(plugin,
94 CPL_PLUGIN_TYPE_RECIPE,
97 gravity_eop_description,
98 "Cesar Enrique Garcia Dabo",
103 gravity_eop_destroy)) {
104 cpl_msg_error(cpl_func,
"Plugin initialization failed");
105 (void)cpl_error_set_where(cpl_func);
109 if (cpl_pluginlist_append(list, plugin)) {
110 cpl_msg_error(cpl_func,
"Error adding plugin to list");
111 (void)cpl_error_set_where(cpl_func);
127 static int gravity_eop_create(cpl_plugin * plugin)
133 if (cpl_error_get_code() != CPL_ERROR_NONE) {
134 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
135 cpl_func, __LINE__, cpl_error_get_where());
136 return (
int)cpl_error_get_code();
139 if (plugin == NULL) {
140 cpl_msg_error(cpl_func,
"Null plugin");
141 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
145 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
146 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
147 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
151 recipe = (cpl_recipe *)plugin;
154 recipe->parameters = cpl_parameterlist_new();
155 if (recipe->parameters == NULL) {
156 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
157 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
161 p = cpl_parameter_new_value (
"gravity.eop.eop_host",
163 "FTP Host to retrieve the EOP from",
166 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"eop_host");
167 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
168 cpl_parameterlist_append (recipe->parameters, p);
171 p = cpl_parameter_new_value (
"gravity.eop.eop_urlpath",
173 "FTP URL path of the EOP file to retrieve",
175 "/products/eop/rapid/standard/finals2000A.data");
176 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"eop_urlpath");
177 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
178 cpl_parameterlist_append (recipe->parameters, p);
190 static int gravity_eop_exec(cpl_plugin * plugin)
195 cpl_errorstate initial_errorstate = cpl_errorstate_get();
198 if (cpl_error_get_code() != CPL_ERROR_NONE) {
199 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
200 cpl_func, __LINE__, cpl_error_get_where());
201 return (
int)cpl_error_get_code();
204 if (plugin == NULL) {
205 cpl_msg_error(cpl_func,
"Null plugin");
206 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
210 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
211 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
212 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
216 recipe = (cpl_recipe *)plugin;
219 if (recipe->parameters == NULL) {
220 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
221 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
223 if (recipe->frames == NULL) {
224 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
225 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
229 recipe_status = gravity_eop(recipe->frames, recipe->parameters);
231 if (!cpl_errorstate_is_equal(initial_errorstate)) {
234 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
237 return recipe_status;
247 static int gravity_eop_destroy(cpl_plugin * plugin)
251 if (plugin == NULL) {
252 cpl_msg_error(cpl_func,
"Null plugin");
253 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
257 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
258 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
259 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
263 recipe = (cpl_recipe *)plugin;
265 cpl_parameterlist_delete(recipe->parameters);
278 static int gravity_eop(cpl_frameset * frameset,
279 const cpl_parameterlist * parlist)
281 const char * eop_host;
282 const char * eop_urlpath;
283 cpl_propertylist * applist;
284 char * timestamp_lastfinal;
285 double mjd_lastfinal;
289 gravity_print_banner ();
290 cpl_msg_set_time_on();
291 cpl_msg_set_component_on();
292 gravi_msg_function_start(1);
296 cpl_errorstate prestate = cpl_errorstate_get();
299 eop_host = gravi_param_get_string (parlist,
"gravity.eop.eop_host");
302 eop_urlpath = gravi_param_get_string (parlist,
"gravity.eop.eop_urlpath");
304 if (!cpl_errorstate_is_equal(prestate)) {
305 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
306 "Could not retrieve the input " 312 const char * eop_data;
314 cpl_msg_info (cpl_func,
"Retrieving EOP file ");
317 if (eop_data == NULL || !cpl_errorstate_is_equal(prestate)) {
318 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
319 "Could not download data from server");
323 cpl_msg_info (cpl_func,
"Convert EOP data to cpl_table");
327 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
329 applist = cpl_propertylist_new();
332 cpl_propertylist_append_string (applist, CPL_DFS_PRO_CATG, GRAVI_EOP_MAP);
333 cpl_propertylist_append_string (applist,
"ESO PRO TECH",
"CATALOG");
334 cpl_propertylist_append_string (applist,
"ESO PRO TYPE",
"IERS");
337 gravity_eop_compute_qc (eop_table, applist, &mjd_lastfinal);
339 timestamp_lastfinal = gravi_convert_to_timestamp (mjd_lastfinal);
342 filename = cpl_sprintf(
"GRAVI_EOP_PARAM.%s.fits", timestamp_lastfinal);
343 cpl_table_save (eop_table, applist, NULL, filename, CPL_IO_CREATE);
345 cpl_msg_info (cpl_func,
"Update the frameset");
348 cpl_frame * product_frame = cpl_frame_new();
349 cpl_frame_set_filename (product_frame, filename);
350 cpl_frame_set_tag (product_frame, CPL_DFS_PRO_CATG);
351 cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_TABLE);
352 cpl_frame_set_group (product_frame, CPL_FRAME_GROUP_PRODUCT);
353 cpl_frame_set_level (product_frame, CPL_FRAME_LEVEL_FINAL);
354 cpl_frameset_insert (frameset, product_frame);
355 cpl_ensure_code (cpl_errorstate_is_equal(prestate), cpl_error_get_code());
358 cpl_propertylist_delete (applist);
359 cpl_table_delete (eop_table);
362 cpl_free (timestamp_lastfinal);
364 gravi_msg_function_exit(1);
365 return (
int)cpl_error_get_code();
368 cpl_error_code gravity_eop_compute_qc (cpl_table * eop_table,
369 cpl_propertylist* header,
370 double * mjd_lastfinal)
373 double mjd_lastprediction;
376 mjd_start = cpl_table_get_double (eop_table,
"MJD", 0, &null);
377 for(
int i = 0 ; i < cpl_table_get_nrow(eop_table); i++)
379 const char * flag = cpl_table_get_string(eop_table,
"FLAG", i);
380 if(!strncmp(flag,
"I", 1))
381 *mjd_lastfinal = cpl_table_get_double(eop_table,
"MJD", i, &null);
382 if(!strncmp(flag,
"P", 1))
383 mjd_lastprediction = cpl_table_get_double(eop_table,
"MJD", i, &null);
386 cpl_msg_info (cpl_func,
"QC EOP MJD START = %.3f", mjd_start);
387 cpl_msg_info (cpl_func,
"QC EOP MJD LAST FINAL = %.3f", *mjd_lastfinal);
388 cpl_msg_info (cpl_func,
"QC EOP MJD LAST PREDICTION = %.3f", mjd_lastprediction);
390 cpl_propertylist_append_double (header,
"ESO QC EOP MJD START", mjd_start);
391 cpl_propertylist_append_double (header,
"ESO QC EOP MJD LAST FINAL", *mjd_lastfinal);
392 cpl_propertylist_append_double (header,
"ESO QC EOP MJD LAST PREDICTION", mjd_lastprediction);
393 cpl_propertylist_append_double (header,
"MJD-OBS", *mjd_lastfinal);
395 return CPL_ERROR_NONE;
char * gravity_eop_download_finals2000A(const char *eop_host, const char *eop_urlpath, int *data_length)
Retrieve the Earth Orientation Parameters computed by IERS.
cpl_table * gravity_eop_data_totable(const char *eop_data, int data_length)
Export a raw string buffer containing EOP data to a CPL table.