diff --git a/src/Interpolation/bilinear_helper.jl b/src/Interpolation/bilinear_helper.jl index ae241b4..ca0ecea 100644 --- a/src/Interpolation/bilinear_helper.jl +++ b/src/Interpolation/bilinear_helper.jl @@ -38,8 +38,8 @@ function approx(x, y, xout) return yout end -array(val; dims) = reshape(val, dims...) -array(val, dims) = array(val; dims) +# array(val; dims) = reshape(val, dims...) +# array(val, dims) = array(val; dims) function meshgrid(x, y) X = repeat(x', length(y), 1) diff --git a/src/Interpolation/utilize.jl b/src/Interpolation/utilize.jl index c1755ef..d69641d 100644 --- a/src/Interpolation/utilize.jl +++ b/src/Interpolation/utilize.jl @@ -1,19 +1,5 @@ export rm_empty, weighted_nanmean!, weighted_nanmean, earth_dist - -function weighted_nanmean(x::AbstractVector{T1}, w::AbstractVector{T2}) where {T1,T2} - T = promote_type(T1, T2) - ∑ = ∅ = T(0) - ∑w = ∅w = T2(0) - - @inbounds for i = eachindex(x) - # if !isnan(x[i]); end - xᵢ = x[i] - notnan = xᵢ == xᵢ - ∑ += ifelse(notnan, x[i] * w[i], ∅) - ∑w += ifelse(notnan, w[i], ∅w) - end - return ∑ / ∑w -end +import Ipaper: weighted_nanmean # byrow function weighted_nanmean(mat::AbstractMatrix{T1}, w::AbstractVector{T2}) where {T1,T2} diff --git a/src/NetCDFTools.jl b/src/NetCDFTools.jl index 42b2c62..8db343b 100644 --- a/src/NetCDFTools.jl +++ b/src/NetCDFTools.jl @@ -21,6 +21,11 @@ import DataFrames: AbstractDataFrame, GroupedDataFrame using Ipaper @reexport using Ipaper.sf +export exact_extract, coverage_fraction + +function exact_extract end +function coverage_fraction end + include("NCDataset.jl") include("tools.jl") diff --git a/src/precompile.jl b/src/precompile.jl index 7fa9bbb..bb8cc0a 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -13,9 +13,8 @@ precompile(nc_calendar, (String,)) "http://esgf-data04.diasjp.net/thredds/dodsC/esg_dataroot/CMIP6/ScenarioMIP/CSIRO-ARCCSS/ACCESS-CM2/ssp126/r1i1p1f1/day/huss/gn/v20210317/huss_day_ACCESS-CM2_ssp126_r1i1p1f1_gn_20150101-20641231.nc", "http://esgf-data04.diasjp.net/thredds/dodsC/esg_dataroot/CMIP6/ScenarioMIP/CSIRO-ARCCSS/ACCESS-CM2/ssp126/r1i1p1f1/day/huss/gn/v20210317/huss_day_ACCESS-CM2_ssp126_r1i1p1f1_gn_20650101-21001231.nc" ] - - - @compile_workload begin + + @compile_workload begin info = CMIP.CMIPFiles_info(fs; detailed=false) info_s = CMIP.CMIPFiles_summary(info) @@ -23,7 +22,7 @@ precompile(nc_calendar, (String,)) for T in (Float64, Float32) lon = 70:5:140 lat = 15:5:55 - + Lon = 70:2.5:140 Lat = 15:2.5:55 Z = rand(T, length(lon), length(lat), 2) diff --git a/src/tools.jl b/src/tools.jl index 0565268..c5bdeec 100644 --- a/src/tools.jl +++ b/src/tools.jl @@ -29,7 +29,7 @@ function split_chunk(n::Int, nchunk=4; chunk=nothing, ratio_small::Real=0.0) lst[end-1] = i_beg:i_end deleteat!(lst, nchunk) end - lst + return lst end function split_chunk(x::Union{UnitRange,AbstractVector}, nchunk=4; kw...) @@ -47,7 +47,35 @@ function split_date(dates; ny_win=10, kw...) lst_index = map(grp -> begin findall(indexin(years, grp) .!== nothing) end, lst) - lst_index + return lst_index end -export split_chunk, split_date + +function updateMask!(A::AbstractArray{T,2}, mask::BitMatrix) where {T<:Real} + missval = T(NaN) + A[.!mask] .= missval + return A +end + +function updateMask!(A::AbstractArray{T,3}, mask::BitMatrix) where {T<:Real} + missval = T(NaN) + nlon, nlat, ntime = size(A) + # lgl = .!mask + @inbounds @par for k in 1:ntime + for j = 1:nlat, i = 1:nlon + !mask[i, j] && (A[i, j, k] = missval) + end + end + return A +end + +function updateMask!(A::AbstractArray{T,N}, mask::BitMatrix) where {T<:Real,N} + @inbounds for t in axes(A, N) + ind = (repeat([:], N - 1)..., t) + x = @view A[ind...] + updateMask!(x, mask) + end + A +end + +export split_chunk, split_date, updateMask! diff --git a/src/tools_rbase.jl b/src/tools_rbase.jl deleted file mode 100644 index b01f14b..0000000 --- a/src/tools_rbase.jl +++ /dev/null @@ -1,32 +0,0 @@ -export exact_extract, coverage_fraction, updateMask! - - -function exact_extract end -function coverage_fraction end - -function updateMask!(A::AbstractArray{T,2}, mask::BitMatrix) where {T<:Real} - missval = T(NaN) - A[.!mask] .= missval - A -end - -function updateMask!(A::AbstractArray{T,3}, mask::BitMatrix) where {T<:Real} - missval = T(NaN) - nlon, nlat, ntime = size(A) - # lgl = .!mask - @inbounds @par for k in 1:ntime - for j = 1:nlat, i = 1:nlon - !mask[i, j] && (A[i, j, k] = missval) - end - end - A -end - -function updateMask!(A::AbstractArray{T,N}, mask::BitMatrix) where {T<:Real,N} - @inbounds for t in axes(A, N) - ind = (repeat([:], N - 1)..., t) - x = @view A[ind...] - updateMask!(x, mask) - end - A -end diff --git a/test/interp/test-weighted_nanmean.jl b/test/interp/test-weighted_nanmean.jl index 44cc2be..40e39e9 100644 --- a/test/interp/test-weighted_nanmean.jl +++ b/test/interp/test-weighted_nanmean.jl @@ -1,4 +1,4 @@ -using Test +using Test, Ipaper, NetCDFTools @testset "weighted_nanmean" begin x = [1, 2, 3, NaN] diff --git a/test/runtests.jl b/test/runtests.jl index 7a5164e..5ea7b22 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,6 @@ using Test using NetCDFTools using Ipaper, Dates -import NetCDFTools: weighted_nanmean dir_root = dirname(dirname(@__FILE__)) proj_path(f) = dirname(dirname(@__FILE__)) * "/" * f diff --git a/test/test-Ipaper.jl b/test/test-Ipaper.jl index c6e48f2..193d266 100644 --- a/test/test-Ipaper.jl +++ b/test/test-Ipaper.jl @@ -1,15 +1,16 @@ using Test, NetCDFTools, Ipaper + @testset "updateMask" begin A = array(1.0:16, (4, 4)) |> collect mask = A .> 10 updateMask!(A, mask) - length(findall(A .> 10)) == 6 + @test length(findall(A .> 10)) == 6 - A = rand(4, 4, 2) - mask = A[:, :, 1] .> 0.5 + A = rand(4, 4, 2, 2) + mask = A[:, :, 1, 1] .> 0.5 updateMask!(A, mask) - @test isempty(findall(A[:, :, 1] .<= 0.5)) + @test isempty(findall(A[:, :, 1, 1] .<= 0.5)) end