GRAVI Pipeline Reference Manual 1.7.2
Loading...
Searching...
No Matches
gravity_eop.c
Go to the documentation of this file.
1/*
2 * This file is part of the GRAVITY Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <cpl.h>
29#include <time.h>
30#include <hdrl_download.h>
31#include <hdrl_utils.h>
32#include "gravi_data.h"
33#include "gravi_dfs.h"
34#include "gravi_pfits.h"
35
36#include "gravi_utils.h"
37
38#include "gravi_eop.h"
39
40/*-----------------------------------------------------------------------------
41 Private function prototypes
42 -----------------------------------------------------------------------------*/
43
44static int gravity_eop_create(cpl_plugin *);
45static int gravity_eop_exec(cpl_plugin *);
46static int gravity_eop_destroy(cpl_plugin *);
47static int gravity_eop(cpl_frameset *, const cpl_parameterlist *);
48cpl_error_code gravity_eop_compute_qc(cpl_table * eop_table,
49 cpl_propertylist* header,
50 double * mjd_lastfinal);
51
52/*-----------------------------------------------------------------------------
53 Static variables
54 -----------------------------------------------------------------------------*/
55
56static const char gravity_eop_short[] = "Download the last values of the Earth Orientation Parameters and DUT from IERS.";
57static const char gravity_eop_description[] =
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"
63 "* Write product\n"
65 "None : No input\n"
67 GRAVI_EOP_MAP" : EOP calibration file (gravity_eop_calib.fits)\n"
68 "";
69
70static const char gravity_eop_name[] = "gravity_eop";
71
72/*-----------------------------------------------------------------------------
73 Function code
74 -----------------------------------------------------------------------------*/
75
76/*----------------------------------------------------------------------------*/
86/*----------------------------------------------------------------------------*/
87int cpl_plugin_get_info(cpl_pluginlist * list)
88{
89 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
90 cpl_plugin * plugin = &recipe->interface;
91
92 if (cpl_plugin_init(plugin,
93 CPL_PLUGIN_API,
94 GRAVI_BINARY_VERSION,
95 CPL_PLUGIN_TYPE_RECIPE,
99 "Cesar Enrique Garcia Dabo",
100 PACKAGE_BUGREPORT,
101 "LL",
105 cpl_msg_error(cpl_func, "Plugin initialization failed");
106 (void)cpl_error_set_where(cpl_func);
107 return 1;
108 }
109
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);
113 return 1;
114 }
115
116 return 0;
117}
118
119/*----------------------------------------------------------------------------*/
127/*----------------------------------------------------------------------------*/
128static int gravity_eop_create(cpl_plugin * plugin)
129{
130 cpl_recipe * recipe;
131 cpl_parameter * p;
132
133 /* Do not create the recipe if an error code is already set */
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();
138 }
139
140 if (plugin == NULL) {
141 cpl_msg_error(cpl_func, "Null plugin");
142 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
143 }
144
145 /* Verify plugin type */
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);
149 }
150
151 /* Get the recipe */
152 recipe = (cpl_recipe *)plugin;
153
154 /* Create the parameters list in the cpl_recipe object */
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);
159 }
160
161 /* --eop_host */
162 p = cpl_parameter_new_value ("gravity.eop.eop_host",
163 CPL_TYPE_STRING,
164 "FTP Host to retrieve the EOP from",
165 "gravity.eop",
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);
170
171 /* --eop_urlpath */
172 p = cpl_parameter_new_value ("gravity.eop.eop_urlpath",
173 CPL_TYPE_STRING,
174 "FTP URL path of the EOP file to retrieve",
175 "gravity.eop",
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);
180
181 return 0;
182}
183
184/*----------------------------------------------------------------------------*/
190/*----------------------------------------------------------------------------*/
191static int gravity_eop_exec(cpl_plugin * plugin)
192{
193
194 cpl_recipe * recipe;
195 int recipe_status;
196 cpl_errorstate initial_errorstate = cpl_errorstate_get();
197
198 /* Return immediately if an error code is already set */
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();
203 }
204
205 if (plugin == NULL) {
206 cpl_msg_error(cpl_func, "Null plugin");
207 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
208 }
209
210 /* Verify plugin type */
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);
214 }
215
216 /* Get the recipe */
217 recipe = (cpl_recipe *)plugin;
218
219 /* Verify parameter and frame lists */
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);
223 }
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);
227 }
228
229 /* Invoke the recipe */
230 recipe_status = gravity_eop(recipe->frames, recipe->parameters);
231
232 if (!cpl_errorstate_is_equal(initial_errorstate)) {
233 /* Dump the error history since recipe execution start.
234 At this point the recipe cannot recover from the error */
235 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
236 }
237
238 return recipe_status;
239}
240
241/*----------------------------------------------------------------------------*/
247/*----------------------------------------------------------------------------*/
248static int gravity_eop_destroy(cpl_plugin * plugin)
249{
250 cpl_recipe * recipe;
251
252 if (plugin == NULL) {
253 cpl_msg_error(cpl_func, "Null plugin");
254 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
255 }
256
257 /* Verify plugin type */
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);
261 }
262
263 /* Get the recipe */
264 recipe = (cpl_recipe *)plugin;
265
266 cpl_parameterlist_delete(recipe->parameters);
267
268 return 0;
269}
270
271cpl_error_code gravity_eop_compute_qc (cpl_table * eop_table,
272 cpl_propertylist* header,
273 double * mjd_lastfinal)
274{
275 double mjd_start;
276 double mjd_lastprediction;
277 int null;
278
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++)
281 {
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);
287 }
288
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);
292
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);
297
298 return CPL_ERROR_NONE;
299}
300/*----------------------------------------------------------------------------*/
307/*----------------------------------------------------------------------------*/
308static int gravity_eop(cpl_frameset * frameset,
309 const cpl_parameterlist * parlist)
310{
311 const char * eop_host;
312 const char * eop_urlpath;
313 cpl_propertylist * applist;
314 char * timestamp_lastfinal;
315 double mjd_lastfinal;
316 char * filename;
317
318 /* Message */
321
322 /* Use the errorstate to detect an error in a function that does not
323 return an error code. */
324 cpl_errorstate prestate = cpl_errorstate_get();
325
326
327 /* Retrieving eop_host */
328 eop_host = gravi_param_get_string (parlist, "gravity.eop.eop_host");
329
330 /* Retrieving eop_urlpath */
331 eop_urlpath = gravi_param_get_string (parlist, "gravity.eop.eop_urlpath");
332
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 "
336 "parameters");
337 }
338
339
340 /* Retrieve EOP file from the site. The data are stored in the buffer
341 eop_data, which should be freed with free() function */
342 char * eop_data;
343 size_t data_length;
344 cpl_msg_info (cpl_func, "Retrieving EOP file ");
345
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);
349
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");
353 }
354
355 /* Convert to a CPL_TABLE */
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);
358
359 /* Check for a change in the CPL error state */
360 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
361
362 applist = cpl_propertylist_new();
363
364 /* Add the product category */
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");
368
369 /* Add a QC parameter */
370 gravity_eop_compute_qc (eop_table, applist, &mjd_lastfinal);
371
372 timestamp_lastfinal = gravi_convert_to_timestamp (mjd_lastfinal);
373
374 /* Saving the product */
375 filename = cpl_sprintf("GRAVI_EOP_PARAM.%s.fits", timestamp_lastfinal);
376 cpl_table_save (eop_table, applist, NULL, filename, CPL_IO_CREATE);
377
378 cpl_msg_info (cpl_func,"Update the frameset");
379
380 /* Updating the frameset */
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());
389
390 /* Cleanup */
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);
396
398 return (int)cpl_error_get_code();
399}
400
401
#define GRAVI_RECIPE_OUTPUT
Definition: gravi_dfs.h:39
#define GRAVI_EOP_MAP
Definition: gravi_dfs.h:78
#define GRAVI_RECIPE_FLOW
Definition: gravi_dfs.h:37
#define GRAVI_RECIPE_INPUT
Definition: gravi_dfs.h:38
cpl_table_save(vis_met, NULL, NULL, "vismet.fits", CPL_IO_CREATE)
cpl_propertylist * header
Definition: gravi_old.c:2004
cpl_msg_info(cpl_func, "Compute WAVE_SCAN for %s", GRAVI_TYPE(type_data))
#define gravi_msg_function_exit(flag)
Definition: gravi_utils.h:85
#define FREE(function, variable)
Definition: gravi_utils.h:69
#define gravi_msg_function_start(flag)
Definition: gravi_utils.h:84
static const char gravity_eop_name[]
Definition: gravity_eop.c:70
static int gravity_eop(cpl_frameset *, const cpl_parameterlist *)
Implement the recipe functionality.
Definition: gravity_eop.c:308
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: gravity_eop.c:87
static const char gravity_eop_short[]
Definition: gravity_eop.c:56
cpl_error_code gravity_eop_compute_qc(cpl_table *eop_table, cpl_propertylist *header, double *mjd_lastfinal)
Definition: gravity_eop.c:271
static int gravity_eop_create(cpl_plugin *)
Setup the recipe options
Definition: gravity_eop.c:128
static int gravity_eop_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
Definition: gravity_eop.c:248
static const char gravity_eop_description[]
Definition: gravity_eop.c:57
static int gravity_eop_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
Definition: gravity_eop.c:191
const char * gravi_param_get_string(const cpl_parameterlist *parlist, const char *name)
Definition: gravi_dfs.c:1550
void gravity_print_banner(void)
Definition: gravi_dfs.c:61
char * gravi_convert_to_timestamp(double mjd)
Definition: gravi_pfits.c:1196