Skip to contents

Loading

Loading weather data from templates could by done with the same load_template function as loading calibration data. However, data should for each meteorological station should be separated into different sheets. All data are loaded into nested list of list format, which could be used to run other functions.

library(SWATprepR)
temp_path <- system.file("extdata", "weather_data.xlsx", package = "SWATprepR")
met_lst <- load_template(temp_path, 3035)
## [1] "Loading data from template."
## [1] "Reading station ID9 data."
## [1] "Reading station ID3 data."
## [1] "Reading station ID10 data."
## [1] "Reading station ID11 data."
## [1] "Reading station ID12 data."
## [1] "Reading station ID13 data."
## [1] "Reading station ID1 data."
## [1] "Reading station ID8 data."
## [1] "Reading station ID2 data."
## [1] "Reading station ID4 data."
## [1] "Reading station ID5 data."
## [1] "Reading station ID6 data."
## [1] "Reading station ID7 data."
## [1] "Loading of data is finished."

Plotting

Function plot_weather allows to plot meteorological data by selecting variables, time period and way of aggregating them. Time period should be selected one of “second”,“minute”,“hour”,“day”, “week”, “month”, or “year”. While aggregation functions could be selected from dplyr package summarise function available options. All provided meteorological stations` data will be displayed. Relevant stations could be selected interactively on generated plot by selecting or deselecting stations in legend. show all or hide all buttons allows to select or deselect all stations. Press Shift + left mouse bottom and drag to zoom specific area, or press left mouse button and grad to explore plot, or press Shift + left and right mouse buttons at same time to zoom out. Toolbox in top right corner of figure could be used for these operations as well.

plot_weather(met_lst, "PCP", "month", "sum")

Another function plot_weather_compare allows comparing two meteorological datasets of same time scale. Potential examples of its usage could be different. One could be comparing different datasets in order to decide, which one should be used in modeling. Another example might be to examine how cleaning operations improved original data. Moreover, this function could be helpful in examining climate data downscaling or bias-correction results comparing them with observed data. Same options for aggregation as in plot_weather_compare function can be used.

temp_path2 <- system.file("extdata", "weather_data_raw.xlsx", package = "SWATprepR")
met_lst2 <- load_template(temp_path2, 3035)
## [1] "Loading data from template."
## [1] "Reading station ID9 data."
## [1] "Reading station ID3 data."
## [1] "Reading station ID10 data."
## [1] "Reading station ID11 data."
## [1] "Reading station ID12 data."
## [1] "Reading station ID13 data."
## [1] "Reading station ID1 data."
## [1] "Reading station ID8 data."
## [1] "Reading station ID2 data."
## [1] "Reading station ID4 data."
## [1] "Reading station ID5 data."
## [1] "Reading station ID6 data."
## [1] "Reading station ID7 data."
## [1] "Loading of data is finished."
plot_weather_compare(met_lst, met_lst2, "PCP", "month", "mean", "clean", "raw")

Interpolating

In cases where too few stations are available in a selected catchment or huge data gaps exists, interpolation could be applied between stations to cover those gaps. Example below loads basin boundary to be used in the modeling and adds with already loaded meteorological stations to the map. It could be seen that basin has just one station inside it. However, in this case station has only very few observations with huge gaps. Therefore, it is highly recommended in such cases collect data from nearby stations. These data can help to fill gaps and also could be used to generate spatial distribution for variables, which could be employed to propagate time series data for any number of virtual stations. Therefore, any number of meteorological stations could be prepared with full time series data.

library(mapview)
library(sf)
library(dplyr)
basin_path <- system.file("extdata", "GIS/basin.shp", package = "SWATprepR")
basin <-st_transform(st_read(basin_path), 4326) %>% 
  mutate(NAME = "Basin")
## Reading layer `basin' from data source 
##   `C:\Users\laptop\AppData\Local\R\win-library\4.2\SWATprepR\extdata\GIS\basin.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 4898668 ymin: 3304508 xmax: 4915266 ymax: 3319644
## Projected CRS: ETRS89-extended / LAEA Europe
stations <- st_transform(met_lst$stations, 4326)
mapview(stations) + mapview(basin)

For such need function interpolate was prepared. It allows to do interpolation between data in presented stations. Function’s grid_spacing parameter defines, how dense spatial interpolation points will be. Interpolate function creates virtual stations. Data needed for this function are loaded stations observations, basin boundary shape path, DEM raster path. It is important, that GIS data used in function would be of same coordinate system.

Function parameters are grid_spacing, p_vector - character vector representing weather variables to interpolate (optional, default all variables selected c(“PCP”, “SLR”, “RELHUM”, “WNDSPD”, “TMP_MAX”, “TMP_MIN” ), idw_exponent - numeric value for exponent parameter to be used in interpolation (optional, default value is 2). The final results of this function will be in the nested list format, which is used with other functions (after loading weather data from template).

DEM_path <- system.file("extdata", "GIS/DEM.tif", package = "SWATprepR")
met_lst_int <- SWATprepR::interpolate(met_lst, basin_path, DEM_path, 2000) 

WGN input function

Weather generator inputs also can be directly prepared with prepare_wgn function from this package.

wgn <- prepare_wgn(met_lst, 
                   TMP_MAX = met_lst$data$ID10$TMP_MAX, 
                   TMP_MIN = met_lst$data$ID10$TMP_MIN, 
                   PCP = met_lst$data$ID9$PCP, 
                   RELHUM = met_lst$data$ID9$RELHUM, 
                   WNDSPD = met_lst$data$ID10$WNDSPD, 
                   MAXHHR = met_lst$data$ID11$MAXHHR, 
                   SLR = met_lst$data$ID9$SLR)
write.csv(wgn$wgn_st, "wgn_st.csv", row.names = FALSE, quote = FALSE)
write.csv(wgn$wgn_data, "wgn_data.csv", row.names = FALSE, quote = FALSE)

Writing

SWAT+ input .sqlite database

add_weather function can be used to write all weather files into project’s folder and update tables in project’s sqlite database with weather, weather generator and relation to weather station information.

##Path to .sqlite
db_path <- "./output/test/project.sqlite"
add_weather(db_path, met_lst, wgn)

SWAT+ input text files

prepare_climate function can be used to prepare/update SWAT+ model input text files for weather data. The function prepares following files:

  • Weather time series input files: .pcp, .slr, .hmd, .tmp, .wnd;
  • Reference files for weather files: wnd.cli, tmp.cli, pcp.cli, slr.cli, hmd.cli;
  • Weather generator files: weather-sta.cli, weather-wgn.cli.

The function also updates aquifer.con, chandeg.con, hru.con, reservoir.con, rout_unit.con and time.sim SWAT+ model input files. If these files have to be updated, they should be is the same directory, which is provided for the function. Following line provide an example, how it could be applied.

prepare_climate(met_lst, "txtinout", "1991-01-01", "2020-12-31")