Last active
June 8, 2021 07:58
-
-
Save diodon/ce43032cd6ee09cc649a65c92fa73791 to your computer and use it in GitHub Desktop.
Get latest readings from an AIMS weather station, provided the station ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import warnings | |
| warnings.filterwarnings('ignore') | |
| import json | |
| import requests | |
| import numpy as np | |
| import prettytable | |
| from sparklines import sparklines | |
| def WSsummary(site): | |
| ''' | |
| Produce a summary of the recent data from a AIMS weather station | |
| :param site: site ID | |
| :return: nothing | |
| ''' | |
| try: | |
| AIMSurl = "https://api.aims.gov.au/weather/station/" + str(site) | |
| WSjson = json.loads(requests.get(AIMSurl).text) | |
| print("Station ID: {id}".format(id=WSjson['site_id'])) | |
| print("Station Name: {stName}".format(stName=WSjson['site_name'])) | |
| print("Station location: {lon}LON, {lat}LAT".format(lon=WSjson['longitude'], lat=WSjson['latitude'])) | |
| print("Station metadata record: {metadata}".format(metadata=WSjson['metadata'])) | |
| if WSjson['status']['online'] == 'true': | |
| WSstatus = 'ONLINE' | |
| else: | |
| WSstatus = 'OFFLINE' | |
| WSstatusMessage = WSjson['status']['message'] | |
| print("Current Status: {status}: {statusText}".format(status=WSstatus, statusText=WSstatusMessage)) | |
| parameterList = list(WSjson['series'].keys()) | |
| tbl = prettytable.PrettyTable() | |
| for param in parameterList: | |
| paramName = WSjson['series'][param]['parameterName'] | |
| minutesAgo = WSjson['series'][param]['minutesAgo'] | |
| paramUnits = WSjson['series'][param]['uomSymbol'] | |
| if len(WSjson['series'][param]['data12Hours']) > 0: | |
| WSdata = WSjson['series'][param]['data12Hours'] | |
| plotType = 'Hourly values' | |
| elif len(WSjson['series'][param]['data7Days']) > 0: | |
| WSdata = WSjson['series'][param]['data7Days'] | |
| plotType = 'Daily averages' | |
| else: | |
| print("NO DATA AVAILABLE") | |
| return | |
| WSdate = [] | |
| WSvalue = [] | |
| for item in WSdata: | |
| WSdate.append(item['date']) | |
| WSvalue.append(item['qc']) | |
| paramLastDate = WSdate[-1] | |
| paramLastValue = WSvalue[-1] | |
| paramPlot = [] | |
| for v in sparklines(WSvalue): | |
| paramPlot.append(v) | |
| paramPlot = paramPlot[0] | |
| #if plotType=="Daily averages": | |
| paramPlot = paramPlot[len(paramPlot)-85:] | |
| tbl.add_row([paramName, paramUnits, minutesAgo, paramLastDate, paramLastValue, paramPlot]) | |
| tbl.field_names = ['Parameter', 'Units', 'Last Read (min ago)', 'Last Date', 'Last Value', 'Plot ' + plotType] | |
| print(tbl) | |
| except Exception as e: | |
| print('ERROR: site ID not found') | |
| return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment