Skip to content
Snippets Groups Projects
Commit 69ee3eb9 authored by Marco Matthies's avatar Marco Matthies
Browse files

Add docstrings and improve function naming

parent d353b955
No related branches found
No related tags found
No related merge requests found
...@@ -30,11 +30,23 @@ function Base.length(weather::Weather) ...@@ -30,11 +30,23 @@ function Base.length(weather::Weather)
return Dates.value(weather.lastdate - weather.firstdate) + 1 return Dates.value(weather.lastdate - weather.firstdate) + 1
end end
"""
daynumber(weather, date)
Returns the number of days, counting `weather.firstdate` as day `1`.
"""
function daynumber(weather::Weather, date::Date) function daynumber(weather::Weather, date::Date)
@assert weather.firstdate <= date <= weather.lastdate @assert weather.firstdate <= date <= weather.lastdate
return Dates.value(date - weather.firstdate) + 1 return Dates.value(date - weather.firstdate) + 1
end end
"""
findspans(predicate_fn, array) -> Vector{Tuple{Int, Int}}
Returns spans of indices in a 1-d `array` where a `predicate_fn`
returns `true`. The spans are returned as a `Vector{Tuple{Int,
Int}}`, where each tuple is of the form `(start_index, end_index)`.
"""
function findspans(predicate_fn, arr::AbstractVector) function findspans(predicate_fn, arr::AbstractVector)
spans = Tuple{Int, Int}[] spans = Tuple{Int, Int}[]
startidx = nothing startidx = nothing
...@@ -54,10 +66,16 @@ function findspans(predicate_fn, arr::AbstractVector) ...@@ -54,10 +66,16 @@ function findspans(predicate_fn, arr::AbstractVector)
return spans return spans
end end
function fix_missing_weatherdata!(df::DataFrame) """
colnames_to_fix = ["min_temperature", "max_temperature", "mean_temperature", check_missing_weatherdata(dataframe)
"precipitation", "potential_evapotranspiration"]
for colname in colnames_to_fix Check the weather input data for missing values in columns where input
values are required.
"""
function check_missing_weatherdata(df::DataFrame)
colnames = ["min_temperature", "max_temperature", "mean_temperature",
"precipitation", "potential_evapotranspiration"]
for colname in colnames
spans = findspans(ismissing, getproperty(df, colname)) spans = findspans(ismissing, getproperty(df, colname))
# TODO: fix missing values and warn here instead of error # TODO: fix missing values and warn here instead of error
if ! isempty(spans) if ! isempty(spans)
...@@ -101,7 +119,7 @@ function initweather(weatherfile::String, startdate::Date, enddate::Date) ...@@ -101,7 +119,7 @@ function initweather(weatherfile::String, startdate::Date, enddate::Date)
* " expected $(Dates.value(enddate - startdate) + 1), got $(nrow(df)))." * " expected $(Dates.value(enddate - startdate) + 1), got $(nrow(df)))."
* " Missing dates are $(setdiff(startdate:enddate, df.date)).") * " Missing dates are $(setdiff(startdate:enddate, df.date)).")
end end
fix_missing_weatherdata!(df) check_missing_weatherdata(df)
return Weather(; firstdate = minimum(df.date), return Weather(; firstdate = minimum(df.date),
lastdate = maximum(df.date), lastdate = maximum(df.date),
windspeed = df.mean_windspeed, windspeed = df.mean_windspeed,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment