# (C) Copyright 2005- ECMWF.

remove is_localtime;
transient is_localtime=1;

include "grib2/stepUnits.def"

alias time.stepUnits = stepUnits;

# Method used to calculate the field value at the local time specified in section 1
codetable[1] localTimeMethod ('4.248.table',masterDir,localDir)=255 : dump;

#  n - number of Forecasts used in Local Time
unsigned[1] numberOfForecastsUsedInLocalTime : dump;

localTimeForecastList list(numberOfForecastsUsedInLocalTime)
{
  # Year of Forecast used in Local Time
  unsigned[2] yearOfForecastUsedInLocalTime=0 : dump, edition_specific;

  # Month of Forecast used in Local Time
  unsigned[1] monthOfForecastUsedInLocalTime=0 : dump, edition_specific;

  # Day of Forecast used in Local Time
  unsigned[1] dayOfForecastUsedInLocalTime=0 : dump, edition_specific;

  # Hour of Forecast used in Local Time
  unsigned[1] hourOfForecastUsedInLocalTime=0 : dump, edition_specific;

  # Minute of Forecast used in Local Time
  unsigned[1] minuteOfForecastUsedInLocalTime=0 : dump, edition_specific;

  # Second of Forecast used in Local Time
  unsigned[1] secondOfForecastUsedInLocalTime=0 : dump, edition_specific;

  # Indicator of unit of time for ForecastTime
  codetable[1] indicatorOfUnitForForecastTime('4.4.table',masterDir,localDir)=1 : dump;

  # Length of the time range over which statistical processing is done, in units defined by the previous octet
  unsigned[4] forecastTime=0 : dump;

  # Number of time increments of Forecast used in Local Time
  unsigned[1] numberOfTimeIncrementsOfForecastsUsedInLocalTime=1 : dump;

  # Indicator of unit of time for the increment between the successive steps used
  codetable[1]  indicatorOfUnitForTimeIncrement('4.4.table',masterDir,localDir)=255 : dump;

  # Time increment between successive fields, in units defined by the previous octet
  unsigned[4] timeIncrement=0 : dump;
}

alias ls.time = dataTime;
# See ECC-707
transient lsdate_bug = 1: hidden;
transient lstime_bug = 1: hidden;


if (numberOfForecastsUsedInLocalTime == 1) {
  meta dateOfForecastUsedInLocalTime g2date(yearOfForecastUsedInLocalTime, monthOfForecastUsedInLocalTime, dayOfForecastUsedInLocalTime) : dump;
  meta timeOfForecastUsedInLocalTime   time(hourOfForecastUsedInLocalTime, minuteOfForecastUsedInLocalTime, secondOfForecastUsedInLocalTime) : dump;

  #transient stepOfForecastUsedInLocalTime = forecastTime : dump;

  # need something here to compute the diff between date time of *OfForecastUsedInLocalTime and the local time defined in section 1 (dataDate and dataTime)
  # this would be the offset (step) used to index data in mars as mars.step
  #meta dateTimeOfForecastUsedInLocalTime julian_date(yearOfForecastUsedInLocalTime, monthOfForecastUsedInLocalTime, dayOfForecastUsedInLocalTime,
  #                                                   hourOfForecastUsedInLocalTime, minuteOfForecastUsedInLocalTime, secondOfForecastUsedInLocalTime) : hidden;
  #meta dateTimeOfLocalTime               julian_date(dataDate, dataTime) : hidden;

  meta jdSelected julian_day(dateOfForecastUsedInLocalTime,
                      hourOfForecastUsedInLocalTime, minuteOfForecastUsedInLocalTime, secondOfForecastUsedInLocalTime);
  meta jdLocal julian_day(dataDate, hour,minute,second);

  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 stepOfLocalTime = dateTimeOfLocalTime - dateTimeOfForecastUsedInLocalTime : dump;

  alias mars.date = dateOfForecastUsedInLocalTime : dump;
  alias mars.time = timeOfForecastUsedInLocalTime : dump;
  alias mars.step = endStep;
  alias time.dataDate = dateOfForecastUsedInLocalTime;
  alias time.dataTime = timeOfForecastUsedInLocalTime;
  alias time.endStep = endStep;

}
if (numberOfForecastsUsedInLocalTime > 1) {
  meta selectedFcIndex closest_date(dataDate, dataTime,
    numberOfForecastsUsedInLocalTime,
    yearOfForecastUsedInLocalTime, monthOfForecastUsedInLocalTime, dayOfForecastUsedInLocalTime,
    hourOfForecastUsedInLocalTime, minuteOfForecastUsedInLocalTime, secondOfForecastUsedInLocalTime):hidden;

  if (selectedFcIndex >=0 && selectedFcIndex < numberOfForecastsUsedInLocalTime) {
    meta selectedYear   element(yearOfForecastUsedInLocalTime, selectedFcIndex);
    meta selectedMonth  element(monthOfForecastUsedInLocalTime, selectedFcIndex);
    meta selectedDay    element(dayOfForecastUsedInLocalTime, selectedFcIndex);
    meta selectedHour   element(hourOfForecastUsedInLocalTime, selectedFcIndex);
    meta selectedMinute element(minuteOfForecastUsedInLocalTime, selectedFcIndex);
    meta selectedSecond element(secondOfForecastUsedInLocalTime, selectedFcIndex);
    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);

    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 time.dataDate = dateOfForecastUsedInLocalTime;
    alias time.dataTime = timeOfForecastUsedInLocalTime;
    alias time.endStep = endStep;

  }
}
