-
Notifications
You must be signed in to change notification settings - Fork 22
Remove cdo/python-cdo from fre-cli, add xarray time averager #830
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
Open
Copilot
wants to merge
22
commits into
main
Choose a base branch
from
copilot/deprecate-cdo-python-cdo-use
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
13cd040
deprecate cdo/python-cdo: replace CDO usage with xarray, add deprecat…
Copilot 8f5552e
revert unrelated JSON file changes
Copilot 5439a08
address code review: use xarray .dt accessor and context managers
Copilot 0443782
remove CDO, add xarray averager, rename frepytools to NumpyTimeAverag…
Copilot 4c91d82
fix: handle non-numeric vars and cftime time_bnds in xarrayTimeAverag…
Copilot e7f465b
test: add comprehensive unit tests for xarrayTimeAverager and add wei…
Copilot 164c394
test: address code review — rename bnds_dtype → time_bnds_encoding, f…
Copilot c8fdd85
fix: NumpyTimeAverager now handles arbitrary dimensions (scalar, 3D, …
Copilot 61babc7
some additional logging is helpful
ilaflott 80ecff9
put default back to cdo to check for smooth transition behavior. add …
ilaflott 5e603f3
refactor: move NumpyTimeAverager to numpyTimeAverager.py, add monthly…
Copilot 7689a02
revert unrelated CMOR test file changes from test runs
Copilot 381ba00
fix: typo in numpyTimeAverager.py comment
Copilot 50482c9
fix: strip stale unlimited_dims encoding from xarrayTimeAverager outp…
Copilot 8563837
feat: add rigorous numerical accuracy tests and fre-python-tools mont…
Copilot 473849f
refactor: derive unlimited_dims from encoding rather than hardcoding …
Copilot 09c34e4
feat: add test_numpyTimeAverager.py covering uncovered exception/erro…
Copilot b5dcf5a
feat: add cross-pkg weighted averaging bitwise reproducibility tests …
Copilot f88e5e6
fix: correct rtol value in cross-pkg test docstring from 1e-12 to 1e-6
Copilot b18aadc
fix: correctly reduce time-metadata variables (time_bnds, time, avera…
Copilot 2333bbb
update frenctools timeaverager tests
ilaflott 99729f4
docs: update time averager documentation — README.md, docs/tools/app.…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| '''required for generate_time_averages module import functionality''' | ||
| __all__ = ['generate_time_averages', 'timeAverager', 'wrapper', 'combine', | ||
| 'frenctoolsTimeAverager', 'cdoTimeAverager', 'frepytoolsTimeAverager'] | ||
| 'frenctoolsTimeAverager', 'cdoTimeAverager', 'frepytoolsTimeAverager', | ||
| 'xarrayTimeAverager'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,35 @@ | ||
| ''' class using (mostly) cdo functions for time-averages ''' | ||
| ''' stub that redirects pkg='cdo' requests to the xarray time averager ''' | ||
|
|
||
| import logging | ||
| import warnings | ||
|
|
||
| from netCDF4 import Dataset | ||
| import numpy as np | ||
|
|
||
| import cdo | ||
| from cdo import Cdo | ||
|
|
||
| from .timeAverager import timeAverager | ||
| from .xarrayTimeAverager import xarrayTimeAverager | ||
|
|
||
| fre_logger = logging.getLogger(__name__) | ||
|
|
||
| class cdoTimeAverager(timeAverager): | ||
|
|
||
| class cdoTimeAverager(xarrayTimeAverager): # pylint: disable=invalid-name | ||
| ''' | ||
| class inheriting from abstract base class timeAverager | ||
| generates time-averages using cdo (mostly, see weighted approach) | ||
| Legacy entry-point kept for backward compatibility. | ||
| CDO/python-cdo has been removed. All work is now done by xarrayTimeAverager. | ||
| ''' | ||
|
|
||
| def generate_timavg(self, infile = None, outfile = None): | ||
| def generate_timavg(self, infile=None, outfile=None): | ||
| """ | ||
| use cdo package routines via python bindings | ||
| Emit a loud warning then delegate to the xarray implementation. | ||
|
|
||
| :param self: This is an instance of the class cdoTimeAverager | ||
| :param infile: path to history file, or list of paths, default is None | ||
| :type infile: str, list | ||
| :param outfile: path to where output file should be stored, default is None | ||
| :param infile: path to input NetCDF file | ||
| :type infile: str | ||
| :param outfile: path to output file | ||
| :type outfile: str | ||
| :return: 1 if the instance variable self.avg_typ is unsupported, 0 if function has a clean exit | ||
| :return: 0 on success | ||
| :rtype: int | ||
| """ | ||
|
|
||
| if self.avg_type not in ['all', 'seas', 'month']: | ||
| fre_logger.error('requested unknown avg_type %s.', self.avg_type) | ||
| raise ValueError(f'requested unknown avg_type {self.avg_type}') | ||
|
|
||
| if self.var is not None: | ||
| fre_logger.warning('WARNING: variable specification not twr supported for cdo time averaging. ignoring!') | ||
|
|
||
| fre_logger.info('python-cdo version is %s', cdo.__version__) | ||
|
|
||
| _cdo = Cdo() | ||
|
|
||
| wgts_sum = 0 | ||
| if not self.unwgt: #weighted case, cdo ops alone don't support a weighted time-average. | ||
|
|
||
| nc_fin = Dataset(infile, 'r') | ||
|
|
||
| time_bnds = nc_fin['time_bnds'][:].copy() | ||
| # Ensure float64 precision for consistent results across numpy versions | ||
| # NumPy 2.0 changed type promotion rules (NEP 50), so explicit casting | ||
| # is needed to avoid precision differences | ||
| time_bnds = np.asarray(time_bnds, dtype=np.float64) | ||
| # Transpose once to avoid redundant operations | ||
| time_bnds_transposed = np.moveaxis(time_bnds, 0, -1) | ||
| wgts = time_bnds_transposed[1] - time_bnds_transposed[0] | ||
| # Use numpy.sum for consistent dtype handling across numpy versions | ||
| wgts_sum = np.sum(wgts, dtype=np.float64) | ||
|
|
||
| fre_logger.debug('wgts_sum = %s', wgts_sum) | ||
|
|
||
| if self.avg_type == 'all': | ||
| fre_logger.info('time average over all time requested.') | ||
| if self.unwgt: | ||
| _cdo.timmean(input = infile, output = outfile, returnCdf = True) | ||
| else: | ||
| _cdo.divc( str(wgts_sum), input = "-timsum -muldpm "+infile, output = outfile) | ||
| fre_logger.info('done averaging over all time.') | ||
|
|
||
| elif self.avg_type == 'seas': | ||
| fre_logger.info('seasonal time-averages requested.') | ||
| _cdo.yseasmean(input = infile, output = outfile, returnCdf = True) | ||
| fre_logger.info('done averaging over seasons.') | ||
|
|
||
| elif self.avg_type == 'month': | ||
| fre_logger.info('monthly time-averages requested.') | ||
| outfile_str = str(outfile) | ||
| _cdo.ymonmean(input = infile, output = outfile_str, returnCdf = True) | ||
| fre_logger.info('done averaging over months.') | ||
|
|
||
| fre_logger.warning(" splitting by month") | ||
| outfile_root = outfile_str.removesuffix(".nc") + '.' | ||
| _cdo.splitmon(input = outfile_str, output = outfile_root) | ||
| fre_logger.debug('Done with splitting by month, outfile_root = %s', outfile_root) | ||
|
|
||
| fre_logger.info('done averaging') | ||
| fre_logger.info('output file created: %s', outfile) | ||
| return 0 | ||
| msg = ( | ||
| "WARNING *** CDO/python-cdo has been REMOVED from fre-cli. " | ||
| "pkg='cdo' now uses the xarray time-averager under the hood. " | ||
| "Please switch to pkg='xarray' or pkg='fre-python-tools'. ***" | ||
| ) | ||
| warnings.warn(msg, FutureWarning, stacklevel=2) | ||
| fre_logger.warning(msg) | ||
| return super().generate_timavg(infile=infile, outfile=outfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.