-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap_uframe_datastreams.py
executable file
·345 lines (285 loc) · 11.6 KB
/
map_uframe_datastreams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
from uframe import *
from uframe.availability import get_parameter_stream
import sys
import csv
import json
import argparse
def main(args):
"""
Download metadata records for all available parameters and associated streams
(telemetered/recovered) from the default UFrame instance as CSV. The response
is printed to STDOUT.
The default uFrame instance is: http://uframe-test.ooi.rutgers.edu. Other
UFrame instances may be specified using the --baseurl option pointing to a
valid UFrame instance.
"""
uframe = UFrame()
if args.base_url:
uframe = UFrame(base_url=args.base_url)
if args.ref_des:
stream_map = map_parameters_by_reference_designator(args.ref_des, method=args.method, uframe=uframe)
else:
stream_map = map_uframe_datastreams(args.array_id, subsite=args.subsite, method=args.method, uframe=uframe)
if args.file_format == 'json':
sys.stdout.write(json.dumps(stream_map))
sys.stdout.flush()
else:
stdout_writer = csv.writer(sys.stdout)
column_map = {'parameter' : 'particleKey',
'method' : 'stream method',
'stream' : 'stream stream',
'calculated' : '',
'reference_designator' : 'stream sensor',
'pdId' : 'pdId',
'units' : 'units',
'beginTime' : 'stream beginTime',
'endTime' : 'stream endTime',
'num_records' : 'stream count',
'fillValue' : 'fillValue',
'type' : 'type',
'unsigned' : 'unsigned',
'metadata_url' : 'metadata_url'}
if args.all:
cols = ['reference_designator',
'stream',
'parameter',
'method',
'pdId',
'units',
'beginTime',
'endTime',
'num_records',
'fillValue',
'type',
'unsigned',
'metadata_url']
else:
cols = ['reference_designator',
'stream',
'parameter',
'method',
'calculated']
if args.particles:
cols.append('num_records')
if args.urls:
cols.append('metadata_url')
stdout_writer.writerow(cols)
for stream in stream_map:
row = []
for col in cols:
if col not in column_map.keys():
continue
tokens = column_map[col].split(' ')
if len(tokens) == 1:
if col == 'calculated':
if stream['shape'] == 'FUNCTION':
row.append(1)
else:
row.append(0)
else:
row.append(stream[column_map[col]])
else:
row.append(stream[tokens[0]][tokens[1]])
try:
stdout_writer.writerow(row)
except ValueError as e:
sys.stderr.write('{:s}\n'.format(e.message))
continue
return len(stream_map)
def map_uframe_datastreams(array_id=None, subsite=None, method=None, uframe=UFrame()):
"""
Download metadata records for all available parameters and associated streams
(telemetered/recovered) from the default UFrame instance as CSV (default) or
JSON.
The default uFrame instance is: http://uframe-test.ooi.rutgers.edu
"""
stream_map = []
# Get the list of available array names
arrays = get_arrays(uframe_base=uframe)
if not arrays:
sys.stderr.write('UFrame instance contains no arrays\n')
sys.stderr.flush()
return stream_map
if array_id:
if array_id not in arrays:
sys.stderr.write('Invalid array specified: {:s}\n'.format(array_id))
sys.stderr.flush()
return stream_map
else:
arrays = [array_id]
for array_id in arrays:
#sys.stdout.write('Fetching Stream: {:s}\n'.format(array_id))
# Get the list of available platforms for the current array_id
platforms = get_platforms(array_id, uframe_base=uframe)
if not platforms:
sys.stderr.write('{:s}: Array contains no platforms\n'.format(array_id))
sys.stderr.flush()
continue
for platform in platforms:
if subsite and subsite != platform:
continue
#sys.stdout.write('Fetching Platform: {:s}\n'.format(platform))
# Get the list of sensors for this platform
sensors = get_platform_sensors(array_id, platform, uframe_base=uframe)
if not sensors:
sys.stderr.write('{:s}-{:s}: Platform contains no sensors'.format(array_id, platform))
sys.stderr.flush()
continue
for sensor in sensors:
#sys.stdout.write('Fetching Sensor: {:s}\n'.format(sensor))
# Get the metadata for this sensor
meta = get_sensor_metadata(array_id, platform, sensor, uframe_base=uframe)
if not meta:
sys.stderr.write('{:s}-{:s}-{:s}: Sensor contains no particleKeys'.format(array_id, platform, sensor))
sys.stderr.flush()
continue
# Create the metadata url
url = '{:s}/{:s}/{:s}/{:s}/metadata'.format(
uframe.url,
array_id,
platform,
sensor)
streams = map_streams(meta, url, method=method)
for stream in streams:
stream_map.append(stream)
return stream_map
def map_streams(meta, url, method=None):
'''
Return a stream map containing metadata for all streams and parameters
in the metadata (meta) array of dictionaries.
Parameters:
meta: a metdata response returned from a query of a UFrame instance for
a specified instrument. The format for the request is as follows:
http://UFRAME_URL:12756/sensor/inv/SITE/NODE/INSTRUMENT/metadata
url: the url used to retrieve the instrument stream/parameter metadata
records from above.
method: Return only streams for the specified method (i.e.: telemetered,
'recovered', etc.)
Returns:
stream_map: array of dictionaries containing information on all parameters
contained in the the meta argument.
'''
stream_map = []
for parameter in meta['parameters']:
streams = []
for t in range(len(meta['times'])):
if method:
if meta['times'][t]['stream'] == parameter['stream'] and meta['times'][t]['method'].startswith(method):
streams.append(meta['times'][t])
else:
if meta['times'][t]['stream'] == parameter['stream']:
streams.append(meta['times'][t])
for stream in streams:
param_copy = parameter.copy()
param_copy['stream'] = stream
param_copy['metadata_url'] = url
stream_map.append(param_copy)
return stream_map
def map_parameters_by_reference_designator(ref_des, method=None, uframe=UFrame()):
'''
Return a stream map containing metadata for all streams and parameters
for the specified reference designator.
Parameters:
ref_des: fully-qualified reference designator of the form:
SITE-NODE-INSTRUMENT
uframe: UFrame instance pointing the desired UFrame installation.
Returns:
stream_map: array of dictionaries containing information on all parameters
contained in the the meta response.
'''
# Split the reference designtor on dashes
r_tokens = ref_des.split('-')
if len(r_tokens) != 4:
sys.stderr.write('Invalid reference designator: {:s}\n'.format(ref_des))
sys.stderr.flush()
return False
metadata = get_sensor_metadata(r_tokens[0],
r_tokens[1],
'-'.join([r_tokens[2], r_tokens[3]]),
uframe_base=uframe)
if not metadata:
sys.stderr.write('No metadata found for: {:s}\n'.format(ref_des))
sys.stderr.flush()
return []
# Create the metadata url
url = '{:s}/{:s}/{:s}/{:s}/metadata'.format(
uframe.url,
r_tokens[0],
r_tokens[1],
'-'.join([r_tokens[2], r_tokens[3]]))
stream_map = map_streams(metadata, url, method=method)
return stream_map
#def stream_map_to_csv(stream_map):
#
# stdout_writer = csv.writer(sys.stdout)
#
# cols = ['reference_designator',
# 'stream',
# 'method',
# 'parameter',
# 'pdId',
# 'units',
# 'beginTime',
# 'endTime',
# 'num_records',
# 'metadata_url']
#
# stdout_writer.writerow(cols)
#
# count = 0
# for stream in stream_map:
#
# row = [stream['stream']['sensor'],
# stream['stream']['stream'],
# stream['stream']['method'],
# stream['particleKey'],
# stream['pdId'],
# stream['units'],
# stream['stream']['beginTime'],
# stream['stream']['endTime'],
# stream['stream']['count'],
# stream['metadata_url']]
#
# stdout_writer.writerow(row)
# count = count + 1
#
# return count
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description=main.__doc__)
arg_parser.add_argument('--array',
help='Specify the name of the array to search for (i.e.: CE01ISSM). If not specified, all arrays are downloaded.',
dest='array_id',
default=None)
arg_parser.add_argument('-r', '--refdes',
help='Map streams for a single reference designator (i.e.: CE01ISSM-MFD35-02-PRESFA000)',
dest='ref_des',
default = None)
arg_parser.add_argument('-s', '--subsite',
dest='subsite',
help='Restrict results to the specified subsite (i.e.: GI01SUMO).')
arg_parser.add_argument('-m', '--method',
dest='method',
default=None,
help='Specify a stream method (i.e.: telemetered, recovered_host, etc.)')
arg_parser.add_argument('-p', '--particles',
dest='particles',
action='store_true',
help='Print created particle diagnostics.')
arg_parser.add_argument('-a', '--all',
help = 'Print entire metadata record. Default is false and results in printing stream/parameter info only.',
dest='all',
action='store_true')
arg_parser.add_argument('-b', '--baseurl',
dest='base_url',
help='Specify an alternate uFrame server URL. Must start with \'http://\'.')
arg_parser.add_argument('-f', '--format',
dest='file_format',
default='csv',
help='Specify the response type format (\'csv\' <Default> or \'json\').')
arg_parser.add_argument('-u', '--url',
help = 'Print the instrument metadata stream url.',
dest = 'urls',
action = 'store_true')
parsed_args = arg_parser.parse_args()
num_streams = main(parsed_args)