1 /*
2  * Copyright 2005-2018 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 /*! \file grib_api.h
12   \brief grib_api C header file
13 
14   This is the only file that must be included to use the grib_api library
15   from C.
16 */
17 
18 #ifndef grib_api_H
19 #define grib_api_H
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 
30 /* Microsoft Windows Visual Studio support */
31 #include "grib_api_windef.h"
32 
33 #ifndef GRIB_ON_WINDOWS
34 #include <sys/time.h>
35 #endif
36 #include <math.h>
37 
38 #if defined( __GNUC__) || defined(__clang__)
39 #define GRIB_API_DEPRECATED __attribute__((deprecated))
40 #else
41 #define GRIB_API_DEPRECATED
42 #endif
43 
44 
45 #include "grib_api_version.h"
46 
47 /* sections */
48 #define GRIB_SECTION_PRODUCT 	(1<<0)
49 #define GRIB_SECTION_GRID 		(1<<1)
50 #define GRIB_SECTION_LOCAL 		(1<<2)
51 #define GRIB_SECTION_DATA 		(1<<3)
52 #define GRIB_SECTION_BITMAP		(1<<4)
53 
54 
55 /* LOG MODES
56 Log mode for information for processing information
57 */
58 /*  Log mode for info */
59 #define GRIB_LOG_INFO           0
60 /*  Log mode for warnings */
61 #define GRIB_LOG_WARNING        1
62 /*  Log mode for errors */
63 #define GRIB_LOG_ERROR          2
64 /*  Log mode for fatal errors */
65 #define GRIB_LOG_FATAL          3
66 /*  Log mode for debug */
67 #define GRIB_LOG_DEBUG          4
68 
69 /* Types */
70 /*  undefined */
71 #define GRIB_TYPE_UNDEFINED 0
72 /*  long integer */
73 #define GRIB_TYPE_LONG 1
74 /*  double */
75 #define GRIB_TYPE_DOUBLE 2
76 /*  char*    */
77 #define GRIB_TYPE_STRING 3
78 /*  bytes */
79 #define GRIB_TYPE_BYTES 4
80 /*  section */
81 #define GRIB_TYPE_SECTION 5
82 /*  label */
83 #define GRIB_TYPE_LABEL 6
84 /*  missing */
85 #define GRIB_TYPE_MISSING 7
86 
87 /* Missing values */
88 /* #define GRIB_MISSING_LONG   0x80000001*/
89 #define GRIB_MISSING_LONG 0xffffffff
90 #define GRIB_MISSING_DOUBLE -1e+100
91 
92 /*set spec flags*/
93 #define GRIB_UTIL_SET_SPEC_FLAGS_ONLY_PACKING	(1<<0)
94 
95 /* Dump option flags*/
96 #define GRIB_DUMP_FLAG_READ_ONLY    (1<<0)
97 #define GRIB_DUMP_FLAG_DUMP_OK      (1<<1)
98 #define GRIB_DUMP_FLAG_VALUES       (1<<2)
99 #define GRIB_DUMP_FLAG_CODED        (1<<3)
100 #define GRIB_DUMP_FLAG_OCTECT       (1<<4)
101 #define GRIB_DUMP_FLAG_ALIASES      (1<<5)
102 #define GRIB_DUMP_FLAG_TYPE         (1<<6)
103 #define GRIB_DUMP_FLAG_HEXADECIMAL  (1<<7)
104 #define GRIB_DUMP_FLAG_NO_DATA      (1<<8)
105 #define GRIB_DUMP_FLAG_ALL_DATA      (1<<9)
106 
107 /* grib_nearest flags */
108 #define GRIB_NEAREST_SAME_GRID   (1<<0)
109 #define GRIB_NEAREST_SAME_DATA   (1<<1)
110 #define GRIB_NEAREST_SAME_POINT  (1<<2)
111 
112 /*! Iteration is carried out on all the keys available in the message
113 \ingroup keys_iterator
114 \see grib_keys_iterator_new
115 */
116 #define GRIB_KEYS_ITERATOR_ALL_KEYS            0
117 
118 /*! read only keys are skipped by keys iterator.
119 \ingroup keys_iterator
120 \see grib_keys_iterator_new
121 */
122 #define GRIB_KEYS_ITERATOR_SKIP_READ_ONLY          (1<<0)
123 
124 /*! optional keys are skipped by keys iterator.
125 \ingroup keys_iterator
126 \see grib_keys_iterator_new */
127 #define GRIB_KEYS_ITERATOR_SKIP_OPTIONAL           (1<<1)
128 
129 /*! edition specific keys are skipped by keys iterator.
130 \ingroup keys_iterator
131 \see grib_keys_iterator_new */
132 #define GRIB_KEYS_ITERATOR_SKIP_EDITION_SPECIFIC   (1<<2)
133 
134 /*! coded keys are skipped by keys iterator.
135 \ingroup keys_iterator
136 \see grib_keys_iterator_new */
137 #define GRIB_KEYS_ITERATOR_SKIP_CODED              (1<<3)
138 
139 /*! computed keys are skipped by keys iterator.
140 \ingroup keys_iterator
141 \see grib_keys_iterator_new */
142 #define GRIB_KEYS_ITERATOR_SKIP_COMPUTED           (1<<4)
143 
144 /*! duplicates of a key are skipped by keys iterator.
145 \ingroup keys_iterator
146 \see grib_keys_iterator_new */
147 #define GRIB_KEYS_ITERATOR_SKIP_DUPLICATES         (1<<5)
148 
149 /*! function keys are skipped by keys iterator.
150 \ingroup keys_iterator
151 \see grib_keys_iterator_new */
152 #define GRIB_KEYS_ITERATOR_SKIP_FUNCTION           (1<<6)
153 
154 typedef struct grib_key_value_list grib_key_value_list;
155 
156 typedef struct grib_values grib_values;
157 
158 struct grib_values {
159   const char* name;
160   int         type;
161   long        long_value;
162   double      double_value;
163   const char* string_value;
164   int         error;
165   int         has_value;
166   int         equal;
167   grib_values* next;
168 } ;
169 
170 
171 /*! Grib handle,   structure giving access to parsed grib values by keys
172     \ingroup grib_handle
173 */
174 typedef struct grib_handle    grib_handle;
175 
176 /*! Grib multi field handle,   structure used to build multi fields messages.
177     \ingroup grib_handle
178  */
179 typedef struct grib_multi_handle    grib_multi_handle;
180 
181 /*! Grib context,  structure containing the memory methods, the parsers and the formats.
182     \ingroup grib_context
183 */
184 typedef struct grib_context   grib_context;
185 
186 /*! Grib iterator, structure supporting a geographic iteration of values on a grib message.
187     \ingroup grib_iterator
188 */
189 typedef struct grib_iterator  grib_iterator;
190 
191 /*! Grib nearest, structure used to find the nearest points of a latitude longitude point.
192     \ingroup grib_iterator
193 */
194 typedef struct grib_nearest  grib_nearest;
195 
196 /*! Grib box, structure used to crop a box given north/west/south/east boundaries.
197     \ingroup grib_box
198 */
199 typedef struct grib_box  grib_box;
200 typedef struct grib_points  grib_points;
201 
202 /*! Grib keys iterator. Iterator over keys.
203     \ingroup keys_iterator
204 */
205 typedef struct grib_keys_iterator    grib_keys_iterator;
206 
207 
208 typedef struct grib_fieldset grib_fieldset;
209 
210 typedef struct grib_order_by grib_order_by;
211 typedef struct grib_where grib_where;
212 
213 typedef struct grib_darray grib_darray;
214 typedef struct grib_iarray grib_iarray;
215 
216 grib_fieldset *grib_fieldset_new_from_files(grib_context *c, char *filenames[], int nfiles, char **keys, int nkeys, char *where_string, char *order_by_string, int *err);
217 
218 
219 
220 void grib_fieldset_delete(grib_fieldset* set);
221 void grib_fieldset_rewind(grib_fieldset* set);
222 int grib_fieldset_apply_order_by(grib_fieldset* set,const char* order_by_string);
223 grib_handle* grib_fieldset_next_handle(grib_fieldset* set,int* err);
224 int grib_fieldset_count(grib_fieldset *set);
225 int grib_values_check(grib_handle* h, grib_values* values, int count);
226 
227 /*! \defgroup grib_index The grib_index
228 The grib_index is the structure giving indexed access to messages in a file.
229  */
230 /*! @{*/
231 
232 /*! index structure to access messages in a file.
233 */
234 typedef struct grib_index grib_index;
235 
236 /**
237  *  Create a new index form a file. The file is indexed with the keys in argument.
238  *
239  * @param c           : context  (NULL for default context)
240  * @param filename    : name of the file of messages to be indexed
241  * @param keys        : comma separated list of keys for the index.
242  *    The type of the key can be explicitly declared appending :l for long,
243  *    :d for double, :s for string to the key name. If the type is not
244  *    declared explicitly, the native type is assumed.
245  * @param err         :  0 if OK, integer value on error
246  * @return            the newly created index
247  */
248 grib_index* grib_index_new_from_file(grib_context* c,
249                             char* filename,const char* keys,int *err);
250 /**
251  *  Create a new index based on a set of keys.
252  *
253  * @param c           : context  (NULL for default context)
254  * @param keys        : comma separated list of keys for the index.
255  *    The type of the key can be explicitly declared appending :l for long,
256  *    :d for double, :s for string to the key name. If the type is not
257  *    declared explicitly, the native type is assumed.
258  * @param err         :  0 if OK, integer value on error
259  * @return            the newly created index
260  */
261 grib_index* grib_index_new(grib_context* c, const char* keys,int *err);
262 
263 /**
264  *  Indexes the file given in argument in the index given in argument.
265  *
266  * @param index       : index
267  * @param filename    : name of the file of messages to be indexed
268  * @return            0 if OK, integer value on error
269  */
270 int grib_index_add_file(grib_index *index, const char *filename);
271 int grib_index_write(grib_index *index, const char *filename);
272 grib_index* grib_index_read(grib_context* c,const char* filename,int *err);
273 
274 /**
275  *  Get the number of distinct values of the key in argument contained in the index. The key must belong to the index.
276  *
277  * @param index       : an index created from a file.
278  *     The index must have been created with the key in argument.
279  * @param key         : key for which the number of values is computed
280  * @param size        : number of distinct values of the key in the index
281  * @return            0 if OK, integer value on error
282  */
283 int grib_index_get_size(grib_index* index,const char* key,size_t* size);
284 
285 /**
286  *  Get the distinct values of the key in argument contained in the index. The key must belong to the index. This function is used when the type of the key was explicitly defined as long or when the native type of the key is long.
287  *
288  * @param index       : an index created from a file.
289  *     The index must have been created with the key in argument.
290  * @param key         : key for which the values are returned
291  * @param values      : array of values. The array must be allocated before entering this function and its size must be enough to contain all the values.
292  * @param size        : size of the values array
293  * @return            0 if OK, integer value on error
294  */
295 int grib_index_get_long(grib_index* index,const char* key,
296                         long* values,size_t *size);
297 
298 /**
299  *  Get the distinct values of the key in argument contained in the index. The key must belong to the index. This function is used when the type of the key was explicitly defined as double or when the native type of the key is double.
300  *
301  * @param index       : an index created from a file.
302  *     The index must have been created with the key in argument.
303  * @param key         : key for which the values are returned
304  * @param values      : array of values. The array must be allocated before entering this function and its size must be enough to contain all the values.
305  * @param size        : size of the values array
306  * @return            0 if OK, integer value on error
307  */
308 int grib_index_get_double(grib_index* index,const char* key,
309                           double* values,size_t *size);
310 
311 /**
312  *  Get the distinct values of the key in argument contained in the index. The key must belong to the index. This function is used when the type of the key was explicitly defined as string or when the native type of the key is string.
313  *
314  * @param index       : an index created from a file.
315  *     The index must have been created with the key in argument.
316  * @param key         : key for which the values are returned
317  * @param values      : array of values. The array must be allocated before entering this function and its size must be enough to contain all the values.
318  * @param size        : size of the values array
319  * @return            0 if OK, integer value on error
320  */
321 int grib_index_get_string(grib_index* index,const char* key,
322                           char** values,size_t *size);
323 
324 
325 /**
326  *  Select the message subset with key==value. The value is a long. The key must have been created with long type or have long as native type if the type was not explicitly defined in the index creation.
327  *
328  * @param index       : an index created from a file.
329  *     The index must have been created with the key in argument.
330  * @param key         : key to be selected
331  * @param value       : value of the key to select
332  * @return            0 if OK, integer value on error
333  */
334 int grib_index_select_long(grib_index* index,const char* key,long value);
335 
336 /**
337  *  Select the message subset with key==value. The value is a double. The key must have been created with double type or have double as native type if the type was not explicitly defined in the index creation.
338  *
339  * @param index       : an index created from a file.
340  *     The index must have been created with the key in argument.
341  * @param key         : key to be selected
342  * @param value       : value of the key to select
343  * @return            0 if OK, integer value on error
344  */
345 int grib_index_select_double(grib_index* index,const char* key,double value);
346 
347 /**
348  *  Select the message subset with key==value. The value is a string. The key must have been created with string type or have string as native type if the type was not explicitly defined in the index creation.
349  *
350  * @param index       : an index created from a file.
351  *     The index must have been created with the key in argument.
352  * @param key         : key to be selected
353  * @param value       : value of the key to select
354  * @return            0 if OK, integer value on error
355  */
356 int grib_index_select_string(grib_index* index,const char* key,char* value);
357 
358 /**
359  *  Create a new handle from an index after having selected the key values.
360  *  All the keys belonging to the index must be selected before calling this function. Successive calls to this function will return all the handles compatible with the constraints defined selecting the values of the index keys.
361  * When no more handles are available from the index a NULL pointer is returned and the err variable is set to GRIB_END_OF_INDEX.
362  *
363  * @param index       : an index created from a file.
364  * @param err         :  0 if OK, integer value on error. GRIB_END_OF_INDEX when no more handles are contained in the index.
365  * @return            grib handle.
366  */
367 grib_handle* grib_handle_new_from_index(grib_index* index,int *err);
368 
369 /**
370  *  Delete the index.
371  *
372  * @param index       : index to be deleted.
373  */
374 void grib_index_delete(grib_index* index);
375 
376 /*! @} */
377 
378 /*! \defgroup grib_handle The grib_handle
379 The grib_handle is the structure giving access to parsed grib values by keys.
380 */
381 /*! @{*/
382 /**
383 *  Counts the messages contained in a file resource.
384 *
385 * @param c           : the context from which the handle will be created (NULL for default context)
386 * @param f           : the file resource
387 * @param n           : the number of messages in the file
388 * @return            0 if OK, integer value on error
389 */
390 int grib_count_in_file(grib_context* c, FILE* f,int* n);
391 
392 /**
393 *  Create a handle from a file resource.
394 *  The file is read until a message is found. The message is then copied.
395 *  Remember always to delete the handle when it is not needed anymore to avoid
396 *  memory leaks.
397 *
398 * @param c           : the context from which the handle will be created (NULL for default context)
399 * @param f           : the file resource
400 * @param error       : error code set if the returned handle is NULL and the end of file is not reached
401 * @return            the new handle, NULL if the resource is invalid or a problem is encountered
402 */
403 grib_handle* grib_handle_new_from_file(grib_context* c, FILE* f, int* error);
404 
405 /**
406 *  Write a coded message in a file.
407 *
408 * @param h           : grib_handle to be written
409 * @param file        : name of the file
410 * @param mode        : mode
411 * @return            0 if OK, integer value on error
412 */
413 int grib_write_message(grib_handle* h,const char* file,const char* mode);
414 
415 typedef struct grib_string_list grib_string_list;
416 struct grib_string_list {
417   char* value;
418   grib_string_list* next;
419 };
420 
421 grib_handle* grib_util_sections_copy(grib_handle* hfrom,grib_handle* hto,int what,int *err);
422 grib_string_list* grib_util_get_param_id(const char* mars_param);
423 grib_string_list* grib_util_get_mars_param(const char* param_id);
424 
425 /**
426 *  Create a handle from a user message in memory. The message will not be freed at the end.
427 *  The message will be copied as soon as a modification is needed.
428 *
429 * @param c           : the context from which the handle will be created (NULL for default context)
430 * @param data        : the actual message
431 * @param data_len    : the length of the message in number of bytes
432 * @return            the new handle, NULL if the message is invalid or a problem is encountered
433 */
434 grib_handle* grib_handle_new_from_message(grib_context* c, void* data, size_t data_len);
435 
436 /**
437 *  Create a handle from a user message in memory. The message will not be freed at the end.
438 *  The message will be copied as soon as a modification is needed.
439 *  This function works also with multi field messages.
440 *  Note: The data pointer argument may be modified
441 *
442 * @param c           : the context from which the handle will be created (NULL for default context)
443 * @param data        : the actual message
444 * @param data_len    : the length of the message in number of bytes
445 * @param error       : error code
446 * @return            the new handle, NULL if the message is invalid or a problem is encountered
447 */
448 grib_handle* grib_handle_new_from_multi_message(grib_context* c,void** data,
449                                                 size_t *data_len,int* error);
450 
451 /**
452 *  Create a handle from a user message. The message is copied and will be freed with the handle
453 *
454 * @param c           : the context from which the handle will be created (NULL for default context)
455 * @param data        : the actual message
456 * @param data_len    : the length of the message in number of bytes
457 * @return            the new handle, NULL if the message is invalid or a problem is encountered
458 */
459 grib_handle* grib_handle_new_from_message_copy(grib_context* c, const void* data, size_t data_len);
460 
461 
462 /**
463 *  Create a handle from a read_only template resource.
464 *  The message is copied at the creation of the handle
465 *  This function is deprecated: please use grib_handle_new_from_samples
466 *
467 * @param c           : the context from which the handle will be created (NULL for default context)
468 * @param res_name    : the resource name
469 * @return            the new handle, NULL if the resource is invalid or a problem is encountered
470 */
471 GRIB_API_DEPRECATED grib_handle* grib_handle_new_from_template (grib_context* c, const char* res_name)  ;
472 
473 /**
474  *  Create a handle from a message contained in a samples directory.
475  *  The message is copied at the creation of the handle
476  *
477  * @param c           : the context from which the handle will be created (NULL for default context)
478  * @param res_name    : the resource name
479  * @return            the new handle, NULL if the resource is invalid or a problem is encountered
480  */
481 grib_handle* grib_handle_new_from_samples (grib_context* c, const char* res_name)  ;
482 
483 
484 
485 /**
486 *  Clone an existing handle using the context of the original handle,
487 *  The message is copied and reparsed
488 *
489 * @param h           : The handle to be cloned
490 * @return            the new handle, NULL if the message is invalid or a problem is encountered
491 */
492 grib_handle* grib_handle_clone             (grib_handle* h)                 ;
493 
494 /**
495 *  Frees a handle, also frees the message if it is not a user message
496 *  @see  grib_handle_new_from_message
497 * @param h           : The handle to be deleted
498 * @return            0 if OK, integer value on error
499 */
500 int   grib_handle_delete   (grib_handle* h);
501 
502 /**
503  *  Create an empty multi field handle.
504  *  Remember always to delete the multi handle when it is not needed anymore to avoid
505  *  memory leaks.
506  *
507  * @param c           : the context from which the handle will be created (NULL for default context)
508  */
509 grib_multi_handle* grib_multi_handle_new     (grib_context* c);
510 
511 /**
512  *  Append the sections starting with start_section of the message pointed by h at
513  *  the end of the multi field handle mh.
514  *  Remember always to delete the multi handle when it is not needed anymore to avoid
515  *  memory leaks.
516  *
517  * @param h           : The handle from which the sections are copied.
518  * @param start_section : section number. Starting from this section all the sections to the end of the message will be copied.
519  * @param mh           : The multi field handle on which the sections are appended.
520  * @return            0 if OK, integer value on error
521  */
522 int grib_multi_handle_append(grib_handle* h,int start_section,grib_multi_handle* mh);
523 
524 /**
525  * Delete multi field handle.
526  *
527  * @param mh           : The multi field handle to be deleted.
528  * @return            0 if OK, integer value on error
529  */
530 int grib_multi_handle_delete(grib_multi_handle* mh);
531 
532 /**
533  *  Write a multi field handle in a file.
534  *  Remember always to delete the multi handle when it is not needed anymore to avoid
535  *  memory leaks.
536  *
537  * @param mh           : The multi field handle to be written.
538  * @param f            : File on which the file handle is written.
539  * @return            0 if OK, integer value on error
540  */
541 int grib_multi_handle_write(grib_multi_handle* mh,FILE* f);
542 
543 /*! @} */
544 
545 /*! \defgroup handling_coded_messages Handling coded messages */
546 /*! @{ */
547 /**
548 * getting the message attached to a handle
549 *
550 * @param h              : the grib handle to which the buffer should be gathered
551 * @param message        : the pointer to be set to the handle's data
552 * @param message_length : On exit, the message size in number of bytes
553 * @return            0 if OK, integer value on error
554 */
555 int grib_get_message(grib_handle* h ,const void** message, size_t *message_length);
556 
557 
558 /**
559 * getting a copy of the message attached to a handle
560 *
561 * @param h              : the grib handle to which the buffer should be returned
562 * @param message        : the pointer to the data buffer to be filled
563 * @param message_length : On entry, the size in number of bytes of the allocated empty message.
564 *                         On exit, the actual message length in number of bytes
565 * @return            0 if OK, integer value on error
566 */
567 int grib_get_message_copy(grib_handle* h ,  void* message,size_t *message_length);
568 /*! @} */
569 
570 /*! \defgroup iterators Iterating on latitude/longitude/values */
571 /*! @{ */
572 
573 /*!
574 * \brief Create a new iterator from a handle, using current geometry and values.
575 *
576 * \param h           : the handle from which the iterator will be created
577 * \param flags       : flags for future use.
578 * \param error       : error code
579 * \return            the new iterator, NULL if no iterator can be created
580 */
581 grib_iterator*      grib_iterator_new      (grib_handle*   h, unsigned long flags,int* error);
582 
583 /**
584 * Get latitude/longitude and data values.
585 * The Latitudes, longitudes and values arrays must be properly allocated by the caller.
586 * Their required dimension can be obtained by getting the value of the integer key "numberOfPoints".
587 *
588 * @param h           : handle from which geography and data values are taken
589 * @param lats        : returned array of latitudes
590 * @param lons        : returned array of longitudes
591 * @param values      : returned array of data values
592 * @return            0 if OK, integer value on error
593 */
594 int grib_get_data(grib_handle *h, double *lats, double *lons, double *values, size_t *size);
595 
596 /**
597 * Get the next value from an iterator.
598 *
599 * @param i           : the iterator
600 * @param lat         : on output latitude in degree
601 * @param lon         : on output longitude in degree
602 * @param value       : on output value of the point
603 * @return            positive value if successful, 0 if no more data are available
604 */
605 int                 grib_iterator_next     (grib_iterator *i, double* lat,double* lon,double* value);
606 
607 /**
608 * Get the previous value from an iterator.
609 *
610 * @param i           : the iterator
611 * @param lat         : on output latitude in degree
612 * @param lon         : on output longitude in degree
613 * @param value       : on output value of the point*
614 * @return            positive value if successful, 0 if no more data are available
615 */
616 int                 grib_iterator_previous (grib_iterator *i, double* lat,double* lon,double* value);
617 
618 /**
619 * Test procedure for values in an iterator.
620 *
621 * @param i           : the iterator
622 * @return            boolean, 1 if the iterator still nave next values, 0 otherwise
623 */
624 int                 grib_iterator_has_next (grib_iterator *i);
625 
626 /**
627 * Test procedure for values in an iterator.
628 *
629 * @param i           : the iterator
630 * @return            0 if OK, integer value on error
631 */
632 int                 grib_iterator_reset    (grib_iterator *i);
633 
634 /**
635 *  Frees an iterator from memory
636 *
637 * @param i           : the iterator
638 * @return            0 if OK, integer value on error
639 */
640 int                 grib_iterator_delete   (grib_iterator *i);
641 
642 /*!
643 * \brief Create a new nearest from a handle, using current geometry .
644 *
645 * \param h           : the handle from which the iterator will be created
646 * \param error       : error code
647 * \return            the new nearest, NULL if no nearest can be created
648 */
649 grib_nearest*      grib_nearest_new      (grib_handle*   h, int* error);
650 
651 /**
652 * Find the 4 nearest points of a latitude longitude point.
653 * The flags are provided to speed up the process of searching. If you are
654 * sure that the point you are asking for is not changing from a call
655 * to another you can use GRIB_NEAREST_SAME_POINT. The same is valid for
656 * the grid. Flags can be used together doing a bitwise OR.
657 * The distances are given in kilometres.
658 *
659 * @param nearest     : nearest structure
660 * @param h           : handle from which geography and data values are taken
661 * @param inlat       : latitude of the point to search for
662 * @param inlon       : longitude of the point to search for
663 * @param flags       : GRIB_NEAREST_SAME_POINT, GRIB_NEAREST_SAME_GRID
664 * @param outlats     : returned array of latitudes of the nearest points
665 * @param outlons     : returned array of longitudes of the nearest points
666 * @param values      : returned array of data values of the nearest points
667 * @param distances   : returned array of distances from the nearest points
668 * @param indexes     : returned array of indexes of the nearest points
669 * @param len         : size of the arrays
670 * @return            0 if OK, integer value on error
671 */
672 int grib_nearest_find(grib_nearest *nearest,grib_handle* h,double inlat,double inlon,
673     unsigned long flags,double* outlats,double* outlons,
674     double* values,double* distances,int* indexes,size_t *len);
675 
676 /**
677 *  Frees an nearest from memory
678 *
679 * @param nearest           : the nearest
680 * @return            0 if OK, integer value on error
681 */
682 int                 grib_nearest_delete   (grib_nearest *nearest);
683 
684 /**
685 * Find the nearest point of a set of points whose latitudes and longitudes
686 * are given in the inlats, inlons arrays respectively.
687 * If the flag is_lsm is 1 the nearest land point is returned and the
688 * grib passed as handle (h) is considered a land sea mask.
689 * The land nearest point is the nearest point with land sea mask value>=0.5.
690 * If no nearest land points are found the nearest value is returned.
691 * If the flag is_lsm is 0 the nearest point is returned.
692 * values, distances, indexes (in the "values" array) for the nearest points (ilons,ilats)
693 * are returned.
694 * The distances are given in kilometres.
695 *
696 * @param h           : handle from which geography and data values are taken
697 * @param is_lsm      : lsm flag (1-> nearest land, 0-> nearest)
698 * @param inlats      : latitudes of the points to search for
699 * @param inlons      : longitudes of the points to search for
700 * @param npoints     : number of points (size of the inlats,inlons,outlats,outlons,values,distances,indexes arrays)
701 * @param outlats     : returned array of latitudes of the nearest points
702 * @param outlons     : returned array of longitudes of the nearest points
703 * @param values      : returned array of data values of the nearest points
704 * @param distances   : returned array of distances from the nearest points
705 * @param indexes     : returned array of indexes of the nearest points
706 * @return            0 if OK, integer value on error
707 */
708 int grib_nearest_find_multiple(grib_handle* h,int is_lsm,
709     double* inlats,double* inlons,long npoints,
710     double* outlats,double* outlons,
711     double* values,double* distances, int* indexes);
712 
713 /* @} */
714 
715 /*! \defgroup get_set Accessing header and data values   */
716 /*! @{ */
717 /**
718 *  Get the number offset of a key, in a message if several keys of the same name
719 *  are present, the offset of the last one is returned
720 *
721 * @param h           : the handle to get the offset from
722 * @param key         : the key to be searched
723 * @param offset      : the address of a size_t where the offset will be set
724 * @return            0 if OK, integer value on error
725 */
726 int                  grib_get_offset(grib_handle* h, const char* key, size_t* offset);
727 
728 /**
729 *  Get the number of coded value from a key, if several keys of the same name are present, the total sum is returned
730 *
731 * @param h           : the handle to get the offset from
732 * @param key         : the key to be searched
733 * @param size        : the address of a size_t where the size will be set
734 * @return            0 if OK, integer value on error
735 */
736 int         grib_get_size(grib_handle* h, const char* key,size_t *size);
737 
738 /**
739 *  Get the length of the string representation of the key, if several keys of the same name are present, the maximum length is returned
740 *
741 * @param h           : the handle to get the offset from
742 * @param key         : the key to be searched
743 * @param length        : the address of a size_t where the length will be set
744 * @return            0 if OK, integer value on error
745 */
746 int         grib_get_length(grib_handle* h, const char* key,size_t *length);
747 
748 /**
749 *  Get a long value from a key, if several keys of the same name are present, the last one is returned
750 *  @see  grib_set_long
751 *
752 * @param h           : the handle to get the data from
753 * @param key         : the key to be searched
754 * @param value       : the address of a long where the data will be retrieved
755 * @return            0 if OK, integer value on error
756 */
757 int          grib_get_long         (grib_handle* h, const char* key, long*   value);
758 
759 /**
760 *  Get a double value from a key, if several keys of the same name are present, the last one is returned
761 *  @see  grib_set_double
762 *
763 * @param h           : the handle to get the data from
764 * @param key         : the key to be searched
765 * @param value       : the address of a double where the data will be retrieved
766 * @return            0 if OK, integer value on error
767 */
768 int grib_get_double       (grib_handle* h, const char* key, double* value);
769 
770 /**
771 *  Get as double the i-th element of the "key" array
772 *
773 * @param h           : the handle to get the data from
774 * @param key         : the key to be searched
775 * @param i           : zero based index
776 * @param value       : the address of a double where the data will be retrieved
777 * @return            0 if OK, integer value on error
778 */
779 int grib_get_double_element(grib_handle* h, const char* key, int i, double* value);
780 
781 /**
782 *  Get as double array the elements of the "key" array whose indexes are listed in the input array i
783 *
784 * @param h           : the handle to get the data from
785 * @param key         : the key to be searched
786 * @param i           : zero based array of indexes
787 * @param size        : size of the i and value arrays
788 * @param value       : the address of a double where the data will be retrieved
789 * @return            0 if OK, integer value on error
790 */
791 int grib_get_double_elements(grib_handle* h, const char* key, int* i, long size,double* value);
792 
793 /**
794 *  Get a string value from a key, if several keys of the same name are present, the last one is returned
795 * @see  grib_set_string
796 *
797 * @param h           : the handle to get the data from
798 * @param key         : the key to be searched
799 * @param mesg       : the address of a string where the data will be retrieved
800 * @param length      : the address of a size_t that contains allocated length of the string on input, and that contains the actual length of the string on output
801 * @return            0 if OK, integer value on error
802 */
803 int grib_get_string       (grib_handle* h, const char* key, char*   mesg,             size_t *length);
804 
805 /**
806 *  Get raw bytes values from a key. If several keys of the same name are present, the last one is returned
807 * @see  grib_set_bytes
808 *
809 * @param h           : the handle to get the data from
810 * @param key         : the key to be searched
811 * @param bytes       : the address of a byte array where the data will be retrieved
812 * @param length      : the address of a size_t that contains allocated length of the byte array on input, and that contains the actual length of the byte array on output
813 * @return            0 if OK, integer value on error
814 */
815 int grib_get_bytes        (grib_handle* h, const char* key, unsigned char*  bytes, size_t *length);
816 /**
817 *  Get double array values from a key. If several keys of the same name are present, the last one is returned
818 * @see  grib_set_double_array
819 *
820 * @param h           : the handle to get the data from
821 * @param key         : the key to be searched
822 * @param vals       : the address of a double array where the data will be retrieved
823 * @param length      : the address of a size_t that contains allocated length of the double array on input, and that contains the actual length of the double array on output
824 * @return            0 if OK, integer value on error
825 */
826 int grib_get_double_array (grib_handle* h, const char* key, double* vals, size_t *length);
827 
828 /**
829 *  Get long array values from a key. If several keys of the same name are present, the last one is returned
830 * @see  grib_set_long_array
831 *
832 * @param h           : the handle to get the data from
833 * @param key         : the key to be searched
834 * @param vals       : the address of a long array where the data will be retrieved
835 * @param length      : the address of a size_t that contains allocated length of the long array on input, and that contains the actual length of the long array on output
836 * @return            0 if OK, integer value on error
837 */
838 int grib_get_long_array   (grib_handle* h, const char* key, long* vals, size_t *length);
839 
840 
841 /*   setting      data         */
842 /**
843 *  Copy the keys belonging to a given namespace from a source handle to a destination handle
844 *
845 *
846 * @param dest      : destination handle
847 * @param name      : namespace
848 * @param src       : source handle
849 * @return          0 if OK, integer value on error
850 */
851 int grib_copy_namespace(grib_handle* dest, const char* name, grib_handle* src);
852 
853 /**
854 *  Set a long value from a key. If several keys of the same name are present, the last one is set
855 *  @see  grib_get_long
856 *
857 * @param h           : the handle to set the data to
858 * @param key         : the key to be searched
859 * @param val         : a long where the data will be read
860 * @return            0 if OK, integer value on error
861 */
862 int grib_set_long         (grib_handle* h, const char*  key , long val);
863 
864 /**
865 *  Set a double value from a key. If several keys of the same name are present, the last one is set
866 *  @see  grib_get_double
867 *
868 * @param h           : the handle to set the data to
869 * @param key         : the key to be searched
870 * @param val       : a double where the data will be read
871 * @return            0 if OK, integer value on error
872 */
873 int grib_set_double       (grib_handle* h, const char*  key , double   val);
874 
875 /**
876 *  Set a string value from a key. If several keys of the same name are present, the last one is set
877 *  @see  grib_get_string
878 *
879 * @param h           : the handle to set the data to
880 * @param key         : the key to be searched
881 * @param mesg       : the address of a string where the data will be read
882 * @param length      : the address of a size_t that contains the length of the string on input, and that contains the actual packed length of the string on output
883 * @return            0 if OK, integer value on error
884 */
885 int grib_set_string       (grib_handle* h, const char*  key , const char* mesg, size_t *length);
886 
887 /**
888 *  Set a bytes array from a key. If several keys of the same name are present, the last one is set
889 *  @see  grib_get_bytes
890 *
891 * @param h           : the handle to set the data to
892 * @param key         : the key to be searched
893 * @param bytes       : the address of a byte array where the data will be read
894 * @param length      : the address of a size_t that contains the length of the byte array on input, and that contains the actual packed length of the byte array  on output
895 * @return            0 if OK, integer value on error
896 */
897 int grib_set_bytes        (grib_handle* h, const char*  key, const unsigned char* bytes, size_t *length);
898 
899 /**
900 *  Set a double array from a key. If several keys of the same name are present, the last one is set
901 *   @see  grib_get_double_array
902 *
903 * @param h           : the handle to set the data to
904 * @param key         : the key to be searched
905 * @param vals        : the address of a double array where the data will be read
906 * @param length      : a size_t that contains the length of the byte array on input
907 * @return            0 if OK, integer value on error
908 */
909 int grib_set_double_array (grib_handle* h, const char*  key , const double*        vals   , size_t length);
910 
911 /**
912 * Same as grib_set_double_array but allows setting of READ-ONLY keys like codedValues.
913 * Use with great caution!!
914 */
915 int grib_set_force_double_array(grib_handle* h, const char* key, const double* vals, size_t length);
916 
917 
918 /**
919 *  Set a long array from a key. If several keys of the same name are present, the last one is set
920 *  @see  grib_get_long_array
921 *
922 * @param h           : the handle to set the data to
923 * @param key         : the key to be searched
924 * @param vals        : the address of a long array where the data will be read
925 * @param length      : a size_t that contains the length of the long array on input
926 * @return            0 if OK, integer value on error
927 */
928 int grib_set_long_array   (grib_handle* h, const char*  key , const long*          vals   , size_t length);
929 /*! @} */
930 
931 
932 /**
933 *  Print all keys, with the context print procedure and dump mode to a resource
934 *
935 * @param h            : the handle to be printed
936 * @param out          : output file handle
937 * @param mode         : available dump modes are: debug wmo c_code
938 * @param option_flags : all the GRIB_DUMP_FLAG_x flags can be used
939 * @param arg          : used to provide a format to output data (experimental)
940 */
941 void   grib_dump_content(grib_handle* h,FILE* out,const char* mode, unsigned long option_flags,void* arg);
942 
943 /**
944 *  Print all keys from the parsed definition files available in a context
945 *
946 * @param f           : the File used to print the keys on
947 * @param c           : the context that contains the cached definition files to be printed
948 */
949 void     grib_dump_action_tree(grib_context* c,  FILE* f) ;
950 
951 /*! \defgroup context The context object
952  The context is a long life configuration object of the grib_api.
953  It is used to define special allocation and free routines or
954  to set special grib_api behaviours and variables.
955  */
956 /*! @{ */
957 /**
958 * Grib free procedure, format of a procedure referenced in the context that is used to free memory
959 *
960 * @param c           : the context where the memory freeing will apply
961 * @param data        : pointer to the data to be freed
962 * must match @see grib_malloc_proc
963 */
964 typedef void  (*grib_free_proc)     (const grib_context* c, void* data);
965 
966 /**
967 * Grib malloc procedure, format of a procedure referenced in the context that is used to allocate memory
968 * @param c             : the context where the memory allocation will apply
969 * @param length        : length to be allocated in number of bytes
970 * @return              a pointer to the allocated memory, NULL if no memory can be allocated
971 * must match @see grib_free_proc
972 */
973 typedef void* (*grib_malloc_proc)   (const grib_context* c, size_t length);
974 
975 /**
976 * Grib realloc procedure, format of a procedure referenced in the context that is used to reallocate memory
977 * @param c             : the context where the memory allocation will apply
978 * @param data          : pointer to the data to be reallocated
979 * @param length        : length to be allocated in number of bytes
980 * @return              a pointer to the allocated memory
981 */
982 typedef void* (*grib_realloc_proc)   (const grib_context* c, void* data, size_t length);
983 
984 /**
985 * Grib loc proc, format of a procedure referenced in the context that is used to log internal messages
986 *
987 * @param c             : the context where the logging will apply
988 * @param level         : the log level, as defined in log modes
989 * @param mesg          : the message to be logged
990 */
991 typedef void  (*grib_log_proc)      (const grib_context* c, int level, const char* mesg);
992 
993 /**
994 * Grib print proc, format of a procedure referenced in the context that is used to print external messages
995 *
996 * @param c             : the context where the logging will apply
997 * @param descriptor    : the structure to be printed on, must match the implementation
998 * @param mesg          : the message to be printed
999 */
1000 typedef void  (*grib_print_proc)    (const grib_context* c, void* descriptor, const char* mesg);
1001 
1002 
1003 /**
1004 * Grib data read proc, format of a procedure referenced in the context that is used to read from a stream in a resource
1005 *
1006 * @param c             : the context where the read will apply
1007 * @param *ptr          : the resource
1008 * @param size          : size to read
1009 * @param *stream       : the stream
1010 * @return              size read
1011 */
1012 typedef size_t  (*grib_data_read_proc) (const grib_context* c,void *ptr, size_t size, void *stream);
1013 
1014 /**
1015 * Grib data read write, format of a procedure referenced in the context that is used to write to a stream from a resource
1016 *
1017 * @param c             : the context where the write will apply
1018 * @param *ptr          : the resource
1019 * @param size          : size to read
1020 * @param *stream       : the stream
1021 * @return              size written
1022 */
1023 typedef size_t  (*grib_data_write_proc)(const grib_context* c,const void *ptr, size_t size,  void *stream);
1024 
1025 /**
1026 * Grib data tell, format of a procedure referenced in the context that is used to tell the current position in a stream
1027 *
1028 * @param c             : the context where the tell will apply
1029 * @param *stream       : the stream
1030 * @return              the position in the stream
1031 */
1032 typedef off_t    (*grib_data_tell_proc) (const grib_context* c, void *stream);
1033 
1034 /**
1035 * Grib data seek, format of a procedure referenced in the context that is used to seek the current position in a stream
1036 *
1037 * @param c             : the context where the tell will apply
1038 * @param offset        : the offset to seek to
1039 * @param whence        : If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END,
1040                          the offset  is  relative  to  the start of the file,
1041              the current position indicator, or end-of-file, respectively.
1042 * @param *stream       : the stream
1043 * @return            0 if OK, integer value on error
1044 */
1045 typedef off_t    (*grib_data_seek_proc) (const grib_context* c, off_t offset, int whence, void *stream);
1046 
1047 /**
1048 * Grib data eof, format of a procedure referenced in the context that is used to test end of file
1049 *
1050 * @param c             : the context where the tell will apply
1051 * @param *stream       : the stream
1052 * @return              the position in the stream
1053 */
1054 typedef int    (*grib_data_eof_proc) (const grib_context* c, void *stream);
1055 
1056 /**
1057 *  Get the static default context
1058 *
1059 * @return            the default context, NULL it the context is not available
1060 */
1061 grib_context*    grib_context_get_default(void);
1062 
1063 /**
1064 *  Create and allocate a new context from a parent context.
1065 *
1066 * @param c           : the context to be cloned, NULL for default context
1067 * @return            the new and empty context, NULL if error
1068 */
1069 grib_context*    grib_context_new                        (grib_context* c);
1070 
1071 /**
1072 *  Frees the cached definition files of the context
1073 *
1074 * @param c           : the context to be deleted
1075 */
1076 void             grib_context_delete                     (grib_context* c);
1077 
1078 /**
1079 *  Set the GTS header mode on.
1080 *  The GTS headers will be preserved.
1081 *
1082 * @param c           : the context
1083 */
1084 void grib_gts_header_on(grib_context* c) ;
1085 
1086 /**
1087 *  Set the GTS header mode off.
1088 *  The GTS headers will be deleted.
1089 *
1090 * @param c           : the context
1091 */
1092 void grib_gts_header_off(grib_context* c);
1093 
1094 /**
1095 *  Set the GRIBEX mode on.
1096 *  Grib files will be compatible with GRIBEX.
1097 *
1098 * @param c           : the context
1099 */
1100 void grib_gribex_mode_on(grib_context* c);
1101 
1102 /**
1103 *  Get the GRIBEX mode.
1104 *
1105 * @param c           : the context
1106 */
1107 int grib_get_gribex_mode ( grib_context* c);
1108 
1109 /**
1110 *  Set the GRIBEX mode off.
1111 *  GRIB files won't be always compatible with GRIBEX.
1112 *
1113 * @param c           : the context
1114 */
1115 void grib_gribex_mode_off(grib_context* c);
1116 
1117 /**
1118 *  Sets memory procedures of the context
1119 *
1120 * @param c           : the context to be modified
1121 * @param griballoc   : the memory allocation procedure to be set @see grib_malloc_proc
1122 * @param gribfree    : the memory freeing procedure to be set @see grib_free_proc
1123 */
1124 void grib_context_set_memory_proc(grib_context* c, grib_malloc_proc griballoc,
1125        grib_free_proc gribfree,
1126        grib_realloc_proc gribrealloc);
1127 
1128 /**
1129 *  Sets memory procedures of the context for persistent data
1130 *
1131 * @param c           : the context to be modified
1132 * @param griballoc   : the memory allocation procedure to be set @see grib_malloc_proc
1133 * @param gribfree    : the memory freeing procedure to be set @see grib_free_proc
1134 */
1135 void  grib_context_set_persistent_memory_proc(grib_context* c, grib_malloc_proc griballoc,
1136   grib_free_proc gribfree);
1137 
1138 /**
1139 *  Sets memory procedures of the context for large buffers
1140 *
1141 * @param c           : the context to be modified
1142 * @param griballoc   : the memory allocation procedure to be set @see grib_malloc_proc
1143 * @param gribfree    : the memory freeing procedure to be set @see grib_free_proc
1144 */
1145 void  grib_context_set_buffer_memory_proc(grib_context* c, grib_malloc_proc griballoc,
1146        grib_free_proc gribfree,
1147        grib_realloc_proc gribrealloc);
1148 
1149 /**
1150 *  Sets the context printing procedure used for user interaction
1151 *
1152 * @param c            : the context to be modified
1153 * @param printp       : the printing procedure to be set @see grib_print_proc
1154 */
1155 void   grib_context_set_print_proc(grib_context* c, grib_print_proc printp);
1156 
1157 /**
1158 *  Sets the context logging procedure used for system (warning, errors, infos ...) messages
1159 *
1160 * @param c            : the context to be modified
1161 * @param logp         : the logging procedure to be set @see grib_log_proc
1162 */
1163 void    grib_context_set_logging_proc(grib_context* c, grib_log_proc logp);
1164 
1165 /**
1166 *  Turn on support for multiple fields in single grib messages
1167 *
1168 * @param c            : the context to be modified
1169 */
1170 void grib_multi_support_on(grib_context* c);
1171 
1172 /**
1173 *  Turn off support for multiple fields in single grib messages
1174 *
1175 * @param c            : the context to be modified
1176 */
1177 void grib_multi_support_off(grib_context* c);
1178 
1179 /**
1180 *  Reset file handle in multiple field support mode
1181 *
1182 * @param c            : the context to be modified
1183 * @param f            : the file pointer
1184 */
1185 void grib_multi_support_reset_file(grib_context* c, FILE* f);
1186 
1187 char* grib_samples_path(const grib_context *c);
1188 /*! @} */
1189 
1190 /**
1191 *  Get the API version
1192 *
1193 *  @return        API version
1194 */
1195 long grib_get_api_version(void);
1196 
1197 /**
1198 *  Get the Git version control SHA1 identifier
1199 *
1200 *  @return character string with SHA1 identifier
1201 */
1202 const char* grib_get_git_sha1(void);
1203 
1204 /**
1205 *  Get the package name
1206 *
1207 *  @return character string with package name
1208 */
1209 const char* grib_get_package_name(void);
1210 
1211 /**
1212 *  Prints the API version
1213 *
1214 *
1215 */
1216 void grib_print_api_version(FILE* out);
1217 
1218 /*! \defgroup keys_iterator Iterating on keys names
1219 The keys iterator is designed to get the key names defined in a message.
1220 Key names on which the iteration is carried out can be filtered through their
1221 attributes or by the namespace they belong to.
1222 */
1223 /*! @{ */
1224 /*! Create a new iterator from a valid and initialised handle.
1225 *  @param h             : the handle whose keys you want to iterate
1226 *  @param filter_flags  : flags to filter out some of the keys through their attributes
1227 *  @param name_space     : if not null the iteration is carried out only on
1228 *                         keys belonging to the namespace passed. (NULL for all the keys)
1229 *  @return              keys iterator ready to iterate through keys according to filter_flags
1230 *                         and namespace
1231 */
1232 grib_keys_iterator* grib_keys_iterator_new(grib_handle* h,unsigned long filter_flags, const char* name_space);
1233 
1234 /*! Step to the next iterator.
1235 *  @param kiter         : valid grib_keys_iterator
1236 *  @return              1 if next iterator exists, 0 if no more elements to iterate on
1237 */
1238 int grib_keys_iterator_next(grib_keys_iterator *kiter);
1239 
1240 
1241 /*! get the key name from the iterator
1242 *  @param kiter         : valid grib_keys_iterator
1243 *  @return              key name
1244 */
1245 const char* grib_keys_iterator_get_name(grib_keys_iterator *kiter);
1246 
1247 /*! Delete the iterator.
1248 *  @param kiter         : valid grib_keys_iterator
1249 *  @return              0 if OK, integer value on error
1250 */
1251 int grib_keys_iterator_delete( grib_keys_iterator* kiter);
1252 
1253 /*! Rewind the iterator.
1254 *  @param kiter         : valid grib_keys_iterator
1255 *  @return              0 if OK, integer value on error
1256 */
1257 int grib_keys_iterator_rewind(grib_keys_iterator* kiter);
1258 
1259 int grib_keys_iterator_set_flags(grib_keys_iterator *kiter,unsigned long flags);
1260 
1261 int grib_keys_iterator_get_long(grib_keys_iterator *kiter, long *v, size_t *len);
1262 int grib_keys_iterator_get_double(grib_keys_iterator *kiter, double *v, size_t *len);
1263 int grib_keys_iterator_get_string(grib_keys_iterator *kiter, char *v, size_t *len);
1264 int grib_keys_iterator_get_bytes(grib_keys_iterator *kiter, unsigned char *v, size_t *len);
1265 
1266 /* @} */
1267 
1268 void grib_update_sections_lengths(grib_handle* h);
1269 
1270 
1271 /**
1272 * Convert an error code into a string
1273 * @param code       : the error code
1274 * @return           the error message
1275 */
1276 const char* grib_get_error_message(int code);
1277 const char* grib_get_type_name(int type);
1278 
1279 int grib_get_native_type(grib_handle* h, const char* name,int* type);
1280 
1281 void grib_check(const char* call,const char*  file,int line,int e,const char* msg);
1282 #define GRIB_CHECK(a,msg) grib_check(#a,__FILE__,__LINE__,a,msg)
1283 #define GRIB_CHECK_NOLINE(a,msg) grib_check(#a,0,0,a,msg)
1284 
1285 
1286 int grib_set_values(grib_handle* h,grib_values*  grib_values , size_t arg_count);
1287 grib_handle* grib_handle_new_from_partial_message_copy(grib_context* c, const void* data, size_t size);
1288 grib_handle* grib_handle_new_from_partial_message(grib_context* c,void* data, size_t buflen);
1289 int grib_is_missing(grib_handle* h, const char* key, int* err);
1290 int grib_is_defined(grib_handle* h, const char* key);
1291 int grib_set_missing(grib_handle* h, const char* key);
1292 /* The truncation is the Gaussian number (or order) */
1293 int grib_get_gaussian_latitudes(long truncation,double* latitudes);
1294 
1295 int grib_julian_to_datetime(double jd, long *year, long *month, long *day, long *hour, long *minute, long *second);
1296 int grib_datetime_to_julian(long year, long month, long day, long hour, long minute, long second, double *jd);
1297 long grib_julian_to_date(long jdate);
1298 long grib_date_to_julian(long ddate);
1299 
1300 void grib_get_reduced_row(long pl,double lon_first,double lon_last,long* npoints,long* ilon_first, long* ilon_last);
1301 
1302 /* read products */
1303 int wmo_read_any_from_file(FILE *f, void *buffer, size_t *len);
1304 int wmo_read_grib_from_file(FILE* f,void* buffer,size_t* len);
1305 int wmo_read_bufr_from_file(FILE* f,void* buffer,size_t* len);
1306 int wmo_read_gts_from_file(FILE* f,void* buffer,size_t* len);
1307 int wmo_read_any_from_stream(void *stream_data, long (*stream_proc )(void *, void *buffer, long len ), void *buffer, size_t *len);
1308 void* wmo_read_any_from_stream_malloc(void* stream_data,long (*stream_proc)(void*,void* buffer,long len) ,size_t *size, int* err);
1309 void *wmo_read_any_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err);
1310 void *wmo_read_gts_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err);
1311 void *wmo_read_bufr_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err);
1312 grib_handle* eccode_gts_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error );
1313 grib_handle* eccode_bufr_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error );
1314 grib_handle* eccode_grib_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error );
1315 int grib_get_message_offset ( grib_handle* h,off_t* offset);
1316 int grib_get_message_size ( grib_handle* h,size_t* size);
1317 
1318 struct grib_points {
1319     grib_context* context;
1320     double* latitudes;
1321     double* longitudes;
1322     size_t* indexes;
1323     size_t* group_start;
1324     size_t* group_len;
1325     size_t n_groups;
1326     size_t n;
1327     size_t size;
1328 };
1329 
1330 grib_box* grib_box_new(grib_handle* h,int* error);
1331 grib_points* grib_box_get_points(grib_box *box,double north, double west,double south,double east, int *err);
1332 int grib_points_get_values(grib_handle* h, grib_points* points, double* val);
1333 
1334 
1335 /* --------------------------------------- */
1336 
1337 
1338 
1339 
1340 #define GRIB_UTIL_GRID_SPEC_REGULAR_LL 1
1341 #define GRIB_UTIL_GRID_SPEC_ROTATED_LL 2
1342 
1343 #define GRIB_UTIL_GRID_SPEC_REGULAR_GG 3
1344 #define GRIB_UTIL_GRID_SPEC_ROTATED_GG 4
1345 #define GRIB_UTIL_GRID_SPEC_REDUCED_GG 5
1346 
1347 #define GRIB_UTIL_GRID_SPEC_SH         6
1348 #define GRIB_UTIL_GRID_SPEC_REDUCED_LL 7
1349 #define GRIB_UTIL_GRID_SPEC_POLAR_STEREOGRAPHIC 8
1350 
1351 #define GRIB_UTIL_GRID_SPEC_REDUCED_ROTATED_GG 9
1352 
1353 
1354 typedef struct grib_util_grid_spec {
1355 
1356 	int grid_type;
1357 
1358 	/* Grid */
1359 	long   Ni;
1360 	long   Nj;
1361 
1362 	double iDirectionIncrementInDegrees;
1363 	double jDirectionIncrementInDegrees;
1364 
1365 	double longitudeOfFirstGridPointInDegrees;
1366 	double longitudeOfLastGridPointInDegrees;
1367 
1368 	double latitudeOfFirstGridPointInDegrees;
1369 	double latitudeOfLastGridPointInDegrees;
1370 
1371 	/* Rotation */
1372 	long uvRelativeToGrid;
1373 	double latitudeOfSouthernPoleInDegrees;
1374 	double longitudeOfSouthernPoleInDegrees;
1375 
1376 	/* Scanning mode */
1377 	long iScansNegatively;
1378 	long jScansPositively;
1379 
1380 	/* Gaussian number */
1381 	long N;
1382 
1383 	/* bitmap */
1384 	long bitmapPresent;
1385 	double missingValue;
1386 
1387 	/* pl list for reduced */
1388 	const long *pl;
1389 	long pl_size;
1390 
1391 	/* Spherical harmonics */
1392 	long truncation;
1393 
1394 	/* polar stereographic */
1395 	double orientationOfTheGridInDegrees;
1396 	long DyInMetres;
1397 	long DxInMetres;
1398 
1399 } grib_util_grid_spec;
1400 
1401 typedef struct grib_util_grid_spec2 {
1402 
1403     int grid_type;
1404     const char* grid_name; /* e.g. N320 */
1405 
1406     /* Grid */
1407     long   Ni;
1408     long   Nj;
1409 
1410     double iDirectionIncrementInDegrees;
1411     double jDirectionIncrementInDegrees;
1412 
1413     double longitudeOfFirstGridPointInDegrees;
1414     double longitudeOfLastGridPointInDegrees;
1415 
1416     double latitudeOfFirstGridPointInDegrees;
1417     double latitudeOfLastGridPointInDegrees;
1418 
1419     /* Rotation */
1420     long uvRelativeToGrid;
1421     double latitudeOfSouthernPoleInDegrees;
1422     double longitudeOfSouthernPoleInDegrees;
1423     double angleOfRotationInDegrees;
1424 
1425     /* Scanning mode */
1426     long iScansNegatively;
1427     long jScansPositively;
1428 
1429     /* Gaussian number */
1430     long N;
1431 
1432     /* bitmap */
1433     long bitmapPresent;
1434     double missingValue;
1435 
1436     /* pl list for reduced */
1437     const long *pl;
1438     long pl_size;
1439 
1440     /* Spherical harmonics */
1441     long truncation;
1442 
1443     /* polar stereographic */
1444     double orientationOfTheGridInDegrees;
1445     long DyInMetres;
1446     long DxInMetres;
1447 
1448 } grib_util_grid_spec2;
1449 
1450 #define GRIB_UTIL_PACKING_TYPE_SAME_AS_INPUT      0
1451 #define GRIB_UTIL_PACKING_TYPE_SPECTRAL_COMPLEX 1
1452 #define GRIB_UTIL_PACKING_TYPE_SPECTRAL_SIMPLE    2
1453 #define GRIB_UTIL_PACKING_TYPE_JPEG    3
1454 #define GRIB_UTIL_PACKING_TYPE_GRID_COMPLEX    4
1455 #define GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE    5
1456 #define GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE_MATRIX    6
1457 #define GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER    7
1458 
1459 #define GRIB_UTIL_PACKING_SAME_AS_INPUT    	0
1460 #define GRIB_UTIL_PACKING_USE_PROVIDED  	1
1461 
1462 #define GRIB_UTIL_ACCURACY_SAME_BITS_PER_VALUES_AS_INPUT      0
1463 #define GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES       1
1464 #define GRIB_UTIL_ACCURACY_SAME_DECIMAL_SCALE_FACTOR_AS_INPUT 2
1465 #define GRIB_UTIL_ACCURACY_USE_PROVIDED_DECIMAL_SCALE_FACTOR  3
1466 
1467 typedef struct grib_util_packing_spec {
1468 
1469 	/* Packing options */
1470 	long packing_type;
1471 	long packing;
1472 	long boustrophedonic;
1473 
1474 	long editionNumber; /* =0 for default value */
1475 
1476 	/* Accuracy */
1477 	long accuracy;
1478 	long bitsPerValue;
1479 	long decimalScaleFactor;
1480 
1481 	long computeLaplacianOperator;
1482 	int truncateLaplacian;
1483 	double laplacianOperator;
1484 
1485 	/* local definition */
1486 	long deleteLocalDefinition; /* default(=0) local definition is taken from the input field */
1487 
1488 	/* Extra values when packing */
1489 	grib_values extra_settings[80];
1490 	long        extra_settings_count;
1491 } grib_util_packing_spec;
1492 
1493 
1494 grib_handle *grib_util_set_spec(grib_handle *h,
1495 	const grib_util_grid_spec    *grid_spec,
1496 	const grib_util_packing_spec *packing_spec,  /* NULL for defaults (same as input) */
1497 	int flags,
1498 	const double *data_values,
1499 	size_t data_values_count,
1500 	int *err);
1501 
1502 grib_handle *grib_util_set_spec2(grib_handle *h,
1503     const grib_util_grid_spec2   *grid_spec,
1504     const grib_util_packing_spec *packing_spec,  /* NULL for defaults (same as input) */
1505     int flags,
1506     const double *data_values,
1507     size_t data_values_count,
1508     int *err);
1509 
1510 /* --------------------------------------- */
1511 
1512 
1513 #ifdef __cplusplus
1514 }
1515 #endif
1516 #endif
1517 /* This part is automatically generated by ./errors.pl, do not edit */
1518 #ifndef grib_errors_H
1519 #define grib_errors_H
1520 /*! \defgroup errors Error codes
1521 Error codes returned by the grib_api functions.
1522 */
1523 /*! @{*/
1524 /** No error */
1525 #define GRIB_SUCCESS		0
1526 /** End of resource reached */
1527 #define GRIB_END_OF_FILE		-1
1528 /** Internal error */
1529 #define GRIB_INTERNAL_ERROR		-2
1530 /** Passed buffer is too small */
1531 #define GRIB_BUFFER_TOO_SMALL		-3
1532 /** Function not yet implemented */
1533 #define GRIB_NOT_IMPLEMENTED		-4
1534 /** Missing 7777 at end of message */
1535 #define GRIB_7777_NOT_FOUND		-5
1536 /** Passed array is too small */
1537 #define GRIB_ARRAY_TOO_SMALL		-6
1538 /** File not found */
1539 #define GRIB_FILE_NOT_FOUND		-7
1540 /** Code not found in code table */
1541 #define GRIB_CODE_NOT_FOUND_IN_TABLE		-8
1542 /** Array size mismatch */
1543 #define GRIB_WRONG_ARRAY_SIZE		-9
1544 /** Key/value not found */
1545 #define GRIB_NOT_FOUND		-10
1546 /** Input output problem */
1547 #define GRIB_IO_PROBLEM		-11
1548 /** Message invalid */
1549 #define GRIB_INVALID_MESSAGE		-12
1550 /** Decoding invalid */
1551 #define GRIB_DECODING_ERROR		-13
1552 /** Encoding invalid */
1553 #define GRIB_ENCODING_ERROR		-14
1554 /** Code cannot unpack because of string too small */
1555 #define GRIB_NO_MORE_IN_SET		-15
1556 /** Problem with calculation of geographic attributes */
1557 #define GRIB_GEOCALCULUS_PROBLEM		-16
1558 /** Out of memory */
1559 #define GRIB_OUT_OF_MEMORY		-17
1560 /** Value is read only */
1561 #define GRIB_READ_ONLY		-18
1562 /** Invalid argument */
1563 #define GRIB_INVALID_ARGUMENT		-19
1564 /** Null handle */
1565 #define GRIB_NULL_HANDLE		-20
1566 /** Invalid section number */
1567 #define GRIB_INVALID_SECTION_NUMBER		-21
1568 /** Value cannot be missing */
1569 #define GRIB_VALUE_CANNOT_BE_MISSING		-22
1570 /** Wrong message length */
1571 #define GRIB_WRONG_LENGTH		-23
1572 /** Invalid key type */
1573 #define GRIB_INVALID_TYPE		-24
1574 /** Unable to set step */
1575 #define GRIB_WRONG_STEP		-25
1576 /** Wrong units for step (step must be integer) */
1577 #define GRIB_WRONG_STEP_UNIT		-26
1578 /** Invalid file id */
1579 #define GRIB_INVALID_FILE		-27
1580 /** Invalid grib id */
1581 #define GRIB_INVALID_GRIB		-28
1582 /** Invalid index id */
1583 #define GRIB_INVALID_INDEX		-29
1584 /** Invalid iterator id */
1585 #define GRIB_INVALID_ITERATOR		-30
1586 /** Invalid keys iterator id */
1587 #define GRIB_INVALID_KEYS_ITERATOR		-31
1588 /** Invalid nearest id */
1589 #define GRIB_INVALID_NEAREST		-32
1590 /** Invalid order by */
1591 #define GRIB_INVALID_ORDERBY		-33
1592 /** Missing a key from the fieldset */
1593 #define GRIB_MISSING_KEY		-34
1594 /** The point is out of the grid area */
1595 #define GRIB_OUT_OF_AREA		-35
1596 /** Concept no match */
1597 #define GRIB_CONCEPT_NO_MATCH		-36
1598 /** Definitions files not found */
1599 #define GRIB_NO_DEFINITIONS		-37
1600 /** Wrong type while packing */
1601 #define GRIB_WRONG_TYPE		-38
1602 /** End of resource */
1603 #define GRIB_END		-39
1604 /** Unable to code a field without values */
1605 #define GRIB_NO_VALUES		-40
1606 /** Grid description is wrong or inconsistent */
1607 #define GRIB_WRONG_GRID		-41
1608 /** End of index reached */
1609 #define GRIB_END_OF_INDEX		-42
1610 /** Null index */
1611 #define GRIB_NULL_INDEX		-43
1612 /** End of resource reached when reading message */
1613 #define GRIB_PREMATURE_END_OF_FILE		-44
1614 /** An internal array is too small */
1615 #define GRIB_INTERNAL_ARRAY_TOO_SMALL		-45
1616 /** Message is too large for the current architecture */
1617 #define GRIB_MESSAGE_TOO_LARGE		-46
1618 /** Constant field */
1619 #define GRIB_CONSTANT_FIELD		-47
1620 /** Switch unable to find a matching case */
1621 #define GRIB_SWITCH_NO_MATCH		-48
1622 /** Underflow */
1623 #define GRIB_UNDERFLOW		-49
1624 /** Message malformed */
1625 #define GRIB_MESSAGE_MALFORMED		-50
1626 /** Index is corrupted */
1627 #define GRIB_CORRUPTED_INDEX		-51
1628 /** Invalid number of bits per value */
1629 #define GRIB_INVALID_BPV		-52
1630 /** Edition of two messages is different */
1631 #define GRIB_DIFFERENT_EDITION		-53
1632 /** Value is different */
1633 #define GRIB_VALUE_DIFFERENT		-54
1634 /** Invalid key value */
1635 #define GRIB_INVALID_KEY_VALUE		-55
1636 /*! @}*/
1637 #endif
1638