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

This commit is contained in:
Shahram Najm 2022-06-07 17:28:30 +01:00
parent e2216e5340
commit 34fd416e98
2 changed files with 77 additions and 9 deletions

View File

@ -85,8 +85,6 @@ if (numberOfForecastsUsedInLocalTime > 1) {
yearOfForecastUsedInLocalTime, monthOfForecastUsedInLocalTime, dayOfForecastUsedInLocalTime,
hourOfForecastUsedInLocalTime, minuteOfForecastUsedInLocalTime, secondOfForecastUsedInLocalTime):hidden;
#transient selectedIndex = 1;
meta selectedYear element(yearOfForecastUsedInLocalTime, selectedIndex);
meta selectedMonth element(monthOfForecastUsedInLocalTime, selectedIndex);
meta selectedDay element(dayOfForecastUsedInLocalTime, selectedIndex);

View File

@ -9,6 +9,7 @@
*/
#include "grib_api_internal.h"
#include <float.h>
/*
This is used by make_class.pl
@ -187,13 +188,82 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
static int unpack_double(grib_accessor* a, double* val, size_t* len)
{
//int ret = 0;
//long date, hour, minute, second;
//long year, month, day;
//grib_accessor_closest_date* self = (grib_accessor_closest_date*)a;
*val = 1;
return 0;
int err = 0;
long num_forecasts = 0; /* numberOfForecastsUsedInLocalTime */
/* These relate to the date and time in Section 1 */
long ymdLocal, hmsLocal, yearLocal, monthLocal, dayLocal, hourLocal, minuteLocal, secondLocal;
double jLocal = 0;
double minDiff = DBL_MAX;
size_t i = 0, minIndex = 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 */
long *yearArray, *monthArray, *dayArray, *hourArray, *minuteArray, *secondArray;
grib_accessor_closest_date* self = (grib_accessor_closest_date*)a;
grib_handle* h = grib_handle_of_accessor(a);
grib_context* c = a->context;
if ((err = grib_get_long_internal(h, self->numForecasts, &num_forecasts)) != GRIB_SUCCESS) return err;
Assert(num_forecasts > 1);
if ((err = grib_get_long(h, self->dateLocal, &ymdLocal)) != GRIB_SUCCESS) return err;
yearLocal = ymdLocal / 10000;
ymdLocal %= 10000;
monthLocal = ymdLocal / 100;
ymdLocal %= 100;
dayLocal = ymdLocal;
if ((err= grib_get_long(h, self->timeLocal, &hmsLocal)) != GRIB_SUCCESS) return err;
hourLocal = hmsLocal / 10000;
hmsLocal %= 10000;
minuteLocal = hmsLocal / 100;
hmsLocal %= 100;
secondLocal = hmsLocal;
if ((err = grib_get_size(h, self->year, &size)) != GRIB_SUCCESS) return err;
Assert(size == (size_t)num_forecasts);
yearArray = (long*)grib_context_malloc_clear(c, size * sizeof(long));
if ((err = grib_get_long_array_internal(h, self->year, yearArray, &size)) != GRIB_SUCCESS) return err;
if ((err = grib_get_size(h, self->month, &size)) != GRIB_SUCCESS) return err;
Assert(size == (size_t)num_forecasts);
monthArray = (long*)grib_context_malloc_clear(c, size * sizeof(long));
if ((err = grib_get_long_array_internal(h, self->month, monthArray, &size)) != GRIB_SUCCESS) return err;
if ((err = grib_get_size(h, self->day, &size)) != GRIB_SUCCESS) return err;
Assert(size == (size_t)num_forecasts);
dayArray = (long*)grib_context_malloc_clear(c, size * sizeof(long));
if ((err = grib_get_long_array_internal(h, self->day, dayArray, &size)) != GRIB_SUCCESS) return err;
if ((err = grib_get_size(h, self->hour, &size)) != GRIB_SUCCESS) return err;
Assert(size == (size_t)num_forecasts);
hourArray = (long*)grib_context_malloc_clear(c, size * sizeof(long));
if ((err = grib_get_long_array_internal(h, self->hour, hourArray, &size)) != GRIB_SUCCESS) return err;
if ((err = grib_get_size(h, self->minute, &size)) != GRIB_SUCCESS) return err;
Assert(size == (size_t)num_forecasts);
minuteArray = (long*)grib_context_malloc_clear(c, size * sizeof(long));
if ((err = grib_get_long_array_internal(h, self->minute, minuteArray, &size)) != GRIB_SUCCESS) return err;
if ((err = grib_get_size(h, self->second, &size)) != GRIB_SUCCESS) return err;
Assert(size == (size_t)num_forecasts);
secondArray = (long*)grib_context_malloc_clear(c, size * sizeof(long));
if ((err = grib_get_long_array_internal(h, self->second, secondArray, &size)) != GRIB_SUCCESS) return err;
grib_datetime_to_julian(yearLocal, monthLocal, dayLocal, hourLocal, minuteLocal, secondLocal, &jLocal);
for(i=0; i< size; ++i) {
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) {
minDiff = diff;
minIndex = i;
}
}
*val = minIndex;
//*val = 1;
return GRIB_SUCCESS;
//return GRIB_NOT_IMPLEMENTED;
#if 0
ret = grib_get_long_internal(grib_handle_of_accessor(a), self->date, &date);