Skip to content

Commit

Permalink
Add id method, and make sure extension is used for granule id.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Dec 21, 2024
1 parent e27ba93 commit 5ff0686
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/GEDI/GEDI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ or in case of v"2": `GEDI02_A_2019242104318_O04046_01_T02343_02_003_02_V002.h5`.
See section 2.4 in the user guide.
"""
function info(g::GEDI_Granule)
gedi_info(g.id)
gedi_info(id(g))
end

function gedi_info(filename)
Expand Down Expand Up @@ -80,7 +80,7 @@ function track_angle(g::GEDI_Granule, latitude = 0.0, nparts = 100)
v, i = findmin(abs.(latitudes .- min(abs(latitude), gedi_inclination)))
a = angles[i]

info = gedi_info(g.id)
info = gedi_info(id(g))
if info.sub_orbit <= 2 # ascending
return a
else
Expand Down
2 changes: 1 addition & 1 deletion src/GEDI/L2A.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function lines(
map(Tuple(zip(ftracks, grounds))) do (track, ground)
track_df = points(granule, file, track, step, bbox, ground, canopy, filtered)
line = Line(track_df.longitude, track_df.latitude, Float64.(track_df.height))
(; geom = line, track = track, strong_beam = track_df.strong_beam[1], granule = granule.id)
(; geom = line, track = track, strong_beam = track_df.strong_beam[1], granule = id(granule))
end
end
PartitionedTable(nts, granule)
Expand Down
2 changes: 1 addition & 1 deletion src/ICESat-2/ATL03.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function lines(
track = track,
strong_beam = track_df.strong_beam[i],
t = track_df.datetime[i],
granule = granule.id,
granule = id(granule),
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/ICESat-2/ATL08.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function lines(granule::ICESat2_Granule{:ATL08}; tracks = icesat2_tracks, step =
height[height.==fill_value] .= NaN
line = Line(longitude, latitude, height)
# i = div(length(t), 2) + 1
(geom = line, track = track, strong_beam = atlas_beam_type == "strong", granule = granule.id)
(geom = line, track = track, strong_beam = atlas_beam_type == "strong", granule = id(granule))
end
end
return PartitionedTable(nts, granule)
Expand Down
10 changes: 5 additions & 5 deletions src/ICESat-2/ICESat-2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function track_angle(g::ICESat2_Granule, latitude::Real = 0.0, nparts = 100)
v, i = findmin(f -> abs(f - min(abs(latitude), icesat2_inclination)), latitudes)
a = angles[i]

info = icesat2_info(g.id)
info = icesat2_info(id(g))
if info.ascending
return a
else
Expand All @@ -97,7 +97,7 @@ function track_angle(g::ICESat2_Granule, latitude::Vector{Real}, nparts = 100)
a[I] = angles[i]
end

info = icesat2_info(g.id)
info = icesat2_info(id(g))
if info.ascending
return a
else
Expand Down Expand Up @@ -129,7 +129,7 @@ Converts the granule `g` to the product `product`, by guessing the correct name.
"""
function Base.convert(product::Symbol, g::ICESat2_Granule{T}) where {T}
g = ICESat2_Granule{product}(
_convert(g.id, T, product),
_convert(id(g), T, product),
_convert(g.url, T, product),
g.info,
g.polygons,
Expand Down Expand Up @@ -157,7 +157,7 @@ Derive info based on the filename. The name is built up as follows:
`ATL03_[yyyymmdd][hhmmss]_[ttttccss]_[vvv_rr].h5`. See section 1.2.5 in the user guide.
"""
function info(g::ICESat2_Granule)
icesat2_info(g.id)
icesat2_info(id(g))
end

# Granule regions 1-14. Region 4 (North Pole) and region 11 (South Pole) are both ascending descending
Expand All @@ -182,5 +182,5 @@ function icesat2_info(filename)
end

function is_blacklisted(g::Granule)
g.id in blacklist
id(g) in blacklist
end
2 changes: 1 addition & 1 deletion src/ICESat/ICESat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Derive info based on the filename. The name is built up as follows:
ATL03_[yyyymmdd][hhmmss]_[ttttccss]_[vvv_rr].h5. See section 1.2.5 in the user guide.
"""
function info(g::ICESat_Granule)
return icesat_info(g.id)
return icesat_info(id(g))
end

function icesat_info(filename)
Expand Down
15 changes: 8 additions & 7 deletions src/granule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ function _s3_download(url, fn, config = create_aws_config())
end

abstract type Granule end
Base.:(==)(a::Granule, b::Granule) = a.id == b.id
Base.:(==)(a::Granule, b::Granule) = id(a) == id(b)
id(g::Granule) = g.id

Base.show(io::IO, g::Granule) = _show(io, g)
Base.show(io::IO, ::MIME"text/plain", g::Granule) = _show(io, g)
function _show(io, g::T) where {T<:Granule}
print(io, "$T with id $(g.id)")
print(io, "$T with id $(id(g))")
end

MultiPolygonType = Vector{Vector{Vector{Vector{Float64}}}}
Expand All @@ -74,7 +75,7 @@ if it doesn't already exists locally.
Will require credentials (netrc) which can be set with [`netrc!`](@ref).
"""
function download!(granule::Granule, folder = ".")
fn = joinpath(abspath(folder), granule.id)
fn = joinpath(abspath(folder), id(granule))
if isfile(fn)
granule.url = fn
return granule
Expand Down Expand Up @@ -148,7 +149,7 @@ function download!(granules::Vector{<:Granule}, folder::AbstractString = ".")
end

for granule in granules
granule.url = joinpath(folder, granule.id)
granule.url = joinpath(folder, id(granule))
end
granules
end
Expand All @@ -172,8 +173,8 @@ function Base.filesize(granule::T) where {T<:Granule}
filesize(granule.url)
end

Base.isequal(a::Granule, b::Granule) = a.id == b.id
Base.hash(g::Granule, h::UInt) = hash(g.id, h)
Base.isequal(a::Granule, b::Granule) = id(a) == id(b)
Base.hash(g::Granule, h::UInt) = hash(id(g), h)

"""
sync(folder::AbstractString, all::Bool=false; kwargs...)
Expand Down Expand Up @@ -219,7 +220,7 @@ function _sync!(granules, folder, all; kwargs...)
ngranules = if length(granules) == 0 || !haskey(info(granules[end]), :date) || all
Set(search(mission(g), sproduct(g); kwargs...))
else
sort!(granules, by = x -> x.id)
sort!(granules, by = x -> id(x))
Set(search(mission(g), sproduct(g); after = info(granules[end]).date, kwargs...))
end
setdiff!(ngranules, Set(granules))
Expand Down
13 changes: 11 additions & 2 deletions src/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ prefix(::Mission{:ICESat2}) = "ATL"
prefix(::Mission{:GEDI}) = "GEDI"
mission(::Mission{T}) where {T} = T

_fix_gedi_id(id::Nothing) = nothing
_fix_gedi_id(id::String) = first(splitext(id))
_fix_gedi_id(id::Vector{String}) = _fix_gedi_id.(id)

const earthdata_url = "https://cmr.earthdata.nasa.gov/search/granules.umm_json_v1_6_4"

"""
Expand All @@ -37,6 +41,8 @@ function search(
extent = bbox
end

id = _fix_gedi_id(id)

granules =
earthdata_search(
short_name = string(product),
Expand Down Expand Up @@ -165,13 +171,13 @@ function search(mission::Symbol, product::Symbol, args...; kwargs...)
end

function search(g::Granule; kwargs...)
initial = (; version = info(g).version, id = g.id)
initial = (; version = info(g).version, id = id(g))
only(search(mission(g), sproduct(g); merge(initial, kwargs)...))
end

function search(gg::Vector{<:Granule}; kwargs...)
g = first(gg)
initial = (; version = info(g).version, id = map(x -> x.id, gg))
initial = (; version = info(g).version, id = map(x -> id(x), gg))
search(mission(g), sproduct(g); merge(initial, kwargs)...)
end

Expand Down Expand Up @@ -217,6 +223,8 @@ end

function granule_info(item)::NamedTuple
filename = item.producer_granule_id
endswith(lowercase(filename), ".h5") || (filename *= ".h5")

urls = filter(x -> endswith(lowercase(get(x, "href", "")), ".h5"), item.links)

https = filter(u -> startswith(u.href, "http"), urls)
Expand Down Expand Up @@ -246,6 +254,7 @@ function granule_info_umm(item)::NamedTuple
s3_url = length(s3) > 0 ? s3[1].URL : nothing

filename = item.meta["native-id"]
endswith(lowercase(filename), ".h5") || (filename *= ".h5")

(; filename, https_url, s3_url, polygons = [])
end
Expand Down
6 changes: 3 additions & 3 deletions src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ end

function add_id(table::PartitionedTable)
nts = map(table.tables) do t
nt = (; id = Fill(table.granule.id, length(first(t))))
nt = (; id = Fill(id(table.granule), length(first(t))))
merge(t, nt)
end
return PartitionedTable(nts, table.granule)
Expand All @@ -67,7 +67,7 @@ end
function add_id(table::Table)
g = _granule(table)
t = _table(table)
nt = (; id = Fill(g.id, length(first(t))))
nt = (; id = Fill(id(g), length(first(t))))
nts = merge(t, nt)
return Table(nts, g)
end
Expand All @@ -81,7 +81,7 @@ function add_info(table::Table)
return Table(nts, g)
end

_info(g::Granule) = merge((; id = g.id), info(g))
_info(g::Granule) = merge((; id = id(g)), info(g))

DataAPI.metadatasupport(::Type{<:AbstractTable}) = (read = true, write = false)
DataAPI.metadatakeys(t::AbstractTable) = map(String, keys(pairs(_info(_granule(t)))))
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ and return a new list of granules with the local filepaths if they exist.
function instantiate(granules::Vector{T}, folder::AbstractString) where {T<:Granule}
local_granules = Vector{eltype(granules)}()
for granule in granules
file = joinpath(folder, granule.id)
file = joinpath(folder, id(granule))
if isfile(file)
g = copy(granule)
g.url = file
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ empty_extent = convert(Extent, empty_bbox)
@test length(granules) > 0
@test length(granules[1].polygons) > 0

id = "GEDI02_A_2023003040347_O22988_03_T06105_02_003_02_V002"
id = "GEDI02_A_2023003040347_O22988_03_T06105_02_003_02_V002.h5"
@test length(search(:GEDI, :GEDI02_A; version = 2, id = id)) == 1

@test_throws ArgumentError find(:ICESat2, "GLAH14")
Expand Down

0 comments on commit 5ff0686

Please sign in to comment.