Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
let only write if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Daafip committed Mar 29, 2024
1 parent a79b762 commit fc4bf04
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/ewatercycle_HBV/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import xarray as xr
import warnings
import os
from collections.abc import ItemsView
from pathlib import Path
from typing import Any, Type
Expand Down Expand Up @@ -72,24 +73,27 @@ def _make_cfg_file(self, **kwargs) -> Path:
raise UserWarning("Generic Lumped Forcing does not provide potential evaporation, which this model needs")

elif type(self.forcing).__name__ == 'LumpedMakkinkForcing':
ds = xr.open_dataset(self.forcing.directory / self.forcing.filenames['evspsblpot'])
attributes = ds['evspsblpot'].attrs
attributes['units'] = 'mm'
ds = ds.rename({'evspsblpot': 'pev'})
ds['pev'] = ds['pev'] * 86400
ds['pev'].attrs = attributes
temporary_pev_file = self.forcing.directory / self.forcing.filenames['evspsblpot'].replace('evspsblpot', 'pev_mm')
ds.to_netcdf(temporary_pev_file)
ds.close()

ds = xr.open_dataset(self.forcing.directory / self.forcing.filenames['pr'])
attributes = ds['pr'].attrs
attributes['units'] = 'mm'
ds['pr'] = ds['pr'] * 86400
ds['pr'].attrs = attributes
temporary_pr_file = self.forcing.directory / self.forcing.filenames['pr'].replace('pr', 'pr_mm')
ds.to_netcdf(temporary_pr_file)
ds.close()
temporary_pev_file = self.forcing.directory / self.forcing.filenames['evspsblpot'].replace('evspsblpot',
'pev_mm')
if not temporary_pev_file.is_file():
ds = xr.open_dataset(self.forcing.directory / self.forcing.filenames['evspsblpot'])
attributes = ds['evspsblpot'].attrs
attributes['units'] = 'mm'
ds = ds.rename({'evspsblpot': 'pev'})
ds['pev'] = ds['pev'] * 86400
ds['pev'].attrs = attributes
ds.to_netcdf(temporary_pev_file)
ds.close()

temporary_pr_file = self.forcing.directory / self.forcing.filenames['pr'].replace('pr', 'pr_mm')
if not temporary_pr_file.is_file():
ds = xr.open_dataset(self.forcing.directory / self.forcing.filenames['pr'])
attributes = ds['pr'].attrs
attributes['units'] = 'mm'
ds['pr'] = ds['pr'] * 86400
ds['pr'].attrs = attributes
ds.to_netcdf(temporary_pr_file)
ds.close()

self._config["precipitation_file"] = str(
temporary_pr_file
Expand Down

0 comments on commit fc4bf04

Please sign in to comment.