ECC-1405: Cleanup

This commit is contained in:
Shahram Najm 2022-06-15 19:17:59 +01:00
parent b697f66b2e
commit 1aa377fde1
3 changed files with 20 additions and 78 deletions

View File

@ -1187,10 +1187,7 @@ long grib_get_bits_per_value(double max, double min, long binary_scale_factor);
long grib_get_decimal_scale_fact(double max, double min, long bpval, long binary_scale);
/* grib_templates.c */
/*grib_handle *grib_internal_sample(grib_context *c, const char *name);*/
grib_handle* codes_external_template(grib_context* c, const char* name);
grib_handle* grib_external_template(grib_context* c, const char* name);
grib_handle* bufr_external_template(grib_context* c, const char* name);
grib_handle* codes_external_template(grib_context* c, ProductKind product_kind, const char* name);
char* get_external_template_path(grib_context* c, const char* name);
/* grib_dependency.c */

View File

@ -260,7 +260,7 @@ grib_handle* codes_handle_new_from_samples(grib_context* c, const char* name)
fprintf(stderr, "ECCODES DEBUG codes_handle_new_from_samples '%s'\n", name);
}
g = codes_external_template(c, name);
g = codes_external_template(c, PRODUCT_ANY, name);
if (!g)
grib_context_log(c, GRIB_LOG_ERROR,
"Unable to load sample file '%s.tmpl'\n"
@ -287,7 +287,7 @@ grib_handle* grib_handle_new_from_samples(grib_context* c, const char* name)
fprintf(stderr, "ECCODES DEBUG grib_handle_new_from_samples '%s'\n", name);
}
g = grib_external_template(c, name);
g = codes_external_template(c, PRODUCT_GRIB, name);
if (!g)
grib_context_log(c, GRIB_LOG_ERROR,
"Unable to load GRIB sample file '%s.tmpl'\n"
@ -306,15 +306,11 @@ grib_handle* codes_bufr_handle_new_from_samples(grib_context* c, const char* nam
grib_context_set_handle_file_count(c, 0);
grib_context_set_handle_total_count(c, 0);
/*
* g = grib_internal_sample(c,name);
* if(g) return g;
*/
if (c->debug) {
fprintf(stderr, "ECCODES DEBUG bufr_handle_new_from_samples '%s'\n", name);
}
g = bufr_external_template(c, name);
g = codes_external_template(c, PRODUCT_BUFR, name);
if (!g)
grib_context_log(c, GRIB_LOG_ERROR,
"Unable to load BUFR sample file '%s.tmpl'\n"

View File

@ -49,6 +49,7 @@ grib_handle* grib_internal_sample(grib_context* c,const char* name)
#define ECC_PATH_DELIMITER_CHAR ':'
#endif
/* if product_kind is PRODUCT_ANY, the type of sample file is determined at runtime */
static grib_handle* try_product_template(grib_context* c, ProductKind product_kind, const char* dir, const char* name)
{
char path[1024];
@ -77,21 +78,26 @@ static grib_handle* try_product_template(grib_context* c, ProductKind product_ki
size_t size = 0;
off_t offset = 0;
mesg = wmo_read_any_from_file_malloc(f, 0, &size, &offset, &err);
Assert(size > 4);
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;
if (mesg && !err) {
Assert(size > 4);
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");
}
grib_context_free(c, mesg);
rewind(f);
}
if (product_kind == PRODUCT_BUFR) {
g = codes_bufr_handle_new_from_file(c, f, &err);
} else {
/* Note: Pseudo GRIBs like DIAG and BUDG also come here */
DebugAssert(product_kind == PRODUCT_GRIB);
g = grib_handle_new_from_file(c, f, &err);
}
if (!g) {
@ -118,12 +124,7 @@ static char* try_template_path(grib_context* c, const char* dir, const char* nam
return NULL;
}
/* TODO: The following functions should be renamed/combined:
* codes_external_template
* grib_external_template
* bufr_external_template
*/
grib_handle* codes_external_template(grib_context* c, const char* name)
grib_handle* codes_external_template(grib_context* c, ProductKind product_kind, const char* name)
{
const char* base = c->grib_samples_path;
char buffer[1024];
@ -136,7 +137,7 @@ grib_handle* codes_external_template(grib_context* c, const char* name)
while (*base) {
if (*base == ECC_PATH_DELIMITER_CHAR) {
*p = 0;
g = try_product_template(c, PRODUCT_ANY, buffer, name);
g = try_product_template(c, product_kind, buffer, name);
if (g)
return g;
p = buffer;
@ -146,59 +147,7 @@ grib_handle* codes_external_template(grib_context* c, const char* name)
}
*p = 0;
return g = try_product_template(c, PRODUCT_ANY, buffer, name);
}
grib_handle* grib_external_template(grib_context* c, 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;
g = try_product_template(c, PRODUCT_GRIB, buffer, name);
if (g)
return g;
p = buffer;
base++; /*advance past delimiter*/
}
*p++ = *base++;
}
*p = 0;
return g = try_product_template(c, PRODUCT_GRIB, buffer, name);
}
grib_handle* bufr_external_template(grib_context* c, 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;
g = try_product_template(c, PRODUCT_BUFR, buffer, name);
if (g)
return g;
p = buffer;
base++; /*advance past delimiter*/
}
*p++ = *base++;
}
*p = 0;
g = try_product_template(c, PRODUCT_BUFR, buffer, name);
g = try_product_template(c, product_kind, buffer, name);
return g;
}