Checking Files#
Use arlmet.File.check() to find damage that does not stop a file from
opening. Opening a file already raises arlmet.ARLFormatError when it is
empty or cut short. A file can also hold a time step written partway, skip a time step,
or have records overwritten with other bytes, and still open. HYSPLIT then stops
partway through a run, or reads bad values. Checking a file before a run, or
after downloading or cropping it, finds these first.
The examples run when the docs are built, on met.arl, a small file of made-up
values that the build writes with arlmet.write_dataset()
(Quick Start describes it), and on a folder hrrr/ of more like it, in
which one file has a record overwritten with null bytes and one was cut short.
In [1]: import arlmet
In [2]: with arlmet.File("hrrr/20240101_06_hrrr") as met:
...: for problem in met.check():
...: print(problem)
...:
2024-01-01 07:00: the SHGT record at level 0 (byte 18966) cannot be read. Invalid ARL record header: cannot parse year from '\x00\x00' (bytes 0-2)
check() returns one message per problem, and an empty list for a file
with none:
In [3]: with arlmet.File("met.arl") as met:
...: print(met.check())
...:
[]
What is checked#
The checks follow how HYSPLIT reads a file (metset.f):
Every time step has as many data records as the first. HYSPLIT steps from one index record to the next by the first time step’s record count, so a shorter time step makes it lose its place, and the file seems to end early.
Each time step in the file is as far after the one before it as the second is after the first. HYSPLIT walks the time steps in file order and stops with
meteorological data time interval varieswhen the spacing changes, as it does where a time step is missing, repeated, or out of order. It also finds a time step by its position in the file (advpnt.f), so where it does not stop it reads the wrong hours.arlmet.Fileitself orders time steps by valid time and skips a byte-for-byte repeat (some NOAA HRRR archive files have one), so such a file reads well in arlmet and still needs fixing before HYSPLIT reads it:arlmet.concat()with the file alone, orarlmet.extract_subset(), writes each time step once and in order.Every data record’s header can be read, and names the variable, level, and hour its index record lists. A record overwritten with null bytes, or copied from another time step, fails this.
No record is flagged missing, by a negative forecast hour or the name
NULLin its header (see ARL File Format). HYSPLIT skips such a record, and stops when a time step has more than 20; arl-met reads it as NaN.
check() reads each record’s 50-byte header. For a 1 GB HRRR block that takes
a few seconds the first time the file is read from disk, and under a second
once it is cached.
What is not checked#
The values.
check()reads only the headers. To compare each record’s packed bytes with the checksum its index record holds, callarlmet.DataRecord.verify_checksum(), which reads the whole record (see below).Hours the file should hold but does not. A file cut after a whole time step looks complete. Compare
arlmet.File.timeswith the hours you expect, for example from the file’s name.
Checking the values#
Each index record holds a checksum of each data record’s packed bytes.
arlmet.DataRecord.verify_checksum() returns False when a record’s
bytes do not match it:
In [4]: with arlmet.File("met.arl") as met:
...: bad = [record for record in met.records if not record.verify_checksum()]
...:
In [5]: bad
Out[5]: []
This reads every byte of the file. A stored checksum of 0 means none was
recorded, and verify_checksum() returns True for it: NOAA’s GDAS and
NAM12 files store 0 for the precipitation and fluxes taken from a 6-hour
forecast, at 00, 06, 12 and 18 UTC, and those values are good.
Checking a folder#
To check a folder of files, catch the files that do not open as well:
In [6]: from pathlib import Path
In [7]: for path in sorted(Path("hrrr").glob("*_hrrr")):
...: try:
...: with arlmet.File(path) as met:
...: problems = met.check()
...: except arlmet.ARLFormatError as exc:
...: problems = [str(exc)]
...: if problems:
...: print(path.name, problems[0])
...:
20240101_06_hrrr 2024-01-01 07:00: the SHGT record at level 0 (byte 18966) cannot be read. Invalid ARL record header: cannot parse year from '\x00\x00' (bytes 0-2)
20240101_12_hrrr hrrr/20240101_12_hrrr: file size 75364 bytes is not a whole number of 3161-byte records (61x51 grid); the file is truncated or corrupt.