Skip to main content

Python library used to calculate reservoir water balance.

Project description

tjwb

PyPI - Version

tjwb is a Python library designed for water balance management in reservoirs. The library provides methods to calculate inflow and outflow speeds of various components, such as pumps, box culverts, and valve overflows, based on water level and configuration...

Overview

Subsequent Capacity = Previous Capacity + (Inflow Speed * ΔT) - (Outflow Speed * ΔT)

Where:

  • Subsequent Capacity: The capacity of the reservoir after the current time step.
  • Previous Capacity: The capacity of the reservoir before the current time step.
  • Inflow Speed: The rate at which water is entering the reservoir.
  • Outflow Speed: The rate at which water is leaving the reservoir.
  • ΔT: The time step or interval over which the inflow and outflow are measured.

If the difference between the subsequent capacity and the previous capacity is zero, then the inflow speed and outflow speed are equal:

Subsequent Capacity - Previous Capacity = 0

In this case:

Inflow Speed = Outflow Speed

If the difference between the subsequent capacity and the previous capacity is negative, then the outflow speed is greater than the inflow speed:

Subsequent Capacity - Previous Capacity < 0

In this case:

Outflow Speed > Inflow Speed

Conversely, if the difference between the subsequent capacity and the previous capacity is positive, then the inflow speed is greater than the outflow speed:

Subsequent Capacity - Previous Capacity > 0

In this case:

Inflow Speed > Outflow Speed

Installation

To install the library, use pip:

pip install tjwb

Usage

Basic Example

import pandas as pd
from tjwb import (calculate, RequiredColumnsName, WaterLevelCapacityMappingColumnsName, PumpConfig, ValveOverflowConfig,
                  BoxCulvertConfig)

# Prepare your data
df = pd.DataFrame({
    'Datetime': ['2024-08-28 10:00:00', '2024-08-28 10:05:00'],
    'Water Level': [5.0, 5.5],
    'Pump.Pump-1': [1.0, 1.5],
    'BoxCulvert.BoxCulvert-1': [0.5, 0.7],
    'ValveOverflow.ValveOverflow-1': [0.3, 0.4],
    'ValveOverflow.ValveOverflow-2': [0.3, 0.4],
    'ValveOverflow.ValveOverflow-3': [0.3, 0.4],
})

water_level_capacity_mapping_df = pd.DataFrame({
    'Water Level': [5.0, 5.5],
    'Capacity': [1000, 1500]
})

# Define component configurations
pump_configs = [PumpConfig(column_name_prefix='Pump')]
box_culvert_configs = [BoxCulvertConfig(column_name_prefix='BoxCulvert', elevation=2.0, height=1.0)]
valve_overflow_configs = [ValveOverflowConfig(column_name_prefix='ValveOverflow', elevation=1.5, height=0.5)]

# Calculate results
result = calculate(
    _df=df,
    _water_level_capacity_mapping_df=water_level_capacity_mapping_df,
    water_level_capacity_mapping_columns_name=WaterLevelCapacityMappingColumnsName(),
    required_columns_name=RequiredColumnsName(),
    pump_configs=pump_configs,
    box_culvert_configs=box_culvert_configs,
    valve_overflow_configs=valve_overflow_configs
)

# Convert results to DataFrame
result_df = result.to_dataframe()
print(result_df)

Main Classes and Functions

  • calculate: The main function for calculating the inflow and outflow speeds based on the given configurations.
  • TJWBResult: Holds the calculated results, including datetime, inflow speed, outflow speed, and outflow speeds for each component.
  • RequiredColumnsName: Configuration for the required column names in the input DataFrame.
  • WaterLevelCapacityMappingColumnsName: Configuration for the column names in the water level-capacity mapping DataFrame.
  • Component Configurations:
    • PumpConfig
    • BoxCulvertConfig
    • ValveOverflowConfig

Error Handling

The tjwb library includes various validation steps to ensure that the inputs are correct. Below are the common scenarios where errors might be raised:

  • Invalid DataFrame Structure:

    • If the main DataFrame (_df) or the Water Level Capacity Mapping DataFrame (_water_level_capacity_mapping_df) contains missing required columns, a ValueError will be raised.
    • If the Water Level Capacity Mapping DataFrame contains non-numeric data in the water_level or capacity columns, a TypeError will be raised.
    • If any required column in the main DataFrame contains null values, a ValueError will be raised.
    • If the Datetime column cannot be converted to a valid datetime format, a ValueError will be raised.
  • Component Configuration Errors:

    • If the column_name_prefix of any component contains the character '.', a ValueError will be raised, as this character is not allowed in column name prefixes.
    • If there are duplicate column_name_prefix values among the configured components, a ValueError will be raised.
    • If a column referenced in the component configuration does not exist or is not of numeric type, a TypeError will be raised.
  • Invalid Water Level to Capacity Mapping:

    • If there is a mismatch between the water levels in the main DataFrame and the Water Level Capacity Mapping DataFrame, such that no valid capacity can be mapped, a ValueError will be raised.
  • Calculation Errors:

    • If the calculated inflow or outflow speeds result in negative or NaN values, the library automatically converts these to zero using internal validation functions.

License

This library is released under the MIT License.

Contact

If you have any questions or issues, please open an issue on GitHub or email us at duynguyen02.dev@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tjwb-2.2.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

tjwb-2.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file tjwb-2.2.0.tar.gz.

File metadata

  • Download URL: tjwb-2.2.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-41-generic

File hashes

Hashes for tjwb-2.2.0.tar.gz
Algorithm Hash digest
SHA256 3fa25987b4d62a342a22d12bcd3f2df7025c4020b92101d07c6f147b8a76046a
MD5 5e104abba95ed6d99dfecb8739e39df5
BLAKE2b-256 734ec9859c96a8512e6007f6cadf2db783baee0f45b059ce58deeeb6da1daeec

See more details on using hashes here.

File details

Details for the file tjwb-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: tjwb-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-41-generic

File hashes

Hashes for tjwb-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8213adcdc8d7bd0bb254a7265c5a3b00e11d55e03569f832a7a44f7e9b4dddc5
MD5 2bc7c6922e44ba910376f1b94b8caaf8
BLAKE2b-256 8a19ebc4bd70d4af20b76c249d855ec35ec1750b9a9891326822b1c055f9bb18

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page