|
| 1 | +__author__ = 'stephanie' |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +from matplotlib import dates |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +from odm2api.ODMconnection import dbconnection |
| 11 | +from odm2api.ODM2.services.readService import * |
| 12 | +from odm2api.ODM2.services import CreateODM2 |
| 13 | +# Create a connection to the ODM2 database |
| 14 | +# ---------------------------------------- |
| 15 | + |
| 16 | + |
| 17 | +#connect to database |
| 18 | +# createconnection (dbtype, servername, dbname, username, password) |
| 19 | +# session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)#sqlite |
| 20 | +# session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql |
| 21 | +# session_factory= dbconnection.createConnection('mssql', "(local)", "LBRODM2", "ODM", "odm")#win MSSQL |
| 22 | +session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "", "ODM", "odm")#mac/linux MSSQL |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +#_session = session_factory.getSession() |
| 27 | +read = ReadODM2(session_factory) |
| 28 | +create =CreateODM2(session_factory) |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +# Run some basic sample queries. |
| 33 | +# ------------------------------ |
| 34 | +# Get all of the variables from the database and print their names to the console |
| 35 | +allVars = read.getVariables() |
| 36 | +print "\n-------- Information about Variables ---------" |
| 37 | +for x in allVars: |
| 38 | + print(x.VariableCode + ": " + x.VariableNameCV) |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +# Get all of the people from the database |
| 43 | +allPeople = read.getPeople() |
| 44 | +print "\n-------- Information about People ---------" |
| 45 | +for x in allPeople: |
| 46 | + print(x.PersonFirstName + " " + x.PersonLastName) |
| 47 | + |
| 48 | +try: |
| 49 | + |
| 50 | + print "\n-------- Information about an Affiliation ---------" |
| 51 | + allaff = read.getAffiliations() |
| 52 | + |
| 53 | + for x in allaff: |
| 54 | + print(x.PersonObj.PersonFirstName + ": " + str(x.OrganizationID)) |
| 55 | +except Exception as e: |
| 56 | + print("Unable to demo getAllAffiliations", e) |
| 57 | + |
| 58 | +# Get all of the SamplingFeatures from the database that are Sites |
| 59 | +try: |
| 60 | + print "\n-------- Information about Sites ---------" |
| 61 | + siteFeatures = read.getSamplingFeatures(type='Site') |
| 62 | + numSites = len(siteFeatures) |
| 63 | + |
| 64 | + for x in siteFeatures: |
| 65 | + |
| 66 | + print x.SamplingFeatureCode + ": " + str(x.SamplingFeatureName) + ", " + x.SamplingFeatureTypeCV |
| 67 | + |
| 68 | +except Exception as e: |
| 69 | + print("Unable to demo getSamplingFeaturesByType", e) |
| 70 | + |
| 71 | + |
| 72 | +# Now get the SamplingFeature object for a SamplingFeature code |
| 73 | +try: |
| 74 | + |
| 75 | + sf = read.getSamplingFeatures(code=['USU-LBR-Mendon'])[0] |
| 76 | + print "\n-------- Information about an individual SamplingFeature ---------" |
| 77 | + print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeature(code = x): \n" |
| 78 | + print "SamplingFeatureCode: " + sf.SamplingFeatureCode |
| 79 | + print "SamplingFeatureName: " + sf.SamplingFeatureName |
| 80 | + print "SamplingFeatureDescription: %s" % sf.SamplingFeatureDescription |
| 81 | + print "SamplingFeatureGeotypeCV: %s" % sf.SamplingFeatureGeotypeCV |
| 82 | + print "SamplingFeatureGeometry: %s" % sf.FeatureGeometryWKT |
| 83 | + print "Elevation_m: %s" % str(sf.Elevation_m) |
| 84 | + |
| 85 | +except Exception as e: |
| 86 | + print("Unable to demo getSamplingFeatureByCode: ", e) |
| 87 | + |
| 88 | +#add sampling feature |
| 89 | +print("\n------------ Create Sampling Feature --------- \n") |
| 90 | +try: |
| 91 | + # from odm2api.ODM2.models import SamplingFeatures |
| 92 | + session = session_factory.getSession() |
| 93 | + newsf = Sites(FeatureGeometryWKT = "POINT(-111.946 41.718)", Elevation_m=100, ElevationDatumCV=sf.ElevationDatumCV, |
| 94 | + SamplingFeatureCode= "TestSF",SamplingFeatureDescription = "this is a test to add Feature Geomotry", |
| 95 | + SamplingFeatureGeotypeCV= "Point", SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV,SamplingFeatureUUID= sf.SamplingFeatureUUID+"2", |
| 96 | + SiteTypeCV="cave", Latitude= "100", Longitude= "-100", SpatialReferenceID= 0) |
| 97 | + |
| 98 | + create.createSamplingFeature(newsf) |
| 99 | + #session.commit() |
| 100 | + print("new sampling feature added to database", newsf) |
| 101 | + |
| 102 | +except Exception as e : |
| 103 | + print("error adding a sampling feature: " + str(e)) |
| 104 | + |
| 105 | + |
| 106 | +# Drill down and get objects linked by foreign keys |
| 107 | +print("\n------------ Foreign Key Example --------- \n",) |
| 108 | +try: |
| 109 | + # Call getResults, but return only the first result |
| 110 | + firstResult = read.getResults()[0] |
| 111 | + print("The FeatureAction object for the Result is: ", firstResult.FeatureActionObj) |
| 112 | + print("The Action object for the Result is: ", firstResult.FeatureActionObj.ActionObj) |
| 113 | + print ("\nThe following are some of the attributes for the Action that created the Result: \n" + |
| 114 | + "ActionTypeCV: " + firstResult.FeatureActionObj.ActionObj.ActionTypeCV + "\n" + |
| 115 | + "ActionDescription: " + firstResult.FeatureActionObj.ActionObj.ActionDescription + "\n" + |
| 116 | + "BeginDateTime: " + str(firstResult.FeatureActionObj.ActionObj.BeginDateTime) + "\n" + |
| 117 | + "EndDateTime: " + str(firstResult.FeatureActionObj.ActionObj.EndDateTime) + "\n" + |
| 118 | + "MethodName: " + firstResult.FeatureActionObj.ActionObj.MethodObj.MethodName + "\n" + |
| 119 | + "MethodDescription: " + firstResult.FeatureActionObj.ActionObj.MethodObj.MethodDescription) |
| 120 | +except Exception as e: |
| 121 | + print("Unable to demo Foreign Key Example: ", e) |
| 122 | + |
| 123 | + |
| 124 | +# Now get a particular Result using a ResultID |
| 125 | +print("\n------- Example of Retrieving Attributes of a Time Series Result -------") |
| 126 | +try: |
| 127 | + tsResult = read.getResults(ids = [1])[0] |
| 128 | + print ( |
| 129 | + "The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" + |
| 130 | + "ResultTypeCV: " + tsResult.ResultTypeCV + "\n" + |
| 131 | + # Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object |
| 132 | + "ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" + |
| 133 | + "SampledMedium: " + tsResult.SampledMediumCV + "\n" + |
| 134 | + # Get the variable information from the TimeSeriesResult's Variable object |
| 135 | + "Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n" |
| 136 | + "AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" + |
| 137 | + "Elevation_m: " + str(sf.Elevation_m) + "\n" + |
| 138 | + # Get the site information by drilling down |
| 139 | + "SamplingFeature: " + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + |
| 140 | + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) |
| 141 | +except Exception as e: |
| 142 | + print("Unable to demo Example of retrieving Attributes of a time Series Result: ", e) |
| 143 | + |
| 144 | +# Get the values for a particular TimeSeriesResult |
| 145 | +print("\n-------- Example of Retrieving Time Series Result Values ---------") |
| 146 | + |
| 147 | +tsValues = read.getResultValues(resultid = 1) # Return type is a pandas datafram |
| 148 | + |
| 149 | +# Print a few Time Series Values to the console |
| 150 | +# tsValues.set_index('ValueDateTime', inplace=True) |
| 151 | +try: |
| 152 | + print(tsValues.head()) |
| 153 | + |
| 154 | +except Exception as e: |
| 155 | + print(e) |
| 156 | + |
| 157 | +# Plot the time series |
| 158 | + |
| 159 | +try: |
| 160 | + plt.figure() |
| 161 | + ax=tsValues.plot(x='ValueDateTime', y='DataValue') |
| 162 | + |
| 163 | + plt.show() |
| 164 | +except Exception as e: |
| 165 | + print("Unable to demo plotting of tsValues: ", e) |
0 commit comments