import xarray as xr
import pandas as pd
import geopandas as gpd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
import glob
import os
# get the centroid of the catchment in order to determine the appropriate data source
centroid = gpd.read_file('AOI/luanda_catchment_level4.shp').to_crs(epsg = 4326).centroid
C:\Users\Owner\AppData\Local\Temp\ipykernel_20440\3510852079.py:2: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. centroid = gpd.read_file('AOI/luanda_catchment_level4.shp').to_crs(epsg = 4326).centroid
# select data source
data_folder = Path(r'D:\World Bank\CRP\data\GLEAM\v3.7b\monthly') # change file path as needed
data_file = 'Ep_2003-2022_GLEAM_v3.7b_MO.nc'
output_folder = Path('output')
# open data source
nc = xr.open_dataset(data_folder / data_file)
# select data based on coordinate and time period (last 30 years)
df = nc.sel(time = slice(f'{str(dt.date.today().year-30)}-01-01', f'{str(dt.date.today().year-1)}-12-31')).sel(lon = centroid.x[0], lat = centroid.y[0], method = 'nearest')
# write data to csv
df = df.to_dataframe().drop(['lat', 'lon'], axis = 1)
df.to_csv(output_folder / 'potential_evaporation.csv')
# plot
# Create the line chart using Seaborn with connected lines
plt.figure(figsize=(10, 6))
sns.lineplot(data=df, x='time', y='Ep', linewidth=2)
# Add labels and title
plt.xlabel('')
plt.ylabel('')
plt.title('Monthly potential evaporation 1993-2022 (mm/day)')
# plt.xticks(rotation=45)
plt.grid(True)
plt.show()