Skip to content

Commit d50425e

Browse files
committed
Add wind example and expand test coverage
1 parent fb0c016 commit d50425e

3 files changed

Lines changed: 351 additions & 15 deletions

File tree

examples/wind.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import RapidRefreshData as RRD
2+
3+
using Rasters, ArchGDAL, Dates
4+
5+
datasets = [RRD.RAPDataset(date = Date(2024, 6, 15), cycle_time = "t0$(i)z") for i in 0:5]

src/RapidRefreshData.jl

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ module RapidRefreshData
33
using Dates, Scratch
44

55
const rap_base_url = "https://noaa-rap-pds.s3.amazonaws.com/rap."
6-
const gfs_base_url = "https://noaa-gfs-bdp-pds.s3.amazonaws.com/"
6+
const gfs_base_url = "https://noaa-gfs-bdp-pds.s3.amazonaws.com/gfs"
7+
const hrrr_base_url = "https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr"
78

89

910
#-----------------------------------------------------------------------------# __init__
10-
dir::String = ""
11+
rap_dir::String = ""
12+
gfs_dir::String = ""
13+
hrrr_dir::String = ""
1114

1215
function __init__()
13-
global dir = Scratch.@get_scratch!("datasets")
16+
global rap_dir = Scratch.@get_scratch!("rap")
17+
global gfs_dir = Scratch.@get_scratch!("gfs")
18+
global hrrr_dir = Scratch.@get_scratch!("hrrr")
1419
end
1520

1621
#-----------------------------------------------------------------------------# RAPDataset
@@ -65,7 +70,7 @@ end
6570
Get local cache path for the RAP dataset.
6671
"""
6772
function local_path(dset::RAPDataset)
68-
joinpath(dir, "rap_$(Dates.format(dset.date, "yyyymmdd"))_$(dset.cycle_time)_$(dset.grid)_$(dset.forecast).grib2")
73+
joinpath(rap_dir, "rap_$(Dates.format(dset.date, "yyyymmdd"))_$(dset.cycle_time)_$(dset.grid)_$(dset.forecast).grib2")
6974
end
7075

7176
"""
@@ -83,8 +88,7 @@ end
8388
List all cached RAP datasets.
8489
"""
8590
function local_datasets(::Type{RAPDataset})
86-
files = filter(f -> startswith(f, "rap_") && endswith(f, ".grib2"), readdir(dir))
87-
read.(RAPDataset, files)
91+
read.(RAPDataset, readdir(rap_dir))
8892
end
8993

9094
"""
@@ -140,7 +144,7 @@ end
140144
Generate AWS S3 URL for the GFS dataset.
141145
"""
142146
function url(dset::GFSDataset)
143-
"$(gfs_base_url)gfs.$(Dates.format(dset.date, "yyyymmdd"))/$(dset.cycle)/$(dset.product)/gfs.t$(dset.cycle)z.pgrb2.$(dset.resolution).$(dset.forecast)"
147+
"$(gfs_base_url).$(Dates.format(dset.date, "yyyymmdd"))/$(dset.cycle)/$(dset.product)/gfs.t$(dset.cycle)z.pgrb2.$(dset.resolution).$(dset.forecast)"
144148
end
145149

146150
"""
@@ -149,7 +153,7 @@ end
149153
Get local cache path for the GFS dataset.
150154
"""
151155
function local_path(dset::GFSDataset)
152-
joinpath(dir, "gfs_$(Dates.format(dset.date, "yyyymmdd"))_$(dset.cycle)_$(dset.resolution)_$(dset.product)_$(dset.forecast).grib2")
156+
joinpath(gfs_dir, "gfs_$(Dates.format(dset.date, "yyyymmdd"))_$(dset.cycle)_$(dset.resolution)_$(dset.product)_$(dset.forecast).grib2")
153157
end
154158

155159
"""
@@ -167,8 +171,7 @@ end
167171
List all cached GFS datasets.
168172
"""
169173
function local_datasets(::Type{GFSDataset})
170-
files = filter(f -> startswith(f, "gfs_"), readdir(dir))
171-
read.(GFSDataset, files)
174+
read.(GFSDataset, readdir(gfs_dir))
172175
end
173176

174177
"""
@@ -180,5 +183,88 @@ function clear_local_dataset!(dset::GFSDataset)
180183
isfile(local_path(dset)) && rm(local_path(dset))
181184
end
182185

