Probing a File#
Use arlmet.probe() for a cheap summary of one ARL file: its first valid
time, the time between its first two time steps, how many time steps it
holds, its grid, source, and forecast hour, and whether its size divides into
a whole number of time steps or leaves a remainder (a truncated file). It
reads only the first two index records and os.stat()’s the file, never
a full index record scan or a data record, so it costs about what opening the
file waits on, not what reading every time step does.
The example below runs on met.arl, a small file of made-up values that the
build writes with arlmet.write_dataset() (Quick Start describes
it), and on two copies of it: one cut short, one with its first time step
repeated byte for byte, as some NOAA HRRR archive files do.
In [1]: import arlmet
In [2]: arlmet.probe("met.arl")
Out[2]: Probe(path=PosixPath('met.arl'), first=Timestamp('2024-07-18 00:00:00'), step=Timedelta('0 days 06:00:00'), n_steps=4, grid=Grid(latlon, 61×51), source='DOCS', forecast=0, whole=True)
A probe’s result is a frozen arlmet.Probe: first and step are
the valid time and spacing of the first two time steps, n_steps is how
many the file holds, grid, source and forecast come from the
first index record, and whole says whether the file’s size matches an
exact number of time steps.
Whole or truncated#
n_steps and whole come from one division: the first time step’s
length (its index record plus one data record per variable per level) into
the file’s size. A remainder means the file is cut short partway through a
time step:
In [3]: arlmet.probe("truncated.arl")
Out[3]: Probe(path=PosixPath('truncated.arl'), first=Timestamp('2024-07-18 00:00:00'), step=None, n_steps=1, grid=Grid(latlon, 61×51), source='DOCS', forecast=0, whole=False)
truncated.arl is met.arl with its second time step cut 10 bytes
short: n_steps counts only the one whole time step that fits, whole
is False, and step is None because there is no second whole time
step to compare the first with.
What it does not check#
A probe reads two time steps and assumes every other one is the same length
as the first. Two things it does not catch, that arlmet.File.check()
does by reading every index record in the file:
A later time step with a different set of variables. If the tenth time step holds one fewer variable than the first, every count a probe computes past that point is off, but the probe itself has no way to know: it never reads a tenth index record.
A byte-identical repeated time step, which some NOAA HRRR archive files have and
arlmet.Filequietly skips when it opens one. A probe reads the repeated copy as if it were a genuine second time step:In [4]: arlmet.probe("repeated.arl") Out[4]: Probe(path=PosixPath('repeated.arl'), first=Timestamp('2024-07-18 00:00:00'), step=Timedelta('0 days 00:00:00'), n_steps=5, grid=Grid(latlon, 61×51), source='DOCS', forecast=0, whole=True)
repeated.arlismet.arlwith its first time step copied right after itself. The probe’sstepis zero, since the “second” time step it read is really the first one again; a real archive file’s genuine second time step would be read where the repeated copy’s position happens to put it, which isn_stepscounting one too many and every later time step (beyond the two a probe reads) mislocated too.
For either of these, or to compare each record’s checksum with its packed
bytes, use arlmet.File.check() (Checking Files) or
arlmet.DataRecord.verify_checksum(). Treat a probe’s result as
unverified until one of those has run on the file.
When to use it#
arlmet.inventory() opens every file in a directory and reads every one of
its time steps, to build a table you can query by time window
(Taking an Inventory); that is the right tool for finding the files a run needs.
A probe is for when you already have one file’s path and want to know what
it holds, or whether it looks whole, without paying for a directory’s worth
of reads: for example, a quick look at a single downloaded file, or a
lightweight first pass before deciding whether a file is worth the fuller
read inventory or check give it.