Skip to content

Commit 982becf

Browse files
committed
fix(batch): lookup frequency_nominal from denoised zarrs for MVBS/NASC channel labelling
1 parent 591e8f4 commit 982becf

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

‎scripts/batch_processing/run_combine_daily.py‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,43 @@ def _channel_freq_label(ds: xr.Dataset, ch_idx: int) -> str:
165165
if "frequency_nominal" in ds.coords or "frequency_nominal" in ds.data_vars:
166166
return _resolve_freq_label(float(ds["frequency_nominal"].values[ch_idx]))
167167
ch_name = str(ds.channel.values[ch_idx])
168-
if "ES200" in ch_name or "200" in ch_name:
169-
return "200kHz"
170-
if "ES38" in ch_name or "38" in ch_name:
171-
return "38kHz"
168+
return _freq_from_channel_name(ch_name)
169+
170+
171+
# Cache: channel_name → frequency_label, populated from denoised/raw zarrs
172+
_CHANNEL_FREQ_CACHE: dict[str, str] = {}
173+
174+
175+
def _freq_from_channel_name(ch_name: str) -> str:
176+
"""Resolve frequency from channel name, using cache populated from denoised zarrs."""
177+
if ch_name in _CHANNEL_FREQ_CACHE:
178+
return _CHANNEL_FREQ_CACHE[ch_name]
179+
# Can't determine — return channel name as-is
172180
return ch_name
173181

174182

183+
def _populate_freq_cache(day: str) -> None:
184+
"""Read frequency_nominal from denoised (or raw Sv) zarrs to build
185+
channel_name → freq_label mapping."""
186+
day_dir = BASE_DIR / day
187+
for mode in ["short_pulse", "long_pulse"]:
188+
for suffix in ["--denoised", ""]:
189+
path = day_dir / f"{day}--{mode}{suffix}.zarr"
190+
if not path.is_dir():
191+
continue
192+
try:
193+
ds = xr.open_zarr(str(path), consolidated=False)
194+
if "frequency_nominal" in ds.coords or "frequency_nominal" in ds.data_vars:
195+
chans = ds.channel.values
196+
freqs = ds["frequency_nominal"].values
197+
for ch, f in zip(chans, freqs):
198+
_CHANNEL_FREQ_CACHE[str(ch)] = _resolve_freq_label(float(f))
199+
ds.close()
200+
break # Got what we need for this mode
201+
except Exception:
202+
continue
203+
204+
175205
def combine_mvbs_or_nasc(
176206
day: str,
177207
product: str,
@@ -712,6 +742,10 @@ def process_one_day(args: tuple) -> tuple[str, int, int]:
712742
log.info("Processing %s ...", day)
713743
t0 = time.time()
714744

745+
# Populate frequency cache from denoised/raw zarrs
746+
_CHANNEL_FREQ_CACHE.clear()
747+
_populate_freq_cache(day)
748+
715749
combined_zarrs = combine_one_day(day, products, skip_existing=skip_existing)
716750

717751
n_echograms = 0

0 commit comments

Comments
 (0)