Generalized Grids for Data Extraction
Not all longitude-latitude grids are rectilinear in the lon/lat space. RegionGrid
classifies this type as a GeneralizedGrid
. Examples of such datasets include:
Level 2 products from the Global Precipitation Measurement Mission
Model output from climate models such as Weather Research & Forecasting Model (WRF)
Basically, for each of these datasets, the data is given in such a way that the coordinates of the grid can be expressed via:
- A 2D array of
Point2
types, with eachPoint2
type containing (lon,lat)
using GeoRegions
using RegionGrids
using CairoMakie
Creating Generalized Grids
A Generalized Grid can be created as follows:
ggrd = RegionGrid(geo,Point2.(lon,lat))
where geo
is a GeoRegion
of interest that is found within the domain defined by the longitude and latitude grid vectors.
nlon = 51; nlat = 31
lon = zeros(nlon,nlat)
lat = zeros(nlon,nlat)
for ilat = 1 : nlat, ilon = 1 : nlon
lon[ilon,ilat] = (ilon-26) * (5 + (ilat-16) * 0.1)
lat[ilon,ilat] = (ilat-16) * 5
end
geo = GeoRegion([10,100,-80,10],[50,10,-40,50])
ggrd = RegionGrid(geo,Point2.(lon,lat))
The GeneralMask type has the following properties:
Longitude Indices (ilon)
Latitude Indices (ilat)
Longitude Points (lon)
Latitude Points (lat)
Rotated X Coordinates (X)
Rotated Y Coordinates (Y)
Rotation (°) (θ) : 0.0
RegionGrid Mask (mask)
RegionGrid Weights (weights)
RegionGrid Size : 37 lon points x 17 lat points
RegionGrid Validity : 230 / 629
The API for creating a Generalized Grid can be found here
What is in a Generalized Grid?
RegionGrids.GeneralizedGrid Type
GeneralizedGrid <: RegionGrid
A GeneralizedGrid
is a RegionGrid
that is created based on longitude/latitude grids that are not rectilinear - this can range from curvilinear grids to unstructured grids. It has its own subtypes: RegionMask
and VectorMask
.
All GeneralizedGrid
type will contain the following fields:
lon
- A Matrix ofFloat
s, defining the longitudes for each point in the RegionGrid that describe the region.lat
- A Matrix ofFloat
s, defining the latitude for each point in the RegionGrid that describe the region.ilon
- A Matrix ofInt
s, defining the indices used to extract the longitude vector from the input longitude vector.ilat
- A Matrix ofInt
s, defining the indices used to extract the latitude vector from the input latitude vector.mask
- An Matrix of NaNs and 1s, defining the gridpoints in the RegionGrid where the data is valid.weights
- A Matrix ofFloat
s, defining the latitude-weights of each valid point in the grid. Will be NaN if outside the bounds of the GeoRegion used to define this RectilinearGrid.X
- A Matrix ofFloat
s, defining the X-coordinates (in meters) of each point in the "derotated" RegionGrid about the centroid for the shape of the GeoRegion.Y
- A Matrix ofFloat
s, defining the Y-coordinates (in meters) of each point in the "derotated" RegionGrid about the centroid for the shape of the GeoRegion.θ
- AFloat
storing the information on the angle (in degrees) about which the data was rotated in the anti-clockwise direction. Mathematically, it isrotation - geo.θ
.
We see that in a GeneralizedGrid
type, we have the lon
and lat
arrays that defined the longitude and latitude points that have been cropped to fit the GeoRegion bounds.
ggrd.lon
37×17 Matrix{Float64}:
-73.1 -74.8 -76.5 -78.2 -79.9 … 266.5 264.8 263.1 261.4 259.7
-68.8 -70.4 -72.0 -73.6 -75.2 272.0 270.4 268.8 267.2 265.6
-64.5 -66.0 -67.5 -69.0 -70.5 277.5 276.0 274.5 273.0 271.5
-60.2 -61.6 -63.0 -64.4 -65.8 -77.0 -78.4 -79.8 278.8 277.4
-55.9 -57.2 -58.5 -59.8 -61.1 -71.5 -72.8 -74.1 -75.4 -76.7
-51.6 -52.8 -54.0 -55.2 -56.4 … -66.0 -67.2 -68.4 -69.6 -70.8
-47.3 -48.4 -49.5 -50.6 -51.7 -60.5 -61.6 -62.7 -63.8 -64.9
-43.0 -44.0 -45.0 -46.0 -47.0 -55.0 -56.0 -57.0 -58.0 -59.0
-38.7 -39.6 -40.5 -41.4 -42.3 -49.5 -50.4 -51.3 -52.2 -53.1
-34.4 -35.2 -36.0 -36.8 -37.6 -44.0 -44.8 -45.6 -46.4 -47.2
⋮ ⋱ ⋮
47.3 48.4 49.5 50.6 51.7 60.5 61.6 62.7 63.8 64.9
51.6 52.8 54.0 55.2 56.4 66.0 67.2 68.4 69.6 70.8
55.9 57.2 58.5 59.8 61.1 … 71.5 72.8 74.1 75.4 76.7
60.2 61.6 63.0 64.4 65.8 77.0 78.4 79.8 81.2 82.6
64.5 66.0 67.5 69.0 70.5 82.5 84.0 85.5 87.0 88.5
68.8 70.4 72.0 73.6 75.2 88.0 89.6 91.2 92.8 94.4
73.1 74.8 76.5 78.2 79.9 93.5 95.2 96.9 98.6 100.3
77.4 79.2 81.0 82.8 84.6 … 99.0 100.8 102.6 104.4 106.2
81.7 83.6 85.5 87.4 89.3 104.5 106.4 108.3 110.2 112.1
ggrd.lat
37×17 Matrix{Float64}:
-35.0 -30.0 -25.0 -20.0 -15.0 … 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 … 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
⋮ ⋱ ⋮
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 … 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 … 20.0 25.0 30.0 35.0 40.0 45.0
-35.0 -30.0 -25.0 -20.0 -15.0 20.0 25.0 30.0 35.0 40.0 45.0
An example of using Generalized Grids
Say we have some sample data, here randomly generated.
data = rand(nlon,nlat)
51×31 Matrix{Float64}:
0.585288 0.732279 0.862279 … 0.986175 0.684235 0.0447014
0.811962 0.00153869 0.0425938 0.824747 0.684412 0.825468
0.913245 0.807015 0.176977 0.42597 0.808038 0.685482
0.00591397 0.526647 0.752574 0.253944 0.540465 0.546164
0.932385 0.114191 0.753108 0.878591 0.203074 0.861466
0.0135261 0.697987 0.297691 … 0.168824 0.711468 0.0247925
0.500688 0.409249 0.276857 0.469383 0.93184 0.466594
0.498454 0.321512 0.347448 0.23162 0.697587 0.627282
0.766734 0.953574 0.3335 0.0418077 0.946221 0.76243
0.315102 0.316463 0.236397 0.220869 0.354476 0.921667
⋮ ⋱ ⋮
0.460016 0.623668 0.811306 0.443598 0.71832 0.205518
0.0755606 0.416746 0.254595 0.178275 0.548486 0.512967
0.083095 0.802623 0.394095 0.626199 0.615773 0.931258
0.649111 0.822021 0.739348 … 0.0542676 0.87277 0.224159
0.843085 0.151477 0.605341 0.336206 0.277844 0.829956
0.692508 0.150892 0.212714 0.810697 0.871382 0.119814
0.13901 0.494614 0.773705 0.868366 0.292851 0.26284
0.61532 0.0581427 0.137806 0.130025 0.344132 0.972368
0.849897 0.449517 0.00662376 … 0.134048 0.708736 0.345807
We extract the valid data within the GeoRegion of interest that we defined above:
ndata = extract(data,ggrd)
37×17 Matrix{Float64}:
0.669034 NaN NaN … NaN NaN NaN NaN NaN
0.587709 NaN NaN NaN NaN NaN NaN NaN
0.774934 0.15671 NaN NaN NaN NaN NaN NaN
NaN 0.0562816 0.635678 NaN NaN NaN NaN NaN
NaN 0.602553 0.855209 NaN NaN NaN NaN NaN
NaN 0.628565 0.175293 … NaN NaN NaN NaN NaN
NaN 0.544684 0.84546 NaN NaN NaN NaN NaN
NaN 0.881892 0.792502 NaN NaN NaN NaN NaN
NaN NaN 0.805258 NaN NaN NaN NaN NaN
NaN NaN 0.527171 NaN NaN NaN NaN NaN
⋮ ⋱ ⋮
NaN NaN NaN 0.420436 NaN NaN NaN NaN
NaN NaN NaN 0.853461 NaN NaN NaN NaN
NaN NaN NaN … NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN … NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
And now let us visualize the results.
slon,slat = coordinates(geo) # extract the coordinates
fig = Figure()
ax1 = Axis(
fig[1,1],width=300,height=150,
limits=(-180,180,-90,90)
)
contourf!(ax1,lon,lat,data,levels=-1:0.2:1)
lines!(ax1,slon,slat,color=:black,linewidth=2)
lines!(ax1,slon.+360,slat,color=:black,linewidth=2,linestyle=:dash)
hidexdecorations!(ax1,ticks=false,grid=false)
ax2 = Axis(
fig[1,2],width=300,height=150,
limits=(-180,180,-90,90)
)
contourf!(ax2,ggrd.lon,ggrd.lat,ndata,levels=-1:0.2:1)
lines!(ax2,slon,slat,color=:black,linewidth=2)
Label(fig[2,:],"Longitude / º")
Label(fig[:,0],"Latitude / º",rotation=pi/2)
resize_to_layout!(fig)
fig