eccodes/python/grib_interface.c

2415 lines
57 KiB
C
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 12:04:10 +00:00
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include "grib_api_internal.h"
#include "assert.h"
#if HAVE_SYS_TYPES_H
2020-03-18 15:27:53 +00:00
#include <sys/types.h>
2013-03-25 12:04:10 +00:00
#endif
#if HAVE_SYS_STAT_H
2020-03-18 15:27:53 +00:00
#include <sys/stat.h>
2013-03-25 12:04:10 +00:00
#endif
#ifdef HAVE_FCNTL_H
2020-03-18 15:27:53 +00:00
#include <fcntl.h>
2013-03-25 12:04:10 +00:00
#endif
#include <ctype.h>
2016-08-04 15:43:24 +00:00
#include <errno.h>
2013-03-25 12:04:10 +00:00
#if GRIB_PTHREADS
2020-03-18 15:27:53 +00:00
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t index_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t multi_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t keys_iterator_mutex = PTHREAD_MUTEX_INITIALIZER;
static void init()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&handle_mutex, &attr);
pthread_mutex_init(&index_mutex, &attr);
pthread_mutex_init(&multi_handle_mutex, &attr);
pthread_mutex_init(&iterator_mutex, &attr);
pthread_mutex_init(&keys_iterator_mutex, &attr);
pthread_mutexattr_destroy(&attr);
}
2015-12-30 14:39:02 +00:00
#elif GRIB_OMP_THREADS
2020-03-18 15:27:53 +00:00
static int once = 0;
static omp_nest_lock_t handle_mutex;
static omp_nest_lock_t index_mutex;
static omp_nest_lock_t multi_handle_mutex;
static omp_nest_lock_t iterator_mutex;
static omp_nest_lock_t keys_iterator_mutex;
static void init()
{
GRIB_OMP_CRITICAL(lock_python)
2015-12-30 14:39:02 +00:00
{
2020-03-18 15:27:53 +00:00
if (once == 0) {
2015-12-30 14:39:02 +00:00
omp_init_nest_lock(&handle_mutex);
omp_init_nest_lock(&index_mutex);
omp_init_nest_lock(&multi_handle_mutex);
omp_init_nest_lock(&iterator_mutex);
omp_init_nest_lock(&keys_iterator_mutex);
once = 1;
}
}
2020-03-18 15:27:53 +00:00
}
2013-03-25 12:04:10 +00:00
#endif
2020-03-18 15:27:53 +00:00
int GRIB_NULL = -1;
int GRIB_NULL_NEAREST = -1;
2013-03-25 12:04:10 +00:00
typedef struct l_grib_file l_grib_file;
2020-03-18 15:27:53 +00:00
struct l_grib_file
{
2015-02-12 18:32:04 +00:00
int id;
FILE* f;
l_grib_file* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_grib_handle l_grib_handle;
2020-03-18 15:27:53 +00:00
struct l_grib_handle
{
2015-02-12 18:32:04 +00:00
int id;
grib_handle* h;
l_grib_handle* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_grib_index l_grib_index;
2020-03-18 15:27:53 +00:00
struct l_grib_index
{
2015-02-12 18:32:04 +00:00
int id;
grib_index* h;
l_grib_index* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_grib_multi_handle l_grib_multi_handle;
2020-03-18 15:27:53 +00:00
struct l_grib_multi_handle
{
2015-02-12 18:32:04 +00:00
int id;
grib_multi_handle* h;
l_grib_multi_handle* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_grib_iterator l_grib_iterator;
2020-03-18 15:27:53 +00:00
struct l_grib_iterator
{
2015-02-12 18:32:04 +00:00
int id;
grib_iterator* i;
l_grib_iterator* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_grib_keys_iterator l_grib_keys_iterator;
2020-03-18 15:27:53 +00:00
struct l_grib_keys_iterator
{
2015-02-12 18:32:04 +00:00
int id;
grib_keys_iterator* i;
l_grib_keys_iterator* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_bufr_keys_iterator l_bufr_keys_iterator;
2020-03-18 15:27:53 +00:00
struct l_bufr_keys_iterator
{
int id;
bufr_keys_iterator* i;
l_bufr_keys_iterator* next;
};
2020-03-18 15:27:53 +00:00
static l_grib_handle* handle_set = NULL;
static l_grib_index* index_set = NULL;
static l_grib_multi_handle* multi_handle_set = NULL;
static l_grib_file* file_set = NULL;
static l_grib_iterator* iterator_set = NULL;
static l_grib_keys_iterator* keys_iterator_set = NULL;
static l_bufr_keys_iterator* bufr_keys_iterator_set = NULL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
static int push_file(FILE* f)
{
2015-02-12 18:32:04 +00:00
l_grib_file* current = file_set;
l_grib_file* previous = file_set;
l_grib_file* new = NULL;
2020-03-18 15:27:53 +00:00
int myindex = 1;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!file_set) {
file_set = malloc(sizeof(l_grib_file));
2015-02-12 18:32:04 +00:00
file_set->id = myindex;
file_set->f = f;
file_set->next = NULL;
return myindex;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
2020-03-18 15:27:53 +00:00
current->f = f;
return current->id;
}
else {
2015-02-12 18:32:04 +00:00
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
2015-02-12 18:32:04 +00:00
}
}
2020-03-18 15:27:53 +00:00
new = malloc(sizeof(l_grib_file));
new->id = myindex;
new->f = f;
new->next = current;
2015-02-12 18:32:04 +00:00
previous->next = new;
return myindex;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static void _push_handle(grib_handle* h, int* gid)
{
2020-03-18 15:27:53 +00:00
l_grib_handle* current = handle_set;
l_grib_handle* previous = handle_set;
l_grib_handle* new = NULL;
int myindex = 1;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (*gid > 0) {
while (current) {
if (current->id == *gid) break;
2015-02-12 18:32:04 +00:00
current = current->next;
}
if (current) {
grib_handle_delete(current->h);
2020-03-18 15:27:53 +00:00
current->h = h;
2015-02-12 18:32:04 +00:00
return;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
current = handle_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!handle_set) {
handle_set = malloc(sizeof(l_grib_handle));
2015-02-12 18:32:04 +00:00
handle_set->id = myindex;
handle_set->h = h;
handle_set->next = NULL;
2020-03-18 15:27:53 +00:00
*gid = myindex;
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
current->h = h;
2020-03-18 15:27:53 +00:00
*gid = current->id;
2015-02-12 18:32:04 +00:00
return;
}
2020-03-18 15:27:53 +00:00
else {
2015-02-12 18:32:04 +00:00
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
new = malloc(sizeof(l_grib_handle));
new->id = myindex;
new->h = h;
new->next = current;
2015-02-12 18:32:04 +00:00
previous->next = new;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
*gid = myindex;
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static void _push_index(grib_index* h, int* gid)
{
2020-03-18 15:27:53 +00:00
l_grib_index* current = index_set;
l_grib_index* previous = index_set;
l_grib_index* new = NULL;
int myindex = 1;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (*gid > 0) {
while (current) {
if (current->id == *gid) break;
2015-02-12 18:32:04 +00:00
current = current->next;
}
if (current) {
grib_index_delete(current->h);
2020-03-18 15:27:53 +00:00
current->h = h;
2015-02-12 18:32:04 +00:00
return;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
current = index_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!index_set) {
index_set = malloc(sizeof(l_grib_index));
2015-02-12 18:32:04 +00:00
index_set->id = myindex;
index_set->h = h;
index_set->next = NULL;
2020-03-18 15:27:53 +00:00
*gid = myindex;
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
current->h = h;
2020-03-18 15:27:53 +00:00
*gid = current->id;
2015-02-12 18:32:04 +00:00
return;
}
2020-03-18 15:27:53 +00:00
else {
2015-02-12 18:32:04 +00:00
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
new = malloc(sizeof(l_grib_index));
new->id = myindex;
new->h = h;
new->next = current;
2015-02-12 18:32:04 +00:00
previous->next = new;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
*gid = myindex;
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static void _push_multi_handle(grib_multi_handle* h, int* gid)
{
2020-03-18 15:27:53 +00:00
l_grib_multi_handle* current = multi_handle_set;
l_grib_multi_handle* previous = multi_handle_set;
l_grib_multi_handle* new = NULL;
int myindex = 1;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (*gid > 0) {
while (current) {
if (current->id == *gid) break;
2015-02-12 18:32:04 +00:00
current = current->next;
}
if (current) {
grib_multi_handle_delete(current->h);
2020-03-18 15:27:53 +00:00
current->h = h;
2015-02-12 18:32:04 +00:00
return;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
current = multi_handle_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!multi_handle_set) {
multi_handle_set = malloc(sizeof(l_grib_multi_handle));
2015-02-12 18:32:04 +00:00
multi_handle_set->id = myindex;
multi_handle_set->h = h;
multi_handle_set->next = NULL;
2020-03-18 15:27:53 +00:00
*gid = myindex;
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
current->h = h;
2020-03-18 15:27:53 +00:00
*gid = current->id;
2015-02-12 18:32:04 +00:00
return;
}
2020-03-18 15:27:53 +00:00
else {
2015-02-12 18:32:04 +00:00
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
new = malloc(sizeof(l_grib_multi_handle));
new->id = myindex;
new->h = h;
new->next = current;
2015-02-12 18:32:04 +00:00
previous->next = new;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
*gid = myindex;
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static void push_handle(grib_handle* h, int* gid)
{
2020-03-18 15:27:53 +00:00
GRIB_MUTEX_INIT_ONCE(&once, &init)
GRIB_MUTEX_LOCK(&handle_mutex)
2020-03-18 15:27:53 +00:00
_push_handle(h, gid);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&handle_mutex)
return;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static void push_index(grib_index* h, int* gid)
{
2020-03-18 15:27:53 +00:00
GRIB_MUTEX_INIT_ONCE(&once, &init)
GRIB_MUTEX_LOCK(&index_mutex)
2020-03-18 15:27:53 +00:00
_push_index(h, gid);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&index_mutex)
return;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static void push_multi_handle(grib_multi_handle* h, int* gid)
{
2020-03-18 15:27:53 +00:00
GRIB_MUTEX_INIT_ONCE(&once, &init)
GRIB_MUTEX_LOCK(&multi_handle_mutex)
2020-03-18 15:27:53 +00:00
_push_multi_handle(h, gid);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&multi_handle_mutex)
return;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static int _push_iterator(grib_iterator* i)
{
2015-02-12 18:32:04 +00:00
l_grib_iterator* current = iterator_set;
l_grib_iterator* previous = iterator_set;
l_grib_iterator* new = NULL;
2020-03-18 15:27:53 +00:00
int myindex = 1;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!iterator_set) {
iterator_set = malloc(sizeof(l_grib_iterator));
2015-02-12 18:32:04 +00:00
iterator_set->id = myindex;
iterator_set->i = i;
iterator_set->next = NULL;
return myindex;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
current->i = i;
return current->id;
}
2020-03-18 15:27:53 +00:00
else {
2015-02-12 18:32:04 +00:00
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
new = malloc(sizeof(l_grib_iterator));
new->id = myindex;
new->i = i;
new->next = current;
2015-02-12 18:32:04 +00:00
previous->next = new;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return myindex;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static int push_iterator(grib_iterator* i)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _push_iterator(i);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static int _push_keys_iterator(grib_keys_iterator* i)
{
2015-02-12 18:32:04 +00:00
l_grib_keys_iterator* current = keys_iterator_set;
l_grib_keys_iterator* previous = keys_iterator_set;
l_grib_keys_iterator* new = NULL;
2020-03-18 15:27:53 +00:00
int myindex = 1;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!keys_iterator_set) {
keys_iterator_set = malloc(sizeof(l_grib_keys_iterator));
2015-02-12 18:32:04 +00:00
keys_iterator_set->id = myindex;
keys_iterator_set->i = i;
keys_iterator_set->next = NULL;
return myindex;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
current->i = i;
return current->id;
}
2020-03-18 15:27:53 +00:00
else {
2015-02-12 18:32:04 +00:00
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
if (!previous) return -1;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
new = malloc(sizeof(l_grib_keys_iterator));
new->id = myindex;
new->i = i;
new->next = current;
2015-02-12 18:32:04 +00:00
previous->next = new;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return myindex;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static int push_keys_iterator(grib_keys_iterator* i)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _push_keys_iterator(i);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
//BUFR keys iterator
2020-03-18 15:27:53 +00:00
static int _push_bufr_keys_iterator(bufr_keys_iterator* i)
{
l_bufr_keys_iterator* current = bufr_keys_iterator_set;
l_bufr_keys_iterator* previous = bufr_keys_iterator_set;
2020-03-18 15:27:53 +00:00
l_bufr_keys_iterator* the_new = NULL;
int myindex = 1;
2020-03-18 15:27:53 +00:00
if (!bufr_keys_iterator_set) {
bufr_keys_iterator_set = (l_bufr_keys_iterator*)malloc(sizeof(l_bufr_keys_iterator));
Assert(bufr_keys_iterator_set);
bufr_keys_iterator_set->id = myindex;
bufr_keys_iterator_set->i = i;
bufr_keys_iterator_set->next = NULL;
return myindex;
}
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id < 0) {
current->id = -(current->id);
current->i = i;
return current->id;
}
2020-03-18 15:27:53 +00:00
else {
myindex++;
previous = current;
2020-03-18 15:27:53 +00:00
current = current->next;
}
}
2020-03-18 15:27:53 +00:00
if (!previous) return -1;
the_new = (l_bufr_keys_iterator*)malloc(sizeof(l_bufr_keys_iterator));
Assert(the_new);
the_new->id = myindex;
the_new->i = i;
the_new->next = current;
previous->next = the_new;
return myindex;
}
2020-03-18 15:27:53 +00:00
static int push_bufr_keys_iterator(bufr_keys_iterator* i)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
2020-03-18 15:27:53 +00:00
ret = _push_bufr_keys_iterator(i);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
return ret;
}
static grib_handle* _get_handle(int handle_id)
{
2020-03-18 15:27:53 +00:00
l_grib_handle* current = handle_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == handle_id) return current->h;
2015-02-12 18:32:04 +00:00
current = current->next;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_index* _get_index(int index_id)
{
2020-03-18 15:27:53 +00:00
l_grib_index* current = index_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == index_id) return current->h;
2015-02-12 18:32:04 +00:00
current = current->next;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_multi_handle* _get_multi_handle(int multi_handle_id)
{
2020-03-18 15:27:53 +00:00
l_grib_multi_handle* current = multi_handle_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == multi_handle_id) return current->h;
2015-02-12 18:32:04 +00:00
current = current->next;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_handle* get_handle(int handle_id)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&handle_mutex)
2020-03-18 15:27:53 +00:00
h = _get_handle(handle_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&handle_mutex)
return h;
2013-03-25 12:04:10 +00:00
}
static grib_index* get_index(int index_id)
{
2020-03-18 15:27:53 +00:00
grib_index* h = NULL;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&index_mutex)
2020-03-18 15:27:53 +00:00
h = _get_index(index_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&index_mutex)
return h;
2013-03-25 12:04:10 +00:00
}
static grib_multi_handle* get_multi_handle(int multi_handle_id)
{
2020-03-18 15:27:53 +00:00
grib_multi_handle* h = NULL;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&multi_handle_mutex)
2020-03-18 15:27:53 +00:00
h = _get_multi_handle(multi_handle_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&multi_handle_mutex)
return h;
2013-03-25 12:04:10 +00:00
}
static FILE* get_file(int file_id)
{
2020-03-18 15:27:53 +00:00
l_grib_file* current = file_set;
while (current) {
if (current->id == file_id) return current->f;
2015-02-12 18:32:04 +00:00
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_iterator* _get_iterator(int iterator_id)
{
2020-03-18 15:27:53 +00:00
l_grib_iterator* current = iterator_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == iterator_id) return current->i;
2015-02-12 18:32:04 +00:00
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_iterator* get_iterator(int iterator_id)
{
2020-03-18 15:27:53 +00:00
grib_iterator* i = NULL;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&iterator_mutex)
2020-03-18 15:27:53 +00:00
i = _get_iterator(iterator_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&iterator_mutex)
return i;
2013-03-25 12:04:10 +00:00
}
static grib_keys_iterator* _get_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
l_grib_keys_iterator* current = keys_iterator_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == keys_iterator_id) return current->i;
2015-02-12 18:32:04 +00:00
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_keys_iterator* get_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* i = NULL;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
2020-03-18 15:27:53 +00:00
i = _get_keys_iterator(keys_iterator_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return i;
2013-03-25 12:04:10 +00:00
}
//BUFR keys iterator
static bufr_keys_iterator* _get_bufr_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
l_bufr_keys_iterator* current = bufr_keys_iterator_set;
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == keys_iterator_id) return current->i;
current = current->next;
}
return NULL;
}
static bufr_keys_iterator* get_bufr_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
bufr_keys_iterator* i = NULL;
GRIB_MUTEX_INIT_ONCE(&once, &init)
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
2020-03-18 15:27:53 +00:00
i = _get_bufr_keys_iterator(keys_iterator_id);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return i;
}
static int clear_file(int file_id)
{
2015-02-12 18:32:04 +00:00
l_grib_file* current = file_set;
2020-03-18 15:27:53 +00:00
int ret = 0;
while (current) {
if (current->id == file_id) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
if (current->f) {
ret = fclose(current->f);
if (0 == ret) {
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
2015-02-12 18:32:04 +00:00
return GRIB_IO_PROBLEM;
}
}
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
current = current->next;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
static int _clear_handle(int handle_id)
{
2020-03-18 15:27:53 +00:00
l_grib_handle* current = handle_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
/* look for the input grib id to release it */
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == handle_id) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
2020-03-18 15:27:53 +00:00
if (current->h) return grib_handle_delete(current->h);
2015-02-12 18:32:04 +00:00
}
current = current->next;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
/* fail with invalid grib id if not found */
return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
}
static int _clear_index(int index_id)
{
2020-03-18 15:27:53 +00:00
l_grib_index* current = index_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == index_id) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
if (current->h) {
grib_index_delete(current->h);
return GRIB_SUCCESS;
}
}
current = current->next;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
static int _clear_multi_handle(int multi_handle_id)
{
2020-03-18 15:27:53 +00:00
l_grib_multi_handle* current = multi_handle_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == multi_handle_id) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
2020-03-18 15:27:53 +00:00
if (current->h) return grib_multi_handle_delete(current->h);
2015-02-12 18:32:04 +00:00
}
current = current->next;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
static int clear_handle(int handle_id)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&handle_mutex)
2020-03-18 15:27:53 +00:00
ret = _clear_handle(handle_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&handle_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
static int clear_index(int index_id)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&index_mutex)
2020-03-18 15:27:53 +00:00
ret = _clear_index(index_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&index_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
static int clear_multi_handle(int multi_handle_id)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&multi_handle_mutex)
2020-03-18 15:27:53 +00:00
ret = _clear_multi_handle(multi_handle_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&multi_handle_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
static int _clear_iterator(int iterator_id)
{
2020-03-18 15:27:53 +00:00
l_grib_iterator* current = iterator_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == iterator_id) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
return grib_iterator_delete(current->i);
}
current = current->next;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_ITERATOR;
2013-03-25 12:04:10 +00:00
}
static int clear_iterator(int iterator_id)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _clear_iterator(iterator_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
static int _clear_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
l_grib_keys_iterator* current = keys_iterator_set;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == keys_iterator_id) {
2015-02-12 18:32:04 +00:00
current->id = -(current->id);
return grib_keys_iterator_delete(current->i);
}
current = current->next;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_KEYS_ITERATOR;
2013-03-25 12:04:10 +00:00
}
static int clear_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _clear_keys_iterator(keys_iterator_id);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
//BUFR keys iterator
static int _clear_bufr_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
l_bufr_keys_iterator* current = bufr_keys_iterator_set;
2020-03-18 15:27:53 +00:00
while (current) {
if (current->id == keys_iterator_id) {
current->id = -(current->id);
return codes_bufr_keys_iterator_delete(current->i);
}
current = current->next;
}
return GRIB_INVALID_KEYS_ITERATOR;
}
static int clear_bufr_keys_iterator(int keys_iterator_id)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
2020-03-18 15:27:53 +00:00
ret = _clear_bufr_keys_iterator(keys_iterator_id);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
return ret;
}
int grib_c_read_any_from_file(int* fid, char* buffer, int* nbytes)
{
2015-02-12 18:32:04 +00:00
grib_context* c;
2020-03-18 15:27:53 +00:00
int err = 0;
size_t size = (size_t)nbytes;
FILE* f = get_file(*fid);
2015-02-12 18:32:04 +00:00
if (f) {
2020-03-18 15:27:53 +00:00
c = grib_context_get_default();
err = grib_read_any_from_file(c, f, buffer, &size);
*nbytes = size;
2015-02-12 18:32:04 +00:00
return err;
2020-03-18 15:27:53 +00:00
}
else {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
}
2013-03-25 12:04:10 +00:00
}
int grib_c_write_file(int* fid, char* buffer, int* nbytes)
{
2015-02-12 18:32:04 +00:00
grib_context* c;
2020-03-18 15:27:53 +00:00
FILE* f = get_file(*fid);
2015-02-12 18:32:04 +00:00
if (f) {
int ioerr;
2020-03-18 15:27:53 +00:00
c = grib_context_get_default();
if (fwrite(buffer, 1, *nbytes, f) != *nbytes) {
ioerr = errno;
grib_context_log(c, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "IO ERROR: %s", strerror(ioerr));
2015-02-12 18:32:04 +00:00
return GRIB_IO_PROBLEM;
}
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
}
int grib_c_read_file(int* fid, char* buffer, int* nbytes)
{
2015-02-12 18:32:04 +00:00
grib_context* c;
2020-03-18 15:27:53 +00:00
FILE* f = get_file(*fid);
2015-02-12 18:32:04 +00:00
if (f) {
int ioerr;
2020-03-18 15:27:53 +00:00
c = grib_context_get_default();
if (fread(buffer, 1, *nbytes, f) != *nbytes) {
ioerr = errno;
grib_context_log(c, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "IO ERROR: %s", strerror(ioerr));
2015-02-12 18:32:04 +00:00
return GRIB_IO_PROBLEM;
}
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
}
2020-03-18 15:27:53 +00:00
int grib_c_open_file(int* fid, char* name, char* op)
{
2020-03-18 15:27:53 +00:00
FILE* f = NULL;
int ioerr = 0;
int ret = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
/*TODO Proper context passed as external parameter */
2020-03-18 15:27:53 +00:00
grib_context* context = grib_context_get_default();
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
f = fopen(name, op);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!f) {
ioerr = errno;
grib_context_log(context, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "IO ERROR: %s: %s", strerror(ioerr), name);
2015-02-12 18:32:04 +00:00
*fid = -1;
2020-03-18 15:27:53 +00:00
ret = GRIB_IO_PROBLEM;
}
else {
2015-02-12 18:32:04 +00:00
*fid = push_file(f);
2020-03-18 15:27:53 +00:00
ret = GRIB_SUCCESS;
2015-02-12 18:32:04 +00:00
}
return ret;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_close_file(int* fid)
{
2015-02-12 18:32:04 +00:00
return clear_file(*fid);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_multi_support_on(void)
{
2015-02-12 18:32:04 +00:00
grib_multi_support_on(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_multi_support_off(void)
{
2015-02-12 18:32:04 +00:00
grib_multi_support_off(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static int _grib_c_iterator_new_(int* gid, int* iterid, int* mode)
{
2020-03-18 15:27:53 +00:00
int err = 0;
2015-02-12 18:32:04 +00:00
grib_handle* h;
grib_iterator* iter;
2020-03-18 15:27:53 +00:00
h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
if (!h) {
2020-03-18 15:27:53 +00:00
*iterid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_NULL_HANDLE;
}
2020-03-18 15:27:53 +00:00
iter = grib_iterator_new(h, *mode, &err);
2015-02-12 18:32:04 +00:00
if (iter)
2020-03-18 15:27:53 +00:00
*iterid = push_iterator(iter);
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
*iterid = -1;
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_iterator_new(int* gid, int* iterid, int* mode)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _grib_c_iterator_new_(gid, iterid, mode);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_iterator_next(int* iterid, double* lat, double* lon, double* value)
{
2020-03-18 15:27:53 +00:00
grib_iterator* iter = get_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_iterator_next(iter, lat, lon, value);
2013-03-25 12:04:10 +00:00
}
int grib_c_iterator_delete(int* iterid)
{
2015-02-12 18:32:04 +00:00
return clear_iterator(*iterid);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
static int _grib_c_keys_iterator_new_(int* gid, int* iterid, char* name_space)
{
2020-03-18 15:27:53 +00:00
int err = 0;
2015-02-12 18:32:04 +00:00
grib_handle* h;
grib_keys_iterator* iter;
2020-03-18 15:27:53 +00:00
h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
if (!h) {
2020-03-18 15:27:53 +00:00
*iterid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_NULL_HANDLE;
}
2020-03-18 15:27:53 +00:00
iter = grib_keys_iterator_new(h, 0, name_space);
2015-02-12 18:32:04 +00:00
if (iter)
2020-03-18 15:27:53 +00:00
*iterid = push_keys_iterator(iter);
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
*iterid = -1;
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_keys_iterator_new(int* gid, int* iterid, char* name_space)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _grib_c_keys_iterator_new_(gid, iterid, name_space);
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
int grib_c_keys_iterator_next(int* iterid)
{
2015-02-12 18:32:04 +00:00
int ret = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
ret = grib_keys_iterator_next(iter);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return ret;
2013-03-25 12:04:10 +00:00
}
int grib_c_keys_iterator_delete(int* iterid)
{
2015-02-12 18:32:04 +00:00
return clear_keys_iterator(*iterid);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_keys_iterator_get_name(int* iterid, char* name, int len)
{
2020-03-18 15:27:53 +00:00
size_t lsize = len;
char buf[1024] = {0,};
2020-03-18 15:27:53 +00:00
grib_keys_iterator* kiter = get_keys_iterator(*iterid);
if (!kiter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
if (grib_keys_iterator_get_accessor(kiter) == NULL)
return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
sprintf(buf, "%s", grib_keys_iterator_get_name(kiter));
lsize = strlen(buf);
if (len < lsize) return GRIB_ARRAY_TOO_SMALL;
2020-03-18 15:27:53 +00:00
memcpy(name, buf, lsize);
name[lsize] = '\0';
return 0;
}
int grib_c_keys_iterator_rewind(int* kiter)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* i = get_keys_iterator(*kiter);
if (!i) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_rewind(i);
}
/*BUFR keys iterator*/
2020-03-18 15:27:53 +00:00
static int _codes_c_bufr_keys_iterator_new_(int* gid, int* iterid)
{
2020-03-18 15:27:53 +00:00
int err = 0;
grib_handle* h;
bufr_keys_iterator* iter;
2020-03-18 15:27:53 +00:00
h = get_handle(*gid);
if (!h) {
2020-03-18 15:27:53 +00:00
*iterid = -1;
return GRIB_NULL_HANDLE;
}
2020-03-18 15:27:53 +00:00
iter = codes_bufr_keys_iterator_new(h, 0);
if (iter)
2020-03-18 15:27:53 +00:00
*iterid = push_bufr_keys_iterator(iter);
else
2020-03-18 15:27:53 +00:00
*iterid = -1;
return err;
}
2020-03-18 15:27:53 +00:00
int codes_c_bufr_keys_iterator_new(int* gid, int* iterid)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
GRIB_MUTEX_INIT_ONCE(&once, &init)
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
2020-03-18 15:27:53 +00:00
ret = _codes_c_bufr_keys_iterator_new_(gid, iterid);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
}
int codes_c_bufr_keys_iterator_next(int* iterid)
{
2020-03-18 15:27:53 +00:00
int ret = 0;
bufr_keys_iterator* iter = get_bufr_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
ret = codes_bufr_keys_iterator_next(iter);
return ret;
}
2020-03-18 15:27:53 +00:00
int codes_c_bufr_keys_iterator_get_name(int* iterid, char* name, int len)
{
2020-03-18 15:27:53 +00:00
size_t lsize = len;
char buf[1024] = {0,};
2020-03-18 15:27:53 +00:00
bufr_keys_iterator* kiter = get_bufr_keys_iterator(*iterid);
if (!kiter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
if (codes_bufr_keys_iterator_get_accessor(kiter) == NULL)
return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
sprintf(buf, "%s", codes_bufr_keys_iterator_get_name(kiter));
lsize = strlen(buf);
if (len < lsize) return GRIB_ARRAY_TOO_SMALL;
2020-03-18 15:27:53 +00:00
memcpy(name, buf, lsize);
name[lsize] = '\0';
return 0;
}
int codes_c_bufr_keys_iterator_rewind(int* kiter)
{
2020-03-18 15:27:53 +00:00
bufr_keys_iterator* i = get_bufr_keys_iterator(*kiter);
if (!i) return GRIB_INVALID_KEYS_ITERATOR;
return codes_bufr_keys_iterator_rewind(i);
}
int codes_c_bufr_keys_iterator_delete(int* iterid)
{
return clear_bufr_keys_iterator(*iterid);
}
2013-03-25 12:04:10 +00:00
int grib_c_gribex_mode_on(void)
{
2015-02-12 18:32:04 +00:00
grib_gribex_mode_on(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
int grib_c_gribex_mode_off(void)
{
2015-02-12 18:32:04 +00:00
grib_gribex_mode_off(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
int grib_c_skip_computed(int* iterid)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_keys_iterator_set_flags(iter, GRIB_KEYS_ITERATOR_SKIP_COMPUTED);
2013-03-25 12:04:10 +00:00
}
int grib_c_skip_coded(int* iterid)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_keys_iterator_set_flags(iter, GRIB_KEYS_ITERATOR_SKIP_CODED);
2013-03-25 12:04:10 +00:00
}
int grib_c_skip_edition_specific(int* iterid)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_keys_iterator_set_flags(iter, GRIB_KEYS_ITERATOR_SKIP_EDITION_SPECIFIC);
2013-03-25 12:04:10 +00:00
}
int grib_c_skip_duplicates(int* iterid)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_keys_iterator_set_flags(iter, GRIB_KEYS_ITERATOR_SKIP_DUPLICATES);
2013-03-25 12:04:10 +00:00
}
int grib_c_skip_read_only(int* iterid)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_keys_iterator_set_flags(iter, GRIB_KEYS_ITERATOR_SKIP_READ_ONLY);
2013-03-25 12:04:10 +00:00
}
int grib_c_skip_function(int* iterid)
{
2020-03-18 15:27:53 +00:00
grib_keys_iterator* iter = get_keys_iterator(*iterid);
2015-02-12 18:32:04 +00:00
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2020-03-18 15:27:53 +00:00
return grib_keys_iterator_set_flags(iter, GRIB_KEYS_ITERATOR_SKIP_FUNCTION);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_new_from_message(int* gid, void* buffer, size_t* bufsize)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
h = grib_handle_new_from_message_copy(0, buffer, *bufsize);
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*gid = -1;
2020-03-18 15:27:53 +00:00
return GRIB_INTERNAL_ERROR;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_new_from_message_copy(int* gid, void* buffer, size_t* bufsize)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
h = grib_handle_new_from_message_copy(0, buffer, *bufsize);
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*gid = -1;
2020-03-18 15:27:53 +00:00
return GRIB_INTERNAL_ERROR;
2013-03-25 12:04:10 +00:00
}
int grib_c_grib_new_from_samples(int* gid, char* name)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
h = grib_handle_new_from_samples(NULL, name);
2015-02-12 18:32:04 +00:00
/* grib_context_set_debug(h->context,1);*/
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*gid = -1;
2020-03-18 15:27:53 +00:00
return GRIB_FILE_NOT_FOUND;
2013-03-25 12:04:10 +00:00
}
int grib_c_bufr_new_from_samples(int* gid, char* name)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
2020-03-18 15:27:53 +00:00
h = codes_bufr_handle_new_from_samples(NULL, name);
/* grib_context_set_debug(h->context,1);*/
2020-03-18 15:27:53 +00:00
if (h) {
push_handle(h, gid);
return GRIB_SUCCESS;
}
*gid = -1;
2020-03-18 15:27:53 +00:00
return GRIB_FILE_NOT_FOUND;
}
2020-03-18 15:27:53 +00:00
int grib_c_clone(int* gidsrc, int* giddest)
{
2020-03-18 15:27:53 +00:00
grib_handle* src = get_handle(*gidsrc);
grib_handle* dest = NULL;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (src) {
2015-02-12 18:32:04 +00:00
dest = grib_handle_clone(src);
2020-03-18 15:27:53 +00:00
if (dest) {
push_handle(dest, giddest);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
*giddest = -1;
return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_copy_namespace(int* gidsrc, char* name, int* giddest)
{
2020-03-18 15:27:53 +00:00
grib_handle* src = get_handle(*gidsrc);
grib_handle* dest = get_handle(*giddest);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (src && dest)
return grib_copy_namespace(dest, name, src);
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_count_in_file(FILE* f, int* n)
{
2015-02-12 18:32:04 +00:00
int err = 0;
2020-03-18 15:27:53 +00:00
if (f) err = grib_count_in_file(0, f, n);
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_new_gts_from_file(FILE* f, int headers_only, int* gid)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
int err = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (f) {
h = gts_new_from_file(0, f, &err);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_new_metar_from_file(FILE* f, int headers_only, int* gid)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
int err = 0;
2020-03-18 15:27:53 +00:00
if (f) {
h = metar_new_from_file(0, f, &err);
2020-03-18 15:27:53 +00:00
if (h) {
push_handle(h, gid);
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
return GRIB_END_OF_FILE;
}
}
2020-03-18 15:27:53 +00:00
*gid = -1;
return GRIB_INVALID_FILE;
}
2020-03-18 15:27:53 +00:00
int grib_c_new_any_from_file(FILE* f, int headers_only, int* gid)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
int err = 0;
if (f) {
h = codes_handle_new_from_file(0, f, PRODUCT_ANY, &err);
if (h) {
push_handle(h, gid);
2015-03-12 18:04:42 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
2015-03-12 18:04:42 +00:00
return GRIB_END_OF_FILE;
}
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-03-12 18:04:42 +00:00
return GRIB_INVALID_FILE;
}
2020-03-18 15:27:53 +00:00
int grib_c_new_bufr_from_file(FILE* f, int headers_only, int* gid)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
int err = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (f) {
2015-02-12 18:32:04 +00:00
/* h = bufr_new_from_file(0,f,headers_only,&err); */
2020-03-18 15:27:53 +00:00
h = codes_handle_new_from_file(0, f, PRODUCT_BUFR, &err);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
int grib_c_new_from_file(FILE* f, int* gid, int headers_only)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
int err = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (f) {
h = grib_new_from_file(0, f, headers_only, &err);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
2015-02-12 18:32:04 +00:00
if (err == GRIB_SUCCESS) {
return GRIB_END_OF_FILE;
2020-03-18 15:27:53 +00:00
}
else {
2015-02-12 18:32:04 +00:00
/* A real error occurred */
return err;
}
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_new_from_index(int* iid, int* gid)
{
int err = 0;
2015-02-12 18:32:04 +00:00
grib_index* i = get_index(*iid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_handle* h = NULL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (i) {
h = grib_handle_new_from_index(i, &err);
if (h) {
push_handle(h, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_END_OF_INDEX;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_INDEX;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_new_from_file(char* file, char* keys, int* gid)
{
int err = 0;
grib_index* i = NULL;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (*file) {
i = grib_index_new_from_file(0, file, keys, &err);
if (i) {
push_index(i, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_add_file(int* iid, char* file)
{
grib_index* i = get_index(*iid);
int err = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!i) {
return GRIB_INVALID_INDEX;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_index_add_file(i, file);
2015-02-12 18:32:04 +00:00
return err;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_release(int* hid)
{
2015-02-12 18:32:04 +00:00
return clear_index(*hid);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_multi_release(int* hid)
{
2015-02-12 18:32:04 +00:00
return clear_multi_handle(*hid);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_release(int* hid)
{
2015-02-12 18:32:04 +00:00
return clear_handle(*hid);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_dump(int* gid)
{
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h)
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
else
2020-03-18 15:27:53 +00:00
grib_dump_content(h, stdout, "wmo", 0, NULL);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_print(int* gid, char* key)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2015-02-12 18:32:04 +00:00
grib_dumper* d = NULL;
2020-03-18 15:27:53 +00:00
if (!h) {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
d = grib_dumper_factory("serialize", h, stdout, 0, 0);
2015-02-12 18:32:04 +00:00
err = grib_print(h, key, d);
grib_dumper_delete(d);
2020-03-18 15:27:53 +00:00
return err;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_error_string(int* err, char* buf, int len)
{
2015-02-12 18:32:04 +00:00
const char* err_msg = grib_get_error_message(*err);
2020-03-18 15:27:53 +00:00
size_t erlen = strlen(err_msg);
if (len <= erlen) return GRIB_ARRAY_TOO_SMALL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
strncpy(buf, err_msg, (size_t)erlen);
2015-02-12 18:32:04 +00:00
buf[erlen] = '\0';
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_size_int(int* gid, char* key, int* val)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t tsize = 0;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_get_size(h, key, &tsize);
2015-02-12 18:32:04 +00:00
*val = tsize;
2020-03-18 15:27:53 +00:00
return err;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_message_offset(int* gid, size_t* offset)
{
2015-02-12 18:32:04 +00:00
int err = GRIB_SUCCESS;
off_t myoffset;
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 14:23:07 +00:00
2020-03-18 15:27:53 +00:00
if (!h)
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
else {
2020-03-18 15:27:53 +00:00
err = grib_get_message_offset(h, &myoffset);
*offset = myoffset;
2015-02-12 18:32:04 +00:00
return err;
}
2013-03-25 14:23:07 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_message_size(int* gid, size_t* size)
{
grib_handle* h = get_handle(*gid);
2013-03-25 14:23:07 +00:00
2020-03-18 15:27:53 +00:00
if (!h)
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
else
return grib_get_message_size(h, size);
2013-03-25 14:23:07 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_string_length(int* gid, char* key, size_t* val)
{
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h)
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
else
return grib_get_string_length(h, key, val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_size_long(int* gid, char* key, long* val)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t tsize = 0;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_get_size(h, key, &tsize);
2015-02-12 18:32:04 +00:00
*val = tsize;
2020-03-18 15:27:53 +00:00
return err;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_get_size_int(int* gid, char* key, int* val)
{
grib_index* h = get_index(*gid);
int err = GRIB_SUCCESS;
size_t tsize = 0;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_index_get_size(h, key, &tsize);
2015-02-12 18:32:04 +00:00
*val = tsize;
2020-03-18 15:27:53 +00:00
return err;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_get_size_long(int* gid, char* key, long* val)
{
grib_index* h = get_index(*gid);
int err = GRIB_SUCCESS;
size_t tsize = 0;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_index_get_size(h, key, &tsize);
2015-02-12 18:32:04 +00:00
*val = tsize;
2020-03-18 15:27:53 +00:00
return err;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_int(int* gid, char* key, int* val)
{
grib_handle* h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
long long_val;
int err = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
err = grib_get_long(h, key, &long_val);
2015-02-12 18:32:04 +00:00
*val = long_val;
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_long(int* gid, char* key, long* val)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
err = grib_get_long(h, key, val);
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_double(int* gid, char* key, double* val)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
err = grib_get_double(h, key, val);
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_int_array(int* gid, char* key, int* val, int* size)
{
grib_handle* h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
long* long_val = NULL;
2020-03-18 15:27:53 +00:00
int err = GRIB_SUCCESS;
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (sizeof(long) == sizeof(int)) {
2015-02-12 18:32:04 +00:00
long_val = (long*)val;
2020-03-18 15:27:53 +00:00
err = grib_get_long_array(h, key, long_val, &lsize);
*size = lsize;
return err;
2015-02-12 18:32:04 +00:00
}
2020-03-18 15:27:53 +00:00
if (*size)
long_val = grib_context_malloc(h->context, (*size) * (sizeof(long)));
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
long_val = grib_context_malloc(h->context, (sizeof(long)));
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!long_val) return GRIB_OUT_OF_MEMORY;
2015-02-12 18:32:04 +00:00
err = grib_get_long_array(h, key, long_val, &lsize);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
for (*size = 0; *size < lsize; (*size)++)
2015-02-12 18:32:04 +00:00
val[*size] = long_val[*size];
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, long_val);
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_long_array(int* gid, char* key, long* val, int* size)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
err = grib_get_long_array(h, key, val, &lsize);
*size = lsize;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_get_string(int* gid, char* key, char* val, int* eachsize, int* size)
{
grib_index* h = get_index(*gid);
int err = GRIB_SUCCESS;
2015-02-12 18:32:04 +00:00
int i;
size_t lsize = *size;
char** bufval;
2020-03-18 15:27:53 +00:00
char* p = val;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
bufval = (char**)grib_context_malloc_clear(h->context, sizeof(char*) * *size);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
err = grib_index_get_string(h, key, bufval, &lsize);
2013-03-25 12:04:10 +00:00
*size = lsize;
2015-02-12 18:32:04 +00:00
if (err) return err;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
for (i = 0; i < lsize; i++) {
int l = strlen(bufval[i]);
2015-02-12 18:32:04 +00:00
int j;
2020-03-18 15:27:53 +00:00
if (*eachsize < l) {
grib_context_free(h->context, bufval);
printf("eachsize=%d strlen(bufval[i])=%d\n", *eachsize, (unsigned int)strlen(bufval[i]));
2015-02-12 18:32:04 +00:00
return GRIB_ARRAY_TOO_SMALL;
}
2020-03-18 15:27:53 +00:00
memcpy(p, bufval[i], l);
p += l;
for (j = 0; j < *eachsize - l; j++)
*(p++) = ' ';
2015-02-12 18:32:04 +00:00
}
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, bufval);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_get_long(int* gid, char* key, long* val, int* size)
{
grib_index* h = get_index(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
err = grib_index_get_long(h, key, val, &lsize);
2015-02-12 18:32:04 +00:00
*size = lsize;
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_get_int(int* gid, char* key, int* val, int* size)
{
grib_index* h = get_index(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
long* lval = 0;
2015-02-12 18:32:04 +00:00
int i;
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
lval = grib_context_malloc(h->context, sizeof(long) * *size);
2015-02-12 18:32:04 +00:00
if (!lval) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_index_get_long(h, key, lval, &lsize);
2020-03-18 15:27:53 +00:00
for (i = 0; i < lsize; i++)
val[i] = lval[i];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*size = lsize;
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_index_get_real8(int* gid, char* key, double* val, int* size)
{
grib_index* h = get_index(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
err = grib_index_get_double(h, key, val, &lsize);
2015-02-12 18:32:04 +00:00
*size = lsize;
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_int_array(int* gid, char* key, int* val, int* size)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2015-02-12 18:32:04 +00:00
long* long_val = NULL;
2020-03-18 15:27:53 +00:00
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (sizeof(long) == sizeof(int)) {
2015-02-12 18:32:04 +00:00
long_val = (long*)val;
2020-03-18 15:27:53 +00:00
return grib_set_long_array(h, key, long_val, lsize);
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (lsize)
long_val = grib_context_malloc(h->context, (lsize) * (sizeof(long)));
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
long_val = grib_context_malloc(h->context, (sizeof(long)));
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!long_val) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
for (lsize = 0; lsize < (*size); lsize++)
2015-02-12 18:32:04 +00:00
long_val[lsize] = val[lsize];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_set_long_array(h, key, long_val, lsize);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, long_val);
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_long_array(int* gid, char* key, long* val, int* size)
{
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return grib_set_long_array(h, key, val, lsize);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_int(int* gid, char* key, int* val)
{
grib_handle* h = get_handle(*gid);
long long_val = *val;
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_set_long(h, key, long_val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_long(int* gid, char* key, long* val)
{
grib_handle* h = get_handle(*gid);
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_set_long(h, key, *val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_missing(int* gid, char* key)
{
grib_handle* h = get_handle(*gid);
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_missing(h, key);
2013-03-25 12:04:10 +00:00
}
/*
* Submits all keys with the given values in one go.
* 'keyvals' is a string of the form:
* key1=val1,key2=val2,key3=val3
*/
int grib_c_set_key_vals(int* gid, char* keyvals)
{
2015-02-12 18:32:04 +00:00
grib_handle* h = get_handle(*gid);
if (!h) return GRIB_INVALID_GRIB;
{
2020-03-18 15:27:53 +00:00
int err = GRIB_SUCCESS;
grib_values values[1024] = {{0,},};
2015-02-12 18:32:04 +00:00
int count = 1000; /* max. num key/val pairs */
if ((err = parse_keyval_string(NULL, keyvals, 1, GRIB_TYPE_UNDEFINED, values, &count)) != GRIB_SUCCESS) {
return err;
}
2020-03-18 15:27:53 +00:00
if ((err = grib_set_values(h, values, count)) != GRIB_SUCCESS) {
2015-02-12 18:32:04 +00:00
return err;
}
}
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_is_missing(int* gid, char* key, int* isMissing)
{
2020-03-18 15:27:53 +00:00
int err = 0;
grib_handle* h = get_handle(*gid);
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
*isMissing = grib_is_missing(h, key, &err);
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_is_defined(int* gid, char* key, int* isDefined)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
*isDefined = grib_is_defined(h, key);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
int grib_c_set_real4(int* gid, char* key, float* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
double val8 = *val;
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_double(h, key, val8);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_real4_element(int* gid, char* key, int* index, float* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
double val8 = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
err = grib_get_double_element(h, key, *index, &val8);
2015-02-12 18:32:04 +00:00
*val = val8;
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_real4_elements(int* gid, char* key, int* index, float* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
long i = 0;
double* val8 = NULL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (*size)
val8 = grib_context_malloc(h->context, (*size) * (sizeof(double)));
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
val8 = grib_context_malloc(h->context, sizeof(double));
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!val8) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
err = grib_get_double_elements(h, key, index, (long)lsize, val8);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
for (i = 0; i < lsize; (i)++)
2015-02-12 18:32:04 +00:00
val[i] = val8[i];
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, val8);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
int grib_c_get_real4(int* gid, char* key, float* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
double val8 = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
err = grib_get_double(h, key, &val8);
2015-02-12 18:32:04 +00:00
*val = val8;
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_real4_array(int* gid, char* key, float* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
double* val8 = NULL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (*size)
val8 = grib_context_malloc(h->context, (*size) * (sizeof(double)));
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
val8 = grib_context_malloc(h->context, sizeof(double));
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!val8) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
err = grib_get_double_array(h, key, val8, &lsize);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
for (*size = 0; *size < lsize; (*size)++)
2015-02-12 18:32:04 +00:00
val[*size] = val8[*size];
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, val8);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_real4_array(int* gid, char* key, float* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
double* val8 = NULL;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (*size)
val8 = grib_context_malloc(h->context, lsize * (sizeof(double)));
2015-02-12 18:32:04 +00:00
else
2020-03-18 15:27:53 +00:00
val8 = grib_context_malloc(h->context, sizeof(double));
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!val8) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
for (lsize = 0; lsize < *size; lsize++)
2015-02-12 18:32:04 +00:00
val8[lsize] = val[lsize];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_set_double_array(h, key, val8, lsize);
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, val8);
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
int grib_c_index_select_real8(int* gid, char* key, double* val)
{
2020-03-18 15:27:53 +00:00
grib_index* h = get_index(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_index_select_double(h, key, *val);
2013-03-25 12:04:10 +00:00
}
int grib_c_index_select_string(int* gid, char* key, char* val)
{
2020-03-18 15:27:53 +00:00
grib_index* h = get_index(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_index_select_string(h, key, val);
2013-03-25 12:04:10 +00:00
}
int grib_c_index_select_int(int* gid, char* key, int* val)
{
2020-03-18 15:27:53 +00:00
grib_index* h = get_index(*gid);
long lval = *val;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_index_select_long(h, key, lval);
2013-03-25 12:04:10 +00:00
}
int grib_c_index_select_long(int* gid, char* key, long* val)
{
2020-03-18 15:27:53 +00:00
grib_index* h = get_index(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_index_select_long(h, key, *val);
2013-03-25 12:04:10 +00:00
}
int grib_c_set_real8(int* gid, char* key, double* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
return grib_set_double(h, key, *val);
2013-03-25 12:04:10 +00:00
}
int grib_c_get_real8(int* gid, char* key, double* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_get_double(h, key, val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_double(int* gid, char* key, double* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_double(h, key, *val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_real8_element(int* gid, char* key, int* index, double* val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return grib_get_double_element(h, key, *index, val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_real8_elements(int* gid, char* key, int* index, double* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return grib_get_double_elements(h, key, index, *size, val);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_find_nearest_four_single(int* gid, int* is_lsm,
double* inlat, double* inlon,
double* outlats, double* outlons,
double* values, double* distances,
int* indexes)
{
2020-03-18 15:27:53 +00:00
grib_nearest* nearest = NULL;
int err = 0, result = 0;
unsigned long flags = 0;
size_t len = 4;
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
nearest = grib_nearest_new(h, &err);
if (err != GRIB_SUCCESS) return err;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
result = grib_nearest_find(nearest, h, *inlat, *inlon,
flags, outlats, outlons, values, distances, indexes, &len);
2015-02-12 18:32:04 +00:00
grib_nearest_delete(nearest);
return result;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_find_nearest_single(int* gid, int* is_lsm,
double* inlats, double* inlons,
double* outlats, double* outlons,
double* values, double* distances,
int* indexes)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return grib_nearest_find_multiple(h, *is_lsm,
inlats, inlons, 1, outlats, outlons,
values, distances, indexes);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_find_nearest_multiple(int* gid, int* is_lsm,
double* inlats, double* inlons,
double* outlats, double* outlons,
double* values, double* distances,
int* indexes, int* npoints)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return grib_nearest_find_multiple(h, *is_lsm,
inlats, inlons, *npoints, outlats, outlons,
values, distances, indexes);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_real8_array(int* gid, char* key, double* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = *size;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!h) {
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_get_double_array(h, key, val, &lsize);
2015-02-12 18:32:04 +00:00
*size = lsize;
2020-03-18 15:27:53 +00:00
return err;
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_real8_array(int* gid, char* key, double* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_double_array(h, key, val, lsize);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_double_array(int* gid, char* key, double* val, int* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_double_array(h, key, val, lsize);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_string(int* gid, char* key, char* val, size_t* lsize)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_get_string(h, key, val, lsize);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_string_array(int* gid, char* key, char** val, size_t* lsize)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2015-05-11 13:58:23 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2015-05-11 13:58:23 +00:00
err = grib_get_string_array(h, key, val, lsize);
2020-03-18 15:27:53 +00:00
return err;
2015-05-11 13:58:23 +00:00
}
int grib_c_set_string(int* gid, char* key, char* val, int len2)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
size_t lsize = len2;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_string(h, key, val, &lsize);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_set_string_array(int* gid, char* key, const char** val)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t lsize = 0;
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2016-08-02 17:07:03 +00:00
/* Note: The array passed in will have its final entry as NULL */
/* so to find its size we just iterate thru it. */
/* See typemap for char** in swig interface file */
2020-03-18 15:27:53 +00:00
while (val[lsize]) {
++lsize;
}
err = grib_set_string_array(h, key, val, lsize);
return err;
}
2020-03-18 15:27:53 +00:00
int grib_c_get_data_real4(int* gid, float* lats, float* lons, float* values, size_t* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
double *lat8 = NULL, *lon8 = NULL, *val8 = NULL;
size_t i = 0;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
val8 = grib_context_malloc(h->context, (*size) * (sizeof(double)));
if (!val8) return GRIB_OUT_OF_MEMORY;
lon8 = grib_context_malloc(h->context, (*size) * (sizeof(double)));
if (!lon8) return GRIB_OUT_OF_MEMORY;
lat8 = grib_context_malloc(h->context, (*size) * (sizeof(double)));
if (!lat8) return GRIB_OUT_OF_MEMORY;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
err = grib_get_data(h, lat8, lon8, val8);
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
for (i = 0; i < *size; i++) {
2015-02-12 18:32:04 +00:00
values[i] = val8[i];
2020-03-18 15:27:53 +00:00
lats[i] = lat8[i];
lons[i] = lon8[i];
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_context_free(h->context, val8);
grib_context_free(h->context, lat8);
grib_context_free(h->context, lon8);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
/*
int grib_c_get_data_real8(int* gid,double* lats, double* lons,double* values,size_t* size)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
return grib_get_data(h,lats,lons,values);
2013-03-25 12:04:10 +00:00
}
*/
2020-03-18 15:27:53 +00:00
int grib_c_copy_message(int* gid, void* mess, size_t* len)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
if (!h)
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (*len < h->buffer->ulength) {
grib_context_log(h->context, GRIB_LOG_ERROR, "grib_copy_message: buffer=%ld message size=%ld", *len, h->buffer->ulength);
2015-02-12 18:32:04 +00:00
return GRIB_BUFFER_TOO_SMALL;
}
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
memcpy(mess, h->buffer->data, h->buffer->ulength);
*len = h->buffer->ulength;
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_bufr_copy_data(int* msgid_src, int* msgid_dst)
{
2020-03-18 15:27:53 +00:00
int err = 0;
grib_handle* src = get_handle(*msgid_src);
grib_handle* dest = get_handle(*msgid_dst);
if (!src || !dest) {
return GRIB_INVALID_GRIB;
}
err = codes_bufr_copy_data(src, dest);
return err;
}
2020-03-18 15:27:53 +00:00
void grib_c_check(int* err, char* call, char* str)
{
2020-03-18 15:27:53 +00:00
grib_context* c = grib_context_get_default();
if (*err == GRIB_SUCCESS || *err == GRIB_END_OF_FILE) return;
grib_context_log(c, GRIB_LOG_ERROR, "%s: %s %s",
call, str, grib_get_error_message(*err));
2015-02-12 18:32:04 +00:00
exit(*err);
2013-03-25 12:04:10 +00:00
}
int grib_c_write(int* gid, FILE* f)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
const void* mess = NULL;
2020-03-18 15:27:53 +00:00
size_t mess_len = 0;
2015-02-12 18:32:04 +00:00
2020-03-18 15:27:53 +00:00
if (!f) return GRIB_INVALID_FILE;
2015-02-12 18:32:04 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
grib_get_message(h, &mess, &mess_len);
if (fwrite(mess, 1, mess_len, f) != mess_len) {
2015-02-12 18:32:04 +00:00
perror("grib_write");
return GRIB_IO_PROBLEM;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
int grib_c_multi_new(int* mgid)
{
2020-03-18 15:27:53 +00:00
grib_multi_handle* mh = grib_multi_handle_new(0);
2013-03-25 12:04:10 +00:00
if (!mh) return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
push_multi_handle(mh, mgid);
2013-03-25 12:04:10 +00:00
return GRIB_SUCCESS;
}
int grib_c_multi_write(int* gid, FILE* f)
{
2020-03-18 15:27:53 +00:00
grib_multi_handle* h = get_multi_handle(*gid);
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
if (!f) return GRIB_INVALID_FILE;
2015-02-12 18:32:04 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2020-03-18 15:27:53 +00:00
return grib_multi_handle_write(h, f);
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_multi_append(int* ingid, int* sec, int* mgid)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*ingid);
grib_multi_handle* mh = get_multi_handle(*mgid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!mh) {
2020-03-18 15:27:53 +00:00
mh = grib_multi_handle_new(h->context);
push_multi_handle(mh, mgid);
2015-02-12 18:32:04 +00:00
}
2020-03-18 15:27:53 +00:00
return grib_multi_handle_append(h, *sec, mh);
2013-03-25 12:04:10 +00:00
}
int grib_c_get_native_type(int* gid, char* key, int* type)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_get_native_type(h, key, type);
2013-03-25 12:04:10 +00:00
}
int grib_c_index_write(int* gid, char* file)
{
2020-03-18 15:27:53 +00:00
grib_index* i = get_index(*gid);
int err = GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!i) {
return GRIB_INVALID_GRIB;
2020-03-18 15:27:53 +00:00
}
else {
err = grib_index_write(i, file);
2015-02-12 18:32:04 +00:00
return err;
}
2013-03-25 12:04:10 +00:00
}
int grib_c_index_read(char* file, int* gid)
{
2020-03-18 15:27:53 +00:00
int err = 0;
grib_index* i = NULL;
2015-02-12 18:32:04 +00:00
if (*file) {
2020-03-18 15:27:53 +00:00
i = grib_index_read(0, file, &err);
2015-02-12 18:32:04 +00:00
if (i) {
2020-03-18 15:27:53 +00:00
push_index(i, gid);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2020-03-18 15:27:53 +00:00
}
else {
2015-02-12 18:32:04 +00:00
*gid = -1;
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
void no_fail_on_wrong_length(int flag)
{
2020-03-18 15:27:53 +00:00
grib_context* c = grib_context_get_default();
2015-02-12 18:32:04 +00:00
int value;
assert(c != NULL);
2020-03-18 15:27:53 +00:00
value = (flag != 0) ? 1 : 0;
2015-02-12 18:32:04 +00:00
c->no_fail_on_wrong_length = value;
2013-03-25 12:04:10 +00:00
}
void grib_c_gts_header_on(void)
{
2020-03-18 15:27:53 +00:00
grib_context* c = grib_context_get_default();
2015-02-12 18:32:04 +00:00
assert(c != NULL);
grib_gts_header_on(c);
2013-03-25 12:04:10 +00:00
}
void grib_c_gts_header_off(void)
{
2020-03-18 15:27:53 +00:00
grib_context* c = grib_context_get_default();
2015-02-12 18:32:04 +00:00
assert(c != NULL);
grib_gts_header_off(c);
2013-03-25 12:04:10 +00:00
}
long grib_c_get_api_version(void)
{
2015-02-12 18:32:04 +00:00
return grib_get_api_version();
2013-03-25 12:04:10 +00:00
}
2020-03-18 15:27:53 +00:00
int grib_c_get_message(int* gid, const void** msg, size_t* size)
{
2020-03-18 15:27:53 +00:00
grib_handle* h = get_handle(*gid);
return grib_get_message(h, msg, size);
2013-03-25 12:04:10 +00:00
}
void grib_c_set_definitions_path(const char* path)
{
2020-03-18 15:27:53 +00:00
grib_context* c = grib_context_get_default();
grib_context_set_definitions_path(c, path);
}
void grib_c_set_samples_path(const char* path)
{
2020-03-18 15:27:53 +00:00
grib_context* c = grib_context_get_default();
grib_context_set_samples_path(c, path);
}
2019-08-09 13:52:12 +00:00
int codes_c_bufr_multi_element_constant_arrays_on(void)
{
codes_bufr_multi_element_constant_arrays_on(NULL);
return GRIB_SUCCESS;
}
int codes_c_bufr_multi_element_constant_arrays_off(void)
{
codes_bufr_multi_element_constant_arrays_off(NULL);
return GRIB_SUCCESS;
}