Downloading Archived Meteorology#
arl-met includes archive classes for downloading ARL meteorology files from the NOAA ARL public archives.
Install the archive dependencies first:
pip install "arlmet[archives]"
The examples on this page download files from NOAA, so they are shown but not run when the docs are built, except the one that only picks an archive by name.
Choose an archive#
Each class knows the filename and archive layout for one product, and is registered under a short name.
Name |
Class |
Product |
Typical coverage |
|
|---|---|---|---|---|
|
HRRR 3 km |
CONUS, 6-hour files, June 2019–present |
1 hour |
|
|
HRRR 3 km, version 1 |
CONUS, 6-hour files, June 2015–2019 |
1 hour |
|
|
NAM 12 km |
North America, daily files |
3 hours |
|
|
NAM hybrid sigma-pressure |
CONUS, Alaska, or Hawaii ( |
1 hour |
|
|
GDAS 1 degree |
Global, weekly files |
3 hours |
|
|
GDAS 0.5 degree |
Global, daily files, 2007–2019 |
3 hours |
|
|
GFS 0.25 degree |
Global, daily files |
3 hours |
|
|
North American Regional Reanalysis 32 km |
North America, monthly files, 1979–2019 |
3 hours |
|
|
NCEP/NCAR Reanalysis 2.5 degree |
Global, monthly files |
6 hours |
|
|
WRF 27 km, time-averaged ( |
North America, daily files, from 1980 |
1 hour |
Choose an archive by name#
arlmet.archives.ARCHIVES maps each name to its class, and
arlmet.archives.get_archive() builds an archive from its name plus any
options, which is convenient when the product comes from a config file:
In [1]: from arlmet.archives import ARCHIVES, get_archive
In [2]: sorted(ARCHIVES)
Out[2]:
['gdas0p5',
'gdas1',
'gfs0p25',
'hrrr',
'hrrr.v1',
'nam12',
'nams',
'narr',
'reanalysis',
'wrf27km']
In [3]: archive = get_archive("nams", domain="ak")
In [4]: archive
Out[4]: NAMSArchive(domain='ak')
files = archive.fetch("2024-07-18", "2024-07-19", dest_dir="./met/")
An unknown name raises ValueError listing the available ones. A subclass of
arlmet.archives.Archive that sets name is registered
automatically when it is defined, so your own archives work with
get_archive() too.
The time step and the file span#
Two times describe an archive, and they are different things. The file span
is how much time one file covers: 6 hours for HRRR, a day for NAM 12 km, a week
for GDAS 1 degree. The step (step, the last
column above) is the time between one time step and the next inside a file, the
spacing of its index records. A HRRR file spans 6 hours and holds 6 time steps an
hour apart; a NAM 12 km file spans a day and holds 8, three hours apart, from
00:00 to 21:00. So a file’s last time step is one step before the file’s end, and
a run that reaches past 21:00 needs the next day’s NAM file, although 21:30, say,
lies in the first.
In [5]: ARCHIVES["hrrr"].step
Out[5]: Timedelta('0 days 01:00:00')
In [6]: get_archive("nam12").step
Out[6]: Timedelta('0 days 03:00:00')
Each step was read from the index records of real NOAA files of that product
(files from 1979 to 2025). It is the same for every file of an archive, and for
all three domains of "nams".
Download files for a time range#
from arlmet.archives import HRRRArchive
archive = HRRRArchive()
files = archive.fetch(
"2024-07-18 00:00",
"2024-07-19 00:00",
dest_dir="./met",
)
fetch() returns the local paths in chronological order. Duplicate archive
keys are removed automatically when the requested time range spans multiple
hours within the same ARL file.
Archive files are named by UTC. A time without a time zone is UTC, and a
time-zone-aware one is converted to UTC, so
pd.Timestamp("2024-07-31 20:00", tz="America/Denver") selects the files
of August 1, 02 UTC. paths_for_range(), fetch() and sync() all
work this way.
Crop on download#
For large global products, pass bbox= so the downloaded file is cropped
before it is cached locally.
from arlmet.archives import GFSArchive
archive = GFSArchive()
files = archive.fetch(
"2024-07-18 00:00",
"2024-07-19 00:00",
dest_dir="./met",
bbox=(-125.0, 30.0, -100.0, 50.0),
)
This uses arlmet.extract_subset() internally after the raw file is
downloaded. The box must be large enough to hold the cropped file’s index
record: for GFS 0.25 degree with all levels, at least 3,340 grid cells, so a 4
by 3 degree box raises ValueError after the whole file has been downloaded
(Cropping ARL Data has the rule).
Pass levels= to keep only some vertical levels, counted from 0 at the
surface. It works with or without bbox.
files = archive.fetch(
"2024-07-18 00:00",
"2024-07-19 00:00",
dest_dir="./met",
bbox=(-125.0, 30.0, -100.0, 50.0),
levels=range(20), # the lowest 20 levels
)
The crop is part of each cached file’s name (for example
...crop_-125.00_30.00_-100.00_50.00.levels_0-19), so files cropped
differently are cached separately. Bbox values are written with two decimals
unless they have more, which are kept in full (...crop_-111.925_...).
The full, uncropped file is downloaded into dest_dir (not the system temp
directory) and deleted once the crop is written, so dest_dir needs room
for one full file at a time (about 3 GB for HRRR).
Choose a mirror#
Each archive is served from three mirrors. The default, "s3", is usually
the fastest choice.
files = archive.fetch(
"2024-07-18",
"2024-07-19",
dest_dir="./met",
mirror="ftp",
)
The mirrors are:
"s3": NOAA public S3 bucket vias3fs"ftp": NOAA ARL FTP archive"http": READY web archive
Caching and overwrite behavior#
Downloaded files are reused if a matching local file already exists. Pass
overwrite=True to force a fresh download.
Files are written to a hidden .<name>.<random>.partial file first and
renamed into place only once complete, so an interrupted fetch never leaves a
truncated file that a later call would reuse. A .partial file left behind
by a killed process is never used and can be deleted.
Several callers can fetch into one folder at once: threads, processes such as
PYSTILT’s workers, or jobs on other nodes that share the folder. Each file is
downloaded once. The first caller to reach a file makes a hidden folder
.<name>.lock beside it (with a note of its host and process) and the others
wait for the finished file. While they wait they watch the download’s temp files
grow. If nothing changes for about five minutes, because the first caller was
killed or its download hung, one of them removes the lock, downloads the file
itself, and removes what the first caller left. So a folder left by a killed
job makes the next caller wait those five minutes once; you can also delete it
yourself.
The lock saves work only. Every copy has its own temp file, so a caller that was
taken for dead while only paused costs a second download, never a damaged file.
Once a file is there it is not replaced, even by a second copy finishing later:
on NFS, replacing a file gives anyone reading it from another node a Stale file
handle error. overwrite=True does replace it, one caller at a time, so use
it only when nothing is reading the file.
Requesting a time range that begins before an archive’s start_date (the
start of its archive) raises ValueError.
files = archive.fetch(
"2024-07-18",
"2024-07-19",
dest_dir="./met",
overwrite=True,
)
Keep a local mirror in step with NOAA#
fetch() gets the files for a time range. sync()
keeps a whole folder complete and checked, so a daily job can replace a
mirroring tool such as lftp mirror:
from arlmet.archives import HRRRArchive
result = HRRRArchive().sync(dest_dir="/data/hrrr")
result.downloaded # files fetched this run
result.mismatched # local files whose MD5 differs from NOAA's: reported, left alone
NOAA publishes listing.md5.txt, one md5 name line per file, at the top
of every product on the S3 and FTP mirrors, and rewrites it when a new file is
complete and when NOAA corrects one. sync() reads it and, for each file in
the archive’s range:
No local copy: downloads it to a hidden
.partialfile, computes its MD5 as it streams, and renames it into place only if it matches NOAA’s. Otherwise it deletes it and lists the file inresult.failed.A copy the manifest records as NOAA’s: does nothing, and does not read the file.
A copy the manifest records as differing from NOAA’s: lists it in
result.mismatched, without reading it, and leaves it alone. It stays there on every run until NOAA’s file changes.A copy NOAA has corrected (NOAA’s MD5 is not the one recorded when the copy was checked): downloads it again the same way and replaces it where it is, whether the old copy matched or not.
A copy the manifest does not list (a folder filled another way), or whose size or modification time changed since it was recorded: computes its MD5 and records it. If it is NOAA’s it is in
result.adopted; if not, inresult.mismatched, and left alone.
Nothing is deleted. Files NOAA does not list, including the name~date~
backups lftp leaves, are not touched.
The folder is the product’s own (for HRRR, the one that holds 2024/07/...),
and new files go where paths_for_range() puts
them. A local copy is found by its name, anywhere under the folder, so a
folder in another layout (all the HRRR v1 files in one folder, say) is
checked and recorded, never downloaded again. Run dry_run=True first on
a folder you care about: it reads nothing and writes nothing, and
result.downloaded shows what a real run would fetch.
# Which files would be fetched, without reading or writing anything?
result = HRRRArchive().sync(dest_dir="/data/hrrr", dry_run=True)
len(result.downloaded), len(result.adopted)
# Only part of the archive
HRRRArchive().sync("2024-07-01", "2024-07-31", dest_dir="/data/hrrr")
The manifest#
arlmet-manifest.tsv in the folder keeps apart what each local file is and
what NOAA’s copy was when it was checked, as rsync does with its file
list. It is a plain text file with one line per file and five fields separated
by tabs: the local file’s MD5, NOAA’s MD5 when it was checked, the file’s size in
bytes, its modification time in nanoseconds, and its path relative to the folder
(last, so it may contain spaces). For example (tabs shown as spaces):
0f4c...e2a1 0f4c...e2a1 3377102848 1718150400000000000 2024/07/20240718_00-05_hrrr
A run reads a file only if it is not in the manifest or its size or modification time differ from the line, and then updates the line. A file whose MD5 differs from NOAA’s is recorded too, so it is reported on every run without being read; it is replaced only when NOAA’s MD5 changes. To have a file checked again, delete its line. The manifest is written as the run goes, so a run that is killed keeps what it checked (the first run over a big mirror reads every file once, which takes a while).
One run at a time#
A folder .arlmet-sync.lock made with mkdir (which is atomic also on
NFS, where a flock may not be seen by another host) stops a second run
with a RuntimeError naming the host and start time of the first. It is
removed at the end of a run, also after an error. A run that is killed without
a chance to clean up (kill -9, a lost node) leaves it; once you are sure no
sync is running, delete the folder.
A file that cannot be fetched, or whose download does not match NOAA’s MD5, is
logged and listed in result.failed, and the run goes on; the next run tries
it again. Turn on logging (logging.basicConfig(level=logging.INFO)) to see
each file as it is fetched.