wcswatin

R-CMD-check License: GPL v3 Codecov test coverage

Overview

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).

Key Features

Installation

Install the development version from GitHub:

# install.packages("devtools")
devtools::install_github("reginalexavier/wcswatin")

Quick Start

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"
)

Workflow Overview

Conceptual workflow of the wcswatin package

Conceptual workflow of the wcswatin package

Main Functions

Data Input & Inspection

Raster/NetCDF Processing

Station Data Processing

SWAT-Specific Functions

Data Analysis & Utilities

Data Requirements

The 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.

Supported Data Sources

Documentation

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.

Getting Help

Citation

If you use wcswatin in your research, please cite:

Exavier R, Kawakubo F, Zeilhofer P (2026). wcswatin: Weather & Climate SWAT INput (WCSWATIN). R package version 0.1.1, https://github.com/reginalexavier/wcswatin

@software{
  title = {wcswatin: Weather & Climate SWAT INput (WCSWATIN)},
  author = {Réginal Exavier and Fernando Shinji Kawakubo and Peter Zeilhofer},
  year = {2026},
  note = {R package version 0.1.1},
  url = {https://github.com/reginalexavier/wcswatin},
}

License

GPL (>= 3)

Acknowledgments

This project is funded by the Critical Ecosystem Partnership Fund (CEPF).