#!/usr/bin/env python # coding: utf-8 #

Use ODM2API to connect to an ODM2 database, create a dropdownlist to pick a time series and visualize them

#

1) First load standard python libraries

# In[36]: get_ipython().run_line_magic('matplotlib', 'inline') import sys import os import pprint import numpy import getpass import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter from IPython.display import display, HTML import ipywidgets as widgets #

2) Next load odm2api components we will use

# In[37]: from odm2api.ODMconnection import dbconnection import odm2api.ODM2.services.readService as odm2 from odm2api.ODM2.models import * #

3) now connect to the database and instantiate the read service

# In[38]: #print("Enter your ODM2 username") container = widgets.Box() # would be nice If I could get a container to hold the # user name and password prompt, getpass doesn't seem to play well with the other # widgets though username_text = widgets.Text( value='', placeholder='Enter username', description='', disabled=False) username_output_text = widgets.Text( value='', placeholder='Enter username', description='Username',disabled=False) database_address_text = widgets.Text( value='', placeholder='Enter database address', description='',disabled=False) database_address_output_text = widgets.Text( value='',placeholder='Enter database address', description='database address',disabled=False) database_text = widgets.Text( value='', placeholder='Enter database name', description='', disabled=False) database_output_text = widgets.Text( value='', placeholder='Enter database name', description='database name', disabled=False) def bind_username_to_output(sender): username_output_text.value = username_text.value def bind_database_address_to_output(sender): database_address_output_text.value = database_address_text.value def bind_database_to_output(sender): database_output_text.value = database_text.value def login(sender): #print('Database address : %s, Username: %s, database name: %s' % ( # database_address_text.value, username_text.value, database_text.value)) container.close() username_text.on_submit(bind_username_to_output) login_btn = widgets.Button(description="Login") login_btn.on_click(login) container.children = [username_text,database_address_text, database_text, login_btn] container # In[39]: print("enter your password: ") p = getpass.getpass() # In[40]: session_factory = dbconnection.createConnection('postgresql', database_address_text.value, database_text.value, username_text.value, p) read = odm2.ReadODM2(session_factory) #

4) Now get some time series results based on the action related to them

#

    a) In this case the result contains a featureaction and featureactions contain an action

#

5) Next loop through the results and create a string based representation of a result.

#

6) Create a dropdown list in order to pick a time series result

# In[41]: #featureaction = 1700 results = read.getResults(actionid=30) resultids = [] resultnames = {} def print_result_name(result): print result def on_change(change): print(change['new']) for r in results: #print(r.ResultID) resultids.append(str(r.ResultID)) detailr = read.getDetailedResultInfo(resultTypeCV = 'Time series coverage',resultID=r.ResultID) for detail in detailr: namestr = str(detail.samplingFeatureCode + "- " + detail.methodCode + "- "+ detail.variableCode + "- " + detail.unitsName) resultnames[namestr]= detail.resultID #print(detailr.Methods) print(resultids) resultWidget = widgets.Dropdown(options=resultnames) rwidget = widgets.interactive(print_result_name,result=resultWidget) rwidget.observe(on_change) display(rwidget) #

7) Use the selected value to retrieve the time series values

# In[34]: print(resultWidget.value) selectedResult = read.getDetailedResultInfo(resultTypeCV = 'Time series coverage',resultID=resultWidget.value) SUNAResultValues = read.getResultValues(resultid=resultWidget.value, starttime='2016-8-1', endtime= '2016-8-30') #

8) Format a plot and plot the selected time series

# In[35]: dateFmt = DateFormatter('%m-%d') fig, ax = plt.subplots() ax.plot_date(SUNAResultValues.ValueDateTime, SUNAResultValues.DataValue) ax.xaxis.set_major_formatter(dateFmt) plt.xticks(rotation=70) for result in selectedResult: plt.title(str(result.variableCode) + ' September 2016') plt.ylabel(result.unitsName) #

9) Use the numpy libaray to calculate some statistics about the time series

#

10) Dynamically create histogram bins based on the time series standard deviation and graph the histogram

# In[24]: print("maximum of values") print(numpy.amax(SUNAResultValues.DataValue)) print("minimum of values") print(numpy.amin(SUNAResultValues.DataValue)) print("mean value") varmean = numpy.mean(SUNAResultValues.DataValue) print(numpy.mean(SUNAResultValues.DataValue)) print("standard deviation of values") varstd = numpy.std(SUNAResultValues.DataValue) print(numpy.std(SUNAResultValues.DataValue)) print("standard deviation of values") varbins = [] for i in range(-6,6): varbins.append(varmean + varstd*i*.5) # histogram = numpy.histogram(wellResultValues.DataValue) #, bins=[101,102,103,104,105,106,107,108,109] print(varbins) plt.hist(SUNAResultValues.DataValue, bins=varbins) #[101,102,103,104,105,106,107,108,109] plt.show() # In[ ]: