# Load the ERA5 data (adjust filename)
file_path = "your_era5_data.csv" # Replace with actual file path
df = pd.read_csv(file_path, parse_dates=["timestamp"])
# Ensure columns are correctly named (adjust if needed)
df.rename(columns={"temperature": "t2m", "precipitation": "precip"}, inplace=True)
# Extract year, month, and quarter
df["year"] = df["timestamp"].dt.year
df["month"] = df["timestamp"].dt.month
df["quarter"] = df["timestamp"].dt.quarter
# Group by month and compute statistics
monthly = df.groupby(["year", "month"]).agg(
mean_temp=("t2m", "mean"),
total_precip=("precip", "sum")
annual = monthly.groupby("year").agg(
annual_mean_temp=("mean_temp", "mean"),
min_temp=("min_temp", "min"),
max_temp=("max_temp", "max"),
annual_precip=("total_precip", "sum")
bioclim["BIO1"] = annual["annual_mean_temp"]
bioclim["BIO2"] = monthly.groupby("year")["mean_temp"].agg(lambda x: x.max() - x.min()).values
bioclim["BIO3"] = (bioclim["BIO2"] / (annual["max_temp"] - annual["min_temp"])) * 100
bioclim["BIO4"] = monthly.groupby("year")["mean_temp"].std().values * 100
bioclim["BIO5"] = annual["max_temp"]
bioclim["BIO6"] = annual["min_temp"]
bioclim["BIO7"] = annual["max_temp"] - annual["min_temp"]
# Compute quarterly statistics
quarterly = df.groupby(["year", "quarter"]).agg(
mean_temp=("t2m", "mean"),
total_precip=("precip", "sum")
# Find the wettest and driest quarters
wettest_quarter = quarterly.groupby("year")["total_precip"].idxmax()
driest_quarter = quarterly.groupby("year")["total_precip"].idxmin()
bioclim["BIO8"] = quarterly.loc[wettest_quarter, "mean_temp"].values
bioclim["BIO9"] = quarterly.loc[driest_quarter, "mean_temp"].values
bioclim["BIO10"] = quarterly.groupby("year")["mean_temp"].max().values
bioclim["BIO11"] = quarterly.groupby("year")["mean_temp"].min().values
bioclim["BIO12"] = annual["annual_precip"]
bioclim["BIO13"] = monthly.groupby("year")["total_precip"].max().values
bioclim["BIO14"] = monthly.groupby("year")["total_precip"].min().values
bioclim["BIO15"] = (monthly.groupby("year")["total_precip"].mean() /
monthly.groupby("year")["total_precip"].std()).values * 100
bioclim["BIO16"] = quarterly.loc[wettest_quarter, "total_precip"].values
bioclim["BIO17"] = quarterly.loc[driest_quarter, "total_precip"].values
bioclim["BIO18"] = quarterly.groupby("year")["total_precip"].max().values
bioclim["BIO19"] = quarterly.groupby("year")["total_precip"].min().values
bioclim.to_csv("bioclimatic_variables.csv", index=False)
print("Bioclimatic variables calculated and saved!")