Skip to content

Commit 459c621

Browse files
authoredMar 21, 2025··
Implement glacier climate and climate_step prints (#89)
* improve prints of glacier, climate and climate_step * minor fix * improve code coverage
1 parent 9fffac1 commit 459c621

File tree

5 files changed

+170
-11
lines changed

5 files changed

+170
-11
lines changed
 

‎Project.toml

+3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
1616
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
1717
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
1818
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
19+
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
20+
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
1921
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
2022
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
2123
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
2224
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
2325
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
2426
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
27+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
2528
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
2629
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
2730
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

‎src/Sleipnir.jl

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ using Dates, DateFormats
2727
using NetCDF
2828
using GR
2929
using DataStructures
30+
using ImageInTerminal
31+
using ImageCore
32+
using Printf
3033

3134
# ##############################################
3235
# ############ PARAMETERS ###############

‎src/glaciers/climate/Climate2D.jl

+110
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,113 @@ Base.:(==)(a::Climate2D, b::Climate2D) = a.raw_climate == b.raw_climate && a.cli
6262
a.climate_step == b.climate_step && a.climate_2D_step == b.climate_2D_step &&
6363
a.longterm_temps == b.longterm_temps && a.avg_temps == b.avg_temps &&
6464
a.avg_gradients == b.avg_gradients
65+
66+
# Display setup
67+
function Base.show(io::IO, climate::Climate2D)
68+
printstyled("Climate2D\n";color=:yellow)
69+
70+
print(" avg_gradients = ")
71+
printstyled(round(climate.avg_gradients; digits=4); color=:blue)
72+
println(" °C/m")
73+
74+
print(" avg_temps = ")
75+
printstyled(round(climate.avg_temps; digits=2);color=:blue)
76+
println(" °C")
77+
78+
print(" climate_2D_step: ")
79+
printstyled("Climate2Dstep";color=:yellow)
80+
print(" with a ")
81+
printstyled(size(climate.climate_2D_step.temp); color=:red)
82+
println(" grid")
83+
84+
print(" climate_raw_step: ")
85+
printstyled("RasterStack";color=:yellow)
86+
print(" with ")
87+
printstyled(length(climate.climate_raw_step);color=:red)
88+
print(" time steps [ ")
89+
printstyled(Dates.format(dims(climate.climate_raw_step, Ti)[begin], "yyyy-mm-dd");color=:green)
90+
print("")
91+
printstyled(Dates.format(dims(climate.climate_raw_step, Ti)[end], "yyyy-mm-dd");color=:green)
92+
println(" ]")
93+
94+
println(" climate_step:")
95+
print(" gradient = ")
96+
printstyled(round(climate.climate_step["gradient"];digits=3);color=:blue)
97+
println(" °C/m (sum of clipped gradients)")
98+
print(" temp = ")
99+
printstyled(round(climate.climate_step["temp"];digits=3);color=:blue)
100+
println(" °C (sum of positive temperatures)")
101+
print(" avg_temp = ")
102+
printstyled(round(climate.climate_step["avg_temp"];digits=3);color=:blue)
103+
println(" °C")
104+
print(" avg_gradient = ")
105+
printstyled(round(climate.climate_step["avg_gradient"];digits=3);color=:blue)
106+
println(" °C/m")
107+
print(" ref_hgt = ")
108+
printstyled(round(climate.climate_step["ref_hgt"];digits=1);color=:blue)
109+
println(" m")
110+
print(" prcp = ")
111+
printstyled(round(climate.climate_step["prcp"];digits=1);color=:blue)
112+
println(" kg/m²")
113+
114+
print(" longterm_temps: ")
115+
printstyled("$(typeof(climate.longterm_temps))";color=:yellow)
116+
print(" with ")
117+
printstyled("$(length(climate.longterm_temps))";color=:red)
118+
println(" elements")
119+
120+
print(" raw_climate: ")
121+
printstyled("RasterStack";color=:yellow)
122+
print(" with ")
123+
printstyled("$(length(climate.raw_climate))";color=:red)
124+
print(" time steps [ ")
125+
printstyled(Dates.format(dims(climate.raw_climate, Ti)[begin], "yyyy-mm-dd");color=:green)
126+
print("")
127+
printstyled(Dates.format(dims(climate.raw_climate, Ti)[end], "yyyy-mm-dd");color=:green)
128+
print(" ]")
129+
end
130+
131+
function Base.show(io::IO, climate_step::Climate2Dstep)
132+
printstyled("Climate2Dstep";color=:yellow)
133+
print(" with a ")
134+
printstyled(size(climate_step.temp); color=:red)
135+
println(" grid")
136+
137+
print(" avg_gradient = ")
138+
printstyled(round(climate_step.avg_gradient; digits=4); color=:blue)
139+
println(" °C/m")
140+
141+
print(" gradient = ")
142+
printstyled(round(climate_step.gradient; digits=4); color=:blue)
143+
println(" °C/m (sum of clipped gradients)")
144+
145+
print(" ref_hgt = ")
146+
printstyled(round(climate_step.ref_hgt; digits=1); color=:blue)
147+
println(" m")
148+
149+
print(" x: ")
150+
printstyled(typeof(climate_step.x);color=:yellow)
151+
print(" in [ ")
152+
printstyled("$(round(minimum(climate_step.x);digits=6))° → $(round(maximum(climate_step.x);digits=6))°"; color=:blue)
153+
println(" ]")
154+
155+
print(" y: ")
156+
printstyled(typeof(climate_step.y);color=:yellow)
157+
print(" in [ ")
158+
printstyled("$(round(minimum(climate_step.y);digits=6))° → $(round(maximum(climate_step.y);digits=6))°"; color=:blue)
159+
println(" ]")
160+
161+
println(" min,mean,max:")
162+
163+
print(" PDD: ")
164+
printstyled("$(round(minimum(climate_step.PDD); digits=1)) $(round(mean(climate_step.PDD); digits=1)) $(round(maximum(climate_step.PDD); digits=1))\n"; color=:blue)
165+
166+
print(" rain: ")
167+
printstyled("$(round(minimum(climate_step.rain); digits=1)) $(round(mean(climate_step.rain); digits=1)) $(round(maximum(climate_step.rain); digits=1))\n"; color=:blue)
168+
169+
print(" snow: ")
170+
printstyled("$(round(minimum(climate_step.snow); digits=1)) $(round(mean(climate_step.snow); digits=1)) $(round(maximum(climate_step.snow); digits=1))\n"; color=:blue)
171+
172+
print(" temp: ")
173+
printstyled("$(round(minimum(climate_step.temp); digits=1)) $(round(mean(climate_step.temp); digits=1)) $(round(maximum(climate_step.temp); digits=1))\n"; color=:blue)
174+
end

‎src/glaciers/glacier/Glacier2D.jl

+48-11
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,57 @@ Base.:(≈)(a::Glacier2D, b::Glacier2D) = a.rgi_id == b.rgi_id && a.climate == b
170170

171171
# Display setup
172172
function Base.show(io::IO, glacier::Glacier2D)
173-
prec = 6
174-
println("Glacier $(glacier.rgi_id) with a $(glacier.nx)x$(glacier.ny) grid (Δx=$(glacier.Δx), Δy=$(glacier.Δy))")
175-
print("at position ($(round(glacier.cenlat;digits=prec))°, $(round(glacier.cenlon;digits=prec))°)")
176-
glathidaTxt = !isnothing(glacier.H_glathida) ? "w/ glathida elevation" : "w/o glathida elevation"
177-
println(" $(glathidaTxt)")
178-
println("A=$(glacier.A) C=$(glacier.C) n=$(glacier.n)")
173+
if !isnothing(glacier.H₀)
174+
H=round.(255*glacier.H₀/maximum(glacier.H₀))
175+
display(Gray.(Int.(H'[end:-1:1,1:end])/255))
176+
end
177+
178+
print("Glacier2D ")
179+
printstyled(glacier.rgi_id;color=:yellow)
180+
print(" with a ")
181+
printstyled("$(glacier.nx)x$(glacier.ny)";color=:red)
182+
print(" grid ")
183+
printstyled("(Δx=$(glacier.Δx), Δy=$(glacier.Δy))";color=:red)
184+
println("")
185+
if !isnothing(glacier.cenlat) & !isnothing(glacier.cenlon)
186+
print("at position ")
187+
printstyled("($(round(glacier.cenlat;digits=6))°, $(round(glacier.cenlon;digits=6))°)";color=:green)
188+
else
189+
print("at undefined location")
190+
end
191+
if isnothing(glacier.H_glathida)
192+
printstyled(" w/o";color=:red)
193+
else
194+
printstyled(" w/";color=:blue)
195+
end
196+
println(" glathida elevation")
197+
198+
if !isnothing(glacier.S) & !isnothing(glacier.H₀)
199+
print("Min,mean,max bedrock elevation S : ")
200+
printstyled("$(round(minimum(glacier.S[glacier.H₀.>0]);digits=1)) $(round(mean(glacier.S[glacier.H₀.>0]);digits=1)) $(round(maximum(glacier.S[glacier.H₀.>0]);digits=1))\n";color=:blue)
201+
print("Mean,max ice thickness H₀ : ")
202+
printstyled("$(round(mean(glacier.H₀[glacier.H₀.>0]);digits=1)) $(round(maximum(glacier.H₀[glacier.H₀.>0]);digits=1))\n";color=:blue)
203+
end
204+
205+
print("A= ")
206+
printstyled(@sprintf("%.3e", glacier.A); color=:blue)
207+
print(" C= ")
208+
printstyled(glacier.C; color=:blue)
209+
print(" n= ")
210+
printstyled(glacier.n; color=:blue)
179211
end
212+
180213
# Vectorial form
181-
function Base.show(io::IO, ::MIME"text/plain", glaciers::Vector{Glacier2D})
214+
function Base.show(io::IO, ::MIME"text/plain", glaciers::Vector{G}) where {G <: AbstractGlacier}
182215
len = length(glaciers)
183-
print("$(len)-element Vector{Glacier2D}")
184-
regions = counter([split(split(glacier.rgi_id, "-")[2], ".")[1] for glacier in glaciers])
185-
regionsFormatted = ["$(k[1]) (x$(k[2]))" for k in regions]
186-
println(" distributed over regions $(join(regionsFormatted, ", "))")
216+
print("$(len)-element $(typeof(glaciers))")
217+
try
218+
regions = counter([split(split(glacier.rgi_id, "-")[2], ".")[1] for glacier in glaciers])
219+
regionsFormatted = ["$(k[1]) (x$(k[2]))" for k in regions]
220+
println(" distributed over regions $(join(regionsFormatted, ", "))")
221+
catch
222+
println(" distributed over undefined regions")
223+
end
187224
if len>5
188225
print(join([glacier.rgi_id for glacier in glaciers[1:2]], " "))
189226
print(" ... ")

‎test/glaciers_construction.jl

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function glaciers2D_constructor(; save_refs::Bool = false, use_glathida_data::Bo
2121

2222
glaciers = initialize_glaciers(rgi_ids, params; test=true)
2323

24+
# Test prints
25+
println(glaciers)
26+
println(glaciers[1])
27+
println(glaciers[1].climate)
28+
println(glaciers[1].climate.climate_2D_step)
29+
2430
if save_refs
2531
jldsave(joinpath(Sleipnir.root_dir, string("test/data/glaciers/glaciers2D_", file_suffix, ".jld2")); glaciers)
2632
end

0 commit comments

Comments
 (0)
Please sign in to comment.