-
Notifications
You must be signed in to change notification settings - Fork 21
fre app regrid edits for fre-workflows functionality
#669
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
base: main
Are you sure you want to change the base?
Changes from all commits
40a1e1c
2e711c1
21182fc
6d73be2
3be10fd
5ec043a
64a97f0
8c040bb
634cf02
c8f8499
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pprint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import tarfile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import xarray as xr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -10,6 +11,12 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger = logging.getLogger(__name__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # it appears that fregrid believes the grid_spec file must have a datetime attached to it, like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # YYYYMMDD.grid_spec.tile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # this is behavior in noaa-gfdl::fre-nctools==2022.02.01 that can't be changed, and so must be worked around. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # hence, "ATTACH_LEGACY_DT" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ATTACH_LEGACY_DT = True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # list of variables/fields that will not be regridded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| non_regriddable_variables = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "geolon_c", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -47,7 +54,6 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_grid_spec(datadict: dict) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Gets the grid_spec.nc file from the tar file specified in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| yaml["postprocess"]["settings"]["pp_grid_spec"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -66,30 +72,61 @@ def get_grid_spec(datadict: dict) -> str: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input mosaic filename | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #grid spec filename | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid_spec = "grid_spec.nc" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid_spec = str(Path("grid_spec.nc").resolve()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.info('grid spec filename is: %s', grid_spec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #get tar file containing the grid_spec file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pp_grid_spec_tar = datadict["yaml"]["postprocess"]["settings"]["pp_grid_spec"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug(f"Going to untar this grid spec tarfile: {pp_grid_spec_tar}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.info('grid spec tar archive file name is: %s', pp_grid_spec_tar) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #untar grid_spec tar file into the current work directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug('checking if %s is a tar file...', pp_grid_spec_tar) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if tarfile.is_tarfile(pp_grid_spec_tar): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug('it is a tar file! attempting top open %s', pp_grid_spec_tar) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with tarfile.open(pp_grid_spec_tar, "r") as tar: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug('opened! about to extract all from opened tar file object into %s', os.getcwd()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tar.extractall() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug('everything extracted!') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug('contents extracted are ... %s', str(os.listdir(os.getcwd())) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #error if grid_spec file is not found after extracting from tar file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not Path(grid_spec).exists(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise IOError(f"Cannot find {grid_spec} in tar file {pp_grid_spec_tar}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug(f"Current directory: {Path.cwd()}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug('grid_spec = %s exists!', grid_spec) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.debug(f"Found grid_spec file: {grid_spec}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return grid_spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid_spec_dt_symlink = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ATTACH_LEGACY_DT: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.warning('creating symlink to account for legacy fre-nctools 2022.02.01 behavior') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid_spec_dt_symlink = Path(grid_spec).parent / f'{datadict["input_date"]}.grid_spec.nc' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_input_mosaic(datadict: dict) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.warning('grid_spec_dt_symlink = %s', grid_spec_dt_symlink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # what in the ever loving demon magic is going on here??? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid_spec_dt_symlink.symlink_to(Path(grid_spec)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except FileExistsError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # i am sorry python gods, i have failed out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if grid_spec_dt_symlink.exists(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.warning('great? how did this happen?') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # continued inexplicable success. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if grid_spec_dt_symlink.is_symlink(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fre_logger.warning('symlink created: %s', grid_spec_dt_symlink) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise Exception('problem with accounting for legacy fregrid/fre-nctools behavior, symbolic link creation ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'as-intended-side-effect failed. consult life choices.') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+109
to
125
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # what in the ever loving demon magic is going on here??? | |
| try: | |
| grid_spec_dt_symlink.symlink_to(Path(grid_spec)) | |
| except FileExistsError: | |
| pass | |
| # i am sorry python gods, i have failed out | |
| if grid_spec_dt_symlink.exists(): | |
| fre_logger.warning('great? how did this happen?') | |
| # continued inexplicable success. | |
| if grid_spec_dt_symlink.is_symlink(): | |
| fre_logger.warning('symlink created: %s', grid_spec_dt_symlink) | |
| else: | |
| raise Exception('problem with accounting for legacy fregrid/fre-nctools behavior, symbolic link creation ' | |
| 'as-intended-side-effect failed. consult life choices.') | |
| # Attempt to create a symbolic link for legacy fre-nctools compatibility. | |
| try: | |
| grid_spec_dt_symlink.symlink_to(Path(grid_spec)) | |
| except FileExistsError: | |
| # Symlink already exists; proceed. | |
| pass | |
| # Check if the symlink exists after attempted creation. | |
| if grid_spec_dt_symlink.exists(): | |
| fre_logger.warning('Symlink exists: %s', grid_spec_dt_symlink) | |
| # Confirm that the symlink was created successfully. | |
| if grid_spec_dt_symlink.is_symlink(): | |
| fre_logger.warning('Symlink created: %s', grid_spec_dt_symlink) | |
| else: | |
| raise Exception( | |
| 'Failed to create symbolic link for legacy fregrid/fre-nctools behavior. ' | |
| 'Symbolic link creation did not succeed. Please check file permissions and whether the symlink already exists.' | |
| ) |
Copilot
AI
Nov 13, 2025
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.
Missing type annotation for the newly added input_dir parameter. The docstring documents it but the function signature should include : str type hint for consistency with other parameters.
Copilot
AI
Nov 13, 2025
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.
Variable grid_spec_symlink is not used.
| datadict["grid_spec"], grid_spec_symlink = get_grid_spec(datadict) | |
| datadict["grid_spec"] = get_grid_spec(datadict)[0] |
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.
Typo: "attempting top open" should be "attempting to open".