"""
Load a folder of Bruker NanoScope ``.spm`` files, one per frame, as an AFMImageStack.
Files are read with AFMReader, which returns heights in nm. Frames are taken in
file-name order.
Timing: if every file's header has ``Relative frame time``, those times give
``timestamp`` (s from the first frame) and ``frame_duration_s`` (the time to
the next frame), which include any pause between frames. If any file lacks it,
the whole stack uses an even interval of lines / ``Scan Rate``.
``scan_direction`` is stored per frame, because NanoScope time series can
alternate between up and down frames. ``start_epoch_ms`` comes from each
file's ``Date``.
"""
import logging
from datetime import datetime, timezone
from pathlib import Path
import numpy as np
from AFMReader import spm
from playnano.afm_stack import AFMImageStack
from playnano.io.formats._afmreader import unpack_afmreader_result
from playnano.utils.io_utils import build_frame_metadata
logger = logging.getLogger(__name__)
def _first_float(header, key):
"""Return leading numeric part of ``header[key]`` (strips unit suffix), or None."""
# NanoScope values often carry units, e.g. 'Scan Rate: 3.38041' but also
# 'Slow Axis Size: 1000 nm' or 'Scan Size: 503.906 503.906 nm'. Taking the
# first whitespace-delimited token gets the number without needing per-key
# unit knowledge.
v = header.get(key)
if v is None:
return None
try:
return float(str(v).split()[0])
except ValueError:
return None
def _parse_spm_date(s):
"""
Parse a NanoScope ``Date`` string to a naive local datetime, or None.
NanoScope writes wall-clock time on the acquisition PC, e.g.
``'04:38:14 PM Fri Sep 11 2026'``: 12-hour clock with AM/PM, then a day
abbreviation, month, day, year. No timezone, no sub-second component.
"""
if s is None:
return None
try:
return datetime.strptime(str(s).strip(), "%I:%M:%S %p %a %b %d %Y")
except ValueError:
return None
def _read_spm_header_fields(fpath):
"""
Read the timing/direction fields this reader cares about from one .spm file.
Returns
-------
tuple[float | None, datetime | None, str | None]
``(relative_frame_time_s, start_datetime, scan_direction)``. Any element may be
``None`` if the corresponding field is absent or unparseable — the
caller decides how to fall back.
Notes
-----
``Frame direction`` is the *actual* direction of this frame (per-file), not
the *setting* (``Capture direction``, which is what the operator asked for).
Use ``Frame direction`` for motion: in default "capture both" mode it's
what alternates across the sequence.
"""
h = parse_spm_header(fpath)
rel_t = _first_float(h, "Relative frame time")
start = _parse_spm_date(h.get("Date"))
fd = h.get("Frame direction")
# Map NanoScope's 'Up'/'Down' to playNano's convention across readers.
motion = (
None
if fd is None
else {"Down": "topDown", "Up": "bottomUp"}.get(fd.strip(), fd.strip())
)
return rel_t, start, motion
[docs]
def load_spm_folder(folder_path: Path | str, channel: str) -> AFMImageStack:
"""
Load a folder of NanoScope ``.spm`` files as one stack.
Parameters
----------
folder_path : str or pathlib.Path
Folder of frame files: ``.spm`` or numbered ``.001``, ``.002`` ...
files. Files are sorted by name.
channel : str
Channel to load, for example ``"Height"``, ``"ZSensor"`` or
``"Height Sensor"``.
Returns
-------
AFMImageStack
The stack. ``pixel_size_nm`` is the first frame's; each
``frame_metadata`` entry holds ``timestamp`` (s from the first frame),
``frame_pixel_size_nm``, ``line_rate``, ``frame_duration_s``,
``start_epoch_ms`` and ``scan_direction``. The last frame's
``frame_duration_s`` is None when real frame times are used, because
there is no next frame to measure to.
Raises
------
ValueError
If ``folder_path`` is not a folder, the first file has no
``Scan Rate``, or the frames differ in shape.
FileNotFoundError
If the folder has no ``.spm`` or numbered files.
Notes
-----
``Date`` is local time with one-second precision, but it is stored as if
it were UTC, so ``start_epoch_ms`` can be off by the time zone offset. Use
it for records, not to align recordings to better than a second.
"""
folder = Path(folder_path)
if not folder.is_dir():
raise ValueError(f"{folder} is not a directory.")
# NanoScope writes .spm or numeric extensions like .001/.002/…
spm_files = sorted(
f
for f in folder.iterdir()
if f.is_file()
and (
f.suffix.lower() == ".spm"
or (f.suffix[1:].isdigit() and len(f.suffix) == 4)
)
)
if not spm_files:
raise FileNotFoundError(f"No .spm files found in {folder}.")
logger.info(f"Found {len(spm_files)} .spm files.")
# --- Shape, dtype, and the fallback line rate from the first file ---
first_img, first_pixel_size_nm, _ = unpack_afmreader_result(
spm.load_spm(spm_files[0], channel)
)
height_px, width_px = first_img.shape
dtype = first_img.dtype
num_frames = len(spm_files)
image_stack = np.empty((num_frames, height_px, width_px), dtype=dtype)
first_header = parse_spm_header(spm_files[0])
try:
line_rate = float(first_header.get("Scan Rate"))
except (TypeError, ValueError):
line_rate = None
lines_per_frame = height_px
if line_rate is None or lines_per_frame is None:
raise ValueError(
f"Missing data: line_rate={line_rate}, lines_per_frame={lines_per_frame}"
)
# Synthetic per-frame interval, used only as the fallback below.
frame_interval = lines_per_frame / line_rate
# --- Per-file header fields for timing/direction, read ONCE ---
rel_ts, starts, motions = zip(
*[_read_spm_header_fields(fp) for fp in spm_files], strict=False
)
# Real per-frame timing when every file has 'Relative frame time'.
# Otherwise fall back to the synth interval for the whole stack — mixing
# real and synth values within one stack would silently produce a
# misleading timeline.
if all(t is not None for t in rel_ts):
t0 = rel_ts[0]
timestamps = np.array([t - t0 for t in rel_ts])
if len(rel_ts) > 1:
diffs = np.diff(rel_ts).astype(float)
# No frame N+1 to measure against, so the last frame's duration is unknown.
durations = np.concatenate([diffs, [np.nan]])
else:
durations = np.array([np.nan])
else:
logger.warning(
"Some .spm files missing 'Relative frame time';"
" falling back to synth timing."
)
timestamps = np.arange(num_frames) * frame_interval
durations = np.full(num_frames, frame_interval)
def _epoch_ms(dt):
"""
Convert naive local datetime -> UTC epoch ms.
This is approximate: assumes UTC, second precision.
"""
return (
None
if dt is None
else int(dt.replace(tzinfo=timezone.utc).timestamp() * 1000)
)
# --- Per-frame loop: pixels + metadata ---
frame_metadata = []
for i, fpath in enumerate(spm_files):
logger.debug(f"Loading {fpath.name}")
img, px_size_nm, _ = unpack_afmreader_result(spm.load_spm(fpath, channel))
if img.shape != (height_px, width_px):
raise ValueError(f"Inconsistent image shape in {fpath}")
image_stack[i] = img
# NaN durations -> None so metadata stays clean (consumers tolerate None).
dur = durations[i]
frame_duration_s = None if not np.isfinite(dur) else float(dur)
frame_metadata.append(
build_frame_metadata(
timestamp=float(timestamps[i]),
frame_pixel_size_nm=px_size_nm,
line_rate=line_rate,
frame_duration_s=frame_duration_s,
start_epoch_ms=_epoch_ms(starts[i]),
scan_direction=motions[i],
)
)
logger.debug(f"Loaded {num_frames} frames with shape {image_stack.shape}.")
return AFMImageStack(
data=image_stack,
pixel_size_nm=first_pixel_size_nm,
channel=channel,
file_path=str(folder),
frame_metadata=frame_metadata,
)