1
1
from __future__ import (absolute_import , division , print_function )
2
2
3
+ import warnings
4
+
3
5
from odm2api .ODM2 import serviceBase
4
6
from odm2api .ODM2 .models import (
5
7
ActionAnnotations , ActionDirectives , ActionExtensionPropertyValues , Actions ,
29
31
SpatialReferenceExternalIdentifiers , SpatialReferences , SpecimenBatchPositions ,
30
32
SpectraResultValueAnnotations , SpectraResultValues , TaxonomicClassifierExternalIdentifiers ,
31
33
TaxonomicClassifiers , TimeSeriesResultValueAnnotations , TimeSeriesResultValues ,
32
- TimeSeriesResults , TrajectoryResultValueAnnotations , TrajectoryResultValues ,
34
+ TrajectoryResultValueAnnotations , TrajectoryResultValues ,
33
35
TransectResultValueAnnotations , TransectResultValues , Units , VariableExtensionPropertyValues ,
34
36
VariableExternalIdentifiers , Variables ,
35
37
)
38
40
39
41
from sqlalchemy import distinct , exists
40
42
41
- import warnings
42
-
43
43
__author__ = 'sreeder'
44
44
45
45
@@ -75,8 +75,9 @@ def __init__(self, affiliation, person, org):
75
75
76
76
77
77
class SamplingFeatureDataSet ():
78
- datasets = {}
79
- related_features = {}
78
+ datasets = {}
79
+ related_features = {}
80
+
80
81
def __init__ (self , samplingfeature , datasetresults , relatedfeatures ):
81
82
sf = samplingfeature
82
83
@@ -94,26 +95,24 @@ def __init__(self, samplingfeature, datasetresults, relatedfeatures):
94
95
self .assignDatasets (datasetresults )
95
96
self .assignRelatedFeatures (relatedfeatures )
96
97
97
-
98
98
print (self .datasets )
99
99
100
100
def assignDatasets (self , datasetresults ):
101
101
self .datasets = {}
102
102
if datasetresults :
103
103
for dsr in datasetresults :
104
104
if dsr .DataSetObj not in self .datasets :
105
- #if the dataset is not in the dictionary, add it and the first result
106
- self .datasets [dsr .DataSetObj ]= []
105
+ # if the dataset is not in the dictionary, add it and the first result
106
+ self .datasets [dsr .DataSetObj ] = []
107
107
res = dsr .ResultObj
108
108
# res.FeatureActionObj = None
109
109
self .datasets [dsr .DataSetObj ].append (res )
110
110
else :
111
- #if the dataset is in the dictionary, append the result object to the list
111
+ # if the dataset is in the dictionary, append the result object to the list
112
112
res = dsr .ResultObj
113
113
# res.FeatureActionObj = None
114
114
self .datasets [dsr .DataSetObj ].append (res )
115
115
116
-
117
116
def assignRelatedFeatures (self , relatedfeatures ):
118
117
self .related_features = {}
119
118
if relatedfeatures :
@@ -169,7 +168,8 @@ def resultExists(self, result):
169
168
)
170
169
return ret .scalar ()
171
170
172
- except :
171
+ except Exception as e :
172
+ print ('Error running Query: {}' .format (e ))
173
173
return None
174
174
175
175
# Annotations
@@ -225,7 +225,8 @@ def getAnnotations(self, annottype=None, codes=None, ids=None, **kwargs):
225
225
query = query .filter (Annotations .AnnotationID .in_ (ids ))
226
226
return query .all ()
227
227
228
- except :
228
+ except Exception as e :
229
+ print ('Error running Query: {}' .format (e ))
229
230
return None
230
231
231
232
# CV
@@ -384,15 +385,16 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
384
385
variables = [
385
386
x [0 ] for x in
386
387
self ._session .query (distinct (Results .VariableID ))
387
- .filter (Results .FeatureActionID == FeatureActions .FeatureActionID )
388
- .filter (FeatureActions .SamplingFeatureID == SamplingFeatures .SamplingFeatureID )
389
- .filter (SamplingFeatures .SamplingFeatureCode == sitecode ).all ()
388
+ .filter (Results .FeatureActionID == FeatureActions .FeatureActionID )
389
+ .filter (FeatureActions .SamplingFeatureID == SamplingFeatures .SamplingFeatureID )
390
+ .filter (SamplingFeatures .SamplingFeatureCode == sitecode ).all ()
390
391
]
391
392
if ids :
392
393
ids = list (set (ids ).intersection (variables ))
393
394
else :
394
395
ids = variables
395
- except :
396
+ except Exception as e :
397
+ print ('Error running Query: {}' .format (e ))
396
398
pass
397
399
398
400
if results :
@@ -402,7 +404,8 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results=False):
402
404
ids = list (set (ids ).intersection (variables ))
403
405
else :
404
406
ids = variables
405
- except :
407
+ except Exception as e :
408
+ print ('Error running Query: {}' .format (e ))
406
409
pass
407
410
408
411
query = self ._session .query (Variables )
@@ -520,10 +523,10 @@ def getSamplingFeatures(self, ids=None, codes=None, uuids=None,
520
523
if results :
521
524
try :
522
525
fas = [x [0 ] for x in self ._session .query (distinct (Results .FeatureActionID )).all ()]
523
- except :
526
+ except Exception as e :
527
+ print ('Error running Query: {}' .format (e ))
524
528
return None
525
- sf = [x [0 ] for x in self ._session .query (distinct (FeatureActions .SamplingFeatureID ))
526
- .filter (FeatureActions .FeatureActionID .in_ (fas )).all ()]
529
+ sf = [x [0 ] for x in self ._session .query (distinct (FeatureActions .SamplingFeatureID )).filter (FeatureActions .FeatureActionID .in_ (fas )).all ()] # noqa
527
530
if ids :
528
531
ids = list (set (ids ).intersection (sf ))
529
532
else :
@@ -557,8 +560,8 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None
557
560
558
561
"""
559
562
560
- sf = self ._session .query (distinct (SamplingFeatures .SamplingFeatureID ))\
561
- .select_from (RelatedFeatures )
563
+ sf = self ._session .query (distinct (SamplingFeatures .SamplingFeatureID )) \
564
+ .select_from (RelatedFeatures )
562
565
563
566
if sfid :
564
567
sf = sf .join (RelatedFeatures .RelatedFeatureObj ).filter (RelatedFeatures .SamplingFeatureID == sfid )
@@ -576,8 +579,6 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid=None, relationshiptype=None
576
579
print ('Error running Query: {}' .format (e ))
577
580
return None
578
581
579
-
580
-
581
582
# Action
582
583
def getActions (self , ids = None , acttype = None , sfid = None , ** kwargs ):
583
584
"""
@@ -656,7 +657,6 @@ def getUnits(self, ids=None, name=None, unittype=None, **kwargs):
656
657
print ('Error running Query: {}' .format (e ))
657
658
return None
658
659
659
-
660
660
# Organization
661
661
def getOrganizations (self , ids = None , codes = None ):
662
662
"""
@@ -738,7 +738,7 @@ def getAffiliations(self, ids=None, personfirst=None, personlast=None, orgcode=N
738
738
return None
739
739
740
740
# Results
741
- def getResults (self , ids = None , restype = None , uuids = None , actionid = None , simulationid = None ,
741
+ def getResults (self , ids = None , restype = None , uuids = None , actionid = None , simulationid = None ,
742
742
variableid = None , siteid = None , sfids = None , sfuuids = None , sfcodes = None , ** kwargs ):
743
743
744
744
# TODO what if user sends in both type and actionid vs just actionid
@@ -791,10 +791,10 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
791
791
if uuids :
792
792
query = query .filter (Results .ResultUUID .in_ (uuids ))
793
793
if simulationid :
794
- query = query .join (FeatureActions )\
795
- .join (Actions )\
796
- .join (Simulations )\
797
- .filter_by (SimulationID = simulationid )
794
+ query = query .join (FeatureActions ) \
795
+ .join (Actions ) \
796
+ .join (Simulations ) \
797
+ .filter_by (SimulationID = simulationid )
798
798
if actionid :
799
799
query = query .join (FeatureActions ).filter_by (ActionID = actionid )
800
800
if 'sfid' in kwargs :
@@ -811,16 +811,15 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
811
811
query = query .join (FeatureActions ).filter (FeatureActions .SamplingFeatureID .in_ (sfids ))
812
812
813
813
if siteid :
814
-
815
814
sfids = [x [0 ] for x in self ._session .query (
816
815
distinct (SamplingFeatures .SamplingFeatureID ))
817
816
.select_from (RelatedFeatures )
818
817
.join (RelatedFeatures .SamplingFeatureObj )
819
818
.filter (RelatedFeatures .RelatedFeatureID == siteid )
820
819
.all ()
821
- ]
820
+ ]
822
821
823
- #TODO does this code do the same thing as the code above?
822
+ # TODO does this code do the same thing as the code above?
824
823
# sf_list = self.getRelatedSamplingFeatures(rfid=siteid)
825
824
# sfids = []
826
825
# for sf in sf_list:
@@ -835,7 +834,7 @@ def getResults(self, ids=None, restype = None, uuids=None, actionid=None, simula
835
834
return None
836
835
837
836
# Datasets
838
- def getDataSets (self , ids = None , codes = None , uuids = None , dstype = None ):
837
+ def getDataSets (self , ids = None , codes = None , uuids = None , dstype = None ):
839
838
"""
840
839
Retrieve a list of Datasets
841
840
@@ -907,7 +906,7 @@ def getDataSetsResults(self, ids=None, codes=None, uuids=None, dstype=None):
907
906
if all (v is None for v in [ids , codes , uuids ]):
908
907
raise ValueError ('Expected DataSetID OR DataSetUUID OR DataSetCode argument' )
909
908
910
- q = self ._session .query (DataSetsResults )\
909
+ q = self ._session .query (DataSetsResults ) \
911
910
.join (DataSets )
912
911
if ids :
913
912
q = q .filter (DataSets .DataSetID .in_ (ids ))
@@ -956,12 +955,11 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None):
956
955
resids .append (ds .ResultID )
957
956
958
957
try :
959
- return self .getResultValues (resultids = resids )
958
+ return self .getResultValues (resultids = resids )
960
959
except Exception as e :
961
960
print ('Error running Query {}' .format (e ))
962
961
return None
963
962
964
-
965
963
def getSamplingFeatureDatasets (self , ids = None , codes = None , uuids = None , dstype = None , sftype = None ):
966
964
"""
967
965
Retrieve a list of Datasets associated with the given sampling feature data.
@@ -991,11 +989,12 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
991
989
992
990
"""
993
991
994
-
995
992
# make sure one of the three arguments has been sent in
996
993
if all (v is None for v in [ids , codes , uuids , sftype ]):
997
- raise ValueError ('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType '
998
- 'argument' )
994
+ raise ValueError (
995
+ 'Expected samplingFeatureID OR samplingFeatureUUID '
996
+ 'OR samplingFeatureCode OR samplingFeatureType '
997
+ 'argument' )
999
998
1000
999
sf_query = self ._session .query (SamplingFeatures )
1001
1000
if sftype :
@@ -1012,18 +1011,17 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
1012
1011
sf_list .append (sf )
1013
1012
1014
1013
try :
1015
- sfds = []
1014
+ sfds = []
1016
1015
for sf in sf_list :
1017
1016
1018
- q = self ._session .query (DataSetsResults )\
1019
- .join (Results )\
1020
- .join (FeatureActions )\
1017
+ q = self ._session .query (DataSetsResults ) \
1018
+ .join (Results ) \
1019
+ .join (FeatureActions ) \
1021
1020
.filter (FeatureActions .SamplingFeatureID == sf .SamplingFeatureID )
1022
1021
1023
1022
if dstype :
1024
1023
q = q .filter_by (DatasetTypeCV = dstype )
1025
1024
1026
-
1027
1025
vals = q .all ()
1028
1026
1029
1027
related = self .getRelatedSamplingFeatures (sf .SamplingFeatureID )
@@ -1082,17 +1080,21 @@ def getEquipment(self, codes=None, equiptype=None, sfid=None, actionid=None, **k
1082
1080
warnings .warn ('The parameter \' type\' is deprecated. Please use the equiptype parameter instead.' ,
1083
1081
DeprecationWarning , stacklevel = 2 )
1084
1082
equiptype = kwargs ['type' ]
1083
+
1084
+ # NOTE: Equiptype currently unused!
1085
+ if equiptype :
1086
+ pass
1085
1087
e = self ._session .query (Equipment )
1086
1088
if sfid :
1087
1089
e = e .join (EquipmentUsed ) \
1088
- .join (Actions ) \
1089
- .join (FeatureActions ) \
1090
- .filter (FeatureActions .SamplingFeatureID == sfid )
1090
+ .join (Actions ) \
1091
+ .join (FeatureActions ) \
1092
+ .filter (FeatureActions .SamplingFeatureID == sfid )
1091
1093
if codes :
1092
1094
e = e .filter (Equipment .EquipmentCode .in_ (codes ))
1093
1095
if actionid :
1094
1096
e = e .join (EquipmentUsed ).join (Actions ) \
1095
- .filter (Actions .ActionID == actionid )
1097
+ .filter (Actions .ActionID == actionid )
1096
1098
return e .all ()
1097
1099
1098
1100
def CalibrationReferenceEquipment (self ):
0 commit comments