@@ -84,7 +84,24 @@ def get_records(self, *args, **kw):
8484
8585 q = q .filter (" and " .join (fs ))
8686 q = q .expand ("Thing/Locations" )
87- return [di .thing .locations .entities [0 ] for di in q .list ()]
87+
88+ # NM ENV has multiple datastreams per parameter per location (e.g. id 8 and arsenic)
89+ # because of this duplicative site information is retrieved (we operated under the assumption one datastream per location per parameter)
90+ # so we need to filter out duplicates, otherwise there will be multiple site records and duplicative parameter records
91+ all_sites = [di .thing .locations .entities [0 ] for di in q .list ()]
92+
93+ # can't do list(set(all_sites)) because the Location entities are not hashable
94+ site_dictionary = {}
95+ for site in all_sites :
96+ site_id = site .id
97+ if site_id not in site_dictionary .keys ():
98+ site_dictionary [site_id ] = site
99+
100+ distinct_sites = list (site_dictionary .values ())
101+ # print(
102+ # f"Found {len(all_sites)} datastreams for {analyte} and {len(distinct_sites)} distinct sites."
103+ # )
104+ return distinct_sites
88105
89106
90107class DWBAnalyteSource (STAnalyteSource ):
@@ -120,16 +137,20 @@ def get_records(self, site, *args, **kw):
120137 f"Thing/Locations/id eq { site .id } and ObservedProperty/id eq { analyte } "
121138 )
122139
123- ds = q .list ().entities [0 ]
140+ # NMED DWB has multiple datastreams per parameter per location (e.g. id 8 and arsenic)
141+ # print(
142+ # f"Found {len(q.list().entities)} datastreams for {site.id} and {analyte}."
143+ # )
124144 rs = []
125- for obs in ds .get_observations ().query ().list ():
126- rs .append (
127- {
128- "location" : site ,
129- "datastream" : ds ,
130- "observation" : obs ,
131- }
132- )
145+ for datastream in q .list ().entities :
146+ for obs in datastream .get_observations ().query ().list ():
147+ rs .append (
148+ {
149+ "location" : site ,
150+ "datastream" : datastream ,
151+ "observation" : obs ,
152+ }
153+ )
133154
134155 return rs
135156
0 commit comments