-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Dates | ||
import Dates: Date, DateTime, Year, Month, Day, year, month, day, format | ||
# using CFTime | ||
|
||
include("interval_intersect.jl") | ||
include("utilize.jl") | ||
include("weight_d8mon.jl") | ||
|
||
export dates_miss, dates_nmiss, | ||
DateTime, Date, year, month, day, Year, Month, Day, format, | ||
make_datetime, make_date, | ||
date_doy, | ||
date_year, date_ym, date_dn | ||
export weight_d8mon | ||
export interval_intersect, datediff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function interval_intersect(x::Tuple{T,T}, y::Tuple{T,T}) where {T<:Real} | ||
left = max(x[1], y[1]) # intersect | ||
right = min(x[2], y[2]) | ||
|
||
if left <= right | ||
return right - left | ||
else | ||
return 0 | ||
end | ||
end | ||
|
||
function interval_intersect(x::Tuple{T,T}, y::Tuple{T,T}) where {T<:Union{Date,DateTime}} | ||
left = max(x[1], y[1]) # intersect | ||
right = min(x[2], y[2]) | ||
|
||
if left <= right | ||
return convert(Dates.Day, right - left) | ||
else | ||
return Day(0) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
weight_d8_mon(dates_beg::Vector{T}, date::T) where {T<:Union{Date,DateTime}} | ||
weight_d8_mon(dates_beg::Vector{T}, dates_end::Vector{T}, date::T) where {T<:Union{Date,DateTime}} | ||
convert MODIS 8-day to monthly | ||
""" | ||
function weight_d8mon(dates_beg::Vector{T}, date::T) where {T<:Union{Date,DateTime}} | ||
dates_end = add_d8.(dates_beg) | ||
weight_d8mon(dates_beg, dates_end, date) | ||
end | ||
|
||
function weight_d8mon(dates_beg::Vector{T}, dates_end::Vector{T}, date::T) where {T<:Union{Date,DateTime}} | ||
|
||
date_beg = date | ||
date_end = DateTime(year(date_beg), month(date_beg), daysinmonth(date_beg)) | ||
interval = (date_beg, date_end) | ||
|
||
inds = findall(@.( | ||
date_beg <= dates_beg <= date_end || | ||
date_beg <= dates_end <= date_end)) | ||
days_full = datediff.(dates_end[inds], dates_beg[inds]) .+ 1 | ||
|
||
days = map(i -> begin | ||
date_beg, date_end = dates_beg[i], dates_end[i] | ||
int2 = date_beg, date_end | ||
interval_intersect(interval, int2) + Day(1) |> x -> x.value | ||
end, inds) | ||
(; date_beg=dates_beg[inds], date_end=dates_end[inds], index=inds, | ||
days, days_full, w=days ./ days_full) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
weighted_sum(x::AbstractVector, w::AbstractVector) = sum(x .* w) | ||
|
||
function weighted_nansum(x::AbstractVector{T}, w::AbstractVector) where {T<:Real} | ||
∑ = T(0) | ||
@inbounds for i in eachindex(x) | ||
∑ += ifelse(x[i] == x[i], x[i] * w[i], T(0)) | ||
end | ||
return ∑ | ||
end | ||
|
||
function weighted_nansum(A::AbstractArray{T,3}, w::AbstractVector) where {T<:Real} | ||
nlon, nlat, ntime = size(A) | ||
R = zeros(nlon, nlat) #.* T(NaN) | ||
|
||
@inbounds for i = 1:nlon, j = 1:nlat | ||
# ∑ = T(0) | ||
for k = 1:ntime | ||
xi = A[i, j, k] | ||
xi == xi && (R[i, j] += xi * w[i]) | ||
# R[i, j] += xi == xi ? xi * w[i] : T(0) | ||
end | ||
end | ||
return R | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters