# Copyright 2005- ECMWF.

position offsetSection1;
# GRIB-API 1.16.0 length[4] section1Length ;
section_length[4] section1Length ;
meta section1Pointer section_pointer(offsetSection1,section1Length,1);

unsigned[1] numberOfSection = 1 :read_only;

#codetable[2] centre  'grib2/centre.table' : dump,string_type;
codetable[2] centre  'common/c-11.table' : dump,string_type;
alias identificationOfOriginatingGeneratingCentre=centre;
meta centreDescription codetable_title(centre);


alias parameter.centre=centre;
alias ls.centre=centre;
alias originatingCentre=centre;


unsigned[2] subCentre  : dump;

_if (subCentre==98 ) {
alias centreForLocal=subCentre;
} else {
alias centreForLocal=centre;
}

#MasterTablesVersion
#codetable[1] tablesVersion 'grib2/tables/1.0.table' = tablesVersionLatest : edition_specific;
codetable[1] tablesVersion 'grib2/tables/1.0.table' = 19 : edition_specific;
alias gribMasterTablesVersionNumber=tablesVersion;

transient masterDir="grib2/tables/[tablesVersion]";
if (tablesVersion > tablesVersionLatest) {
  transient masterDir="grib2/tables/[tablesVersionLatest]";
} 
when (tablesVersion!=255) {
  set masterDir="grib2/tables/[tablesVersion]";
} else {
  set masterDir="grib2/tables/4";
}

#codetable[1] localTablesVersion 'grib2/tables/[tablesVersion]/1.1.table' = 1 ;
codetable[1] localTablesVersion 'grib2/tables/local/[centreForLocal]/1.1.table' = 1 ;
alias versionNumberOfGribLocalTables=localTablesVersion;

transient localDir="";
#if (localTablesVersion != 0) {
if (localTablesVersion != 0 and localTablesVersion != 255) {
  transient localDir="grib2/tables/local/[centre]/[localTablesVersion]";
}

# Significance of Reference Time
codetable[1] significanceOfReferenceTime ('1.2.table',masterDir,localDir) = 1 : dump;

# Year
# (4 digits)
unsigned[2] year ;

# Month
unsigned[1] month ;

# Day
unsigned[1] day ;

# Hour
unsigned[1] hour ;

# Minute
unsigned[1] minute ;

# Second
unsigned[1] second ;

meta    dataDate    g2date(year,month,day) : dump;
alias mars.date = dataDate;
alias ls.date = dataDate;

meta julianDay julian_day(dataDate,hour,minute,second) : edition_specific;

meta      dataTime    time(hour,minute,second) : dump;
alias mars.time = dataTime;


meta dateTimeL evaluate(date*10000 + time ) ;
meta dateTime sprintf("%d",dateTimeL):  no_copy, dump;

# Production status of processed data in this GRIB message
codetable[1] productionStatusOfProcessedData ('1.3.table',masterDir,localDir) : dump;

# Type of processed data in this GRIB message
codetable[1] typeOfProcessedData ('1.4.table',masterDir,localDir) = 255  : dump,string_type,no_fail;

alias ls.dataType=typeOfProcessedData;

meta md5Section1 md5(offsetSection1,section1Length);

meta selectStepTemplateInterval select_step_template(productDefinitionTemplateNumber,0); # 0 -> not instant
meta selectStepTemplateInstant  select_step_template(productDefinitionTemplateNumber,1); # 1 -> instant

transient stepTypeInternal="instant" : hidden,no_copy;

concept stepType {
    "instant" = {selectStepTemplateInstant=1; stepTypeInternal="instant";}
    "avg"     = {selectStepTemplateInterval=1; stepTypeInternal="avg";}
    "avgd"    = {selectStepTemplateInterval=1; stepTypeInternal="avgd";}
    "accum"   = {selectStepTemplateInterval=1; stepTypeInternal="accum";}
    "max"     = {selectStepTemplateInterval=1; stepTypeInternal="max";}
    "min"     = {selectStepTemplateInterval=1; stepTypeInternal="min";}
    "diff"    = {selectStepTemplateInterval=1; stepTypeInternal="diff";}
    "sdiff"   = {selectStepTemplateInterval=1; stepTypeInternal="sdiff";}
    "rms"     = {selectStepTemplateInterval=1; stepTypeInternal="rms";}
    "sd"      = {selectStepTemplateInterval=1; stepTypeInternal="sd";}
    "cov"     = {selectStepTemplateInterval=1; stepTypeInternal="cov";}
    "ratio"   = {selectStepTemplateInterval=1; stepTypeInternal="ratio";}
    "stdanom" = {selectStepTemplateInterval=1; stepTypeInternal="stdanom";}
    "sum"     = {selectStepTemplateInterval=1; stepTypeInternal="sum";}
}

# 0=atmospheric chemical constituents
# 1=atmospheric chemical constituents based on a distribution function
# 2=atmospheric chemical constituents with source or sink
meta is_chemical         g2_chemical(productDefinitionTemplateNumber, stepType, 0);
meta is_chemical_distfn  g2_chemical(productDefinitionTemplateNumber, stepType, 1);
meta is_chemical_srcsink g2_chemical(productDefinitionTemplateNumber, stepType, 2);

# 0=aerosol
# 1=optical properties of aerosol
meta is_aerosol          g2_aerosol(productDefinitionTemplateNumber, stepType, 0);
meta is_aerosol_optical  g2_aerosol(productDefinitionTemplateNumber, stepType, 1);

transient setCalendarId = 0 ;
transient deleteCalendarId = 0 ;
alias calendarIdPresent = zero;
if ( ((section1Length > 21) or setCalendarId > 0) and deleteCalendarId == 0) {
  alias calendarIdPresent = present;
  codetable[2] calendarIdentificationTemplateNumber ('1.5.table',masterDir,localDir) : dump,string_type,no_fail;
  template calendarIdentification "grib2/template.1.[calendarIdentificationTemplateNumber:l].def";
}

# DWD - ab Version 2.24.0 nicht mehr in ecmwf definitions - auskommentiert - loeschen mit 2.25.0
# concept is_uerra(zero) {
#   '1' = {productionStatusOfProcessedData=10;}
#   '1' = {productionStatusOfProcessedData=11;}
#   '1' = {productionStatusOfProcessedData=9;}
#   '1' = {productionStatusOfProcessedData=8;}
#   '0' = {dummy=1;}
# }

constant conceptsMasterDir="grib2" : hidden;
constant conceptsLocalDirAll="grib2/localConcepts/[centre:s]" : hidden;
constant conceptsLocalDirECMF="grib2/localConcepts/ecmf" : hidden;


# ab Version 2.20
# ECC-806: Local concepts precedence order
if (preferLocalConcepts) {
  constant conceptsDir1 = conceptsMasterDir : hidden;
  constant conceptsDir2 = conceptsLocalDirAll : hidden;
} else {
  constant conceptsDir1 = conceptsLocalDirAll : hidden;
  constant conceptsDir2 = conceptsMasterDir : hidden;
}

