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

Fix weather data errors and extract weather R script

parent b7a3886f
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,15 @@ TODO.md ...@@ -2,3 +2,15 @@ TODO.md
docs/planning docs/planning
*results*/ *results*/
profile*txt profile*txt
# R stuff
.Rhistory
.Rprofile
renv/
# temporary downloaded files from DWD
/data/regions/auxiliary/DWDdata/
/data/regions/auxiliary/daily_kl_historical_tageswerte_*.zip
/data/regions/auxiliary/soil_daily_historical_*.txt
/data/regions/auxiliary/soil_daily_historical_*.txt.gz
...@@ -15,25 +15,31 @@ library(rdwd) ...@@ -15,25 +15,31 @@ library(rdwd)
#rdwd::updateRdwd() # run this now and again to make sure we have the latest file index #rdwd::updateRdwd() # run this now and again to make sure we have the latest file index
## SELECTION PARAMETERS region_to_stationid = c(
"Jena" = 2444,
region = "Oberrhein" # select from `stationid` list below "Eichsfeld" = 2925,
"Thüringer Becken" = 896,
"Hohenlohe" = 3761,
"Bodensee" = 6263,
"Oberrhein" = 5275
)
startdate = as.Date("1990-01-01") # earliest date to include (if available) download_data = function(
region, # e.g. "Jena"
startdate = as.Date("1990-01-01"), # earliest date to include (if available)
enddate = as.Date("2024-12-31") # latest date to include (if available) enddate = as.Date("2024-12-31") # latest date to include (if available)
) {
## DOWNLOAD DATA ## DOWNLOAD DATA
### observed climate data (these include most of our parameters) ### observed climate data (these include most of our parameters)
stationid = c("Jena" = 2444, regions = names(region_to_stationid)
"Eichsfeld" = 2925, if (! region %in% regions) {
"Thüringer Becken" = 896, cat("Available regions: ", regions, "\n")
"Hohenlohe" = 3761, stop(paste("Region ", region), " not found in available regions.\n")
"Bodensee" = 6263, }
"Oberrhein" = 5275) stationid = region_to_stationid[region]
observed_url = selectDWD(id = stationid,
observed_url = selectDWD(id = stationid[region],
res = "daily", res = "daily",
per = "historical", per = "historical",
var = "kl") var = "kl")
...@@ -74,18 +80,52 @@ weather = climdata %>% ...@@ -74,18 +80,52 @@ weather = climdata %>%
max_temperature=TXK, TXK=NULL, max_temperature=TXK, TXK=NULL,
min_temperature=TNK, TNK=NULL) min_temperature=TNK, TNK=NULL)
firstdate = weather$date[1] firstdate = min(weather$date, na.rm = TRUE)
lastdate = weather$date[nrow(weather)] lastdate = max(weather$date, na.rm = TRUE)
all_dates = tibble(date = seq.Date(firstdate, lastdate, by = "day"))
ETo = soildata[names(soildata)[grep("v2", names(soildata))]][[1]] %>% ETo = soildata[names(soildata)[grep("v2", names(soildata))]][[1]] %>%
as_tibble %>% select(Datum, VPGFAO) %>% as_tibble %>% select(Datum, VPGFAO) %>%
filter(Datum >= firstdate, Datum <= lastdate) %>% filter(Datum >= firstdate, Datum <= lastdate) %>%
mutate(potential_evapotranspiration=VPGFAO, VPGFAO=NULL) mutate(potential_evapotranspiration=VPGFAO, VPGFAO=NULL)
# align and combine the two data sets timewise weather = all_dates %>%
gapbefore = rep(NA, as.numeric(ETo$Datum[1] - firstdate)) left_join(weather, by="date") %>%
gapafter = rep(NA, as.numeric(lastdate - ETo$Datum[nrow(ETo)])) left_join(ETo %>% rename(date = Datum), by="date")
potevap = c(gapbefore, ETo$potential_evapotranspiration, gapafter)
weather = weather %>% mutate(potential_evapotranspiration = potevap) return(weather)
}
write.csv(weather, file=paste0(region, "_weather.csv"), row.names=FALSE)
main = function(args, script_name = "extract_weather_data.R") {
print_usage = function(script_name, args) {
cat(paste("usage:", script_name), "region1 [region2] ...\n")
cat(paste(" ", script_name), "--all\n")
}
if (length(args) == 0) {
cat("error: not enough arguments\n")
print_usage(script_name, args)
return(-1)
}
if ("-h" %in% args || "--help" %in% args) {
print_usage(script_name, args)
return(0)
}
if ("--all" %in% args) {
regions = names(region_to_stationid)
} else {
regions = args
}
for (region in regions) {
weather = download_data(region)
write.csv(weather, file=paste0("weather_", gsub(" ", "_", region), ".csv"), row.names=FALSE)
}
return(0)
}
# Run main function if executed directly
if (sys.nframe() == 0) {
args = commandArgs(trailingOnly = TRUE)
cat("\nRegions = ", args, "\n\n")
status = main(args)
quit(status = status)
}
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -23,7 +23,7 @@ enddate = 2023-12-31 # last day of the simulation ...@@ -23,7 +23,7 @@ enddate = 2023-12-31 # last day of the simulation
#enddate = 2022-01-02 # last day of the simulation (test value) #enddate = 2022-01-02 # last day of the simulation (test value)
[world] [world]
mapdirectory = "data/regions/jena-small" # the directory in which all geographic data are stored mapdirectory = "data/regions/jena" # the directory in which all geographic data are stored
mapresolution = 10 # map resolution in meters mapresolution = 10 # map resolution in meters
landcovermap = "landcover.tif" # name of the landcover map in the map directory landcovermap = "landcover.tif" # name of the landcover map in the map directory
farmfieldsmap = "fields.tif" # name of the field geometry map in the map directory farmfieldsmap = "fields.tif" # name of the field geometry map in the map directory
......
...@@ -28,24 +28,27 @@ Load a weather file, extract the values that are relevant to this model run ...@@ -28,24 +28,27 @@ Load a weather file, extract the values that are relevant to this model run
(specified by start and end dates), and return a dictionary of Weather objects (specified by start and end dates), and return a dictionary of Weather objects
mapped to dates. mapped to dates.
**Note:** This requires a weather file in the format produced by `data/extract_weather_data.R`. **Note:** This requires a weather file in the format produced by
`data/regions/auxiliary/extract_weather_data.R`.
""" """
function initweather(weatherfile::String, startdate::Date, enddate::Date) function initweather(weatherfile::String, startdate::Date, enddate::Date)
@debug "Initialising weather" @debug "Initialising weather"
data = CSV.File(weatherfile, missingstring="NA", dateformat="yyyymmdd", data = CSV.File(weatherfile, missingstring="NA", dateformat="yyyy-mm-dd",
types=[Date, Float64, Float64, Float64, Float64, Float64, types=[Date, Float64, Float64, Float64, Float64, Float64,
Float64, Float64, Float64]) Float64, Float64, Float64, Float64])
weather = Dict{Date,Weather}() weather = Dict{Date,Weather}()
for row in data for row in data
if row.date >= startdate && row.date <= enddate if row.date >= startdate && row.date <= enddate
weather[row.date] = Weather(row.mean_windspeed, row.precipitation, weather[row.date] = Weather(row.mean_windspeed, row.precipitation,
row.sunshine_hours, row.mean_cloud_cover, row.sunshine_hours, row.mean_cloud_cover,
row.mean_humidity, row.mean_temperature, row.mean_humidity, row.mean_temperature,
row.max_temperature, row.min_temperature) row.max_temperature, row.min_temperature,
row.potential_evapotranspiration)
end end
end end
if length(weather) <= Dates.value(enddate-startdate) if length(weather) <= Dates.value(enddate-startdate)
@warn "There are missing days in the weather input file." @warn ("There are missing days in the weather input file:"
* " expected $(Dates.value(enddate-startdate) + 1), got $(length(weather)).")
end end
weather weather
end end
......
...@@ -60,10 +60,6 @@ end ...@@ -60,10 +60,6 @@ end
@testset "Weather initialisation" begin @testset "Weather initialisation" begin
# these tests are specific to the Jena weather file # these tests are specific to the Jena weather file
model = inittestmodel() model = inittestmodel()
@test_logs((:warn, "There are missing days in the weather input file."),
Ps.initweather(joinpath(TESTSETTINGS["world.mapdirectory"],
TESTSETTINGS["world.weatherfile"]),
Date("2022-01-01"), Date("2023-12-31")))
@test length(keys(model.weather)) == 59 @test length(keys(model.weather)) == 59
@test ismissing(Ps.windspeed(model)) @test ismissing(Ps.windspeed(model))
@test Ps.precipitation(model) == 1.3 @test Ps.precipitation(model) == 1.3
...@@ -73,5 +69,5 @@ end ...@@ -73,5 +69,5 @@ end
@test Ps.maxtemp(model) == 7.5 @test Ps.maxtemp(model) == 7.5
@test Ps.mintemp(model) == 0.1 @test Ps.mintemp(model) == 0.1
stepsimulation!(model) stepsimulation!(model)
@test Ps.vapourpressure(model) == 7.7 @test Ps.evapotranspiration(model) == 0.6
end end
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment