{ "cells": [ { "cell_type": "markdown", "id": "2d51524a-23bb-4c7a-a293-7fed2cb013f2", "metadata": {}, "source": [ "# XMACIS2 Python API" ] }, { "cell_type": "markdown", "id": "608ef0be-932f-4039-866a-857dba266244", "metadata": {}, "source": [ "This notebook retreives climate data in tabular form from the Xmacis server.\n", "By default, the notebook will retrieve the following fields:\n", "\n", "1. Daily max T\n", "1. Daily min T\n", "1. Daily mean T\n", "1. Daily mean T departure from normal \n", "1. Daily heating degree days\n", "1. Daily cooling degree days\n", "1. Daily precip\n", "1. Daily snowfall\n", "\n", "The user only needs to specify the site, and the beginning/end dates of the period of interest. " ] }, { "cell_type": "markdown", "id": "39ab1bf6-0a26-4d28-b5a2-61b581e75be1", "metadata": {}, "source": [ "
\n", "

Task 1:

\n", " In the following markdown cell, enter your name, the site, and the beginning/end dates of your period of interest.\n", "
" ] }, { "cell_type": "markdown", "id": "46ff3ba8-2589-4384-9fd3-84664775b4f3", "metadata": {}, "source": [ "Name: Kevin Tyle\n", "Site: Albany NY\n", "Data period: 1993-03-10 - 1993-03-16" ] }, { "cell_type": "markdown", "id": "7b56e442-a7ca-40ca-b934-16cf4f69e6bd", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "id": "dc89fbf4-f9a2-4b3d-8233-0f6362cc32e1", "metadata": { "tags": [] }, "outputs": [], "source": [ "import urllib\n", "import requests\n", "import json\n", "import pandas as pd" ] }, { "cell_type": "markdown", "id": "6d445bf3-43ca-49a3-87d1-71aeb9fd6153", "metadata": {}, "source": [ "
\n", "

Task 2:

\n", " Specify the site (e.g., alb)\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "31c91b50-1f18-4a93-8b34-2b4bbd576dc8", "metadata": { "tags": [] }, "outputs": [], "source": [ "site = 'ALB'" ] }, { "cell_type": "markdown", "id": "342572fd-ba49-42c7-b5b8-8f4f1cb41d82", "metadata": {}, "source": [ "
\n", "

Task 3:

\n", " Specify the beginning and end dates (e.g., 2022-01-01 and 2022-12-31)\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "b2b19548-2ad9-43c9-9d00-ae7465562609", "metadata": { "tags": [] }, "outputs": [], "source": [ "sdate = '1993-03-10'\n", "edate = '1993-03-16'" ] }, { "cell_type": "markdown", "id": "0b7da857-9b48-44a2-919d-ac56196700a5", "metadata": {}, "source": [ "Define the fields to retrieve (hard-coded here to retrieve the 8 listed above) and the corresponding column names that will appear in the downloaded dataframe" ] }, { "cell_type": "code", "execution_count": null, "id": "fbb84079-2020-44ff-85c3-ea6db58fc46d", "metadata": { "tags": [] }, "outputs": [], "source": [ "input_dict = {\"elems\":[\"maxt\",\"mint\",\"avgt\",{\"name\":\"avgt\",\"normal\":\"departure\"},\"hdd\",\"cdd\",\"pcpn\",\"snow\",\"snwd\"],\"sid\":site,\"sdate\":sdate,\"edate\":edate}\n", "output_cols = ['DATE','MAX','MIN','AVG','DEP','HDD','CDD','PCP','SNW','DPT']" ] }, { "cell_type": "markdown", "id": "b2216fbd-b4c6-402f-a126-427cda343277", "metadata": {}, "source": [ "Construct the web query and retrieve the data" ] }, { "cell_type": "code", "execution_count": null, "id": "16023016-7427-4c27-aa9b-b77c4afa1ff3", "metadata": { "tags": [] }, "outputs": [], "source": [ "params = urllib.parse.urlencode({'params':json.dumps(input_dict)}).encode(\"utf-8\")\n", "req = urllib.request.Request('http://data.rcc-acis.org/StnData', params, {'Accept':'application/json'})\n", "response = urllib.request.urlopen(req)\n", "a = response.read()\n", "z= json.loads(a)\n", "b=z[\"data\"]" ] }, { "cell_type": "markdown", "id": "42cd41b7-2c53-4b8f-a191-29fe60ea7d5d", "metadata": { "tags": [] }, "source": [ "Construct a Pandas `DataFrame` from the downloaded data" ] }, { "cell_type": "code", "execution_count": null, "id": "be83a800-5147-406d-a6cb-826c65101d92", "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame(b,columns=output_cols)" ] }, { "cell_type": "markdown", "id": "6e63126e-0b8a-4c90-97ab-a0e246812d32", "metadata": { "tags": [] }, "source": [ "Examine the `DataFrame`" ] }, { "cell_type": "code", "execution_count": null, "id": "8e361fef-d54c-4359-bd57-b05bfca6d947", "metadata": { "tags": [] }, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "id": "24f3be9e-4f55-4882-861a-ff05b6b1597a", "metadata": {}, "source": [ "
\n", "

Task 4:

\n", " Name the CSV file that you will output, using the appropriate strings for site and start/end dates, e.g.:\n", " climo_alb_220101_221231.csv\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "3e9b6467-d1ec-4270-9a7d-a9b2473abb65", "metadata": { "tags": [] }, "outputs": [], "source": [ "csvFile = 'climo_alb_930310_930316.csv'" ] }, { "cell_type": "markdown", "id": "264a206c-4d53-44a6-b41f-90b6b2106a24", "metadata": {}, "source": [ "Write the `DataFrame` to the CSV file in your current directory." ] }, { "cell_type": "code", "execution_count": null, "id": "b20c6a9c-f4dc-48df-8e68-efc5728a7a3e", "metadata": { "tags": [] }, "outputs": [], "source": [ "df.to_csv(csvFile,index=False)" ] }, { "cell_type": "markdown", "id": "3016f317-e87f-40e5-897c-4c6f2b9eda64", "metadata": {}, "source": [ "## Reference:\n", "Based on Python 2.7 code as seen in section VI of the [RCC-ACIS webservices page](https://www.rcc-acis.org/docs_webservices.html)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 August 2022 Environment", "language": "python", "name": "aug22" }, "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.10.5" } }, "nbformat": 4, "nbformat_minor": 5 }