# 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 2/template.3.20 ----------------------------------------------------------------------
# TEMPLATE 3.20, Polar stereographic projection
include "grib2/template.3.shape_of_the_earth.def";
transient oneThousand=1000;

#  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 geography.Ny=Ny;

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

#  Lo1 - longitude of first grid point
unsigned[4] longitudeOfFirstGridPoint : edition_specific;
meta geography.longitudeOfFirstGridPointInDegrees scale(longitudeOfFirstGridPoint,oneConstant,grib3divider,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,grib3divider,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,grib3divider,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/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);


# END   2/template.3.20 ----------------------------------------------------------------------
