Skip to content

Commit

Permalink
Create processERA5.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Balinus authored Sep 12, 2024
1 parent de56c3a commit c9648e5
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/processERA5.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
ERA5Land_dailysum(xout, xin; index_list = time_to_index)
Compute the daily sum of ERA5 land data. **used internally**
# Arguments
- `xout::AbstractArray`: Output array to store the computed daily sums.
- `xin::AbstractArray`: Input array containing ERA5 land data.
- `index_list::Function`: (optional) Function to convert time indices.
# Details
- The function computes the daily sum of ERA5 land data by doing first a diff operator and then summing the non-missing values in each day's data.
- Missing values are represented as `missing` in the input array and are replaced with `NaN` before computation.
"""
function ERA5Land_dailysum(xout, xin; index_list = time_to_index)

xout .= NaN
if !all(ismissing, xin)
for i in eachindex(index_list)
data = view(xin, index_list[i])
if !all(ismissing, data)
diffxin = Base.diff(replace(data, missing => NaN))
xout[i] = sum(.!isnan.(diffxin))
end
end
end
end

"""
ERA5Land_dailysum(cube::YAXArray; keep_1stday=false, kwargs...)
Process ERA5-Land data by converting it to daily resolution. The function applies a diff function and then a sum for daily accumulated values.
# Arguments
- `cube::YAXArray`: The input data cube.
- `keep_1stday::Bool`: Whether to keep the first day of the data. Default is `false`.
- `kwargs...`: Additional keyword arguments.
# Returns
- `cube::YAXArray`: The processed data cube.
"""
function ERA5Land_dailysum(cube::YAXArray; keep_1stday=false, kwargs...)

# On veut des données quotidiennes pour ERA5-Land
time_to_index = (cube.time .- Hour(1))
time_index = yearmonthday.(time_to_index)
new_dates = unique(time_index)

if keep_1stday
firstindex = 1
else
firstindex = 2
end
index_in_cube = [findall(==(i), time_index) for i in unique(time_index)][firstindex:end]

# Dimensions
indims = InDims("Ti")
# outdims = OutDims(RangeAxis("time", dates_builder_yearmonthday(new_dates[firstindex:end])))
outdims = OutDims(Dim{:Ti}(dates_builder_yearmonthday(new_dates[firstindex:end])))

return mapCube(ERA5Land_dailysum, cube, indims=indims, outdims=outdims, index_list=index_in_cube)

end

0 comments on commit c9648e5

Please sign in to comment.