mirror of https://github.com/ecmwf/eccodes.git
ECC-1405: Add new function: codes_handle_new_from_samples
This commit is contained in:
parent
fa7f760a7b
commit
b697f66b2e
|
@ -25,7 +25,8 @@ int main(int argc, char** argv)
|
|||
exit(1);
|
||||
}
|
||||
|
||||
h = codes_grib_handle_new_from_samples(NULL, "GRIB2");
|
||||
/* h = codes_grib_handle_new_from_samples(NULL, "GRIB2"); */
|
||||
h = codes_handle_new_from_samples(NULL, "GRIB2");
|
||||
if (!h) {
|
||||
fprintf(stderr, "Cannot create grib handle\n");
|
||||
return 1;
|
||||
|
@ -363,10 +364,7 @@ int main(int argc, char** argv)
|
|||
|
||||
CODES_CHECK(codes_set_double_array(h, "values", vdouble, size), 0);
|
||||
free(vdouble);
|
||||
CODES_CHECK(codes_set_long(h, "dirty_statistics", 1), 0);
|
||||
CODES_CHECK(codes_set_long(h, "changeDecimalPrecision", 0), 0);
|
||||
CODES_CHECK(codes_set_long(h, "decimalPrecision", 0), 0);
|
||||
CODES_CHECK(codes_set_long(h, "setBitsPerValue", 0), 0);
|
||||
|
||||
/* Save the message */
|
||||
|
||||
f = fopen(argv[1], "wb");
|
||||
|
|
|
@ -477,6 +477,17 @@ codes_handle* codes_grib_handle_new_from_samples(codes_context* c, const char* s
|
|||
*/
|
||||
codes_handle* codes_bufr_handle_new_from_samples(codes_context* c, const char* sample_name);
|
||||
|
||||
/**
|
||||
* Create a handle from a file contained in a samples directory.
|
||||
* The samples file can be GRIB, BUFR etc. Its type will be determined at runtime.
|
||||
* The message is copied at the creation of the handle
|
||||
*
|
||||
* @param c : the context from which the handle will be created (NULL for default context)
|
||||
* @param sample_name : the name of the sample file
|
||||
* @return the new handle, NULL if the resource is invalid or a problem is encountered
|
||||
*/
|
||||
codes_handle* codes_handle_new_from_samples(codes_context* c, const char* sample_name);
|
||||
|
||||
|
||||
/**
|
||||
* Clone an existing handle using the context of the original handle,
|
||||
|
|
|
@ -1025,6 +1025,7 @@ void grib_empty_section(grib_context* c, grib_section* b);
|
|||
void grib_section_delete(grib_context* c, grib_section* b);
|
||||
int grib_handle_delete(grib_handle* h);
|
||||
grib_handle* grib_new_handle(grib_context* c);
|
||||
grib_handle* codes_handle_new_from_samples(grib_context* c, const char* name);
|
||||
grib_handle* grib_handle_new_from_samples(grib_context* c, const char* name);
|
||||
grib_handle* codes_bufr_handle_new_from_samples(grib_context* c, const char* name);
|
||||
int grib_write_message(const grib_handle* h, const char* file, const char* mode);
|
||||
|
@ -1187,6 +1188,7 @@ long grib_get_decimal_scale_fact(double max, double min, long bpval, long binary
|
|||
|
||||
/* 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);
|
||||
char* get_external_template_path(grib_context* c, const char* name);
|
||||
|
|
|
@ -248,6 +248,29 @@ static grib_handle* grib_handle_create(grib_handle* gl, grib_context* c, const v
|
|||
return gl;
|
||||
}
|
||||
|
||||
grib_handle* codes_handle_new_from_samples(grib_context* c, const char* name)
|
||||
{
|
||||
grib_handle* g = 0;
|
||||
if (c == NULL)
|
||||
c = grib_context_get_default();
|
||||
grib_context_set_handle_file_count(c, 0);
|
||||
grib_context_set_handle_total_count(c, 0);
|
||||
|
||||
if (c->debug) {
|
||||
fprintf(stderr, "ECCODES DEBUG codes_handle_new_from_samples '%s'\n", name);
|
||||
}
|
||||
|
||||
g = codes_external_template(c, name);
|
||||
if (!g)
|
||||
grib_context_log(c, GRIB_LOG_ERROR,
|
||||
"Unable to load sample file '%s.tmpl'\n"
|
||||
" from %s\n"
|
||||
" (ecCodes Version=%s)",
|
||||
name, c->grib_samples_path, ECCODES_VERSION_STR);
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
grib_handle* grib_handle_new_from_samples(grib_context* c, const char* name)
|
||||
{
|
||||
grib_handle* g = 0;
|
||||
|
|
|
@ -70,6 +70,24 @@ static grib_handle* try_product_template(grib_context* c, ProductKind product_ki
|
|||
grib_context_log(c, GRIB_LOG_PERROR, "cannot open %s", path);
|
||||
return NULL;
|
||||
}
|
||||
if (product_kind == PRODUCT_ANY)
|
||||
{
|
||||
/* Determine the product kind from sample file */
|
||||
char* mesg = NULL;
|
||||
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;
|
||||
} 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 {
|
||||
|
@ -77,7 +95,7 @@ static grib_handle* try_product_template(grib_context* c, ProductKind product_ki
|
|||
g = grib_handle_new_from_file(c, f, &err);
|
||||
}
|
||||
if (!g) {
|
||||
grib_context_log(c, GRIB_LOG_ERROR, "cannot create handle from %s", path);
|
||||
grib_context_log(c, GRIB_LOG_ERROR, "Cannot create handle from %s", path);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
@ -100,6 +118,37 @@ 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)
|
||||
{
|
||||
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_ANY, buffer, name);
|
||||
if (g)
|
||||
return g;
|
||||
p = buffer;
|
||||
base++; /*advance past delimiter*/
|
||||
}
|
||||
*p++ = *base++;
|
||||
}
|
||||
|
||||
*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;
|
||||
|
|
Loading…
Reference in New Issue