Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 3, 2024
1 parent cdeea99 commit 4052196
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 38 deletions.
2 changes: 1 addition & 1 deletion ext/IpaperNaNExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function _nanquantile!(q::AbstractVector, x::AbstractVector,
for k = eachindex(probs)
q[k] = NaNStatistics._nanquantile!(x, probs[k], (1,))[1]
end
q
return q
end

function _nanquantile!(A, q::Real, dims::Int64)
Expand Down
10 changes: 4 additions & 6 deletions ext/IpaperSlopeExt/slope_mk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,15 @@ function slope_mk(y::AbstractVector, x::AbstractVector=1:length(y);
end

VS = var_S * essf
if S == 0
z = 0.0
z0 = 0.0
elseif S > 0
z = 0.0
z0 = 0.0
if S > 0
z = (S - 1) / sqrt2(VS)
z0 = (S - 1) / sqrt(var_S)
else
elseif S < 0
z = (S + 1) / sqrt2(VS)
z0 = (S + 1) / sqrt(var_S)
end

# pvalue0 = 2 * pnorm(-abs(z0))
# Tau = S / (0.5 * n * (n - 1))
pvalue = 2 * pnorm(-abs(z))
Expand Down
10 changes: 5 additions & 5 deletions src/sf/Ops.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# https://gdal.org/tutorials/geotransforms_tut.html
function getgeotransform(ra::AbstractSpatRaster)
x, y = st_dims(ra)

cellx, celly = st_cellsize(ra)
y0 = y[1] - celly / 2
x0 = x[1] - cellx / 2
[x0, cellx, 0, y0, 0, celly]
end


flipud(x::AbstractArray{T,2}) where {T<:Real} = x[end:-1:1, :]
flipud(x::AbstractArray{T,3}) where {T<:Real} = x[end:-1:1, :, :]
flipud(x::AbstractArray{T,2}) where {T<:Real} = @view x[end:-1:1, :]
flipud(x::AbstractArray{T,3}) where {T<:Real} = @view x[end:-1:1, :, :]
fliplr(x::AbstractArray{T,2}) where {T<:Real} = @view x[:, end:-1:1]
fliplr(x::AbstractArray{T,3}) where {T<:Real} = @view x[:, end:-1:1, :]

fliplr(x::AbstractArray{T,2}) where {T<:Real} = x[:, end:-1:1]
fliplr(x::AbstractArray{T,3}) where {T<:Real} = x[:, end:-1:1, :]
export flipud, fliplr
21 changes: 10 additions & 11 deletions src/sf/SpatRaster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function SpatRaster(A::AbstractArray{T,N}, b::bbox; reverse_lat=true, time=nothi
SpatRaster(; A, b, cellsize, lon, lat, time, bands, name)
end

function SpatRaster(r::SpatRaster, A::AbstractArray; reverse_lat=true)
function SpatRaster(A::AbstractArray, r::SpatRaster; reverse_lat=true)
(; b, time, bands, name) = r
if size(A)[1:2] != size(r.A)[1:2]
lon, lat = bbox2dims(b; size=size(A), reverse_lat)
Expand All @@ -45,13 +45,12 @@ end
Base.ndims(ra::AbstractSpatRaster) = ndims(ra.A)
Base.size(ra::AbstractSpatRaster) = size(ra.A)
Base.size(ra::AbstractSpatRaster{T,2}) where {T} = (size(ra.A)..., 1)

Base.parent(ra::AbstractSpatRaster) = ra.A
Base.iterate(ra::AbstractSpatRaster) = iterate(ra.A)
Base.length(ra::AbstractSpatRaster) = length(ra.A)
# Base.parent(ra::AbstractSpatRaster) = ra.A
# Base.iterate(ra::AbstractSpatRaster) = iterate(ra.A)
# Base.length(ra::AbstractSpatRaster) = length(ra.A)
# Base.size(ra::AbstractSpatRaster) = size(ra.A)
Base.eltype(::Type{AbstractSpatRaster{T}}) where {T} = T
Base.map(f, ra::AbstractSpatRaster) = SpatRaster(ra, map(f, ra.A))
# Base.eltype(::Type{AbstractSpatRaster{T}}) where {T} = T
# Base.map(f, ra::AbstractSpatRaster) = SpatRaster(map(f, ra.A), ra)

# !note about NaN values
Base_ops = ((:Base, :+), (:Base, :-), (:Base, :*), (:Base, :/),
Expand All @@ -63,12 +62,12 @@ for (m, f) in Base_ops
# _f = Symbol(m, ".:", f)
@eval begin
$m.$f(a::AbstractSpatRaster, b::AbstractSpatRaster) = begin
size(a) != size(b) || throw(DimensionMismatch("size mismatch"))
SpatRaster(a, $m.$f.(a.A, b.A))
size(a) != size(b) && throw(DimensionMismatch("size mismatch"))
SpatRaster($m.$f.(a.A, b.A), a)
end

$m.$f(a::AbstractSpatRaster, b::Real) = SpatRaster(a, $m.$f.(a.A, b))
$m.$f(a::Real, b::AbstractSpatRaster) = SpatRaster(a, $m.$f.(a, b.A))
$m.$f(a::AbstractSpatRaster, b::Real) = SpatRaster($m.$f.(a.A, b), a)
$m.$f(a::Real, b::AbstractSpatRaster) = SpatRaster($m.$f.(a, b.A), b)
end
end

Expand Down
10 changes: 5 additions & 5 deletions src/sf/st_resample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function resample2(r::AbstractArray; fact=10, deepcopy=false)
end

# for Raster
st_resample(x::AbstractArray; fact=10, kw...) = resample2(x; fact, kw...)
# st_resample(x::AbstractArray; fact=10, kw...) = resample2(x; fact, kw...)

function st_resample(ra::SpatRaster; fact=10, deepcopy=false)
A = resample2(ra.A; fact, deepcopy)
SpatRaster(A, ra.b)
end
# function st_resample(ra::SpatRaster; fact=10, deepcopy=false)
# A = resample2(ra.A; fact, deepcopy)
# SpatRaster(A, ra.b)
# end

# function st_resample(z::ZArray; fact=10, missingval=0)
# dat = resample2(z; fact)
Expand Down
12 changes: 6 additions & 6 deletions src/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ end
which_isnull(x) = findall(x .== nothing)
which_notnull(x) = findall(x .!= nothing)

which_isnan(x) = findall(isnan(x))
which_notnan(x) = findall(.!isnan(x))
which_isnan(x) = findall(isnan.(x))
which_notnan(x) = findall(.!isnan.(x))

Base.isnan(x::AbstractArray) = isnan.(x)

all_isnan(x::AbstractArray) = all(isnan(x))
any_isnan(x::AbstractArray) = any(isnan(x))
# Base.isnan(x::AbstractArray) = isnan.(x)
all_isnan(x::AbstractArray) = all(isnan.(x))
any_isnan(x::AbstractArray) = any(isnan.(x))


# TODO: need to test
Expand Down Expand Up @@ -187,6 +186,7 @@ export which_isnull, which_notnull,
abind,
findnear,
set_seed;
export array
export isnan, all_isnan, any_isnan;
export obj_size, r_summary, r_split
export zip_continue
Expand Down
18 changes: 17 additions & 1 deletion test/sf/test-rast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ using Test, Ipaper, Ipaper.sf, ArchGDAL
A = rand(4, 4)
r2 = rast(A, b)

@test ndims(r2) == 2
@test (r2 + 1).A == (1 + r2).A
@test (r2 + r2).A == 2 * r2.A

f = "test.tif"
write_gdal(r2, f)
@test read_gdal(f)[:, :, 1] == A
Expand All @@ -18,7 +22,7 @@ using Test, Ipaper, Ipaper.sf, ArchGDAL
@test st_bbox(f) == b
isfile(f) && rm(f)

print(r2)
print(r3)
@test size(r2) == (4, 4, 1)
@test size(r3) == (4, 4, 3)

Expand All @@ -28,6 +32,8 @@ using Test, Ipaper, Ipaper.sf, ArchGDAL
@test (r3 / 2).A == r3.A ./ 2
end



@testset "gdal_nodata" begin
b = bbox(-180.0, -60.0, 180.0, 90.0)
A = rand(4, 4)
Expand All @@ -41,3 +47,13 @@ end
@test gdal_nodata("test2.tif")[1] == 2.0
rm.(["test.tif", "test2.tif"])
end


@testset "flipud" begin
A = array(1:16; dims=(4, 4))
A |> flipud |> flipud == A
@test flipud(A)[:, 1] == [4, 3, 2, 1]

A |> fliplr |> fliplr == A
@test fliplr(A)[1, :] == [13, 9, 5, 1]
end
4 changes: 2 additions & 2 deletions test/sf/test-st_extract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
inds, vals = st_extract(ra, points)
@test length(vals) == 2

r2 = st_resample(ra; fact=10)
@test size(r2) == (16, 12, 1)
# r2 = st_resample(ra; fact=10)
# @test size(r2) == (16, 12, 1)
end
9 changes: 8 additions & 1 deletion test/test-Ipaper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ end
@test collect(values(tbl)) == [3, 4]
end


@testset "nan" begin
x = [1, 2, NaN, 4]
@test all_isnan(x) == false
@test all_isnan([NaN, NaN])
@test all_isnan([NaN])
@test which_isnan(x) == [3]
@test which_notnan(x) == [1, 2, 4]
end

0 comments on commit 4052196

Please sign in to comment.