Request example with Python 3 - BWS500 - BWS Cloud Companion

BWS Cloud Companion API Reference

Document code
M212639EN
Revision
E
Language
English
Product
BWS500
BWS Cloud Companion
Document type
User guide

The following example retrieves data from the API and prints the response.

xml-api-request.py file

#! /usr/bin/python3

import requests
import pandas as pd
import xml.etree.ElementTree as et
from datetime import datetime, timedelta
from collections import defaultdict

def wxBeaconApiRequest(startUTC, endUTC, serial, sensor, apikey):

    endpoint = 'https://wxbeacon.vaisala.com/api/xml/'
    query = '?d='+serial+'&s='+sensor+'&k='+apikey+'&t0='+startUTC+'&t1='+endUTC
    URL = endpoint + query
    observations = et.fromstring(requests.get(URL).content)[1]

    d = defaultdict(dict)
    for obs in observations:
        timestamp = obs.find("timestamp").text
        type = obs.find("type").text
        value = obs.find("value").text
        d[timestamp][type] = float(value)

    df = pd.DataFrame.from_dict(d, orient='index')
    # convert index to datetime objects
    df.index = pd.to_datetime(df.index)

    return df

# get last 24h data
utcNow       = datetime.utcnow()
startTimeUTC = (utcNow-timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%S')
endTimeUTC   = utcNow.strftime('%Y-%m-%dT%H:%M:%S')
serial       = 'U1840274'
sensor       = 'WXT530-U1740774'
apikey       = '99aace683508494f9cf37acad32bdb38'

df = wxBeaconApiRequest(startTimeUTC, endTimeUTC, serial, sensor, apikey)

print(df)

 

 

 

Example when run in command line:

$ python3 xml-api-requests.py 
                     Rain intensity  Wind direction  Air pressure  ...  Wind speed  Relative humidity  Rain accumula
tion                                                                                                               
2021-06-08 10:09:00             2.8           186.4        1024.1  ...         7.7               64.0           17.2
7510                                                                                                               
2021-06-08 10:10:00             4.8           126.2        1024.1  ...         6.1               63.9           17.3
5588                                                                                                               
2021-06-08 10:11:00             6.2            72.1        1024.1  ...         6.1               63.7           17.4
5996                                                                                                               
2021-06-08 10:12:00             6.9            30.3        1024.1  ...         3.7               63.6           17.5
7555                                                                                                               
2021-06-08 10:13:00             6.9             5.6        1024.1  ...         3.3               63.5           17.6
9001                                                                                                               
...                             ...             ...           ...  ...         ...                ...               
 ...                                                                                                               
2021-06-09 10:02:00             6.6            53.8        1004.3  ...         5.3               64.8           17.7
2989                                                                                                               
2021-06-09 10:03:00             5.5           103.7        1004.3  ...         6.2               64.7           17.8
2120                                                                                                               
2021-06-09 10:04:00             3.7           162.3        1004.3  ...         6.7               64.5           17.8
8278                                                                                                               
2021-06-09 10:05:00             1.4           222.9        1004.2  ...         6.4               64.4           17.9
0574                                                                                                               
2021-06-09 10:06:00             0.0           278.7        1004.2  ...         3.7               64.3           17.9
0574                                                                                                               

[1438 rows x 8 columns]

Linear correction values set in the UI apply to all response values.