Coverage for src / CSET / operators / pressure.py: 100%
40 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-08 16:49 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-08 16:49 +0000
1# © Crown copyright, Met Office (2022-2026) and CSET contributors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""Operators for pressure conversions."""
17import iris.cube
18import numpy as np
20from CSET._common import iter_maybe
21from CSET.operators._atmospheric_constants import E0, KAPPA, P0
22from CSET.operators.misc import convert_units
25def vapour_pressure(
26 temperature: iris.cube.Cube | iris.cube.CubeList,
27) -> iris.cube.Cube | iris.cube.CubeList:
28 """Calculate the vapour pressure of the atmosphere."""
29 v_pressure = iris.cube.CubeList([])
30 for T in iter_maybe(temperature):
31 es = T.copy()
32 exponent = 17.27 * (T - 273.16) / (T - 35.86)
33 es.data = E0 * np.exp(exponent.core_data())
34 es.units = "hPa"
35 es.rename("vapour_pressure")
36 v_pressure.append(es)
37 if len(v_pressure) == 1:
38 return v_pressure[0]
39 else:
40 return v_pressure
43def vapour_pressure_from_relative_humidity(
44 temperature: iris.cube.Cube | iris.cube.CubeList,
45 relative_humidity: iris.cube.Cube | iris.cube.CubeList,
46) -> iris.cube.Cube | iris.cube.CubeList:
47 """Calculate the vapour pressure using RH."""
48 v_pressure = iris.cube.CubeList([])
49 for T, RH in zip(
50 iter_maybe(temperature), iter_maybe(relative_humidity), strict=True
51 ):
52 RH = convert_units(RH, "1")
53 vp = vapour_pressure(T) * RH
54 vp.units = "hPa"
55 vp.rename("vapour_pressure")
56 v_pressure.append(vp)
57 if len(v_pressure) == 1:
58 return v_pressure[0]
59 else:
60 return v_pressure
63def exner_pressure(
64 pressure: iris.cube.Cube | iris.cube.CubeList,
65) -> iris.cube.Cube | iris.cube.CubeList:
66 """Calculate the exner pressure."""
67 pi = iris.cube.CubeList([])
68 for P in iter_maybe(pressure):
69 PI = P.copy()
70 P = convert_units(P, "hPa")
71 PI.data = (P.core_data() / P0) ** KAPPA
72 PI.rename("exner_pressure")
73 PI.units = "1"
74 pi.append(PI)
75 if len(pi) == 1:
76 return pi[0]
77 else:
78 return pi