186+
#-----------------------------------------------------------------------------# HRRRDataset
187+
"""
188+
HRRRDataset(; date, cycle, region, product, forecast)
189+
190+
NOAA High-Resolution Rapid Refresh (HRRR) dataset descriptor.
191+
192+
# Fields
193+
- `date::Date`: Forecast initialization date (default: today)
194+
- `cycle::String`: Model run hour - "00" to "23" (default: "00")
195+
- `region::String`: Geographic region - "conus" (Continental US) or "alaska" (default: "conus")
196+
- `product::String`: Product type - "wrfsfc" (surface), "wrfprs" (pressure), "wrfnat" (native), or "wrfsub" (subhourly) (default: "wrfsfc")
197+
- `forecast::String`: Forecast hour - "f00" to "f48" (default: "f00")
198+
"""
199+
@kwdef struct HRRRDataset
200+
date::Date = today()
201+
cycle::String = "00" # "00" to "23"
202+
region::String = "conus" # "conus", "alaska"
203+
product::String = "wrfsfc" # "wrfsfc", "wrfprs", "wrfnat", "wrfsub"
204+
forecast::String = "f00" # "f00" to "f48"
205+
end
206+
207+
function Base.show(io::IO, dset::HRRRDataset)
208+
(;date, cycle, region, product, forecast) = dset
209+
print(io, "HRRRDataset", (; date=string(date), cycle, region, product, forecast))
210+
end
211+
212+
function Base.read(::Type{HRRRDataset}, local_path::String)
213+
filename = split(basename(local_path), ".")[1]
214+
parts = split(filename, "_")
215+
# Skip "hrrr" prefix (parts[1]) and parse remaining parts
216+
date = Date(parts[2], "yyyymmdd")
217+
cycle = parts[3]
218+
region = parts[4]
219+
product = parts[5]
220+
forecast = parts[6]
221+
return HRRRDataset(; date, cycle, region, product, forecast)
222+
end
223+
224+
"""
225+
url(dset::HRRRDataset) -> String
226+
227+
Generate AWS S3 URL for the HRRR dataset.
228+
"""
229+
function url(dset::HRRRDataset)
230+
"$(hrrr_base_url).$(Dates.format(dset.date, "yyyymmdd"))/$(dset.region)/hrrr.t$(dset.cycle)z.$(dset.product)$(dset.forecast).grib2"
231+
end
232+
233+
"""
234+
local_path(dset::HRRRDataset) -> String
235+
236+
Get local cache path for the HRRR dataset.
237+
"""
238+
function local_path(dset::HRRRDataset)
239+
joinpath(hrrr_dir, "hrrr_$(Dates.format(dset.date, "yyyymmdd"))_$(dset.cycle)_$(dset.region)_$(dset.product)_$(dset.forecast).grib2")
240+
end
241+
242+
"""
243+
get(dset::HRRRDataset) -> String
244+
245+
Download HRRR dataset to cache (if needed) and return filepath.
246+
"""
247+
function Base.get(dset::HRRRDataset)
248+
isfile(local_path(dset)) ? local_path(dset) : Base.download(url(dset), local_path(dset))
249+
end
250+
251+
"""
252+
local_datasets(HRRRDataset) -> Vector{HRRRDataset}
253+
254+
List all cached HRRR datasets.
255+
"""
256+
function local_datasets(::Type{HRRRDataset})
257+
read.(HRRRDataset, readdir(hrrr_dir))
258+
end
259+
260+
"""
261+
clear_local_dataset!(dset::HRRRDataset)
262+
263+
Remove HRRR dataset from local cache.
264+
"""
265+
function clear_local_dataset!(dset::HRRRDataset)
266+
isfile(local_path(dset)) && rm(local_path(dset))
267+
end
268+
183269

184270
end

0 commit comments

Comments
 (0)