Unstructured Grids for Data Extraction
There are also RegionGrid types without an actual grid, maybe there are a set of coordinates and geometries that define the corners or the centres of a mesh, such as:
- Model output from climate models such as cubed-sphere mesh output of the Community Earth Systems Model 2 (CESM2).
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 Vector of
Point2types, with eachPoint2type containing (lon,lat)
using GeoRegions
using RegionGrids
using CairoMakieCreating Unstructured Grids
A Unstructured 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.
lon = collect(10:20:360); nlon = length(lon)
lat = collect(-80:20:90); nlat = length(lat)
glon = zeros(nlon,nlat); glon .= lon; glon = glon[:]
glat = zeros(nlon,nlat); glat .= lat'; glat = glat[:]
plon = glon .+ 14rand(nlon*nlat) .- 7
plat = glat .+ 14rand(nlon*nlat) .- 7
geo = GeoRegion([10,100,-80,10],[50,10,-40,50])
iggrd = RegionGrid(geo,Point2.(glon,glat))
pggrd = RegionGrid(geo,Point2.(plon,plat))The VectorMask Grid type has the following properties:
Indices (ipoint) : [51, 55, 70, 71, 72, 73, 74, 75, 90, 91, 92, 93, 94, 108, 109]
Longitude Points (lon) : [-68.24406787366178, 4.001122214122667, -43.73345988981572, -28.284722405991374, -13.368821578482311, 4.036629078661573, 36.63176496594365, 47.22430306714027, -10.93308206883654, 10.504098092238973, 30.437873868054226, 46.762004631320664, 65.34644562751859, -13.009610940093523, 3.045101789724331]
Latitude Points (lat) : [-34.92730103496109, -14.863442634580185, -14.944662109300994, -16.44153594814545, -21.31073796876577, 0.7579686004540349, -5.524369726875455, -1.29296558853156, -1.4052908007288423, 26.56362646132839, 22.693828007190596, 17.034476179242695, 13.69831758318783, 24.172641407868177, 37.76480707736063]
Rotated X Coordinates (X)
Rotated Y Coordinates (Y)
Rotation (°) (θ) : 0.0
RegionGrid Weights (weights)
RegionGrid Size : (15,) pointsThe API for creating a Unstructured Grid can be found here
What is in a Unstructured Grid?
RegionGrids.UnstructuredGrid Type
UnstructuredGrid <: RegionGridA UnstructuredGrid is a RegionGrid that is created based on an unstructured grid often used in cubed-sphere or unstructured-mesh grids.
All UnstructuredGrid type will contain the following fields:
lon- A Vector ofFloats, defining the longitudes for each point in the RegionGrid that describe the region.lat- A Vector ofFloats, defining the latitude for each point in the RegionGrid that describe the region.ipoint- A Vector ofInts, defining the indices of the valid points from the original unstructured grid that were extracted into the RegionGrid.weights- A Vector ofFloats, 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 Vector ofFloats, defining the X-coordinates (in meters) of each point in the "derotated" RegionGrid about the centroid for the shape of the GeoRegion.Y- A Vector ofFloats, defining the Y-coordinates (in meters) of each point in the "derotated" RegionGrid about the centroid for the shape of the GeoRegion.θ- AFloatstoring 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 UnstructuredGrid type, we have the lon and lat vectors that defined the perturbed longitude and latitude points that are within the GeoRegion.
pggrd.lon15-element Vector{Float64}:
-68.24406787366178
4.001122214122667
-43.73345988981572
-28.284722405991374
-13.368821578482311
4.036629078661573
36.63176496594365
47.22430306714027
-10.93308206883654
10.504098092238973
30.437873868054226
46.762004631320664
65.34644562751859
-13.009610940093523
3.045101789724331pggrd.lat15-element Vector{Float64}:
-34.92730103496109
-14.863442634580185
-14.944662109300994
-16.44153594814545
-21.31073796876577
0.7579686004540349
-5.524369726875455
-1.29296558853156
-1.4052908007288423
26.56362646132839
22.693828007190596
17.034476179242695
13.69831758318783
24.172641407868177
37.76480707736063An example of using Unstructured Grids
Say we have some sample data, here randomly generated.
data = rand(nlon,nlat)[:]162-element Vector{Float64}:
0.3478415286387807
0.7409595510339312
0.7036897096177958
0.2455304335123396
0.946295027233593
0.792616639538769
0.3694604456085562
0.9920417685517259
0.6599081036754361
0.6652015968995164
⋮
0.16342440816846215
0.3107232389489485
0.028283301903656244
0.4074328874905664
0.6289720684155027
0.19764277289284793
0.49539285902055974
0.19126118407592252
0.9533035799896278We extract the valid data within the GeoRegion of interest that we defined above:
ndata = extract(data,iggrd)
pdata = extract(data,pggrd)15-element Vector{Float64}:
0.9065563425649826
0.20896651049501969
0.10107399443318055
0.03420930081188911
0.4643736510089548
0.25617903329721536
0.3586287285390325
0.5032041275424867
0.28367820889492734
0.41604663563403455
0.3623993460508096
0.8677671111619495
0.8747664293850191
0.8091671700594689
0.10565052424380283And now let us visualize the results.
slon,slat = coordinates(geo) # extract the coordinates
fig = Figure()
ax1 = Axis(
fig[1,1],width=450,height=150,
limits=(-180,360,-90,90)
)
scatter!(ax1,glon,glat,color=:lightgrey)
scatter!(ax1,plon,plat,color=data)
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[2,1],width=450,height=150,
limits=(-180,360,-90,90)
)
scatter!(ax2,iggrd.lon,iggrd.lat,color=:lightgrey)
scatter!(ax2,pggrd.lon,pggrd.lat,color=pdata)
lines!(ax2,slon,slat,color=:black,linewidth=2)
Label(fig[3,:],"Longitude / º")
Label(fig[:,0],"Latitude / º",rotation=pi/2)
resize_to_layout!(fig)
fig