Skip to content

Commit 4c8b8fa

Browse files
authored
Merge pull request #19 from JuliaGeo/fc/fixdataloading
Fix data loading
2 parents 7ee1ba5 + da2394b commit 4c8b8fa

6 files changed

Lines changed: 79 additions & 493 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/Manifest.toml
2+
test/Manifest.toml

Project.toml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
name = "SentinelDataSource"
22
uuid = "afd1bdb2-e5b9-463c-8ecd-ef9b9458f1b0"
3-
authors = ["Felix Cremer <fcremer@bgc-jena.mpg.de> and contributors"]
43
version = "0.1.0-DEV"
4+
authors = ["Felix Cremer <fcremer@bgc-jena.mpg.de> and contributors"]
55

66
[deps]
77
CFTime = "179af706-886a-5703-950a-314cd64e0468"
88
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"
99
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
10-
DiskArrayTools = "fcd2136c-9f69-4db6-97e5-f31981721d63"
1110
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
1211
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
13-
YAXArrayBase = "90b8fcef-0c2d-428d-9c56-5f86629e9d14"
14-
YAXArrays = "c21b50f5-aa40-41ea-b809-c0f5e47bfa5c"
12+
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
1513
Zarr = "0a941bbe-ad1d-11e8-39d9-ab76183a1d99"
1614
ZarrDatasets = "519a4cdf-1362-424a-9ea1-b1d782dbb24b"
1715

1816
[compat]
1917
CFTime = "0.2.1"
20-
CommonDataModel = "0.3.8"
21-
DimensionalData = "0.29.12"
22-
DiskArrayTools = "0.1.12"
18+
CommonDataModel = "0.3.8, 0.4"
19+
DimensionalData = "0.29.12, 0.30"
2320
DiskArrays = "0.4.11"
24-
Rasters = "0.14.4"
25-
YAXArrayBase = "0.7.5"
26-
YAXArrays = "0.6.1"
21+
Rasters = "0.15"
22+
TimerOutputs = "0.5.29"
2723
Zarr = "0.9.4"
2824
ZarrDatasets = "0.1.3"
2925
julia = "1.6.7"

docs/src/spectraldiversity.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using SentinelDataSource
2+
using YAXArrays
3+
using Distances
4+
using DimensionalData
5+
using AWS
6+
7+
struct AnonymousGCS <:AbstractAWSConfig end
8+
struct NoCredentials end
9+
AWS.region(aws::AnonymousGCS) = "" # No region
10+
AWS.credentials(aws::AnonymousGCS) = NoCredentials() # No credentials
11+
AWS.check_credentials(c::NoCredentials) = c # Skip credentials check
12+
AWS.sign!(aws::AnonymousGCS, ::AWS.Request) = nothing # Don't sign request
13+
function AWS.generate_service_url(aws::AnonymousGCS, service::String, resource::String)
14+
service == "s3" || throw(ArgumentError("Can only handle s3 requests to GCS"))
15+
awsurl = string("https://objects.eodc.eu:2222", resource)
16+
return awsurl
17+
end
18+
AWS.global_aws_config(AnonymousGCS())
19+
path = "s3://e05ab01a9d56408d82ac32d69a5aae2a:sample-data/tutorial_data/cpm_v253/S2A_MSIL2A_20180601T102021_N0500_R065_T32UPC_20230902T045008.zarr/measurements/reflectance/r20m"
20+
21+
pathbase = "s3://e05ab01a9d56408d82ac32d69a5aae2a:sample-data/eopf-sample-output/"
22+
zopen(path)
23+
c = Cube(open_dataset(zopen(path)))
24+
yax = YAXArray((Dim{:x}(1:500), Dim{:y}(500:-1:1), Dim{:Variables}(1:10)), rand(500,500,10))
25+
c = yax
26+
function spectraldiversity(c)
27+
dlat = dims(c, :x)
28+
dlon = dims(c, :y)
29+
dband = dims(c, :Variables)
30+
# This needs a better interface for within index space
31+
latinterval = MovingIntervals(center=dlat.val, width=3*step(dlat), n=length(dlat)-1, step=step(dlat))
32+
loninterval = MovingIntervals(center=dlon.val, width=-3*step(dlon), n=length(dlon)-1, step=-step(dlon))
33+
bandinterval = MovingIntervals(:closed, :closed, left=first(dband.val), right=last(dband.val), n=1)
34+
meanwindows = windows(c, :y=>loninterval, :Variables => Whole())
35+
@show meanwindows[10,10,1]
36+
@time xmap(innerdiversity, meanwindows)
37+
end
38+
spectraldiversity(c)
39+
function innerdiversity(xout, arr)
40+
winsize = prod(size(arr)) ÷ size(arr,3)
41+
arrreshaped = reshape(arr, (winsize, size(arr,3)))
42+
tslist = eachrow(arrreshaped)
43+
dists = pairwise(Euclidean(), tslist) .* 2 ./ winsize .^ 4
44+
xout .= sum(dists) ./ 2
45+
end
46+
47+
spectraldiversity(c)

src/SentinelDataSource.jl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
module SentinelDataSource
2-
using DimensionalData: DimTree
2+
using DimensionalData: DimTree, DimArray, DimensionalData
33
using Zarr: zopen
44
using ZarrDatasets: ZarrDataset
55
using CommonDataModel: CommonDataModel as CDM
6-
using Rasters:Raster
7-
6+
using Rasters: Rasters
7+
using TimerOutputs
88
export open_tree
99

1010
open_tree(path::AbstractString) = open_tree(ZarrDataset(path))
1111

1212
function open_tree(dataset::ZarrDataset)
13-
stem = DimTree()
14-
groupnames = CDM.groupnames(dataset)
15-
varnames = CDM.varnames(dataset)
16-
alldimnames = nesteddimnames(dataset)
17-
for v in setdiff(varnames, alldimnames)
18-
setindex!(stem, Raster(CDM.variable(dataset, v), lazy=true),Symbol(v))
13+
@timeit_debug "stem" stem = DimTree()
14+
@timeit_debug "groups" groupnames = CDM.groupnames(dataset)
15+
@timeit_debug "vars" varnames = CDM.varnames(dataset)
16+
@timeit_debug "dims" alldimnames = nesteddimnames(dataset)
17+
diffnames = setdiff(varnames, alldimnames)
18+
for v in diffnames
19+
@timeit_debug "var $v" begin
20+
var = CDM.variable(dataset, v)
21+
vardims = Rasters._dims(var)
22+
metadata_out = Rasters._metadata(var)
23+
missingval_out = Rasters._read_missingval_pair(var, metadata_out, Rasters.nokw)
24+
mod = Rasters._mod(eltype(var), metadata_out, missingval_out;scaled=true, coerce=true)
25+
#=
26+
Rasters.FileArray{ZarrDataset}(var, filename;
27+
name=v, Rasters.nokw, mod, write=false
28+
)
29+
=#
30+
vardata = Rasters._maybe_modify(var, mod)
31+
setindex!(stem, DimArray(vardata, vardims),Symbol(v))
32+
end
1933
end
2034
for g in groupnames
21-
setindex!(stem, open_tree(CDM.group(dataset, g)),Symbol(g))
35+
@timeit_debug "forg $g" setindex!(stem, open_tree(CDM.group(dataset, g)),Symbol(g))
2236
end
2337
stem
2438
end

0 commit comments

Comments
 (0)