Skip to content
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

Adding hurs (realtive humidity) derivation script #2397

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions esmvalcore/preprocessor/_derive/hurs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Derivation of variable `hurs`."""

from iris import NameConstraint
import iris
import numpy as np

Check notice on line 5 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L5

Unused numpy imported as np (unused-import)
import cf_units

from ._baseclass import DerivedVariableBase

# Constants
GAS_CONSTANT_WV = 461.5 # JK-1kg-1

Check notice on line 11 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L11

at least two spaces before inline comment (E261)
ENTALPY_OF_VAPORIZATION = 2.501e6 # Jkg-1

Check notice on line 12 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L12

at least two spaces before inline comment (E261)

class DerivedVariable(DerivedVariableBase):

Check notice on line 14 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L14

expected 2 blank lines, found 1 (E302)
"""Derivation of variable `hurs`."""

@staticmethod
def required(project):
"""Declare the variables needed for derivation."""
required = [
{'short_name': 'tdps'},
{'short_name': 'tas'},]

Check notice on line 22 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L22

missing whitespace after ',' (E231)
return required

@staticmethod
def calculate(cubes):
"""Compute relative humidity.

Relative humidity computed from dewpoint temperature and

Check notice on line 29 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L29

trailing whitespace (W291)
surface air temperature following Bohren and Albrecht 1998.
"""
tdps_cube = cubes.extract_cube(NameConstraint(var_name='tdps'))
tas_cube = cubes.extract_cube(NameConstraint(var_name='tas'))

cubes_difference = iris.analysis.maths.subtract(tas_cube, tdps_cube)

Check notice on line 35 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L35

trailing whitespace (W291)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cubes_difference = iris.analysis.maths.subtract(tas_cube, tdps_cube)
cubes_difference = tas_cube - tdps_cube

Would this also work? Did you read the section on cube maths in the iris documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, so when I was trying it with simple tas_cube-tdps_cube the issue was that I was running out of memory, while it wasn't a case with subtract. I will test it again with the time diagnostic and will let you know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be surprising because it should do the same thing, i.e. the minus operator on an Iris cube is implemented using the exact same function:
https://github.com/SciTools/iris/blob/c5e0353da11c263656c6c77e5e56eafefcede077/lib/iris/cube.py#L3968

cubes_product = iris.analysis.maths.multiply(tas_cube, tdps_cube)

log_humidity_cube = iris.analysis.maths.divide(-ENTALPY_OF_VAPORIZATION * cubes_difference,

Check notice on line 38 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L38

line too long (99 > 79 characters) (E501)
GAS_CONSTANT_WV * cubes_product)

Check notice on line 39 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L39

continuation line over-indented for visual indent (E127)

hurs_cube = 100 * iris.analysis.maths.exp(log_humidity_cube)

hurs_cube.units = cf_units.Unit('%')

return hurs_cube

Check notice on line 45 in esmvalcore/preprocessor/_derive/hurs.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

esmvalcore/preprocessor/_derive/hurs.py#L45

no newline at end of file (W292)