Largely from https://github.com/ODM2/ODM2PythonAPI/blob/master/Examples/Sample.py
odm2
conda channel, based on the new 0.5.0-alpha
odm2api release. See my odm2api_odm2channel
env. Ran into problems b/c the SQLite database needed to be updated to have a SamplingFeature.FeatureGeometryWKT
field; so I added and populated it manually with SQLite Manager
.sfgeometry_em_1
branch, with my overhauls. Using odm2api_dev
env.odm2api_jan31test
). The code also fails the same way with the odm2api
env, but it does still run fine with the odm2api_jan21
env! I'm investigating the differences between those two envs.Emilio Mayorga
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import dates
In [2]:
from odm2api.ODMconnection import dbconnection
from odm2api.ODM2.services.readService import ReadODM2
In [3]:
# Create a connection to the ODM2 database
# ----------------------------------------
odm2db_fpth = '/home/mayorga/Desktop/TylerYeats/ODM2-LittleBear1.sqlite'
session_factory = dbconnection.createConnection('sqlite', odm2db_fpth, 2.0)
read = ReadODM2(session_factory)
In [4]:
# Run some basic sample queries.
# ------------------------------
# Get all of the variables from the database and print their names to the console
allVars = read.getVariables()
for x in allVars:
print x.VariableCode + ": " + x.VariableNameCV
In [5]:
# Get all of the people from the database
allPeople = read.getPeople()
for x in allPeople:
print x.PersonFirstName + " " + x.PersonLastName
try:
print "\n-------- Information about an Affiliation ---------"
allaff = read.getAffiliations()
for x in allaff:
print x.PersonObj.PersonFirstName + ": " + str(x.OrganizationID)
except Exception as e:
print "Unable to demo getAllAffiliations", e
In [6]:
allaff = read.getAffiliations()
type(allaff)
Out[6]:
In [7]:
# from odm2api.ODM2.models import SamplingFeatures
# read._session.query(SamplingFeatures).filter_by(SamplingFeatureTypeCV='Site').all()
In [8]:
# Get all of the SamplingFeatures from the database that are Sites
try:
siteFeatures = read.getSamplingFeatures(type='Site')
numSites = len(siteFeatures)
for x in siteFeatures:
print x.SamplingFeatureCode + ": " + x.SamplingFeatureName
except Exception as e:
print "Unable to demo getSamplingFeatures(type='Site')", e
In [9]:
read.getSamplingFeatures()
Out[9]:
In [10]:
read.getSamplingFeatures(codes=['USU-LBR-Mendon'])
Out[10]:
In [11]:
# Now get the SamplingFeature object for a SamplingFeature code
sf_lst = read.getSamplingFeatures(codes=['USU-LBR-Mendon'])
vars(sf_lst[0])
Out[11]:
In [12]:
sf = sf_lst[0]
In [13]:
print sf, "\n"
print type(sf)
print type(sf.FeatureGeometryWKT), sf.FeatureGeometryWKT
print type(sf.FeatureGeometry)
In [14]:
vars(sf.FeatureGeometry)
Out[14]:
In [15]:
sf.FeatureGeometry.__doc__
Out[15]:
In [16]:
sf.FeatureGeometry.geom_wkb, sf.FeatureGeometry.geom_wkt
Out[16]:
In [17]:
# 4/25/2016: Don't know why the shape is listed 4 times ...
type(sf.shape()), sf.shape().wkt
Out[17]:
In [18]:
read.getResults()
Out[18]:
In [19]:
firstResult = read.getResults()[0]
firstResult.FeatureActionObj.ActionObj
Out[19]:
In [20]:
try:
# Call getResults, but return only the first result
firstResult = read.getResults()[0]
action_firstResult = firstResult.FeatureActionObj.ActionObj
print "The FeatureAction object for the Result is: ", firstResult.FeatureActionObj
print "The Action object for the Result is: ", action_firstResult
print ("\nThe following are some of the attributes for the Action that created the Result: \n" +
"ActionTypeCV: " + action_firstResult.ActionTypeCV + "\n" +
"ActionDescription: " + action_firstResult.ActionDescription + "\n" +
"BeginDateTime: " + str(action_firstResult.BeginDateTime) + "\n" +
"EndDateTime: " + str(action_firstResult.EndDateTime) + "\n" +
"MethodName: " + action_firstResult.MethodObj.MethodName + "\n" +
"MethodDescription: " + action_firstResult.MethodObj.MethodDescription)
except Exception as e:
print "Unable to demo Foreign Key Example: ", e
In [21]:
tsResult = read.getResults(ids=[1])[0]
type(tsResult), vars(tsResult)
Out[21]:
Why are ProcessingLevelObj
, VariableObj
and UnitsObj
objects not shown in the above vars()
listing!? They are actually available, as demonstrated in much of the code below.
In [22]:
try:
tsResult = read.getResults(ids=[1])[0]
# Get the site information by drilling down
sf_tsResult = tsResult.FeatureActionObj.SamplingFeatureObj
print(
"Some of the attributes for the TimeSeriesResult retrieved using getResults(ids=[]): \n" +
"ResultTypeCV: " + tsResult.ResultTypeCV + "\n" +
# Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object
"ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" +
"SampledMedium: " + tsResult.SampledMediumCV + "\n" +
# Get the variable information from the TimeSeriesResult's Variable object
"Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n" +
"AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" +
# Get the site information by drilling down
"Elevation_m: " + str(sf_tsResult.Elevation_m) + "\n" +
"SamplingFeature: " + sf_tsResult.SamplingFeatureCode + " - " +
sf_tsResult.SamplingFeatureName)
except Exception as e:
print "Unable to demo Example of retrieving Attributes of a time Series Result: ", e
In [23]:
# Get the values for a particular TimeSeriesResult
tsValues = read.getResultValues(resultid=1) # Return type is a pandas dataframe
# Print a few Time Series Values to the console
# tsValues.set_index('ValueDateTime', inplace=True)
tsValues.head()
Out[23]:
In [24]:
# Plot the time series
try:
fig = plt.figure()
ax = fig.add_subplot(111)
tsValues.plot(x='ValueDateTime', y='DataValue', kind='line',
title=tsResult.VariableObj.VariableNameCV + " at " +
tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName,
ax=ax)
ax.set_ylabel(tsResult.VariableObj.VariableNameCV + " (" +
tsResult.UnitsObj.UnitsAbbreviation + ")")
ax.set_xlabel("Date/Time")
ax.xaxis.set_minor_locator(dates.MonthLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
ax.xaxis.set_major_locator(dates.YearLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y'))
ax.grid(True)
except Exception as e:
print "Unable to demo plotting of tsValues: ", e