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.750476 0.141416 0.0657558 … 0.84605 0.372799 0.337101
0.729899 0.0318642 0.543102 0.0307104 0.933933 0.0498614
0.184167 0.603927 0.405464 0.993651 0.549961 0.215488
0.123563 0.789686 0.0431161 0.100531 0.550829 0.773133
0.282951 0.598363 0.0626793 0.6694 0.0831456 0.663115
0.161533 0.0220016 0.287018 … 0.0374872 0.0681916 0.468703
0.939452 0.132607 0.994848 0.519606 0.430089 0.997553
0.726337 0.74893 0.531871 0.416448 0.267559 0.720623
0.339993 0.277406 0.421344 0.790729 0.628852 0.354506
0.496361 0.155102 0.703342 0.694504 0.914751 0.284766
⋮ ⋱ ⋮
0.288009 0.645902 0.597636 0.901904 0.0213672 0.282349
0.826053 0.934116 0.906691 … 0.23628 0.747908 0.208708
0.965508 0.511975 0.868846 0.146189 0.469919 0.910197
0.971883 0.650833 0.30029 0.351409 0.60614 0.331385
0.125688 0.441121 0.793517 0.71683 0.836271 0.229145
0.549003 0.504985 0.155624 0.432793 0.654033 0.996327
0.545428 0.868613 0.586419 … 0.731191 0.166486 0.37019
0.0409959 0.0211776 0.830638 0.107845 0.557183 0.163685
0.96865 0.0708585 0.974863 0.177344 0.93218 0.164375
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.373149 NaN NaN … NaN NaN NaN NaN NaN NaN
NaN 0.221454 NaN NaN NaN NaN NaN NaN NaN
NaN 0.46516 0.256135 NaN NaN NaN NaN NaN NaN
NaN 0.992899 0.307881 NaN NaN NaN NaN NaN NaN
NaN 0.0161134 0.852083 NaN NaN NaN NaN NaN NaN
NaN 0.575169 0.0448235 … NaN NaN NaN NaN NaN NaN
NaN NaN 0.0899412 NaN NaN NaN NaN NaN NaN
NaN NaN 0.709187 NaN NaN NaN NaN NaN NaN
NaN NaN 0.618064 NaN NaN NaN NaN NaN NaN
NaN NaN 0.0986203 0.765622 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