-
Notifications
You must be signed in to change notification settings - Fork 9
/
makie_plots.jl
627 lines (543 loc) · 17 KB
/
makie_plots.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
export timeseries_fluxes_fig,
timeseries_H2O_fig,
fingerprint_fig,
diurnal,
diurnal_plot!,
diurnals_fig,
make_plots
# Make all sort of plots with data and model output
# 1. Time series (e.g., C fluxes, h2o fluxes, energy fluxes, met drivers)
# 2. Seasonal pattern (i.e., monthly average and std)
# 3. Diurnal pattern (i.e., hourly average and std)
# 4. Response curves (e.g., NEE vs. PAR with VPD color and SWC brightness...)
# 5. Energy conservation (i.e., Rn - G vs. L + H)
# 6. Water budget (i.e., cumulative ET vs. P)
# 7. Data quality plots (e.g., NEE vs. u* by T and SWC bins)
# 8. Fingerprint plots (showing both seasonality and diurnal pattern)
# 9. Wavelet coherence
# to do in another script: animations
# 1. Time series of GPP, ET and SW_OUT
function timeseries_fluxes_fig(
inputs,
climaland,
earth_param_set;
dashboard = false,
) # will run for any inputs or climaland output of FLUXNET sites
if dashboard == true
WGLMakie.activate!() # for dashboards
end
index_t_start = findfirst(isequal(climaland.DateTime[1]), inputs.DateTime)
index_t_end = findfirst(isequal(climaland.DateTime[end]), inputs.DateTime)
fig = Figure(size = (1000, 1000)) # note: do not load Plots.jl in this branch (it is loading in plot_utils)
fontsize_theme = Theme(fontsize = 20)
set_theme!(fontsize_theme)
# create empty axis, with a specific layout
ax_C = Axis(
fig[1, 1],
ylabel = L"\text{GPP} \, (\mu\text{mol m}^{-2} \, \text{s}^{-1})",
) # C fluxes
ax_W = Axis(fig[2, 1], ylabel = L"\text{ET (mm)}") # h2o fluxes
ax_SWOUT = Axis(
fig[3, 1],
ylabel = L"\text{SW OUT} \, (\text{W} \, \text{m}^{-2})",
) # shortwave out
# ax_T = Axis(fig[4, 1]) # air, canopy, and soil temperature
# for time series, CairoMakie should allow DateTime type soon (but not yet)
# so the 2 lines of code below are a trick to be able to use DateTime - will be removed later
dateticks =
optimize_ticks(climaland.DateTime[1], climaland.DateTime[end])[1][2:(end - 1)] # first and last are weirdly placed
# add plots into axis ax_C
p_GPP_m = lines!(
ax_C,
datetime2unix.(climaland.DateTime),
climaland.GPP .* 1e6,
color = :blue,
)
p_GPP_d = lines!(
ax_C,
datetime2unix.(inputs.DateTime[index_t_start:index_t_end]),
inputs.GPP[index_t_start:index_t_end] .* 1e6,
color = :black,
)
# ax_W
p_ET_m = lines!(
ax_W,
datetime2unix.(climaland.DateTime),
(climaland.vapor_flux_liq .* 1e3 .* 24 .* 3600) .+
(climaland.transpiration .* 1e3 .* 24 .* 3600),
color = :blue,
) # not sure about units
p_ET_d = lines!(
ax_W,
datetime2unix.(inputs.DateTime[index_t_start:index_t_end]),
inputs.LE[index_t_start:index_t_end] ./
(LP.LH_v0(earth_param_set) * 1000) .* (1e3 * 24 * 3600),
color = :black,
) # not sure units
# ax_SW_OUT
p_SWOUT_m = lines!(
ax_SWOUT,
datetime2unix.(climaland.DateTime),
climaland.SW_out,
color = :blue,
)
p_SWOUT_d = lines!(
ax_SWOUT,
datetime2unix.(inputs.DateTime[index_t_start:index_t_end]),
inputs.SW_OUT[index_t_start:index_t_end],
color = :black,
)
# xticks
ax_C.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_W.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_SWOUT.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
axislegend(
ax_C,
[p_GPP_d, p_GPP_m],
["Observations", "ClimaLand"],
"",
position = :rt,
orientation = :horizontal,
)
ylims!(ax_C, (0, 40))
ylims!(ax_W, (0, 30))
ylims!(ax_SWOUT, (0, 200))
[
xlims!(
axes,
(
datetime2unix(climaland.DateTime[1]),
datetime2unix(climaland.DateTime[end]),
),
) for axes in [ax_C, ax_W, ax_SWOUT]
]
fig
return fig
end
# 2. Time series of SWC, Precip, moisture stress, stomatal conductance
function timeseries_H2O_fig(
inputs,
climaland,
earth_param_set;
dashboard = false,
) # will run for any inputs or climaland output of FLUXNET sites
if dashboard == true
WGLMakie.activate!() # for dashboards
end
index_t_start = findfirst(isequal(climaland.DateTime[1]), inputs.DateTime)
index_t_end = findfirst(isequal(climaland.DateTime[end]), inputs.DateTime)
# create an empty figure
fig = Figure(size = (1000, 1000)) # note: do not load Plots.jl in this branch (it is loading in plot_utils)
fontsize_theme = Theme(fontsize = 20)
set_theme!(fontsize_theme)
# create empty axis, with a specific layout
ax_H2O =
Axis(fig[1, 1], ylabel = L"\theta \, (\text{m}^{3} \, \text{m}^{-3})") # soil moisture
ax_H2O_rain = Axis(
fig[1, 1],
ylabel = L"\text{Rainfall (mm)}",
yaxisposition = :right,
ygridvisible = false,
ylabelcolor = :blue,
yticklabelcolor = :blue,
)
ax_MS = Axis(fig[2, 1], ylabel = L"\text{Moisture stress}") # moisture stress
ax_SC = Axis(
fig[3, 1],
ylabel = L"\text{Stomatal conductance} \, (\text{mol m}^{-2} \, \text{s}^{-1})",
) # stomatal conductance
# ax_T = Axis(fig[4, 1]) # air, canopy, and soil temperature
# for time series, CairoMakie should allow DateTime type soon (but not yet)
# so the 2 lines of code below are a trick to be able to use DateTime - will be removed later
dateticks =
optimize_ticks(climaland.DateTime[1], climaland.DateTime[end])[1][2:(end - 1)] # first and last are weirdly placed
# add plots into axis ax_H2O
p_H2O_m = lines!(
ax_H2O,
datetime2unix.(climaland.DateTime),
climaland.θ_l,
color = :green,
)
p_H2O_d = lines!(
ax_H2O,
datetime2unix.(inputs.DateTime[index_t_start:index_t_end]),
inputs.SWC[index_t_start:index_t_end],
color = :black,
)
# Rain on secondary axis
p_rain = barplot!(
ax_H2O_rain,
datetime2unix.(inputs.DateTime[index_t_start:index_t_end]),
inputs.P[index_t_start:index_t_end],
color = :blue,
)
# Moisture stress
p_MS = lines!(
ax_MS,
datetime2unix.(climaland.DateTime),
climaland.β,
color = :green,
) # not sure about units
# Stomatal conductance
p_SC = lines!(
ax_SC,
datetime2unix.(climaland.DateTime),
climaland.gs,
color = :green,
)
# xticks
ax_H2O.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_H2O_rain.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
hidexdecorations!(ax_H2O_rain)
ax_MS.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_SC.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
axislegend(
ax_H2O,
[p_H2O_d, p_H2O_m],
["Observations", "ClimaLand"],
"",
position = :rt,
orientation = :horizontal,
)
#ylims!(ax_C, (0, 40))
#ylims!(ax_W, (0, 30))
#ylims!(ax_SWOUT, (0, 200))
[
xlims!(
axes,
(
datetime2unix(climaland.DateTime[1]),
datetime2unix(climaland.DateTime[end]),
),
) for axes in [ax_H2O, ax_H2O_rain, ax_MS, ax_SC]
]
fig
return fig
end
# 3. Fingerprint plot
function fingerprint_fig(inputs, climaland, earth_param_set; dashboard = false) # will run for any inputs or climaland output of FLUXNET sites
if dashboard == true
WGLMakie.activate!() # for dashboards
end
index_t_start = findfirst(isequal(climaland.DateTime[1]), inputs.DateTime)
index_t_end = findfirst(isequal(climaland.DateTime[end]), inputs.DateTime)
fig = Figure(size = (1000, 1000))
fontsize_theme = Theme(fontsize = 20)
set_theme!(fontsize_theme)
# create empty axis, with a specific layout
ax_C = Axis(
fig[1, 1],
ylabel = "Hour of the day",
xlabel = "Date",
title = L"\text{GPP} \, (\mu\text{mol m}^{-2} \, \text{s}^{-1})",
) # C fluxes
ax_W = Axis(
fig[2, 1],
ylabel = "Hour of the day",
xlabel = "Date",
title = L"\text{ET (mm)}",
) # h2o fluxes
ax_M = Axis(
fig[3, 1],
ylabel = "Hour of the day",
xlabel = "Date",
title = L"\text{soil moisture}",
) # soil moisture
ax_R = Axis(
fig[4, 1],
ylabel = "Hour of the day",
xlabel = "Date",
title = L"\text{incoming shortwave radiation}",
) # radiation
# for time series, CairoMakie should allow DateTime type soon (but not yet)
# so the 2 lines of code below are a trick to be able to use DateTime - will be removed later
dateticks =
optimize_ticks(inputs.DateTime[1], inputs.DateTime[end])[1][2:(end - 1)] # first and last are weirdly placed
# Fingerprint plot
hm_GPP = heatmap!(
ax_C,
datetime2unix.(DateTime.(Date.(inputs.DateTime))),
hour.(inputs.DateTime) .+ (minute.(inputs.DateTime) ./ 60),
inputs.GPP .* 1e6,
)
Colorbar(fig[1, 2], hm_GPP)
hm_ET = heatmap!(
ax_W,
datetime2unix.(DateTime.(Date.(inputs.DateTime))),
hour.(inputs.DateTime) .+ (minute.(inputs.DateTime) ./ 60),
inputs.LE ./ (LP.LH_v0(earth_param_set) * 1000) .* (1e3 * 24 * 3600),
)
Colorbar(fig[2, 2], hm_ET)
hm_M = heatmap!(
ax_M,
datetime2unix.(DateTime.(Date.(inputs.DateTime))),
hour.(inputs.DateTime) .+ (minute.(inputs.DateTime) ./ 60),
inputs.SWC,
)
Colorbar(fig[3, 2], hm_M)
hm_R = heatmap!(
ax_R,
datetime2unix.(DateTime.(Date.(inputs.DateTime))),
hour.(inputs.DateTime) .+ (minute.(inputs.DateTime) ./ 60),
inputs.SW_IN,
)
Colorbar(fig[4, 2], hm_R)
ax_C.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_W.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_M.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
ax_R.xticks[] =
(datetime2unix.(dateticks), Dates.format.(dateticks, "mm/dd"))
fig
return fig
end
function diurnal(datetime, data)
hourlyquantile = [
quantile(data[hour.(datetime) .== h], [0.25, 0.5, 0.75]) for h in 0:1:23
]
return hourlyquantile
end
function diurnal_plot!(
fig,
ax,
datetime,
data,
color;
alpha = 0.3,
linestyle = :solid,
)
fig
hourlyquantile = diurnal(datetime, data)
diurnal_p = lines!(
ax,
0.5:1:23.5,
getindex.(hourlyquantile[1:24], 2),
color = color,
linestyle = linestyle,
)
diurnal_q = band!(
ax,
0.5:1:23.5,
getindex.(hourlyquantile[1:24], 1),
getindex.(hourlyquantile[1:24], 3),
color = (color, alpha),
)
return diurnal_p
end
function diurnals_fig(inputs, climaland, earth_param_set; dashboard = false) # will run for any inputs or climaland output of FLUXNET sites
if dashboard == true
WGLMakie.activate!() # for dashboards
end
index_t_start = findfirst(isequal(climaland.DateTime[1]), inputs.DateTime)
index_t_end = findfirst(isequal(climaland.DateTime[end]), inputs.DateTime)
fig = Figure(size = (1000, 1000))
fontsize_theme = Theme(fontsize = 20)
set_theme!(fontsize_theme)
ax_C = Axis(
fig[1, 1],
ylabel = L"\text{CO}_{2} \, (\mu\text{mol m}^{-2} \, \text{s}^{-1})",
) # C fluxes
ax_W = Axis(fig[2, 1], ylabel = L"\text{H}_{2}\text{O} \, \text{(mm)}") # h2o fluxes
ax_SIF = Axis(fig[3, 1], ylabel = L"\text{SIF}") # SIF
ax_E = Axis(
fig[4, 1],
ylabel = L"\text{Radiation} \, (\text{W} \, \text{m}^{-2})",
xlabel = L"\text{Hour of the day}",
xgridvisible = false,
) # shortwave out
# CO2 fluxes
# model
p_GPP_m = diurnal_plot!(
fig,
ax_C,
climaland.DateTime,
climaland.GPP .* 1e6,
:green,
)
p_RA_m = diurnal_plot!(
fig,
ax_C,
climaland.DateTime,
climaland.Ra .* 1e6,
:black,
)
# data
p_GPP_d = diurnal_plot!(
fig,
ax_C,
inputs.DateTime[index_t_start:index_t_end],
inputs.GPP[index_t_start:index_t_end] .* 1e6,
:green,
alpha = 0.1,
linestyle = :dot,
)
# H2O fluxes
# model
p_ET_m = diurnal_plot!(
fig,
ax_W,
climaland.DateTime,
climaland.transpiration .* 1e3 .* 24 .* 3600,
:blue,
)
# data
p_ET_d = diurnal_plot!(
fig,
ax_W,
inputs.DateTime[index_t_start:index_t_end],
inputs.LE[index_t_start:index_t_end] ./
(LP.LH_v0(earth_param_set) * 1000) .* (1e3 * 24 * 3600),
:blue,
alpha = 0.1,
linestyle = :dot,
)
# SIF
p_SIF_m =
diurnal_plot!(fig, ax_SIF, climaland.DateTime, climaland.SIF, :black)
# Energy fluxes
# model
# diurnal_plot!(fig, ax_E, climaland.DateTime, climaland.LW_out, :red)
p_SWout_m =
diurnal_plot!(fig, ax_E, climaland.DateTime, climaland.SW_out, :red)
# data
p_SWout_d = diurnal_plot!(
fig,
ax_E,
inputs.DateTime[index_t_start:index_t_end],
inputs.SW_OUT[index_t_start:index_t_end],
:red,
alpha = 0.1,
linestyle = :dot,
)
[xlims!(axes, (0, 24)) for axes in [ax_C, ax_W, ax_SIF, ax_E]]
axislegend(
ax_C,
[p_GPP_d, p_GPP_m, p_RA_m],
["GPP Obs.", "GPP model", "Ra model"],
"",
position = :rt,
orientation = :vertical,
)
axislegend(
ax_W,
[p_ET_d, p_ET_m],
["ET obs.", "ET model"],
"",
position = :rt,
orientation = :vertical,
)
axislegend(
ax_SIF,
[p_SIF_m],
["SIF model"],
"",
position = :rt,
orientation = :vertical,
)
axislegend(
ax_E,
[p_SWout_d, p_SWout_m],
["SWout obs.", "SWout model"],
"",
position = :rt,
orientation = :vertical,
)
hidexdecorations!(ax_C)
hidexdecorations!(ax_W)
hidexdecorations!(ax_SIF)
fig
return fig
end
# 5. Cumulative P and ET
function cumulative_H2O_fig(
inputs,
climaland,
earth_param_set;
dashboard = false,
)
if dashboard == true
WGLMakie.activate!() # for dashboards
end
index_t_start = findfirst(isequal(climaland.DateTime[1]), inputs.DateTime)
index_t_end = findfirst(isequal(climaland.DateTime[end]), inputs.DateTime)
fig = Figure(size = (1000, 1000))
fontsize_theme = Theme(fontsize = 20)
set_theme!(fontsize_theme)
ax = Axis(fig[1, 1], ylabel = "Cumulative water flux (mm)")
ET_m = climaland.transpiration .* 1e3 .* 24 .* 3600
ET_obs =
inputs.LE[index_t_start:index_t_end] ./
(LP.LH_v0(earth_param_set) * 1000) .* (1e3 * 24 * 3600)
P_obs = inputs.P[index_t_start:index_t_end] .* 1e3 .* 24 .* 3600
p_P = lines!(ax, (1 / 48):(1 / 48):(length(P_obs) / 48), cumsum(P_obs)) # Precip
p_ET_m = lines!(ax, (1 / 48):(1 / 48):(length(ET_m) / 48), cumsum(ET_m)) # ET model
p_ET_d = lines!(ax, (1 / 48):(1 / 48):(length(ET_obs) / 48), cumsum(ET_obs)) # ET observations
axislegend(
ax,
[p_P, p_ET_m, p_ET_d],
["Precipitation", "ET modeled", "ET observed"],
"",
position = :lt,
orientation = :vertical,
)
fig
return fig
end
# 6. Energy balance closure (L + H = Rn - G)
function make_plots(
inputs,
climaland;
FT = Float64,
save_fig = true,
dashboard = false,
) # will run for any inputs or climaland output of FLUXNET sites
if dashboard == true
WGLMakie.activate!() # for dashboards
else
CairoMakie.activate!()
end
earth_param_set = LP.LandParameters(FT)
args = (inputs, climaland, earth_param_set)
fig1 = timeseries_fluxes_fig(args...)
fig2 = timeseries_H2O_fig(args...)
fig3 = fingerprint_fig(args...)
fig4 = diurnals_fig(args...)
fig5 = cumulative_H2O_fig(args...)
if save_fig == true
if isdir("figures")
nothing
else
mkdir("figures")
end
names = [
"timeseries_fluxes.pdf",
"timeseries_H2O.pdf",
"fingerprint.pdf",
"diurnals.pdf",
"cumulative_water.pdf",
]
[
save(joinpath("figures", name), fig) for
(name, fig) in zip(names, [fig1, fig2, fig3, fig4, fig5])
]
return nothing
end
if save_fig == false
return (
timeseries = fig1,
water = fig2,
fingerprint = fig3,
diurnals = fig4,
cumulative_water = fig5,
)
end
end