Read Output
This section explains how to read output data generated from SWAT+
simulations.
It covers accessing results from both standard simulations and Sobol-based sensitivity analyses.
Read Time Series Data
A standard SWAT+
simulation generates TXT files with time series columns: day
, mon
, and yr
for day, month, and year, respectively.
The following method creates a time series DataFrame
that includes a new date
column with datetime.date
objects and save the resulting DataFrame to a JSON file.
import pySWATPlus
output = pySWATPlus.DataManager().simulated_timeseries_df(
data_file=r"C:\Users\Username\custom_folder\channel_sd_mon.txt",
has_units=True,
begin_date='01-Jun-2011',
end_date='01-Jun-2013',
ref_day=15,
apply_filter={'name': ['cha561']},
usecols=['name', 'flo_out'],
json_file=r"C:\Users\Username\output_folder\tmp.json"
)
print(output)
Read Sensitivity Simulation Data
The sensitivity analysis generates a file called sensitivity_simulation.json
within the simulation folder.
This JSON file stores all information necessary for Sobol simulation analysis, including:
problem
: the Sobol problem definitionsample
: the generated sample arraysimulation
: simulation outputs stored as JSON strings
To retrieve and process this data for further analysis:
import json
import numpy
import pandas
import io
# Path to the JSON file generated by the Sobol interface
json_file = r"C:\Users\Username\simulation_folder\sensitivity_simulation_sobol.json"
# Load the JSON file
with open(json_file, 'r') as input_json:
sensitivity_dict = json.load(input_json)
# Retrieve the Sobol problem definition
problem_dict = sensitivity_dict['problem']
# Retrieve the Sobol sample array
sample_array = numpy.array(sensitivity_dict['sample'])
# Retrieve targeted DataFrames from sensitivity simulations
simulation_dict = sensitivity_dict['simulation']
df_name = 'channel_sd_mon_df' # Example output
df_dict = {}
for sim_key, sim_val in simulation_dict.items():
# Convert JSON string to pandas DataFrame
df = pandas.read_json(
path_or_buf=io.StringIO(sim_val[df_name])
)
# Ensure 'date' column is in datetime.date format
df['date'] = df['date'].dt.date
df_dict[sim_key] = df