Skip to content

Commit b3dbd19

Browse files
committed
Merge branch 'testing'
2 parents 7db0d24 + 6137ae5 commit b3dbd19

File tree

4 files changed

+300
-46
lines changed

4 files changed

+300
-46
lines changed

src/efts_io/_ncdf_stf2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def _create_cf_time_axis(data: xr.DataArray, timestep_str: str) -> tuple[np.ndar
9393
units = f"{timestep_str} since {formatted_string_with_tz}"
9494
return axis, units, calendar
9595

96+
9697
def _validate_station_id_for_int32(station_id: np.ndarray, intdata_type: str) -> None:
9798
"""Validate that station_id values can be safely stored as int32.
9899

@@ -114,6 +115,7 @@ def _validate_station_id_for_int32(station_id: np.ndarray, intdata_type: str) ->
114115
f"station_id values must be in the int32 range [{np.iinfo(np.int32).min}, {np.iinfo(np.int32).max}] to be stored in STF2.0 format.",
115116
)
116117

118+
117119
def write_nc_stf2(
118120
out_nc_file: str,
119121
dataset: xr.Dataset,
@@ -481,7 +483,8 @@ def add_optional_variables(data: xr.DataArray, ncfile: Dataset, var_id: str) ->
481483
# This prevents double-close in the exception handler
482484
ncfile.close()
483485

484-
def _get_stationid_data_types(stf_nc_vers: Any, d_type:np.ndarray, d_type_long:np.ndarray) -> None:
486+
487+
def _get_stationid_data_types(stf_nc_vers: Any, d_type: np.ndarray, d_type_long: np.ndarray) -> None:
485488
if int(stf_nc_vers) == 1:
486489
d_type[1] = "fcast"
487490
d_type_long[1] = "forecast"

src/efts_io/wrapper.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def load_from_stf2_file(file_path: str, time_zone_timestamps: bool) -> xr.Datase
8787

8888
# work around https://jira.csiro.au/browse/WIRADA-635
8989
# lead_time can be a problem with xarray, so do not decode "times"
90-
x = xr.open_dataset(file_path, decode_times=False)
91-
90+
# we also use mask_and_scale=False because of https://github.com/csiro-hydroinformatics/efts-io/issues/24
91+
x = xr.open_dataset(file_path, decode_times=False, mask_and_scale=False)
9292
# replace the time and station names coordinates values
9393
# TODO This is probably not a long term solution for round-tripping a read/write or vice and versa
9494
decod = times.CFDatetimeCoder(use_cftime=True)
@@ -181,11 +181,19 @@ def __init__(self, data: Union[str, xr.Dataset]) -> None:
181181
self.ENS_MEMBER_DIMNAME = ENS_MEMBER_DIMNAME
182182
# self.identifiers_dimensions: list = []
183183
self.data: xr.Dataset
184+
from pathlib import Path
185+
186+
if data is None:
187+
raise ValueError("input cannot be None")
188+
if isinstance(data, Path):
189+
data = str(data)
184190
if isinstance(data, str):
185191
new_dataset = load_from_stf2_file(data, self.time_zone_timestamps)
186192
self.data = new_dataset
187-
else:
193+
elif isinstance(data, xr.Dataset):
188194
self.data = data
195+
else:
196+
raise TypeError(f"Unsupported type {type(data)}")
189197

190198
self.stf2_int_datatype = "i4" # default integer type for STF2 saving
191199

@@ -275,8 +283,10 @@ def append_history(self, message: str, timestamp: Optional[datetime] = None) ->
275283
message: The message to append.
276284
timestamp: If not provided, the current UTC time is used.
277285
"""
286+
from datetime import UTC
287+
278288
if timestamp is None:
279-
timestamp = datetime.now(datetime.timezone.utc).isoformat()
289+
timestamp = datetime.now(UTC).isoformat()
280290

281291
current_history = self.data.attrs.get(HISTORY_ATTR_KEY, "")
282292
if current_history:

tests/test_create.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def _create_test_ds():
126126
},
127127
}
128128
)
129-
# 2025-11 It has been decided to support transparent conversion of string station
130-
# IDs to integers on save for STF2.0.
129+
# 2025-11 It has been decided to support transparent conversion of string station
130+
# IDs to integers on save for STF2.0.
131131
# It was otherwise confusing for users.
132132
# besides it helps to promote the use of string station IDs in memory datasets.
133133
assert w.writeable_to_stf2()
@@ -192,7 +192,7 @@ def _saving_to_stf2(station_ids, intdata_type="i4", delete=True):
192192
# create a temporary file
193193
import tempfile
194194

195-
# save to STF2.0 will clean up the file if write fails,
195+
# save to STF2.0 will clean up the file if write fails,
196196
# so in that case we should allow for deletion to be True or False
197197
with tempfile.NamedTemporaryFile(suffix=".nc", delete=delete) as tmp:
198198
filename = tmp.name

0 commit comments

Comments
 (0)