OSIsoft Academic Hub: Data Access for Matlab and Python

This short document explains how to access your sensor data with Matlab and Python. A third section describe how the full OSIsoft PI Web API (in read-only mode) is exposed for the Academic Hub.

Matlab

It is very easy to access your Academic Hub data with Matlab. A single webread call with the right URL and options gives you back a Matlab table called data.

Copy the script below into your own code and modify the 5 parameters to access your data ([[1.]] up to [[5.]]).

Note that you need the reader account credentials (username and password) to access your data. Please ask your Hub administrator to get it.

clc,clear
%% Use this script to access lab data from the OSIsoft Academic Hub
% Formatted time-series data will be returned to the table called 'data'
%---------------------------------------------------

%% Parameters to modify for data access 
% You will need to modify these parameters for your equipment 
% and for the correct time range

% Time can be specified in 2 ways: absolute or relative 
% [[1a]]. Absolute time (25,000 first values)
StartTime = '2019-01-01T00:00:00Z'; % UTC
EndTime   = '2019-01-02T00:00:00Z';
% [[1b.]] Relative time, see also https://academic.osisoft.com/time-help
% Suffixes are 'd', 'h', 'm' and 's' for day, hour, minute and second 
% StartTime = '*-1d';
% EndTime = '*';
% [[2.]] Interpolation interval 
Interval = '1m'; % '1s' for 1-second interval; '1m' for 1-minute interval
% [[3.]] Path to where your sensor data is stored on the Academic Hub
EquipmentPath= 'osi_unitops1\hub_dan_signals';  
% [[4.]] University reader account credentials 
username = '<enter university reader username>'
password = '<enter university reader password>' 
%% End of parameters to modify for data access 

%% Data access portion - students should not need to edit this
% Controls the maximum value per attribute to return. Failure will occur 
% Any value > 50,000 for MaxCount will be capped at 50,000
MaxCount = '25000';

options = weboptions('Username', username,...
    'Password', password, 'ContentType', 'auto',...
    'Timeout', 30, 'CertificateFilename', '',...
    'HeaderFields', ["Accept-Encoding" ""]);

base_url='https://academicpi.azure-api.net/hub/api/';

% URL interpolated data with specified interval
interpolated_url = [ base_url, ...
    'Csv/ElementInterpolated',... % controller
    '?path=\\PIAF-ACAD\Classroom Data\Source Data\', EquipmentPath, ...
    '&startTime=', StartTime, '&endTime=', EndTime, ...
    '&interval=', Interval, '&maxCount=', MaxCount ];
tic
disp('Getting data...')
try
data = webread(interpolated_url, options); 
catch issue
    warning(issue.message)
    return
end
toc

Python

The easiest and most efficient way to get ready-to-use data for any kind of analytics is with those two modules, most often pre-installed with data science Python distribution like Anaconda:

Note that you need the reader account credentials (username and password) to access your data. Please ask your Hub administrator to get it.

import io 
import requests
import pandas as pd

# ----------- User parameters ------------
# Absolute time 
start_time = '2019-01-01T00:00:00Z'  # UTC
end_time = '2019-01-02T00:00:00Z'
# Relative time 
# start_time = '*-1d'
# end_time = '*'
interpolation_interval = '1m'
equipment_path = 'osi_unitops1\\hub_dan_signals'
max_count = '10000' 
# Credential
username = '<enter university reader username>'
password = '<enter university reader password>'

base_url = 'https://academicpi.azure-api.net/hub/api/' 
path_param = '?path=\\\\PIAF-ACAD\\Classroom Data\\Source Data\\{0}'
time_params = '&startTime={1}&endTime={2}&interval={3}&maxCount={4}'

interpolated_url_template = base_url + 'Csv/ElementInterpolated' + path_param + time_params  
interpolated_url = interpolated_url_template.format(equipment_path, start_time, end_time, interpolation_interval, max_count)


response = requests.get(interpolated_url, auth=(username, password))
if response.status_code != 200:
    print('# Request failed with code:', response.status_code, response.text)
else:
    df = pd.read_csv(io.StringIO(response.text))
    print(df)

PI Web API

For more complex applications which requires more than CSV formatted data, it is possible to access the PI Web API

https://<server hostname>/piwebapi/<command>

replaced by:

https://academicpi.azure-api.net/hub/api/<command>

The following code snippet shows how to get the result of https://<server>/piwebapi/system for the Hub:

import json
import requests

response = requests.get('https://academicpi.azure-api.net/hub/api/system', auth=(username, password))
if response.status_code != 200:
    print('# Request failed with code:', response.status_code, response.text)
else:
    print(json.dumps(response.json(), indent=4))     

which returns:

{
    "ProductTitle": "PI Web API 2017 R2",
    "ProductVersion": "1.10.0.475",
    "Links": {
        "Self": "https://academicpi.azure-api.net/hub/api/system",
        "CacheInstances": "https://academicpi.azure-api.net/hub/api/system/cacheinstances",
        "Configuration": "https://academicpi.azure-api.net/hub/api/system/configuration",
        "UserInfo": "https://academicpi.azure-api.net/hub/api/system/userinfo",
        "Versions": "https://academicpi.azure-api.net/hub/api/system/versions",
        "Status": "https://academicpi.azure-api.net/hub/api/system/status"
    }
}

For more information on PI Web API, please consult the Getting Started documentation


Last Update: 2/15/2019