Skip to content

Commit fb0c016

Browse files
committed
Replace parse with read and add tests for both
1 parent 7036dcf commit fb0c016

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

src/RapidRefreshData.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function Base.show(io::IO, dset::RAPDataset)
3939
print(io, "RAPDataset", (; date=string(date), cycle_time, grid, product, forecast))
4040
end
4141

42-
function Base.parse(::Type{RAPDataset}, local_path::String)
42+
function Base.read(::Type{RAPDataset}, local_path::String)
4343
filename = split(basename(local_path), ".")[1]
4444
parts = split(filename, "_")
4545
# Skip "rap" prefix (parts[1]) and parse remaining parts
@@ -84,7 +84,7 @@ List all cached RAP datasets.
8484
"""
8585
function local_datasets(::Type{RAPDataset})
8686
files = filter(f -> startswith(f, "rap_") && endswith(f, ".grib2"), readdir(dir))
87-
parse.(RAPDataset, files)
87+
read.(RAPDataset, files)
8888
end
8989

9090
"""
@@ -122,7 +122,7 @@ function Base.show(io::IO, dset::GFSDataset)
122122
print(io, "GFSDataset", (; date=string(date), cycle, resolution, product, forecast))
123123
end
124124

125-
function Base.parse(::Type{GFSDataset}, local_path::String)
125+
function Base.read(::Type{GFSDataset}, local_path::String)
126126
filename = split(basename(local_path), ".")[1]
127127
parts = split(filename, "_")
128128
# Skip "gfs" prefix (parts[1]) and parse remaining parts
@@ -168,7 +168,7 @@ List all cached GFS datasets.
168168
"""
169169
function local_datasets(::Type{GFSDataset})
170170
files = filter(f -> startswith(f, "gfs_"), readdir(dir))
171-
parse.(GFSDataset, files)
171+
read.(GFSDataset, files)
172172
end
173173

174174
"""

test/runtests.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,41 @@ using Dates
3535
@test dset_partial.forecast == "f03"
3636
end
3737

38+
@testset "Base.read(::Type{RAPDataset}, path)" begin
39+
# Test parsing a standard RAP filename
40+
path1 = "rap_20240115_t00z_awp130_f00.grib2"
41+
dset1 = read(RapidRefreshData.RAPDataset, path1)
42+
@test dset1.date == Date(2024, 1, 15)
43+
@test dset1.cycle_time == "t00z"
44+
@test dset1.grid == "awp130"
45+
@test dset1.forecast == "f00"
46+
47+
# Test parsing with different parameters
48+
path2 = "rap_20241225_t12z_awp252_f06.grib2"
49+
dset2 = read(RapidRefreshData.RAPDataset, path2)
50+
@test dset2.date == Date(2024, 12, 25)
51+
@test dset2.cycle_time == "t12z"
52+
@test dset2.grid == "awp252"
53+
@test dset2.forecast == "f06"
54+
55+
# Test parsing with full path
56+
full_path = "/some/directory/rap_20240305_t18z_awp130_f03.grib2"
57+
dset3 = read(RapidRefreshData.RAPDataset, full_path)
58+
@test dset3.date == Date(2024, 3, 5)
59+
@test dset3.cycle_time == "t18z"
60+
@test dset3.grid == "awp130"
61+
@test dset3.forecast == "f03"
62+
63+
# Test that read works with broadcast
64+
paths = ["rap_20240101_t00z_awp130_f00.grib2", "rap_20240102_t06z_awp252_f12.grib2"]
65+
dsets = read.(RapidRefreshData.RAPDataset, paths)
66+
@test length(dsets) == 2
67+
@test dsets[1].date == Date(2024, 1, 1)
68+
@test dsets[2].date == Date(2024, 1, 2)
69+
@test dsets[1].cycle_time == "t00z"
70+
@test dsets[2].cycle_time == "t06z"
71+
end
72+
3873
@testset "url() function" begin
3974
# Test URL generation with default dataset
4075
test_date = Date(2024, 1, 15)
@@ -234,6 +269,46 @@ using Dates
234269
@test dset_partial.forecast == "f012"
235270
end
236271

272+
@testset "Base.read(::Type{GFSDataset}, path)" begin
273+
# Test parsing a standard GFS filename
274+
path1 = "gfs_20240115_00_0p25_atmos_f000.grib2"
275+
dset1 = read(RapidRefreshData.GFSDataset, path1)
276+
@test dset1.date == Date(2024, 1, 15)
277+
@test dset1.cycle == "00"
278+
@test dset1.resolution == "0p25"
279+
@test dset1.product == "atmos"
280+
@test dset1.forecast == "f000"
281+
282+
# Test parsing with different parameters
283+
path2 = "gfs_20241225_12_0p50_wave_f024.grib2"
284+
dset2 = read(RapidRefreshData.GFSDataset, path2)
285+
@test dset2.date == Date(2024, 12, 25)
286+
@test dset2.cycle == "12"
287+
@test dset2.resolution == "0p50"
288+
@test dset2.product == "wave"
289+
@test dset2.forecast == "f024"
290+
291+
# Test parsing with full path
292+
full_path = "/some/directory/gfs_20240305_06_1p00_atmos_f012.grib2"
293+
dset3 = read(RapidRefreshData.GFSDataset, full_path)
294+
@test dset3.date == Date(2024, 3, 5)
295+
@test dset3.cycle == "06"
296+
@test dset3.resolution == "1p00"
297+
@test dset3.product == "atmos"
298+
@test dset3.forecast == "f012"
299+
300+
# Test that read works with broadcast
301+
paths = ["gfs_20240101_00_0p25_atmos_f000.grib2", "gfs_20240102_18_0p50_wave_f384.grib2"]
302+
dsets = read.(RapidRefreshData.GFSDataset, paths)
303+
@test length(dsets) == 2
304+
@test dsets[1].date == Date(2024, 1, 1)
305+
@test dsets[2].date == Date(2024, 1, 2)
306+
@test dsets[1].cycle == "00"
307+
@test dsets[2].cycle == "18"
308+
@test dsets[1].product == "atmos"
309+
@test dsets[2].product == "wave"
310+
end
311+
237312
@testset "GFSDataset url() function" begin
238313
# Test URL generation with default dataset
239314
test_date = Date(2024, 1, 15)

0 commit comments

Comments
 (0)