# processes historical precipitation data from CHIRPS
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_9416\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
# use CHIRPS for 50°S - 50°N
# use WorldClim for others
if abs(centroid.y[0]) <= 50:
data_folder = Path('D:/World Bank/CRP/data/CHIRPS') # change file path as needed
data_file = 'chirps-v2.0.monthly.nc'
else:
data_folder = Path('D:/World Bank/CRP/data/WorldClim') # change file path as needed
data_file = ''
# WorldClim is not yet downloaded. TODO
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-01')).sel(longitude = centroid.x[0], latitude = centroid.y[0], method = 'nearest')
# write data to csv
df = df.to_dataframe().drop(['latitude', 'longitude'], axis = 1)
df.to_csv(output_folder / 'precip.csv')
# plot
# Create the line chart using Seaborn with connected lines
plt.figure(figsize=(10, 6))
sns.lineplot(data=df, x='time', y='precip', linewidth=2)
# Add labels and title
plt.xlabel('')
plt.ylabel('')
plt.title('Monthly precipitation 1993-2022 (mm)')
# plt.xticks(rotation=45)
plt.grid(True)
plt.show()