From 788f823b1c665e35a1124267d2564170de36f442 Mon Sep 17 00:00:00 2001 From: Rob Cermak Date: Sat, 28 Jan 2023 21:40:45 -0500 Subject: [PATCH 1/2] Fix for allowing a weight file to be written when it does not exist. --- xesmf/frontend.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/xesmf/frontend.py b/xesmf/frontend.py index 70e601dd..2122d8ec 100644 --- a/xesmf/frontend.py +++ b/xesmf/frontend.py @@ -4,6 +4,7 @@ import warnings +import os import cf_xarray as cfxr import numpy as np import xarray as xr @@ -319,11 +320,31 @@ def __init__( if reuse_weights and (filename is None) and (weights is None): raise ValueError('To reuse weights, you need to provide either filename or weights.') + # Apparent use rules: + # if reuse_weights is False, use weights if provided, else compute + # if reuse_weights is True, filename is optional, prefer use of + # weights argument. The difference is, if a filename is passed + # to weights it MUST exist. If a filename is passed to filename, + # if it does not exist, this will allow weights to be computed + # in the first iteration. If weights and filename are specified, + # filename will override the weights argument. if not reuse_weights and weights is None: weights = self._compute_weights() # Dictionary of weights else: - weights = filename if filename is not None else weights - + # If a filename is specified, override the weights argument + # otherwise, the weights argument is ultimately used. If + # the filename does not exist, allow it to be created on + # first pass. + if filename: + if os.path.isfile(filename): + weights = filename + else: + weights = self._compute_weights() # Dictionary of weights + # Temporarily change this to False to trigger saving weights + reuse_weights = False + + # The weights have to be computed, specified as a filename, + # or provided by a defined weights argument. assert weights is not None # Convert weights, whatever their format, to a sparse coo matrix From c6e3f31d5d3e006dc87e7df814d352911936a747 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Jan 2023 03:08:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xesmf/frontend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xesmf/frontend.py b/xesmf/frontend.py index 2122d8ec..84f88332 100644 --- a/xesmf/frontend.py +++ b/xesmf/frontend.py @@ -2,9 +2,9 @@ Frontend for xESMF, exposed to users. """ +import os import warnings -import os import cf_xarray as cfxr import numpy as np import xarray as xr