{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "rpglcqUkiGQ-" }, "source": [ "# Download forecast precipitation dataset from CHIRPS-GEFS website using Python\n", "\n", "*Written by Men Vuthy, 2022*\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "id": "NNOwGDOtb9m2" }, "source": [ "### Objective\n", "\n", "\n", "\n", "* The objective is to download forecast daily precipitation from CHIRPS-GEFS website by using Python, and save in local directory.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "HsEEYpsBcRS4" }, "source": [ "Data folder:\n", "\n", "![img1](img-markdown/CHIRPS-datasets.png \"Img1\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Code" ] }, { "cell_type": "markdown", "metadata": { "id": "twXmTpgEic0a" }, "source": [ "Import modules\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "vSEATtIpRtNl" }, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "from urllib import request" ] }, { "cell_type": "markdown", "metadata": { "id": "bLLpkULJg5WJ" }, "source": [ "Parameter Setting" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "lDlmABxCYHB1" }, "outputs": [], "source": [ "# Main website url\n", "url = 'https://data.chc.ucsb.edu/products/EWX/data/forecasts/CHIRPS-GEFS_precip_v12/05day/precip_mean/'\n", "\n", "# Directory for export\n", "export_dir = '/content/data_download/'\n", "\n", "# Start date range from i to j\n", "start_date_i = '2000-01-01'\n", "start_date_j = '2000-01-10'\n", "\n", "# End date range from i to j\n", "end_date_i = '2000-01-05'\n", "end_date_j = '2000-01-14'" ] }, { "cell_type": "markdown", "metadata": { "id": "BDM7twwag9IV" }, "source": [ "Start download" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "BDSbXEL4byaO" }, "outputs": [], "source": [ "# Create list of start date\n", "dateRange_pdcore = pd.date_range(start_date_i, start_date_j)\n", "dateRange = pd.DataFrame({'date':dateRange_pdcore })\n", "\n", "startDate = []\n", "for i in range(len(dateRange)):\n", " ts = pd.to_datetime(str(dateRange.date.values[i])) \n", " d = ts.strftime('%Y%m%d')\n", " startDate.append(d)\n", "\n", "# Create list of end date\n", "dateRange_pdcore = pd.date_range(end_date_i, end_date_j)\n", "dateRange = pd.DataFrame({'date':dateRange_pdcore })\n", "\n", "endtDate = []\n", "for i in range(len(dateRange)):\n", " ts = pd.to_datetime(str(dateRange.date.values[i])) \n", " d = ts.strftime('%Y%m%d')\n", " endtDate.append(d)\n", "\n", "# download_url, save_localFile = [], []\n", "for i in range(len(startDate)):\n", " # Create file name\n", " download_url = os.path.join(url + 'data-mean_'+ startDate[i]+ '_'+ endtDate[i]+ '.tif')\n", "\n", " # Create file name for saving to local drive\n", " save_localFile = export_dir + 'data-mean_'+ startDate[i]+ '_'+ endtDate[i]+ '.tif'\n", "\n", " # Download remote and save locally\n", " request.urlretrieve(download_url, save_localFile)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "VFlafrwpi7Dc" }, "source": [ "After executing above code, the downloaded data will be located in the `export_dir`." ] }, { "cell_type": "markdown", "metadata": { "id": "s5e_hdkGdRTJ" }, "source": [ "Downloaded data:\n", "\n", "![img2](img-markdown/downloaded-data.png \"Img2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "download-CHIRPS-data.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3.7.10", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.7.10" }, "vscode": { "interpreter": { "hash": "54697fe90b6b48df13497a1835b00f8636d17074f5f61d6ce3a5a8b26dd66d49" } } }, "nbformat": 4, "nbformat_minor": 0 }