Skip to content

Commit

Permalink
Merge Pull request #61 from ODM2/Dev1_0
Browse files Browse the repository at this point in the history
resolve issues #60, #50, #36, #34, #33, #32, #29
  • Loading branch information
Stephanie Reeder committed Apr 22, 2016
2 parents 4acec1f + 9ac80e4 commit 599b6cd
Show file tree
Hide file tree
Showing 18 changed files with 3,883 additions and 4,505 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ log
.idea

.DS_Store
.ipynb_checkpoints
93 changes: 39 additions & 54 deletions Examples/Sample.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
__author__ = 'stephanie'
import sys
import os



import matplotlib.pyplot as plt
from matplotlib import dates


#this will be removed when we can installthe api
# this_file = os.path.realpath(__file__)
# directory = os.path.dirname(os.path.dirname(this_file))
# print directory
# sys.path.insert(0, directory)

from odm2api.ODMconnection import dbconnection
from odm2api.ODM2.services.readService import *
from odm2api.ODM2.services import CreateODM2
# Create a connection to the ODM2 database
# ----------------------------------------


#connect to database
#createconnection (dbtype, servername, dbname, username, password)
#session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')
#session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)
session_factory = dbconnection.createConnection('sqlite', '/Users/denversmith/Downloads/ODM2.sqlite', 2.0)
# session_factory= dbconnection.createConnection('mssql')
# createconnection (dbtype, servername, dbname, username, password)
# session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)#sqlite
# session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql
# session_factory= dbconnection.createConnection('mssql', "(local)", "LBRODM2", "ODM", "odm")#win MSSQL
session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "", "ODM", "odm")#mac/linux MSSQL
# session_factory = dbconnection.createConnection('sqlite', '/Users/Stephanie/Desktop/TestODM2.db', 2.0)



#_session = session_factory.getSession()

read = ReadODM2(session_factory)
create =CreateODM2(session_factory)



# Run some basic sample queries.
# ------------------------------
# Get all of the variables from the database and print their names to the console
allVars = read.getVariables()

print ("\n-------- Information about Variables ---------")
for x in allVars:
print(x.VariableCode + ": " + x.VariableNameCV)



# Get all of the people from the database
allPeople = read.getPeople()

print ("\n-------- Information about People ---------")
for x in allPeople:
print(x.PersonFirstName + " " + x.PersonLastName)

try:
print("\n-------- Information about an Affiliation ---------")
allaff = read.getAllAffiliations()
allaff = read.getAffiliations()
for x in allaff:
print(x.PersonObj.PersonFirstName + ": " + str(x.OrganizationID))
except Exception as e:
print("Unable to demo getAllAffiliations", e)

# Get all of the SamplingFeatures from the database that are Sites
try:
siteFeatures = read.getSamplingFeaturesByType('Site')
print ("\n-------- Information about Sites ---------")
siteFeatures = read.getSamplingFeatures()
# siteFeatures = read.getSamplingFeatures(type='Site')
numSites = len(siteFeatures)

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


# Now get the SamplingFeature object for a SamplingFeature code
try:
sf = read.getSamplingFeatureByCode('USU-LBR-Mendon')
sf = read.getSamplingFeatures(code=['USU-LBR-Mendon'])[0]
print(sf)
print("\n-------- Information about an individual SamplingFeature ---------")
print("The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeatureByCode(): \n")
Expand All @@ -87,18 +85,14 @@
#add sampling feature
print("\n------------ Create Sampling Feature --------- \n")
try:
from odm2api.ODM2.models import SamplingFeatures
newsf = SamplingFeatures()
# from odm2api.ODM2.models import SamplingFeatures
session = session_factory.getSession()
newsf.FeatureGeometry = "POINT(-111.946 41.718)"
newsf.Elevation_m=100
newsf.ElevationDatumCV=sf.ElevationDatumCV
newsf.SamplingFeatureCode= "TestSF"
newsf.SamplingFeatureDescription = "this is a test to add Feature Geomotry"
newsf.SamplingFeatureGeotypeCV= "Point"
newsf.SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV
newsf.SamplingFeatureUUID= sf.SamplingFeatureUUID+"2"
session.add(newsf)
newsf = Sites(FeatureGeometryWKT = "POINT(-111.946 41.718)", Elevation_m=100, ElevationDatumCV=sf.ElevationDatumCV,
SamplingFeatureCode= "TestSF",SamplingFeatureDescription = "this is a test to add Feature Geomotry",
SamplingFeatureGeotypeCV= "Point", SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV,SamplingFeatureUUID= sf.SamplingFeatureUUID+"2",
SiteTypeCV="cave", Latitude= "100", Longitude= "-100", SpatialReferenceID= 0)

create.createSamplingFeature(newsf)
#session.commit()
print("new sampling feature added to database", newsf)

Expand All @@ -125,29 +119,29 @@


# Now get a particular Result using a ResultID
print("\n------- Example of Retrieving Attributes of a Time Series Result -------")
print("\n------- Example of Retrieving Attributes of a Result -------")
try:
tsResult = read.getTimeSeriesResultByResultId(1)
tsResult = read.getResults(ids = [1])[0]
print (
"The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" +
"ResultTypeCV: " + tsResult.ResultObj.ResultTypeCV + "\n" +
"The following are some of the attributes for the TimeSeriesResult retrieved using getResults(ids=[1]): \n" +
"ResultTypeCV: " + tsResult.ResultTypeCV + "\n" +
# Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object
"ProcessingLevel: " + tsResult.ResultObj.ProcessingLevelObj.Definition + "\n" +
"SampledMedium: " + tsResult.ResultObj.SampledMediumCV + "\n" +
"ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" +
"SampledMedium: " + tsResult.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" +
"Elevation_m: " + str(sf.Elevation_m) + "\n" +
"Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n"
#"AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" +

# Get the site information by drilling down
"SamplingFeature: " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " +
tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName)
"SamplingFeature: " + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " +
tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName)
except Exception as e:
print("Unable to demo Example of retrieving Attributes of a time Series Result: ", e)

# 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
tsValues = read.getResultValues(resultid = 1) # Return type is a pandas datafram

# Print a few Time Series Values to the console
# tsValues.set_index('ValueDateTime', inplace=True)
Expand All @@ -159,18 +153,9 @@
# 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)
plt.figure()
ax=tsValues.plot(x='ValueDateTime', y='DataValue')

plt.show()
except Exception as e:
print("Unable to demo plotting of tsValues: ", e)
132 changes: 0 additions & 132 deletions odm2api/ODM2/apiCustomType.py

This file was deleted.

Loading

0 comments on commit 599b6cd

Please sign in to comment.