|
1 | 1 | __author__ = 'stephanie'
|
2 |
| -import sys |
3 |
| -import os |
| 2 | + |
4 | 3 |
|
5 | 4 |
|
6 | 5 | import matplotlib.pyplot as plt
|
7 | 6 | from matplotlib import dates
|
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('mssql', "(local)", "LBRODM2", "ODM", "odm") |
27 |
| -session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "LBRODM2", "ODM", "odm") |
| 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 |
28 | 23 |
|
29 | 24 |
|
30 | 25 |
|
31 | 26 | #_session = session_factory.getSession()
|
32 |
| - |
33 | 27 | read = ReadODM2(session_factory)
|
| 28 | +create =CreateODM2(session_factory) |
34 | 29 |
|
35 | 30 |
|
36 | 31 |
|
|
64 | 59 | numSites = len(siteFeatures)
|
65 | 60 |
|
66 | 61 | for x in siteFeatures:
|
67 |
| - print x.SamplingFeatureCode + ": " + x.SamplingFeatureName |
| 62 | + print x.SamplingFeatureCode + ": " + str(x.SamplingFeatureName) + ", " + x.SamplingFeatureTypeCV |
68 | 63 | except Exception as e:
|
69 | 64 | print "Unable to demo getSamplingFeaturesByType", e
|
70 | 65 |
|
71 | 66 |
|
72 | 67 | # Now get the SamplingFeature object for a SamplingFeature code
|
73 | 68 | try:
|
74 |
| - sf = read.getSamplingFeatures(code='USU-LBR-Mendon') |
| 69 | + sf = read.getSamplingFeatures(code='USU-LBR-Mendon')[0] |
75 | 70 | print sf
|
76 | 71 | print "\n-------- Information about an individual SamplingFeature ---------"
|
77 |
| - print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeatureByCode(): \n" |
| 72 | + print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeature(code = x): \n" |
78 | 73 | print "SamplingFeatureCode: " + sf.SamplingFeatureCode
|
79 | 74 | print "SamplingFeatureName: " + sf.SamplingFeatureName
|
80 | 75 | print "SamplingFeatureDescription: %s" % sf.SamplingFeatureDescription
|
81 | 76 | print "SamplingFeatureGeotypeCV: %s" % sf.SamplingFeatureGeotypeCV
|
82 |
| - print "SamplingFeatureGeometry: %s" % sf.FeatureGeometry |
| 77 | + print "SamplingFeatureGeometry: %s" % sf.FeatureGeometryWKT |
83 | 78 | print "Elevation_m: %s" % str(sf.Elevation_m)
|
84 | 79 | except Exception as e:
|
85 | 80 | print "Unable to demo getSamplingFeatureByCode: ", e
|
86 | 81 |
|
87 | 82 | #add sampling feature
|
88 | 83 | print "\n------------ Create Sampling Feature --------- \n",
|
89 | 84 | try:
|
90 |
| - from odm2api.ODM2.models import SamplingFeatures |
91 |
| - newsf = SamplingFeatures() |
| 85 | + # from odm2api.ODM2.models import SamplingFeatures |
92 | 86 | 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) |
| 87 | + newsf = Sites(FeatureGeometryWKT = "POINT(-111.946 41.718)", Elevation_m=100, ElevationDatumCV=sf.ElevationDatumCV, |
| 88 | + SamplingFeatureCode= "TestSF",SamplingFeatureDescription = "this is a test to add Feature Geomotry", |
| 89 | + SamplingFeatureGeotypeCV= "Point", SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV,SamplingFeatureUUID= sf.SamplingFeatureUUID+"2", |
| 90 | + SiteTypeCV="cave", Latitude= "100", Longitude= "-100", SpatialReferenceID= 0) |
| 91 | + |
| 92 | + create.createSamplingFeature(newsf) |
102 | 93 | #session.commit()
|
103 | 94 | print "new sampling feature added to database", newsf
|
104 | 95 |
|
|
127 | 118 | # Now get a particular Result using a ResultID
|
128 | 119 | print "\n------- Example of Retrieving Attributes of a Time Series Result -------"
|
129 | 120 | try:
|
130 |
| - tsResult = read.getTimeSeriesResultByResultId(1) |
| 121 | + tsResult = read.getResults(id = 1)[0] |
131 | 122 | print (
|
132 | 123 | "The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" +
|
133 |
| - "ResultTypeCV: " + tsResult.ResultObj.ResultTypeCV + "\n" + |
| 124 | + "ResultTypeCV: " + tsResult.ResultTypeCV + "\n" + |
134 | 125 | # Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object
|
135 |
| - "ProcessingLevel: " + tsResult.ResultObj.ProcessingLevelObj.Definition + "\n" + |
136 |
| - "SampledMedium: " + tsResult.ResultObj.SampledMediumCV + "\n" + |
| 126 | + "ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" + |
| 127 | + "SampledMedium: " + tsResult.SampledMediumCV + "\n" + |
137 | 128 | # Get the variable information from the TimeSeriesResult's Variable object
|
138 |
| - "Variable: " + tsResult.ResultObj.VariableObj.VariableCode + ": " + tsResult.ResultObj.VariableObj.VariableNameCV + "\n" |
| 129 | + "Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n" |
139 | 130 | "AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" +
|
140 | 131 | "Elevation_m: " + str(sf.Elevation_m) + "\n" +
|
141 | 132 | # Get the site information by drilling down
|
142 |
| - "SamplingFeature: " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + |
143 |
| - tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) |
| 133 | + "SamplingFeature: " + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + |
| 134 | + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) |
144 | 135 | except Exception as e:
|
145 | 136 | print "Unable to demo Example of retrieving Attributes of a time Series Result: ", e
|
146 | 137 |
|
147 | 138 | # Get the values for a particular TimeSeriesResult
|
148 | 139 | print "\n-------- Example of Retrieving Time Series Result Values ---------"
|
149 | 140 |
|
150 |
| -tsValues = read.getTimeSeriesResultValuesByResultId(1) # Return type is a pandas dataframe |
151 |
| -tsValues = read.getResultValues(type = 'timeseries', id = 1) |
| 141 | +tsValues = read.getResultValues(resultid = 1) # Return type is a pandas datafram |
152 | 142 |
|
153 | 143 | # Print a few Time Series Values to the console
|
154 | 144 | # tsValues.set_index('ValueDateTime', inplace=True)
|
155 | 145 | try:
|
| 146 | + print "tsValues " |
156 | 147 | print tsValues.head()
|
157 | 148 | except Exception as e:
|
158 | 149 | print e
|
159 | 150 |
|
160 | 151 | # Plot the time series
|
161 | 152 |
|
162 | 153 | try:
|
163 |
| - fig = plt.figure() |
164 |
| - ax = fig.add_subplot(111) |
165 |
| - tsValues.plot(x='ValueDateTime', y='DataValue', kind='line', |
166 |
| - title=tsResult.ResultObj.VariableObj.VariableNameCV + " at " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName, |
167 |
| - ax=ax) |
168 |
| - ax.set_ylabel(tsResult.ResultObj.VariableObj.VariableNameCV + " (" + tsResult.ResultObj.UnitsObj.UnitsAbbreviation + ")") |
169 |
| - ax.set_xlabel("Date/Time") |
170 |
| - ax.xaxis.set_minor_locator(dates.MonthLocator()) |
171 |
| - ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) |
172 |
| - ax.xaxis.set_major_locator(dates.YearLocator()) |
173 |
| - ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y')) |
174 |
| - ax.grid(True) |
| 154 | + plt.figure() |
| 155 | + ax=tsValues.plot(x='ValueDateTime', y='DataValue') |
| 156 | + |
175 | 157 | plt.show()
|
176 | 158 | except Exception as e:
|
177 | 159 | print "Unable to demo plotting of tsValues: ", e
|
0 commit comments