Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
languagepy
import requests
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

url = 'http://localhost:8080/FewsWebServices/rest/fewspiservice/v1/timeseries'
params = dict(
    documentVersion='1.23',
    documentFormat='PI_JSON',
    locationIds=['acnj'],
    parameterIds=['H.observed'],
    startTime='2017-06-20T00:00:00Z',
    endTime ='2017-06-22T00:00:15Z',
)

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']
        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('%B %d %H:%M')
fig, ax = plt.subplots()
ax.plot(x, y, label='H.obs')
ax.xaxis.set_major_formatter(dateFormatter)
ax.xaxis.set_major_locator(plt.MaxNLocator(5))
ax.yaxis.set_major_locator(plt.MaxNLocator(10))
ax.grid(True)

plt.show()


Running the python script will produce a plot like this: