Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add netCDF example #274

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"
MakieThemes = "e296ed71-da82-5faf-88ab-0034a9761098"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
NaturalEarth = "436b0209-26ab-4e65-94a9-6526d86fea76"
Polylabel = "49a44318-e865-4b63-9842-695152d634c1"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ examples = String[
# "projections.jl",
"tissot.jl",
# "rotating_earth.jl",
"netcdf.jl"
]
example_dir = joinpath(dirname(@__DIR__), "examples")
mkpath(example_dir)
Expand Down
1 change: 1 addition & 0 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ meshimage
projections
tissot
healpix
netcdf
```

## Examples from Cartopy
Expand Down
50 changes: 50 additions & 0 deletions examples/netcdf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#=
# Data from netCDF
Here, we show how to plot data from a netCDF file via GeoMakie.
The example uses global sea surface temperature.
=#

using NCDatasets
using GeoMakie
using CairoMakie

# Download the data
src = "https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc"
dst = "tos_O1_2001-2002.nc"
download(src, dst)

# Read latitude and longitude coordinates + sea surface temperature
# Temperature has a time index... just select the first time step
ds = NCDataset("tos_O1_2001-2002.nc", "r")
lat = ds["lat"][:]
lon = ds["lon"][:]
temp = ds["tos"][:,:,1]
close(ds)
rm("tos_O1_2001-2002.nc")

# Longitude ranges from 0 to 360
# Convert to -180 to 180 and sort temp accordingly
lon[lon .> 180] .-= 360
sort_idx = sortperm(lon)
lon = lon[sort_idx]
temp = temp[sort_idx,:]

# Plot the data as a heatmap with coastlines and a colorbar.
#
# First, create a figure, and a GeoAxis inside it:
fig = Figure(size=(900,500))
ax = GeoAxis(fig[1,1]; dest="+proj=longlat +datum=WGS84", xgridwidth=0.15, ygridwidth=0.15)
## then, plot a heatmap with the data we used!
sp = heatmap!(ax, lon, lat, temp; colormap=cgrad(:viridis))
## finally, plot some coastlines to give the plot context
lines!(ax, GeoMakie.coastlines(); color = :black, linewidth=1.0)
## and a colorbar
cb = Colorbar(fig[1, 2], sp, width=30, height=Relative(0.7), label="Sea Surface Temperature (K)")
fig

#=
```@cardmeta
Cover = fig
Description = "Plotting data from a netCDF file via NCDatasets.jl"
```
=#