eccodes/src/grib_templates.cc

174 lines
5.3 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
2023-06-24 11:58:32 +00:00
// 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.
// Note: This is now superseded by MEMFS
//
// typedef struct grib_templates {
// const char* name;
// const unsigned char* data;
// size_t size;
// } grib_templates;
2013-03-25 12:04:10 +00:00
2023-06-24 11:58:32 +00:00
// grib_handle* grib_internal_sample(grib_context* c,const char* name)
// {
// size_t i;
2024-01-30 12:08:46 +00:00
// const size_t num_samples = sizeof(templates) / sizeof(templates[0]);
2023-06-24 11:58:32 +00:00
// for(i = 0; i < num_samples; i++)
// 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
2024-03-01 23:34:10 +00:00
// 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
2024-03-01 23:34:10 +00:00
// if product_kind is PRODUCT_ANY, the type of sample file is determined at runtime
static grib_handle* try_product_sample(grib_context* c, ProductKind product_kind, 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
if (string_ends_with(name, ".tmpl"))
snprintf(path, sizeof(path), "%s/%s", dir, name);
else
snprintf(path, sizeof(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) {
2024-03-01 23:34:10 +00:00
fprintf(stderr, "ECCODES DEBUG try_product_sample product=%s, path='%s'\n", codes_get_product_name(product_kind), path);
2015-12-22 17:55:45 +00:00
}
2014-04-24 16:48:24 +00:00
2024-03-01 23:34:10 +00:00
if (codes_access(path, F_OK) == 0) { // 0 means file exists
2020-01-22 13:10:59 +00:00
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;
}
if (product_kind == PRODUCT_ANY)
{
2024-03-01 23:34:10 +00:00
// Determine the product kind from sample file
char* mesg = NULL;
size_t size = 0;
off_t offset = 0;
2022-06-16 21:56:09 +00:00
mesg = (char*)wmo_read_any_from_file_malloc(f, 0, &size, &offset, &err);
2022-06-15 18:17:59 +00:00
if (mesg && !err) {
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(size > 4);
2022-06-15 18:17:59 +00:00
if (strncmp(mesg, "GRIB", 4) == 0 || strncmp(mesg, "DIAG", 4) == 0 || strncmp(mesg, "BUDG", 4) == 0) {
product_kind = PRODUCT_GRIB;
} else if (strncmp(mesg, "BUFR", 4) == 0) {
product_kind = PRODUCT_BUFR;
} else {
grib_context_log(c, GRIB_LOG_ERROR, "Could not determine product kind");
}
grib_context_free(c, mesg);
rewind(f);
} else {
grib_context_log(c, GRIB_LOG_ERROR, "Could not determine product kind");
}
}
2022-03-14 19:54:22 +00:00
if (product_kind == PRODUCT_BUFR) {
g = codes_bufr_handle_new_from_file(c, f, &err);
} else {
2024-03-01 23:34:10 +00:00
// Note: Pseudo GRIBs like DIAG and BUDG also come here
2023-06-26 20:21:07 +00:00
DEBUG_ASSERT(product_kind == PRODUCT_GRIB);
2022-03-14 19:54:22 +00:00
g = grib_handle_new_from_file(c, f, &err);
2016-07-19 10:52:55 +00:00
}
if (!g) {
grib_context_log(c, GRIB_LOG_ERROR, "Cannot create handle from %s", path);
2016-07-19 10:52:55 +00:00
}
fclose(f);
}
return g;
}
2024-03-01 23:34:10 +00:00
static char* try_sample_path(grib_context* c, const char* dir, const char* name)
2013-03-25 12:04:10 +00:00
{
2024-03-01 23:34:10 +00:00
// The ".tmpl" extension is historic. It should have been ".sample"
2021-02-26 14:27:09 +00:00
char path[2048];
if (string_ends_with(name, ".tmpl"))
snprintf(path, sizeof(path), "%s/%s", dir, name);
else
snprintf(path, sizeof(path), "%s/%s.tmpl", dir, name);
2013-03-25 12:04:10 +00:00
2024-03-01 23:34:10 +00:00
if (codes_access(path, F_OK) == 0) { // 0 means file exists
2020-01-22 13:10:59 +00:00
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
}
2024-03-01 23:34:10 +00:00
// External here means on disk
grib_handle* codes_external_sample(grib_context* c, ProductKind product_kind, const char* name)
{
const char* base = c->grib_samples_path;
char buffer[1024];
char* p = buffer;
grib_handle* g = NULL;
if (!base)
return NULL;
while (*base) {
if (*base == ECC_PATH_DELIMITER_CHAR) {
*p = 0;
2024-03-01 23:34:10 +00:00
g = try_product_sample(c, product_kind, buffer, name);
if (g)
return g;
p = buffer;
2024-03-01 23:34:10 +00:00
base++; //advance past delimiter
}
*p++ = *base++;
}
*p = 0;
2024-03-01 23:34:10 +00:00
g = try_product_sample(c, product_kind, buffer, name);
2016-07-19 10:52:55 +00:00
return g;
}
2024-03-01 23:34:10 +00:00
char* get_external_sample_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;
2024-03-01 23:34:10 +00:00
g = try_sample_path(c, buffer, name);
2020-01-22 13:10:59 +00:00
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;
2024-03-01 23:34:10 +00:00
return g = try_sample_path(c, buffer, name);
2013-03-25 12:04:10 +00:00
}