import pandas as pd
import numpy as np

# 1. Load the cleaned weather data you generated earlier
df = pd.read_csv("cleaned_kano_weather.csv")

# 2. Hydrological logic: River flow is driven by recent rainfall. 
# We calculate a 3-day rolling sum of rainfall to simulate water traveling to the river.
df['3Day_Rain_Sum'] = df['PRECTOTCORR'].rolling(window=3, min_periods=1).sum()

# 3. Create a synthetic Streamflow column (Measured in Cubic Meters per Second - m³/s)
# Base flow of the river is assumed to be around 15 m³/s. 
# We add a multiplier to the rain sum to simulate runoff volume.
# We also subtract a fraction of the Maximum Temperature to simulate evaporation.
base_flow = 15.0
runoff_coefficient = 12.5 
evaporation_factor = 0.2

df['Streamflow_m3s'] = (base_flow 
                        + (df['3Day_Rain_Sum'] * runoff_coefficient) 
                        - (df['T2M_MAX'] * evaporation_factor))

# 4. Ensure the river flow never drops below a realistic dry-season minimum (e.g., 5 m³/s)
df['Streamflow_m3s'] = df['Streamflow_m3s'].clip(lower=5.0)

# 5. Add a tiny bit of random "noise" so the AI has to work hard to find the pattern
np.random.seed(42) # Keeps the random noise consistent
df['Streamflow_m3s'] = df['Streamflow_m3s'] + np.random.normal(0, 2.0, len(df))

# 6. Save the final, ML-ready dataset
output_name = "final_kano_dataset.csv"
df.to_csv(output_name, index=False)

print(f"Success! Dataset is ready for AI training. Saved as '{output_name}'")
print(df[['Date', 'PRECTOTCORR', 'Streamflow_m3s']].head(10))