Section 3 Weather Input
3.1 Loading Data
To add weather data to our SWAT project, we will use svatools
(Svajunas, Rets, and Strauch 2023)
update 2024: svatools has been migrated to SWATprepR
3.1.2 Load in the file(s) and load svatools template
First we load in the template and fill it in with our values and rename it to “cs10_weather_data.xlsx”. This is not done within R. No detailed documentation exists on the source of this data (yet).
There has been some discussion in issue #48
#temp_path <- system.file("extdata", "weather_data.xlsx", package = "svatools")
# /// fill out this template and save "cs10_weather_data.xlsx" ///
We are using the following projection for this project:
Now we can load it in with Svatools.
met_lst <- svatools::load_template(template_path = "model_data/input/met/cs10_weather_data.xlsx", epgs_code)
## [1] "Loading data from template."
## [1] "Reading station ID1 data."
## [1] "Loading of data is finished."
3.1.3 Proof the station
Checking the location of the station:
basin_path <- "model_data/input/shape/cs10_basin.shp"
basin <- st_transform(st_read(basin_path), epgs_code) %>%
mutate(NAME = "Basin")
## Reading layer `cs10_basin' from data source
## `D:\git\swat-cs10\model_data\input\shape\cs10_basin.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 603439.4 ymin: 6607792 xmax: 610984.4 ymax: 6622017
## Projected CRS: ETRS89 / UTM zone 32N
3.2 Weather Generator
We only have one weather station, which means only one weather generator.
wgn <- prepare_wgn(met_lst,
TMP_MAX = met_lst$data$ID1$TMP_MAX,
TMP_MIN = met_lst$data$ID1$TMP_MIN,
PCP = met_lst$data$ID1$PCP,
RELHUM = met_lst$data$ID1$RELHUM,
WNDSPD = met_lst$data$ID1$WNDSPD,
MAXHHR = met_lst$data$ID1$MAXHHR,
SLR = met_lst$data$ID1$SLR)
## [1] "Coordinate system checked and transformed to EPSG:4326."
## [1] "Working on station ID1:WS_AAS"
3.4 Atmospheric Deposition
Instructions and documentation can be found here
Getting the data:
basin_path <- "model_data/input/shape/cs10_basin.shp"
df <-
get_atmo_dep(
basin_path,
start_year = 2010,
end_year = 2020,
t_ext = "year"
)
readr::write_csv(df, file = "model_data/input/met/atmodep.csv")
A plot of the results:
df <- readr::read_csv("model_data/input/met/atmodep.csv", show_col_types = F)
ggplot(pivot_longer(df, !DATE, names_to = "par", values_to = "values"), aes(x = DATE, y = values))+
geom_line()+
facet_wrap(~par, scales = "free_y")+
theme_bw()
Adding the data to the SQLITE
Big thank you to Svajunas for his svatools package, making our lives much easier!
3.5 Re-analysis Weather
Note: we switched to this dataset in early 2024 as our model performed much better with this dataset. Detailed documentation of this performance increase might someday be added here.
As of March, 2024, our SWAT+ setup uses virtual weather stations derived from a spatially exhaustive dataset of weather data from MetNo’s (met.no) Reanalysis3 project. To do this, our in-house package miljotools
was used. You can read more about how this process works, and more about the dataset here
#remotes::install_github(repo = "moritzshore/miljotools", ref = remotes::github_release())
require(miljotools)
## Loading required package: miljotools
## Registered S3 methods overwritten by 'ggtern':
## method from
## grid.draw.ggplot ggplot2
## plot.ggplot ggplot2
## print.ggplot ggplot2
The data was sourced and stored in a separate repository:
https://gitlab.nibio.no/moritzshore/metnoreanalysis3-downloads
Therefore the code shown here will only be for documentation purposes and will not actually effect the current project setup.
dir.create("model_data/temp", showWarnings = F)
file.copy(from = list.files("model_data/cs10_setup/swat_input/", full.names = T), to = "model_data/temp", overwrite = T)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
The following function gets the (hourly) data. For demonstration purposes, only 3 days worth.
download_folder <- get_metno_reanalysis3(
area = "model_data/input/shape/cs10_basin.shp",
directory = "model_data/temp",
fromdate = "2015-01-01 00:00:00",
todate = "2015-01-03 00:00:00",
area_buffer = 1500,
preview = T
)
## MILJOTOOLS >> 'grid_resolution' not chosen, defaulting to 1x1km grid...
## downloading default variables:
## air_temperature_2m
## integral_of_surface_downwelling_shortwave_flux_in_air_wrt_time
## relative_humidity_2m
## precipitation_amount
## wind_speed_10m
## wind_direction_10m
## [1] "getting coordinates.."
## [1] "building query.."
## fetching with grid size of 1 x 1 km
## [1] "creating download folder.."
## [1] "starting download"
## [1] "downloading: y2015"
## downloading files (2/49)downloading files (3/49)downloading files (4/49)downloading files (5/49)downloading files (6/49)downloading files (7/49)downloading files (8/49)downloading files (9/49)downloading files (10/49)downloading files (11/49)downloading files (12/49)downloading files (13/49)downloading files (14/49)downloading files (15/49)downloading files (16/49)downloading files (17/49)downloading files (18/49)downloading files (19/49)downloading files (20/49)downloading files (21/49)downloading files (22/49)downloading files (23/49)downloading files (24/49)downloading files (25/49)downloading files (26/49)downloading files (27/49)downloading files (28/49)downloading files (29/49)downloading files (30/49)downloading files (31/49)downloading files (32/49)downloading files (33/49)downloading files (34/49)downloading files (35/49)downloading files (36/49)downloading files (37/49)downloading files (38/49)downloading files (39/49)downloading files (40/49)downloading files (41/49)downloading files (42/49)downloading files (43/49)downloading files (44/49)downloading files (45/49)downloading files (46/49)downloading files (47/49)downloading files (48/49)downloading files (49/49)
## [1] "download complete!, merging files.."
## [1] "cropping dataset to area coverage.."
## Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
## deprecated. It might return a CRS with a non-EPSG compliant axis order.
## [1] "writing station data to csv.."
## writing files... (2/122)writing files... (3/122)writing files... (4/122)writing files... (5/122)writing files... (6/122)writing files... (7/122)writing files... (8/122)writing files... (9/122)writing files... (10/122)writing files... (11/122)writing files... (12/122)writing files... (13/122)writing files... (14/122)writing files... (15/122)writing files... (16/122)writing files... (17/122)writing files... (18/122)writing files... (19/122)writing files... (20/122)writing files... (21/122)writing files... (22/122)writing files... (23/122)writing files... (24/122)writing files... (25/122)writing files... (26/122)writing files... (27/122)writing files... (28/122)writing files... (29/122)writing files... (30/122)writing files... (31/122)writing files... (32/122)writing files... (33/122)writing files... (34/122)writing files... (35/122)writing files... (36/122)writing files... (37/122)writing files... (38/122)writing files... (39/122)writing files... (40/122)writing files... (41/122)writing files... (42/122)writing files... (43/122)writing files... (44/122)writing files... (45/122)writing files... (46/122)writing files... (47/122)writing files... (48/122)writing files... (49/122)writing files... (50/122)writing files... (51/122)writing files... (52/122)writing files... (53/122)writing files... (54/122)writing files... (55/122)writing files... (56/122)writing files... (57/122)writing files... (58/122)writing files... (59/122)writing files... (60/122)writing files... (61/122)writing files... (62/122)writing files... (63/122)writing files... (64/122)writing files... (65/122)writing files... (66/122)writing files... (67/122)writing files... (68/122)writing files... (69/122)writing files... (70/122)writing files... (71/122)writing files... (72/122)writing files... (73/122)writing files... (74/122)writing files... (75/122)writing files... (76/122)writing files... (77/122)writing files... (78/122)writing files... (79/122)writing files... (80/122)writing files... (81/122)writing files... (82/122)writing files... (83/122)writing files... (84/122)writing files... (85/122)writing files... (86/122)writing files... (87/122)writing files... (88/122)writing files... (89/122)writing files... (90/122)writing files... (91/122)writing files... (92/122)writing files... (93/122)writing files... (94/122)writing files... (95/122)writing files... (96/122)writing files... (97/122)writing files... (98/122)writing files... (99/122)writing files... (100/122)writing files... (101/122)writing files... (102/122)writing files... (103/122)writing files... (104/122)writing files... (105/122)writing files... (106/122)writing files... (107/122)writing files... (108/122)writing files... (109/122)writing files... (110/122)writing files... (111/122)writing files... (112/122)writing files... (113/122)writing files... (114/122)writing files... (115/122)writing files... (116/122)writing files... (117/122)writing files... (118/122)writing files... (119/122)writing files... (120/122)writing files... (121/122)writing files... (122/122)
## writing metadata..
## finished! files have been written to:
## model_data/temp/met_no_dl_20241115125120
Our SWAT+ setup runs on a daily timestep, so we need daily data:
daily_data_path <-
reanalysis3_daily(path = download_folder,
outpath = "model_data/temp/",
precision = 2)
Now with the help of SWATprepR
we can add this data to our SWAT+ setup. (excuse the long output)
## creating weather generator
## [1] "Working on station ID1:sta_x1_y11"
## [1] "Working on station ID2:sta_x1_y12"
## [1] "Working on station ID3:sta_x1_y13"
## [1] "Working on station ID4:sta_x1_y14"
## [1] "Working on station ID5:sta_x1_y15"
## [1] "Working on station ID6:sta_x1_y16"
## [1] "Working on station ID7:sta_x2_y2"
## [1] "Working on station ID8:sta_x2_y3"
## [1] "Working on station ID9:sta_x2_y4"
## [1] "Working on station ID10:sta_x2_y5"
## [1] "Working on station ID11:sta_x2_y6"
## [1] "Working on station ID12:sta_x2_y7"
## [1] "Working on station ID13:sta_x2_y8"
## [1] "Working on station ID14:sta_x2_y9"
## [1] "Working on station ID15:sta_x2_y10"
## [1] "Working on station ID16:sta_x2_y11"
## [1] "Working on station ID17:sta_x2_y12"
## [1] "Working on station ID18:sta_x2_y13"
## [1] "Working on station ID19:sta_x2_y14"
## [1] "Working on station ID20:sta_x2_y15"
## [1] "Working on station ID21:sta_x2_y16"
## [1] "Working on station ID22:sta_x2_y17"
## [1] "Working on station ID23:sta_x3_y1"
## [1] "Working on station ID24:sta_x3_y2"
## [1] "Working on station ID25:sta_x3_y3"
## [1] "Working on station ID26:sta_x3_y4"
## [1] "Working on station ID27:sta_x3_y5"
## [1] "Working on station ID28:sta_x3_y6"
## [1] "Working on station ID29:sta_x3_y7"
## [1] "Working on station ID30:sta_x3_y8"
## [1] "Working on station ID31:sta_x3_y9"
## [1] "Working on station ID32:sta_x3_y10"
## [1] "Working on station ID33:sta_x3_y11"
## [1] "Working on station ID34:sta_x3_y12"
## [1] "Working on station ID35:sta_x3_y13"
## [1] "Working on station ID36:sta_x3_y14"
## [1] "Working on station ID37:sta_x3_y15"
## [1] "Working on station ID38:sta_x3_y16"
## [1] "Working on station ID39:sta_x3_y17"
## [1] "Working on station ID40:sta_x4_y1"
## [1] "Working on station ID41:sta_x4_y2"
## [1] "Working on station ID42:sta_x4_y3"
## [1] "Working on station ID43:sta_x4_y4"
## [1] "Working on station ID44:sta_x4_y5"
## [1] "Working on station ID45:sta_x4_y6"
## [1] "Working on station ID46:sta_x4_y7"
## [1] "Working on station ID47:sta_x4_y8"
## [1] "Working on station ID48:sta_x4_y9"
## [1] "Working on station ID49:sta_x4_y10"
## [1] "Working on station ID50:sta_x4_y11"
## [1] "Working on station ID51:sta_x4_y12"
## [1] "Working on station ID52:sta_x4_y13"
## [1] "Working on station ID53:sta_x4_y14"
## [1] "Working on station ID54:sta_x4_y15"
## [1] "Working on station ID55:sta_x4_y16"
## [1] "Working on station ID56:sta_x4_y17"
## [1] "Working on station ID57:sta_x5_y1"
## [1] "Working on station ID58:sta_x5_y2"
## [1] "Working on station ID59:sta_x5_y3"
## [1] "Working on station ID60:sta_x5_y4"
## [1] "Working on station ID61:sta_x5_y5"
## [1] "Working on station ID62:sta_x5_y6"
## [1] "Working on station ID63:sta_x5_y7"
## [1] "Working on station ID64:sta_x5_y8"
## [1] "Working on station ID65:sta_x5_y9"
## [1] "Working on station ID66:sta_x5_y10"
## [1] "Working on station ID67:sta_x5_y11"
## [1] "Working on station ID68:sta_x5_y12"
## [1] "Working on station ID69:sta_x5_y13"
## [1] "Working on station ID70:sta_x5_y14"
## [1] "Working on station ID71:sta_x5_y15"
## [1] "Working on station ID72:sta_x5_y16"
## [1] "Working on station ID73:sta_x5_y17"
## [1] "Working on station ID74:sta_x6_y2"
## [1] "Working on station ID75:sta_x6_y3"
## [1] "Working on station ID76:sta_x6_y4"
## [1] "Working on station ID77:sta_x6_y5"
## [1] "Working on station ID78:sta_x6_y6"
## [1] "Working on station ID79:sta_x6_y7"
## [1] "Working on station ID80:sta_x6_y8"
## [1] "Working on station ID81:sta_x6_y9"
## [1] "Working on station ID82:sta_x6_y10"
## [1] "Working on station ID83:sta_x6_y11"
## [1] "Working on station ID84:sta_x6_y12"
## [1] "Working on station ID85:sta_x6_y13"
## [1] "Working on station ID86:sta_x6_y14"
## [1] "Working on station ID87:sta_x6_y15"
## [1] "Working on station ID88:sta_x6_y16"
## [1] "Working on station ID89:sta_x6_y17"
## [1] "Working on station ID90:sta_x7_y6"
## [1] "Working on station ID91:sta_x7_y7"
## [1] "Working on station ID92:sta_x7_y8"
## [1] "Working on station ID93:sta_x7_y9"
## [1] "Working on station ID94:sta_x7_y10"
## [1] "Working on station ID95:sta_x7_y11"
## [1] "Working on station ID96:sta_x7_y12"
## [1] "Working on station ID97:sta_x7_y13"
## [1] "Working on station ID98:sta_x7_y14"
## [1] "Working on station ID99:sta_x7_y15"
## [1] "Working on station ID100:sta_x7_y16"
## [1] "Working on station ID101:sta_x8_y7"
## [1] "Working on station ID102:sta_x8_y8"
## [1] "Working on station ID103:sta_x8_y9"
## [1] "Working on station ID104:sta_x8_y10"
## [1] "Working on station ID105:sta_x8_y11"
## [1] "Working on station ID106:sta_x8_y12"
## [1] "Working on station ID107:sta_x8_y13"
## [1] "Working on station ID108:sta_x8_y14"
## [1] "Working on station ID109:sta_x8_y15"
## [1] "Working on station ID110:sta_x9_y8"
## [1] "Working on station ID111:sta_x9_y9"
## [1] "Working on station ID112:sta_x9_y10"
## [1] "Working on station ID113:sta_x9_y11"
## [1] "Working on station ID114:sta_x9_y12"
## [1] "Working on station ID115:sta_x9_y13"
## [1] "Working on station ID116:sta_x9_y14"
## [1] "Working on station ID117:sta_x9_y15"
## [1] "Working on station ID118:sta_x10_y10"
## [1] "Working on station ID119:sta_x10_y11"
## [1] "Working on station ID120:sta_x10_y12"
## [1] "Working on station ID121:sta_x10_y13"
## [1] "Working on station ID1:sta_x1_y11"
## [1] "Working on station ID2:sta_x1_y12"
## [1] "Working on station ID3:sta_x1_y13"
## [1] "Working on station ID4:sta_x1_y14"
## [1] "Working on station ID5:sta_x1_y15"
## [1] "Working on station ID6:sta_x1_y16"
## [1] "Working on station ID7:sta_x2_y2"
## [1] "Working on station ID8:sta_x2_y3"
## [1] "Working on station ID9:sta_x2_y4"
## [1] "Working on station ID10:sta_x2_y5"
## [1] "Working on station ID11:sta_x2_y6"
## [1] "Working on station ID12:sta_x2_y7"
## [1] "Working on station ID13:sta_x2_y8"
## [1] "Working on station ID14:sta_x2_y9"
## [1] "Working on station ID15:sta_x2_y10"
## [1] "Working on station ID16:sta_x2_y11"
## [1] "Working on station ID17:sta_x2_y12"
## [1] "Working on station ID18:sta_x2_y13"
## [1] "Working on station ID19:sta_x2_y14"
## [1] "Working on station ID20:sta_x2_y15"
## [1] "Working on station ID21:sta_x2_y16"
## [1] "Working on station ID22:sta_x2_y17"
## [1] "Working on station ID23:sta_x3_y1"
## [1] "Working on station ID24:sta_x3_y2"
## [1] "Working on station ID25:sta_x3_y3"
## [1] "Working on station ID26:sta_x3_y4"
## [1] "Working on station ID27:sta_x3_y5"
## [1] "Working on station ID28:sta_x3_y6"
## [1] "Working on station ID29:sta_x3_y7"
## [1] "Working on station ID30:sta_x3_y8"
## [1] "Working on station ID31:sta_x3_y9"
## [1] "Working on station ID32:sta_x3_y10"
## [1] "Working on station ID33:sta_x3_y11"
## [1] "Working on station ID34:sta_x3_y12"
## [1] "Working on station ID35:sta_x3_y13"
## [1] "Working on station ID36:sta_x3_y14"
## [1] "Working on station ID37:sta_x3_y15"
## [1] "Working on station ID38:sta_x3_y16"
## [1] "Working on station ID39:sta_x3_y17"
## [1] "Working on station ID40:sta_x4_y1"
## [1] "Working on station ID41:sta_x4_y2"
## [1] "Working on station ID42:sta_x4_y3"
## [1] "Working on station ID43:sta_x4_y4"
## [1] "Working on station ID44:sta_x4_y5"
## [1] "Working on station ID45:sta_x4_y6"
## [1] "Working on station ID46:sta_x4_y7"
## [1] "Working on station ID47:sta_x4_y8"
## [1] "Working on station ID48:sta_x4_y9"
## [1] "Working on station ID49:sta_x4_y10"
## [1] "Working on station ID50:sta_x4_y11"
## [1] "Working on station ID51:sta_x4_y12"
## [1] "Working on station ID52:sta_x4_y13"
## [1] "Working on station ID53:sta_x4_y14"
## [1] "Working on station ID54:sta_x4_y15"
## [1] "Working on station ID55:sta_x4_y16"
## [1] "Working on station ID56:sta_x4_y17"
## [1] "Working on station ID57:sta_x5_y1"
## [1] "Working on station ID58:sta_x5_y2"
## [1] "Working on station ID59:sta_x5_y3"
## [1] "Working on station ID60:sta_x5_y4"
## [1] "Working on station ID61:sta_x5_y5"
## [1] "Working on station ID62:sta_x5_y6"
## [1] "Working on station ID63:sta_x5_y7"
## [1] "Working on station ID64:sta_x5_y8"
## [1] "Working on station ID65:sta_x5_y9"
## [1] "Working on station ID66:sta_x5_y10"
## [1] "Working on station ID67:sta_x5_y11"
## [1] "Working on station ID68:sta_x5_y12"
## [1] "Working on station ID69:sta_x5_y13"
## [1] "Working on station ID70:sta_x5_y14"
## [1] "Working on station ID71:sta_x5_y15"
## [1] "Working on station ID72:sta_x5_y16"
## [1] "Working on station ID73:sta_x5_y17"
## [1] "Working on station ID74:sta_x6_y2"
## [1] "Working on station ID75:sta_x6_y3"
## [1] "Working on station ID76:sta_x6_y4"
## [1] "Working on station ID77:sta_x6_y5"
## [1] "Working on station ID78:sta_x6_y6"
## [1] "Working on station ID79:sta_x6_y7"
## [1] "Working on station ID80:sta_x6_y8"
## [1] "Working on station ID81:sta_x6_y9"
## [1] "Working on station ID82:sta_x6_y10"
## [1] "Working on station ID83:sta_x6_y11"
## [1] "Working on station ID84:sta_x6_y12"
## [1] "Working on station ID85:sta_x6_y13"
## [1] "Working on station ID86:sta_x6_y14"
## [1] "Working on station ID87:sta_x6_y15"
## [1] "Working on station ID88:sta_x6_y16"
## [1] "Working on station ID89:sta_x6_y17"
## [1] "Working on station ID90:sta_x7_y6"
## [1] "Working on station ID91:sta_x7_y7"
## [1] "Working on station ID92:sta_x7_y8"
## [1] "Working on station ID93:sta_x7_y9"
## [1] "Working on station ID94:sta_x7_y10"
## [1] "Working on station ID95:sta_x7_y11"
## [1] "Working on station ID96:sta_x7_y12"
## [1] "Working on station ID97:sta_x7_y13"
## [1] "Working on station ID98:sta_x7_y14"
## [1] "Working on station ID99:sta_x7_y15"
## [1] "Working on station ID100:sta_x7_y16"
## [1] "Working on station ID101:sta_x8_y7"
## [1] "Working on station ID102:sta_x8_y8"
## [1] "Working on station ID103:sta_x8_y9"
## [1] "Working on station ID104:sta_x8_y10"
## [1] "Working on station ID105:sta_x8_y11"
## [1] "Working on station ID106:sta_x8_y12"
## [1] "Working on station ID107:sta_x8_y13"
## [1] "Working on station ID108:sta_x8_y14"
## [1] "Working on station ID109:sta_x8_y15"
## [1] "Working on station ID110:sta_x9_y8"
## [1] "Working on station ID111:sta_x9_y9"
## [1] "Working on station ID112:sta_x9_y10"
## [1] "Working on station ID113:sta_x9_y11"
## [1] "Working on station ID114:sta_x9_y12"
## [1] "Working on station ID115:sta_x9_y13"
## [1] "Working on station ID116:sta_x9_y14"
## [1] "Working on station ID117:sta_x9_y15"
## [1] "Working on station ID118:sta_x10_y10"
## [1] "Working on station ID119:sta_x10_y11"
## [1] "Working on station ID120:sta_x10_y12"
## [1] "Working on station ID121:sta_x10_y13"
## [1] "weather-wgn.cli file was successfully written."
## [1] "weather-sta.cli file was successfully written."
## [1] "pcp files were successfully written."
## [1] "tmp files were successfully written."
## [1] "slr files were successfully written."
## [1] "hmd files were successfully written."
## [1] "wnd files were successfully written."
## [1] "Updated weather stations in aquifer.con file."
## [1] "Updated weather stations in hru.con file."
## [1] "Updated weather stations in reservoir.con file."
## [1] "Updated weather stations in chandeg.con file."
## [1] "Updated weather stations in rout_unit.con file."
## [1] "time.sim file was successfully written."
## [1] "Climate data were successfully written in model_data/temp/"
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 6473102 345.8 9811277 524.0 9811277 524.0
## Vcells 13621862 104.0 926881357 7071.6 1810315149 13811.7
These three functions can be consolidated into one workflow with the following function:
swat_weather_input_chain(
area = "model_data/input/shape/cs10_basin.shp",
swat_setup = "model_data/temp/",
directory = "model_data/temp/",
from = "2013-01-01 00:00:00",
to = "2022-12-31 00:00:00"
)
Note: as of right now we have unit conversion problems with solar radiation, so we are using our old weather station for SLR.
Note 2: we are only running with a single metno station.
The current state of the weather setup can be seen in panel A :