diff --git a/Project.toml b/Project.toml index e08d7b0..f874b15 100644 --- a/Project.toml +++ b/Project.toml @@ -18,8 +18,8 @@ GeoMakie = "db073c08-6b98-4ee5-b6a4-5efafb3259c6" Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" [extensions] -GeoMakieExt = "GeoMakie" -MakieExt = "Makie" +ClimaAnalysisGeoMakieExt = "GeoMakie" +ClimaAnalysisMakieExt = "Makie" [compat] Aqua = "0.8" diff --git a/ext/MakieExt.jl b/ext/ClimaAnalysisMakieExt.jl similarity index 98% rename from ext/MakieExt.jl rename to ext/ClimaAnalysisMakieExt.jl index 763c918..4b03dab 100644 --- a/ext/MakieExt.jl +++ b/ext/ClimaAnalysisMakieExt.jl @@ -1,4 +1,4 @@ -module MakieExt +module ClimaAnalysisMakieExt import Makie import ClimaAnalysis @@ -554,7 +554,7 @@ end """ _to_unitrange(x::Number, lo::Number, hi::Number) -Linearly transform x ∈ [lo, hi] to [0, 1]. +Linearly transform x ∈ [lo, hi] to [0, 1]. """ _to_unitrange(x::Number, lo::Number, hi::Number) = (x - lo) / (hi - lo) @@ -581,7 +581,7 @@ symmetrically around zero maps the same color intensity to the same magnitude. # Returns - `cmap::Makie.ColorGradient`: a colormap """ -function _constrained_cmap( +function Visualize._constrained_cmap( cols::Vector, lo, hi; @@ -589,10 +589,17 @@ function _constrained_cmap( categorical = false, rev = false, ) - _constrained_cmap(Makie.ColorScheme(cols), lo, hi; mid, categorical, rev) + Visualize._constrained_cmap( + Makie.ColorScheme(cols), + lo, + hi; + mid, + categorical, + rev, + ) end -function _constrained_cmap( +function Visualize._constrained_cmap( cols::Makie.ColorScheme, lo, hi; diff --git a/ext/GeoMakieExt.jl b/ext/GeoMakieExt.jl deleted file mode 100644 index 012e690..0000000 --- a/ext/GeoMakieExt.jl +++ /dev/null @@ -1,202 +0,0 @@ -module GeoMakieExt - -import GeoMakie -import GeoMakie: Makie -import ClimaAnalysis -import ClimaAnalysis: Visualize - -MakiePlace = Union{Makie.Figure, Makie.GridLayout} - -function _geomakie_plot_on_globe!( - place::MakiePlace, - var::ClimaAnalysis.OutputVar; - p_loc = (1, 1), - plot_coastline = true, - plot_colorbar = true, - more_kwargs = Dict( - :plot => Dict(), - :cb => Dict(), - :axis => Dict(), - :coast => Dict(:color => :black), - ), - plot_fn = Makie.surface!, -) - length(var.dims) == 2 || error("Can only plot 2D variables") - - lon_name = "" - lat_name = "" - - for dim in var.index2dim - if dim in ClimaAnalysis.Var.LONGITUDE_NAMES - lon_name = dim - elseif dim in ClimaAnalysis.Var.LATITUDE_NAMES - lat_name = dim - else - error("$dim is neither longitude nor latitude") - end - end - - lon = var.dims[lon_name] - lat = var.dims[lat_name] - - units = var.attributes["units"] - short_name = var.attributes["short_name"] - colorbar_label = "$short_name [$units]" - - axis_kwargs = get(more_kwargs, :axis, Dict()) - plot_kwargs = get(more_kwargs, :plot, Dict()) - cb_kwargs = get(more_kwargs, :cb, Dict()) - coast_kwargs = get(more_kwargs, :coast, Dict(:color => :black)) - - var.attributes["long_name"] = - ClimaAnalysis.Utils.warp_string(var.attributes["long_name"]) - - title = get(axis_kwargs, :title, var.attributes["long_name"]) - - GeoMakie.GeoAxis(place[p_loc...]; title, axis_kwargs...) - - plot = plot_fn(lon, lat, var.data; plot_kwargs...) - plot_coastline && Makie.lines!(GeoMakie.coastlines(); coast_kwargs...) - - if plot_colorbar - p_loc_cb = Tuple([p_loc[1], p_loc[2] + 1]) - Makie.Colorbar( - place[p_loc_cb...], - plot, - label = colorbar_label; - cb_kwargs..., - ) - end -end - -""" - heatmap2D_on_globe!(fig::Makie.Figure, - var::ClimaAnalysis.OutputVar; - p_loc = (1,1), - plot_coastline = true, - plot_colorbar = true, - more_kwargs) - heatmap2D_on_globe!(grid_layout::Makie.GridLayout, - var::ClimaAnalysis.OutputVar; - p_loc = (1,1), - plot_coastline = true, - plot_colorbar = true, - more_kwargs) - - -Plot a heatmap of the given 2D `var`iable on a projected geoid. - -The plot comes with labels, units, and a colorbar. - -This function assumes that the following attributes are available: -- long_name -- short_name -- units - -The dimensions have to be longitude and latitude. - -Additional arguments to the plotting and axis functions -======================================================= - -`more_kwargs` can be a dictionary that maps symbols to additional options for: -- the axis (`:axis`) -- the plotting function (`:plot`) -- the colorbar (`:cb`) -- the coastline (`:coast`) - -The coastline is plotted from `GeoMakie.coastline` using the `lines!` plotting function. - -The values are splatted in the relevant functions. Populate them with a -Dictionary of `Symbol`s => values to pass additional options. -""" -function Visualize.heatmap2D_on_globe!( - place::MakiePlace, - var::ClimaAnalysis.OutputVar; - p_loc = (1, 1), - plot_coastline = true, - plot_colorbar = true, - more_kwargs = Dict( - :plot => Dict(), - :cb => Dict(), - :axis => Dict(), - :coast => Dict(:color => :black), - ), -) - return _geomakie_plot_on_globe!( - place, - var; - p_loc, - plot_coastline, - plot_colorbar, - more_kwargs, - plot_fn = Makie.surface!, - ) -end - -""" - contours2D_on_globe!(fig::Makie.Figure, - var::ClimaAnalysis.OutputVar; - p_loc = (1,1), - plot_coastline = true, - plot_colorbar = true, - plot_contours = true, - more_kwargs) - contours2D_on_globe!(grid_layout::Makie.GridLayout, - var::ClimaAnalysis.OutputVar; - p_loc = (1,1), - plot_coastline = true, - plot_colorbar = true, - plot_contours = true, - more_kwargs) - - -Plot discrete contours of the given 2D `var`iable on a projected geoid. - -The plot comes with labels, units, and a colorbar. - -This function assumes that the following attributes are available: -- long_name -- short_name -- units - -The dimensions have to be longitude and latitude. - -Additional arguments to the plotting and axis functions -======================================================= - -`more_kwargs` can be a dictionary that maps symbols to additional options for: -- the axis (`:axis`) -- the plotting function (`:plot`) -- the colorbar (`:cb`) -- the coastline (`:coast`) - -The coastline is plotted from `GeoMakie.coastline` using the `lines!` plotting function. - -The values are splatted in the relevant functions. Populate them with a -Dictionary of `Symbol`s => values to pass additional options. -""" -function Visualize.contour2D_on_globe!( - place::MakiePlace, - var::ClimaAnalysis.OutputVar; - p_loc = (1, 1), - plot_coastline = true, - plot_colorbar = true, - more_kwargs = Dict( - :plot => Dict(), - :cb => Dict(), - :axis => Dict(), - :coast => Dict(:color => :black), - ), -) - _geomakie_plot_on_globe!( - place, - var; - p_loc, - plot_coastline, - plot_colorbar, - more_kwargs, - plot_fn = Makie.contourf!, - ) -end - -end diff --git a/src/Visualize.jl b/src/Visualize.jl index 307c814..aad3238 100644 --- a/src/Visualize.jl +++ b/src/Visualize.jl @@ -2,6 +2,8 @@ module Visualize export plot! +function _constrained_cmap end + function heatmap2D! end function sliced_heatmap! end diff --git a/test/test_GeoMakieExt.jl b/test/test_GeoMakieExt.jl index ca37624..5b070ee 100644 --- a/test/test_GeoMakieExt.jl +++ b/test/test_GeoMakieExt.jl @@ -39,17 +39,15 @@ using OrderedCollections fig2, var2D, more_kwargs = Dict( - :plot => - ClimaAnalysis.Utils.kwargs(colormap = Makie.colorschemes[:vik]), + :plot => ClimaAnalysis.Utils.kwargs(colormap = :vik), ), ) output_name = joinpath(tmp_dir, "test_contours2D_globe.png") Makie.save(output_name, fig2) - # Test cmap - MakieExt = Base.get_extension(ClimaAnalysis, :MakieExt) - test_cmap = MakieExt._constrained_cmap( + # Test cmap + test_cmap = ClimaAnalysis.Visualize._constrained_cmap( Makie.colorschemes[:vik], 0.0, 15000.0 + (5000.0 / 3.0), @@ -70,7 +68,7 @@ using OrderedCollections output_name = joinpath(tmp_dir, "test_contours2D_globe_with_test_cmap.png") Makie.save(output_name, fig3) - test_cmap = MakieExt._constrained_cmap( + test_cmap = ClimaAnalysis.Visualize._constrained_cmap( range(Makie.colorant"red", stop = Makie.colorant"green", length = 15), 0.0, 15000.0 + (5000.0 / 3.0),