Provides workflows to prepare weather and climate time series from
gridded and station data for 'SWAT' ('Soil and Water Assessment Tool').
Supports data extraction, aggregation, interpolation, quality control, unit
conversion, and export of per-location model input files. For the underlying
model, see Arnold et al. (1998) "Large Area Hydrologic Modeling and
Assessment Part I: Model Development"
wcswatin provides workflows to prepare weather and climate time series from gridded and station data for the Soil and Water Assessment Tool (SWAT). It supports data extraction, aggregation, interpolation, quality control, unit conversion, and export of per-location model input files.
The package provides two complementary workflows:
Developed with funding from the Critical Ecosystem Partnership Fund (CEPF).
raster_info() and var_names()datacube_aggregation()future-based parallel extraction through cube2table()Install the released version from CRAN:
install.packages("wcswatin")
To install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("reginalexavier/wcswatin")
library(wcswatin)
# Inspect a downloaded NetCDF file before choosing the processing route
nc_file <- system.file(
"extdata/nc_data/hourly_multi_2days_2025.nc",
package = "wcswatin"
)
raster_info(nc_file)
var_names(nc_file)
# Load a raster cube and extract values at reference stations
daily_nc <- system.file(
"extdata/nc_data/daily_2m_temperature_daily_maximum_2025.nc",
package = "wcswatin"
)
stations_file <- system.file(
"extdata/pcp_stations/pcp.txt",
package = "wcswatin"
)
station_values <- tbl_from_references(
raster_file = input_raster(daily_nc),
ref_points = stations_file,
prefix_colname = "t2m"
)
head(station_values)
# Interpolate daily station tables to target points
interpolated_points <- ts_to_point(
my_folder = "path/to/station_files",
targeted_points_path = "path/to/centroids.shp",
poly_degree = 2
)
ts_point_to_files(
points_list = interpolated_points,
output_folder = "path/to/swat_pcp",
file_prefix = "pcp"
)
View the complete workflow in full resolution
input_raster(): Load NetCDF or GeoTIFF files as SpatRaster objectsinput_table(): Load tabular data with validationinput_vector(): Load spatial vector data (shapefiles, etc.)raster_info(): Summarize raster variables, units, dates, and
dimensionsvar_names(): List available variables in NetCDF filesstudy_area_records(): Extract grid points within watershed
boundariesmain_input_var(): Create SWAT main files for gridded variablescube2table(): Convert raster data cube to tabular formatlayervalues2pixel(): Write one time series for each grid celldatacube_aggregation(): Aggregate or select daily layers before
extractiondaily_aggregation(): Aggregate hourly SWAT-style files to daily
filestbl_from_references(): Extract raster values at reference pointsfiles_to_table(): Consolidate multiple station files into a single
tabletable_to_files(): Split consolidated data back into individual filesfill_gap(): Fill missing data using correlation methodspoint_to_daily(): Import and organize daily station datasave_daily_tbl(): Save daily tables in SWAT formatts_to_point(): Trend surface interpolation to specific points
(watershed centroids)ts_point_to_files(): Save ts_to_point() outputs as SWAT-style
filests_to_area(): Trend surface interpolation to create continuous
raster surfacesvar_main_creator(): Generate SWAT input metadata tablesmain_input_var(): Create main variable input tables for SWATrh_calculator(): Calculate relative humidity from other variableswindspeed_calculator(): Calculate wind speed from componentscount_na(): Check data completeness and missing valuessummary_table(): Generate statistical summariessummary_plot(): Visualize data distributionsunit_converter(): Convert between measurement unitsThe package works with spatial data in WGS 84 geographic coordinate
system (EPSG:4326), which is the standard format for most climate
datasets. Reference point tables should include NAME, LAT, and LON
columns. When possible, vector reference points are projected to the
raster CRS before extraction.
For NetCDF inputs, inspect time metadata before processing. Hourly files
can be processed through cube2table() and later aggregated with
daily_aggregation(), while daily NetCDF products can often be
extracted directly. For accumulated products timestamped at a specific
hour, datacube_aggregation(mode = "value_at_hour") and
daily_aggregation(mode = "value_at_hour") make that convention
explicit.
NetCDF files can be downloaded with any CDS workflow. The optional
cds-downloader CLI
can help create repeatable CDS download requests, but it is not required
by wcswatin.
To obtain the current citation in R, run:
citation("wcswatin")
If you use wcswatin in your research, please cite:
Exavier R, Kawakubo F, Zeilhofer P (2026). wcswatin: Weather and Climate Inputs for ‘SWAT’. doi:10.32614/CRAN.package.wcswatin. R package version 0.2.0, https://CRAN.R-project.org/package=wcswatin.
@Manual{
title = {wcswatin: Weather and Climate Inputs for 'SWAT'},
author = {Réginal Exavier and Fernando Shinji Kawakubo and Peter Zeilhofer},
year = {2026},
note = {R package version 0.2.0},
url = {https://CRAN.R-project.org/package=wcswatin},
doi = {10.32614/CRAN.package.wcswatin},
}
GPL (>= 3)
This project is funded by the Critical Ecosystem Partnership Fund (CEPF).