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);
54 static const char gravity_eop_short[] =
"Download the last values of the Earth Orientation Parameters and DUT from IERS.";
55 static const char gravity_eop_description[] =
56 "This recipe download the latest version of the Earth Orientation Parameter \n" 57 "and DUT from the IERS site. A web connection is required.\n" 58 "The recipe doesn't need any input, but it creates the following product:\n" 59 "gravity_eop_calib.fits " GRAVI_EOP_MAP
"\n" 62 static const char gravity_eop_name[] =
"gravity_eop";
79 int cpl_plugin_get_info(cpl_pluginlist * list)
81 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
82 cpl_plugin * plugin = &recipe->interface;
84 if (cpl_plugin_init(plugin,
87 CPL_PLUGIN_TYPE_RECIPE,
90 gravity_eop_description,
91 "Cesar Enrique Garcia Dabo",
96 gravity_eop_destroy)) {
97 cpl_msg_error(cpl_func,
"Plugin initialization failed");
98 (void)cpl_error_set_where(cpl_func);
102 if (cpl_pluginlist_append(list, plugin)) {
103 cpl_msg_error(cpl_func,
"Error adding plugin to list");
104 (void)cpl_error_set_where(cpl_func);
120 static int gravity_eop_create(cpl_plugin * plugin)
126 if (cpl_error_get_code() != CPL_ERROR_NONE) {
127 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
128 cpl_func, __LINE__, cpl_error_get_where());
129 return (
int)cpl_error_get_code();
132 if (plugin == NULL) {
133 cpl_msg_error(cpl_func,
"Null plugin");
134 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
138 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
139 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
140 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
144 recipe = (cpl_recipe *)plugin;
147 recipe->parameters = cpl_parameterlist_new();
148 if (recipe->parameters == NULL) {
149 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
150 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
154 p = cpl_parameter_new_value (
"gravity.eop.eop_host",
156 "FTP Host to retrieve the EOP from",
159 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"eop_host");
160 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
161 cpl_parameterlist_append (recipe->parameters, p);
164 p = cpl_parameter_new_value (
"gravity.eop.eop_urlpath",
166 "FTP URL path of the EOP file to retrieve",
168 "/products/eop/rapid/standard/finals2000A.data");
169 cpl_parameter_set_alias (p, CPL_PARAMETER_MODE_CLI,
"eop_urlpath");
170 cpl_parameter_disable (p, CPL_PARAMETER_MODE_ENV);
171 cpl_parameterlist_append (recipe->parameters, p);
183 static int gravity_eop_exec(cpl_plugin * plugin)
188 cpl_errorstate initial_errorstate = cpl_errorstate_get();
191 if (cpl_error_get_code() != CPL_ERROR_NONE) {
192 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
193 cpl_func, __LINE__, cpl_error_get_where());
194 return (
int)cpl_error_get_code();
197 if (plugin == NULL) {
198 cpl_msg_error(cpl_func,
"Null plugin");
199 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
203 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
204 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
205 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
209 recipe = (cpl_recipe *)plugin;
212 if (recipe->parameters == NULL) {
213 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
214 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
216 if (recipe->frames == NULL) {
217 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
218 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
222 recipe_status = gravity_eop(recipe->frames, recipe->parameters);
224 if (!cpl_errorstate_is_equal(initial_errorstate)) {
227 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
230 return recipe_status;
240 static int gravity_eop_destroy(cpl_plugin * plugin)
244 if (plugin == NULL) {
245 cpl_msg_error(cpl_func,
"Null plugin");
246 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
250 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
251 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
252 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
256 recipe = (cpl_recipe *)plugin;
258 cpl_parameterlist_delete(recipe->parameters);
271 static int gravity_eop(cpl_frameset * frameset,
272 const cpl_parameterlist * parlist)
274 const char * eop_host;
275 const char * eop_urlpath;
276 cpl_propertylist * applist;
279 gravity_print_banner ();
280 cpl_msg_set_time_on();
281 cpl_msg_set_component_on();
282 gravi_msg_function_start(1);
286 cpl_errorstate prestate = cpl_errorstate_get();
289 eop_host = gravi_param_get_string (parlist,
"gravity.eop.eop_host");
292 eop_urlpath = gravi_param_get_string (parlist,
"gravity.eop.eop_urlpath");
294 if (!cpl_errorstate_is_equal(prestate)) {
295 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
296 "Could not retrieve the input " 302 const char * eop_data;
304 cpl_msg_info (cpl_func,
"Retrieving EOP file ");
305 eop_data = gravity_eop_download_finals2000A (eop_host, eop_urlpath, &data_length);
307 if (eop_data == NULL || !cpl_errorstate_is_equal(prestate)) {
308 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
309 "Could not download data from server");
313 cpl_msg_info (cpl_func,
"Convert EOP data to cpl_table");
314 cpl_table * eop_table = gravity_eop_data_totable (eop_data, data_length);
317 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
319 applist = cpl_propertylist_new();
322 cpl_propertylist_append_string (applist, CPL_DFS_PRO_CATG, GRAVI_EOP_MAP);
323 cpl_propertylist_append_string (applist,
"ESO PRO TECH",
"CATALOG");
324 cpl_propertylist_append_string (applist,
"ESO PRO TYPE",
"IERS");
327 gravity_eop_compute_qc (eop_table, applist);
330 cpl_table_save (eop_table, applist, NULL,
"gravity_eop.fits", CPL_IO_CREATE);
332 cpl_msg_info (cpl_func,
"Update the frameset");
335 cpl_frame * product_frame = cpl_frame_new();
336 cpl_frame_set_filename (product_frame,
"gravity_eop.fits");
337 cpl_frame_set_tag (product_frame, CPL_DFS_PRO_CATG);
338 cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_TABLE);
339 cpl_frame_set_group (product_frame, CPL_FRAME_GROUP_PRODUCT);
340 cpl_frame_set_level (product_frame, CPL_FRAME_LEVEL_FINAL);
341 cpl_frameset_insert (frameset, product_frame);
342 cpl_ensure_code (cpl_errorstate_is_equal(prestate), cpl_error_get_code());
345 cpl_propertylist_delete (applist);
346 cpl_table_delete (eop_table);
348 gravi_msg_function_exit(1);
349 return (
int)cpl_error_get_code();
352 cpl_error_code gravity_eop_compute_qc (cpl_table * eop_table,
353 cpl_propertylist* header)
356 double mjd_lastfinal;
357 double mjd_lastprediction;
360 mjd_start = cpl_table_get_double (eop_table,
"MJD", 0, &null);
361 for(
int i = 0 ; i < cpl_table_get_nrow(eop_table); i++)
363 const char * flag = cpl_table_get_string(eop_table,
"FLAG", i);
364 if(!strncmp(flag,
"I", 1))
365 mjd_lastfinal = cpl_table_get_double(eop_table,
"MJD", i, &null);
366 if(!strncmp(flag,
"P", 1))
367 mjd_lastprediction = cpl_table_get_double(eop_table,
"MJD", i, &null);
370 cpl_msg_info (cpl_func,
"QC EOP MJD START = %.3f", mjd_start);
371 cpl_msg_info (cpl_func,
"QC EOP MJD LAST FINAL = %.3f", mjd_lastfinal);
372 cpl_msg_info (cpl_func,
"QC EOP MJD LAST PREDICTION = %.3f", mjd_lastprediction);
374 cpl_propertylist_append_double (header,
"ESO QC EOP MJD START", mjd_start);
375 cpl_propertylist_append_double (header,
"ESO QC EOP MJD LAST FINAL", mjd_lastfinal);
376 cpl_propertylist_append_double (header,
"ESO QC EOP MJD LAST PREDICTION", mjd_lastprediction);
377 cpl_propertylist_append_double (header,
"MJD-OBS", mjd_lastfinal);
379 return CPL_ERROR_NONE;