Skip to content

Commit 9ed8fc3

Browse files
authored
Merge pull request #9 from EarthyScience/la/examples
load hdf
2 parents e1b4a74 + 99c89db commit 9ed8fc3

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ docs/build/
77
node_modules
88
.CondaPkg
99
.vscode
10-
*.png
10+
*.png
11+
.qodo
12+
*.hdf

docs/Project.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Don't forget to run
2-
#
3-
# pkg> dev ..
4-
#
51
[deps]
62
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
7-
LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589"
3+
DocumenterVitepress = "4710194d-e776-4893-9690-8d956a29c365"
4+
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
5+
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
6+
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
7+
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
88
UnpackSinTiles = "017367d4-aa44-5400-a2e3-1f6255c9d3f1"
99

10-
[compat]
11-
Documenter = "1"
10+
[sources]
11+
UnpackSinTiles = {path = ".."}

docs/example.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnpackSinTiles
2+
using Rasters
3+
using GLMakie
4+
5+
# https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/61/MYD11C1/2024/001/
6+
# https://e4ftl01.cr.usgs.gov/MOLA/MYD11C1.061/2024.12.31/
7+
8+
# hdf_path = joinpath(@__DIR__, "MYD11C1.A2024001.061.2024005085414.hdf")
9+
hdf_path = joinpath(@__DIR__, "MYD11C1.A2024366.061.2025003174450.hdf")
10+
hdf = open_hdf(hdf_path)
11+
12+
_keys = get_hdf_keys(hdf)
13+
14+
meta = parse_metadata(hdf)
15+
16+
# Load the LST data
17+
lst = load_hdf_variable(hdf, "LST_Day_CMG")
18+
lst_32 = Float32.(lst)
19+
ras = Raster(lst_32, (Y, X))
20+
21+
heatmap(ras)

src/UnpackSinTiles.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ module UnpackSinTiles
88
using Dates
99
using SparseArrays
1010

11-
hdf(f) = @pyconst(pyimport("pyhdf.SD").SD)(f)
12-
export hdf
11+
open_hdf(f) = @pyconst(pyimport("pyhdf.SD").SD)(f)
12+
export open_hdf
1313

14+
include("loadhdf.jl")
1415
include("loadTile.jl")
1516
include("metadata.jl")
1617
include("pixelOperations.jl")

src/loadTile.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424
function openTile(tile, in_date, root_path)
2525
full_path = getTilePath(tile, in_date, root_path)
2626
if !isnothing(full_path)
27-
return hdf(full_path)
27+
return open_hdf(full_path)
2828
else
2929
return nothing
3030
end

src/loadhdf.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export get_hdf_keys
2+
export load_hdf_variable
3+
4+
"""
5+
get_hdf_keys(hdf)
6+
7+
This function retrieves the keys of the datasets in the HDF file.
8+
# Arguments
9+
- hdf: HDF file object
10+
"""
11+
function get_hdf_keys(hdf)
12+
pyKeys = hdf.datasets().keys()
13+
return ["$k" for k in pyKeys]
14+
end
15+
16+
"""
17+
load_hdf_variable(hdf, variable; close_file = true)
18+
19+
This function retrieves the specified variable from the HDF file and converts it to a Julia array.
20+
21+
# Arguments
22+
- hdf: HDF file object
23+
- variable: Name of the variable to load
24+
- close_file: Whether to close the HDF file after loading (default: true)
25+
"""
26+
function load_hdf_variable(hdf, variable; close_file = true)
27+
hdf_data = hdf.select(variable).get()
28+
if close_file
29+
hdf.end() # close hdf tile
30+
end
31+
hdf_data_jl = pyconvert(Array, hdf_data)
32+
return hdf_data_jl
33+
end

0 commit comments

Comments
 (0)