import xarray as xr
import pandas as pd
import os
import geopandas as gpd
from shapely.geometry import mapping
import numpy as np
!pip install rioxarray
Collecting rioxarray Downloading rioxarray-0.14.0-py3-none-any.whl (53 kB) Requirement already satisfied: pyproj>=2.2 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rioxarray) (3.4.1) Requirement already satisfied: numpy>=1.21 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rioxarray) (1.22.3) Requirement already satisfied: packaging in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rioxarray) (21.3) Requirement already satisfied: rasterio>=1.2 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rioxarray) (1.3.6) Requirement already satisfied: xarray>=0.17 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rioxarray) (2022.3.0) Requirement already satisfied: certifi in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from pyproj>=2.2->rioxarray) (2021.10.8) Requirement already satisfied: snuggs>=1.4.1 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (1.4.7) Requirement already satisfied: attrs in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (21.4.0) Requirement already satisfied: click>=4.0 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (8.0.4) Requirement already satisfied: click-plugins in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (1.1.1) Requirement already satisfied: affine in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (2.4.0) Requirement already satisfied: cligj>=0.5 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (0.7.2) Requirement already satisfied: setuptools in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from rasterio>=1.2->rioxarray) (58.0.4) Requirement already satisfied: colorama in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from click>=4.0->rasterio>=1.2->rioxarray) (0.4.4) Requirement already satisfied: pyparsing>=2.1.6 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from snuggs>=1.4.1->rasterio>=1.2->rioxarray) (3.0.4) Requirement already satisfied: pandas>=1.1 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from xarray>=0.17->rioxarray) (1.5.3) Requirement already satisfied: pytz>=2020.1 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from pandas>=1.1->xarray>=0.17->rioxarray) (2021.3) Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from pandas>=1.1->xarray>=0.17->rioxarray) (2.8.2) Requirement already satisfied: six>=1.5 in c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages (from python-dateutil>=2.8.1->pandas>=1.1->xarray>=0.17->rioxarray) (1.16.0) Installing collected packages: rioxarray Successfully installed rioxarray-0.14.0
WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\regionmask_env\lib\site-packages) WARNING: You are using pip version 22.0.4; however, version 23.0.1 is available. You should consider upgrading via the 'C:\Users\dboateng\Anaconda3\envs\regionmask_env\python.exe -m pip install --upgrade pip' command.
# set paths
import regionmask
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")
# read data
ghana_shape = gpd.read_file(path_shapefile)
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")
tp_monthly = tp_monthly.assign_coords({"longitude": (((tp_monthly.longitude + 180) % 360) - 180)})
import rioxarray
tp_monthly.rio.set_spatial_dims(x_dim="longitude", y_dim="latitude", inplace=True)
tp_monthly.rio.write_crs("epsg:4326", inplace=True)
<xarray.DataArray 'tp' (latitude: 721, longitude: 1440)> array([[0.00069632, 0.00069632, 0.00069632, ..., 0.00069632, 0.00069632, 0.00069632], [0.00070333, 0.00070337, 0.00070338, ..., 0.00070332, 0.00070333, 0.00070335], [0.0006945 , 0.00069459, 0.00069464, ..., 0.00069443, 0.00069443, 0.00069447], ..., [0.00017392, 0.0001739 , 0.00017387, ..., 0.00017391, 0.00017391, 0.00017392], [0.00017434, 0.00017429, 0.00017429, ..., 0.00017439, 0.00017439, 0.0001744 ], [0.00018674, 0.00018674, 0.00018674, ..., 0.00018674, 0.00018674, 0.00018674]], dtype=float32) Coordinates: expver float64 1.0 * longitude (longitude) float32 0.0 0.25 0.5 0.75 ... -1.0 -0.75 -0.5 -0.25 * latitude (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0 spatial_ref int32 0
clipped = tp_monthly.rio.clip(ghana_shape.geometry.apply(mapping), ghana_shape.crs, drop=True)
clipped
<xarray.DataArray 'tp' (latitude: 22, longitude: 1440)> array([[0.00216022, 0.00199021, 0.00189564, ..., 0.00212618, 0.001947 , 0.00184436], [ nan, nan, nan, ..., 0.00205568, 0.00212555, 0.00212988], [0.00244041, 0.00207277, 0.00209045, ..., 0.00240557, 0.00269521, 0.00254029], ..., [0.0036659 , 0.00372819, 0.00330154, ..., 0.00386723, 0.00438464, 0.00364647], [0.00347596, 0.00212743, 0.00262582, ..., 0.0040779 , 0.00491352, 0.00403812], [0.00188213, 0.00165231, 0.00205362, ..., 0.00494683, 0.00430314, 0.0027265 ]], dtype=float32) Coordinates: * latitude (latitude) float64 11.0 10.75 10.5 10.25 ... 6.5 6.25 6.0 5.75 * longitude (longitude) float64 0.0 0.25 0.5 0.75 ... -1.0 -0.75 -0.5 -0.25 expver float64 1.0 spatial_ref int32 0
clipped.sortby("longitude").plot()
<matplotlib.collections.QuadMesh at 0x2cd27796280>
africa_shape = gpd.read_file(afr_shape)
africa_shape
ADM0_CODE | ADM0_NAME | CONTINENT | ISO3 | ISO2 | UNI | UNDP | FAOSTAT | GAUL | RIC_ISO3 | ... | CRA | ECOWAS | IGAD | IOC | SADC | CICOS | ICPAC | BDMS | MOI | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 6 | Sudan | Africa | SDN | SD | 729.0 | SUD | 276.0 | 6.0 | ICPAC-SDN | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | MULTIPOLYGON (((38.58148 18.02542, 38.58203 18... |
1 | 8 | Angola | Africa | AGO | AO | 24.0 | ANG | 7.0 | 8.0 | BDMS-AGO | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | MULTIPOLYGON (((11.79481 -16.81925, 11.79375 -... |
2 | 29 | Benin | Africa | BEN | BJ | 204.0 | BEN | 53.0 | 29.0 | CRA-BEN | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((1.86343 6.28872, 1.86292 6.288... |
3 | 35 | Botswana | Africa | BWA | BW | 72.0 | BOT | 20.0 | 35.0 | BDMS-BWA | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | POLYGON ((25.17447 -17.77881, 25.18476 -17.783... |
4 | 42 | Burkina Faso | Africa | BFA | BF | 854.0 | BKF | 233.0 | 42.0 | CRA-BFA | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | POLYGON ((-0.45567 15.08082, -0.45411 15.07937... |
5 | 45 | Cameroon | Africa | CMR | CM | 120.0 | CMR | 32.0 | 45.0 | CICOS-CMR | ... | 0 | 0 | 0 | 0 | 0 | 11.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((9.26462 3.94935, 9.26302 3.948... |
6 | 47 | Cape Verde | Africa | CPV | CV | 132.0 | CVI | 35.0 | 47.0 | CRA-CPV | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-24.69949 14.89513, -24.69853 ... |
7 | 49 | Central African Republic | Africa | CAF | CF | 140.0 | CAF | 37.0 | 49.0 | CICOS-CAF | ... | 0 | 0 | 0 | 0 | 0 | 11.0 | 0.0 | 0.0 | NaN | POLYGON ((22.52068 11.00757, 22.55266 10.99373... |
8 | 50 | Chad | Africa | TCD | TD | 148.0 | CHD | 39.0 | 50.0 | CRA_CICOS-TCD | ... | 13 | 0 | 0 | 0 | 0 | 11.0 | 0.0 | 0.0 | 0.0 | POLYGON ((24.00000 16.00000, 24.00000 15.71112... |
9 | 58 | Comoros | Africa | COM | KM | 174.0 | COI | 45.0 | 58.0 | MOI-COM | ... | 0 | 0 | 0 | 5 | 0 | NaN | NaN | NaN | 15.0 | MULTIPOLYGON (((43.71993 -12.40682, 43.72064 -... |
10 | 59 | Congo | Africa | COG | CG | 178.0 | PRC | 46.0 | 59.0 | CICOS-COG | ... | 0 | 0 | 0 | 0 | 0 | 11.0 | 0.0 | 0.0 | NaN | POLYGON ((17.60632 3.64125, 17.65328 3.63904, ... |
11 | 66 | Côte d'Ivoire | Africa | CIV | CI | 384.0 | IVC | 107.0 | 66.0 | CRA-CIV | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-6.61524 4.74392, -6.61529 4.7... |
12 | 68 | Democratic Republic of the Congo | Africa | COD | CD | 180.0 | ZAI | 250.0 | 68.0 | BDMS-COD | ... | 0 | 0 | 0 | 0 | 6 | 11.0 | 0.0 | 16.0 | 0.0 | MULTIPOLYGON (((12.44326 -6.05846, 12.44326 -6... |
13 | 70 | Djibouti | Africa | DJI | DJ | 262.0 | DJI | 72.0 | 70.0 | ICPAC-DJI | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | MULTIPOLYGON (((42.63588 11.47977, 42.63588 11... |
14 | 74 | South Sudan | Africa | SSD | SS | 728.0 | NaN | 277.0 | 74.0 | ICPAC-SSD | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | POLYGON ((33.02315 12.22673, 33.25586 12.21435... |
15 | 76 | Equatorial Guinea | Africa | GNQ | GQ | 226.0 | EQG | 61.0 | 76.0 | CICOS-GNQ | ... | 0 | 0 | 0 | 0 | 0 | 11.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((5.63372 -1.40429, 5.63449 -1.4... |
16 | 77 | Eritrea | Africa | ERI | ER | 232.0 | ERI | 178.0 | 77.0 | ICPAC-ERI | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | MULTIPOLYGON (((43.15610 12.71929, 43.15624 12... |
17 | 79 | Ethiopia | Africa | ETH | ET | 231.0 | ETH | 238.0 | 79.0 | ICPAC-ETH | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | POLYGON ((37.94868 14.83911, 37.95420 14.83687... |
18 | 89 | Gabon | Africa | GAB | GA | 266.0 | GAB | 74.0 | 89.0 | CICOS-GAB | ... | 0 | 0 | 0 | 0 | 0 | 11.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((9.54272 -2.06359, 9.54272 -2.0... |
19 | 90 | Gambia | Africa | GMB | GM | 270.0 | GAM | 75.0 | 90.0 | CRA-GMB | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-16.82265 13.38340, -16.82221 ... |
20 | 94 | Ghana | Africa | GHA | GH | 288.0 | GHA | 81.0 | 94.0 | CRA-GHA | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-1.94577 4.79251, -1.94592 4.7... |
21 | 105 | Guinea-Bissau | Africa | GNB | GW | 624.0 | GBS | 175.0 | 105.0 | CRA-GNB | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-15.72265 10.85933, -15.72317 ... |
22 | 106 | Guinea | Africa | GIN | GN | 324.0 | GUI | 90.0 | 106.0 | CRA-GIN | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-13.27189 9.29412, -13.27383 9... |
23 | 133 | Kenya | Africa | KEN | KE | 404.0 | KEN | 114.0 | 133.0 | ICPAC-KEN | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | 15.0 | MULTIPOLYGON (((39.37656 -4.72125, 39.37412 -4... |
24 | 142 | Lesotho | Africa | LSO | LS | 426.0 | LES | 122.0 | 142.0 | BDMS-LSO | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | POLYGON ((28.69016 -28.58986, 28.69980 -28.600... |
25 | 144 | Liberia | Africa | LBR | LR | 430.0 | LIR | 123.0 | 144.0 | CRA-LBR | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-7.82210 4.42817, -7.82210 4.4... |
26 | 150 | Madagascar | Africa | MDG | MG | 450.0 | MAG | 129.0 | 150.0 | BDMS_MOI-MDG | ... | 0 | 0 | 0 | 5 | 6 | 0.0 | 0.0 | 16.0 | 15.0 | MULTIPOLYGON (((44.22955 -25.22101, 44.22670 -... |
27 | 152 | Malawi | Africa | MWI | MW | 454.0 | MLW | 130.0 | 152.0 | BDMS-MWI | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | MULTIPOLYGON (((34.76880 -12.09417, 34.72768 -... |
28 | 155 | Mali | Africa | MLI | ML | 466.0 | MLI | 133.0 | 155.0 | CRA-MLI | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | POLYGON ((-5.00000 24.99792, -5.00000 24.99589... |
29 | 159 | Mauritania | Africa | MRT | MR | 478.0 | MAU | 136.0 | 159.0 | CRA-MRT | ... | 13 | 0 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-16.50235 16.05727, -16.50409 ... |
30 | 160 | Mauritius | Africa | MUS | MU | 480.0 | MAR | 137.0 | 160.0 | BDMS_MOI-MUS | ... | 0 | 0 | 0 | 5 | 6 | 0.0 | 0.0 | 16.0 | 15.0 | MULTIPOLYGON (((57.33786 -20.47313, 57.33677 -... |
31 | 170 | Mozambique | Africa | MOZ | MZ | 508.0 | MOZ | 144.0 | 170.0 | BDMS-MOZ | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | 15.0 | MULTIPOLYGON (((32.99153 -25.97012, 32.99189 -... |
32 | 172 | Namibia | Africa | NAM | NA | 516.0 | NAM | 147.0 | 172.0 | BDMS-NAM | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | MULTIPOLYGON (((15.52104 -27.66624, 15.52115 -... |
33 | 181 | Niger | Africa | NER | NE | 562.0 | NER | 158.0 | 181.0 | CRA-NER | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | POLYGON ((12.12185 23.48946, 12.15302 23.48107... |
34 | 182 | Nigeria | Africa | NGA | NG | 566.0 | NIR | 159.0 | 182.0 | CRA-NGA | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((5.95832 4.33796, 5.95832 4.337... |
35 | 217 | Senegal | Africa | SEN | SN | 686.0 | SEN | 195.0 | 217.0 | CRA-SEN | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-16.78778 12.66777, -16.78882 ... |
36 | 220 | Seychelles | Africa | SYC | SC | 690.0 | SEY | 196.0 | 220.0 | BDMS_MOI-SYC | ... | 0 | 0 | 0 | 5 | 6 | 0.0 | 0.0 | 16.0 | 15.0 | MULTIPOLYGON (((51.12800 -10.22187, 51.12868 -... |
37 | 221 | Sierra Leone | Africa | SLE | SL | 694.0 | SIL | 197.0 | 221.0 | CRA-SLE | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | MULTIPOLYGON (((-11.58407 6.97383, -11.58407 6... |
38 | 226 | Somalia | Africa | SOM | SO | 706.0 | SOM | 201.0 | 226.0 | ICPAC-SOM | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | MULTIPOLYGON (((41.57762 -1.66024, 41.57721 -1... |
39 | 227 | South Africa | Africa | ZAF | ZA | 710.0 | SAF | 202.0 | 227.0 | BDMS-ZAF | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | MULTIPOLYGON (((37.84717 -46.85358, 37.84717 -... |
40 | 235 | Swaziland | Africa | SWZ | SZ | 748.0 | SWA | 209.0 | 235.0 | BDMS-SWZ | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | POLYGON ((31.42451 -25.71965, 31.47514 -25.751... |
41 | 243 | Togo | Africa | TGO | TG | 768.0 | TOG | 217.0 | 243.0 | CRA-TGO | ... | 13 | 3 | 0 | 0 | 0 | 0.0 | 0.0 | 0.0 | NaN | POLYGON ((-0.00000 11.11002, 0.03523 11.10232,... |
42 | 253 | Uganda | Africa | UGA | UG | 800.0 | UGA | 226.0 | 253.0 | ICPAC-UGA | ... | 0 | 0 | 4 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | POLYGON ((34.09062 3.87878, 34.09114 3.87310, ... |
43 | 257 | United Republic of Tanzania | Africa | TZA | TZ | 834.0 | URT | 215.0 | 257.0 | BDMS-TZA | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | 15.0 | MULTIPOLYGON (((40.21717 -10.29314, 40.21717 -... |
44 | 270 | Zambia | Africa | ZMB | ZM | 894.0 | ZAM | 251.0 | 270.0 | BDMS-ZMB | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | POLYGON ((30.78884 -8.25962, 30.79738 -8.28310... |
45 | 271 | Zimbabwe | Africa | ZWE | ZW | 716.0 | ZIM | 181.0 | 271.0 | BDMS-ZWE | ... | 0 | 0 | 0 | 0 | 6 | 0.0 | 0.0 | 16.0 | NaN | POLYGON ((29.90322 -15.62243, 29.90864 -15.623... |
46 | 4 | Algeria | Africa | DZA | DZ | 12.0 | ALG | 4.0 | 4.0 | OTHER-DZA | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((-1.91302 35.08822, -1.91302 35... |
47 | 43 | Burundi | Africa | BDI | BI | 108.0 | BDI | 29.0 | 43.0 | ICPAC-BDI | ... | 0 | 0 | 0 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | POLYGON ((30.43090 -2.32831, 30.43728 -2.32935... |
48 | 102 | Abyei | Africa | NaN | NaN | 0.0 | NaN | 0.0 | 0.0 | NaN | ... | 0 | 0 | 4 | 0 | 0 | NaN | NaN | NaN | NaN | POLYGON ((28.97701 9.64116, 28.97701 9.64116, ... |
49 | 145 | Libya | Africa | LBY | LY | 434.0 | LIB | 124.0 | 145.0 | OTHER-LBY | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((19.12258 30.29128, 19.12199 30... |
50 | 169 | Morocco | Africa | MAR | MA | 504.0 | MOR | 143.0 | 169.0 | OTHER-MAR | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((-9.79329 31.49641, -9.79350 31... |
51 | 205 | Rwanda | Africa | RWA | RW | 646.0 | RWA | 184.0 | 205.0 | ICPAC-RWA | ... | 0 | 0 | 0 | 0 | 0 | 0.0 | 14.0 | 0.0 | NaN | POLYGON ((30.46679 -1.06294, 30.46446 -1.06678... |
52 | 214 | Sao Tome and Principe | Africa | STP | ST | 678.0 | STP | 193.0 | 214.0 | OTHER-STP | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((6.52215 -0.01493, 6.52073 -0.0... |
53 | 248 | Tunisia | Africa | TUN | TN | 788.0 | TUN | 222.0 | 248.0 | OTHER-TUN | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((11.41957 33.17910, 11.41803 33... |
54 | 268 | Western Sahara | Africa | NaN | NaN | 0.0 | NaN | 0.0 | 0.0 | NaN | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((-17.01258 21.40293, -17.01285 ... |
55 | 40760 | Hala'ib triangle | Africa | NaN | NaN | 0.0 | NaN | 0.0 | 0.0 | NaN | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((36.66130 22.24455, 36.66102 22... |
56 | 40762 | Ma'tan al-Sarra | Africa | NaN | NaN | 0.0 | NaN | 0.0 | 0.0 | NaN | ... | 0 | 0 | 4 | 0 | 0 | NaN | NaN | NaN | NaN | POLYGON ((34.00521 21.77233, 33.95924 21.76744... |
57 | 40765 | Egypt | Africa | EGY | EG | 818.0 | EGY | 59.0 | 40765.0 | OTHER-EGY | ... | 0 | 0 | 0 | 0 | 0 | NaN | NaN | NaN | NaN | MULTIPOLYGON (((36.24782 23.56470, 36.24783 23... |
58 | 61013 | Ilemi triangle | Africa | NaN | NaN | 0.0 | NaN | 0.0 | 0.0 | NaN | ... | 0 | 0 | 4 | 0 | 0 | NaN | NaN | NaN | NaN | POLYGON ((35.32822 4.97037, 35.31111 4.91972, ... |
59 rows × 24 columns
regions =ghana_shape["REGION"]
regions
0 WESTERN NORTH 1 WESTERN 2 VOLTA 3 UPPER WEST 4 UPPER EAST 5 SAVANNAH 6 OTI 7 NORTHERN 8 NORTH EAST 9 GREATER ACCRA 10 EASTERN 11 CENTRAL 12 BONO EAST 13 BONO 14 ASHANTI 15 AHAFO Name: REGION, dtype: object
my_list
{'AHAFO', 'ASHANTI', 'BONO', 'BONO EAST', 'CENTRAL', 'EASTERN', 'GREATER ACCRA', 'NORTH EAST', 'NORTHERN', 'OTI', 'SAVANNAH', 'UPPER EAST', 'UPPER WEST', 'VOLTA', 'WESTERN', 'WESTERN NORTH'}
countries_mask_poly = regionmask.Regions(outlines=ghana_shape.geometry)
countries_mask_poly
<regionmask.Regions> Name: unnamed overlap: False Regions: 0 r0 Region0 1 r1 Region1 2 r2 Region2 3 r3 Region3 4 r4 Region4 .. .. ... 11 r11 Region11 12 r12 Region12 13 r13 Region13 14 r14 Region14 15 r15 Region15 [16 regions]
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")
mask = countries_mask_poly.mask(tp_monthly, lat_name="latitude", lon_name="longitude", wrap_lon=True)
mask
<xarray.DataArray 'mask' (latitude: 721, longitude: 1440)> array([[nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], ..., [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan]]) Coordinates: expver float64 1.0 * latitude (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0 * longitude (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8
mask.plot()
<matplotlib.collections.QuadMesh at 0x2cd4a8bc940>
droped = mask.dropna(dim=("latitude"), how="any")
droped
<xarray.DataArray 'mask' (latitude: 0, longitude: 1440)> array([], shape=(0, 1440), dtype=float64) Coordinates: expver float64 1.0 * latitude (latitude) float32 * longitude (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8
countries = list(africa_shape["ISO3"])
my_list = set(list(countries))
indexes = [countries.index(x) for x in my_list]
countries_mask_poly = regionmask.Regions(name="ADM0_NAME", numbers=indexes,
names=africa_shape.ADM0_NAME[indexes],
outlines=list(africa_shape.geometry.values[i] for i in range(0, africa_shape.shape[0])),
abbrevs=africa_shape["ADM0_CODE"][indexes])
countries_mask_poly
<regionmask.Regions> Name: ADM0_NAME overlap: False Regions: 0 6 Sudan 1 8 Angola 2 29 Benin 3 35 Botswana 4 42 Burkina Faso .. ... ... 50 169 Morocco 51 205 Rwanda 52 214 Sao Tome and Principe 53 248 Tunisia 57 40765 Egypt [55 regions]
countries_mask_poly[20]
<regionmask._OneRegion: Ghana (94 / 20)>