Skip to content

Commit 3f4079a

Browse files
committed
Move extrapolate kwarg to pressure intp constructor
1 parent 22b9028 commit 3f4079a

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

src/Remapping/interpolate_pressure.jl

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct PressureInterpolator{
3030
FACE <: Fields.Field,
3131
SPACE <: Spaces.AbstractSpace,
3232
LEVELS,
33+
EXTRAPOLATE <: ClimaInterpolations.Interpolation1D.Extrapolate1D,
3334
}
3435
"""A ClimaCore.Field representing pressure on center space. This field is
3536
defined on a space with height (z) as the vertical coordinate, not
@@ -51,6 +52,10 @@ struct PressureInterpolator{
5152
"""A 1D vector of pressure coordinates to interpolate onto for every
5253
column"""
5354
pressure_levels::LEVELS
55+
56+
"""Extrapolation condition when interpolating outside of the pressure
57+
range"""
58+
extrapolate::EXTRAPOLATE
5459
end
5560

5661
"""
@@ -130,15 +135,23 @@ function construct_pfull_grid(::Type{FT}, pressure_levels, device) where {FT}
130135
end
131136

132137
"""
133-
PressureInterpolator(pfull_field::Fields.Field, pressure_levels)
138+
PressureInterpolator(
139+
pfull_field::Fields.Field,
140+
pressure_levels;
141+
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
142+
)
134143
135144
Construct a `PressureInterpolator` from `pfull_field`, a pressure field defined
136145
on a center space and `pressure_levels`, a vector of pressure levels to
137146
interpolate to.
138147
139148
The pressure levels must be in ascending or descending order.
140149
"""
141-
function PressureInterpolator(pfull_field::Fields.Field, pressure_levels)
150+
function PressureInterpolator(
151+
pfull_field::Fields.Field,
152+
pressure_levels;
153+
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
154+
)
142155
if issorted(pressure_levels, rev = true)
143156
pressure_levels = sort(pressure_levels)
144157
end
@@ -150,7 +163,8 @@ function PressureInterpolator(pfull_field::Fields.Field, pressure_levels)
150163
pressure_space = construct_pressure_space(FT, space, pressure_levels)
151164
return PressureInterpolator(
152165
pfull_field,
153-
pressure_space,
166+
pressure_space;
167+
extrapolate,
154168
)
155169
end
156170

@@ -160,18 +174,22 @@ end
160174
pressure_space::Union{
161175
Spaces.AbstractFiniteDifferenceSpace,
162176
Spaces.ExtrudedFiniteDifferenceSpace,
163-
},
177+
};
178+
extrapolate = ClimaInterpolations.Interpolation1D.Flat()
164179
)
165180
166181
Construct a `PressureInterpolator` from `pfull_field`, a pressure field, and
167182
`pressure_space`, a space with pressure as the vertical coordinate.
183+
184+
The default extrapolation behavior is flat extrapolation.
168185
"""
169186
function PressureInterpolator(
170187
pfull_field::Fields.Field,
171188
pressure_space::Union{
172189
Spaces.AbstractFiniteDifferenceSpace,
173190
Spaces.ExtrudedFiniteDifferenceSpace,
174-
},
191+
};
192+
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
175193
)
176194
axes(pfull_field).staggering isa Grids.CellCenter || error("The staggering of the
177195
pressure field must be cell center")
@@ -197,6 +215,7 @@ function PressureInterpolator(
197215
scratch_face_pressure_field,
198216
pressure_space,
199217
pressure_levels,
218+
extrapolate,
200219
)
201220
end
202221

@@ -258,21 +277,19 @@ end
258277
"""
259278
interpolate_pressure(
260279
field::Fields.Field,
261-
pfull_intp::PressureInterpolator;
262-
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
280+
pfull_intp::PressureInterpolator,
263281
)
264282
265283
Vertically interpolate field onto a space identical to that of field, but with
266284
pressure as the vertical coordinate and return the interpolated field.
267285
"""
268286
function interpolate_pressure(
269287
field::Fields.Field,
270-
pfull_intp::PressureInterpolator;
271-
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
288+
pfull_intp::PressureInterpolator,
272289
)
273290
(; pfull_field) = pfull_intp
274291
dest = fill(one(eltype(pfull_field)), pfull_intp.pressure_space)
275-
Remapping.interpolate_pressure!(dest, field, pfull_intp; extrapolate)
292+
Remapping.interpolate_pressure!(dest, field, pfull_intp)
276293
return dest
277294
end
278295

@@ -281,7 +298,6 @@ end
281298
dest::Fields.Field,
282299
field::Fields.Field,
283300
pfull_intp::PressureInterpolator;
284-
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
285301
)
286302
287303
Vertically interpolate `field` onto `dest` and return `nothing`.
@@ -291,10 +307,14 @@ The vertical coordinate of the space of `dest` must be in pressure.
291307
function interpolate_pressure!(
292308
dest::Fields.Field,
293309
field::Fields.Field,
294-
pfull_intp::PressureInterpolator;
295-
extrapolate = ClimaInterpolations.Interpolation1D.Flat(),
310+
pfull_intp::PressureInterpolator,
296311
)
297-
(; scratch_center_pressure_field, scratch_face_pressure_field, pressure_levels) =
312+
(;
313+
scratch_center_pressure_field,
314+
scratch_face_pressure_field,
315+
pressure_levels,
316+
extrapolate,
317+
) =
298318
pfull_intp
299319
scratch_pfull_array = if axes(field).staggering isa Grids.CellCenter
300320
Fields.field2array(scratch_center_pressure_field)

test/Remapping/interpolate_pressure.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ for FT in (Float32, Float64)
6666
pfull_field = fill(FT(1.0), space)
6767
pfull_field_array = Fields.field2array(pfull_field)
6868
pfull_field_array .= collect(10:-1:1)
69-
pfull_levels = [0.0, 1.0, 5.5, 10.0, 12.0]
69+
pressure_levels = [0.0, 1.0, 5.5, 10.0, 12.0]
7070

7171
# Pressures should be in sorted order
7272
@test_throws ErrorException PressureInterpolator(pfull_field, [5.5, 10.0, 1.0])
7373

74-
pfull_intp = PressureInterpolator(pfull_field, pfull_levels)
74+
pfull_intp = PressureInterpolator(pfull_field, pressure_levels)
7575
pfull_field_array = Fields.field2array(Remapping.pfull_field(pfull_intp))
7676

7777
# Check the pressures along each column is sorted in descending
@@ -106,6 +106,22 @@ for FT in (Float32, Float64)
106106

107107
interpolate_pressure!(dest2, dummy_field, pfull_intp)
108108
@test dest2 == dest
109+
110+
# Test linear extrapolation
111+
pressure_levels = [-100.0, 100.0]
112+
extrapolate = ClimaInterpolations.Interpolation1D.LinearExtrapolation()
113+
linear_pfull_intp =
114+
PressureInterpolator(pfull_field, pressure_levels; extrapolate)
115+
dummy_field = fill(FT(1.0), space)
116+
dummy_field_array = Fields.field2array(dummy_field)
117+
dummy_field_array .= collect(10:-1:1)
118+
dest = interpolate_pressure(dummy_field, linear_pfull_intp)
119+
dest_array = Fields.field2array(dest)
120+
@test dest_array == repeat(
121+
[-100.0, 100.0],
122+
1,
123+
size(pfull_field_array, 2),
124+
)
109125
end
110126
end
111127

0 commit comments

Comments
 (0)