Skip to content

Data Analysis

This section explains how to analysis data generated by different interfaces.

Simulation Data

This section analyzes the simulation data generated using the run_swat method.

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

Model Performance

Model performance can be evaluated by comparing simulated outputs with observed data, using selected indicators available in the property indicator_names.

output = pySWATPlus.PerformanceMetrics().indicator_from_file(
    sim_file=r"C:\Users\Username\custom_folder\channel_sd_day.txt",
    sim_col='flo_out',
    extract_sim={
        'has_units': True,
        'apply_filter': {'name': ['cha561']}
    },
    obs_file=r"C:\Users\Username\observed_folder\discharge_daily.csv",
    date_format='%Y-%m-%d',
    obs_col='discharge',
    indicators=['NSE', 'KGE', 'RMSE']
)

Statistics from Daily Time Series

Monthly and yearly statistics such as maximum, minimum, mean, and standard deviation derived from daily time series data help summarize and interpret simulation variability over time. The following interface computes monthly and yearly statistical summaries for a Hydrological Response Unit (HRU) based on daily simulated flow discharge data. These metrics provide insight into seasonal patterns, flow extremes, and overall hydrological stability.

output = pySWATPlus.DataManager().hru_stats_from_daily_simulation(
    sim_file=r"C:\Users\Username\custom_folder\channel_sd_day.txt",
    has_units=True,
    gis_id=561,
    sim_col='flo_out',
    output_dir=r"C:\Users\Username\output_dir"
)

Sensitivity Simulation Scenarios

This section analyzes the time series scenarios generated from the sensitivity analysis based on sampled parameters.

Read Time Series Scenarios

The sensitivity analysis performed using the simulation_by_sample_parameters method generates a file named sensitivity_simulation.json. This JSON file contains all the information required for sensitivity analysis, including:

  • problem: Problem definition dictionary
  • sample: List of generated samples
  • simulation: Simulated DataFrame corresponding to each sample

To retrieve the selected time series DataFrame for all scenarios, use:

output = pySWATPlus.DataManager().read_sensitive_dfs(
    sensim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
    df_name='channel_sd_mon_df',
    add_problem=True,
    add_sample=True
)

Scenario Performance

For a selected DataFrame, the performance of all sensitivity scenarios can be assessed by comparing simulated outputs with observed data, using selected indicators available in the property indicator_names.

output = pySWATPlus.SensitivityAnalyzer().scenario_indicators(
    sensim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
    df_name='channel_sd_mon_df',
    sim_col='flo_out',
    obs_file=r"C:\Users\Username\observed_folder\discharge_monthly.csv",
    date_format='%Y-%m-%d',
    obs_col='discharge',
    indicators=['NSE', 'MSE'],
    json_file=r"C:\Users\Username\data_analysis\performance_metrics.json"
)