#!/usr/bin/env python # coding: utf-8 # In[2]: import xarray as xr import pandas as pd import os import geopandas as gpd import numpy as np import matplotlib.pyplot as plt import regionmask # In[3]: era_data_path="C:/Users/dboateng/Desktop/Datasets/ERA5/monthly_1950_2021/" path_shapefile="C:/Users/dboateng/Desktop/Datasets/Station/Ghana/Ghana_ShapeFile/gh_wgs16dregions.shp" afr_shape = "C:/Users/dboateng/Desktop/Datasets/Station/Ghana/Africa_shapefile/afr_g2014_2013_0.shp" from1979to2012 = pd.date_range(start="1979-01-01", end="2012-12-31", freq="MS") # In[4]: tp_monthly= xr.open_dataset(os.path.join(era_data_path, "tp_monthly.nc")) tp_monthly = tp_monthly["tp"].sel(time=from1979to2012).mean(dim="time") # In[5]: ghana_shape = gpd.read_file(path_shapefile) # In[15]: np.array(ghana_shape.geometry[0]) # In[16]: poly = regionmask.Regions(np.array(ghana_shape.geometry[0])) # In[8]: poly # In[9]: mask = poly.mask(tp_monthly, lat_name="latitude", lon_name="longitude", wrap_lon=True) # In[10]: mask # In[11]: pr_mask = tp_monthly.where(mask==0) # In[12]: pr_mask