|
1 | 1 | __author__ = 'stephanie'
|
2 |
| -import sys |
3 |
| -import os |
4 | 2 |
|
5 | 3 |
|
6 |
| -import matplotlib.pyplot as plt |
7 |
| -from matplotlib import dates |
| 4 | + |
| 5 | +# import matplotlib.pyplot as plt |
| 6 | + |
8 | 7 |
|
9 | 8 |
|
10 |
| -#this will be removed when we can installthe api |
11 |
| -# this_file = os.path.realpath(__file__) |
12 |
| -# directory = os.path.dirname(os.path.dirname(this_file)) |
13 |
| -# print directory |
14 |
| -# sys.path.insert(0, directory) |
15 | 9 |
|
16 | 10 | from odm2api.ODMconnection import dbconnection
|
17 | 11 | from odm2api.ODM2.services.readService import *
|
| 12 | +from odm2api.ODM2.services import CreateODM2 |
18 | 13 | # Create a connection to the ODM2 database
|
19 | 14 | # ----------------------------------------
|
20 | 15 |
|
21 | 16 |
|
22 | 17 | #connect to database
|
23 |
| -#createconnection (dbtype, servername, dbname, username, password) |
24 |
| -#session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm') |
25 |
| -#session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0) |
26 |
| -session_factory = dbconnection.createConnection('sqlite', '/Users/denversmith/Downloads/ODM2.sqlite', 2.0) |
27 |
| -# session_factory= dbconnection.createConnection('mssql') |
| 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 | +# session_factory = dbconnection.createConnection('sqlite', '/Users/stephanie/DEV/ODM2/usecases/WOF_to_ODM2/ODM2.sqlite', 2.0) |
28 | 24 |
|
29 | 25 |
|
30 | 26 |
|
31 |
| -#_session = session_factory.getSession() |
32 | 27 |
|
| 28 | +#_session = session_factory.getSession() |
33 | 29 | read = ReadODM2(session_factory)
|
| 30 | +create =CreateODM2(session_factory) |
34 | 31 |
|
35 | 32 |
|
36 | 33 |
|
37 | 34 | # Run some basic sample queries.
|
38 | 35 | # ------------------------------
|
39 | 36 | # Get all of the variables from the database and print their names to the console
|
40 | 37 | allVars = read.getVariables()
|
41 |
| - |
| 38 | +print ("\n-------- Information about Variables ---------") |
42 | 39 | for x in allVars:
|
43 | 40 | print(x.VariableCode + ": " + x.VariableNameCV)
|
44 | 41 |
|
45 | 42 |
|
46 | 43 |
|
47 | 44 | # Get all of the people from the database
|
48 | 45 | allPeople = read.getPeople()
|
49 |
| - |
| 46 | +print ("\n-------- Information about People ---------") |
50 | 47 | for x in allPeople:
|
51 | 48 | print(x.PersonFirstName + " " + x.PersonLastName)
|
52 | 49 |
|
53 | 50 | try:
|
54 | 51 | print("\n-------- Information about an Affiliation ---------")
|
55 |
| - allaff = read.getAllAffiliations() |
| 52 | + allaff = read.getAffiliations() |
56 | 53 | for x in allaff:
|
57 | 54 | print(x.PersonObj.PersonFirstName + ": " + str(x.OrganizationID))
|
58 | 55 | except Exception as e:
|
59 | 56 | print("Unable to demo getAllAffiliations", e)
|
60 | 57 |
|
61 | 58 | # Get all of the SamplingFeatures from the database that are Sites
|
62 | 59 | try:
|
63 |
| - siteFeatures = read.getSamplingFeaturesByType('Site') |
| 60 | + print ("\n-------- Information about Sites ---------") |
| 61 | + siteFeatures = read.getSamplingFeatures() |
| 62 | + # siteFeatures = read.getSamplingFeatures(type='Site') |
64 | 63 | numSites = len(siteFeatures)
|
65 |
| - |
| 64 | + print ("Successful query") |
66 | 65 | for x in siteFeatures:
|
67 |
| - print(x.SamplingFeatureCode + ": " + x.SamplingFeatureName) |
| 66 | + print(x.SamplingFeatureCode + ": " + x.SamplingFeatureTypeCV ) |
68 | 67 | except Exception as e:
|
69 | 68 | print("Unable to demo getSamplingFeaturesByType", e)
|
70 | 69 |
|
71 | 70 |
|
72 | 71 | # Now get the SamplingFeature object for a SamplingFeature code
|
73 | 72 | try:
|
74 |
| - sf = read.getSamplingFeatureByCode('USU-LBR-Mendon') |
| 73 | + sf = read.getSamplingFeatures(codes=['USU-LBR-Mendon'])[0] |
75 | 74 | print(sf)
|
76 | 75 | print("\n-------- Information about an individual SamplingFeature ---------")
|
77 | 76 | print("The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeatureByCode(): \n")
|
|
87 | 86 | #add sampling feature
|
88 | 87 | print("\n------------ Create Sampling Feature --------- \n")
|
89 | 88 | try:
|
90 |
| - from odm2api.ODM2.models import SamplingFeatures |
91 |
| - newsf = SamplingFeatures() |
| 89 | + # from odm2api.ODM2.models import SamplingFeatures |
92 | 90 | session = session_factory.getSession()
|
93 |
| - newsf.FeatureGeometry = "POINT(-111.946 41.718)" |
94 |
| - newsf.Elevation_m=100 |
95 |
| - newsf.ElevationDatumCV=sf.ElevationDatumCV |
96 |
| - newsf.SamplingFeatureCode= "TestSF" |
97 |
| - newsf.SamplingFeatureDescription = "this is a test to add Feature Geomotry" |
98 |
| - newsf.SamplingFeatureGeotypeCV= "Point" |
99 |
| - newsf.SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV |
100 |
| - newsf.SamplingFeatureUUID= sf.SamplingFeatureUUID+"2" |
101 |
| - session.add(newsf) |
| 91 | + newsf = Sites(FeatureGeometryWKT = "POINT(-111.946 41.718)", Elevation_m=100, ElevationDatumCV=sf.ElevationDatumCV, |
| 92 | + SamplingFeatureCode= "TestSF",SamplingFeatureDescription = "this is a test in sample.py", |
| 93 | + SamplingFeatureGeotypeCV= "Point", SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV,SamplingFeatureUUID= sf.SamplingFeatureUUID+"2", |
| 94 | + SiteTypeCV="cave", Latitude= "100", Longitude= "-100", SpatialReferenceID= 0) |
| 95 | + |
| 96 | + c=create.createSamplingFeature(newsf) |
102 | 97 | #session.commit()
|
103 |
| - print("new sampling feature added to database", newsf) |
| 98 | + print("new sampling feature added to database", c) |
104 | 99 |
|
105 | 100 | except Exception as e :
|
106 | 101 | print("error adding a sampling feature: " + str(e))
|
|
125 | 120 |
|
126 | 121 |
|
127 | 122 | # Now get a particular Result using a ResultID
|
128 |
| -print("\n------- Example of Retrieving Attributes of a Time Series Result -------") |
| 123 | +print("\n------- Example of Retrieving Attributes of a Result -------") |
129 | 124 | try:
|
130 |
| - tsResult = read.getTimeSeriesResultByResultId(1) |
| 125 | + tsResult = read.getResults(ids = [1])[0] |
131 | 126 | print (
|
132 |
| - "The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" + |
133 |
| - "ResultTypeCV: " + tsResult.ResultObj.ResultTypeCV + "\n" + |
| 127 | + "The following are some of the attributes for the TimeSeriesResult retrieved using getResults(ids=[1]): \n" + |
| 128 | + "ResultTypeCV: " + tsResult.ResultTypeCV + "\n" + |
134 | 129 | # Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object
|
135 |
| - "ProcessingLevel: " + tsResult.ResultObj.ProcessingLevelObj.Definition + "\n" + |
136 |
| - "SampledMedium: " + tsResult.ResultObj.SampledMediumCV + "\n" + |
| 130 | + "ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" + |
| 131 | + "SampledMedium: " + tsResult.SampledMediumCV + "\n" + |
137 | 132 | # Get the variable information from the TimeSeriesResult's Variable object
|
138 |
| - "Variable: " + tsResult.ResultObj.VariableObj.VariableCode + ": " + tsResult.ResultObj.VariableObj.VariableNameCV + "\n" |
139 |
| - "AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" + |
140 |
| - "Elevation_m: " + str(sf.Elevation_m) + "\n" + |
| 133 | + "Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n" |
| 134 | + #"AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" + |
| 135 | + |
141 | 136 | # Get the site information by drilling down
|
142 |
| - "SamplingFeature: " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + |
143 |
| - tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) |
| 137 | + "SamplingFeature: " + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + |
| 138 | + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) |
144 | 139 | except Exception as e:
|
145 | 140 | print("Unable to demo Example of retrieving Attributes of a time Series Result: ", e)
|
146 | 141 |
|
147 | 142 | # Get the values for a particular TimeSeriesResult
|
148 | 143 | print("\n-------- Example of Retrieving Time Series Result Values ---------")
|
149 | 144 |
|
150 |
| -tsValues = read.getTimeSeriesResultValuesByResultId(1) # Return type is a pandas dataframe |
| 145 | +tsValues = read.getResultValues(resultid = 1) # Return type is a pandas datafram |
151 | 146 |
|
152 | 147 | # Print a few Time Series Values to the console
|
153 | 148 | # tsValues.set_index('ValueDateTime', inplace=True)
|
|
159 | 154 | # Plot the time series
|
160 | 155 |
|
161 | 156 | try:
|
162 |
| - fig = plt.figure() |
163 |
| - ax = fig.add_subplot(111) |
164 |
| - tsValues.plot(x='ValueDateTime', y='DataValue', kind='line', |
165 |
| - title=tsResult.ResultObj.VariableObj.VariableNameCV + " at " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName, |
166 |
| - ax=ax) |
167 |
| - ax.set_ylabel(tsResult.ResultObj.VariableObj.VariableNameCV + " (" + tsResult.ResultObj.UnitsObj.UnitsAbbreviation + ")") |
168 |
| - ax.set_xlabel("Date/Time") |
169 |
| - ax.xaxis.set_minor_locator(dates.MonthLocator()) |
170 |
| - ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) |
171 |
| - ax.xaxis.set_major_locator(dates.YearLocator()) |
172 |
| - ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y')) |
173 |
| - ax.grid(True) |
| 157 | + plt.figure() |
| 158 | + ax=tsValues.plot(x='ValueDateTime', y='DataValue') |
| 159 | + |
174 | 160 | plt.show()
|
175 | 161 | except Exception as e:
|
176 | 162 | print("Unable to demo plotting of tsValues: ", e)
|
0 commit comments