@@ -280,13 +280,57 @@ function make_plots_generic(
280280end
281281
282282"""
283- make_spectra_generic
283+ compute_spectrum(var)
284284
285- Use ClimaCoreSpectra to compute and plot spectra for the given `vars`.
285+ Compute the spectrum associated to the given variable. Returns a ClimaAnalysis.OutputVar.
286+ """
287+ function compute_spectrum (var; mass_weight = nothing )
288+ # power_spectrum_2d seems to work only when the two dimensions have precisely one
289+ # twice as many points as the other
290+ dim1, dim2, dim3 = var. index2dim[1 : 3 ]
286291
287- Extra arguments are passed to `ClimaAnalysis.slice`
292+ len1 = length (var. dims[dim1])
293+ len2 = length (var. dims[dim2])
294+
295+ len1 == 2 len2 || error (" Cannot take this spectrum ($len1 != 2 $len2 )" )
296+
297+ (dim1 == " lon" || dim1 == " long" ) ||
298+ error (" First dimension has to be longitude (found $dim1 )" )
299+ dim2 == " lat" || error (" Second dimension has to be latitude (found $dim2 )" )
300+ dim3 == " z" || error (" Third dimension has to be altitude (found $dim3 )" )
301+
302+ FT = eltype (var. data)
303+ mass_weight =
304+ isnothing (mass_weight) ? ones (FT, length (var. dims[dim3])) : mass_weight
305+ spectrum_data, wave_numbers, _spherical, mesh_info =
306+ power_spectrum_2d (FT, var. data, mass_weight)
307+
308+ power_spectrum =
309+ dropdims (sum (spectrum_data, dims = 1 ), dims = 1 )[begin : (end - 1 ), :]
310+
311+ w_numbers = collect (0 : 1 : (mesh_info. num_spherical - 1 ))
312+
313+ dims = Dict (" Spherical Wavenumber" => w_numbers, dim3 => var. dims[dim3])
314+
315+ dim_attributes = Dict (
316+ " Spherical Wavenumber" => Dict (" units" => " " ),
317+ dim3 => var. dim_attributes[dim3],
318+ )
319+
320+ attributes = Dict (
321+ " short_name" => " log_spectrum_" * var. attributes[" short_name" ],
322+ " long_name" => " Spectrum of " * var. attributes[" long_name" ],
323+ " units" => " " ,
324+ )
325+
326+ return ClimaAnalysis. OutputVar (
327+ attributes,
328+ dims,
329+ dim_attributes,
330+ log .(power_spectrum),
331+ )
332+ end
288333
289- """
290334function make_spectra_generic (
291335 output_path,
292336 vars,
@@ -307,38 +351,35 @@ function make_spectra_generic(
307351 dim1, dim2 = var. index2dim[1 : 2 ]
308352
309353 length (var. dims[dim1]) == 2 * length (var. dims[dim2]) ||
310- error (" Cannot take a this spectrum" )
354+ error (" Cannot take this spectrum" )
311355
312356 FT = eltype (var. data)
313357 mass_weight = ones (FT, 1 )
314358 spectrum_data, wave_numbers, spherical, mesh_info =
315359 power_spectrum_2d (FT, var. data, mass_weight)
316360
317361 # From ClimaCoreSpectra/examples
318- X = collect (0 : 1 : (mesh_info. num_fourier))
319362 Y = collect (0 : 1 : (mesh_info. num_spherical))
320363 Z = spectrum_data[:, :, 1 ]
321364
322- dims = Dict (" num_fourier" => X, " num_spherical" => Y)
323- dim_attributes = Dict (
324- " num_fourier" => Dict (" units" => " " ),
325- " num_spherical" => Dict (" units" => " " ),
326- )
365+ dims = Dict (" Spherical Wavenumber" => Y)
366+ dim_attributes =
367+ Dict (" Spherical Wavenumber" => Dict (" units" => " " ))
327368
328369 attributes = Dict (
329- " short_name" => " log fft_" * var. attributes[" short_name" ],
370+ " short_name" =>
371+ " log_spectrum_" * var. attributes[" short_name" ],
330372 " long_name" => " Spectrum of " * var. attributes[" long_name" ],
331373 " units" => " " ,
332374 )
333- path = nothing
334375
335376 return ClimaAnalysis. OutputVar (
336377 attributes,
337378 dims,
338379 dim_attributes,
339380 log .(Z),
340- path,
341381 )
382+
342383 end |> collect
343384
344385 make_plots_generic (output_path, spectra, args... ; output_name, kwargs... )
@@ -574,10 +615,19 @@ DryBaroWavePlots = Union{Val{:sphere_baroclinic_wave_rhoe}}
574615function make_plots (:: DryBaroWavePlots , output_paths:: Vector{<:AbstractString} )
575616 simdirs = SimDir .(output_paths)
576617 short_names, reduction = [" pfull" , " va" , " wa" , " rv" ], " inst"
618+ short_names_spectra = [" ke" ]
577619 vars = map_comparison (simdirs, short_names) do simdir, short_name
578- return get (simdir; short_name, reduction)
620+ return slice ( get (simdir; short_name, reduction), time = LAST_SNAP )
579621 end
580- make_plots_generic (output_paths, vars, z = 1500 , time = LAST_SNAP)
622+ vars_spectra =
623+ map_comparison (simdirs, short_names_spectra) do simdir, short_name
624+ compute_spectrum (
625+ slice (get (simdir; short_name, reduction), time = LAST_SNAP),
626+ )
627+ end
628+ vars = vcat (vars... , vars_spectra... )
629+
630+ make_plots_generic (output_paths, vars, z = 1500 )
581631end
582632
583633function make_plots (
@@ -601,6 +651,14 @@ function make_plots(
601651 vars = map_comparison (simdirs, short_names) do simdir, short_name
602652 return get (simdir; short_name, reduction)
603653 end
654+ vars_spectra =
655+ map_comparison (simdirs, short_names_spectra) do simdir, short_name
656+ compute_spectrum (
657+ slice (get (simdir; short_name, reduction), time = 10 days),
658+ )
659+ end
660+ vars = vcat (vars... , vars_spectra... )
661+
604662 make_plots_generic (output_paths, vars, z = 1500 , time = 10 days)
605663end
606664
@@ -649,6 +707,13 @@ function make_plots(
649707 vars = map_comparison (simdirs, short_names) do simdir, short_name
650708 return get (simdir; short_name, reduction)
651709 end
710+ vars_spectra =
711+ map_comparison (simdirs, short_names_spectra) do simdir, short_name
712+ compute_spectrum (
713+ slice (get (simdir; short_name, reduction), time = 10 days),
714+ )
715+ end
716+ vars = vcat (vars... , vars_spectra... )
652717 make_plots_generic (output_paths, vars, z = 1500 , time = 10 days)
653718end
654719
@@ -1126,6 +1191,7 @@ function make_plots(::EDMFSpherePlots, output_paths::Vector{<:AbstractString})
11261191
11271192 short_names =
11281193 [" ua" , " wa" , " waup" , " thetaa" , " ta" , " taup" , " haup" , " tke" , " arup" ]
1194+
11291195 reduction = " average"
11301196 period = " 1h"
11311197 latitudes = [0.0 , 30.0 , 60.0 , 90.0 ]
0 commit comments