# 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.
#

# START grib2::section
# SECTION 3, GRID DEFINITION SECTION
#  Length of section in octets

# For grib2 -> 1
constant gridDescriptionSectionPresent = 1;
position offsetSection3;

section_length[4] section3Length ;
meta section3Pointer section_pointer(offsetSection3,section3Length,3);

#  Number of section
unsigned[1] numberOfSection = 3 :read_only;

#  Source of grid definition
# NOTE 1 NOT FOUND
codetable[1] sourceOfGridDefinition ('3.0.table',masterDir,localDir) ;

#  Number of data points
unsigned[4] numberOfDataPoints  : dump;
alias numberOfPoints=numberOfDataPoints;

#  Number of octets for optional list of numbers defining number of points
# NOTE 2 NOT FOUND
unsigned[1] numberOfOctectsForNumberOfPoints;

#  Interpretation of list of numbers defining number of points
codetable[1] interpretationOfNumberOfPoints ('3.11.table',masterDir,localDir)  : dump;

if(numberOfOctectsForNumberOfPoints  == 0){
  transient PLPresent = 0 ;
}else{
  transient PLPresent = 1 ;
}

codetable[2] gridDefinitionTemplateNumber ('3.1.table',masterDir,localDir) =0 : dump,edition_specific;
meta gridDefinitionDescription codetable_title(gridDefinitionTemplateNumber);

alias is_rotated_grid=zero;

template gridDefinitionSection "grib2/template.3.[gridDefinitionTemplateNumber:l].def";

if(PLPresent){
  if(numberOfOctectsForNumberOfPoints == 1){
   unsigned[1] pl[Nj] : dump;
  }
  if(numberOfOctectsForNumberOfPoints == 2){
   unsigned[2] pl[Nj] : dump;
  }
  if(numberOfOctectsForNumberOfPoints == 3){
   unsigned[3] pl[Nj] : dump;
  }
  alias geography.pl=pl;
}

when (PLPresent == 0) {
  set numberOfOctectsForNumberOfPoints = 0;
  set interpretationOfNumberOfPoints = 0;
}

section_padding section3Padding : read_only;

concept gridType {
 "regular_ll"            = { gridDefinitionTemplateNumber=0;  PLPresent=0;  }
 "reduced_ll"            = { gridDefinitionTemplateNumber=0;  PLPresent=1;  }
 "rotated_ll"            = { gridDefinitionTemplateNumber=1;  PLPresent=0;  }
 "stretched_ll"          = { gridDefinitionTemplateNumber=2;  PLPresent=0;  }
 "stretched_rotated_ll"  = { gridDefinitionTemplateNumber=3;  PLPresent=0;  }
 "mercator"              = { gridDefinitionTemplateNumber=10; PLPresent=0;  }
 "transverse_mercator"   = { gridDefinitionTemplateNumber=12; PLPresent=0;  }
 "polar_stereographic"   = { gridDefinitionTemplateNumber=20; PLPresent=0;  }
 "lambert"               = { gridDefinitionTemplateNumber=30; PLPresent=0;  }
 "albers"                = { gridDefinitionTemplateNumber=31; PLPresent=0;  }

 "regular_gg"            = { gridDefinitionTemplateNumber=40; PLPresent=0;  }
 "reduced_gg"            = { gridDefinitionTemplateNumber=40; PLPresent=1; numberOfOctectsForNumberOfPoints=2;iDirectionIncrementGiven=0;numberOfPointsAlongAParallel = missing(); }

 "rotated_gg"            = { gridDefinitionTemplateNumber=41; PLPresent=0;  }
 "reduced_rotated_gg"    = { gridDefinitionTemplateNumber=41; PLPresent=1; numberOfOctectsForNumberOfPoints=2;iDirectionIncrementGiven=0;numberOfPointsAlongAParallel = missing(); }

 "stretched_gg"          = { gridDefinitionTemplateNumber=42; PLPresent=0;  }
 "reduced_stretched_gg"  = { gridDefinitionTemplateNumber=42; PLPresent=1; numberOfOctectsForNumberOfPoints=2;iDirectionIncrementGiven=0;numberOfPointsAlongAParallel = missing(); }

 "stretched_rotated_gg"  = { gridDefinitionTemplateNumber=43; PLPresent=0;  }
 "reduced_stretched_rotated_gg" = { gridDefinitionTemplateNumber=43; PLPresent=1; numberOfOctectsForNumberOfPoints=2;iDirectionIncrementGiven=0;numberOfPointsAlongAParallel = missing(); }

# For consistency add the prefix regular_
"regular_rotated_gg"           = { gridDefinitionTemplateNumber=41; PLPresent=0; } # = rotated_gg
"regular_stretched_gg"         = { gridDefinitionTemplateNumber=42; PLPresent=0; } # = stretched_gg
"regular_stretched_rotated_gg" = { gridDefinitionTemplateNumber=43; PLPresent=0; } # = stretched_rotated_gg

 "sh"                    = { gridDefinitionTemplateNumber=50; PLPresent=0;}
 "rotated_sh"            = { gridDefinitionTemplateNumber=51; PLPresent=0;}
 "stretched_sh"          = { gridDefinitionTemplateNumber=52; PLPresent=0;}
 "stretched_rotated_sh"  = { gridDefinitionTemplateNumber=53; PLPresent=0;}
 "space_view"            = { gridDefinitionTemplateNumber=90; PLPresent=0;}
 "triangular_grid"       = { gridDefinitionTemplateNumber=100;PLPresent=0;}
 "unstructured_grid"     = { gridDefinitionTemplateNumber=101;PLPresent=0;}
 "equatorial_azimuthal_equidistant" = { gridDefinitionTemplateNumber=110; PLPresent=0;}
 "azimuth_range"         = { gridDefinitionTemplateNumber=120;PLPresent=0; }
 "irregular_latlon"      = { gridDefinitionTemplateNumber=130;PLPresent=0; }
 "lambert_azimuthal_equal_area"= { gridDefinitionTemplateNumber=140;PLPresent=0; }
 "cross_section"         = { gridDefinitionTemplateNumber=1000;PLPresent=0; }
 "Hovmoller"             = { gridDefinitionTemplateNumber=1100;PLPresent=0; }
 "time_section"          = { gridDefinitionTemplateNumber=1200;PLPresent=0; }
 "unknown"               = {PLPresent=0;}
 "unknown_PLPresent"     = {PLPresent=1;}
} : dump;

alias ls.gridType=gridType;
alias geography.gridType=gridType;
alias typeOfGrid=gridType;

meta md5Section3 md5(offsetSection3,section3Length);
alias md5GridSection = md5Section3;

