eccodes/src/grib_bufr_descriptor.cc

117 lines
3.6 KiB
C++
Raw Normal View History

2014-09-01 12:20:22 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2014-09-01 12:20:22 +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"
2024-03-19 12:17:17 +00:00
#include "accessor/grib_accessor_class_bufr_data_array.h"
#include "grib_scaling.h"
2014-09-01 12:20:22 +00:00
bufr_descriptor* grib_bufr_descriptor_new(grib_accessor* tables_accessor, int code, int silent, int* err)
2017-06-19 11:31:53 +00:00
{
2024-03-19 12:17:17 +00:00
bufr_descriptor* ret = accessor_bufr_elements_table_get_descriptor(tables_accessor, code, err);
if (!silent && *err)
2024-08-11 23:54:29 +00:00
grib_context_log(tables_accessor->context_, GRIB_LOG_ERROR,
2020-01-22 13:10:59 +00:00
"unable to get descriptor %06d from table", code);
2017-06-19 11:31:53 +00:00
return ret;
2014-09-01 12:20:22 +00:00
}
2017-06-19 11:31:53 +00:00
bufr_descriptor* grib_bufr_descriptor_clone(bufr_descriptor* d)
{
bufr_descriptor* cd;
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
if (!d)
return NULL;
2014-09-01 12:20:22 +00:00
2020-01-22 13:10:59 +00:00
cd = (bufr_descriptor*)grib_context_malloc_clear(d->context, sizeof(bufr_descriptor));
2020-01-22 13:10:59 +00:00
cd->context = d->context;
cd->code = d->code;
cd->F = d->F;
cd->X = d->X;
cd->Y = d->Y;
2017-06-19 11:31:53 +00:00
/* cd->name=grib_context_strdup(d->context,d->name); See ECC-489 */
strcpy(cd->shortName, d->shortName);
strcpy(cd->units, d->units);
2020-01-22 13:10:59 +00:00
cd->scale = d->scale;
cd->factor = d->factor;
cd->width = d->width;
cd->reference = d->reference;
cd->type = d->type;
cd->nokey = d->nokey;
2017-06-19 11:31:53 +00:00
return cd;
2014-09-01 12:20:22 +00:00
}
2023-12-10 14:04:56 +00:00
int grib_bufr_descriptor_set_code(bufr_descriptor* v, int code)
2017-06-19 11:31:53 +00:00
{
2023-12-10 14:04:56 +00:00
if (!v) return GRIB_NULL_POINTER;
2024-12-20 12:58:07 +00:00
ECCODES_ASSERT(v->type == BUFR_DESCRIPTOR_TYPE_REPLICATION || v->type == BUFR_DESCRIPTOR_TYPE_OPERATOR);
2023-12-10 14:04:56 +00:00
v->code = code;
v->F = code / 100000;
2024-12-20 12:58:07 +00:00
if (v->type == BUFR_DESCRIPTOR_TYPE_REPLICATION) ECCODES_ASSERT(v->F == 1);
if (v->type == BUFR_DESCRIPTOR_TYPE_OPERATOR) ECCODES_ASSERT(v->F == 2);
2023-12-10 14:04:56 +00:00
v->X = (code - v->F * 100000) / 1000;
v->Y = (code - v->F * 100000) % 1000;
return GRIB_SUCCESS;
// Old implementation also had a "grib_accessor* tables_accessor" argument:
// if (tables_accessor == NULL)
// return GRIB_NULL_POINTER;
// bufr_descriptor* d = accessor_bufr_elements_table_get_descriptor(tables_accessor, code, &err);
// v->code = d->code;
// v->F = d->F;
// v->X = d->X;
// v->Y = d->Y;
// /* grib_context_free(c,v->name); See ECC-489 */
// /* v->name=grib_context_strdup(c,d->name); See ECC-489 */
// strcpy(v->shortName,d->shortName);
// strcpy(v->units,d->units);
// v->scale = d->scale;
// v->factor = d->factor;
// v->width = d->width;
// v->reference = d->reference;
// v->type = d->type;
// v->nokey = d->nokey;
// grib_bufr_descriptor_delete(d);
// return err;
2014-09-01 12:20:22 +00:00
}
2020-01-22 13:10:59 +00:00
void grib_bufr_descriptor_set_scale(bufr_descriptor* v, long scale)
2017-06-19 11:31:53 +00:00
{
2020-01-22 13:10:59 +00:00
if (!v)
return;
v->scale = scale;
if (scale != 0)
v->type = BUFR_DESCRIPTOR_TYPE_DOUBLE;
v->factor = codes_power<double>(-scale, 10);
2014-09-01 12:20:22 +00:00
}
2023-12-10 14:04:56 +00:00
int grib_bufr_descriptor_can_be_missing(const bufr_descriptor* v)
2017-06-19 11:31:53 +00:00
{
2020-01-22 13:10:59 +00:00
if (v->code == 31031 || v->code == 999999)
return 0;
if (v->width == 1)
return 0;
2017-06-19 11:31:53 +00:00
return 1;
2016-04-20 12:55:47 +00:00
}
2017-06-19 11:31:53 +00:00
void grib_bufr_descriptor_delete(bufr_descriptor* v)
{
2020-01-22 13:10:59 +00:00
grib_context* c = NULL;
if (!v)
return;
c = v->context;
2014-09-01 12:20:22 +00:00
2017-06-19 11:31:53 +00:00
/* grib_context_free(c,v->name); See ECC-489 */
2020-01-22 13:10:59 +00:00
grib_context_free(c, v);
2014-09-01 12:20:22 +00:00
}