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.520607 0.656478 0.8623 0.364012 … 0.407073 0.10893 0.499349
0.685958 0.170849 0.584417 0.431761 0.467307 0.315705 0.294867
0.651259 0.536291 0.507484 0.148737 0.608901 0.515085 0.174403
0.162005 0.770139 0.790914 0.729053 0.704964 0.917222 0.405027
0.79426 0.767143 0.630531 0.679615 0.216706 0.64735 0.326352
0.121802 0.586907 0.86278 0.190179 … 0.257755 0.888234 0.0607443
0.556957 0.481187 0.293685 0.405705 0.919959 0.384567 0.783191
0.81301 0.31472 0.407746 0.530773 0.90862 0.974702 0.490176
0.810525 0.3017 0.450825 0.377438 0.3316 0.647843 0.233956
0.448305 0.0385708 0.336562 0.0236978 0.996079 0.0341095 0.0573869
⋮ ⋱ ⋮
0.767291 0.794692 0.391959 0.871937 0.317132 0.75175 0.492259
0.145043 0.247796 0.335396 0.818748 … 0.659016 0.903467 0.0589598
0.217171 0.689141 0.60046 0.559253 0.423461 0.198745 0.0189442
0.824684 0.371659 0.0807141 0.706745 0.877306 0.634762 0.0403287
0.168069 0.892426 0.61952 0.0926675 0.968932 0.584513 0.711143
0.256901 0.375477 0.151346 0.15687 0.399286 0.0752857 0.503156
0.588399 0.934283 0.34465 0.94413 … 0.158606 0.750039 0.56092
0.394082 0.687597 0.867584 0.889833 0.34219 0.427055 0.909798
0.402845 0.449754 0.985092 0.244497 0.58814 0.115636 0.354293
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.241761 NaN … NaN NaN NaN NaN NaN NaN
NaN 0.687105 NaN NaN NaN NaN NaN NaN
NaN 0.509172 NaN NaN NaN NaN NaN NaN
NaN 0.00676107 NaN NaN NaN NaN NaN NaN
NaN 0.28973 NaN NaN NaN NaN NaN NaN
NaN 0.450687 … 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 0.815507 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