odm2api sample

Largely from https://github.com/ODM2/ODM2PythonAPI/blob/master/Examples/Sample.py

  • 2/7/2016. Tested successfully with sfgeometry_em_1 branch, with my overhauls. Using odm2api_dev env.
  • 2/1 - 1/31. Errors with SamplingFeatures code, with latest odm2api from master (on 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.
  • 1/22-20,9/2016.

Emilio Mayorga


In [1]:
%matplotlib inline
import sys
import os
import matplotlib.pyplot as plt
from matplotlib import dates


/home/mayorga/miniconda/envs/odm2api_odm2channel/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

In [2]:
from odm2api.ODMconnection import dbconnection
from odm2api.ODM2.services.readService import *

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


USU36: Temperature

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.getAllAffiliations()
    for x in allaff:
        print x.PersonObj.PersonFirstName + ": " + str(x.OrganizationID)
except Exception as e:
    print "Unable to demo getAllAffiliations", e


Jeff Horsburgh

-------- Information about an Affiliation ---------
Unable to demo getAllAffiliations 'NoneType' object is not iterable

In [6]:
allaff = read.getAllAffiliations()
type(allaff)


Out[6]:
NoneType

SamplingFeatures tests


In [7]:
from odm2api.ODM2.models import SamplingFeatures
read._session.query(SamplingFeatures).filter_by(SamplingFeatureTypeCV='Site').all()


Out[7]:
[<SamplingFeatures('USU-LBR-Mendon', 'Little Bear River at Mendon Road near Mendon, Utah', 'None', '1345.0', 'POINT (41.718473 -111.946402)')>]

In [8]:
read.getSamplingFeaturesByType('Site')


Out[8]:
[<SamplingFeatures('USU-LBR-Mendon', 'Little Bear River at Mendon Road near Mendon, Utah', 'None', '1345.0', 'POINT (41.718473 -111.946402)')>]

In [9]:
# Get all of the SamplingFeatures from the database that are Sites
try:
    siteFeatures = read.getSamplingFeaturesByType('Site')
    numSites = len(siteFeatures)

    for x in siteFeatures:
        print x.SamplingFeatureCode + ": " + x.SamplingFeatureName
except Exception as e:
    print "Unable to demo getSamplingFeaturesByType", e


USU-LBR-Mendon: Little Bear River at Mendon Road near Mendon, Utah

In [10]:
read.getSamplingFeatures()


Out[10]:
[<SamplingFeatures('USU-LBR-Mendon', 'Little Bear River at Mendon Road near Mendon, Utah', 'None', '1345.0', 'POINT (41.718473 -111.946402)')>]

In [11]:
read.getSamplingFeatureByCode('USU-LBR-Mendon')


Out[11]:
<SamplingFeatures('USU-LBR-Mendon', 'Little Bear River at Mendon Road near Mendon, Utah', 'None', '1345.0', 'POINT (41.718473 -111.946402)')>

In [12]:
# Now get the SamplingFeature object for a SamplingFeature code
sf = read.getSamplingFeatureByCode('USU-LBR-Mendon')
# vars(sf)
# 1/31/2016: Leads to error with latest from odm2api master:
# "TypeError: vars() argument must have __dict__ attribute"

In [13]:
print sf, "\n"
print type(sf)
print type(sf.FeatureGeometry)


<SamplingFeatures('USU-LBR-Mendon', 'Little Bear River at Mendon Road near Mendon, Utah', 'None', '1345.0', 'POINT (41.718473 -111.946402)')> 

<class 'odm2api.ODM2.models.SamplingFeatures'>
<class 'geoalchemy.spatialite.SQLitePersistentSpatialElement'>

In [14]:
vars(sf.FeatureGeometry)


Out[14]:
{'desc': <WKBSpatialElement at 0x7f6cd3b82fd0; u'POINT (41.718473 -111.946402)'>}

In [15]:
sf.FeatureGeometry.__doc__


Out[15]:
'Represents a Geometry value as loaded from the database.'

In [16]:
sf.FeatureGeometry.geom_wkb, sf.FeatureGeometry.geom_wkt


Out[16]:
(u'POINT (41.718473 -111.946402)', None)

In [17]:
type(sf.shape()), sf.shape().wkt


Out[17]:
(shapely.geometry.point.Point, 'POINT (41.718473 -111.946402)')

Back to the rest of the demo


In [18]:
# Drill down and get objects linked by foreign keys
print "\n------------ Foreign Key Example --------- \n",
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


------------ Foreign Key Example --------- 
The FeatureAction object for the Result is:  <FeatureActions('1', '1', '1', )>
The Action object for the Result is:  <Actions('1', 'Observation', '2007-08-16 16:30:00', 'An observation action that generated a time series result.')>

The following are some of the attributes for the Action that created the Result: 
ActionTypeCV: Observation
ActionDescription: An observation action that generated a time series result.
BeginDateTime: 2007-08-16 16:30:00
EndDateTime: 2009-01-16 12:30:00
MethodName: Quality Control Level 1 Data Series created from raw QC Level 0 data using ODM Tools.
MethodDescription: Quality Control Level 1 Data Series created from raw QC Level 0 data using ODM Tools.

In [19]:
# Now get a particular Result using a ResultID
print "\n------- Example of Retrieving Attributes of a Time Series Result -------"
try:
    tsResult = read.getTimeSeriesResultByResultId(1)
    # Get the site information by drilling down
    sf_tsResult = tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj
    print(
        "Some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" +
        "ResultTypeCV: " + tsResult.ResultObj.ResultTypeCV + "\n" +
        # Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object
        "ProcessingLevel: " + tsResult.ResultObj.ProcessingLevelObj.Definition + "\n" +
        "SampledMedium: " + tsResult.ResultObj.SampledMediumCV + "\n" +
        # Get the variable information from the TimeSeriesResult's Variable object
        "Variable: " + tsResult.ResultObj.VariableObj.VariableCode + ": " + tsResult.ResultObj.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


------- Example of Retrieving Attributes of a Time Series Result -------
Some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): 
ResultTypeCV: Time series coverage
ProcessingLevel: Quality controlled data
SampledMedium: Surface Water
Variable: USU36: Temperature
AggregationStatistic: Average
Elevation_m: 1345.0
SamplingFeature: USU-LBR-Mendon - Little Bear River at Mendon Road near Mendon, Utah

In [20]:
tsResult = read.getTimeSeriesResultByResultId(1)

In [21]:
type(tsResult), vars(tsResult)


Out[21]:
(odm2api.ODM2.models.TimeSeriesResults,
 {'AggregationStatisticCV': u'Average',
  'IntendedTimeSpacing': 30.0,
  'IntendedTimeSpacingUnitsID': 102,
  'ResultID': 1,
  'ResultObj': <Results('1', '6af05c61-989d-11e5-9d19-6c4008bf018e', 'Time series coverage', '1', '24206')>,
  'SpatialReferenceID': None,
  'XLocation': None,
  'XLocationUnitsID': None,
  'YLocation': None,
  'YLocationUnitsID': None,
  'ZLocation': None,
  'ZLocationUnitsID': None,
  '_sa_instance_state': <sqlalchemy.orm.state.InstanceState at 0x7f6cd3afdf50>})

In [22]:
vars(tsResult.ResultObj)


Out[22]:
{'FeatureActionID': 1,
 'FeatureActionObj': <FeatureActions('1', '1', '1', )>,
 'ProcessingLevelID': 1,
 'ProcessingLevelObj': <ProcessingLevels('1', '1', 'Quality controlled data', 'Quality controlled data that have passed quality assurance procedures such as routine estimation of timing and sensor calibration or visual inspection and removal of obvious errors. An example is USGS published streamflow records following parsing through USGS quality control procedures.')>,
 'ResultDateTime': datetime.datetime(2015, 12, 1, 19, 35, 57, 609318),
 'ResultDateTimeUTCOffset': -7,
 'ResultID': 1,
 'ResultTypeCV': u'Time series coverage',
 'ResultUUID': u'6af05c61-989d-11e5-9d19-6c4008bf018e',
 'SampledMediumCV': u'Surface Water',
 'StatusCV': u'Unknown',
 'TaxonomicClassifierID': None,
 'UnitsID': 96,
 'ValidDateTime': None,
 'ValidDateTimeUTCOffset': None,
 'ValueCount': 24206,
 'VariableID': 1,
 'VariableObj': <Variables('1', 'USU36', 'Temperature')>,
 '_sa_instance_state': <sqlalchemy.orm.state.InstanceState at 0x7f6cd4505f10>}

In [23]:
# Get the values for a particular TimeSeriesResult
print "\n-------- Example of Retrieving Time Series Result Values ---------"

tsValues = read.getTimeSeriesResultValuesByResultId(1)  # Return type is a pandas dataframe
# Print a few Time Series Values to the console
# tsValues.set_index('ValueDateTime', inplace=True)
tsValues.head()


-------- Example of Retrieving Time Series Result Values ---------
<class 'odm2api.ODM2.models.TimeSeriesResultValues'> <TimeSeriesResultValues('-0.2683333', '2009-01-04 18:00:00', '30.0')>
Out[23]:
ValueID ResultID DataValue ValueDateTime ValueDateTimeUTCOffset CensorCodeCV QualityCodeCV TimeAggregationInterval TimeAggregationIntervalUnitsID
0 23641 1 -0.268333 2009-01-04 18:00:00 -7 nc Unknown 30.0 102
1 23617 1 -0.266667 2009-01-04 06:00:00 -7 nc Unknown 30.0 102
2 22922 1 -0.263333 2008-12-18 01:00:00 -7 nc Unknown 30.0 102
3 23616 1 -0.263333 2009-01-04 05:30:00 -7 nc Unknown 30.0 102
4 23618 1 -0.263333 2009-01-04 06:30:00 -7 nc Unknown 30.0 102

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.ResultObj.VariableObj.VariableNameCV + " at " + 
                        tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName,
                  ax=ax)
    ax.set_ylabel(tsResult.ResultObj.VariableObj.VariableNameCV + " (" + 
                  tsResult.ResultObj.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



In [ ]: