I have daily series of precipitation, evaporation of river runoff within a river…
- User
I have daily series of precipitation, evaporation of river runoff within a river catchment. I also have monthly series of water storage within a catchment. The data on water storage are accurate, but there is an error in other components of the water balance. I want to use the data on precipitation, evaporation and river runoff to decompose the average monthly series of water storage into daily ones. Find a program in Python for this. Also write the equation.
Assistant
Here's a Python program to disaggregate monthly water storage into daily values using daily precipitation, evaporation, and runoff data:
Water Balance Equation
The fundamental water balance equation is:
dS/dt = P - E - QFor discrete daily time steps:
S(t) = S(t-1) + P(t) - E(t) - Q(t)Where:
- S(t) = water storage at time t
- P(t) = precipitation at time t
- E(t) = evaporation at time t
- Q(t) = river runoff at time t
Python Implementation
import numpy as np import pandas as pd from scipy.optimize import minimize import matplotlib.pyplot as plt from datetime import datetime, timedelta class WaterStorageDisaggregator: def __init__(self, daily_data, monthly_storage): """ Initialize the disaggregator Parameters: daily_data: DataFrame with columns ['date', 'precipitation', 'evaporation', 'runoff'] monthly_storage: DataFrame with columns ['date', 'storage'] (monthly data) """ self.daily_data = daily_data.copy() self.monthly_storage = monthly_storage.copy() # Ensure date columns are datetime self.daily_data['date'] = pd.to_datetime(self.daily_data['date']) self.monthly_storage['date'] = pd.to_datetime(self.monthly_storage['date']) # Add month-year column to daily data for grouping self.daily_data['month_year'] = self.daily_data['date'].dt.to_period('M') self.monthly_storage['month_year'] = self.monthly_storage['date'].dt.to_period('M') def disaggregate_storage(self, initial_storage=None, method='optimization'): """ Disaggregate monthly storage to daily values Parameters: initial_storage: Initial storage value (if None, uses first monthly value) method: 'optimization' or 'proportional' Returns: DataFrame with daily storage values """ if method == 'optimization': return self._optimize_disaggregation(initial_storage) elif method == 'proportional': return self._proportional_disaggregation(initial_storage) else: raise ValueError("Method must be 'optimization' or 'proportional'") def _optimize_disaggregation(self, initial_storage): """ Use optimization to disaggregate storage while maintaining water balance """ # Group daily data by month monthly_groups = self.daily_data.groupby('month_year') # Initialize results daily_storage = [] current_storage = initial_storage or self.monthly_storage.iloc[0]['storage'] for month_period, group in monthly_groups: # Get corresponding monthly storage monthly_obs = self.monthly_storage[ self.monthly_storage['month_year'] == month_period ] if len(monthly_obs) == 0: continue target_monthly_change = monthly_obs.iloc[0]['storage'] - current_storage # Extract daily water balance components for this month P = group['precipitation'].values E = group['evaporation'].values Q = group['runoff'].values n_days = len(P) # Objective function: minimize adjustment to daily components def objective(x): # x contains adjustment factors for P, E, Q P_adj = P * x[0] E_adj = E * x[1] Q_adj = Q * x[2] # Calculate implied monthly storage change daily_changes = P_adj - E_adj - Q_adj calculated_monthly_change = np.sum(daily_changes) # Penalize deviation from observed monthly change monthly_error = (calculated_monthly_change - target_monthly_change) ** 2 # Penalize large adjustments to maintain realistic values adjustment_penalty = 0.1 * ((x[0]-1)**2 + (x[1]-1)**2 + (x[2]-1)**2) return monthly_error + adjustment_penalty # Constraints: adjustment factors should be reasonable bounds = [(0.5, 2.0), (0.5, 2.0), (0.5, 2.0)] initial_guess = [1.0, 1.0, 1.0] # Optimize result = minimize(objective, initial_guess, bounds=bounds, method='L-BFGS-B') # Apply optimal adjustments P_adj = P * result.x[0] E_adj = E * result.x[1] Q_adj = Q * result.x[2] # Calculate daily storage values for this month month_storage = [current_storage] for i in range(n_days): daily_change = P_adj[i] - E_adj[i] - Q_adj[i] next_storage = month_storage[-1] + daily_change month_storage.append(next_storage) # Add to results (exclude the initial value for the month) for i, (idx, row) in enumerate(group.iterrows()): daily_storage.append({ 'date': row['date'], 'storage': month_storage[i+1], 'precipitation_adj': P_adj[i], 'evaporation_adj': E_adj[i], 'runoff_adj': Q_adj[i] }) # Update current storage for next month current_storage = monthly_obs.iloc[0]['storage'] return pd.DataFrame(daily_storage) def _proportional_disaggregation(self, initial_storage): """ Simple proportional disaggregation based on daily water balance """ # Group daily data by month monthly_groups = self.daily_data.groupby('month_year') daily_storage = [] current_storage = initial_storage or self.monthly_storage.iloc[0]['storage'] for month_period, group in monthly_groups: # Get corresponding monthly storage monthly_obs = self.monthly_storage[ self.monthly_storage['month_year'] == month_period ] if len(monthly_obs) == 0: continue target_end_storage = monthly_obs.iloc[0]['storage'] target_monthly_change = target_end_storage - current_storage # Calculate implied monthly change from daily data daily_changes = (group['precipitation'] - group['evaporation'] - group['runoff']) implied_monthly_change = daily_changes.sum() # Scale daily changes proportionally if abs(implied_monthly_change) > 1e-6: scale_factor = target_monthly_change / implied_monthly_change scaled_daily_changes = daily_changes * scale_factor else: scaled_daily_changes = daily_changes # Calculate daily storage values month_storage = current_storage for i, (idx, row) in enumerate(group.iterrows()): month_storage += scaled_daily_changes.iloc[i] daily_storage.append({ 'date': row['date'], 'storage': month_storage }) current_storage = target_end_storage return pd.DataFrame(daily_storage) def validate_results(self, daily_storage_df): """ Validate that daily storage aggregates to monthly storage """ daily_storage_df['month_year'] = daily_storage_df['date'].dt.to_period('M') # Get end-of-month storage values monthly_aggregated = daily_storage_df.groupby('month_year')['storage'].last().reset_index() monthly_aggregated.columns = ['month_year', 'aggregated_storage'] # Merge with observed monthly storage comparison = self.monthly_storage.merge(monthly_aggregated, on='month_year') comparison['error'] = comparison['storage'] - comparison['aggregated_storage'] comparison['relative_error'] = comparison['error'] / comparison['storage'] * 100 print("Validation Results:") print(f"Mean absolute error: {comparison['error'].abs().mean():.3f}") print(f"Mean relative error: {comparison['relative_error'].abs().mean():.2f}%") print(f"Max absolute error: {comparison['error'].abs().max():.3f}") return comparison # Example usage def example_usage(): # Create sample data dates = pd.date_range('2023-01-01', '2023-12-31', freq='D') daily_data = pd.DataFrame({ 'date': dates, 'precipitation': np.random.exponential(2, len(dates)), 'evaporation': np.random.exponential(1.5, len(dates)), 'runoff': np.random.exponential(1, len(dates)) }) # Create monthly storage data monthly_dates = pd.date_range('2023-01-31', '2023-12-31', freq='M') monthly_storage = pd.DataFrame({ 'date': monthly_dates, 'storage': 1000 + np.cumsum(np.random.normal(0, 50, len(monthly_dates))) }) # Initialize disaggregator disaggregator = WaterStorageDisaggregator(daily_data, monthly_storage) # Disaggregate storage daily_storage = disaggregator.disaggregate_storage(method='optimization') # Validate results validation = disaggregator.validate_results(daily_storage) # Plot results plt.figure(figsize=(12, 8)) plt.subplot(2, 1, 1) plt.plot(daily_storage['date'], daily_storage['storage'], 'b-', label='Daily Storage') plt.plot(monthly_storage['date'], monthly_storage['storage'], 'ro', label='Monthly Storage (Observed)') plt.ylabel('Storage') plt.title('Water Storage Disaggregation') plt.legend() plt.grid(True) plt.subplot(2, 1, 2) plt.plot(daily_data['date'], daily_data['precipitation'], 'g-', alpha=0.7, label='Precipitation') plt.plot(daily_data['date'], daily_data['evaporation'], 'r-', alpha=0.7, label='Evaporation') plt.plot(daily_data['date'], daily_data['runoff'], 'b-', alpha=0.7, label='Runoff') plt.ylabel('Water Flux') plt.xlabel('Date') plt.legend() plt.grid(True) plt.tight_layout() plt.show() return daily_storage, validation if __name__ == "__main__": daily_storage, validation = example_usage()Key Features
-
Optimization Method: Adjusts daily P, E, Q values to match monthly storage constraints while minimizing changes to the original data
-
Proportional Method: Simple scaling approach that proportionally adjusts daily water balance components
-
Validation: Checks that disaggregated daily storage aggregates correctly to monthly values
-
Flexibility: Handles missing data and different time periods
The optimization approach is recommended as it maintains realistic daily variations while ensuring water balance consistency with the accurate monthly storage data.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.