Skip to content

Commit aadc9d7

Browse files
committed
Fix bug where interpolation do not update with ops
There was a bug where the interpolation of a OutputVar is not updated when doing a binary operation between an OutputVar and a Real.
1 parent 4b5fce7 commit aadc9d7

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/Var.jl

+26-10
Original file line numberDiff line numberDiff line change
@@ -1007,36 +1007,52 @@ macro overload_binary_op(op)
10071007
)
10081008
end
10091009
function Base.$op(x::OutputVar, y::Real)
1010-
ret_var = copy(x)
1011-
@. ret_var.data = $op(x.data, y)
1012-
empty!(ret_var.attributes)
1010+
ret_attributes = empty(x.attributes)
10131011

10141012
specific_attributes = ("short_name", "long_name")
10151013

10161014
for attr in specific_attributes
10171015
if haskey(x.attributes, attr)
1018-
ret_var.attributes[attr] =
1016+
ret_attributes[attr] =
10191017
string(x.attributes[attr], " ", string($op), " ", y)
10201018
end
10211019
end
10221020

1023-
return ret_var
1021+
ret_dims = deepcopy(x.dims)
1022+
ret_dim_attributes = deepcopy(x.dim_attributes)
1023+
1024+
ret_data = @. $op(x.data, y)
1025+
1026+
return OutputVar(
1027+
ret_attributes,
1028+
ret_dims,
1029+
ret_dim_attributes,
1030+
ret_data,
1031+
)
10241032
end
10251033
function Base.$op(x::Real, y::OutputVar)
1026-
ret_var = copy(y)
1027-
@. ret_var.data = $op(x, y.data)
1028-
empty!(ret_var.attributes)
1034+
ret_attributes = empty(y.attributes)
10291035

10301036
specific_attributes = ("short_name", "long_name")
10311037

10321038
for attr in specific_attributes
10331039
if haskey(y.attributes, attr)
1034-
ret_var.attributes[attr] =
1040+
ret_attributes[attr] =
10351041
string(x, " ", string($op), " ", y.attributes[attr])
10361042
end
10371043
end
10381044

1039-
return ret_var
1045+
ret_dims = deepcopy(y.dims)
1046+
ret_dim_attributes = deepcopy(y.dim_attributes)
1047+
1048+
ret_data = @. $op(x, y.data)
1049+
1050+
return OutputVar(
1051+
ret_attributes,
1052+
ret_dims,
1053+
ret_dim_attributes,
1054+
ret_data,
1055+
)
10401056
end
10411057
end
10421058
end

test/test_Var.jl

+2
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ end
142142
@test var1plus10.data == data1 .+ 10
143143
@test ClimaAnalysis.short_name(var1plus10) == "bob + 10"
144144
@test ClimaAnalysis.long_name(var1plus10) == "hi + 10"
145+
@test var1plus10((0.0, 0.0, 0.0)) == var1((0.0, 0.0, 0.0)) + 10
145146

146147
tenplusvar1 = 10 + var1
147148

148149
@test tenplusvar1.data == data1 .+ 10
149150
@test ClimaAnalysis.short_name(tenplusvar1) == "10 + bob"
150151
@test ClimaAnalysis.long_name(tenplusvar1) == "10 + hi"
152+
@test tenplusvar1((0.0, 0.0, 0.0)) == 10 + var1((0.0, 0.0, 0.0))
151153

152154
var1plusvar3 = var1 + var3
153155

0 commit comments

Comments
 (0)