...
Code Block | ||
---|---|---|
| ||
import requests import matplotlib.pyplot as plt import matplotlib.dates as mdates import datetime url = 'http://localhost:80808181/FewsWebServices/rest/fewspiservice/v1/timeseries?' params = dict( documentVersion='1.2324', documentFormat='PI_JSON', locationIds=['acnj63306260000'], parameterIds=['H.observedT.obs.mean'], moduleInstanceIds=['ImportObserved'], startTime='20172010-0601-20T0001T00:00:00Z', endTime ='20172016-0601-22T0001T00:00:15Z00Z', 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: else: y.append(None) dateFormatter = mdates.DateFormatter('%B %d %H:%M%Y') fig, ax = plt.subplots() ax.plot(x, y, label='H.obs') ax.xaxis.set_major_formatter(dateFormatter) ax.xaxis.set_major_locator(plt.MaxNLocator(57)) ax.yaxis.set_major_locator(plt.MaxNLocator(108)) ax.grid(True) ax.plot(x, y) plt.show() |
Running the python script will produce a plot like this:
...