eccodes/src/grib_templates.c

199 lines
4.9 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"
#if HAVE_UNISTD_H
2020-01-22 13:10:59 +00:00
#include <unistd.h>
2013-03-25 12:04:10 +00:00
#endif
#if 0
/* This is a mechanism where we generate C code in grib_templates.h
* from our GRIB sample files and then include the header so one
* can instantiate samples without any disk access.
* This is now superseded by MEMFS
*/
2013-03-25 12:04:10 +00:00
typedef struct grib_templates {
2015-12-22 17:55:45 +00:00
const char* name;
const unsigned char* data;
size_t size;
2013-03-25 12:04:10 +00:00
} grib_templates;
#include "grib_templates.h"
2020-01-22 13:10:59 +00:00
#define NUMBER(x) (sizeof(x) / sizeof(x[0]))
2013-03-25 12:04:10 +00:00
grib_handle* grib_internal_sample(grib_context* c,const char* name)
2013-03-25 12:04:10 +00:00
{
size_t i;
const size_t num_samples = NUMBER(templates);
Assert(0);
for(i = 0; i < num_samples; i++)
2015-12-22 17:55:45 +00:00
if(strcmp(name,templates[i].name) == 0)
return grib_handle_new_from_message_copy(c,templates[i].data,templates[i].size);
return NULL;
2013-03-25 12:04:10 +00:00
}
#endif
/* Windows always has a colon in pathnames e.g. C:\temp\file. It uses semi-colons as delimiter */
#ifdef ECCODES_ON_WINDOWS
#define ECC_PATH_DELIMITER_CHAR ';'
#else
#define ECC_PATH_DELIMITER_CHAR ':'
#endif
2020-01-22 13:10:59 +00:00
static grib_handle* try_template(grib_context* c, const char* dir, const char* name)
2013-03-25 12:04:10 +00:00
{
2015-12-22 17:55:45 +00:00
char path[1024];
2020-01-22 13:10:59 +00:00
grib_handle* g = NULL;
int err = 0;
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
sprintf(path, "%s/%s.tmpl", dir, name);
2013-03-25 12:04:10 +00:00
2020-03-13 15:16:23 +00:00
if (c->debug) {
2021-02-19 22:18:43 +00:00
fprintf(stderr, "ECCODES DEBUG try_template path='%s'\n", path);
2015-12-22 17:55:45 +00:00
}
2014-04-24 16:48:24 +00:00
2020-01-22 13:10:59 +00:00
if (codes_access(path, F_OK) == 0) {
FILE* f = codes_fopen(path, "r");
if (!f) {
grib_context_log(c, GRIB_LOG_PERROR, "cannot open %s", path);
2015-12-22 17:55:45 +00:00
return NULL;
}
2020-01-22 13:10:59 +00:00
g = grib_handle_new_from_file(c, f, &err);
2015-12-22 17:55:45 +00:00
if (!g) {
2020-01-22 13:10:59 +00:00
grib_context_log(c, GRIB_LOG_ERROR, "cannot create GRIB handle from %s", path);
2015-12-22 17:55:45 +00:00
}
fclose(f);
2014-04-24 16:48:24 +00:00
}
2013-03-25 12:04:10 +00:00
2015-12-22 17:55:45 +00:00
return g;
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
static grib_handle* try_bufr_template(grib_context* c, const char* dir, const char* name)
2016-07-19 10:52:55 +00:00
{
char path[1024];
2020-01-22 13:10:59 +00:00
grib_handle* g = NULL;
int err = 0;
2016-07-19 10:52:55 +00:00
2020-01-22 13:10:59 +00:00
sprintf(path, "%s/%s.tmpl", dir, name);
2016-07-19 10:52:55 +00:00
2020-03-13 15:16:23 +00:00
if (c->debug) {
2021-02-19 22:18:43 +00:00
fprintf(stderr, "ECCODES DEBUG try_template path='%s'\n", path);
2016-07-19 10:52:55 +00:00
}
2020-01-22 13:10:59 +00:00
if (codes_access(path, F_OK) == 0) {
FILE* f = codes_fopen(path, "r");
if (!f) {
grib_context_log(c, GRIB_LOG_PERROR, "cannot open %s", path);
2016-07-19 10:52:55 +00:00
return NULL;
}
2020-01-22 13:10:59 +00:00
g = codes_bufr_handle_new_from_file(c, f, &err);
2016-07-19 10:52:55 +00:00
if (!g) {
2020-01-22 13:10:59 +00:00
grib_context_log(c, GRIB_LOG_ERROR, "cannot create BUFR handle from %s", path);
2016-07-19 10:52:55 +00:00
}
fclose(f);
}
return g;
}
2020-01-22 13:10:59 +00:00
static char* try_template_path(grib_context* c, const char* dir, const char* name)
2013-03-25 12:04:10 +00:00
{
2015-12-22 17:55:45 +00:00
char path[1024];
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
sprintf(path, "%s/%s.tmpl", dir, name);
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
if (codes_access(path, R_OK) == 0) {
return grib_context_strdup(c, path);
2015-12-22 17:55:45 +00:00
}
2013-03-25 12:04:10 +00:00
2015-12-22 17:55:45 +00:00
return NULL;
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
grib_handle* grib_external_template(grib_context* c, const char* name)
2013-03-25 12:04:10 +00:00
{
2020-01-22 13:10:59 +00:00
const char* base = c->grib_samples_path;
2015-12-22 17:55:45 +00:00
char buffer[1024];
2020-01-22 13:10:59 +00:00
char* p = buffer;
grib_handle* g = NULL;
2015-12-22 17:55:45 +00:00
2020-01-22 13:10:59 +00:00
if (!base)
return NULL;
2015-12-22 17:55:45 +00:00
2020-01-22 13:10:59 +00:00
while (*base) {
if (*base == ECC_PATH_DELIMITER_CHAR) {
2015-12-22 17:55:45 +00:00
*p = 0;
2020-01-22 13:10:59 +00:00
g = try_template(c, buffer, name);
if (g)
return g;
2015-12-22 17:55:45 +00:00
p = buffer;
base++; /*advance past delimiter*/
}
*p++ = *base++;
}
2020-01-22 13:10:59 +00:00
*p = 0;
return g = try_template(c, buffer, name);
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
grib_handle* bufr_external_template(grib_context* c, const char* name)
2016-07-19 10:52:55 +00:00
{
2020-01-22 13:10:59 +00:00
const char* base = c->grib_samples_path;
2016-07-19 10:52:55 +00:00
char buffer[1024];
2020-01-22 13:10:59 +00:00
char* p = buffer;
grib_handle* g = NULL;
2016-07-19 10:52:55 +00:00
2020-01-22 13:10:59 +00:00
if (!base)
return NULL;
2016-07-19 10:52:55 +00:00
2020-01-22 13:10:59 +00:00
while (*base) {
if (*base == ECC_PATH_DELIMITER_CHAR) {
2016-07-19 10:52:55 +00:00
*p = 0;
2020-01-22 13:10:59 +00:00
g = try_bufr_template(c, buffer, name);
if (g)
return g;
2016-07-19 10:52:55 +00:00
p = buffer;
base++; /*advance past delimiter*/
}
*p++ = *base++;
}
*p = 0;
2020-01-22 13:10:59 +00:00
g = try_bufr_template(c, buffer, name);
2016-07-19 10:52:55 +00:00
return g;
}
2020-01-22 13:10:59 +00:00
char* grib_external_template_path(grib_context* c, const char* name)
2013-03-25 12:04:10 +00:00
{
2020-01-22 13:10:59 +00:00
const char* base = c->grib_samples_path;
2015-12-22 17:55:45 +00:00
char buffer[1024];
2020-01-22 13:10:59 +00:00
char* p = buffer;
char* g = NULL;
2015-12-22 17:55:45 +00:00
2020-01-22 13:10:59 +00:00
if (!base)
return NULL;
2015-12-22 17:55:45 +00:00
2020-01-22 13:10:59 +00:00
while (*base) {
if (*base == ECC_PATH_DELIMITER_CHAR) {
2015-12-22 17:55:45 +00:00
*p = 0;
2020-01-22 13:10:59 +00:00
g = try_template_path(c, buffer, name);
if (g)
return g;
2015-12-22 17:55:45 +00:00
p = buffer;
base++;
}
*p++ = *base++;
}
2020-01-22 13:10:59 +00:00
*p = 0;
return g = try_template_path(c, buffer, name);
2013-03-25 12:04:10 +00:00
}