Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily convert to Unitful #181

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ext/ClimaAnalysisUnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ function Var.convert_units(
new_units::AbstractString;
conversion_function = nothing,
)
has_unitful_units =
Var.has_units(var) && (var.attributes["units"] isa Unitful.Units)
maybe_unitful_units =
Var.has_units(var) && (Var.units(var) |> Var._maybe_convert_to_unitful)
has_unitful_units = maybe_unitful_units isa Unitful.Units
new_units_maybe_unitful = Var._maybe_convert_to_unitful(new_units)
new_units_are_unitful = new_units_maybe_unitful isa Unitful.Units

Expand All @@ -60,7 +61,7 @@ function Var.convert_units(
convert_function =
data -> _converted_data_unitful(
data,
var.attributes["units"],
maybe_unitful_units,
new_units_maybe_unitful,
)
else
Expand All @@ -72,7 +73,7 @@ function Var.convert_units(

new_data = convert_function(var.data)
new_attribs = copy(var.attributes)
# The constructor will take care of converting new_units to Unitful
# Units will be a string and not Unitful
new_attribs["units"] = new_units

return Var.OutputVar(new_attribs, var.dims, var.dim_attributes, new_data)
Expand Down
12 changes: 0 additions & 12 deletions src/Var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,6 @@ function OutputVar(attribs, dims, dim_attribs, data)
end
end

function _maybe_process_key_value(k, v)
k != "units" && return k => v
return k => _maybe_convert_to_unitful(v)
end

# Recreating an object to ensure that the type is correct
if !isempty(attribs)
attribs = Dict(_maybe_process_key_value(k, v) for (k, v) in attribs)
end

# TODO: Support units for dimensions too

return OutputVar(
attribs,
OrderedDict(dims),
Expand Down
8 changes: 4 additions & 4 deletions test/test_Var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import Dates
data,
)

@test ClimaAnalysis.units(var_with_unitful) == "m s^-1"
@test var_with_unitful.attributes["units"] == u"m" / u"s"
@test ClimaAnalysis.units(var_with_unitful) == "m/s"
@test var_with_unitful.attributes["units"] == "m/s"

# Unparsable unit
attribs = Dict("long_name" => "hi", "units" => "bob")
Expand Down Expand Up @@ -1160,7 +1160,7 @@ end
@test squared_error_var.data == (data_ones - data_threes) .^ 2
@test ClimaAnalysis.short_name(squared_error_var) == "(sim-obs)^2_short"
@test ClimaAnalysis.long_name(squared_error_var) == "(SIM - OBS)^2 short"
@test ClimaAnalysis.units(squared_error_var) == "kg^2"
@test ClimaAnalysis.units(squared_error_var) == "(kg)^2"

# Flip order in squared_error and check computations
squared_error_var = ClimaAnalysis.squared_error(var_threes, var_ones)
Expand Down Expand Up @@ -1201,7 +1201,7 @@ end
var_unitful = ClimaAnalysis.squared_error(var_unitful, var_unitful)
var_not_unitful =
ClimaAnalysis.squared_error(var_not_unitful, var_not_unitful)
@test ClimaAnalysis.units(var_unitful) == "(kg^2 m^-1)^2"
@test ClimaAnalysis.units(var_unitful) == "(kg^2/m)^2"
@test ClimaAnalysis.units(var_not_unitful) == "(wacky/weird^2)^2"
end

Expand Down