diff --git a/definitions/grib2/local.98.12.def b/definitions/grib2/local.98.12.def index 355362b5e..a6f5cf934 100644 --- a/definitions/grib2/local.98.12.def +++ b/definitions/grib2/local.98.12.def @@ -15,4 +15,8 @@ meta indexingTimeHHMM sprintf("%.2d%.2d",indexingTimeHH,indexingTimeMM); alias mars.date = indexingDate; alias mars.time = indexingTimeHHMM; +# ECC-1425 +meta marsForecastMonth g1forecastmonth(dataDate, forecastTime): read_only; +alias mars.fcmonth = marsForecastMonth; + pad padding_loc12_1(50); diff --git a/definitions/grib2/local.98.16.def b/definitions/grib2/local.98.16.def index c94fae1bb..c4964d0ac 100644 --- a/definitions/grib2/local.98.16.def +++ b/definitions/grib2/local.98.16.def @@ -5,3 +5,7 @@ unsigned[2] methodNumber : dump; alias local.systemNumber=systemNumber; alias local.methodNumber=methodNumber; + +# ECC-1425 +meta marsForecastMonth g1forecastmonth(dataDate, forecastTime): read_only; +alias mars.fcmonth = marsForecastMonth; diff --git a/src/grib_accessor_class_g1forecastmonth.c b/src/grib_accessor_class_g1forecastmonth.c index 45a67b2d5..06c799fdd 100644 --- a/src/grib_accessor_class_g1forecastmonth.c +++ b/src/grib_accessor_class_g1forecastmonth.c @@ -147,14 +147,17 @@ static void init_class(grib_accessor_class* c) static void init(grib_accessor* a, const long l, grib_arguments* c) { grib_accessor_g1forecastmonth* self = (grib_accessor_g1forecastmonth*)a; - int n = 0; - - self->verification_yearmonth = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++); - self->base_date = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++); - self->day = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++); - self->hour = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++); - self->fcmonth = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++); - self->check = grib_arguments_get_name(grib_handle_of_accessor(a), c, n++); + grib_handle* h = grib_handle_of_accessor(a); + int n = 0; + const int count = grib_arguments_get_count(c); + if (count == 6) { /* GRIB1 case -- this needs to be refactored */ + self->verification_yearmonth = grib_arguments_get_name(h, c, n++); + self->base_date = grib_arguments_get_name(h, c, n++); + self->day = grib_arguments_get_name(h, c, n++); + self->hour = grib_arguments_get_name(h, c, n++); + self->fcmonth = grib_arguments_get_name(h, c, n++); + self->check = grib_arguments_get_name(h, c, n++); + } } static void dump(grib_accessor* a, grib_dumper* dumper) @@ -162,39 +165,15 @@ static void dump(grib_accessor* a, grib_dumper* dumper) grib_dump_long(dumper, a, NULL); } -static int unpack_long(grib_accessor* a, long* val, size_t* len) +static int calculate_fcmonth(grib_accessor* a, + long verification_yearmonth, long base_date, long day, long hour, long* result) { - int ret = 0; - grib_accessor_g1forecastmonth* self = (grib_accessor_g1forecastmonth*)a; - - long verification_yearmonth = 0; long base_yearmonth = 0; - long base_date = 0; - long day = 0; - long hour = 0; - long vyear = 0; long vmonth = 0; long byear = 0; long bmonth = 0; - long fcmonth = 0; - long gribForecastMonth = 0; - long check = 0; - - if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), - self->verification_yearmonth, &verification_yearmonth)) != GRIB_SUCCESS) - return ret; - if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->base_date, &base_date)) != GRIB_SUCCESS) - return ret; - if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->day, &day)) != GRIB_SUCCESS) - return ret; - if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->hour, &hour)) != GRIB_SUCCESS) - return ret; - if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->fcmonth, &gribForecastMonth)) != GRIB_SUCCESS) - return ret; - if ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->check, &check)) != GRIB_SUCCESS) - return ret; base_yearmonth = base_date / 100; @@ -207,9 +186,90 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) if (day == 1 && hour == 0) fcmonth++; + *result = fcmonth; + return GRIB_SUCCESS; +} + +static int unpack_long_edition2(grib_accessor* a, long* val, size_t* len) +{ + int err = 0; + grib_handle* h = grib_handle_of_accessor(a); + long dataDate, verification_yearmonth; + long year, month, day, hour, minute, second; + long year2, month2, day2, hour2, minute2, second2; + long forecastTime, indicatorOfUnitOfTimeRange; + double jul_base, jul2, dstep; + + if ((err = grib_get_long(h, "year", &year)) != GRIB_SUCCESS) return err; + if ((err = grib_get_long(h, "month", &month)) != GRIB_SUCCESS) return err; + if ((err = grib_get_long(h, "day", &day)) != GRIB_SUCCESS) return err; + if ((err = grib_get_long(h, "hour", &hour)) != GRIB_SUCCESS) return err; + if ((err = grib_get_long(h, "minute", &minute)) != GRIB_SUCCESS) return err; + if ((err = grib_get_long(h, "second", &second)) != GRIB_SUCCESS) return err; + + if ((err = grib_get_long_internal(h, "dataDate", &dataDate)) != GRIB_SUCCESS) + return err; + + if ((err = grib_get_long_internal(h, "forecastTime", &forecastTime)) != GRIB_SUCCESS) + return err; + if ((err = grib_get_long_internal(h, "indicatorOfUnitOfTimeRange", &indicatorOfUnitOfTimeRange)) != GRIB_SUCCESS) + return err; + if (indicatorOfUnitOfTimeRange != 1) { /* must be hour */ + grib_context_log(a->context, GRIB_LOG_ERROR, "indicatorOfUnitOfTimeRange must be 1 (hour)"); + return GRIB_DECODING_ERROR; + } + + if ((err = grib_datetime_to_julian(year, month, day, hour, minute, second, &jul_base)) != GRIB_SUCCESS) + return err; + + dstep = (((double)forecastTime) * 3600) / 86400; /* as a fraction of a day */ + jul2 = jul_base + dstep; + + if ((err = grib_julian_to_datetime(jul2, &year2, &month2, &day2, &hour2, &minute2, &second2)) != GRIB_SUCCESS) + return err; + + verification_yearmonth = year2*100 + month2; + if ((err = calculate_fcmonth(a, verification_yearmonth, dataDate, day, hour, val)) != GRIB_SUCCESS) + return err; + + return GRIB_SUCCESS; +} + +static int unpack_long_edition1(grib_accessor* a, long* val, size_t* len) +{ + int err = 0; + grib_accessor_g1forecastmonth* self = (grib_accessor_g1forecastmonth*)a; + + long verification_yearmonth = 0; + long base_date = 0; + long day = 0; + long hour = 0; + long gribForecastMonth = 0; + long check = 0; + long fcmonth = 0; + + if ((err = grib_get_long_internal(grib_handle_of_accessor(a), + self->verification_yearmonth, &verification_yearmonth)) != GRIB_SUCCESS) + return err; + if ((err = grib_get_long_internal(grib_handle_of_accessor(a), self->base_date, &base_date)) != GRIB_SUCCESS) + return err; + if ((err = grib_get_long_internal(grib_handle_of_accessor(a), self->day, &day)) != GRIB_SUCCESS) + return err; + if ((err = grib_get_long_internal(grib_handle_of_accessor(a), self->hour, &hour)) != GRIB_SUCCESS) + return err; + if ((err = grib_get_long_internal(grib_handle_of_accessor(a), self->fcmonth, &gribForecastMonth)) != GRIB_SUCCESS) + return err; + if ((err = grib_get_long_internal(grib_handle_of_accessor(a), self->check, &check)) != GRIB_SUCCESS) + return err; + + if ((err = calculate_fcmonth(a, verification_yearmonth, base_date, day, hour, val)) != GRIB_SUCCESS) + return err; + + /* Verification - compare gribForecastMonth with fcmonth */ + fcmonth = *val; if (gribForecastMonth != 0 && gribForecastMonth != fcmonth) { if (check) { - grib_context_log(a->context, GRIB_LOG_FATAL, "%s=%ld (%s-%s)=%ld", self->fcmonth, + grib_context_log(a->context, GRIB_LOG_ERROR, "%s=%ld (%s-%s)=%ld", self->fcmonth, gribForecastMonth, self->base_date, self->verification_yearmonth, fcmonth); Assert(gribForecastMonth == fcmonth); } @@ -219,11 +279,26 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) } } - *val = fcmonth; - return GRIB_SUCCESS; } +static int unpack_long(grib_accessor* a, long* val, size_t* len) +{ + int err = 0; + grib_handle* hand = grib_handle_of_accessor(a); + long edition = 0; + + if ((err = grib_get_long(hand, "edition", &edition)) != GRIB_SUCCESS) + return err; + + if (edition == 1) + return unpack_long_edition1(a, val, len); + if (edition == 2) + return unpack_long_edition2(a, val, len); + + return GRIB_UNSUPPORTED_EDITION; +} + /* TODO: Check for a valid date */ static int pack_long(grib_accessor* a, const long* val, size_t* len) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2d1fce5dc..344004292 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -209,6 +209,7 @@ if( HAVE_BUILD_TOOLS ) grib_ecc-1001 grib_ecc-1030 grib_ecc-1397 + grib_ecc-1425 grib_set_bytes grib_set_force bufr_ecc-556 diff --git a/tests/grib_ecc-1425.sh b/tests/grib_ecc-1425.sh new file mode 100755 index 000000000..16af3d11a --- /dev/null +++ b/tests/grib_ecc-1425.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# (C) Copyright 2005- ECMWF. +# +# 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.ctest.sh + +label="grib_ecc-1425_test" + +tempGrib=temp.$label.grib +tempFilt=temp.${label}.filt + +sample_grib1=$ECCODES_SAMPLES_PATH/GRIB1.tmpl +sample_grib2=$ECCODES_SAMPLES_PATH/GRIB2.tmpl + +cat > $tempFilt < $tempFilt <