-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update humidity mixing ratio to work with pressure and height level data #2056
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
import numpy as np | ||
from iris.cube import Cube, CubeList | ||
from iris.exceptions import ConstraintMismatchError | ||
from numpy import ndarray | ||
from scipy.optimize import newton | ||
|
||
|
@@ -22,7 +23,10 @@ | |
generate_mandatory_attributes, | ||
) | ||
from improver.utilities.common_input_handle import as_cubelist | ||
from improver.utilities.cube_manipulation import sort_coord_in_cube | ||
from improver.utilities.cube_manipulation import ( | ||
enforce_coordinate_ordering, | ||
sort_coord_in_cube, | ||
) | ||
from improver.utilities.interpolation import interpolate_missing_data | ||
from improver.utilities.mathematical_operations import fast_linear_fit | ||
from improver.utilities.spatial import OccurrenceWithinVicinity | ||
|
@@ -315,23 +319,62 @@ def _make_humidity_cube(self, data: np.ndarray) -> Cube: | |
) | ||
return cube | ||
|
||
def generate_pressure_cube(self) -> None: | ||
"""Generate a pressure cube from the pressure coordinate on the temperature cube""" | ||
coord_list = [coord.name() for coord in self.temperature.coords()] | ||
pressure_list = CubeList() | ||
for temp_slice in self.temperature.slices_over("pressure"): | ||
pressure_value = temp_slice.coord("pressure").points | ||
temp_slice.data = np.broadcast_to(pressure_value, temp_slice.shape) | ||
pressure_list.append(temp_slice) | ||
self.pressure = pressure_list.merge_cube() | ||
enforce_coordinate_ordering(self.pressure, coord_list) | ||
self.pressure.rename("surface_air_pressure") | ||
self.pressure.units = "Pa" | ||
|
||
def process(self, *cubes: Union[Cube, CubeList]) -> Cube: | ||
""" | ||
Calculates the humidity mixing ratio from the inputs. | ||
Calculates the humidity mixing ratio from the inputs. The inputs can be on height levels | ||
or pressure levels, and the output will be on the same levels. If the input cubes are on | ||
pressure levels then a pressure cube doesn't need to be provided and the pressure levels | ||
will be inferred from the pressure coordinate on the temperature cube. | ||
|
||
Args: | ||
cubes: | ||
Cubes of temperature (K), pressure (Pa) and relative humidity (1) | ||
Cubes of temperature (K) and relative humidity (1). A cube of pressure (Pa) must also | ||
br provided unless there is a pressure coordinate in the temperature and relative humidity cubes. | ||
|
||
Returns: | ||
Cube of humidity mixing ratio | ||
Cube of humidity mixing ratio on same levels as input cubes | ||
|
||
""" | ||
cubes = as_cubelist(*cubes) | ||
(self.temperature, self.pressure, self.rel_humidity) = cubes.extract_cubes( | ||
["air_temperature", "surface_air_pressure", "relative_humidity"] | ||
|
||
(self.temperature, self.rel_humidity) = cubes.extract_cubes( | ||
["air_temperature", "relative_humidity"] | ||
) | ||
|
||
try: | ||
self.pressure = cubes.extract_cube("surface_air_pressure") | ||
except ConstraintMismatchError: | ||
# If no pressure cube is provided, check if pressure is a coordinate in the temperature and relative humidity cubes | ||
temp_coord = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section seemed a little confusing to me. This is almost exactly the same thing but might be easier to understand/a tiny tiny bit faster: temp_coord_flag = any(
coord.name() == "pressure"
for coord in self.temperature.coords()
)
rh_coord_flag = any(
coord.name() == "pressure"
for coord in self.rel_humidity.coords()
)
if temp_coord_flag & rh_coord_flag:
self.generate_pressure_cube()
else:
raise ValueError(
"No pressure cube called 'surface_air_pressure' found and no pressure coordinate found in temperature or relative humidity cubes"
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer your code to mine so I've updated it. |
||
True | ||
for coord in self.temperature.coords() | ||
if coord.name() == "pressure" | ||
] | ||
rh_coord = [ | ||
True | ||
for coord in self.rel_humidity.coords() | ||
if coord.name() == "pressure" | ||
] | ||
if any(temp_coord) and any(rh_coord): | ||
self.generate_pressure_cube() | ||
else: | ||
raise ValueError( | ||
"No pressure cube called 'surface_air_pressure' found and no pressure coordinate found in temperature or relative humidity cubes" | ||
) | ||
|
||
self.mandatory_attributes = generate_mandatory_attributes( | ||
[self.temperature, self.pressure, self.rel_humidity] | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small typo here "br" replace with be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected