{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "6e29ab03", "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import pandas as pd\n", "import os\n", "import geopandas as gpd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import regionmask" ] }, { "cell_type": "code", "execution_count": 3, "id": "07582dca", "metadata": {}, "outputs": [], "source": [ "era_data_path=\"C:/Users/dboateng/Desktop/Datasets/ERA5/monthly_1950_2021/\"\n", "path_shapefile=\"C:/Users/dboateng/Desktop/Datasets/Station/Ghana/Ghana_ShapeFile/gh_wgs16dregions.shp\"\n", "afr_shape = \"C:/Users/dboateng/Desktop/Datasets/Station/Ghana/Africa_shapefile/afr_g2014_2013_0.shp\"\n", "\n", "from1979to2012 = pd.date_range(start=\"1979-01-01\", end=\"2012-12-31\", freq=\"MS\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "7d806b11", "metadata": {}, "outputs": [], "source": [ "tp_monthly= xr.open_dataset(os.path.join(era_data_path, \"tp_monthly.nc\"))\n", "tp_monthly = tp_monthly[\"tp\"].sel(time=from1979to2012).mean(dim=\"time\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "32123d6c", "metadata": {}, "outputs": [], "source": [ "ghana_shape = gpd.read_file(path_shapefile)" ] }, { "cell_type": "code", "execution_count": 15, "id": "1c716a4c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(,\n", " dtype=object)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array(ghana_shape.geometry[0])" ] }, { "cell_type": "code", "execution_count": 16, "id": "0092beda", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "len() of unsized object", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [16]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m poly \u001b[38;5;241m=\u001b[39m \u001b[43mregionmask\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mRegions\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43mghana_shape\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgeometry\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32m~\\Anaconda3\\envs\\regionmask_env\\lib\\site-packages\\regionmask\\core\\regions.py:95\u001b[0m, in \u001b[0;36mRegions.__init__\u001b[1;34m(self, outlines, numbers, names, abbrevs, name, source, overlap)\u001b[0m\n\u001b[0;32m 83\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[0;32m 84\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m 85\u001b[0m outlines,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 91\u001b[0m overlap\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 92\u001b[0m ):\n\u001b[0;32m 94\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m numbers \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m---> 95\u001b[0m numbers \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43moutlines\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[0;32m 97\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _is_numeric(numbers):\n\u001b[0;32m 98\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumbers\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m must be numeric\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[1;31mTypeError\u001b[0m: len() of unsized object" ] } ], "source": [ "poly = regionmask.Regions(np.array(ghana_shape.geometry[0]))" ] }, { "cell_type": "code", "execution_count": 8, "id": "23760d3d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: unnamed\n", "overlap: False\n", "\n", "Regions:\n", " 0 r0 Region0\n", " 1 r1 Region1\n", " 2 r2 Region2\n", " 3 r3 Region3\n", " 4 r4 Region4\n", ".. .. ...\n", "11 r11 Region11\n", "12 r12 Region12\n", "13 r13 Region13\n", "14 r14 Region14\n", "15 r15 Region15\n", "\n", "[16 regions]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "poly" ] }, { "cell_type": "code", "execution_count": 9, "id": "720b227c", "metadata": {}, "outputs": [], "source": [ "mask = poly.mask(tp_monthly, lat_name=\"latitude\", lon_name=\"longitude\", wrap_lon=True)" ] }, { "cell_type": "code", "execution_count": 10, "id": "82bfab8e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'mask' (latitude: 721, longitude: 1440)>\n",
       "array([[nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       ...,\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan]])\n",
       "Coordinates:\n",
       "    expver     float64 1.0\n",
       "  * latitude   (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0\n",
       "  * longitude  (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8
" ], "text/plain": [ "\n", "array([[nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " ...,\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan]])\n", "Coordinates:\n", " expver float64 1.0\n", " * latitude (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0\n", " * longitude (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mask" ] }, { "cell_type": "code", "execution_count": 11, "id": "209e2810", "metadata": {}, "outputs": [], "source": [ "pr_mask = tp_monthly.where(mask==0)" ] }, { "cell_type": "code", "execution_count": 12, "id": "53091d97", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'tp' (latitude: 721, longitude: 1440)>\n",
       "array([[nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       ...,\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan],\n",
       "       [nan, nan, nan, ..., nan, nan, nan]], dtype=float32)\n",
       "Coordinates:\n",
       "    expver     float64 1.0\n",
       "  * longitude  (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8\n",
       "  * latitude   (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0
" ], "text/plain": [ "\n", "array([[nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " ...,\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan],\n", " [nan, nan, nan, ..., nan, nan, nan]], dtype=float32)\n", "Coordinates:\n", " expver float64 1.0\n", " * longitude (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8\n", " * latitude (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pr_mask" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }