mirror of https://github.com/ecmwf/eccodes.git
ECC-1467: First working version for simple packing
This commit is contained in:
parent
4dd790f2ff
commit
573f2f99cc
|
@ -6,11 +6,13 @@
|
|||
int main(int argc, char** argv)
|
||||
{
|
||||
int err = 0;
|
||||
float* values = NULL;
|
||||
float* fvalues = NULL;
|
||||
double* dvalues = NULL;
|
||||
size_t values_len = 0;
|
||||
size_t i = 0, len = 0;
|
||||
size_t i = 0;
|
||||
|
||||
double average = 0;
|
||||
double daverage = 0;
|
||||
float faverage = 0;
|
||||
|
||||
FILE* in = NULL;
|
||||
const char* filename = "../../data/sample.grib2";
|
||||
|
@ -33,21 +35,28 @@ int main(int argc, char** argv)
|
|||
/* get the size of the values array*/
|
||||
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
|
||||
|
||||
values = (float*)malloc(values_len * sizeof(float));
|
||||
fvalues = (float*)malloc(values_len * sizeof(float));
|
||||
dvalues = (double*)malloc(values_len * sizeof(double));
|
||||
|
||||
/* get data values*/
|
||||
CODES_CHECK(codes_get_float_array(h, "values", values, &values_len), 0);
|
||||
CODES_CHECK(codes_get_float_array(h, "values", fvalues, &values_len), 0);
|
||||
CODES_CHECK(codes_get_double_array(h, "values", dvalues, &values_len), 0);
|
||||
|
||||
average = 0;
|
||||
daverage = 0;
|
||||
faverage = 0;
|
||||
for (i = 0; i < values_len; i++){
|
||||
//printf("%f\n",values[i]);
|
||||
average += values[i];
|
||||
faverage += fvalues[i];
|
||||
daverage += dvalues[i];
|
||||
}
|
||||
|
||||
average /= (double)values_len;
|
||||
daverage /= (double)values_len;
|
||||
faverage /= (double)values_len;
|
||||
|
||||
free(values);
|
||||
printf("There are %d values, average is %g\n", (int)values_len, average);
|
||||
free(dvalues);
|
||||
free(fvalues);
|
||||
printf("There are %d values, double average is %g\n", (int)values_len, daverage);
|
||||
printf("There are %d values, float average is %f\n", (int)values_len, faverage);
|
||||
|
||||
codes_handle_delete(h);
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
SUPER = grib_accessor_class_gen
|
||||
IMPLEMENTS = init
|
||||
IMPLEMENTS = unpack_double;unpack_double_element;unpack_double_element_set
|
||||
IMPLEMENTS = unpack_float
|
||||
IMPLEMENTS = pack_double
|
||||
IMPLEMENTS = value_count
|
||||
IMPLEMENTS = dump;get_native_type
|
||||
|
@ -43,6 +44,7 @@ or edit "accessor.class" and rerun ./make_class.pl
|
|||
static int get_native_type(grib_accessor*);
|
||||
static int pack_double(grib_accessor*, const double* val, size_t* len);
|
||||
static int unpack_double(grib_accessor*, double* val, size_t* len);
|
||||
static int unpack_float(grib_accessor*, float* val, size_t* len);
|
||||
static int value_count(grib_accessor*, long*);
|
||||
static void dump(grib_accessor*, grib_dumper*);
|
||||
static void init(grib_accessor*, const long, grib_arguments*);
|
||||
|
@ -89,7 +91,7 @@ static grib_accessor_class _grib_accessor_class_data_apply_bitmap = {
|
|||
&pack_double, /* grib_pack procedures double */
|
||||
0, /* grib_pack procedures float */
|
||||
&unpack_double, /* grib_unpack procedures double */
|
||||
0, /* grib_unpack procedures float */
|
||||
&unpack_float, /* grib_unpack procedures float */
|
||||
0, /* grib_pack procedures string */
|
||||
0, /* grib_unpack procedures string */
|
||||
0, /* grib_pack array procedures string */
|
||||
|
@ -127,7 +129,6 @@ static void init_class(grib_accessor_class* c)
|
|||
c->pack_long = (*(c->super))->pack_long;
|
||||
c->unpack_long = (*(c->super))->unpack_long;
|
||||
c->pack_float = (*(c->super))->pack_float;
|
||||
c->unpack_float = (*(c->super))->unpack_float;
|
||||
c->pack_string = (*(c->super))->pack_string;
|
||||
c->unpack_string = (*(c->super))->unpack_string;
|
||||
c->pack_string_array = (*(c->super))->pack_string_array;
|
||||
|
@ -264,6 +265,88 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
|
|||
return err;
|
||||
}
|
||||
|
||||
// TODO: Not ideal!! First attempt at unpacking floats...
|
||||
// Should be merged with unpack_double and refactored! Most probably using C++ templates
|
||||
static int unpack_float(grib_accessor* a, float* val, size_t* len)
|
||||
{
|
||||
grib_accessor_data_apply_bitmap* self = (grib_accessor_data_apply_bitmap*)a;
|
||||
|
||||
size_t i = 0;
|
||||
size_t j = 0;
|
||||
size_t n_vals = 0;
|
||||
long nn = 0;
|
||||
int err = 0;
|
||||
size_t coded_n_vals = 0;
|
||||
float* coded_vals = NULL;
|
||||
double missing_value = 0;
|
||||
|
||||
err = grib_value_count(a, &nn);
|
||||
n_vals = nn;
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if (!grib_find_accessor(grib_handle_of_accessor(a), self->bitmap))
|
||||
return grib_get_float_array(grib_handle_of_accessor(a), self->coded_values, val, len);
|
||||
|
||||
if ((err = grib_get_size(grib_handle_of_accessor(a), self->coded_values, &coded_n_vals)) != GRIB_SUCCESS)
|
||||
return err;
|
||||
|
||||
if ((err = grib_get_double_internal(grib_handle_of_accessor(a), self->missing_value, &missing_value)) != GRIB_SUCCESS)
|
||||
return err;
|
||||
|
||||
if (*len < n_vals) {
|
||||
*len = n_vals;
|
||||
return GRIB_ARRAY_TOO_SMALL;
|
||||
}
|
||||
|
||||
if (coded_n_vals == 0) {
|
||||
for (i = 0; i < n_vals; i++)
|
||||
val[i] = missing_value;
|
||||
|
||||
*len = n_vals;
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
||||
if ((err = grib_get_float_array_internal(grib_handle_of_accessor(a), self->bitmap, val, &n_vals)) != GRIB_SUCCESS)
|
||||
return err;
|
||||
|
||||
coded_vals = (float*)grib_context_malloc(a->context, coded_n_vals * sizeof(float));
|
||||
if (coded_vals == NULL)
|
||||
return GRIB_OUT_OF_MEMORY;
|
||||
|
||||
if ((err = grib_get_float_array(grib_handle_of_accessor(a), self->coded_values, coded_vals, &coded_n_vals)) != GRIB_SUCCESS) {
|
||||
grib_context_free(a->context, coded_vals);
|
||||
return err;
|
||||
}
|
||||
|
||||
grib_context_log(a->context, GRIB_LOG_DEBUG,
|
||||
"grib_accessor_class_data_apply_bitmap: unpack_float : creating %s, %d values",
|
||||
a->name, n_vals);
|
||||
|
||||
for (i = 0; i < n_vals; i++) {
|
||||
if (val[i] == 0) {
|
||||
val[i] = missing_value;
|
||||
}
|
||||
else {
|
||||
val[i] = coded_vals[j++];
|
||||
if (j > coded_n_vals) {
|
||||
grib_context_free(a->context, coded_vals);
|
||||
grib_context_log(a->context, GRIB_LOG_ERROR,
|
||||
"grib_accessor_class_data_apply_bitmap [%s]:"
|
||||
" unpack_float : number of coded values does not match bitmap %ld %ld",
|
||||
a->name, coded_n_vals, n_vals);
|
||||
|
||||
return GRIB_ARRAY_TOO_SMALL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*len = n_vals;
|
||||
|
||||
grib_context_free(a->context, coded_vals);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int unpack_double_element(grib_accessor* a, size_t idx, double* val)
|
||||
{
|
||||
grib_accessor_data_apply_bitmap* self = (grib_accessor_data_apply_bitmap*)a;
|
||||
|
|
|
@ -336,7 +336,7 @@ static int _unpack_real(grib_accessor* a, double* dval, float* fval, size_t* len
|
|||
double units_factor = 1.0;
|
||||
double units_bias = 0.0;
|
||||
Assert( ! (fval && dval) );
|
||||
printf("DBG:: SIMPLE PACK unpack real.....\n");
|
||||
|
||||
if (*len < n_vals) {
|
||||
*len = (long)n_vals;
|
||||
return GRIB_ARRAY_TOO_SMALL;
|
||||
|
@ -628,7 +628,7 @@ static int unpack_double(grib_accessor* a, double* dval, size_t* len)
|
|||
if (err)
|
||||
return err;
|
||||
nvals = count;
|
||||
printf("SIMPLE unpak double.....\n");
|
||||
|
||||
return _unpack_real(a, dval, NULL, len, buf, pos, nvals);
|
||||
}
|
||||
|
||||
|
@ -644,7 +644,7 @@ static int unpack_float(grib_accessor* a, float* fval, size_t* len)
|
|||
if (err)
|
||||
return err;
|
||||
nvals = count;
|
||||
printf("SIMPLE unpack float.....\n");
|
||||
|
||||
return _unpack_real(a, NULL, fval, len, buf, pos, nvals);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue