XMACIS2 Python API
Contents
XMACIS2 Python API¶
This notebook retreives climate data in tabular form from the Xmacis server. By default, the notebook will retrieve the following fields:
Daily max T
Daily min T
Daily mean T
Daily mean T departure from normal
Daily heating degree days
Daily cooling degree days
Daily precip
Daily snowfall
The user only needs to specify the site, and the beginning/end dates of the period of interest.
Task 1:
In the following markdown cell, enter your name, the site, and the beginning/end dates of your period of interest.
Name: Kevin Tyle Site: Albany NY Data period: 1993-03-10 - 1993-03-16
Imports¶
import urllib
import requests
import json
import pandas as pd
Task 2:
Specify the site (e.g., alb)
site = 'ALB'
Task 3:
Specify the beginning and end dates (e.g., 2022-01-01 and 2022-12-31)
sdate = '1993-03-10'
edate = '1993-03-16'
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
input_dict = {"elems":["maxt","mint","avgt",{"name":"avgt","normal":"departure"},"hdd","cdd","pcpn","snow","snwd"],"sid":site,"sdate":sdate,"edate":edate}
output_cols = ['DATE','MAX','MIN','AVG','DEP','HDD','CDD','PCP','SNW','DPT']
Construct the web query and retrieve the data
params = urllib.parse.urlencode({'params':json.dumps(input_dict)}).encode("utf-8")
req = urllib.request.Request('http://data.rcc-acis.org/StnData', params, {'Accept':'application/json'})
response = urllib.request.urlopen(req)
a = response.read()
z= json.loads(a)
b=z["data"]
Construct a Pandas DataFrame
from the downloaded data
df = pd.DataFrame(b,columns=output_cols)
Examine the DataFrame
df
DATE | MAX | MIN | AVG | DEP | HDD | CDD | PCP | SNW | DPT | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1993-03-10 | 38 | 25 | 31.5 | -2.1 | 33 | 0 | 0.16 | 3.1 | 2 |
1 | 1993-03-11 | 33 | 25 | 29.0 | -4.9 | 36 | 0 | 0.08 | 1.4 | 6 |
2 | 1993-03-12 | 26 | 10 | 18.0 | -16.2 | 47 | 0 | T | 0.2 | 5 |
3 | 1993-03-13 | 21 | 16 | 18.5 | -16.0 | 46 | 0 | 1.59 | 22.0 | M |
4 | 1993-03-14 | 20 | 11 | 15.5 | -19.4 | 49 | 0 | 0.24 | 4.6 | 28 |
5 | 1993-03-15 | 27 | 1 | 14.0 | -21.2 | 51 | 0 | 0.00 | 0.0 | 27 |
6 | 1993-03-16 | 43 | 17 | 30.0 | -5.6 | 35 | 0 | 0.00 | 0.0 | 27 |
Task 4:
Name the CSV file that you will output, using the appropriate strings for site and start/end dates, e.g.: climo_alb_220101_221231.csv
csvFile = 'climo_alb_930310_930316.csv'
Write the DataFrame
to the CSV file in your current directory.
df.to_csv(csvFile,index=False)
Reference:¶
Based on Python 2.7 code as seen in section VI of the RCC-ACIS webservices page