Skip to content

Commit f40caf7

Browse files
update tutorial
1 parent 722530a commit f40caf7

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

docs/src/tutorial1.jl

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,23 @@ using Statistics
1414

1515
CairoMakie.enable_only_mime!("png") #hide
1616

17-
# Some helper functions for plotting with Makie for plotting maps and timeseries.
18-
19-
function nicemaps(v; timeindex = 1, lon = v["lon"][:], lat = v["lat"][:], title = nothing)
17+
# Some helper functions for plotting maps and timeseries with Makie .
18+
# Note that the variabless v is an array type of CommonDataModel. All related
19+
# variables sharing the same dimensions (in particular coordinate variables)
20+
# can be access by subsetting v with the name of the related variable.
21+
function nicemaps(v; timeindex = 1, lon = v["lon"][:], lat = v["lat"][:], title="")
2022
fig = Figure()
21-
ax = Axis(fig[1, 1],aspect = AxisAspect(1/cosd(mean(lat))))
23+
ax = Axis(fig[1, 1]; aspect = AxisAspect(1/cosd(mean(lat))), title)
2224
hm = heatmap!(ax,lon,lat,v[:,:,timeindex])
23-
if !isnothing(title)
24-
ax.title[] = title
25-
end
2625
Colorbar(fig[1,2],hm)
2726
return fig
2827
end
2928

30-
function nicetimeseries(v::AbstractVector; time = v["time"][:], title = nothing,
31-
timefmt = "yyyy-mm-dd")
32-
fig, ax, ln = lines(Dates.value.(time),v[:])
29+
function nicetimeseries(v::AbstractVector; time = v["time"][:], title = nothing)
30+
fig, ax, ln = lines(time,v[:])
3331
if !isnothing(title)
3432
ax.title[] = title
3533
end
36-
timeticks = DateTime.(2023,1:2:12,1)
37-
ax.xticks = Dates.value.(timeticks),Dates.format.(timeticks,timefmt)
38-
ax.xticklabelrotation = π/4
3934
return fig
4035
end
4136

@@ -62,6 +57,11 @@ end
6257
ds = NCDataset(fname)
6358
ncsst = ds["sst"]
6459

60+
# ncsst2 is a lazy view of the netCDF variable ncsst subjected to the following
61+
# selection criteria. Lon, lat and time are netCDF variables in the datasets.
62+
# The operator ≈ looks for the nearest time instance.
63+
# Use e.g. time ≈ DateTime(2023,4,1) ± Day(1) to set a tolerance
64+
# The operators ≈ and ± are typed as \approx and \pm followed by the TAB key.
6565
ncsst2 = @select(ncsst,300 <= lon <= 360 && 0 <= lat <= 46 && time DateTime(2023,4,1))
6666

6767
nicemaps(ncsst2, title = "NA SST")
@@ -73,13 +73,14 @@ ncsst_first = view(ncsst,:,:,1)
7373
ncsst_na = @select(ncsst_first,300 <= lon <= 360 && 0 <= lat <= 46)
7474
nicemaps(ncsst_na)
7575

76-
# Select all data from a given month
76+
# Select all data from a given month: the julia function `Dates.month` is
77+
# first applied element-wise to all elements of time before comparing to 3.
7778

7879
ncsst_march = @select(ncsst,Dates.month(time) == 3)
7980
nicemaps(ncsst_march, timeindex = 1, title = "SST 1st March 2023")
8081

8182

82-
# Select multiple months using e.g. an interval (from [IntervalSets](https://github.com/JuliaMath/IntervalSets.jl)).
83+
# Select multiple months using e.g. an interval (from [IntervalSets](https://github.com/JuliaMath/IntervalSets.jl)). The upper bound of the interval is inclusive.
8384
# ∈ can be typed by writing `\in` directly followed by the TAB key.
8485

8586
ncsst_march_april = @select(ncsst,Dates.month(time) 3..4)
@@ -118,7 +119,7 @@ size(ncsst_outside)
118119
ds_equator = @select(ds,lat 0 && time DateTime(2023,1,1))
119120
lon_equator = @select(ds_equator,coalesce(sst > 30,false))["lon"]
120121

121-
# The first 3 longitude where SST exceeds 30°C at the equator for 2023-01-01
122+
# The first 3 longitude valeus where SST exceeds 30°C at the equator for 2023-01-01
122123

123124
lon_equator[1:3]
124125

@@ -130,6 +131,7 @@ lon_equator[1:3]
130131
# For example group the SST by month and average per month:
131132

132133
sst_mean = mean(@groupby(ncsst,Dates.Month(time)))
134+
size(sst_mean)
133135

134136
# Instead of using the macro one can also use the function `groupby`:
135137

@@ -168,5 +170,8 @@ is_atlantic_hurricane_season(time) =
168170
DateTime(year(time),6,1) <= time <= DateTime(year(time),11,30)
169171

170172
sst_my_mean = prob_hot(groupby(ncsst,:time => is_atlantic_hurricane_season));
173+
size(sst_my_mean)
171174

175+
# The reducing will create two slices depending wether the condition is
176+
# false (1st slice) or true (2nd slice)
172177
nicemaps(sst_my_mean,timeindex=2, title = "probability(temp. > 26°C) during Atlantic hurricane season")

0 commit comments

Comments
 (0)