Skip to content

Commit 264f09a

Browse files
committed
Add Var.set_units
1 parent f87306d commit 264f09a

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

NEWS.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
ClimaAnalysis.jl Release Notes
22
===============================
3+
v0.5.10
4+
-------
5+
6+
## Features
7+
8+
### Set units
9+
10+
You can now set units for a `OutputVar`. This is useful if you need to change the name of
11+
the units or units are missing.
12+
13+
```julia
14+
new_var = ClimaAnalysis.set_units(var, "kg m s^-1")
15+
```
16+
317

418
v0.5.9
519
------

docs/src/api.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Var.range_dim
5757
Var.reordered_as
5858
Var.resampled_as
5959
Var.convert_units
60+
Var.set_units
6061
Var.integrate_lonlat
6162
Var.integrate_lat
6263
Var.integrate_lon

docs/src/var.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ new_var = ClimaAnalysis.convert_units(var, "kg m/s", conversion_function = (x) -
5555

5656
!!! note If you find some unparseable units, please open an issue. We can fix them!
5757

58+
If units do not exist or you want to change the name of the units, then one can uses the
59+
`set_units` function.
60+
```julia
61+
new_var = ClimaAnalysis.set_units(var, "kg m s^-1")
62+
```
63+
!!! warning "Override existing units"
64+
If units already exist, this will override the units for data in `var`.
65+
5866
## Integration
5967

6068
`OutputVar`s can be integrated with respect to longitude, latitude, or both using
@@ -188,4 +196,4 @@ julia> global_rmse(sim, obs)
188196
189197
julia> units(se_var)
190198
"K^2"
191-
```
199+
```

src/Var.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export OutputVar,
5050
global_bias,
5151
squared_error,
5252
global_mse,
53-
global_rmse
53+
global_rmse,
54+
set_units
5455

5556
"""
5657
Representing an output variable
@@ -241,7 +242,6 @@ end
241242
# Implemented in ClimaAnalysisUnitfulExt
242243
function _maybe_convert_to_unitful end
243244

244-
245245
"""
246246
Var.convert_units(var, new_units; conversion_function = nothing)
247247
@@ -301,6 +301,19 @@ Failure to specify the `conversion_function` will produce an error.
301301
"""
302302
function convert_units end
303303

304+
"""
305+
set_units(var::OutputVar, units::AbstractString)
306+
307+
Set `units` for data in `var`.
308+
309+
!!! warning "Override existing units"
310+
If units already exist, this will override the units for data in `var`. To convert
311+
units, see [`Var.convert_units`](@ref)
312+
"""
313+
function set_units(var::OutputVar, units::AbstractString)
314+
converted_var = convert_units(var, units, conversion_function = identity)
315+
return converted_var
316+
end
304317

305318
"""
306319
is_z_1D(var::OutputVar)

test/test_Var.jl

+46
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,49 @@ end
12471247
@test_throws ErrorException ClimaAnalysis.bias(var_tal, var_tal)
12481248
@test_throws ErrorException ClimaAnalysis.squared_error(var_tal, var_tal)
12491249
end
1250+
1251+
@testset "Setting units" begin
1252+
# Unit exists (unitful)
1253+
lon = collect(range(-179.5, 179.5, 360))
1254+
lat = collect(range(-89.5, 89.5, 180))
1255+
data = ones(length(lon), length(lat))
1256+
dims = OrderedDict(["lon" => lon, "lat" => lat])
1257+
dim_attribs = OrderedDict([
1258+
"lon" => Dict("units" => "deg"),
1259+
"lat" => Dict("units" => "deg"),
1260+
])
1261+
attribs =
1262+
Dict("long_name" => "idk", "short_name" => "short", "units" => "kg")
1263+
var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs, data)
1264+
var_units = ClimaAnalysis.set_units(var, "idk")
1265+
@test ClimaAnalysis.units(var_units) == "idk"
1266+
1267+
# Unit exists (not unitful)
1268+
lon = collect(range(-179.5, 179.5, 360))
1269+
lat = collect(range(-89.5, 89.5, 180))
1270+
data = ones(length(lon), length(lat))
1271+
dims = OrderedDict(["lon" => lon, "lat" => lat])
1272+
dim_attribs = OrderedDict([
1273+
"lon" => Dict("units" => "deg"),
1274+
"lat" => Dict("units" => "deg"),
1275+
])
1276+
attribs =
1277+
Dict("long_name" => "idk", "short_name" => "short", "units" => "wacky")
1278+
var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs, data)
1279+
var_units = ClimaAnalysis.set_units(var, "idk")
1280+
@test ClimaAnalysis.units(var_units) == "idk"
1281+
1282+
# Unit does not exist
1283+
lon = collect(range(-179.5, 179.5, 360))
1284+
lat = collect(range(-89.5, 89.5, 180))
1285+
data = ones(length(lon), length(lat))
1286+
dims = OrderedDict(["lon" => lon, "lat" => lat])
1287+
dim_attribs = OrderedDict([
1288+
"lon" => Dict("units" => "deg"),
1289+
"lat" => Dict("units" => "deg"),
1290+
])
1291+
attribs = Dict("long_name" => "idk", "short_name" => "short")
1292+
var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs, data)
1293+
var_units = ClimaAnalysis.set_units(var, "idk")
1294+
@test ClimaAnalysis.units(var_units) == "idk"
1295+
end

0 commit comments

Comments
 (0)