Skip to content

Commit 5518c50

Browse files
authored
Merge pull request #192 from interob/more-robust-init-rawh5
asserts availability of data points for given nsmooth
2 parents d50b81b + feb507a commit 5518c50

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

modape/modis/smooth.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
# pylint: disable=import-error
1010
from array import array
11-
from collections import namedtuple
1211
from datetime import datetime, timedelta
1312
from pathlib import Path
1413

@@ -87,7 +86,9 @@ def __init__(
8786

8887
# Filename for smoothed HDF5
8988
rawfile_trunk = self.rawfile.name.split(".")
90-
smoothfile_trunk = ".".join(rawfile_trunk[:-2] + ["tx" + txflag] + rawfile_trunk[-2:-1])
89+
smoothfile_trunk = ".".join(
90+
rawfile_trunk[:-2] + ["tx" + txflag] + rawfile_trunk[-2:-1]
91+
)
9192

9293
filename = f"{targetdir}/{smoothfile_trunk}.h5"
9394

@@ -148,7 +149,6 @@ class _EmptyDateHelper:
148149

149150
try:
150151
with h5py.File(self.filename, "x", libver="latest") as h5f:
151-
152152
dset = h5f.create_dataset(
153153
"data",
154154
shape=(rawshape[0], dates_length),
@@ -173,7 +173,9 @@ class _EmptyDateHelper:
173173
try:
174174
# Check projection:
175175
assert (
176-
osr.SpatialReference(raw_attrs["projection"]).IsSame(ds.GetSpatialRef())
176+
osr.SpatialReference(raw_attrs["projection"]).IsSame(
177+
ds.GetSpatialRef()
178+
)
177179
== 1
178180
)
179181
h5gt = tuple(raw_attrs["geotransform"])
@@ -320,7 +322,9 @@ def smooth(
320322
# Resize if date list is bigger than shape of smoothed data
321323
if dates.target_length > smt_shape[1]:
322324
log.debug(
323-
"Resizing dataset! Current %s, required %s", smt_shape[1], dates.target_length
325+
"Resizing dataset! Current %s, required %s",
326+
smt_shape[1],
327+
dates.target_length,
324328
)
325329
smt_dates = h5f_open.get("dates")
326330
smt_dates.resize((dates.target_length,))
@@ -335,6 +339,10 @@ def smooth(
335339

336340
if nsmooth > 0:
337341
read_offset = raw_shape[1] - nsmooth
342+
if read_offset < 0:
343+
raise ValueError(
344+
f"Insufficient data points for requested smoothing path (--nsmooth {nsmooth}): {raw_shape[1]}"
345+
)
338346
else:
339347
read_offset = 0
340348

@@ -345,7 +353,9 @@ def smooth(
345353

346354
if self.tinterpolate:
347355
log.debug("Temporal interpolation triggered!")
348-
arr_smt = np.full((smt_chunks[0], len(dix)), fill_value=nodata, dtype="double")
356+
arr_smt = np.full(
357+
(smt_chunks[0], len(dix)), fill_value=nodata, dtype="double"
358+
)
349359
vector_daily = dates.getDV(nodata)
350360

351361
# Shift for interpolation
@@ -391,11 +401,12 @@ def smooth(
391401
arr_sgrid = next(sgrid_generator)
392402

393403
for ix in map_index:
394-
395404
w = wts[ix, :].astype("double")
396405
if soptimize:
397406
if srange is None:
398-
lag_correlation = lag1corr(arr_raw[ix, :-1], arr_raw[ix, 1:], nodata)
407+
lag_correlation = lag1corr(
408+
arr_raw[ix, :-1], arr_raw[ix, 1:], nodata
409+
)
399410
if lag_correlation > 0.5:
400411
sr = np.arange(-2, 1.2, 0.2).round(2)
401412
elif lag_correlation <= 0.5:
@@ -427,7 +438,6 @@ def smooth(
427438
arr_raw[ix, :] = ws2dp(y=arr_raw[ix, :], lmda=s, w=w, p=p)
428439

429440
if self.tinterpolate:
430-
431441
arr_smt[ix, :] = self._apply_tinterpolate(
432442
z1=arr_raw[ix, :],
433443
nodata=nodata,
@@ -451,7 +461,6 @@ def smooth(
451461
raise HDF5WriteError(msg % self.filename)
452462

453463
if arr_sgrid is not None:
454-
455464
arr_sgrid[arr_sgrid > 0] = np.log10(arr_sgrid[arr_sgrid > 0])
456465

457466
write_check = self.write_chunk(
@@ -484,7 +493,9 @@ def smooth(
484493

485494
if p is not None:
486495
processing_info.update({"pvalue": p})
487-
processing_info["lastrun"] = processing_info["lastrun"] + f" and with P-value of {p}"
496+
processing_info["lastrun"] = (
497+
processing_info["lastrun"] + f" and with P-value of {p}"
498+
)
488499

489500
log.debug("Last run: %s", processing_info["lastrun"])
490501

@@ -539,6 +550,8 @@ def last_smoothed(self):
539550
def _apply_tinterpolate(z1, nodata, vector_daily, dix):
540551
z2 = vector_daily.copy()
541552
z2[z2 != nodata] = z1
542-
z2[...] = ws2d(y=z2, lmda=0.0001, w=np.array((z2 != nodata) * 1, dtype="double"))
553+
z2[...] = ws2d(
554+
y=z2, lmda=0.0001, w=np.array((z2 != nodata) * 1, dtype="double")
555+
)
543556

544557
return z2[dix]

0 commit comments

Comments
 (0)