From f04872d77a62fa9f76355b1e2a608b7d328867f6 Mon Sep 17 00:00:00 2001 From: Trygve Aspelien Date: Thu, 9 Jan 2025 15:40:38 +0100 Subject: [PATCH] Set accumulated values to zero for first time step (#47) * Set accumulated values to zero for first time step * Bump python version since 3.8 is EOL * Bump python version since 3.8 is EOL * Black --- .github/workflows/linting.yaml | 2 +- .github/workflows/python-package.yml | 10 +++++----- pyproject.toml | 2 +- pysurfex/variable.py | 17 +++++++++++++++-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.github/workflows/linting.yaml b/.github/workflows/linting.yaml index 9bde1c7..72fe443 100644 --- a/.github/workflows/linting.yaml +++ b/.github/workflows/linting.yaml @@ -29,7 +29,7 @@ jobs: id: setup-python uses: actions/setup-python@v4 with: - python-version: '3.8' + python-version: '3.9' #---------------------------------------------- # --- configure poetry & install project ---- diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index c2964e8..e59437f 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -17,7 +17,7 @@ jobs: matrix: os: [ "ubuntu-latest" ] env: [ "pytest" ] - python-version: [ "3.8" ] + python-version: [ "3.9" ] name: "${{ matrix.os }}, python=${{ matrix.python-version }}" runs-on: ${{ matrix.os }} @@ -70,7 +70,7 @@ jobs: poetry run pytest - name: Coveralls - if: ${{ matrix.python-version == 3.8 }} + if: ${{ matrix.python-version == 3.9 }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -79,13 +79,13 @@ jobs: poetry run coveralls - name: Create documentation - if: ${{ matrix.python-version == 3.8 }} + if: ${{ matrix.python-version == 3.9 }} run: | cd docs poetry run python auto_sphinx.py make html - name: Commit documentation changes - if: ${{ matrix.python-version == 3.8 }} + if: ${{ matrix.python-version == 3.9 }} run: | git clone https://github.com/metno/pysurfex.git --branch gh-pages --single-branch gh-pages cp -r docs/build/html/* gh-pages/ @@ -97,7 +97,7 @@ jobs: # The above command will fail if no changes were present, so we ignore # the return code. - name: Push changes - if: ${{ matrix.python-version == 3.8 && github.event_name != 'pull_request' }} + if: ${{ matrix.python-version == 3.9 && github.event_name != 'pull_request' }} uses: ad-m/github-push-action@master with: branch: gh-pages diff --git a/pyproject.toml b/pyproject.toml index 9f2ffce..27993d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ requires = ["poetry-core > 1.2.0"] [tool.poetry.dependencies] - python = "^3.8" + python = "^3.9" dateutils = "^0.6.12" f90nml = "^1.4.3" humanize = ">=3.14.0" diff --git a/pysurfex/variable.py b/pysurfex/variable.py index f3cecf8..b92b255 100644 --- a/pysurfex/variable.py +++ b/pysurfex/variable.py @@ -394,6 +394,7 @@ def read_variable(self, geo, validtime, cache=None): """ # Set variable info previous_field = None + first_time = False if self.accumulated: # Re-read field previoustime = validtime - as_timedelta(seconds=self.interval) @@ -408,14 +409,26 @@ def read_variable(self, geo, validtime, cache=None): cache=cache, ) else: + first_time = True previous_field = np.zeros([geo.npoints]) elif self.instant > 0: previous_field = np.zeros([geo.npoints]) - field = self.read_var_points(self.file_var, geo, validtime=validtime, cache=cache) + deaccumulate = False + if self.accumulated or self.instant > 0: + deaccumulate = True + + # Always set accumulated values to zero the first time + if first_time: + deaccumulate = False + field = np.zeros([geo.npoints]) + else: + field = self.read_var_points( + self.file_var, geo, validtime=validtime, cache=cache + ) # Deaccumulate if either two files are read or if instant is > 0. - if self.accumulated or self.instant > 0: + if deaccumulate: field = self.deaccumulate(field, previous_field, self.instant) if any(field[field < 0.0]): raise RuntimeError(