Skip to content

Commit

Permalink
Merge pull request #2528 from samsrabin/fix-subset-data-cth
Browse files Browse the repository at this point in the history
Fix subset_data (cth and cbh not iterable)
  • Loading branch information
samsrabin committed May 6, 2024
2 parents 10a2345 + 3e23cd9 commit 9a28e2a
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 79 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ a9d96219902cf609636886c7073a84407f450d9a
d866510188d26d51bcd6d37239283db690af7e82
0dcd0a3c1abcaffe5529f8d79a6bc34734b195c7
e096358c832ab292ddfd22dd5878826c7c788968
475831f0fb0e31e97f630eac4e078c886558b61c
# Ran SystemTests and python/ctsm through black python formatter
5364ad66eaceb55dde2d3d598fe4ce37ac83a93c
8056ae649c1b37f5e10aaaac79005d6e3a8b2380
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions python/ctsm/site_and_regional/single_point_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# -- import local classes for this script
from ctsm.site_and_regional.base_case import BaseCase, USRDAT_DIR, DatmFiles
from ctsm.utils import add_tag_to_filename
from ctsm.utils import add_tag_to_filename, ensure_iterable

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -415,7 +415,10 @@ def modify_surfdata_atpoint(self, f_orig):
# f_mod["PCT_CROP"][:, :] = 0

# -- loop over all dom_pft and pct_pft
zip_pfts = zip(self.dom_pft, self.pct_pft, self.cth, self.cbh)
iterable_length = len(self.dom_pft)
cth_to_zip = ensure_iterable(self.cth, iterable_length)
cbh_to_zip = ensure_iterable(self.cbh, iterable_length)
zip_pfts = zip(self.dom_pft, self.pct_pft, cth_to_zip, cbh_to_zip)
for dom_pft, pct_pft, cth, cbh in zip_pfts:
if cth is not None:
f_mod["MONTHLY_HEIGHT_TOP"][:, :, :, dom_pft] = cth
Expand Down
8 changes: 4 additions & 4 deletions python/ctsm/test/test_unit_singlept_data_surfdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class TestSinglePointCaseSurfaceNoCrop(unittest.TestCase):
evenly_split_cropland = False
pct_pft = None
num_pft = 16
cth = [0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]
cbh = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
cth = 0.9
cbh = 0.1
include_nonveg = False
uni_snow = True
cap_saturation = True
Expand Down Expand Up @@ -668,8 +668,8 @@ class TestSinglePointCaseSurfaceCrop(unittest.TestCase):
evenly_split_cropland = False
pct_pft = None
num_pft = 78
cth = [0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]
cbh = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
cth = 0.9
cbh = 0.1
include_nonveg = False
uni_snow = False
cap_saturation = False
Expand Down
34 changes: 33 additions & 1 deletion python/ctsm/test/test_unit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os

from ctsm import unit_testing
from ctsm.utils import fill_template_file
from ctsm.utils import fill_template_file, ensure_iterable
from ctsm.config_utils import lon_range_0_to_360, _handle_config_value

# Allow names that pylint doesn't like, because otherwise I find it hard
Expand Down Expand Up @@ -328,6 +328,38 @@ def test_handleConfigValue_isListFalse(self):
self.assertEqual(val_out, float(val_in))


class TestUtilsEnsureIterable(unittest.TestCase):
"""Tests of utils: ensure_iterable"""

def test_ensure_iterable_number(self):
"""
Tests that ensure_iterable(NUMBER, 3) produces a list of 3 NUMBERs
"""
val = 724.1987
self.assertEqual(ensure_iterable(val, 3), [val, val, val])

def test_ensure_iterable_none(self):
"""
Tests that ensure_iterable(None, 2) produces a list of 2 Nones
"""
val = None
self.assertEqual(ensure_iterable(val, 2), [val, val])

def test_ensure_iterable_already(self):
"""
Tests that ensure_iterable() returns the input if it's already iterable
"""
val = [11, 12]
self.assertEqual(ensure_iterable(val, 2), val)

def test_ensure_iterable_error_wrong_length(self):
"""
Tests that ensure_iterable() errors if input is iterable but of the wrong length
"""
with self.assertRaisesRegex(ValueError, "Input is iterable but wrong length"):
ensure_iterable([11, 12], 3)


if __name__ == "__main__":
unit_testing.setup_for_tests()
unittest.main()
18 changes: 18 additions & 0 deletions python/ctsm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ def abort(errmsg):
sys.exit("ERROR: {}".format(errmsg))


def ensure_iterable(thing_we_want_iterable, iterable_length):
"""
Ensure that a variable is iterable
"""
already_iterable = True
try:
iter(thing_we_want_iterable)
except TypeError:
already_iterable = False

if not already_iterable:
thing_we_want_iterable = [thing_we_want_iterable] * iterable_length
elif len(thing_we_want_iterable) != iterable_length:
raise ValueError("Input is iterable but wrong length")

return thing_we_want_iterable


def fill_template_file(path_to_template, path_to_final, substitutions):
"""Given a template file (based on python's template strings), write a copy of the
file with template values filled in.
Expand Down
2 changes: 1 addition & 1 deletion src/biogeophys/SurfaceAlbedoType.F90
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ subroutine InitHistory(this, bounds)

this%azsun_grc(begg:endg) = spval
call hist_addfld1d (fname='AZSUN', units='radians', &
avgflag='A', long_name='cosine of solar zenith angle', &
avgflag='A', long_name='azimuth angle of the sun', &
ptr_lnd=this%azsun_grc, default='inactive')

this%coszen_grc(begg:endg) = spval
Expand Down
2 changes: 1 addition & 1 deletion src/biogeophys/WaterDiagnosticBulkType.F90
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ subroutine InitBulkHistory(this, bounds)
fname=this%info%fname('RH2M_R'), &
units='%', &
avgflag='A', &
long_name=this%info%lname('Rural 2m specific humidity'), &
long_name=this%info%lname('Rural 2m relative humidity'), &
ptr_patch=this%rh_ref2m_r_patch, set_spec=spval, default='inactive')

this%rh_ref2m_u_patch(begp:endp) = spval
Expand Down

0 comments on commit 9a28e2a

Please sign in to comment.