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.latAn 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]
nothingDefining 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.506383 0.642076 0.591221 … 0.210698 0.438838 0.661205
0.239135 0.954632 0.457571 0.903515 0.141843 0.728414
0.667515 0.411777 0.728698 0.239659 0.314317 0.16672
0.584661 0.995066 0.318979 0.0907468 0.298215 0.00615341
0.885425 0.813841 0.202905 0.315023 0.369964 0.276929
0.932085 0.33311 0.759782 … 0.189124 0.627593 0.219503
0.0591521 0.307416 0.319504 0.393087 0.358395 0.0923675
0.728912 0.217059 0.204678 0.450741 0.460005 0.74422
0.0754638 0.77431 0.731047 0.690982 0.821057 0.773719
0.166801 0.805433 0.110401 0.904589 0.0109843 0.857574
⋮ ⋱ ⋮
0.663524 0.0746658 0.390341 0.661445 0.499049 0.492383
0.21588 0.191659 0.490113 … 0.332638 0.0940732 0.0713539
0.541992 0.250405 0.130602 0.643294 0.206951 0.82552
0.438251 0.552963 0.688115 0.39992 0.234829 0.433006
0.376918 0.920959 0.325994 0.211944 0.142902 0.464976
0.71668 0.631989 0.549491 0.284966 0.381784 0.831298
0.583274 0.210668 0.188198 … 0.697459 0.758549 0.527553
0.533964 0.711207 0.0317837 0.609239 0.933259 0.253902
0.814285 0.448001 0.963868 0.607464 0.293668 0.119272Defining 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 / 1102And then use this RegionGrid to extract data for the GeoRegion of interest:
julia
ndata = extract(data,ggrd)58×19 Matrix{Float64}:
0.523021 NaN NaN … NaN NaN NaN NaN NaN NaN
NaN 0.239542 NaN NaN NaN NaN NaN NaN NaN
NaN 0.0745745 0.0373797 NaN NaN NaN NaN NaN NaN
NaN 0.949081 0.391723 NaN NaN NaN NaN NaN NaN
NaN 0.36542 0.64518 NaN NaN NaN NaN NaN NaN
NaN 0.192057 0.738216 … NaN NaN NaN NaN NaN NaN
NaN NaN 0.608534 NaN NaN NaN NaN NaN NaN
NaN NaN 0.691045 NaN NaN NaN NaN NaN NaN
NaN NaN 0.82272 NaN NaN NaN NaN NaN NaN
NaN NaN 0.601663 0.461369 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 NaNData 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