A Python implementation of the US EPA ITRC groundwater gradient calculator, originally developed by Dr. Jim Weaver at the EPA's National Exposure Research Laboratory. This application converts the original JavaScript calculator into a modern Python web application with interactive 3D visualization.
This application calculates hydraulic gradient magnitude and flow direction by fitting a plane to groundwater head measurements from monitoring wells. It supports both confined and unconfined aquifers, handles unit conversions, and provides interactive 3D visualization of the data and calculated gradient.
- Interactive Web Interface: Built with Dash/Plotly for real-time gradient calculation
- 3D Visualization: Interactive plots showing wells, fitted plane, and gradient vector
- Multiple Aquifer Types: Supports both confined and unconfined aquifer calculations
- Unit Flexibility: Handles both metric (meters) and imperial (feet) units
- Well Selection: Include/exclude specific wells from calculations via checkboxes
- Example Datasets: Two pre-loaded examples for testing and demonstration
- Real-time Updates: Calculations update automatically as data is modified
gradient_py/
├── app.py # Dash web application
├── gradient.py # Core gradient calculation class
├── requirements.txt # Python dependencies
├── gradient4plus-ns_1.js # Original EPA JavaScript implementation
└── README.md # This file
The main calculation engine that:
- Fits a plane to 3D point data using least squares regression
- Calculates gradient magnitude and flow direction
- Handles unit conversions and aquifer type adjustments
- Provides standalone plotting capabilities
- Supports up to 30 monitoring wells
Interactive web interface featuring:
- Editable data table for well coordinates and head values
- Real-time 3D plotting with Plotly
- Unit selection dropdowns
- Aquifer type selection
- Well inclusion/exclusion via row selection
- Pre-loaded example datasets
- Gradient results summary
- Python 3.7 or higher
- pip package manager
-
Clone or download the repository:
git clone <repository-url> cd gradient_py
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
python app.py
-
Open your browser to:
http://127.0.0.1:8050
# Create virtual environment
python -m venv gradient_env
# Activate virtual environment
# Windows:
gradient_env\Scripts\activate
# Mac/Linux:
source gradient_env/bin/activate
# Install dependencies
pip install -r requirements.txt
# Run application
python app.py- Load Example Data: Click "Load Example 1" or "Load Example 2" to start with sample datasets
- Edit Well Data: Modify coordinates and head values directly in the table
- Select Wells: Use checkboxes to include/exclude specific wells from calculations
- Configure Settings: Choose units (meters/feet) and aquifer type (confined/unconfined)
- View Results: Gradient magnitude, direction, and fit quality are displayed automatically
- 3D Visualization: Interact with the 3D plot to examine wells, fitted plane, and gradient vector
from gradient import GradientCalculator
# Example data
x = [100, 0, 100, 100, 0, 100]
y = [0, 100, 100, 0, 100, 100]
h = [20, 20, 19.75, 20, 20, 19.75]
# Calculate gradient
gc = GradientCalculator(x, y, h, head_unit='ft', dist_unit='ft', aquifer_type='confined')
# Display results
gc.summary()
# Show 3D plot
gc.plot()- Gradient Magnitude: Unitless value representing the slope of the water table
- Flow Direction: Degrees from North (positive Y-axis) indicating steepest descent direction
- R²: Coefficient of determination showing how well the plane fits the data
- Head Range: Difference between highest and lowest head measurements
Based on the original EPA ITRC example from August 2005:
- 10 monitoring wells
- Large coordinate values (UTM-style)
- Produces gradient magnitude ≈ 0.0019
- Flow direction ≈ 37.78° from North
Simplified dataset showing clear gradient pattern:
- 8 monitoring wells
- Simple coordinate system (0-150 range)
- More pronounced gradient for demonstration
python app.pyAccess at http://127.0.0.1:8050
The application is configured for Plotly Cloud deployment with:
server = app.serverdeclaration inapp.py- Under 80 MiB total file size
- All dependencies in
requirements.txt
To deploy to Plotly Cloud:
- Create account at https://chart-studio.plotly.com/
- Install Plotly CLI:
pip install plotly - Configure credentials:
plotly setup - Deploy:
plotly deploy
Heroku:
# Add Procfile
echo "web: python app.py" > Procfile
# Deploy via Heroku CLIDocker:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8050
CMD ["python", "app.py"]AWS/Azure/GCP:
- Deploy as containerized application
- Use platform-specific Python hosting services
The gradient calculation follows the EPA ITRC methodology:
- Plane Fitting: Uses least squares regression to fit h = ax + by + c to the data
- Gradient Calculation:
- Magnitude: √(a² + b²)
- Direction: arctan2(-a, -b) converted to degrees from North
- Unit Handling: Converts to meters internally, preserves original units for display
- Aquifer Type: For unconfined aquifers, uses h² instead of h in calculations
- X-axis: Eastward (positive to the right)
- Y-axis: Northward (positive upward)
- Flow Direction: Measured clockwise from North (positive Y-axis)
- Gradient Vector: Points in direction of steepest descent (water flow direction)
- Minimum: 3 wells required for plane fitting
- Maximum: 30 wells supported
- Coordinate Format: Any consistent coordinate system (UTM, State Plane, local grid)
- Head Values: Hydraulic head measurements in consistent units
- "Insufficient Wells" Message: Ensure at least 3 wells are selected (checked) in the table
- Poor R² Values: Check for data entry errors or wells that don't follow a planar trend
- Unexpected Gradient Direction: Verify coordinate system and North direction
- Unit Conversion Issues: Ensure consistent units within each measurement type
- For large datasets, consider using a subset of representative wells
- Ensure good spatial distribution of wells for best plane fitting
- Remove obvious outliers that may skew the gradient calculation
- Original EPA ITRC implementation by Dr. Jim Weaver, EPA National Exposure Research Laboratory
- Available at: https://www3.epa.gov/ceampubl/learn2model/part-two/onsite/gradient4plus-ns.html
- ITRC Technical Guidelines for Groundwater Monitoring
This implementation is based on public domain EPA software and is provided for educational and research purposes.
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
For questions about the original methodology, refer to the EPA ITRC documentation. For issues with this Python implementation, please use the project's issue tracker.