From 93de082a3c00d195efe9bff4d132941d4229b999 Mon Sep 17 00:00:00 2001 From: GeorgeS311578 Date: Thu, 24 Sep 2026 00:54:46 -0500 Subject: [PATCH 1/2] Warn about stale SAC file when channel has no data --- HinetPy/win32.py | 12 ++++++ tests/test_stale_file_warning.py | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/test_stale_file_warning.py diff --git a/HinetPy/win32.py b/HinetPy/win32.py index 31532d29..96ce09a1 100644 --- a/HinetPy/win32.py +++ b/HinetPy/win32.py @@ -376,6 +376,18 @@ def _extract_channel_sac( channel.component, channel.id, ) + filename = f"{channel.name}.{channel.component}.{suffix}" + if outdir != ".": + filename = os.path.join(outdir, filename) + if os.path.exists(filename): + logger.warning( + "No new data for %s.%s (%s), but %s already exists " + "on disk and may be stale.", + channel.name, + channel.component, + channel.id, + filename, + ) return None filename = f"{channel.name}.{channel.component}.{suffix}" diff --git a/tests/test_stale_file_warning.py b/tests/test_stale_file_warning.py new file mode 100644 index 00000000..d05e687b --- /dev/null +++ b/tests/test_stale_file_warning.py @@ -0,0 +1,74 @@ +""" +Regression tests for issue #152: stale/missing results reported silently. + +Uses a fake Popen so no win2sac_32 binary, account, or network is needed. +""" + +import logging +import os + +from HinetPy import win32 +from HinetPy.channel import Channel + + +def _make_channel(): + return Channel( + id="3e81", + name="N.NGUH", + component="U", + latitude=35.0, + longitude=140.0, + unit="m/s", + gain=1000.0, + damping=0.7, + period=1.0, + preamplification=20.0, + lsb_value=0.001, + ) + + +class _FakeStderr: + def __init__(self, text): + self._text = text + + def read(self): + return self._text.encode() + + +class _FakeProc: + """Mimics Popen as used in _extract_channel_sac (context manager).""" + + def __init__(self, text): + self.stderr = _FakeStderr(text) + + def __enter__(self): + return self + + def __exit__(self, *args): + return False + + +def _fake_popen_no_data(*args, **kwargs): + return _FakeProc("Data for channel 3e81 not existed\n") + + +def test_no_data_warns_about_stale_file(tmp_path, monkeypatch, caplog): + channel = _make_channel() + stale = tmp_path / "N.NGUH.U.SAC" + stale.write_text("DATO VIEJO", encoding="utf8") + monkeypatch.setattr(win32, "Popen", _fake_popen_no_data) + with caplog.at_level(logging.WARNING, logger="HinetPy.win32"): + result = win32._extract_channel_sac("dummy.cnt", channel, outdir=str(tmp_path)) + assert result is None + assert any("may be stale" in r.message for r in caplog.records) + + +def test_no_data_without_stale_file_warns_once(tmp_path, monkeypatch, caplog): + channel = _make_channel() + monkeypatch.setattr(win32, "Popen", _fake_popen_no_data) + with caplog.at_level(logging.WARNING, logger="HinetPy.win32"): + result = win32._extract_channel_sac("dummy.cnt", channel, outdir=str(tmp_path)) + assert result is None + assert not any("may be stale" in r.message for r in caplog.records) + assert any("not exists" in r.message for r in caplog.records) + assert not os.path.exists(str(tmp_path / "N.NGUH.U.SAC")) From 848645dcad19a2227ebcce85922de0ac334f199c Mon Sep 17 00:00:00 2001 From: GeorgeS311578 Date: Thu, 24 Sep 2026 16:41:58 -0500 Subject: [PATCH 2/2] Compute output filename once in _extract_channel_sac --- HinetPy/win32.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/HinetPy/win32.py b/HinetPy/win32.py index 96ce09a1..d6ffb8a1 100644 --- a/HinetPy/win32.py +++ b/HinetPy/win32.py @@ -351,6 +351,10 @@ def _extract_channel_sac( The extracted SAC file name. """ + filename = f"{channel.name}.{channel.component}.{suffix}" + if outdir != ".": + filename = os.path.join(outdir, filename) + cmd = [ "win2sac_32", winfile, @@ -376,9 +380,6 @@ def _extract_channel_sac( channel.component, channel.id, ) - filename = f"{channel.name}.{channel.component}.{suffix}" - if outdir != ".": - filename = os.path.join(outdir, filename) if os.path.exists(filename): logger.warning( "No new data for %s.%s (%s), but %s already exists " @@ -390,10 +391,6 @@ def _extract_channel_sac( ) return None - filename = f"{channel.name}.{channel.component}.{suffix}" - if outdir != ".": - filename = os.path.join(outdir, filename) - if os.path.exists(filename): # some channels have no data if suffix == "": # remove extra dot if suffix is empty shutil.move(filename, filename[:-1])