Skip to content

Commit 8bf7386

Browse files
author
Felix Cremer
authored
Add percentile range (#122)
* Add percentile range * Fix replace on a path * Filter nans and missings inside prange inner function
1 parent 28e0223 commit 8bf7386

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/RQADeforestation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using NetCDF
1010
using TestItems
1111
using DimensionalData: DimensionalData as DD
1212

13-
export gdalcube, rqatrend
13+
export gdalcube, rqatrend, prange
1414

1515
include("metrics.jl")
1616
include("auxil.jl")

src/main.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ function main(;
7878
threshold=3.0,
7979
folders=["V1M0R1", "V1M1R1", "V1M1R2"],
8080
stack=:lazyagg,
81-
delete_intermediate=false
81+
delete_intermediate=false,
82+
compute_prange=true,
8283
)
8384
outdir = Path(outdir)
8485
in(orbit, ["A", "D"]) || error("Orbit needs to be either A or D")
@@ -140,7 +141,14 @@ function main(;
140141
c = Cube(tmppath)
141142
@time "save to S3" savecube(setchunks(c, (15000,15000)), string(orbitoutpath))
142143
rm(tmppath, recursive=true)
143-
@show delete_intermediate
144+
if compute_prange
145+
prangepath = Path(replace(string(orbitoutpath), "rqatrend"=>"prange", "thresh_3.0_"=>""))
146+
tmppath = tempname() * ".zarr"
147+
@time "prange" prange(tcube, outpath=tmppath, overwrite=true)
148+
cprange = Cube(tmppath)
149+
@time "save to S3" savecube(setchunks(c, (15000,15000)), string(prangepath))
150+
rm(tmppath, recursive=true)
151+
end
144152
if delete_intermediate == false
145153
#PyramidScheme.buildpyramids(orbitoutpath)
146154
Zarr.consolidate_metadata(string(orbitoutpath))

src/rqatrend.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,44 @@ Weighted average of `a` and `b` with weight `γ`.
170170
``(1 - γ) * a + γ * b``
171171
"""
172172
smooth(a, b, γ) = a + γ * (b - a)
173+
174+
"""
175+
prange(xout, xin)
176+
177+
Compute the percentile range for the non-missing time steps of xin, and save it to xout.
178+
`lowerpercentile` and `upperpercentile` specify the boundary of the percentile range.
179+
These have to be between 0 and 1.
180+
"""
181+
function prange(xout, xin, lowpercentile=0.02, upperpercentile=0.98)
182+
xinfiltered = filter(!ismissing, xin)
183+
filter!(!isnan, xinfiltered)
184+
lowerlim, upperlim = quantile(xinfiltered, [lowpercentile, upperpercentile])
185+
xout .= upperlim - lowerlim
186+
end
187+
188+
function prange(cube; lowerpercentile=0.02, upperpercentile=0.98, outpath=tempname() * ".zarr", overwrite=false, kwargs...)
189+
mapCube(prange, cube, lowerpercentile, upperpercentile; indims=InDims("Time"), outdims=OutDims(; outtype=Float32, path=outpath, fill_value=NaN, overwrite, kwargs...))
190+
end
191+
192+
@testitem "prange cube" begin
193+
using YAXArrays
194+
using Dates
195+
using DimensionalData: Ti, X, Y
196+
using Statistics
197+
import Random
198+
Random.seed!(1234)
199+
200+
mock_axes = (
201+
Ti(Date("2022-01-01"):Day(1):Date("2022-01-01")+ Day(100)),
202+
X(range(1, 10, length=10)),
203+
Y(range(1, 5, length=15)),
204+
)
205+
s = size(mock_axes)
206+
mock_data = reshape(1:prod(s), s)
207+
mock_props = Dict()
208+
mock_cube = YAXArray(mock_axes, mock_data, mock_props)
209+
210+
mock_trend = prange(mock_cube)
211+
@test mock_trend.axes == (mock_cube.X, mock_cube.Y)
212+
@test mock_trend[1,1] == 96
213+
end

0 commit comments

Comments
 (0)