Skip to content

Commit dfacbba

Browse files
committed
Upgrade scripts to API v2 (#1)
1 parent a56a1f0 commit dfacbba

8 files changed

Lines changed: 55 additions & 106 deletions

File tree

opus/api.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ def data(self, limit=100, page=1, **kwargs):
7272
kwargs['page'] = page
7373
return Data(self.load('data', **kwargs))
7474

75-
def metadata(self, ring_obs_id):
75+
def metadata(self, opus_id):
7676
'''Get detail for a single observation'''
77-
return Metadata(self.load('metadata/'+ring_obs_id))
77+
return Metadata(self.load('metadata_v2/'+opus_id))
7878

7979
def images(self, size='med', limit=100, page=1, **kwargs):
8080
'''Get image results for a search'''
@@ -90,20 +90,20 @@ def images(self, size='med', limit=100, page=1, **kwargs):
9090
kwargs['page'] = page
9191
return Images(self.load('images/'+size, **kwargs), size)
9292

93-
def image(self, ring_obs_id, size='med'):
93+
def image(self, opus_id, size='med'):
9494
'''Get images for a single observation'''
9595
size = size.lower()
9696
if size not in ['thumb', 'small', 'med', 'full']:
9797
raise ValueError(
9898
'Image size {} unknown (available: [thumb,small,med,full])'.format(size))
9999

100-
json = self.load('image/'+size+'/'+ring_obs_id)
101-
return Image(ring_obs_id, json['path'], json['data'][0]['img'])
100+
json = self.load('image/'+size+'/'+opus_id)
101+
return Image(opus_id, json['path'], json['data'][0]['img'])
102102

103-
def file(self, ring_obs_id):
103+
def file(self, opus_id):
104104
'''Get files for a single observation'''
105-
json = self.load('files/'+ring_obs_id)
106-
return File(ring_obs_id, json['data'][ring_obs_id])
105+
json = self.load('files/'+opus_id)
106+
return File(opus_id, json['data'][opus_id])
107107

108108
def files(self, limit=100, page=1, **kwargs):
109109
'''Get all files results for a search'''
@@ -119,21 +119,21 @@ def mults(self, param='target', **kwargs):
119119
field, given a search, and the result count for each value'''
120120
return Mults(self.load('meta/mults/'+param, **kwargs))
121121

122-
def range(self, param='ringradius1', **kwargs):
122+
def range(self, param='RINGGEOringradius1', **kwargs):
123123
'''Get range endpoints for a field, given a search'''
124124
return Range(param, self.load('meta/range/endpoints/'+param, **kwargs))
125125

126126
def field(self, field):
127127
'''Get information about a particular field'''
128-
return Field(field, self.load('fields/'+field)[field])
128+
return Field(field, self.load('fields/'+field)['data'][field])
129129

130130
def fields(self):
131131
'''Get list of all fields'''
132-
return Fields(self.load('fields'))
132+
return Fields(self.load('fields')['data'])
133133

134-
def category(self, name='obs_general'):
134+
def category(self, opus_id):
135135
'''Get all fields in a category'''
136-
return Category(self.load('categories/'+name))
136+
return Categories(self.load('categories/'+opus_id))
137137

138138
def categories(self):
139139
'''List category names'''

opus/cli/cassini.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
def vims(argv=None, api=api):
99
'''Cassini VIMS data entry point'''
1010
return data(argv, desc='Get Cassini VIMS Images and Cubes from OPUS API',
11-
args=[{'surfacegeometrytargetname': {'help': 'VIMS target name', 'metavar': 'TARGET_NAME'}},
12-
{'--cols': {'default': 'ringobsid,target,revno,time1,primaryfilespec,channel',
11+
args=[{'target': {'help': 'VIMS target name', 'metavar': 'TARGET_NAME'}},
12+
{'--cols': {'default': 'opusid,target,revno,time1,primaryfilespec',
1313
'help': 'Output columns (default: `ringobsid,target,revno,time1,primaryfilespec,channel`)'}}],
14-
defaults={'instrumentid': 'Cassini+VIMS', 'typeid': 'Image,Cube'}, api=api)
14+
defaults={'instrumentid': 'Cassini+VIMS', 'COVIMSswathlength1': 2, 'COVIMSswathwidth1': 2}, api=api)

opus/cli/cli.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,35 @@ def data(argv=None, desc='Get Data from OPUS-SETI API', args=[], defaults={}, ap
6868
def metadata(argv=None, api=api):
6969
'''OPUS SETI API metadata entry point'''
7070
parser = argparse.ArgumentParser(description='Get detail for a single observation from OPUS-SETI API')
71-
parser.add_argument('ring_obs_id', help='Valid ring_obs_id')
71+
parser.add_argument('opus_id', help='Valid OPUS ID')
7272
args, others = parser.parse_known_args(argv)
7373

74-
return api.metadata(args.ring_obs_id)
74+
return api.metadata(args.opus_id)
7575

7676
def image(argv=None, api=api):
7777
'''OPUS SETI API images/previews entry point'''
7878
parser = argparse.ArgumentParser(description='Get image for a single observation from OPUS-SETI API')
79-
parser.add_argument('ring_obs_id', help='Valid ring_obs_id')
79+
parser.add_argument('opus_id', help='Valid opus_id')
8080
parser.add_argument('-s', '--size', help='Image size', default='med', choices=['thumb', 'small', 'med', 'full'])
8181
parser.add_argument('-d', '--download', help='Download the image', action='store_true')
8282
parser.add_argument('-o', '--output', help='Output folder/filename for download', default=None)
8383
args, others = parser.parse_known_args(argv)
8484

85-
img = api.image(args.ring_obs_id, size=args.size)
85+
img = api.image(args.opus_id, size=args.size)
8686
if args.download or args.output is not None:
8787
return img.download(out=args.output)
8888
return img.url
8989

9090
def files(argv=None, api=api):
9191
'''OPUS SETI API files entry point'''
9292
parser = argparse.ArgumentParser(description='Get files for a single observation from OPUS-SETI API')
93-
parser.add_argument('ring_obs_id', help='Valid ring_obs_id')
93+
parser.add_argument('opus_id', help='Valid opus_id')
9494
parser.add_argument('-f', '--fmt', help='File format', default=None, nargs=2, metavar=('GROUP','FORMAT'))
9595
parser.add_argument('-d', '--download', help='Download the image', action='store_true')
9696
parser.add_argument('-o', '--output', help='Output folder/filename for download', default=None)
9797
args, others = parser.parse_known_args(argv)
9898

99-
files = api.file(args.ring_obs_id)
99+
files = api.file(args.opus_id)
100100
if args.fmt is not None:
101101
files = files[args.fmt[0]][args.fmt[1]]
102102
if args.download or args.output is not None:
@@ -115,7 +115,4 @@ def field(argv=None, api=api):
115115
try:
116116
return api.field(args.field)
117117
except KeyError:
118-
try:
119-
return api.fields()[args.field]
120-
except KeyError:
121-
return 'Unknown field. To list all the available fields, run: `opus-field --all`'
118+
return 'Unknown field. To list all the available fields, run: `opus-field --all`'

opus/data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ def __init__(self, columns, row):
8686
self._row = row
8787
self._id = None
8888
for column, value in zip(columns, row):
89-
if 'Ring Observation ID' in str(column):
89+
if 'OPUS ID' in str(column):
9090
self._id = value
9191
else:
9292
self.append(column, value)
9393

9494
if self._id is None:
95-
raise KeyError('`Ring Observation ID` key is missing in `cols` query')
95+
raise KeyError('`OPUS ID` key is missing in `cols` query')
9696

9797
def __repr__(self):
98-
return 'Ring Observation ID: {}\n'.format(str(self)) + \
98+
return 'OPUS ID: {}\n'.format(str(self)) + \
9999
'\n'.join(' - {}: {}'.format(key, value) for key, value in self.items())
100100

101101
def __str__(self):

opus/fields.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,74 +18,23 @@ def __init__(self, field, json):
1818
self._json = json
1919
self.field = field
2020
self.label = json['label']
21+
self.category = json['category']
22+
self.slug = json['slug']
23+
self.old_slug = json['old_slug']
2124

2225
def __repr__(self):
23-
if self.desc is not None:
24-
return '{} ({}):\n => {}\n {}'.format(self.label, self.field, self.desc, self.url)
25-
else:
26-
return '{}: ({})'.format(self.label, self.field)
26+
return '{} ({}):\n => Category {} / Slug: {}'.format(self.label, self.field, self.category, self.slug)
2727

2828
def __str__(self):
2929
return self.label
3030

31-
@property
32-
def desc(self):
33-
desc = self._json['more_info']['def']
34-
if desc is False:
35-
return None
36-
else:
37-
return desc.replace('<br />', '')\
38-
.replace('<br>', '')\
39-
.replace('<Br>', '')\
40-
.replace('\n', '')\
41-
.replace(' ', ' ')\
42-
.replace(' ', ' ')
43-
44-
@property
45-
def url(self):
46-
url = self._json['more_info']['more_info']
47-
if url is False:
48-
return None
49-
else:
50-
return url
51-
52-
5331
class Fields(DataDict):
5432
def __init__(self, json):
5533
DataDict.__init__(self)
5634
self._json = json
57-
self.append('GLOBAL', GlobalField())
5835
for key, value in json.items():
59-
if 'surfacegeometry' in key:
60-
try:
61-
body, field = splitKey(key)
62-
if body not in self.keys():
63-
self.append(body, SurfaceGeometry(body))
64-
self[body].append(field, Field(key, value))
65-
except IndexError:
66-
self['GLOBAL'].append(key.lower(), Field(key, value))
67-
else:
68-
self['GLOBAL'].append(key.lower(), Field(key, value))
36+
self.append(key.lower(), Field(key, value))
6937

7038
def __repr__(self):
7139
return 'OPUS API list of all fields available ({}):\n'.format(len(self)) + \
7240
'\n'.join(' - {}'.format(key) for key in self)
73-
74-
75-
class GlobalField(DataDict):
76-
def __init__(self):
77-
DataDict.__init__(self)
78-
79-
def __repr__(self):
80-
return 'OPUS API Global fields ({}):\n'.format(len(self)) + \
81-
'\n'.join(' - {} ({})'.format(value, key) for key, value in self.items())
82-
83-
84-
class SurfaceGeometry(GlobalField):
85-
def __init__(self, body):
86-
GlobalField.__init__(self)
87-
self.body = body
88-
89-
def __repr__(self):
90-
return 'OPUS API Surface Geometry fields ({}) for `{}`:\n'.format(len(self), self.body.title()) + \
91-
'\n'.join(' - {} ({})'.format(value, key) for key, value in self.items())

opus/files.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@
66

77
class File(DataDict):
88
'''Files for a single observation'''
9-
def __init__(self, ring_obs_id, data):
9+
def __init__(self, opus_id, data):
1010
DataDict.__init__(self)
11-
self.ring_obs_id = ring_obs_id
11+
self.opus_id = opus_id
1212

1313
for label, files in data.items():
14-
if label == 'preview_image':
15-
key = 'PREVIEWS'
14+
if 'Browse Image' in label:
1615
value = Preview(files)
1716
else:
18-
key = label.upper()
1917
value = FileList(files)
18+
key = label.replace('Browse Image ', '')\
19+
.replace(' (calibrated unavailable)', '')\
20+
.upper()\
21+
.replace(' ', '_')\
22+
.replace('(', '')\
23+
.replace(')', '')
2024

2125
self.append(key, value)
2226

2327
def __repr__(self):
24-
return 'OPUS API Files for observation: {}\n'.format(self.ring_obs_id) + \
28+
return 'OPUS API Files for observation: {}\n'.format(self.opus_id) + \
2529
'\n'.join('\n=> {}\n{}'.format(key, value) for key, value in self.items())
2630

2731
def __str__(self):
28-
return self.ring_obs_id
32+
return self.opus_id
2933

3034

3135
class Preview(DataDict):
@@ -44,14 +48,14 @@ class FileList(DataDict):
4448
def __init__(self, files=[]):
4549
DataDict.__init__(self)
4650
for f in files:
47-
if f.endswith('.LBL'):
51+
if f.lower().endswith('.lbl'):
4852
key = 'LBL'
4953
elif f.endswith('.IMG'):
5054
key = 'IMG'
5155
elif f.endswith('.qub'):
5256
key = 'qub'
53-
elif f.endswith('.QUB'):
54-
key = 'QUB'
57+
elif f.endswith('.tab'):
58+
key = 'TAB'
5559
elif f.lower().endswith('.fmt'):
5660
key = f.lower().split('/')[-1].replace('.fmt', '')
5761
else:

opus/images.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66

77
class Image(Downloadable):
8-
def __init__(self, ring_obs_id, path, img):
8+
def __init__(self, opus_id, path, img):
99
Downloadable.__init__(self, path + img)
10-
self.ring_obs_id = ring_obs_id
10+
self.opus_id = opus_id
1111
self.path = path
1212
self.img = img
1313

1414
def __repr__(self):
15-
return 'OPUS API Image object: {}'.format(self.ring_obs_id)
15+
return 'OPUS API Image object: {}'.format(self.opus_id)
1616

1717
def __str__(self):
18-
return self.ring_obs_id
18+
return self.opus_id
1919

2020

2121
class Images(DataDict):
@@ -24,7 +24,7 @@ def __init__(self, json, size):
2424
self._json = json
2525
self.size = size
2626
for img in json['data']:
27-
el = Image(img['ring_obs_id'], img['path'], img['img'])
27+
el = Image(img['opus_id'], img['path'], img['img'])
2828
self._data[str(el)] = el
2929

3030
def __repr__(self):

opus/metadata.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
def read_time(time):
99
'''Read date time'''
10+
if time == 'No':
11+
return None
1012
try:
1113
return dt.strptime(time, '%Y-%m-%dT%H:%M:%S.%f')
1214
except ValueError:
@@ -25,21 +27,18 @@ def __init__(self, json):
2527
value = Constraints(key, constraints)
2628
self.append(key, value)
2729

28-
self.ring_obs_id = self['GENERAL']['ring_obs_id']
30+
self.opus_id = self['GENERAL']['opusid']
2931

3032
def __repr__(self):
31-
return 'OPUS API Metadata Ring observation: {}\n'.format(self.ring_obs_id) + \
33+
return 'OPUS API Metadata Ring observation: {}\n'.format(self.opus_id) + \
3234
'\n'.join('\n{}'.format(values) for values in self.values())
3335

3436

3537
class Constraints(DataDict):
3638
def __init__(self, name, constraints={}):
3739
DataDict.__init__(self)
3840
self.name = name
39-
for key, value in constraints.items():
40-
if key == 'is_image':
41-
value = (value == 1)
42-
41+
for key, value in constraints.items():
4342
if 'time' in key and isinstance(value, six.text_type):
4443
value = read_time(value)
4544

0 commit comments

Comments
 (0)