Prototyping: GRIB clone lightweight

This commit is contained in:
Shahram Najm 2023-11-08 14:01:48 +00:00
parent 7a83e3c1b8
commit 4837d203a2
4 changed files with 44 additions and 0 deletions

View File

@ -175,6 +175,11 @@ grib_handle* codes_handle_clone(const grib_handle* h)
{
return grib_handle_clone(h);
}
grib_handle* codes_handle_clone_light(const grib_handle* h)
{
return grib_handle_clone_light(h);
}
int codes_handle_delete(grib_handle* h)
{
return grib_handle_delete(h);

View File

@ -495,6 +495,7 @@ codes_handle* codes_handle_new_from_samples(codes_context* c, const char* sample
* @return the new handle, NULL if the message is invalid or a problem is encountered
*/
codes_handle* codes_handle_clone(const codes_handle* h);
codes_handle* codes_handle_clone_light(const codes_handle* h);
/**
* Frees a handle, also frees the message if it is not a user message

View File

@ -507,6 +507,7 @@ grib_handle* grib_handle_new_from_samples(grib_context* c, const char* sample_na
* @return the new handle, NULL if the message is invalid or a problem is encountered
*/
grib_handle* grib_handle_clone(const grib_handle* h);
grib_handle* grib_handle_clone_light(const grib_handle* h);
/**
* Frees a handle, also frees the message if it is not a user message

View File

@ -326,6 +326,43 @@ grib_handle* grib_handle_clone(const grib_handle* h)
return result;
}
grib_handle* grib_handle_clone_light(const grib_handle* h)
{
int err = 0;
size_t size1 = 0;
const void* msg1 = NULL;
grib_handle* h1 = (grib_handle*)h;
long edition = 0;
err = grib_get_long(h, "edition", &edition);
if (!err && edition == 1) {
grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Edition not supported", __func__);
return NULL;
}
err = grib_get_message_headers(h1, &msg1, &size1);
if (err) return NULL;
size1 += 4;
grib_handle* result = grib_handle_new_from_partial_message_copy(h->context, msg1, size1);
result->buffer->data[ size1 - 4 ] = '7';
result->buffer->data[ size1 - 3 ] = '7';
result->buffer->data[ size1 - 2 ] = '7';
result->buffer->data[ size1 - 1 ] = '7';
result->buffer->ulength = size1;
result->product_kind = h->product_kind;
long off = 64; // This is only true for GRIB edition 2
err = grib_encode_unsigned_long( result->buffer->data, (unsigned long)size1, &off, 64);
if (err) {
printf("err=%s\n", grib_get_error_message(err));
return NULL;
}
return result;
}
grib_handle* codes_handle_new_from_file(grib_context* c, FILE* f, ProductKind product, int* error)
{
if (product == PRODUCT_GRIB)