Skip to content

Commit

Permalink
added the drought monitoring project code (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Akram Zaytar <[email protected]>
  • Loading branch information
Akramz and Akram Zaytar authored Jun 24, 2021
1 parent 152fb6c commit 7f865b6
Show file tree
Hide file tree
Showing 3 changed files with 787 additions and 26 deletions.
340 changes: 340 additions & 0 deletions .ipynb_checkpoints/6_Project_Drought_Monitoring-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1c38785d",
"metadata": {},
"source": [
"# Drought Monitoring\n",
"\n",
"- This project shows how satellite-based data can be used for drought monitoring.\n",
"- Rainfall is the most important indicator in the determination of drought.\n",
"- A deviation in rainfall from its Long Period Average (LPA) of 30 years is considered as fairly credible indicator of drought.\n",
"- For any given region and time period, we will calculate the percentage deviation of rainfall from long-term average and determine which regions are likely experiencing drought."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8558b153",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Automatic pdb calling has been turned ON\n"
]
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"%pdb on\n",
"%config InlineBackend.figure_format ='retina'\n",
"from IPython.core.debugger import set_trace\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"sns.set_theme()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "18787795",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8d60f82c130b407f9bd897b49f286111",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[40, -100], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=HBox(children=(T…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ee\n",
"import geemap\n",
"ee.Initialize()\n",
"m = geemap.Map()\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "5ac57198",
"metadata": {},
"source": [
"# Data"
]
},
{
"cell_type": "markdown",
"id": "8e542e8e",
"metadata": {},
"source": [
"The data set we are going to use is called **CHIRPS** (Climate Hazards group Infrared Precipitation with Station data). \n",
"\n",
"- 30+ year gridded rainfall data: available from 1981 to present.\n",
"- 0.05 deg (~6km) spatial resolution.\n",
"- Combines satellite rainfall measurements with ground station data.\n",
"- Provides consistent, long time-series data.\n",
"- Primary applications\n",
" - Drought monitoring\n",
" - Global environmental change over lands"
]
},
{
"cell_type": "markdown",
"id": "412166e6",
"metadata": {},
"source": [
"## CHIRPS Pentad\n",
"\n",
"- The primary computing time step for CHIRP is the pentad.\n",
"- Pentad represents the grouping of 5 days.\n",
"- There are 6 pentads in a calendar month.\n",
"- Daily images are derived by disaggregating pentadal product.\n",
"- CHIRPS data has a 3-week latency.\n",
"- The dataset has a single band (precipitation). "
]
},
{
"cell_type": "markdown",
"id": "608830e7",
"metadata": {},
"source": [
"## Caclulate Global Diviation of Monthly Total Rainfall\n",
"\n",
"Steps:\n",
"1. [x] Sum-Aggregate total rainfall on a monthly scale for all images in the collection.\n",
"2. [x] Average-aggregate the image collection to get the mean monthly precipitation. Visualize January's map.\n",
"3. [x] Select the precip image of a year-month of interest. Visualize it.\n",
"4. [x] Calculate the global diviation by `(img - mean) * 100 / mean`.\n",
"5. [x] Visualize the gloabal diviation. "
]
},
{
"cell_type": "markdown",
"id": "09c84005",
"metadata": {},
"source": [
"Let's sum aggregate the total rainfall on a monthly basis (output: 12-image-per year image collection):"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "042b4820",
"metadata": {},
"outputs": [],
"source": [
"start_year = 1981\n",
"end_year = 2020\n",
"\n",
"years = ee.List.sequence(start_year, end_year)\n",
"months = ee.List.sequence(1, 12)\n",
"\n",
"daily = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')\n",
"m.addLayer(daily.first(), {\"palette\": [\"white\", \"blue\"]}, \"CHRIPS Example\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "23a5e730",
"metadata": {},
"outputs": [],
"source": [
"# Make monthly-summed mosaic\n",
"# Loop over years-months to get summed monthly images\n",
"def map_month(year_collection, y, m):\n",
" summed_img = year_collection.filter(ee.Filter.calendarRange(m, m, \"month\")).reduce(ee.Reducer.sum())\n",
" return summed_img.set(\"system:time_start\", ee.Date.fromYMD(y, m, 1)).set(\"year\", y).set(\"month\", m).rename(\"ppt\")\n",
"\n",
"def map_year(y):\n",
" year_collection = daily.filter(ee.Filter.calendarRange(y, y, \"year\"))\n",
" return months.map(lambda m: map_month(year_collection, y, m))\n",
"\n",
"monthly = ee.ImageCollection.fromImages(years.map(map_year).flatten())"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "49318413",
"metadata": {},
"outputs": [],
"source": [
"m.addLayer(monthly.first(), {\"palette\": [\"white\", \"blue\"]}, \"CHRIPS Example\")"
]
},
{
"cell_type": "markdown",
"id": "9d996efe",
"metadata": {},
"source": [
"Let's average aggregate the monthly image collection to get a monthly climatology: "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4f9da836",
"metadata": {},
"outputs": [],
"source": [
"def agg_clim(collection, m):\n",
" \"\"\"Mean-aggregate the images from a specific `month`.\"\"\"\n",
" imgs = collection.filter(ee.Filter.eq(\"month\", m))\n",
" return imgs.mean().set(\"month\", m)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7ddc1c41",
"metadata": {},
"outputs": [],
"source": [
"monthly_clim = ee.ImageCollection.fromImages(months.map(lambda m: agg_clim(monthly, m)))"
]
},
{
"cell_type": "markdown",
"id": "5e8d1d3d",
"metadata": {},
"source": [
"Visualize January's climatology:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "96059962",
"metadata": {},
"outputs": [],
"source": [
"m.addLayer(ee.Image(monthly_clim.toList(monthly_clim.size()).get(6)), \n",
" {\"palette\": [\"white\", \"blue\"]}, \"CHRIPS Example\")"
]
},
{
"cell_type": "markdown",
"id": "7341adfe",
"metadata": {},
"source": [
"Let's select the precip image of a year-month of interest and visualize it:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "6a0cb8ff",
"metadata": {},
"outputs": [],
"source": [
"img_of_interest = monthly.filter(ee.Filter.And(ee.Filter.eq(\"year\", 2020), ee.Filter.eq(\"month\", 6))).first()\n",
"m.addLayer(img_of_interest, \n",
" {\"min\": 0.0, \"max\": 112.0 * 6, \"palette\": ['001137', '0aab1e', 'e7eb05', 'ff4a2d', 'e90000']}, \n",
" \"Image of Interest\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "c84e6467",
"metadata": {},
"outputs": [],
"source": [
"clim_of_interest = monthly_clim.filter(ee.Filter.eq(\"month\", 6)).first()\n",
"m.addLayer(clim_of_interest, \n",
" {\"min\": 0.0, \"max\": 112.0 * 6, \"palette\": ['001137', '0aab1e', 'e7eb05', 'ff4a2d', 'e90000']}, \n",
" \"Clim of Interest\")"
]
},
{
"cell_type": "markdown",
"id": "17295d6d",
"metadata": {},
"source": [
"Let's calculate the diviation:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "397036d7",
"metadata": {},
"outputs": [],
"source": [
"# diviation = (img_of_interest.subtract(clim_of_interest)).divide(clim_of_interest).multiply(100)\n",
"diviation = img_of_interest.subtract(clim_of_interest)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "91f8b58d",
"metadata": {},
"outputs": [],
"source": [
"stack = img_of_interest.rename(\"ppt\").addBands(clim_of_interest.rename(\"clim\"))\n",
"diviation = stack.expression(\n",
" '(PPT - CLIM) / CLIM', {\n",
" 'PPT': stack.select('ppt'),\n",
" 'CLIM': stack.select('clim')\n",
"})\n",
"m.addLayer(diviation, {\"palette\": ['001137', '0aab1e', 'e7eb05', 'ff4a2d', 'e90000']}, \"Diviation\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd74a70e",
"metadata": {},
"outputs": [],
"source": [
"geemap.ee_search()"
]
},
{
"cell_type": "markdown",
"id": "3ad5b1ad",
"metadata": {},
"source": [
"---"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 7f865b6

Please sign in to comment.