ECC-1425: GRIB2: Add mars keyword FCMonth for C3S seasonal

This commit is contained in:
Shahram Najm 2022-08-17 13:30:47 +01:00
commit f7a6484b61
5 changed files with 193 additions and 37 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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)
{

View File

@ -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

72
tests/grib_ecc-1425.sh Executable file
View File

@ -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 <<EOF
set dataDate = 20220301;
set hour = 0;
set P1 = 17;
set P2 = 64;
set timeRangeIndicator = 10;
set localDefinitionNumber = 16;
set marsClass = 'c3';
set marsType = 'fcmean';
set marsStream = 'msmm';
set verifyingMonth = 202208;
set averagingPeriod = 6;
set forecastMonth = 6;
assert( marsForecastMonth == 6 );
write;
EOF
${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample_grib1
grib_check_key_equals $tempGrib "mars.fcmonth" 6
# GRIB2
# ------
cat > $tempFilt <<EOF
set dataDate = 20220214;
set dataTime = 18;
set setLocalDefinition = 1;
set localDefinitionNumber = 12;
set marsClass = 'c3';
set marsType = 'fcmean';
set marsStream = 'msmm';
set productDefinitionTemplateNumber = 8;
set forecastTime = 3288;
set monthOfEndOfOverallTimeInterval = 8;
set dayOfEndOfOverallTimeInterval = 1;
set typeOfTimeIncrement = 2;
set indicatorOfUnitForTimeRange = 3;
set lengthOfTimeRange = 1;
set indicatorOfUnitForTimeIncrement = 1;
set timeIncrement = 6;
set typeOfStatisticalProcessing = 0;
assert( marsForecastMonth == 5 );
write;
EOF
${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample_grib2
grib_check_key_equals $tempGrib "mars.fcmonth" 5
rm -f $tempGrib $tempFilt