# Copyright 2005-2018 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.
#

# Definition 41 - The European Flood Awareness System

# isFillup can be 0, 1 or missing
# When it is 0, it means "Water Balance"
unsigned[1] isFillup = missing() : dump, can_be_missing;
alias local.isFillup = isFillup;

# Forecast Reference Date and Time
unsigned[2] yearOfForecast   = year   : dump;
unsigned[1] monthOfForecast  = month  : dump;
unsigned[1] dayOfForecast    = day    : dump;
unsigned[1] hourOfForecast   = hour   : dump;
unsigned[1] minuteOfForecast = minute : dump;
constant    secondOfForecast = 0;
meta dateOfForecast  g2date(yearOfForecast,monthOfForecast, dayOfForecast) : dump;
meta timeOfForecast  time  (hourOfForecast,minuteOfForecast,secondOfForecast) : dump;

# Calculate the Julian number for the forecast date and time.
# This will be a floating point number with units of 'day'
meta julianForecastDay julian_day(dateOfForecast,hourOfForecast,minuteOfForecast,secondOfForecast): hidden;
# Calculate the difference between the forecast date and reference date
transient diffInDays = (julianForecastDay - julianDay) : hidden;   # float

# Now convert this to hours. First convert to minutes then round up
transient diffInHours = (diffInDays * 1440 + 0.5)/60 : hidden;
meta _anoffset round(diffInHours, 10): dump,long_type;
transient anoffset = _anoffset; # needed to force anoffset to be integer
alias local.anoffset = anoffset;

# ECC-662
unsigned[2] anoffsetFirst     = missing(): dump, can_be_missing;
unsigned[2] anoffsetLast      = missing(): dump, can_be_missing;
unsigned[2] anoffsetFrequency = missing(): dump, can_be_missing;

# Boolean
transient is_efas = 1;
transient lsdate_bug = 1: hidden; # See ECC-707

# Note: the key typeOfPostProcessing is in the PDTNs 70, 71, 72 and 73
concept efas_post_proc {
  "unknown"           = { typeOfPostProcessing=0  ; }
  "lisflood"          = { typeOfPostProcessing=1  ; }
  "lisflood_eric"     = { typeOfPostProcessing=2  ; }
  "lisflood_season"   = { typeOfPostProcessing=3  ; }
  "lisflood_merged"   = { typeOfPostProcessing=4  ; }
  "ericha"            = { typeOfPostProcessing=51 ; }
  "htessel_lisflood"  = { typeOfPostProcessing=101; }
  "htessel_eric"      = { typeOfPostProcessing=102; }
  "htessel_camaflood" = { typeOfPostProcessing=103; }
  "epic"              = { typeOfPostProcessing=152; }
  "unknown"           = { dummy = 1; }
} : hidden;

#Domain. Missing, local or global
#codetable[1] efasDomain "grib2/tables/local/ecmf/efas_domain.table" = 255 : dump, string_type;
#unsigned[1] efas_domain = missing() : can_be_missing, dump;
#concept efasDomain(unknown) {
#  "local"   = { efas_domain = 0; }
#  "global"  = { efas_domain = 1; }
#} : hidden;

# Reanalysis Date and Time
# This date is the date of the new official implementation of the EFAS cycle.
unsigned[2] yearOfReanalysis    = year   : dump;
unsigned[1] monthOfReanalysis   = month  : dump;
unsigned[1] dayOfReanalysis     = day    : dump;
unsigned[1] hourOfReanalysis    = hour   : dump;
unsigned[1] minuteOfReanalysis  = minute : dump;
constant    secondOfReanalysis = 0;
meta dateOfReanalysis  g2date(yearOfReanalysis,monthOfReanalysis, dayOfReanalysis) : dump;
meta timeOfReanalysis  time  (hourOfReanalysis,minuteOfReanalysis,secondOfReanalysis) : dump;

# Note: the key inputOriginatingCentre is in the PDTNs 70, 71, 72 and 73
#concept efas_forecast {
#  "griddedobs" = { inputOriginatingCentre=98; marsType = "an"; }
#  # "reanalysis" = { inputOriginatingCentre=98; marsType = "an"; }
#
#  "hres" = { inputOriginatingCentre=98; marsType = "fc"; } # deterministic
#  "ens"  = { inputOriginatingCentre=98; marsType = "pf"; } # ensemble
#  "ens"  = { inputOriginatingCentre=98; marsType = "cf"; } # ensemble
#
# DWD rules
#  "global"  = { inputOriginatingCentre=78; inputProcessIdentifier = 1; }
#  "lam"     = { inputOriginatingCentre=78; inputProcessIdentifier = 2; }
#
# TODO: For now anything coming from cnmc (COSMO) is local area
#  "lam"     = { inputOriginatingCentre=80; }
#
#  "unknown" = { dummy = 1; }
#} : hidden;
