Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 3, 2024
1 parent 4c5c807 commit 11a8613
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/Interpolation/bilinear_helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 1 addition & 15 deletions src/Interpolation/utilize.jl
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
5 changes: 5 additions & 0 deletions src/NetCDFTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 3 additions & 4 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ 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)

# compile bilinear
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)
Expand Down
34 changes: 31 additions & 3 deletions src/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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!
32 changes: 0 additions & 32 deletions src/tools_rbase.jl

This file was deleted.

2 changes: 1 addition & 1 deletion test/interp/test-weighted_nanmean.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Test
using Test, Ipaper, NetCDFTools

@testset "weighted_nanmean" begin
x = [1, 2, 3, NaN]
Expand Down
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions test/test-Ipaper.jl
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit 11a8613

Please sign in to comment.