# (C) Copyright 2005- ECMWF.

# TEMPLATE 3.20, Polar stereographic projection
include "grib2/templates/template.3.shape_of_the_earth.def"

constant isGridded = true;

# Nx - number of points along X-axis
unsigned[4] Nx : dump;
alias Ni = Nx;
alias numberOfPointsAlongXAxis = Nx;
alias geography.Nx=Nx;

# Ny - number of points along Y-axis
unsigned[4] Ny  : dump;
alias Nj = Ny;
alias numberOfPointsAlongYAxis = Ny;
alias numberOfPointsAlongAMeridian = Nj;
alias geography.Ny=Ny;

# La1 - latitude of first grid point
signed[4] latitudeOfFirstGridPoint : edition_specific ;
meta geography.latitudeOfFirstGridPointInDegrees scale(latitudeOfFirstGridPoint,oneConstant,grib2divider,truncateDegrees) : dump;
alias La1 =  latitudeOfFirstGridPoint;

# Lo1 - longitude of first grid point
unsigned[4] longitudeOfFirstGridPoint : edition_specific;
meta geography.longitudeOfFirstGridPointInDegrees scale(longitudeOfFirstGridPoint,oneConstant,grib2divider,truncateDegrees) : dump;
alias Lo1 =  longitudeOfFirstGridPoint;

# Resolution and component flag
flags[1] resolutionAndComponentFlags 'grib2/tables/[tablesVersion]/3.3.table' : dump;

# LaD - Latitude where Dx and Dy are specified
signed[4] LaD  : edition_specific;
alias latitudeWhereDxAndDyAreSpecified=LaD;
meta geography.LaDInDegrees scale(LaD,oneConstant,grib2divider,truncateDegrees) : dump;
alias latitudeWhereDxAndDyAreSpecifiedInDegrees=LaDInDegrees;

# LoV - orientation of the grid
# LoV is the longitude value of the meridian which is parallel to the y-axis (or columns of the grid)
# along which latitude increases as the y-coordinate increases
signed[4] orientationOfTheGrid  : edition_specific;
alias LoV =  orientationOfTheGrid ;
meta geography.orientationOfTheGridInDegrees scale(orientationOfTheGrid,oneConstant,grib2divider,truncateDegrees) : dump;

# Dx - X-direction grid length
# Grid length is in units of 10-3 m at the latitude specified by LaD
unsigned[4] Dx  : edition_specific;
meta geography.DxInMetres scale(Dx,one,thousand,truncateDegrees) : dump;
alias xDirectionGridLength=Dx;

# Dy - Y-direction grid length
# Grid length is in units of 10-3 m at the latitude specified by LaD
unsigned[4] Dy  : edition_specific;
meta geography.DyInMetres scale(Dy,one,thousand,truncateDegrees) : dump;
alias yDirectionGridLength=Dy;

# Projection centre flag
flags[1] projectionCentreFlag 'grib2/tables/[tablesVersion]/3.5.table' : dump;
# Note our flagbit numbers go from 7 to 0, while WMO convention is from 1 to 8
# If bit 1 is 0, then the North Pole is on the projection plane
# If bit 1 is 1, then the South Pole is on the projection plane
flagbit southPoleOnProjectionPlane(projectionCentreFlag,7) : dump;   # WMO bit 1

include "grib2/templates/template.3.scanning_mode.def"

iterator polar_stereographic(numberOfPoints,missingValue,values,
          radius,Nx,Ny,
          latitudeOfFirstGridPointInDegrees,longitudeOfFirstGridPointInDegrees,
          southPoleOnProjectionPlane,
          orientationOfTheGridInDegrees,
          LaDInDegrees,
          DxInMetres,DyInMetres,
          iScansNegatively, jScansPositively,
          jPointsAreConsecutive, alternativeRowScanning);

nearest polar_stereographic(values,radius,Nx,Ny);

meta latLonValues latlonvalues(values);
alias latitudeLongitudeValues=latLonValues;
meta latitudes latitudes(values,0);
meta longitudes longitudes(values,0);
meta distinctLatitudes latitudes(values,1);
meta distinctLongitudes longitudes(values,1);
