import xarray as xr
import pandas as pd
import os
import geopandas as gpd
import rioxarray
from shapely.geometry import mapping
import numpy as np
!pip install xarray
Requirement already satisfied: xarray in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (2022.3.0) Requirement already satisfied: numpy>=1.18 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from xarray) (1.22.3) Requirement already satisfied: pandas>=1.1 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from xarray) (1.5.3) Requirement already satisfied: packaging>=20.0 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from xarray) (21.3) Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from packaging>=20.0->xarray) (3.0.4) Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from pandas>=1.1->xarray) (2.8.2) Requirement already satisfied: pytz>=2020.1 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from pandas>=1.1->xarray) (2021.3) Requirement already satisfied: six>=1.5 in c:\users\dboateng\anaconda3\envs\clip\lib\site-packages (from python-dateutil>=2.8.1->pandas>=1.1->xarray) (1.16.0)
WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\clip\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\clip\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\clip\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\clip\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\clip\lib\site-packages) WARNING: Ignoring invalid distribution -yqt5-sip (c:\users\dboateng\anaconda3\envs\clip\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\Clip\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"
from1979to2012 = pd.date_range(start="1979-01-01", end="2012-12-31", freq="MS")
# read data
ghana_shape = gpd.read_file(path_shapefile)
ghana_shape
ID | REGION | geometry | |
---|---|---|---|
0 | 0 | WESTERN NORTH | POLYGON ((-3.08758 6.97493, -3.08594 6.97410, ... |
1 | 0 | WESTERN | POLYGON ((-2.18923 6.14451, -2.18797 6.14054, ... |
2 | 0 | VOLTA | POLYGON ((0.59492 6.99984, 0.59428 6.99960, 0.... |
3 | 0 | UPPER WEST | POLYGON ((-2.89647 10.76039, -2.89572 10.76160... |
4 | 0 | UPPER EAST | POLYGON ((-0.33901 11.08862, -0.33870 11.08963... |
5 | 0 | SAVANNAH | POLYGON ((-0.58767 9.32212, -0.58533 9.32024, ... |
6 | 0 | OTI | POLYGON ((-0.05850 8.34983, -0.05817 8.34939, ... |
7 | 0 | NORTHERN | POLYGON ((0.08256 8.52339, 0.08375 8.52006, 0.... |
8 | 0 | NORTH EAST | POLYGON ((-1.05819 10.15529, -1.05908 10.15677... |
9 | 0 | GREATER ACCRA | POLYGON ((0.02074 5.64086, 0.01695 5.63960, 0.... |
10 | 0 | EASTERN | POLYGON ((-0.91043 6.60781, -0.91058 6.60773, ... |
11 | 0 | CENTRAL | POLYGON ((-0.47410 5.40386, -0.47659 5.40170, ... |
12 | 0 | BONO EAST | POLYGON ((-2.07744 7.64129, -2.07765 7.64234, ... |
13 | 0 | BONO | POLYGON ((-2.82534 7.82188, -2.82642 7.82301, ... |
14 | 0 | ASHANTI | POLYGON ((-0.75597 6.91299, -0.75562 6.91007, ... |
15 | 0 | AHAFO | POLYGON ((-2.22717 7.00488, -2.22753 7.00467, ... |
poly = regionmask.Regions(np.array(ghana_shape.geometry))
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")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [17], in <cell line: 1>() ----> 1 tp_monthly= xr.open_dataset(os.path.join(era_data_path, "tp_monthly.nc")) 2 tp_monthly = tp_monthly["tp"].sel(time=from1979to2012).mean(dim="time") File ~\Anaconda3\envs\clip\lib\site-packages\xarray\backends\api.py:479, in open_dataset(filename_or_obj, engine, chunks, cache, decode_cf, mask_and_scale, decode_times, decode_timedelta, use_cftime, concat_characters, decode_coords, drop_variables, backend_kwargs, *args, **kwargs) 476 kwargs.update(backend_kwargs) 478 if engine is None: --> 479 engine = plugins.guess_engine(filename_or_obj) 481 backend = plugins.get_backend(engine) 483 decoders = _resolve_decoders_kwargs( 484 decode_cf, 485 open_backend_dataset_parameters=backend.open_dataset_parameters, (...) 491 decode_coords=decode_coords, 492 ) File ~\Anaconda3\envs\clip\lib\site-packages\xarray\backends\plugins.py:148, in guess_engine(store_spec) 140 else: 141 error_msg = ( 142 "found the following matches with the input file in xarray's IO " 143 f"backends: {compatible_engines}. But their dependencies may not be installed, see:\n" 144 "https://docs.xarray.dev/en/stable/user-guide/io.html \n" 145 "https://docs.xarray.dev/en/stable/getting-started-guide/installing.html" 146 ) --> 148 raise ValueError(error_msg) ValueError: found the following matches with the input file in xarray's IO backends: ['netcdf4', 'h5netcdf']. But their dependencies may not be installed, see: https://docs.xarray.dev/en/stable/user-guide/io.html https://docs.xarray.dev/en/stable/getting-started-guide/installing.html