The following python example will read the time series for a given location and parameter for a period of one day. The script has been tested using python 3.6 and requires the requests, matplotlib and datetime modules:
Code Block | ||
---|---|---|
| ||
import requests
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
url = 'http://localhost:8181/FewsWebServices/rest/fewspiservice/v1/timeseries?'
params = dict(
documentVersion='1.24',
documentFormat='PI_JSON',
locationIds=['63306260000'],
parameterIds=['T.obs.mean'],
moduleInstanceIds=['ImportObserved'],
startTime='2010-01-01T00:00:00Z',
endTime='2016-01-01T00:00:00Z',
showStatistics='true'
)
response = requests.get(url=url, params=params)
data = response.json()
timeSeries = data['timeSeries']
timeZoneValue = float(data['timeZone'])
timeZone = f"{timeZoneValue:05.2f}"
timeZone = timeZone.replace(".", "")
if timeZoneValue >= 0:
timeZone = "+" + timeZone
else:
timeZone = "-" + timeZone
x = []
y = []
# we assume only one series.
for timeSerie in timeSeries:
if 'events' in timeSerie:
header = timeSerie['header']
missingValue = header['missVal']
print(missingValue)
minValue = float(header['minValue'])
maxValue = float(header['maxValue'])
for event in timeSerie['events']:
x.append(datetime.datetime.strptime(event['date'] + ' ' + event['time'] + ' ' + timeZone, '%Y-%m-%d %H:%M:%S %z'))
if event['value'] != missingValue:
y.append(event['value'])
else:
y.append(None)
dateFormatter = mdates.DateFormatter('%Y')
fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(dateFormatter)
ax.xaxis.set_major_locator(plt.MaxNLocator(7))
ax.yaxis.set_major_locator(plt.MaxNLocator(8))
ax.grid(True)
ax.plot(x, y)
plt.show()
|
Running the python script will produce a plot like this: