Skip to content

Conversation

samsrabin
Copy link
Member

@samsrabin samsrabin commented Aug 22, 2025

Description of changes

Enables using instantaneous instead of averaged outputs for GDD generation. Also includes some relevant performance improvements and testing.

Specific notes

Contributors other than yourself, if any: None

CTSM Issues Fixed:

Are answers expected to change (and if so in what way)? No

Any User Interface Changes (namelist or namelist defaults changes)? No

Does this create a need to change or add documentation? Did you do so? No

Testing performed, if any:

  • Python formatting, unit, and system tests pass as of f1bbda2.
  • Some testing on a different branch from which this one has been cherry-picked.
  • Will run rxcropmaturity to confirm successful completion (and no diffs for tests that have baseline)
  • Will run clm_pymods suites to confirm no diffs

samsrabin and others added 11 commits August 22, 2025 16:40
…DF3.

Python has trouble writing large netCDF3 files. This makes it so it saves a netCDF4 file first, just to get *something*, then tries to convert to netCDF3 using nccopy. If that conversion fails for any reason, no error is raised, but a message is printed either way.
# Conflicts:
#	python/ctsm/crop_calendars/cropcal_utils.py
#	python/ctsm/crop_calendars/generate_gdds_functions.py
@samsrabin samsrabin self-assigned this Aug 22, 2025
@samsrabin samsrabin added blocker another issue/PR depends on this one bfb bit-for-bit test: python Pass clm_pymods test suite plus Python sys/unit tests before merging labels Aug 22, 2025
@samsrabin samsrabin linked an issue Aug 22, 2025 that may be closed by this pull request
if casebaseid.split("-")[-1] != "cropMonthOutput":
if casebaseid.split("-")[-1] != "cropMonthOutput" and not casebaseid.endswith(
"clm-cropMonthOutput--clm-h2a"
):
Copy link
Member Author

@samsrabin samsrabin Aug 23, 2025

Choose a reason for hiding this comment

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

Add comment explaining this.

def convert_netcdf_to_classic(filepath):
"""
Try to convert netCDF to netCDF-3 Classic format using nccopy.
"""
Copy link
Member Author

Choose a reason for hiding this comment

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

convert_netcdf_to_classic() should be moved to netcdf_utils and unit-tested.

temp_path = tmp.name

try:
subprocess.run(["nccopy", "-3", filepath, temp_path], check=True)
Copy link
Member Author

Choose a reason for hiding this comment

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

Use xarray's Dataset.to_netcdf() instead?

patterns = [f"*h2i.{this_year-1}-01*.nc", f"*h2i.{this_year-1}-01*.nc.base"]

# TODO: Undo this, replacing with just h2i or h2a
patterns += [f"*h2a.{this_year-1}-01*.nc", f"*h2a.{this_year-1}-01*.nc.base"]
Copy link
Member Author

Choose a reason for hiding this comment

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

Look for h2i files first, falling back to h2a files if (a) h2i files not found or (b) variable not in h2i files.

if convert_netcdf_to_classic(file_out):
logger.info("Conversion succeeded")
else:
logger.info("Conversion failed, perhaps because nccopy wasn't found in your shell")
Copy link
Member Author

Choose a reason for hiding this comment

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

  • Check whether nccopy is available in shell before the initial file.to_netcdf() call. If it's not, just skip straight to old behavior of "NETCDF3_64BIT".
  • If conversion fails, try saving again via file.to_netcdf(), this time straight to "NETCDF3_64BIT".

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @samsrabin I'm just wondering the reasons behind the convert to classic being done here?

I don't know of any compelling reasons to prefer classic over other formats. And classic has some pretty strong restrictions. And it seems preferable to me to write it out in the preferred format from the get go rather than convert. But I would like to hear the thinking here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think I meant for this all to be netCDF-3 64-bit. Will fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bfb bit-for-bit blocker another issue/PR depends on this one test: python Pass clm_pymods test suite plus Python sys/unit tests before merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fixes needed to use h2i files in GDD-generating Python scripts
2 participants