Skip to content

Add MONTHS_PER_YEAR constant and use it in cplhist_monthly_clim - #4310

Open
michellejtan wants to merge 3 commits into
ESCOMP:b4b-devfrom
michellejtan:constants-module
Open

michellejtan wants to merge 3 commits into
ESCOMP:b4b-devfrom
michellejtan:constants-module

Conversation

@michellejtan

Copy link
Copy Markdown

Description of changes

Adds a MONTHS_PER_YEAR constant in a new python/ctsm/constants.py and uses it in place of three hard-coded range(1, 13) month loops in python/ctsm/cplhist/cplhist_monthly_clim.py (year_months(), the averaging step, and the step that joins the monthly files). The PFT constants that #1621 also mentions were already moved to python/ctsm/pft_utils.py in June 2025, so I did not touch them.

Two small leftovers from that earlier PFT work are fixed in python/ctsm/site_and_regional/single_point_case.py, in their own commit so they are easy to drop if you would rather not have them here:

  • the num_pft docstring said "78 pft, else 16 pft"; it now names MAX_PFT_MANAGEDCROPS and MAX_PFT_GENERICCROPS.
  • the --dompft error said "between 1 and 78", but check_dom_pft() accepts 0 (bare ground); it now uses MIN_PFT, so it says "between 0 and 78".

I did not change pft_utils.py, the "NETCDF4_CLASSIC" string that is repeated in several modules, NOLEAP_DAYS, the range(1, 13) calls in the test files, or end_month = 12 in run_tower.py.
I asked on the issue where MONTHS_PER_YEAR should live and did not get an answer.
My proposal location: (python/ctsm/constants.py). I am happy to move it.

Before (b4b-dev at 0efa002):

$ grep -rn "MONTHS_PER_YEAR" --include="*.py" . ; echo "exit=$?"
exit=1
$ grep -rn "range(1, 13)" --include="*.py" python/ctsm | grep -v /test/
python/ctsm/cplhist/cplhist_monthly_clim.py:130:    return [(year, month) for year in range(begyr, endyr + 1) for month in range(1, 13)]
python/ctsm/cplhist/cplhist_monthly_clim.py:751:            for month in range(1, 13)
python/ctsm/cplhist/cplhist_monthly_clim.py:833:        clim_files = [clim_file(self.mondir, month) for month in range(1, 13)]

After:

$ grep -rn "range(1, 13)" --include="*.py" python/ctsm | grep -v /test/ ; echo "exit=$?"
exit=1
$ grep -rn "MONTHS_PER_YEAR" --include="*.py" python/ctsm | grep -v /test/
python/ctsm/constants.py:13:MONTHS_PER_YEAR: Final[int] = 12
python/ctsm/cplhist/cplhist_monthly_clim.py:44:from ctsm.constants import MONTHS_PER_YEAR
python/ctsm/cplhist/cplhist_monthly_clim.py:132:        (year, month) for year in range(begyr, endyr + 1) for month in range(1, MONTHS_PER_YEAR + 1)
python/ctsm/cplhist/cplhist_monthly_clim.py:754:            for month in range(1, MONTHS_PER_YEAR + 1)
python/ctsm/cplhist/cplhist_monthly_clim.py:836:        clim_files = [clim_file(self.mondir, month) for month in range(1, MONTHS_PER_YEAR + 1)]

Specific notes

Contributors other than yourself, if any:

  • None

CTSM issues resolved or otherwise addressed, if any:

If answers are expected to change, describe (delete this line otherwise):
Not applicable: no answers change (python tools only).

Any user interface changes (namelist or namelist defaults changes)?
No namelist changes. The only user-visible change is the wording of the --dompft error in subset_data (it now says "between 0 and 78" instead of "between 1 and 78").

Testing planned or performed, if any:

  • Two new tests fail before the change and pass after: test_unit_constants.py (ImportError before constants.py existed) and test_check_dom_pft_message_lower_bound ("...between 0 and" does not match "...between 1 and 78." before the fix).
  • A third new test, test_noleapDaysMatchMonthsPerYear, checks that NOLEAP_DAYS and year_months() have MONTHS_PER_YEAR entries. It passes before and after; it guards against drift.
  • Python unit tests, from python/: ./run_ctsm_py_tests -u in the ctsm_pylib conda environment: 680 passed (677 passed on b4b-dev before my changes, plus my 3).
  • make -C python black: 182 files left unchanged. make -C python lint: 10.00/10.
  • Python system tests: not run. The CI python-tests workflow runs only the unit tests, and when I tried test_sys_aero_1850_monthly_clim.py (the one that exercises cplhist_monthly_clim) all 5 tests were skipped because NCO (ncra, ncrcat, ncatted) is not installed on my machine.

AI disclosure

  • Was AI used in this contribution? Yes
  • Types of work: exploring the codebase and the issue history, drafting the code and tests. I read the diff, made edits where needed, and ran the checks CI runs (unit tests, black and pylint) myself.
  • Model (optional): Claude Sonnet 5.5, through Claude Code

Requirements before merge:

  • I have followed the CTSM contribution guidelines.
  • The code in this PR branch builds with no errors. N/A: python-only change; I did not build the model.
  • The code in this PR branch runs with no errors. Briefly describe tested configuration(s): the python unit tests (680 pass), black and pylint, in the ctsm_pylib conda environment on Ubuntu (WSL2), Python 3.13.13.
  • This either (a) does not change answers, (b) it only changes answers at roundoff level, or (c) I have performed a scientific evaluation of the answer changes. Which?: (a)
  • I have reviewed relevant parts of the CLM documentation Tech Note or User's Guide to determine if anything needs to be changed or added. If it does, describe: I searched doc/ for the old error text and for --dompft; the single-point user guide mentions --dompft but not its range or the error message, and the Tech Note has no mention of these tools, so nothing needs to change.
  • This PR either (a) does not create a need to update the documentation or (b) includes required documentation updates (see guidelines for contributing documentation). Which?: (a)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant