@@ -708,11 +708,11 @@ reanalysis3_daily <- function(path, outpath = NULL, verbose = FALSE, precision =
708
708
stations <- files [which(grepl(x = files ,pattern = " metadata.csv" ) == FALSE )]
709
709
metadata <- files [which(grepl(x = files ,pattern = " metadata.csv" ))]
710
710
711
- # Custom funciton to convert the hourly data to daily data
711
+ # Custom function to convert the hourly data to daily data
712
712
# certain considerations need to be made when summing or averaging.
713
713
hourly2daily <- function (data ){
714
714
# These will potentially need to be expanded
715
- sum_these <- " precipitation_amount"
715
+ sum_these <- c( " precipitation_amount" , " integral_of_surface_downwelling_shortwave_flux_in_air_wrt_time " )
716
716
max_these <- " air_temperature_2m"
717
717
min_these <- " air_temperature_2m"
718
718
@@ -739,7 +739,7 @@ reanalysis3_daily <- function(path, outpath = NULL, verbose = FALSE, precision =
739
739
summarise(across(all_of(min_data_cols ), min )) %> %
740
740
dplyr :: rename(min_temp = air_temperature_2m )
741
741
742
- # -1 to get rid of the date column and round by precisision
742
+ # -1 to get rid of the date column and round by precision
743
743
full_df <-
744
744
cbind(
745
745
daily_data [1 ],
@@ -804,15 +804,14 @@ reanalysis3_daily <- function(path, outpath = NULL, verbose = FALSE, precision =
804
804
# ' function to complete successfully when not writing to the SQL database
805
805
# '
806
806
# ' @param path path to daily data provided by `reanalysis3_daily()`
807
- # ' @param start optional parameter to define start date oftimeseries
808
- # ' @param end optional parameter to define end date of timeseries
807
+ # ' @param start optional parameter to define start date of time series
808
+ # ' @param end optional parameter to define end date of time series
809
809
# ' @param sqlite_path path to your SWAT+ sqlite file (only needed if you wish to
810
810
# ' update your database). Warning: start and end parameters will be ignored in this case (SWATprepR limitation)
811
- # ' @param outpath path to directory where files will be written. If left blank,
812
- # ' this will be your normal path
813
811
# ' @param verbose print status?
814
812
# ' @param write_wgn calculate and write the weather generator? defaults to true. (for now just based on station #1 (bottom left))
815
813
# ' @param swat_setup path to your SWAT+ setup. (Required!)
814
+ # ' @param backup (logical, defaults to true) creates a backup of your swat folder before modification
816
815
# '
817
816
# ' @return path to generated files.
818
817
# ' @export
@@ -824,17 +823,27 @@ reanalysis3_daily <- function(path, outpath = NULL, verbose = FALSE, precision =
824
823
# ' @importFrom readr read_csv
825
824
# ' @importFrom stringr str_split str_remove
826
825
# ' @importFrom writexl write_xlsx
826
+ # ' @importFrom crayon green italic
827
827
reanalysis3_swatinput <-
828
828
function (path ,
829
829
swat_setup ,
830
- outpath = NULL ,
831
830
write_wgn = TRUE ,
832
831
start = NA ,
833
832
end = NA ,
834
833
sqlite_path = NULL ,
835
- verbose = FALSE ) {
834
+ verbose = FALSE ,
835
+ backup = TRUE ) {
836
+
837
+ # create a backup of the SWAT+ setup
838
+ if (backup ){
839
+ backuppath <- paste0(dirname(swat_setup ), " /miljotools_swat_backup" )
840
+ dir.create(backuppath , showWarnings = F )
841
+ file.copy(swat_setup , backuppath , recursive = T )
842
+ if (verbose ){cat(green(italic(" backing up SWAT directory in" )), underline(backuppath ), " \n " )}
843
+ }
844
+
836
845
837
- # Check that prepr is installed
846
+ # Check that SWATprepR is installed
838
847
if (" SWATprepR" %in% utils :: installed.packages()) {
839
848
# if it is installed, check if it is loaded
840
849
if (" SWATprepR" %in% (.packages())) {
@@ -843,11 +852,9 @@ reanalysis3_swatinput <-
843
852
cat(green(italic(" SWATprepR already loaded!\n " )))
844
853
}
845
854
} else {
855
+ if (verbose ){cat(green(italic(" loading SWATprepR\n " )))}
846
856
# it is not loaded, then load it
847
- # requireNamespace("SWATprepR")
848
- if (verbose ) {
849
- cat(green(italic(" loading SWATprepR\n " )))
850
- }
857
+ requireNamespace(" SWATprepR" )
851
858
}
852
859
# it is not installed? then require the user to install it.
853
860
} else {
@@ -859,20 +866,17 @@ reanalysis3_swatinput <-
859
866
860
867
# get the file paths and differentiate between metadata and data
861
868
files <- list.files(path , full.names = T )
862
- files_short <- list.files(path , full.names = F )
863
- stations_short <- files_short [which(grepl(x = files ,pattern = " metadata.csv" ) == FALSE )]
864
869
stations <- files [which(grepl(x = files ,pattern = " metadata.csv" ) == FALSE )]
865
870
866
871
# load the metadata
867
872
metadata <- readr :: read_csv(paste0(path , " /metadata.csv" ), show_col_types = F ) %> %
868
873
as.data.frame()
869
874
metadata $ Source = " "
870
875
871
- # Convert to prepR format
872
- # in order to use PrepR to create the SWAT weather files, we need to create
873
- # an excel template file in the correct format.
874
- # This custom read function will read and process the csv files to be in the
875
- # format we want them in
876
+ # Convert to prepR format: in order to use PrepR to create the SWAT weather
877
+ # files, we need to create an excel template file in the correct format. This
878
+ # custom read function will read and process the csv files to be in the format
879
+ # we want them in
876
880
custom_read <- function (filepath ){
877
881
# read
878
882
df <- readr :: read_csv(filepath , show_col_types = F )
@@ -924,23 +928,14 @@ reanalysis3_swatinput <-
924
928
my_data <- purrr :: map(stations , custom_read )
925
929
926
930
if (verbose ){cat(green(italic((" converting data into SWATprepR format...\n " ))))}
927
- # add station names
931
+ # add station names
928
932
names(my_data ) <- paste0(" ID" , c(1 : length(my_data )))
929
933
# append the metadata to the front
930
934
stations_list <- append(list (metadata ), my_data )
931
935
# and set the name of the metadata (prepR format)
932
936
names(stations_list )[1 ] <- " Stations"
933
937
934
- # parsing and/or generating outpath and folder/file names
935
- parts <- path %> % stringr :: str_split(" /" ) %> % unlist()
936
- folder <- parts [which(nchar(parts ) > 1 )] %> % dplyr :: last()
937
- # time and date should always be the 4th element.
938
- tod <- folder %> % stringr :: str_split(" _" ) %> % unlist() %> % dplyr :: nth(4 )
939
- if (outpath %> % is.null()){
940
- outpath <- path %> % stringr :: str_remove(paste0(" /" , folder , " /" ))
941
- }
942
-
943
- # recreating metadata format for Svatools
938
+ # recreating metadata format for SWATprepR
944
939
metadata_spat <-
945
940
sf :: st_as_sf(dplyr :: tibble(metadata ),
946
941
coords = c(" Long" ,
@@ -952,7 +947,7 @@ reanalysis3_swatinput <-
952
947
953
948
# recreating data format for SWATprepR
954
949
955
- # this function splits the dataframe into indiviudal lists, and appends
950
+ # this function splits the dataframe into individual lists, and appends
956
951
# the date column to each one in tibble form. The column name for the variable
957
952
# at hand is not assigned here because I could not find a way to do it. It
958
953
# is done in a later step with for loops
@@ -1012,10 +1007,10 @@ reanalysis3_swatinput <-
1012
1007
SLR = meteo_lst $ data $ ID1 $ SLR
1013
1008
)
1014
1009
# writing the weather gen
1015
- if (verbose ){cat(green(italic(" writing weather generator to file in '" , outpath , " '\n " )))}
1010
+ if (verbose ){cat(green(italic(" writing weather generator to file in '" , swat_setup , " '\n " )))}
1016
1011
1017
- write.csv(wgn $ wgn_st , paste0(outpath ," /wgn_st.csv" ), row.names = FALSE , quote = FALSE )
1018
- write.csv(wgn $ wgn_data , paste0(outpath ," /wgn_data.csv" ), row.names = FALSE , quote = FALSE )
1012
+ write.csv(wgn $ wgn_st , paste0(swat_setup ," /wgn_st.csv" ), row.names = FALSE , quote = FALSE )
1013
+ write.csv(wgn $ wgn_data , paste0(swat_setup ," /wgn_data.csv" ), row.names = FALSE , quote = FALSE )
1019
1014
}
1020
1015
1021
1016
# writing
@@ -1059,6 +1054,7 @@ reanalysis3_swatinput <-
1059
1054
# '
1060
1055
# ' @param area The catchment area to retrieve data for. (must be a shapefile)
1061
1056
# ' @param swat_setup The path to your SWAT+ setup (input files, aka TxtInOut)
1057
+ # ' @param grid_resolution (integer) desired resolution of downloaded grid in kilometers.
1062
1058
# ' @param directory directory to download and process data in
1063
1059
# ' @param from start of the to-be-dowloaded timeseries (ie. and min: "2012-09-01
1064
1060
# ' 10:00:00")
@@ -1080,6 +1076,7 @@ reanalysis3_swatinput <-
1080
1076
swat_weather_input_chain <-
1081
1077
function (area ,
1082
1078
swat_setup ,
1079
+ grid_resolution = 1 ,
1083
1080
directory = NULL ,
1084
1081
from = NULL ,
1085
1082
to = NULL ,
@@ -1095,7 +1092,8 @@ swat_weather_input_chain <-
1095
1092
fromdate = from ,
1096
1093
todate = to ,
1097
1094
area_buffer = area_buffer ,
1098
- preview = verbose
1095
+ preview = verbose ,
1096
+ grid_resolution = grid_resolution
1099
1097
)
1100
1098
1101
1099
path2 <- reanalysis3_daily(
@@ -1108,7 +1106,6 @@ swat_weather_input_chain <-
1108
1106
1109
1107
path3 <- reanalysis3_swatinput(
1110
1108
path = path2 ,
1111
- outpath = directory ,
1112
1109
swat_setup = swat_setup ,
1113
1110
write_wgn = write_wgn ,
1114
1111
sqlite_path = sqlite_path ,
0 commit comments