eccodes/fortran/grib_fortran.cc

2927 lines
78 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"
2024-04-16 16:33:57 +00:00
#include "grib_fortran_prototypes.h"
2013-03-25 12:04:10 +00:00
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
2019-11-07 12:03:45 +00:00
/*
2013-03-25 12:04:10 +00:00
#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
2019-11-07 12:03:45 +00:00
*/
2013-03-25 12:04:10 +00:00
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include <ctype.h>
2023-12-30 12:32:31 +00:00
/* Have file ids distinct from GRIB/BUFR ids, in order to be
* protected against user errors where a file id is given
* instead of a GRIB/BUFR id or vice versa
*/
2013-03-25 12:04:10 +00:00
#define MIN_FILE_ID 50000
#if GRIB_PTHREADS
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 read_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;
2024-04-10 16:06:19 +00:00
static void init(void) {
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(&read_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
static int once = 0;
static omp_nest_lock_t handle_mutex;
static omp_nest_lock_t index_mutex;
2017-11-20 10:37:00 +00:00
static omp_nest_lock_t read_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()
{
2015-12-30 14:39:02 +00:00
GRIB_OMP_CRITICAL(lock_fortran)
2023-12-30 12:32:31 +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);
2017-11-20 10:37:00 +00:00
omp_init_nest_lock(&read_mutex);
2015-12-30 14:39:02 +00:00
omp_init_nest_lock(&multi_handle_mutex);
omp_init_nest_lock(&iterator_mutex);
omp_init_nest_lock(&keys_iterator_mutex);
once = 1;
}
2023-12-30 12:32:31 +00:00
}
}
2013-03-25 12:04:10 +00:00
#endif
typedef enum FileMode {
FILE_MODE_READ,
FILE_MODE_WRITE,
FILE_MODE_APPEND
} FileMode;
2023-12-30 12:32:31 +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;
struct l_grib_file {
2015-02-12 18:32:04 +00:00
FILE* f;
char* buffer;
int id;
2022-02-09 22:17:37 +00:00
FileMode mode;
2015-02-12 18:32:04 +00:00
l_grib_file* next;
2013-03-25 12:04:10 +00:00
};
typedef struct l_grib_handle l_grib_handle;
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;
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;
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;
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;
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;
struct l_bufr_keys_iterator {
int id;
bufr_keys_iterator* i;
l_bufr_keys_iterator* next;
};
typedef struct l_binary_message l_binary_message;
struct l_binary_message {
size_t size;
void* data;
};
typedef struct l_message_info l_message_info;
struct l_message_info {
off_t offset;
size_t size;
};
2013-03-25 12:04:10 +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;
2023-08-25 17:43:28 +00:00
/*static l_grib_iterator* iterator_set = NULL;*/
2013-03-25 12:04:10 +00:00
static l_grib_keys_iterator* keys_iterator_set = NULL;
static l_bufr_keys_iterator* bufr_keys_iterator_set = NULL;
static grib_oarray* binary_messages = NULL;
static grib_oarray* info_messages = NULL;
2013-03-25 12:04:10 +00:00
/* Convert from Fortran string to C string - chop at first space character */
static char* cast_char(char* buf, char* fortstr, int len)
{
2015-02-12 18:32:04 +00:00
char *p,*end;
if (len == 0 || fortstr == NULL) return NULL;
memcpy(buf,fortstr,len);
p=buf;
end=buf+len-1;
while (isgraph(*p) && p != end) {
p++;
}
if (*p==' ') *p='\0';
if (p==end) *(p+1)='\0';
else *p='\0';
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return buf;
2013-03-25 12:04:10 +00:00
}
/* Convert from Fortran string to C string - non chopping version */
static char* cast_char_no_cut(char* buf, char* fortstr, int len)
{
if (len == 0 || fortstr == NULL) return NULL;
memcpy(buf,fortstr,len);
buf[len]='\0';
return buf;
}
static void czstr_to_fortran(char* str, int len)
{
2015-02-12 18:32:04 +00:00
char *p,*end;
p=str; end=str+len-1;
while (*p != '\0' && p != end) p++;
while (p !=end) *(p++)=' ';
*p=' ';
2013-03-25 12:04:10 +00:00
}
/*static void czstr_to_fortran_replace0(char* str,int len)
{
char *p,*end;
p=str; end=str+len-1;
while (p != end) {
if (*p=='\0') *p=' ';
p++;
}
}*/
static void fort_char_clean(char* str,int len)
{
2015-02-12 18:32:04 +00:00
char *p,*end;
p=str; end=str+len-1;
while (p != end) *(p++)=' ';
*p=' ';
2013-03-25 12:04:10 +00:00
}
/* Note: the open_mode argument will be all lowercase. See grib_f_open_file_ */
static int push_file(FILE* f, const char* open_mode, char* buffer)
{
2015-02-12 18:32:04 +00:00
l_grib_file* current = file_set;
l_grib_file* previous = file_set;
l_grib_file* the_new = NULL;
int myindex = MIN_FILE_ID;
FileMode fmode = FILE_MODE_READ;
if (strcmp(open_mode, "w") == 0) fmode = FILE_MODE_WRITE;
else if (strcmp(open_mode, "a") == 0) fmode = FILE_MODE_APPEND;
2015-02-12 18:32:04 +00:00
if(!file_set){
file_set = (l_grib_file*)malloc(sizeof(l_grib_file));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(file_set);
file_set->id = myindex;
file_set->f = f;
file_set->mode = fmode;
file_set->buffer = buffer;
file_set->next = NULL;
2015-02-12 18:32:04 +00:00
return myindex;
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id < 0){
current->id = -(current->id);
current->f = f;
current->mode = fmode;
current->buffer = buffer;
2015-02-12 18:32:04 +00:00
return current->id ;
} else{
myindex++;
previous = current;
current = current->next;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
the_new = (l_grib_file*)malloc(sizeof(l_grib_file));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
the_new->id = myindex;
the_new->f = f;
the_new->mode = fmode;
the_new->buffer = buffer;
the_new->next = current;
previous->next = the_new;
2015-02-12 18:32:04 +00:00
return myindex;
2013-03-25 12:04:10 +00:00
}
static void _push_handle(grib_handle *h,int *gid)
{
2020-02-19 16:39:06 +00:00
l_grib_handle* current= NULL;
2015-02-12 18:32:04 +00:00
l_grib_handle* previous= handle_set;
l_grib_handle* the_new= NULL;
int myindex= 1;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
/*
2013-03-25 12:04:10 +00:00
if (*gid > 0 ) {
while(current) {
if(current->id == *gid) break;
current = current->next;
}
if (current) {
grib_handle_delete(current->h);
current->h=h;
return;
}
}
2015-02-12 18:32:04 +00:00
*/
if(!handle_set){
handle_set = (l_grib_handle*)malloc(sizeof(l_grib_handle));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(handle_set);
2015-02-12 18:32:04 +00:00
handle_set->id = myindex;
handle_set->h = h;
handle_set->next = NULL;
*gid=myindex;
return;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
current= handle_set;
while(current){
if(current->id < 0){
current->id = -(current->id);
current->h = h;
*gid=current->id;
return;
}
else{
myindex++;
previous = current;
current = current->next;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
the_new = (l_grib_handle*)malloc(sizeof(l_grib_handle));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
2015-02-12 18:32:04 +00:00
the_new->id = myindex;
the_new->h = h;
the_new->next = current;
previous->next = the_new;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*gid=myindex;
return;
2013-03-25 12:04:10 +00:00
}
static void _push_index(grib_index *h,int *gid)
{
2020-02-20 12:03:49 +00:00
l_grib_index* current= NULL;
2015-02-12 18:32:04 +00:00
l_grib_index* previous= index_set;
l_grib_index* the_new= NULL;
int myindex= 1;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
/*
2020-02-20 12:03:49 +00:00
if (*gid > 0 ) {
while(current) {
if(current->id == *gid) break;
current = current->next;
}
if (current) {
grib_index_delete(current->h);
current->h=h;
return;
}
2013-03-25 12:04:10 +00:00
}
2020-02-20 12:03:49 +00:00
*/
2015-02-12 18:32:04 +00:00
if(!index_set){
index_set = (l_grib_index*)malloc(sizeof(l_grib_index));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(index_set);
2015-02-12 18:32:04 +00:00
index_set->id = myindex;
index_set->h = h;
index_set->next = NULL;
*gid=myindex;
return;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
current= index_set;
while(current){
if(current->id < 0){
current->id = -(current->id);
current->h = h;
*gid=current->id;
return;
}
else{
myindex++;
previous = current;
current = current->next;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
the_new = (l_grib_index*)malloc(sizeof(l_grib_index));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
2015-02-12 18:32:04 +00:00
the_new->id = myindex;
the_new->h = h;
the_new->next = current;
previous->next = the_new;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*gid=myindex;
return;
2013-03-25 12:04:10 +00:00
}
static void _push_multi_handle(grib_multi_handle *h,int *gid)
{
2020-02-20 12:03:49 +00:00
l_grib_multi_handle* current= NULL;
2015-02-12 18:32:04 +00:00
l_grib_multi_handle* previous= multi_handle_set;
l_grib_multi_handle* the_new= NULL;
int myindex= 1;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
/*
2020-02-20 12:03:49 +00:00
if (*gid > 0 ) {
while(current) {
if(current->id == *gid) break;
current = current->next;
}
if (current) {
grib_multi_handle_delete(current->h);
current->h=h;
return;
}
2013-03-25 12:04:10 +00:00
}
2020-02-20 12:03:49 +00:00
*/
2015-02-12 18:32:04 +00:00
if(!multi_handle_set){
multi_handle_set = (l_grib_multi_handle*)malloc(sizeof(l_grib_multi_handle));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(multi_handle_set);
2015-02-12 18:32:04 +00:00
multi_handle_set->id = myindex;
multi_handle_set->h = h;
multi_handle_set->next = NULL;
*gid=myindex;
return;
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
current= multi_handle_set;
while(current){
if(current->id < 0){
current->id = -(current->id);
current->h = h;
*gid=current->id;
return;
}
else{
myindex++;
previous = current;
current = current->next;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
the_new = (l_grib_multi_handle*)malloc(sizeof(l_grib_multi_handle));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
2015-02-12 18:32:04 +00:00
the_new->id = myindex;
the_new->h = h;
the_new->next = current;
previous->next = the_new;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*gid=myindex;
return;
2013-03-25 12:04:10 +00:00
}
static void push_handle(grib_handle *h,int *gid)
{
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&handle_mutex);
_push_handle(h,gid);
GRIB_MUTEX_UNLOCK(&handle_mutex);
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
static void push_index(grib_index *h,int *gid)
{
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&index_mutex);
_push_index(h,gid);
GRIB_MUTEX_UNLOCK(&index_mutex);
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +00:00
}
static void push_multi_handle(grib_multi_handle *h,int *gid)
{
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&multi_handle_mutex);
_push_multi_handle(h,gid);
GRIB_MUTEX_UNLOCK(&multi_handle_mutex);
2015-02-12 18:32:04 +00:00
return;
2013-03-25 12:04:10 +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* the_new = NULL;
int myindex = 1;
if(!keys_iterator_set){
keys_iterator_set = (l_grib_keys_iterator*)malloc(sizeof(l_grib_keys_iterator));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(keys_iterator_set);
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
while(current){
if(current->id < 0){
current->id = -(current->id);
current->i = i;
return current->id;
}
else{
myindex++;
previous = current;
current = current->next;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
if(!previous) return -1;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
the_new = (l_grib_keys_iterator*)malloc(sizeof(l_grib_keys_iterator));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
2015-02-12 18:32:04 +00:00
the_new->id = myindex;
the_new->i = i;
the_new->next = current;
previous->next = the_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
}
static int push_keys_iterator(grib_keys_iterator *i)
{
2015-02-12 18:32:04 +00:00
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
2015-02-12 18:32:04 +00:00
ret=_push_keys_iterator(i);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
2015-02-12 18:32:04 +00:00
return ret;
2013-03-25 12:04:10 +00:00
}
2017-05-02 16:59:04 +00:00
/* BUFR Keys iterator */
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;
l_bufr_keys_iterator* the_new = NULL;
int myindex = 1;
if(!bufr_keys_iterator_set){
bufr_keys_iterator_set = (l_bufr_keys_iterator*)malloc(sizeof(l_bufr_keys_iterator));
2024-12-20 12:58:07 +00:00
ECCODES_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;
}
while(current){
if(current->id < 0){
current->id = -(current->id);
current->i = i;
return current->id;
}
else{
myindex++;
previous = current;
current = current->next;
}
}
if(!previous) return -1;
the_new = (l_bufr_keys_iterator*)malloc(sizeof(l_bufr_keys_iterator));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
the_new->id = myindex;
the_new->i = i;
the_new->next = current;
previous->next = the_new;
return myindex;
}
static int push_bufr_keys_iterator(bufr_keys_iterator *i)
{
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
ret=_push_bufr_keys_iterator(i);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
return ret;
}
static grib_handle* _get_handle(int handle_id)
{
2015-02-12 18:32:04 +00:00
l_grib_handle* current= handle_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == handle_id) return current->h;
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_index* _get_index(int index_id)
{
2015-02-12 18:32:04 +00:00
l_grib_index* current= index_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == index_id) return current->h;
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_multi_handle* _get_multi_handle(int multi_handle_id)
{
2015-02-12 18:32:04 +00:00
l_grib_multi_handle* current= multi_handle_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == multi_handle_id) return current->h;
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_handle* get_handle(int handle_id)
{
2015-02-12 18:32:04 +00:00
grib_handle* h=NULL;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&handle_mutex);
2015-02-12 18:32:04 +00:00
h=_get_handle(handle_id);
GRIB_MUTEX_UNLOCK(&handle_mutex);
2015-02-12 18:32:04 +00:00
return h;
2013-03-25 12:04:10 +00:00
}
static grib_index* get_index(int index_id)
{
2015-02-12 18:32:04 +00:00
grib_index* h=NULL;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&index_mutex);
2015-02-12 18:32:04 +00:00
h=_get_index(index_id);
GRIB_MUTEX_UNLOCK(&index_mutex);
2015-02-12 18:32:04 +00:00
return h;
2013-03-25 12:04:10 +00:00
}
static grib_multi_handle* get_multi_handle(int multi_handle_id)
{
2015-02-12 18:32:04 +00:00
grib_multi_handle* h=NULL;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&multi_handle_mutex);
2015-02-12 18:32:04 +00:00
h=_get_multi_handle(multi_handle_id);
GRIB_MUTEX_UNLOCK(&multi_handle_mutex);
2015-02-12 18:32:04 +00:00
return h;
2013-03-25 12:04:10 +00:00
}
static FILE* get_file(int file_id)
{
2015-02-12 18:32:04 +00:00
l_grib_file* current = file_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if ( file_id < MIN_FILE_ID ) return NULL;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == file_id) return current->f;
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_keys_iterator* _get_keys_iterator(int keys_iterator_id)
{
2015-02-12 18:32:04 +00:00
l_grib_keys_iterator* current = keys_iterator_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == keys_iterator_id) return current->i;
current = current->next;
}
return NULL;
2013-03-25 12:04:10 +00:00
}
static grib_keys_iterator* get_keys_iterator(int keys_iterator_id)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* i=NULL;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
2015-02-12 18:32:04 +00:00
i=_get_keys_iterator(keys_iterator_id);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
2015-02-12 18:32:04 +00:00
return i;
2013-03-25 12:04:10 +00:00
}
2017-05-02 16:59:04 +00:00
/* BUFR */
static bufr_keys_iterator* _get_bufr_keys_iterator(int keys_iterator_id)
{
l_bufr_keys_iterator* current = bufr_keys_iterator_set;
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)
{
bufr_keys_iterator* i=NULL;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
i=_get_bufr_keys_iterator(keys_iterator_id);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
return i;
}
static int clear_file(int file_id)
{
int err = 0;
2015-02-12 18:32:04 +00:00
l_grib_file* current = file_set;
while(current){
if(current->id == file_id){
current->id = -(current->id);
if (current->f) {
if (current->mode == FILE_MODE_WRITE || current->mode == FILE_MODE_APPEND)
err = codes_flush_sync_close_file(current->f);
else
err = fclose(current->f);
if (err) {
int ioerr = errno;
grib_context* c = grib_context_get_default();
grib_context_log(c,(GRIB_LOG_ERROR)|(GRIB_LOG_PERROR),"IO ERROR: %s",strerror(ioerr));
return GRIB_IO_PROBLEM;
}
}
2015-02-12 18:32:04 +00:00
if (current->buffer) free(current->buffer);
return GRIB_SUCCESS;
}
current = current->next;
}
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
static int _clear_handle(int handle_id)
{
2015-02-12 18:32:04 +00:00
l_grib_handle* current = handle_set;
if (handle_id<0) return 0;
while(current){
if(current->id == handle_id){
current->id = -(current->id);
if(current->h) return grib_handle_delete(current->h);
}
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_index(int index_id)
{
2015-02-12 18:32:04 +00:00
l_grib_index* current = index_set;
while(current){
if(current->id == index_id){
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
}
2024-01-11 12:37:38 +00:00
#if 0
static int _clear_multi_handle(int multi_handle_id)
{
2015-02-12 18:32:04 +00:00
l_grib_multi_handle* current = multi_handle_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == multi_handle_id){
current->id = -(current->id);
if(current->h) return grib_multi_handle_delete(current->h);
}
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
}
2024-01-11 12:37:38 +00:00
#endif
2013-03-25 12:04:10 +00:00
static int clear_handle(int handle_id)
{
2015-02-12 18:32:04 +00:00
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&handle_mutex);
2015-02-12 18:32:04 +00:00
ret=_clear_handle(handle_id);
GRIB_MUTEX_UNLOCK(&handle_mutex);
2015-02-12 18:32:04 +00:00
return ret;
2013-03-25 12:04:10 +00:00
}
static int clear_index(int index_id)
{
2015-02-12 18:32:04 +00:00
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&index_mutex);
2015-02-12 18:32:04 +00:00
ret=_clear_index(index_id);
GRIB_MUTEX_UNLOCK(&index_mutex);
2015-02-12 18:32:04 +00:00
return ret;
2013-03-25 12:04:10 +00:00
}
2024-01-11 12:37:38 +00:00
#if 0
static int clear_multi_handle(int multi_handle_id)
{
2015-02-12 18:32:04 +00:00
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&multi_handle_mutex);
2015-02-12 18:32:04 +00:00
ret=_clear_multi_handle(multi_handle_id);
GRIB_MUTEX_UNLOCK(&multi_handle_mutex);
2015-02-12 18:32:04 +00:00
return ret;
2013-03-25 12:04:10 +00:00
}
2024-01-11 12:37:38 +00:00
#endif
2013-03-25 12:04:10 +00:00
static int _clear_keys_iterator(int keys_iterator_id)
{
2015-02-12 18:32:04 +00:00
l_grib_keys_iterator* current = keys_iterator_set;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
while(current){
if(current->id == keys_iterator_id){
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)
{
2015-02-12 18:32:04 +00:00
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
2015-02-12 18:32:04 +00:00
ret=_clear_keys_iterator(keys_iterator_id);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
2015-02-12 18:32:04 +00:00
return ret;
2013-03-25 12:04:10 +00:00
}
2017-05-02 16:59:04 +00:00
/* BUFR */
static int _clear_bufr_keys_iterator(int keys_iterator_id)
{
l_bufr_keys_iterator* current = bufr_keys_iterator_set;
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)
{
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&keys_iterator_mutex);
ret=_clear_bufr_keys_iterator(keys_iterator_id);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex);
return ret;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-08 13:42:39 +00:00
#if 0
2024-01-11 13:40:07 +00:00
int grib_f_read_any_headers_only_from_file_(int* fid, char* buffer, size_t* nbytes)
{
2015-02-12 18:32:04 +00:00
grib_context* c;
int err=0;
FILE* f=get_file(*fid);
if (f) {
c=grib_context_get_default( );
err=grib_read_any_headers_only_from_file(c,f,buffer,nbytes);
return err;
} else {
return GRIB_INVALID_FILE;
}
2013-03-25 12:04:10 +00:00
}
2024-01-08 13:42:39 +00:00
#endif
2013-03-25 12:04:10 +00:00
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_read_any_from_file_(int* fid, void* buffer, size_t* nbytes)
{
FILE* f = get_file(*fid);
2015-02-12 18:32:04 +00:00
if (f) {
grib_context* c = grib_context_get_default( );
int err=grib_read_any_from_file(c,f,buffer,nbytes);
2015-02-12 18:32:04 +00:00
return err;
} else {
return GRIB_INVALID_FILE;
}
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_write_file_(int* fid, void* buffer, size_t* nbytes)
{
2015-02-12 18:32:04 +00:00
grib_context* c;
FILE* f=get_file(*fid);
if (f) {
int ioerr;
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));
return GRIB_IO_PROBLEM;
}
return GRIB_SUCCESS;
} else {
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_read_file_(int* fid, void* buffer, size_t* nbytes)
{
2015-02-12 18:32:04 +00:00
grib_context* c;
FILE* f=get_file(*fid);
if (f) {
int ioerr;
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));
return GRIB_IO_PROBLEM;
}
return GRIB_SUCCESS;
} else {
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_open_file_(int* fid, char* name , char* op, int lname, int lop)
{
2015-02-12 18:32:04 +00:00
FILE* f = NULL;
int ioerr=0;
char oper[1024]; /* GRIB-576: open mode */
2015-02-12 18:32:04 +00:00
char *p;
char fname[1024];
int ret=GRIB_SUCCESS;
char* iobuf=NULL;
char* trimmed = NULL; /* filename trimmed */
2015-02-12 18:32:04 +00:00
grib_context* context=grib_context_get_default();
cast_char(oper,op,lop);
p=oper;
while (*p != '\0') { *p=tolower(*p);p++;}
trimmed = cast_char_no_cut(fname,name,lname); /* ECC-1392 */
2022-06-10 19:51:39 +00:00
string_rtrim( trimmed );
f = fopen(trimmed, oper);
2015-02-12 18:32:04 +00:00
if(!f) {
ioerr=errno;
grib_context_log(context,(GRIB_LOG_ERROR)|(GRIB_LOG_PERROR),"IO ERROR: %s: '%s'",strerror(ioerr),trimmed);
2015-02-12 18:32:04 +00:00
*fid = -1;
ret=GRIB_IO_PROBLEM;
} else {
if (context->io_buffer_size) {
/* printf("setting vbuf = %d\n",context->io_buffer_size); */
2013-03-25 12:04:10 +00:00
#ifdef POSIX_MEMALIGN
2015-02-12 18:32:04 +00:00
if (posix_memalign((void **)&iobuf,sysconf(_SC_PAGESIZE),context->io_buffer_size) ) {
grib_context_log(context,GRIB_LOG_FATAL,"grib_f_open_file_: posix_memalign unable to allocate io_buffer\n");
}
2013-03-25 12:04:10 +00:00
#else
2015-02-12 18:32:04 +00:00
iobuf = (void*)malloc(context->io_buffer_size);
if (!iobuf) {
grib_context_log(context,GRIB_LOG_FATAL,"grib_f_open_file_: Unable to allocate io_buffer\n");
}
2013-03-25 12:04:10 +00:00
#endif
2015-02-12 18:32:04 +00:00
setvbuf(f,iobuf,_IOFBF,context->io_buffer_size);
}
*fid = push_file(f, oper, iobuf);
2015-02-12 18:32:04 +00:00
ret=GRIB_SUCCESS;
}
return ret;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_close_file_(int* fid)
{
2015-02-12 18:32:04 +00:00
return clear_file(*fid);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2013-03-25 12:04:10 +00:00
static int file_count=0;
void grib_f_write_on_fail(int* gid)
{
grib_context* c = grib_context_get_default();
2015-02-12 18:32:04 +00:00
if (c->write_on_fail) {
char filename[100]={0,};
grib_handle* h=NULL;
pid_t pid=getpid();
2013-03-25 12:04:10 +00:00
GRIB_MUTEX_INIT_ONCE(&once,&init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&handle_mutex)
file_count++;
GRIB_MUTEX_UNLOCK(&handle_mutex)
2013-03-25 12:04:10 +00:00
2024-05-08 09:15:10 +00:00
snprintf(filename, sizeof(filename), "%ld_%d_error.grib",(long)pid,file_count);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
h=get_handle(*gid);
if (h) grib_write_message(h,filename,"w");
}
2013-03-25 12:04:10 +00:00
}
void grib_f_write_on_fail_(int* gid)
{
2015-02-12 18:32:04 +00:00
grib_f_write_on_fail(gid);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-04-10 16:06:19 +00:00
int grib_f_multi_support_on_(void)
2024-01-11 13:40:07 +00:00
{
2015-02-12 18:32:04 +00:00
grib_multi_support_on(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2024-04-10 16:06:19 +00:00
int grib_f_multi_support_off_(void)
2024-01-11 13:40:07 +00:00
{
2015-02-12 18:32:04 +00:00
grib_multi_support_off(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2023-08-25 17:43:28 +00:00
#ifdef FORTRAN_GEOITERATOR_SUPPORT
static int _push_iterator(grib_iterator *i)
{
l_grib_iterator* current = iterator_set;
l_grib_iterator* previous = iterator_set;
l_grib_iterator* the_new = NULL;
int myindex = 1;
if(!iterator_set){
iterator_set = (l_grib_iterator*)malloc(sizeof(l_grib_iterator));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(iterator_set);
2023-08-25 17:43:28 +00:00
iterator_set->id = myindex;
iterator_set->i = i;
iterator_set->next = NULL;
return myindex;
}
while(current){
if(current->id < 0){
current->id = -(current->id);
current->i = i;
return current->id;
}
else{
myindex++;
previous = current;
current = current->next;
}
}
the_new = (l_grib_iterator*)malloc(sizeof(l_grib_iterator));
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(the_new);
2023-08-25 17:43:28 +00:00
the_new->id = myindex;
the_new->i = i;
the_new->next = current;
previous->next = the_new;
return myindex;
}
static int push_iterator(grib_iterator *i)
{
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&iterator_mutex);
ret=_push_iterator(i);
GRIB_MUTEX_UNLOCK(&iterator_mutex);
return ret;
}
static grib_iterator* _get_iterator(int iterator_id)
{
l_grib_iterator* current = iterator_set;
while(current){
if(current->id == iterator_id) return current->i;
current = current->next;
}
return NULL;
}
static grib_iterator* get_iterator(int iterator_id)
{
grib_iterator* i=NULL;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&iterator_mutex);
i=_get_iterator(iterator_id);
GRIB_MUTEX_UNLOCK(&iterator_mutex);
return i;
}
static int _clear_iterator(int iterator_id)
{
l_grib_iterator* current = iterator_set;
while(current){
if(current->id == iterator_id){
current->id = -(current->id);
return grib_iterator_delete(current->i);
}
current = current->next;
}
return GRIB_INVALID_ITERATOR;
}
static int clear_iterator(int iterator_id)
{
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&iterator_mutex);
ret=_clear_iterator(iterator_id);
GRIB_MUTEX_UNLOCK(&iterator_mutex);
return ret;
}
2013-03-25 12:04:10 +00:00
static int _grib_f_iterator_new_(int* gid,int* iterid,int* mode) {
2015-02-12 18:32:04 +00:00
int err=0;
grib_handle* h;
grib_iterator* iter;
h=get_handle(*gid);
if (!h) {
*iterid=-1;
return GRIB_NULL_HANDLE;
}
iter=grib_iterator_new(h,*mode,&err);
if (iter)
*iterid=push_iterator(iter);
else
*iterid=-1;
return err;
2013-03-25 12:04:10 +00:00
}
int grib_f_iterator_new_(int* gid,int* iterid,int* mode) {
2015-02-12 18:32:04 +00:00
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init)
2015-02-12 18:32:04 +00:00
GRIB_MUTEX_LOCK(&iterator_mutex)
ret=_grib_f_iterator_new_(gid,iterid,mode);
GRIB_MUTEX_UNLOCK(&iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2013-03-25 12:04:10 +00:00
int grib_f_iterator_next_(int* iterid,double* lat,double* lon,double* value) {
2015-02-12 18:32:04 +00:00
grib_iterator* iter=get_iterator(*iterid);
if (!iter) return GRIB_INVALID_ITERATOR;
return grib_iterator_next(iter,lat,lon,value);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2013-03-25 12:04:10 +00:00
int grib_f_iterator_delete_(int* iterid) {
2015-02-12 18:32:04 +00:00
return clear_iterator(*iterid);
2013-03-25 12:04:10 +00:00
}
2023-08-25 17:43:28 +00:00
#endif /*FORTRAN_GEOITERATOR_SUPPORT*/
2013-03-25 12:04:10 +00:00
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
static int _grib_f_keys_iterator_new_(int* gid,int* iterid,char* name_space,int len)
{
2015-02-12 18:32:04 +00:00
char buf[1024];
grib_handle* h;
grib_keys_iterator* iter;
h=get_handle(*gid);
if (!h) {
*iterid=-1;
return GRIB_NULL_HANDLE;
}
iter=grib_keys_iterator_new(h,0,cast_char(buf,name_space,len));
if (iter)
*iterid=push_keys_iterator(iter);
else
*iterid=-1;
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_keys_iterator_new_(int* gid,int* iterid,char* name_space,int len)
{
2015-02-12 18:32:04 +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)
ret=_grib_f_keys_iterator_new_(gid,iterid,name_space,len);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_keys_iterator_next_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_keys_iterator_next(iter);
2013-03-25 12:04:10 +00:00
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_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
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-04-10 16:06:19 +00:00
int grib_f_gribex_mode_on_(void)
2024-01-11 13:40:07 +00:00
{
2015-02-12 18:32:04 +00:00
grib_gribex_mode_on(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2024-04-10 16:06:19 +00:00
int grib_f_gribex_mode_off_(void)
2024-01-11 13:40:07 +00:00
{
2015-02-12 18:32:04 +00:00
grib_gribex_mode_off(0);
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_skip_computed_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_set_flags(iter,GRIB_KEYS_ITERATOR_SKIP_COMPUTED);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_skip_coded_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_set_flags(iter,GRIB_KEYS_ITERATOR_SKIP_CODED);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_skip_edition_specific_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_set_flags(iter,GRIB_KEYS_ITERATOR_SKIP_EDITION_SPECIFIC);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_skip_duplicates_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_set_flags(iter,GRIB_KEYS_ITERATOR_SKIP_DUPLICATES);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_skip_read_only_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_set_flags(iter,GRIB_KEYS_ITERATOR_SKIP_READ_ONLY);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_skip_function_(int* iterid)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* iter=get_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_set_flags(iter,GRIB_KEYS_ITERATOR_SKIP_FUNCTION);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_keys_iterator_get_name_(int* iterid,char* name,int len)
{
2024-01-11 12:37:38 +00:00
size_t input_len = len;
size_t lsize = len;
2015-02-12 18:32:04 +00:00
char buf[1024]={0,};
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
grib_keys_iterator* kiter=get_keys_iterator(*iterid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!kiter) return GRIB_INVALID_KEYS_ITERATOR;
2013-03-25 12:04:10 +00:00
2024-01-11 12:37:38 +00:00
fort_char_clean(name, len);
2013-03-25 12:04:10 +00:00
2024-05-08 09:15:10 +00:00
snprintf(buf, sizeof(buf), "%s",grib_keys_iterator_get_name(kiter));
2024-01-11 12:37:38 +00:00
lsize = strlen(buf);
if (input_len < lsize) return GRIB_ARRAY_TOO_SMALL;
2013-03-25 12:04:10 +00:00
2024-01-11 12:37:38 +00:00
memcpy(name, buf, lsize);
2013-03-25 12:04:10 +00:00
2024-01-11 12:37:38 +00:00
czstr_to_fortran(name, len);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_keys_iterator_rewind_(int* kiter)
{
2015-02-12 18:32:04 +00:00
grib_keys_iterator* i=get_keys_iterator(*kiter);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!i) return GRIB_INVALID_KEYS_ITERATOR;
return grib_keys_iterator_rewind(i);
2013-03-25 12:04:10 +00:00
}
2023-08-25 17:43:28 +00:00
/* BUFR keys iterator */
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
static int _codes_f_bufr_keys_iterator_new_(int* gid,int* iterid)
{
grib_handle* h;
bufr_keys_iterator* iter;
h=get_handle(*gid);
if (!h) {
*iterid=-1;
return GRIB_NULL_HANDLE;
}
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(h->product_kind==PRODUCT_BUFR);
iter=codes_bufr_keys_iterator_new(h,0);
if (iter)
*iterid=push_bufr_keys_iterator(iter);
else
*iterid=-1;
return GRIB_SUCCESS;
}
2024-01-11 13:40:07 +00:00
int codes_f_bufr_keys_iterator_new_(int* gid,int* iterid)
{
int ret=0;
GRIB_MUTEX_INIT_ONCE(&once,&init)
GRIB_MUTEX_LOCK(&keys_iterator_mutex)
ret=_codes_f_bufr_keys_iterator_new_(gid,iterid);
GRIB_MUTEX_UNLOCK(&keys_iterator_mutex)
return ret;
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int codes_f_bufr_keys_iterator_next_(int* iterid)
{
bufr_keys_iterator* iter=get_bufr_keys_iterator(*iterid);
if (!iter) return GRIB_INVALID_KEYS_ITERATOR;
return codes_bufr_keys_iterator_next(iter);
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int codes_f_bufr_keys_iterator_get_name_(int* iterid, char* name, int len)
{
2024-01-11 12:37:38 +00:00
size_t input_len = len;
size_t lsize = len;
char buf[1024] = {0,};
2024-01-11 12:37:38 +00:00
bufr_keys_iterator* kiter = get_bufr_keys_iterator(*iterid);
if (!kiter) return GRIB_INVALID_KEYS_ITERATOR;
2024-01-11 12:37:38 +00:00
fort_char_clean(name, len);
2024-05-08 09:15:10 +00:00
snprintf(buf, sizeof(buf), "%s", codes_bufr_keys_iterator_get_name(kiter));
2024-01-11 12:37:38 +00:00
lsize = strlen(buf);
if (input_len < lsize) return GRIB_ARRAY_TOO_SMALL;
2024-01-11 12:37:38 +00:00
memcpy(name, buf, lsize);
2024-01-11 12:37:38 +00:00
czstr_to_fortran(name, len);
return 0;
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int codes_f_bufr_keys_iterator_rewind_(int* kiter)
{
bufr_keys_iterator* i=get_bufr_keys_iterator(*kiter);
if (!i) return GRIB_INVALID_KEYS_ITERATOR;
return codes_bufr_keys_iterator_rewind(i);
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int codes_f_bufr_keys_iterator_delete_(int* iterid)
{
return clear_bufr_keys_iterator(*iterid);
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_new_from_message_(int* gid, void* buffer, size_t* bufsize)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = NULL;
h = grib_handle_new_from_message_copy(0, buffer, *bufsize);
2023-12-20 14:17:30 +00:00
if (h){
2015-02-12 18:32:04 +00:00
push_handle(h,gid);
return GRIB_SUCCESS;
}
*gid = -1;
return GRIB_INTERNAL_ERROR;
2013-03-25 12:04:10 +00:00
}
2023-12-20 14:17:30 +00:00
/* See SUP-3893: Need to provide an 'int' version */
2024-01-11 13:40:07 +00:00
int grib_f_new_from_message_int_(int* gid, int* buffer , size_t* bufsize)
{
2023-12-20 15:30:53 +00:00
/* Call the version with void pointer */
return grib_f_new_from_message_(gid, (void*)buffer, bufsize);
2023-12-20 14:17:30 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-21 14:34:54 +00:00
#if 0
2024-01-11 13:40:07 +00:00
int grib_f_new_from_message_copy_(int* gid, void* buffer, size_t* bufsize)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = NULL;
h = grib_handle_new_from_message_copy(0, buffer, *bufsize);
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
}
*gid = -1;
return GRIB_INTERNAL_ERROR;
2013-03-25 12:04:10 +00:00
}
2024-01-21 14:34:54 +00:00
#endif
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_new_from_samples_(int* gid, char* name, int lname)
{
2015-02-12 18:32:04 +00:00
char fname[1024];
2023-12-20 15:30:53 +00:00
grib_handle *h = grib_handle_new_from_samples(NULL,cast_char(fname,name,lname));
2015-02-12 18:32:04 +00:00
/* grib_context_set_debug(h->context,1);*/
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
}
*gid = -1;
return GRIB_FILE_NOT_FOUND;
2013-03-25 12:04:10 +00:00
}
2016-07-19 10:52:55 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int codes_bufr_f_new_from_samples_(int* gid, char* name, int lname)
{
2016-07-19 10:52:55 +00:00
char fname[1024];
2023-12-20 15:30:53 +00:00
grib_handle* h = codes_bufr_handle_new_from_samples(NULL,cast_char(fname,name,lname));
2016-07-19 10:52:55 +00:00
/* grib_context_set_debug(h->context,1);*/
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
}
*gid = -1;
return GRIB_FILE_NOT_FOUND;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_clone_(int* gidsrc,int* giddest)
{
2015-02-12 18:32:04 +00:00
grib_handle *src = get_handle(*gidsrc);
grib_handle *dest = NULL;
if(src){
dest = grib_handle_clone(src);
if(dest){
push_handle(dest,giddest);
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
}
/*****************************************************************************/
2016-11-10 16:33:38 +00:00
int grib_f_copy_key_(int* gidsrc, char* key, int* giddest, int len)
{
grib_handle *src = get_handle(*gidsrc);
grib_handle *dest = get_handle(*giddest);
2016-11-10 16:33:38 +00:00
if(src && dest) {
char buf[1024]={0,};
2016-11-08 17:35:52 +00:00
char* ckey = (char*)key;
2016-11-10 16:33:38 +00:00
const int type = GRIB_TYPE_UNDEFINED; /* will be computed */
return codes_copy_key(src, dest, cast_char(buf,ckey,len), type);
}
return GRIB_INVALID_GRIB;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-21 14:34:54 +00:00
int grib_f_util_sections_copy_(int* gidfrom, int* gidto, int* what, int* gidout)
2024-01-11 13:40:07 +00:00
{
2024-01-21 14:34:54 +00:00
int err = 0;
grib_handle* hfrom = get_handle(*gidfrom);
grib_handle* hto = get_handle(*gidto);
grib_handle* out = 0;
2015-02-12 18:32:04 +00:00
2024-01-21 14:34:54 +00:00
if (hfrom && hto) out = grib_util_sections_copy(hfrom, hto, *what, &err);
if (out) {
push_handle(out, gidout);
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
}
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_copy_namespace_(int* gidsrc,char* name,int* giddest,int len)
{
2015-02-12 18:32:04 +00:00
char buf[1024]={0,};
grib_handle *src = get_handle(*gidsrc);
grib_handle *dest = get_handle(*giddest);
if(src && dest)
return grib_copy_namespace(dest,cast_char(buf,name,len),src);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int any_f_scan_file_(int* fid, int* n)
{
int err = 0;
off_t offset=0;
void *data = NULL;
size_t olen = 0;
l_message_info* msg=0;
FILE* f = get_file(*fid);
grib_context* c=grib_context_get_default();
/* this needs a callback to a destructor*/
2024-01-06 13:55:05 +00:00
/* grib_oarray_delete_content(c, info_messages); */
2024-10-12 14:12:43 +00:00
grib_oarray_delete(info_messages);
2024-10-12 14:03:24 +00:00
info_messages=grib_oarray_new(1000, 1000);
if (f) {
2017-11-20 10:37:00 +00:00
while (err!=GRIB_END_OF_FILE) {
data = wmo_read_any_from_file_malloc ( f, 0, &olen, &offset, &err );
2017-11-20 10:37:00 +00:00
msg=(l_message_info*)grib_context_malloc_clear(c,sizeof(l_message_info));
msg->offset = offset;
msg->size = olen;
2024-10-12 14:12:43 +00:00
if (err == 0 && data) grib_oarray_push(info_messages, msg);
grib_context_free(c, data);
2017-11-20 10:37:00 +00:00
}
if (err == GRIB_END_OF_FILE) err = 0;
}
*n = info_messages->n;
return err;
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
int any_f_new_from_scanned_file_(int* fid, int* msgid, int* gid)
{
grib_handle *h = NULL;
grib_context* c = grib_context_get_default();
int err = 0;
FILE* f = get_file(*fid);
l_message_info* msg = NULL;
/* fortran convention of 1-based index */
const int n = *msgid - 1;
if (info_messages == NULL) {
return GRIB_INVALID_ARGUMENT;
}
if (*msgid < 1 || *msgid > info_messages->n) {
return GRIB_INVALID_ARGUMENT;
}
msg = (l_message_info*)grib_oarray_get(info_messages, n);
if (msg && f) {
GRIB_MUTEX_INIT_ONCE(&once, &init);
2017-11-20 10:37:00 +00:00
GRIB_MUTEX_LOCK(&read_mutex);
fseeko(f, msg->offset, SEEK_SET);
h = any_new_from_file (c, f, &err);
2017-11-20 10:37:00 +00:00
GRIB_MUTEX_UNLOCK(&read_mutex);
}
if (err) return err;
if (h) {
push_handle(h, gid);
return GRIB_SUCCESS;
} else {
*gid = -1;
return GRIB_END_OF_FILE;
}
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int any_f_load_all_from_file_(int* fid, int* n)
{
int err = 0;
off_t offset=0;
2024-01-06 13:55:05 +00:00
void* data = NULL;
size_t olen = 0;
l_binary_message* msg=0;
FILE* f = get_file(*fid);
2024-01-06 13:55:05 +00:00
grib_context* c = grib_context_get_default();
/* this needs a callback to a destructor*/
2024-01-06 13:55:05 +00:00
/* grib_oarray_delete_content(c, binary_messages); */
2024-10-12 14:12:43 +00:00
grib_oarray_delete(binary_messages);
2024-10-12 14:03:24 +00:00
binary_messages = grib_oarray_new(1000, 1000);
if (f) {
2024-01-06 13:55:05 +00:00
while (err != GRIB_END_OF_FILE) {
data = wmo_read_any_from_file_malloc (f, 0,&olen, &offset, &err);
msg = (l_binary_message*)grib_context_malloc_clear(c,sizeof(l_binary_message));
msg->data = data;
msg->size = olen;
2024-10-12 14:12:43 +00:00
if (err == 0 && data) grib_oarray_push(binary_messages, msg);
}
2024-01-06 13:55:05 +00:00
if (err == GRIB_END_OF_FILE) err = 0;
}
2024-01-06 13:55:05 +00:00
*n = binary_messages->n;
return err;
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
2024-01-06 13:55:05 +00:00
int any_f_new_from_loaded_(int* msgid, int* gid)
2017-10-27 10:53:46 +00:00
{
2024-01-06 13:55:05 +00:00
grib_handle* h = NULL;
grib_context* c = grib_context_get_default();
/* fortran convention of 1 based index*/
2024-01-06 13:55:05 +00:00
const int n = *msgid - 1;
2024-01-06 13:55:05 +00:00
l_binary_message* msg = (l_binary_message*)grib_oarray_get(binary_messages, n);
if (msg && msg->data)
2024-01-06 13:55:05 +00:00
h = grib_handle_new_from_message_copy(c, msg->data, msg->size);
2024-01-06 13:55:05 +00:00
if (h) {
push_handle(h, gid);
2017-10-27 10:53:46 +00:00
return GRIB_SUCCESS;
2024-01-06 13:55:05 +00:00
}
else {
*gid = -1;
2017-10-27 10:53:46 +00:00
return GRIB_END_OF_FILE;
}
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int codes_f_clear_loaded_from_file_(void)
{
/* grib_oarray_delete_content(c,binary_messages); */
2024-10-12 14:12:43 +00:00
grib_oarray_delete(binary_messages);
2017-10-27 10:53:46 +00:00
return GRIB_SUCCESS;
}
2023-12-25 15:25:38 +00:00
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_count_in_file_(int* fid,int* n)
{
2015-02-12 18:32:04 +00:00
int err = 0;
FILE* f = get_file(*fid);
2024-01-06 13:55:05 +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
}
2015-03-12 16:28:31 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int any_f_new_from_file_(int* fid, int* gid)
{
2024-01-06 13:55:05 +00:00
int err = 0;
FILE* f = get_file(*fid);
grib_handle* h = NULL;
2015-03-12 16:28:31 +00:00
2024-01-06 13:55:05 +00:00
if (f) {
h = codes_handle_new_from_file(0, f, PRODUCT_ANY, &err);
if (h) {
push_handle(h, gid);
2015-03-12 16:28:31 +00:00
return GRIB_SUCCESS;
2024-01-06 13:55:05 +00:00
}
else {
*gid = -1;
2015-03-12 16:28:31 +00:00
return GRIB_END_OF_FILE;
}
}
2024-01-06 13:55:05 +00:00
*gid = -1;
2015-03-12 16:28:31 +00:00
return GRIB_INVALID_FILE;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int bufr_f_new_from_file_(int* fid, int* gid)
{
2015-02-12 18:32:04 +00:00
int err = 0;
FILE* f = get_file(*fid);
grib_handle *h = NULL;
if(f){
h = codes_handle_new_from_file(0,f,PRODUCT_BUFR,&err);
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
} else {
*gid=-1;
return GRIB_END_OF_FILE;
}
2014-07-07 20:55:19 +00:00
}
2024-01-11 13:40:07 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2014-07-07 20:55:19 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_new_from_file_(int* fid, int* gid)
{
2015-02-12 18:32:04 +00:00
int err = 0;
FILE* f = get_file(*fid);
grib_handle *h = NULL;
if(f){
h = grib_handle_new_from_file(0,f,&err);
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
} else {
*gid=-1;
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
*gid=-1;
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_headers_only_new_from_file_(int* fid, int* gid)
{
2015-02-12 18:32:04 +00:00
int err = 0;
FILE* f = get_file(*fid);
grib_handle *h = NULL;
2023-12-31 16:09:10 +00:00
const int headers_only = 1;
2015-02-12 18:32:04 +00:00
2023-12-31 16:09:10 +00:00
if (f){
h=grib_new_from_file (0, f, headers_only, &err);
if (h){
2015-02-12 18:32:04 +00:00
push_handle(h,gid);
return GRIB_SUCCESS;
} else {
2023-12-31 16:09:10 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2023-12-31 16:09:10 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_new_from_index_(int* index_id, int* gid)
2024-01-11 13:40:07 +00:00
{
2015-02-12 18:32:04 +00:00
int err = 0;
2024-03-06 13:25:27 +00:00
grib_index* i = get_index(*index_id);
2015-02-12 18:32:04 +00:00
grib_handle *h = NULL;
2013-03-25 12:04:10 +00:00
2023-12-31 16:09:10 +00:00
if (i) {
h = grib_handle_new_from_index(i, &err);
if (h){
2015-02-12 18:32:04 +00:00
push_handle(h,gid);
return GRIB_SUCCESS;
} else {
2023-12-31 16:09:10 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_END_OF_INDEX;
}
2013-03-25 12:04:10 +00:00
}
2023-12-31 16:09:10 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_INDEX;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_index_new_from_file_(char* file, char* keys, int* gid, int lfile, int lkeys)
{
2015-02-12 18:32:04 +00:00
int err = 0;
2023-12-31 16:09:10 +00:00
char fname[1024] = {0,};
char knames[1024] = {0,};
2015-02-12 18:32:04 +00:00
grib_index *i = NULL;
2023-12-31 16:09:10 +00:00
if (*file){
2015-02-12 18:32:04 +00:00
i = grib_index_new_from_file(0,cast_char(fname,file,lfile),
cast_char(knames,keys,lkeys),&err);
2023-12-31 16:09:10 +00:00
if (i) {
2015-02-12 18:32:04 +00:00
push_index(i,gid);
return GRIB_SUCCESS;
} else {
*gid=-1;
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2023-12-31 16:09:10 +00:00
*gid = -1;
2015-02-12 18:32:04 +00:00
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_add_file_(int* index_id, char* file, int lfile)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index *i = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!i) {
return GRIB_INVALID_INDEX;
} else {
return grib_index_add_file(i,cast_char(buf,file,lfile));
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_index_read_(char* file, int* gid, int lfile)
{
2015-02-12 18:32:04 +00:00
int err = 0;
char fname[1024]={0,};
grib_index *i = NULL;
if (*file) {
i = grib_index_read(0,cast_char(fname,file,lfile),&err);
if (i) {
push_index(i,gid);
return GRIB_SUCCESS;
} else {
*gid = -1;
return GRIB_END_OF_FILE;
}
2013-03-25 12:04:10 +00:00
}
2015-02-12 18:32:04 +00:00
*gid=-1;
return GRIB_INVALID_FILE;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_index_write_(int* gid, char* file, int lfile)
{
2015-02-12 18:32:04 +00:00
grib_index *i = get_index(*gid);
char buf[1024];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if (!i) {
return GRIB_INVALID_GRIB;
} else {
return grib_index_write(i,cast_char(buf,file,lfile));
2015-02-12 18:32:04 +00:00
}
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_index_release_(int* hid)
{
2015-02-12 18:32:04 +00:00
return clear_index(*hid);
2013-03-25 12:04:10 +00:00
}
2024-01-11 12:37:38 +00:00
/* int grib_f_multi_handle_release_(int* hid){
2015-02-12 18:32:04 +00:00
return clear_multi_handle(*hid);
2024-01-11 12:37:38 +00:00
} */
2013-03-25 12:04:10 +00:00
2024-01-11 13:40:07 +00:00
int grib_f_release_(int* hid)
{
2015-02-12 18:32:04 +00:00
return clear_handle(*hid);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
static void do_the_dump(grib_handle* h)
{
/* Add some debugging info too */
printf("ecCodes version: "); grib_print_api_version(stdout); printf("\n");
printf("Definitions path: %s\n", grib_definition_path(NULL));
printf("Samples path: %s\n", grib_samples_path(NULL));
if (h->product_kind == PRODUCT_GRIB)
{
const int dump_flags = GRIB_DUMP_FLAG_VALUES
| GRIB_DUMP_FLAG_READ_ONLY
| GRIB_DUMP_FLAG_ALIASES
| GRIB_DUMP_FLAG_TYPE;
2023-08-22 13:39:28 +00:00
grib_dump_content(h,stdout, "debug", dump_flags, NULL);
}
else
{
2023-08-22 13:39:28 +00:00
const int dump_flags = GRIB_DUMP_FLAG_CODED | GRIB_DUMP_FLAG_OCTET | GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY;
grib_dump_content(h,stdout, "wmo", dump_flags, NULL);
}
}
2024-01-11 13:40:07 +00:00
int grib_f_dump_(int* gid)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
if(!h)
return GRIB_INVALID_GRIB;
else
do_the_dump(h);
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
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2023-06-15 12:22:25 +00:00
#ifdef USE_GRIB_PRINT
2024-01-11 13:40:07 +00:00
int grib_f_print_(int* gid, char* key, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
grib_dumper* d = NULL;
char buf[1024];
if(!h){
return GRIB_INVALID_GRIB;
}else{
d = grib_dumper_factory("file",h,stdout,0,0);
err = grib_print(h, cast_char(buf,key,len), d);
grib_dumper_delete(d);
return err;
}
2013-03-25 12:04:10 +00:00
}
2023-06-15 12:22:25 +00:00
#endif
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_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);
2021-03-05 12:08:34 +00:00
const size_t erlen = strlen(err_msg);
2015-02-12 18:32:04 +00:00
if( len < erlen) return GRIB_ARRAY_TOO_SMALL;
strncpy(buf, err_msg, (size_t)erlen); /* ECC-1488 */
2015-02-12 18:32:04 +00:00
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_api_version_(int* apiVersion,int len)
{
*apiVersion = grib_get_api_version();
return GRIB_SUCCESS;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2023-12-31 16:09:10 +00:00
int grib_f_get_size_int_(int* gid, char* key, int* val, int len)
{
grib_handle* h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t tsize = 0;
if (!h) return GRIB_INVALID_GRIB;
int err = grib_get_size(h, cast_char(buf, key, len), &tsize);
*val = tsize;
return err;
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_get_size_long_(int* gid, char* key, long* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t tsize = 0;
if (!h) return GRIB_INVALID_GRIB;
int err = grib_get_size(h, cast_char(buf,key,len), &tsize);
*val = tsize;
return err;
2013-03-25 12:04:10 +00:00
}
2023-12-31 16:09:10 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_get_size_int_(int* index_id, char* key, int* val, int len)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t tsize = 0;
if (!h) return GRIB_INVALID_GRIB;
int err = grib_index_get_size(h, cast_char(buf,key,len), &tsize);
*val = tsize;
return err;
2013-03-25 12:04:10 +00:00
}
2024-03-06 13:25:27 +00:00
int grib_f_index_get_size_long_(int* index_id, char* key, long* val, int len)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t tsize = 0;
if (!h) return GRIB_INVALID_GRIB;
int err = grib_index_get_size(h, cast_char(buf,key,len), &tsize);
*val = tsize;
return err;
2013-03-25 12:04:10 +00:00
}
2023-12-31 16:09:10 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_int_(int* gid, char* key, int* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
long long_val;
char buf[1024];
2013-03-25 12:04:10 +00:00
if (!h) return GRIB_INVALID_GRIB;
int err = grib_get_long(h, cast_char(buf,key,len),&long_val);
2015-02-12 18:32:04 +00:00
*val = long_val;
return err;
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_get_long_(int* gid, char* key, long* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
char buf[1024];
2013-03-25 12:04:10 +00:00
if (!h) return GRIB_INVALID_GRIB;
int err = grib_get_long(h, cast_char(buf,key,len),val);
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_native_type_(int* gid, char* key, int* val, int len)
{
grib_handle *h = get_handle(*gid);
int type_val = 0;
char buf[1024];
2023-12-31 16:09:10 +00:00
if (!h) return GRIB_INVALID_GRIB;
int err = grib_get_native_type(h, cast_char(buf,key,len), &type_val);
*val = type_val;
return err;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_int_array_(int* gid, char* key, int *val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
long* long_val = NULL;
int err = GRIB_SUCCESS;
char buf[1024];
size_t lsize = *size;
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(sizeof(long) == sizeof(int)){
long_val = (long*)val;
err = grib_get_long_array(h, cast_char(buf,key,len), long_val, &lsize);
*size = lsize;
return err;
}
if(*size)
long_val = (long*)grib_context_malloc(h->context,(*size)*(sizeof(long)));
else
long_val = (long*)grib_context_malloc(h->context,(sizeof(long)));
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!long_val) return GRIB_OUT_OF_MEMORY;
err = grib_get_long_array(h, cast_char(buf,key,len), long_val, &lsize);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
for(*size=0;*size<lsize;(*size)++)
val[*size] = long_val[*size];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
grib_context_free(h->context,long_val);
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_long_array_(int* gid, char* key, long *val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t lsize = *size;
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
int err = grib_get_long_array(h, cast_char(buf,key,len), val, &lsize);
2015-02-12 18:32:04 +00:00
*size=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
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_byte_array_(int* gid, char* key, unsigned char *val, int* size, int len, int lenv)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t lsize = *size;
2015-02-12 18:32:04 +00:00
if(!h) return GRIB_INVALID_GRIB;
int err = grib_get_bytes(h, cast_char(buf,key,len), val, &lsize);
2015-02-12 18:32:04 +00:00
*size = (int) lsize;
2015-02-12 18:32:04 +00:00
return err;
}
2013-03-25 12:04:10 +00:00
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_get_string_(int* index_id, char* key, char* val, int *eachsize,int* size, int len)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t lsize = *size;
char* p = val;
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
char** bufval=(char**)grib_context_malloc_clear(h->context,sizeof(char*)* *size);
2013-03-25 12:04:10 +00:00
int err = grib_index_get_string(h, cast_char(buf,key,len), bufval, &lsize);
2013-03-25 12:04:10 +00:00
*size = lsize;
2015-02-12 18:32:04 +00:00
if (err) return err;
for (size_t i=0;i<lsize;i++) {
2015-02-12 18:32:04 +00:00
int l=strlen(bufval[i]);
if (*eachsize < l ) {
2023-12-31 16:09:10 +00:00
fprintf(stderr, "eachsize=%d strlen(bufval[i])=%zu\n",
*eachsize, strlen(bufval[i]));
2015-02-12 18:32:04 +00:00
grib_context_free(h->context,bufval);
return GRIB_ARRAY_TOO_SMALL;
}
memcpy(p,bufval[i],l);
p+=l;
for (int j=0;j<*eachsize-l;j++) *(p++)=' ';
2015-02-12 18:32:04 +00:00
}
for (size_t i=0;i<lsize;i++) {
grib_context_free(h->context, bufval[i]);
}
2015-02-12 18:32:04 +00:00
grib_context_free(h->context,bufval);
2013-03-25 12:04:10 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_get_long_(int* index_id, char* key, long *val, int* size, int len)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index* h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2024-03-06 13:25:27 +00:00
if (!h) return GRIB_INVALID_GRIB;
int err = grib_index_get_long(h, cast_char(buf,key,len), val, &lsize);
2013-03-25 12:04:10 +00:00
*size = lsize;
2024-03-06 13:25:27 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_get_int_(int* index_id, char* key, int *val, int* size, int len)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
2024-01-11 13:40:07 +00:00
size_t lsize = *size, i = 0;
2015-02-12 18:32:04 +00:00
long* lval=0;
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
lval=(long*)grib_context_malloc(h->context,sizeof(long)* *size);
if (!lval) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
int err = grib_index_get_long(h, cast_char(buf,key,len), lval, &lsize);
2015-02-12 18:32:04 +00:00
for (i=0;i<lsize;i++) val[i]=lval[i];
*size = lsize;
grib_context_free(h->context, lval);
2015-02-12 18:32:04 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_get_real8_(int* index_id, char* key, double *val, int* size, int len)
2024-01-11 13:40:07 +00:00
{
2024-03-06 13:25:27 +00:00
grib_index* h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2024-03-06 13:25:27 +00:00
if (!h) return GRIB_INVALID_GRIB;
int err = grib_index_get_double(h, cast_char(buf,key,len), val, &lsize);
2015-02-12 18:32:04 +00:00
*size = lsize;
2024-03-06 13:25:27 +00:00
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_int_array_(int* gid, char* key, int* val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
long* long_val = NULL;
size_t lsize = *size;
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(sizeof(long) == sizeof(int)){
long_val = (long*)val;
return grib_set_long_array(h, cast_char(buf,key,len), long_val, lsize);
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(lsize)
long_val = (long*)grib_context_malloc(h->context,(lsize)*(sizeof(long)));
else
long_val = (long*)grib_context_malloc(h->context,(sizeof(long)));
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!long_val) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
for(lsize=0;lsize<(*size);lsize++)
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, cast_char(buf,key,len), long_val, lsize);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
grib_context_free(h->context,long_val);
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_long_array_(int* gid, char* key, long* val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
size_t lsize = *size;
if(!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2018-01-03 16:41:06 +00:00
return grib_set_long_array(h, cast_char(buf,key,len), val, lsize);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_byte_array_(int* gid, char* key, unsigned char* val, int* size, int len, int lenv)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
2015-02-12 18:32:04 +00:00
char buf[1024];
size_t lsize = *size;
if (!h) return GRIB_INVALID_GRIB;
2015-02-12 18:32:04 +00:00
int err = grib_set_bytes(h, cast_char(buf,key,len), val, &lsize);
2015-02-12 18:32:04 +00:00
*size = (int) lsize;
return err;
}
2013-03-25 12:04:10 +00:00
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_int_(int* gid, char* key, int* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
long long_val = *val;
if(!h) return GRIB_INVALID_GRIB;
return grib_set_long(h, cast_char(buf,key,len), long_val);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_set_long_(int* gid, char* key, long* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
if(!h) return GRIB_INVALID_GRIB;
return grib_set_long(h, cast_char(buf,key,len), *val);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_missing_(int* gid, char* key,int len)
{
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
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, cast_char(buf,key,len));
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_is_missing_(int* gid, char* key,int* isMissing,int len)
{
2015-02-12 18:32:04 +00:00
int err=0;
grib_handle *h = get_handle(*gid);
char buf[1024];
2024-12-05 19:50:54 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*isMissing=grib_is_missing(h, cast_char(buf,key,len),&err);
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_is_defined_(int* gid, char* key,int* isDefined,int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
if(!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
*isDefined=grib_is_defined(h, cast_char(buf,key,len));
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2024-12-05 19:50:54 +00:00
/*****************************************************************************/
int grib_f_key_is_computed_(int* gid, char* key,int* isComputed,int len)
{
int err = 0;
grib_handle *h = get_handle(*gid);
char buf[1024];
if (!h) return GRIB_INVALID_GRIB;
*isComputed = codes_key_is_computed(h, cast_char(buf,key,len), &err);
return err;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_real4_(int* gid, char* key, float* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
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, cast_char(buf,key,len), val8);
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_get_real4_element_(int* gid, char* key, int* index,float* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
double val8 = 0;
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
err = grib_get_double_element(h, cast_char(buf,key,len), *index,&val8);
*val = val8;
return err;
2013-03-25 12:04:10 +00:00
}
2024-01-11 13:40:07 +00:00
int grib_f_get_real4_elements_(int* gid, char* key,int* index, float *val,int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
size_t lsize = *size;
2024-01-11 12:37:38 +00:00
size_t i = 0;
2015-02-12 18:32:04 +00:00
double* val8 = NULL;
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(*size)
val8 = (double*)grib_context_malloc(h->context,(*size)*(sizeof(double)));
else
val8 = (double*)grib_context_malloc(h->context,sizeof(double));
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!val8) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_get_double_elements(h, cast_char(buf,key,len), index,(long)lsize,val8);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
for(i=0;i<lsize;(i)++)
val[i] = val8[i];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
grib_context_free(h->context,val8);
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
}
2024-01-11 13:40:07 +00:00
int grib_f_get_real4_(int* gid, char* key, float* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
double val8 = 0;
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
err = grib_get_double(h, cast_char(buf,key,len), &val8);
*val = val8;
return err;
2013-03-25 12:04:10 +00:00
}
2023-12-30 14:17:26 +00:00
/*****************************************************************************/
2023-06-22 21:46:16 +00:00
int grib_f_get_real4_array_(int* gid, char* key, float* val, int* size, int len)
{
2023-05-04 14:59:25 +00:00
/* See ECC-1579:
2023-06-22 21:46:16 +00:00
* Ideally we should ALWAYS be calling:
2023-05-04 14:59:25 +00:00
* err = grib_get_float_array(h, cast_char(buf,key,len), val, &lsize);
2023-06-22 21:46:16 +00:00
*/
2023-06-22 21:46:16 +00:00
grib_handle* h = get_handle(*gid);
size_t lsize = *size;
2015-02-12 18:32:04 +00:00
char buf[1024];
2023-06-22 21:46:16 +00:00
int err = GRIB_SUCCESS;
const int single_precision_mode = (h->context->single_precision != 0);
2013-03-25 12:04:10 +00:00
2023-06-22 21:46:16 +00:00
if (!h) return GRIB_INVALID_GRIB;
2023-04-26 16:52:02 +00:00
2023-06-22 21:46:16 +00:00
if (single_precision_mode) {
err = grib_get_float_array(h, cast_char(buf, key, len), val, &lsize);
}
else {
double* val8 = NULL;
size_t i;
2023-06-22 21:46:16 +00:00
if (*size)
val8 = (double*)grib_context_malloc(h->context, (*size) * (sizeof(double)));
else
val8 = (double*)grib_context_malloc(h->context, sizeof(double));
2023-06-22 21:46:16 +00:00
if (!val8) return GRIB_OUT_OF_MEMORY;
2023-06-22 21:46:16 +00:00
err = grib_get_double_array(h, cast_char(buf, key, len), val8, &lsize);
if (err) {
grib_context_free(h->context, val8);
return err;
}
2023-06-22 21:46:16 +00:00
for (i = 0; i < lsize; i++)
val[i] = val8[i];
2023-06-22 21:46:16 +00:00
grib_context_free(h->context, val8);
}
return err;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2023-03-15 12:52:06 +00:00
int grib_f_set_force_real4_array_(int* gid, char* key, float* val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
size_t lsize = *size;
double* val8 = NULL;
2023-12-30 14:17:26 +00:00
size_t numElements = lsize;
2013-04-16 15:22:10 +00:00
2015-02-12 18:32:04 +00:00
if(!h) return GRIB_INVALID_GRIB;
2013-04-16 15:22:10 +00:00
2015-02-12 18:32:04 +00:00
if(*size)
val8 = (double*)grib_context_malloc(h->context,lsize*(sizeof(double)));
else
val8 = (double*)grib_context_malloc(h->context,sizeof(double));
2013-04-16 15:22:10 +00:00
2015-02-12 18:32:04 +00:00
if(!val8) return GRIB_OUT_OF_MEMORY;
2013-04-16 15:22:10 +00:00
2023-12-30 14:17:26 +00:00
for (lsize = 0; lsize < numElements; lsize++)
2015-02-12 18:32:04 +00:00
val8[lsize] = val[lsize];
2013-04-16 15:22:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_set_force_double_array(h, cast_char(buf,key,len), val8, lsize);
grib_context_free(h->context,val8);
return err;
2013-04-16 15:22:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2023-07-13 15:10:47 +00:00
int grib_f_set_real4_array_(int* gid, char* key, float* val, int* size, int len)
{
2023-07-13 15:10:47 +00:00
grib_handle* h = get_handle(*gid);
int err = GRIB_SUCCESS;
2015-02-12 18:32:04 +00:00
char buf[1024];
2023-07-13 15:10:47 +00:00
const size_t lsize = *size;
const int single_precision_mode = (h->context->single_precision != 0);
2013-03-25 12:04:10 +00:00
2023-07-13 15:10:47 +00:00
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2023-07-13 15:10:47 +00:00
if (single_precision_mode) {
err = grib_set_float_array(h, cast_char(buf, key, len), val, lsize);
}
else {
double* val8 = NULL;
size_t i = 0;
if (*size)
val8 = (double*)grib_context_malloc(h->context, lsize * (sizeof(double)));
else
val8 = (double*)grib_context_malloc(h->context, sizeof(double));
2013-03-25 12:04:10 +00:00
2023-07-13 15:10:47 +00:00
if (!val8) return GRIB_OUT_OF_MEMORY;
2013-03-25 12:04:10 +00:00
2023-07-13 15:10:47 +00:00
for (i = 0; i < lsize; i++)
val8[i] = val[i];
err = grib_set_double_array(h, cast_char(buf, key, len), val8, lsize);
grib_context_free(h->context, val8);
}
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
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_select_real8_(int* index_id, char* key, double* val, int len)
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2015-02-12 18:32:04 +00:00
char buf[1024];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!h) return GRIB_INVALID_GRIB;
return grib_index_select_double(h, cast_char(buf,key,len), *val);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_select_string_(int* index_id, char* key, char* val, int len, int vallen)
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
char buf[1024];
char bufval[1024];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!h) return GRIB_INVALID_GRIB;
/* ECC-1316 */
cast_char_no_cut(bufval,val,vallen);
2022-06-10 19:51:39 +00:00
string_rtrim( bufval );
return grib_index_select_string(h, cast_char(buf,key,len), bufval);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_select_int_(int* index_id, char* key, int* val, int len)
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2024-01-11 13:40:07 +00:00
long lval = *val;
char buf[1024] = {0,};
2013-03-25 12:04:10 +00:00
2024-01-11 13:40:07 +00:00
if (!h) return GRIB_INVALID_GRIB;
return grib_index_select_long(h, cast_char(buf, key, len), lval);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-03-06 13:25:27 +00:00
int grib_f_index_select_long_(int* index_id, char* key, long* val, int len)
{
2024-03-06 13:25:27 +00:00
grib_index *h = get_index(*index_id);
2024-01-11 13:40:07 +00:00
char buf[1024] = {0,};
2013-03-25 12:04:10 +00:00
2024-01-11 13:40:07 +00:00
if (!h) return GRIB_INVALID_GRIB;
return grib_index_select_long(h, cast_char(buf, key, len), *val);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
int grib_f_set_real8_(int* gid, char* key, double* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!h) return GRIB_INVALID_GRIB;
return grib_set_double(h, cast_char(buf,key,len), *val);
2013-03-25 12:04:10 +00:00
}
int grib_f_get_real8_(int* gid, char* key, double* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
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_double(h, cast_char(buf,key,len), val);
2013-03-25 12:04:10 +00:00
}
2023-12-30 14:17:26 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_real8_element_(int* gid, char* key,int* index, double* val, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
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_element(h, cast_char(buf,key,len), *index,val);
2013-03-25 12:04:10 +00:00
}
2023-12-25 15:25:38 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_real8_elements_(int* gid, char* key,int* index, double* val, int *size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
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_double_elements(h, cast_char(buf,key,len), index,*size,val);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2013-03-25 12:04:10 +00:00
int grib_f_find_nearest_four_single_(int* gid,int* is_lsm,
2015-02-12 18:32:04 +00:00
double* inlat,double* inlon,
double* outlats,double* outlons,
double* values,double* distances,
int* indexes)
{
2013-03-25 12:04:10 +00:00
grib_nearest* nearest=NULL;
int err=0, result=0;
2013-03-25 12:04:10 +00:00
unsigned long flags=0;
size_t len=4;
grib_handle *h = get_handle(*gid);
if(!h) return GRIB_INVALID_GRIB;
nearest=grib_nearest_new(h,&err);
if (err!=GRIB_SUCCESS) return err;
result = grib_nearest_find(nearest,h,*inlat,*inlon,
2015-02-12 18:32:04 +00:00
flags,outlats,outlons,values,distances,indexes,&len);
grib_nearest_delete(nearest);
return result;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2013-03-25 12:04:10 +00:00
int grib_f_find_nearest_single_(int* gid,int* is_lsm,
2015-02-12 18:32:04 +00:00
double* inlats,double* inlons,
double* outlats,double* outlons,
double* values,double* distances,
2024-01-11 13:40:07 +00:00
int* indexes)
{
2013-03-25 12:04:10 +00:00
grib_handle *h = get_handle(*gid);
if(!h) return GRIB_INVALID_GRIB;
return grib_nearest_find_multiple(h,*is_lsm,
2015-02-12 18:32:04 +00:00
inlats,inlons,1,outlats,outlons,
values,distances,indexes);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2013-03-25 12:04:10 +00:00
int grib_f_find_nearest_multiple_(int* gid,int* is_lsm,
2015-02-12 18:32:04 +00:00
double* inlats,double* inlons,
double* outlats,double* outlons,
double* values,double* distances,
2024-01-11 13:40:07 +00:00
int* indexes, int* npoints)
{
2013-03-25 12:04:10 +00:00
grib_handle *h = get_handle(*gid);
if(!h) return GRIB_INVALID_GRIB;
return grib_nearest_find_multiple(h,*is_lsm,
2015-02-12 18:32:04 +00:00
inlats,inlons,*npoints,outlats,outlons,
values,distances,indexes);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_real8_array_(int* gid, char* key, double*val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
size_t lsize = *size;
if(!h){
return GRIB_INVALID_GRIB;
}else{
err = grib_get_double_array(h, cast_char(buf,key,len), val, &lsize);
*size = lsize;
return err;
}
2013-03-25 12:04:10 +00:00
}
2013-04-16 15:22:10 +00:00
2023-12-30 14:17:26 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_force_real8_array_(int* gid, char* key, double*val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
size_t lsize = *size;
2013-04-16 15:22:10 +00:00
2023-12-20 15:30:53 +00:00
if(!h) return GRIB_INVALID_GRIB;
2013-04-16 15:22:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_set_force_double_array(h, cast_char(buf,key,len), val, lsize);
2013-04-16 15:22:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_real8_array_(int* gid, char* key, double*val, int* size, int len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char buf[1024];
size_t lsize = *size;
2013-03-25 12:04:10 +00:00
2023-07-13 15:10:47 +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, cast_char(buf,key,len), val, lsize);
2013-03-25 12:04:10 +00:00
}
/*****************************************************************************/
2017-06-21 18:21:58 +00:00
int grib_f_get_string_array_(int* gid, char* key, char* val,int* nvals,int* slen, int len)
{
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t i;
char buf[1024];
size_t lsize = *nvals;
char** cval=0;
char* p=val;
if(!h) return GRIB_INVALID_GRIB;
cval=(char**)grib_context_malloc_clear(h->context,sizeof(char*)*lsize);
err = grib_get_string_array(h, cast_char(buf,key,len), cval, &lsize);
if (err) return err;
if (strlen(cval[0])>*slen) err=GRIB_ARRAY_TOO_SMALL;
for (i=0;i<lsize;i++) {
2016-06-16 13:55:57 +00:00
strcpy(p,cval[i]);
czstr_to_fortran(p,*slen);
grib_context_free(h->context,cval[i]);
p+= *slen;
}
grib_context_free(h->context,cval);
return err;
}
2016-10-17 15:44:27 +00:00
/*****************************************************************************/
int codes_f_bufr_copy_data_(int* gid1,int* gid2)
{
grib_handle *hin = get_handle(*gid1);
grib_handle *hout = get_handle(*gid2);
int err = GRIB_SUCCESS;
if(!hin || !hout ) return GRIB_INVALID_GRIB;
err=codes_bufr_copy_data(hin,hout);
2016-10-17 15:44:27 +00:00
if (err) return err;
return err;
}
/*****************************************************************************/
2017-06-21 18:21:58 +00:00
int grib_f_set_string_array_(int* gid, char* key, char* val,int* nvals,int* slen, int len)
{
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
size_t i;
char buf[1024];
size_t lsize = *nvals;
char** cval=0;
char* p=val;
2017-10-27 10:53:46 +00:00
grib_context* c;
if(!h) return GRIB_INVALID_GRIB;
2017-10-27 10:53:46 +00:00
c=h->context;
cval=(char**)grib_context_malloc_clear(h->context,sizeof(char*)*lsize);
for (i=0;i<lsize;i++) {
2017-06-21 18:21:58 +00:00
cval[i]=(char*)grib_context_malloc_clear(c,sizeof(char)* (*slen+1));
cast_char_no_cut(cval[i],p,*slen);
2022-06-10 19:51:39 +00:00
string_rtrim( cval[i] ); /* trim spaces at end of string */
p+= *slen;
}
2016-07-09 11:37:29 +00:00
err = grib_set_string_array(h, cast_char(buf,key,len), (const char **)cval, lsize);
if (err) return err;
for (i=0;i<lsize;i++) {
grib_context_free(c,cval[i]);
}
grib_context_free(c,cval);
return err;
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_string_(int* gid, char* key, char* val,int len, int len2)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
int err = GRIB_SUCCESS;
char buf[1024];
size_t lsize = len2;
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
fort_char_clean(val,len2);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
err = grib_get_string(h, cast_char(buf,key,len), val, &lsize);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
czstr_to_fortran(val,len2);
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
}
static int is_all_spaces(const char *s)
{
while (*s != '\0') {
if (!isspace(*s)) return 0;
s++;
}
return 1;
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_string_(int* gid, char* key, char* val, int len, int len2)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
char* val_str = NULL;
2013-03-25 12:04:10 +00:00
char buf[1024]={0,};
char buf2[1024]={0,};
2015-02-12 18:32:04 +00:00
size_t lsize = len2;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!h) return GRIB_INVALID_GRIB;
/* For BUFR, the value may contain spaces e.g. stationOrSiteName='CAMPO NOVO' */
/* So do not use cast_char. cast_char_no_cut does not stop at first space */
val_str = cast_char_no_cut(buf2,val,len2);
if (val_str && !is_all_spaces(val_str)) {
2022-06-10 19:51:39 +00:00
string_rtrim( val_str ); /* trim spaces at end of string */
}
2013-03-25 12:04:10 +00:00
return grib_set_string(h, cast_char(buf,key,len), val_str, &lsize);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_data_real4_(int* gid,float* lats, float* lons,float* values,size_t* size)
{
2015-02-12 18:32:04 +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
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
val8 = (double*)grib_context_malloc(h->context,(*size)*(sizeof(double)));
if(!val8) return GRIB_OUT_OF_MEMORY;
lon8 = (double*)grib_context_malloc(h->context,(*size)*(sizeof(double)));
if(!lon8) return GRIB_OUT_OF_MEMORY;
lat8 = (double*)grib_context_malloc(h->context,(*size)*(sizeof(double)));
if(!lat8) return GRIB_OUT_OF_MEMORY;
err=grib_get_data(h,lat8,lon8,val8);
2015-02-12 18:32:04 +00:00
for(i=0;i<*size;i++) {
values[i] = val8[i];
lats[i] = lat8[i];
lons[i] = lon8[i];
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +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
}
2024-01-11 13:40:07 +00:00
int grib_f_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
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_get_message_size_(int* gid, size_t *len)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
if(!h) return GRIB_INVALID_GRIB;
*len = h->buffer->ulength;
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_copy_message_(int* gid, void* mess, size_t* len)
{
2015-02-12 18:32:04 +00:00
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
if(*len < h->buffer->ulength) {
grib_context_log(h->context,GRIB_LOG_ERROR,
"grib_copy_message: buffer=%zu message size=%zu", *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
2015-02-12 18:32:04 +00:00
memcpy(mess,h->buffer->data,h->buffer->ulength);
*len=h->buffer->ulength;
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
void grib_f_check_(int* err,char* call,char* str,int lencall,int lenstr)
{
2015-02-12 18:32:04 +00:00
char bufstr[1024]={0,};
char bufcall[1024]={0,};
grib_context* c=grib_context_get_default();
if ( *err == GRIB_SUCCESS || *err == GRIB_END_OF_FILE ) return;
cast_char(bufcall,call,lencall);
/* ECC-1392 */
cast_char_no_cut(bufstr,str,lenstr);
2015-02-12 18:32:04 +00:00
grib_context_log(c,GRIB_LOG_ERROR,"%s: %s %s",
bufcall,bufstr,grib_get_error_message(*err));
exit(*err);
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_write_(int* gid, int* fid)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*gid);
FILE* f = get_file(*fid);
const void* mess = NULL;
size_t mess_len = 0;
if(!f) return GRIB_INVALID_FILE;
if (!h) return GRIB_INVALID_GRIB;
grib_get_message(h,&mess,&mess_len);
if(fwrite(mess,1, mess_len,f) != mess_len) {
2023-12-30 14:17:26 +00:00
perror("write");
2015-02-12 18:32:04 +00:00
return GRIB_IO_PROBLEM;
}
return GRIB_SUCCESS;
2013-03-25 12:04:10 +00:00
}
2015-01-29 11:53:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_multi_write_(int* gid, int* fid)
{
2015-02-12 18:32:04 +00:00
grib_multi_handle *h = get_multi_handle(*gid);
FILE* f = get_file(*fid);
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
if(!f) return GRIB_INVALID_FILE;
if (!h) return GRIB_INVALID_GRIB;
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_multi_handle_write(h,f);
2013-03-25 12:04:10 +00:00
}
2023-12-20 15:30:53 +00:00
2024-01-11 13:40:07 +00:00
int grib_f_multi_append_(int* ingid, int* sec,int* mgid)
{
2015-02-12 18:32:04 +00:00
grib_handle *h = get_handle(*ingid);
grib_multi_handle *mh = get_multi_handle(*mgid);
if (!h) return GRIB_INVALID_GRIB;
if (!mh) {
mh=grib_multi_handle_new(h->context);
push_multi_handle(mh,mgid);
}
2013-03-25 12:04:10 +00:00
2015-02-12 18:32:04 +00:00
return grib_multi_handle_append(h,*sec,mh);
2013-03-25 12:04:10 +00:00
}
2019-08-09 12:06:58 +00:00
/*****************************************************************************/
2024-04-10 16:06:19 +00:00
int codes_f_bufr_multi_element_constant_arrays_on_(void)
2024-01-11 13:40:07 +00:00
{
2019-08-09 12:06:58 +00:00
codes_bufr_multi_element_constant_arrays_on(NULL);
return GRIB_SUCCESS;
}
2024-04-10 16:06:19 +00:00
int codes_f_bufr_multi_element_constant_arrays_off_(void)
2024-01-11 13:40:07 +00:00
{
2019-08-09 12:06:58 +00:00
codes_bufr_multi_element_constant_arrays_off(NULL);
return GRIB_SUCCESS;
}
/*****************************************************************************/
2024-08-12 10:54:59 +00:00
void grib_f_set_debug_(int* dmode)
{
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(dmode);
grib_context* c = grib_context_get_default();
2024-08-12 10:54:59 +00:00
grib_context_set_debug(c, *dmode);
}
2024-09-19 12:10:38 +00:00
/*****************************************************************************/
void grib_f_set_data_quality_checks_(int* val)
{
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(val);
2024-09-19 12:10:38 +00:00
grib_context* c = grib_context_get_default();
grib_context_set_data_quality_checks(c, *val);
}
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_definitions_path_(char* path, int len)
{
grib_context* c = grib_context_get_default();
char buf[1024];
grib_context_set_definitions_path(c, cast_char(buf,path,len));
return GRIB_SUCCESS;
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_set_samples_path_(char* path, int len)
{
grib_context* c = grib_context_get_default();
char buf[1024];
grib_context_set_samples_path(c, cast_char(buf,path,len));
return GRIB_SUCCESS;
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_julian_to_datetime_(double* jd,long* year,long* month,long* day,long *hour,long *minute,long *second)
{
return grib_julian_to_datetime(*jd,year,month,day,hour,minute,second);
}
2023-12-20 15:30:53 +00:00
/*****************************************************************************/
2024-01-11 13:40:07 +00:00
int grib_f_datetime_to_julian_(long* year,long* month,long* day, long* hour,long* minute,long* second,double* jd)
{
return grib_datetime_to_julian(*year,*month,*day,*hour,*minute,*second,jd);
}