Skip to content

Commit 181afe7

Browse files
committed
Remove the datestring from .mat files.
1 parent 6a0d024 commit 181afe7

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

PyStemmusScope/forcing_io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hdf5storage
33
import numpy as np
44
import xarray as xr
5+
from . import utils
56
from . import variable_conversion as vc
67

78

@@ -159,7 +160,8 @@ def prepare_global_variables(data, input_path, config):
159160

160161
matfiledata['Dur_tot'] = float(total_duration) # Matlab expects a 'double'
161162

162-
hdf5storage.savemat(input_path / 'forcing_globals.mat', matfiledata, appendmat=False)
163+
hdf5storage.savemat(input_path / "forcing_globals.mat", matfiledata, appendmat=False)
164+
utils.sanitize_mat_file(input_path / "forcing_globals.mat")
163165

164166

165167
def prepare_forcing(config):

PyStemmusScope/soil_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,4 @@ def prepare_soil_data(config):
227227
hdf5storage.savemat(
228228
Path(config["InputPath"]) / "soil_parameters.mat", mdict=matfiledata, appendmat=False,
229229
)
230+
utils.sanitize_mat_file(Path(config["InputPath"]) / "soil_parameters.mat")

PyStemmusScope/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,23 @@ def to_absolute_path(
6767
must_exist = os_name() == 'nt'
6868

6969
return pathlike.expanduser().resolve(strict=must_exist)
70+
71+
72+
def sanitize_mat_file(filename):
73+
with open(filename, "rb") as f:
74+
data = f.read()
75+
76+
# Get locations of date string in header
77+
start_datestring = data[:128].find(b"Created on:") + 12
78+
end_datestring = data[:128].find(b"HDF5") - 1
79+
80+
# Rebuild the data, with the dates in the header removed
81+
sanitized_data = (
82+
data[:start_datestring] +
83+
b' '*len(data[start_datestring:end_datestring]) +
84+
data[end_datestring:]
85+
)
86+
87+
# Overwrite the old file
88+
with open(filename, 'wb') as file:
89+
file.write(sanitized_data)

0 commit comments

Comments
 (0)