ECC-1212: MARS key mapping for local time templates (Part 03)

This commit is contained in:
Shahram Najm 2022-06-07 17:58:57 +01:00
parent 34fd416e98
commit 0f6a86cc77
4 changed files with 35 additions and 49 deletions

View File

@ -85,24 +85,26 @@ if (numberOfForecastsUsedInLocalTime > 1) {
yearOfForecastUsedInLocalTime, monthOfForecastUsedInLocalTime, dayOfForecastUsedInLocalTime,
hourOfForecastUsedInLocalTime, minuteOfForecastUsedInLocalTime, secondOfForecastUsedInLocalTime):hidden;
meta selectedYear element(yearOfForecastUsedInLocalTime, selectedIndex);
meta selectedMonth element(monthOfForecastUsedInLocalTime, selectedIndex);
meta selectedDay element(dayOfForecastUsedInLocalTime, selectedIndex);
meta selectedHour element(hourOfForecastUsedInLocalTime, selectedIndex);
meta selectedMinute element(minuteOfForecastUsedInLocalTime, selectedIndex);
meta selectedSecond element(secondOfForecastUsedInLocalTime, selectedIndex);
meta dateOfForecastUsedInLocalTime g2date(selectedYear, selectedMonth, selectedDay);
meta timeOfForecastUsedInLocalTime time(selectedHour, selectedMinute, selectedSecond);
if (selectedIndex >=0 && selectedIndex < numberOfForecastsUsedInLocalTime) {
meta selectedYear element(yearOfForecastUsedInLocalTime, selectedIndex);
meta selectedMonth element(monthOfForecastUsedInLocalTime, selectedIndex);
meta selectedDay element(dayOfForecastUsedInLocalTime, selectedIndex);
meta selectedHour element(hourOfForecastUsedInLocalTime, selectedIndex);
meta selectedMinute element(minuteOfForecastUsedInLocalTime, selectedIndex);
meta selectedSecond element(secondOfForecastUsedInLocalTime, selectedIndex);
meta dateOfForecastUsedInLocalTime g2date(selectedYear, selectedMonth, selectedDay);
meta timeOfForecastUsedInLocalTime time(selectedHour, selectedMinute, selectedSecond);
meta jdLocal julian_day(dataDate, hour,minute,second); #section 1
meta jdSelected julian_day(dateOfForecastUsedInLocalTime, selectedHour, selectedMinute, selectedSecond);
meta jdLocal julian_day(dataDate, hour,minute,second); #section 1
meta jdSelected julian_day(dateOfForecastUsedInLocalTime, selectedHour, selectedMinute, selectedSecond);
transient diffInDays = (jdLocal - jdSelected) : hidden; # float
transient diffInHours = (diffInDays * 1440 + 0.5)/60 : hidden;
meta _endStep round(diffInHours, 10): dump, long_type;
transient endStep = _endStep; # needed to force it to be integer
transient diffInDays = (jdLocal - jdSelected) : hidden; # float
transient diffInHours = (diffInDays * 1440 + 0.5)/60 : hidden;
meta _endStep round(diffInHours, 10): dump, long_type;
transient endStep = _endStep; # needed to force it to be integer
alias mars.date = dateOfForecastUsedInLocalTime : dump;
alias mars.time = timeOfForecastUsedInLocalTime : dump;
alias mars.step = endStep;
alias mars.date = dateOfForecastUsedInLocalTime : dump;
alias mars.time = timeOfForecastUsedInLocalTime : dump;
alias mars.step = endStep;
}
}

View File

@ -186,6 +186,7 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
return ret;
}
/* Sets val to the 'index' of the closes date */
static int unpack_double(grib_accessor* a, double* val, size_t* len)
{
int err = 0;
@ -194,7 +195,7 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
long ymdLocal, hmsLocal, yearLocal, monthLocal, dayLocal, hourLocal, minuteLocal, secondLocal;
double jLocal = 0;
double minDiff = DBL_MAX;
size_t i = 0, minIndex = 0;
size_t i = 0;
size_t size = 0; /* number of elements in the array keys - should be = numberOfForecastsUsedInLocalTime */
/* These relate to the forecast dates and times in Section 4 */
@ -203,6 +204,7 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
grib_accessor_closest_date* self = (grib_accessor_closest_date*)a;
grib_handle* h = grib_handle_of_accessor(a);
grib_context* c = a->context;
*val = -1; /* initialise to an invalid index */
if ((err = grib_get_long_internal(h, self->numForecasts, &num_forecasts)) != GRIB_SUCCESS) return err;
Assert(num_forecasts > 1);
@ -215,8 +217,8 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
dayLocal = ymdLocal;
if ((err= grib_get_long(h, self->timeLocal, &hmsLocal)) != GRIB_SUCCESS) return err;
hourLocal = hmsLocal / 10000;
hmsLocal %= 10000;
hourLocal = hmsLocal / 100;
hmsLocal %= 100;
minuteLocal = hmsLocal / 100;
hmsLocal %= 100;
secondLocal = hmsLocal;
@ -256,37 +258,15 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
double jval = 0, diff = 0;
grib_datetime_to_julian(yearArray[i], monthArray[i], dayArray[i], hourArray[i], minuteArray[i], secondArray[i], &jval);
diff = jLocal - jval;
if (diff >0 && diff < minDiff) {
if (diff >= 0 && diff < minDiff) {
minDiff = diff;
minIndex = i;
*val = i;
}
}
*val = minIndex;
//*val = 1;
if (*val == -1) {
grib_context_log(c, GRIB_LOG_ERROR, "Failed to find a date/time amongst forecasts used in local time");
return GRIB_DECODING_ERROR;
}
return GRIB_SUCCESS;
//return GRIB_NOT_IMPLEMENTED;
#if 0
ret = grib_get_long_internal(grib_handle_of_accessor(a), self->date, &date);
if (ret != GRIB_SUCCESS)
return ret;
ret = grib_get_long_internal(grib_handle_of_accessor(a), self->hour, &hour);
if (ret != GRIB_SUCCESS)
return ret;
ret = grib_get_long_internal(grib_handle_of_accessor(a), self->minute, &minute);
if (ret != GRIB_SUCCESS)
return ret;
ret = grib_get_long_internal(grib_handle_of_accessor(a), self->second, &second);
if (ret != GRIB_SUCCESS)
return ret;
year = date / 10000;
date %= 10000;
month = date / 100;
date %= 100;
day = date;
ret = grib_datetime_to_julian(year, month, day, hour, minute, second, val);
return ret;
#endif
}

View File

@ -227,6 +227,7 @@
<ClCompile Include="..\..\..\src\grib_accessor_class_change_alternative_row_scanning.c" />
<ClCompile Include="..\..\..\src\grib_accessor_class_change_scanning_direction.c" />
<ClCompile Include="..\..\..\src\grib_accessor_class_check_internal_version.c" />
<ClCompile Include="..\..\..\src\grib_accessor_class_closest_date.c" />
<ClCompile Include="..\..\..\src\grib_accessor_class_codeflag.c" />
<ClCompile Include="..\..\..\src\grib_accessor_class_codetable.c" />
<ClCompile Include="..\..\..\src\grib_accessor_class_codetable_title.c" />

View File

@ -207,6 +207,9 @@
<ClCompile Include="..\..\..\src\grib_accessor_class_check_internal_version.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\grib_accessor_class_closest_date.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\grib_accessor_class_codeflag.c">
<Filter>Source Files</Filter>
</ClCompile>