How do you use RegionGrids.jl?
The Basic Outline
In practice, we would use GeoRegions and RegionGrids as follows:
using GeoRegions
using RegionGrids
# 1. Get gridded data, and longitude/latitude
data = ...
dlon = ... # longitudes for gridded data points
dlat = ... # latitudes for gridded data points
# 2. Define a Geographic Region using a shape defined by vectors for longitude and latitude respectively
geo = GeoRegion(lon,lat)
# 3. Create a RegionGrid for data extraction using the GeoRegion, and longitude and latitude vectors
ggrd = RegionGrid(geo,dlon,dlat)
# 4. Extract the data within the GeoRegion of interest
longitude and latitude vectors respectively
ndata = extract(data,ggrd)
newlon = ggrd.lon
newlat = ggrd.lat
An Example
Setup
julia
using GeoRegions
using RegionGrids
using DelimitedFiles
using CairoMakie
download("https://raw.githubusercontent.com/natgeo-wong/GeoPlottingData/main/coastline_resl.txt","coast.cst")
coast = readdlm("coast.cst",comments=true)
clon = coast[:,1]
clat = coast[:,2]
nothing
Defining some data
julia
lon = collect(0:5:360); nlon = length(lon)
lat = collect(-90:5:90); nlat = length(lat)
data = rand(nlon,nlat)
73×37 Matrix{Float64}:
0.0255708 0.108168 0.76062 … 0.10408 0.595314 0.13293
0.0144397 0.412264 0.720294 0.708476 0.502774 0.905723
0.199283 0.345302 0.0328054 0.265496 0.0721799 0.808881
0.215164 0.0671171 0.726652 0.298121 0.688559 0.726663
0.35091 0.582636 0.373856 0.166318 0.371273 0.332584
0.0997439 0.237766 0.898331 … 0.0648232 0.434319 0.0774538
0.842199 0.486945 0.763426 0.839653 0.0813824 0.84755
0.090404 0.150654 0.400154 0.254127 0.563374 0.70994
0.426101 0.793307 0.477778 0.912182 0.733734 0.658121
0.588519 0.0521681 0.242416 0.52234 0.714162 0.8913
⋮ ⋱ ⋮
0.348419 0.786959 0.416303 0.914713 0.926257 0.256063
0.624028 0.541507 0.0323194 … 0.984309 0.649296 0.119236
0.896583 0.790993 0.0579354 0.120394 0.782208 0.180637
0.404228 0.894182 0.535221 0.146898 0.425613 0.826709
0.0296038 0.89512 0.33478 0.875853 0.76477 0.238139
0.699084 0.607525 0.851289 0.390613 0.852597 0.92864
0.77463 0.0398206 0.872765 … 0.138129 0.0940104 0.198267
0.504122 0.915837 0.967923 0.908011 0.265222 0.375767
0.612794 0.244678 0.97489 0.597988 0.879673 0.967676
Defining a Region of Interest
Next, we proceed to define a GeoRegion and extract its coordinates:
julia
geo = GeoRegion([10,230,-50,10],[50,10,-40,50])
slon,slat = coordinates(geo) # extract the coordinates
([10.0, 230.0, -50.0, 10.0], [50.0, 10.0, -40.0, 50.0])
Let us create a RegionGrid for Data Extraction
Following which, we can define a RegionGrid:
julia
ggrd = RegionGrid(geo,lon,lat)
The RLinearMask Grid type has the following properties:
Longitude Indices (ilon) : [63, 64, 65, 66, 67, 68, 69, 70, 71, 72 … 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]
Latitude Indices (ilat) : [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
Longitude Points (lon) : [-50, -45, -40, -35, -30, -25, -20, -15, -10, -5 … 185, 190, 195, 200, 205, 210, 215, 220, 225, 230]
Latitude Points (lat) : [-40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Rotated X Coordinates (X)
Rotated Y Coordinates (Y)
Rotation (°) (θ) : 0.0
RegionGrid Mask (mask)
RegionGrid Weights (weights)
RegionGrid Size : 58 lon points x 19 lat points
RegionGrid Validity : 465 / 1102
And then use this RegionGrid to extract data for the GeoRegion of interest:
julia
ndata = extract(data,ggrd)
58×19 Matrix{Float64}:
0.407313 NaN NaN … NaN NaN NaN NaN NaN NaN
NaN 0.714033 NaN NaN NaN NaN NaN NaN NaN
NaN 0.186752 0.287566 NaN NaN NaN NaN NaN NaN
NaN 0.466394 0.764523 NaN NaN NaN NaN NaN NaN
NaN 0.138879 0.819162 NaN NaN NaN NaN NaN NaN
NaN 0.913627 0.449649 … NaN NaN NaN NaN NaN NaN
NaN NaN 0.109334 NaN NaN NaN NaN NaN NaN
NaN NaN 0.756818 NaN NaN NaN NaN NaN NaN
NaN NaN 0.308987 NaN NaN NaN NaN NaN NaN
NaN NaN 0.28575 0.630523 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 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
Data Visualization
julia
fig = Figure()
ax1 = Axis(
fig[1,1],width=400,height=200,
limits=(0,360,-90,90)
)
heatmap!(ax1,lon,lat,data,colorrange=(-1,1))
lines!(ax1,slon,slat,color=:black,linewidth=5)
lines!(ax1,slon.+360,slat,color=:black,linewidth=5)
ax2 = Axis(
fig[2,1],width=400,height=200,
limits=(0,360,-90,90)
)
heatmap!(ax2,ggrd.lon,ggrd.lat,ndata,colorrange=(-1,1))
heatmap!(ax2,ggrd.lon.+360,ggrd.lat,ndata,colorrange=(-1,1))
lines!(ax2,slon,slat,color=:black,linewidth=5)
lines!(ax2,slon.+360,slat,color=:black,linewidth=5)
resize_to_layout!(fig)
fig