# GRID DEFINITION Polar stereographic
# grib 1 -> 2
constant gridDefinitionTemplateNumber = 20;

unsigned[2] Nx  : dump;
alias Ni =  Nx;
alias numberOfPointsAlongXAxis = Nx;
alias geography.Nx=Nx;

unsigned[2] Ny  : dump;
alias Nj =  Ny;
alias numberOfPointsAlongYAxis = Ny;
alias geography.Ny=Ny;

signed[3] latitudeOfFirstGridPoint : edition_specific ;
meta geography.latitudeOfFirstGridPointInDegrees scale(latitudeOfFirstGridPoint,oneConstant,grib1divider,truncateDegrees) : dump;
alias La1 = latitudeOfFirstGridPoint;

signed[3] longitudeOfFirstGridPoint : edition_specific;
meta geography.longitudeOfFirstGridPointInDegrees scale(longitudeOfFirstGridPoint,oneConstant,grib1divider,truncateDegrees) : dump;
alias Lo1 = longitudeOfFirstGridPoint;

include "grib1/resolution_flags.def";

# LoV - orientation of the grid; i.e.  the longitude value of the meridian which is parallel to the Y-axis
signed[3] orientationOfTheGrid : edition_specific, no_copy ;
meta geography.orientationOfTheGridInDegrees scale(orientationOfTheGrid,oneConstant,grib1divider,truncateDegrees) : dump;
alias LoV = orientationOfTheGrid ;

# Dx - X-direction grid length
unsigned[3] DxInMetres  : dump;
alias xDirectionGridLengthInMetres=DxInMetres;
alias Dx=DxInMetres;
alias geography.DxInMetres=DxInMetres;
alias Di = DxInMetres;

# Dy - Y-direction grid length
unsigned[3] DyInMetres  : dump;
alias yDirectionGridLengthInMetres=DyInMetres;
alias Dy = DyInMetres;
alias Dj = DyInMetres;
alias geography.DyInMetres=DyInMetres;

constant latitudeWhereDxAndDyAreSpecifiedInDegrees=60;
constant LaDInDegrees=60;
alias geography.LaDInDegrees=LaDInDegrees;

# Projection centre flag
unsigned[1] projectionCentreFlag : dump ;
alias projectionCenterFlag=projectionCentreFlag;
# 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
flagbit bipolarAndSymmetricProjection(projectionCentreFlag,6) : dump;  # WMO bit 2

# for change_scanning_direction
alias yFirst=latitudeOfFirstGridPointInDegrees;
alias xFirst=longitudeOfFirstGridPointInDegrees;

include "grib1/scanning_mode.def";

pad padding_grid5_1(4);

meta numberOfDataPoints number_of_points(Nx,Ny,PLPresent,pl) : dump;
alias numberOfPoints=numberOfDataPoints;
meta numberOfValues number_of_values(values,bitsPerValue,numberOfDataPoints,bitmapPresent,bitmap,numberOfCodedValues) : dump;
#alias ls.valuesCount=numberOfValues;

iterator polar_stereographic(numberOfPoints,missingValue,values,
          radius,Nx,Ny,
          latitudeOfFirstGridPointInDegrees,longitudeOfFirstGridPointInDegrees,
          southPoleOnProjectionPlane,
          orientationOfTheGridInDegrees,
          LaDInDegrees,
          Dx,Dy,
          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);
