Skip to content

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. To create a time series DataFrame that includes a new date column with datetime.date objects. Additionally, the method can optionally save the resulting DataFrame to a JSON file. This is controlled by the parameters save_df and json_file: setting save_df=True enables the saving process, and json_file specifies the path where the DataFrame will be written.

output = pySWATPlus.SensitivityAnalyzer().simulated_timeseries_df(
    data_file=r"C:\Users\Username\simulation_folder_2\channel_sd_mon.txt",
    has_units=True,
    start_date='2014-06-01',
    apply_filter={'yr': [2014, 2015], 'gis_id': [561]},
    usecols=['gis_id', 'flo_out'],
    save_df=True,
    json_file=r"C:\Users\Username\output_folder\tmp.json"
)

Read Sensitivity Simulation Data

The high-level Sobol-Based Simulation Interface automates the sensitivity analysis workflow using Sobol samples. By default, it generates a file named sensitivity_simulation_sobol.json in the simulation folder. This JSON file contains all the information needed to analyze Sobol simulations, including:

  • problem: the Sobol problem definition
  • sample: the generated sample array
  • simulation: simulation outputs stored as JSON strings

To retrieve and process this data for further analysis:

import json
import numpy as np
import pandas as pd
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 = np.array(sensitivity_dict['sample'])

# Retrieve targeted simulation DataFrames
simulation_dict = sensitivity_dict['simulation']
df_name = 'channel_sd_yr_df'  # Example output
df_dict = {}

for sim_key, sim_val in simulation_dict.items():
    # Convert JSON string to pandas DataFrame
    df = pd.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

Notes: df_dict contains a DataFrame for each simulation key, which can be easily analyzed or plotted. Replace 'channel_sd_yr_df' with any other output DataFrame as needed.