Skip to content

Commit

Permalink
Add check for whether P(z) is bijective or not
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-kev committed Jan 24, 2025
1 parent 9bc3038 commit dab5acb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Atmos.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ Linearly interpolate `var1d` from `origin_pressure` to `target_pressure`.
Note: Values outside of the range are linearly extrapolated
"""
function _resample_column!(dest, var1d, origin_pressure, target_pressure)
reverse_origin_pressure = reverse(origin_pressure)
isunique =
length(unique(reverse_origin_pressure)) ==
length(reverse_origin_pressure)
!(isunique && issorted(reverse_origin_pressure)) &&
error("P(z) is not bijective, cannot resample column")
# Interpolations.jl require increasing knots, but pressure is decreasing, so
# we have to reverse it
var1d_of_P = Intp.extrapolate(
Intp.interpolate(
(reverse(origin_pressure),),
(reverse_origin_pressure,),
reverse(var1d),
Intp.Gridded(Intp.Linear()),
),
Expand Down
24 changes: 24 additions & 0 deletions test/test_Atmos.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ import OrderedCollections: OrderedDict
Zvar,
Zvar,
)

# P(z) is not bijective (not sorted)
pressure_var = ClimaAnalysis.OutputVar(
attribs,
Dict("z" => z_alt),
dim_attribs,
reverse(pdata),
)
@test_throws ErrorException ClimaAnalysis.Atmos.to_pressure_coordinates(
pressure_var,
pressure_var,
)
pressure = 300.0:-2.0:100.0 |> collect |> reverse
pressure[2] = 100.0
pressure_var = ClimaAnalysis.OutputVar(
attribs,
Dict("z" => z_alt),
dim_attribs,
pressure,
)
@test_throws ErrorException ClimaAnalysis.Atmos.to_pressure_coordinates(
pressure_var,
pressure_var,
)
end

@testset "RMSE of pressure coordinates" begin
Expand Down

0 comments on commit dab5acb

Please sign in to comment.