-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaption_helper.py
executable file
·238 lines (184 loc) · 7.12 KB
/
caption_helper.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
#!/usr/bin/env python
'''This program just reads some meta-data from the label of
an ISIS cube file and presents the text in a way that might help
someone get started on writing a figure caption.'''
# Copyright 2019, Ross A. Beyer ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This program was motivated by this Issue from Laszlo Kestay:
# https://github.com/USGS-Astrogeology/ISIS3/issues/1588
# He says:
# It is a royal pain to find all the data to put in the caption for
# a figure for a paper. It would be lovely if ISIS would make
# publication-ready figures. One part of this would be to pull the
# key data out of the labels and present the user with a simple pile
# of text that they can cut-paste (or put in a text file) for the
# caption. Would be good to be able to select what information you
# wanted in the caption too.
#
# Optional things to add: local time of day, season, type of SPICE used. If
# not map projected, give information on the viewing geometry.
#
# You will need to install the pvl library at the very least, and can install
# the kalasiris library (and ISIS) to get some additional information.
import argparse
import datetime
import math
import os
import subprocess
import sys
import pvl
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('cube', help='Cube file(s) to read.')
args = parser.parse_args()
# Gather data elements into elem:
elem = dict()
label = pvl.load(args.cube)['IsisCube']
elem.update(get_instrument(label.get('Instrument')))
elem['productid'] = None
if label.get('Archive') is not None:
elem['productid'] = label['Archive'].get('ProductId')
elem.update(get_mapping(label.get('Mapping')))
elem.update(get_campt(args.cube))
# Print out results.
for k in elem.keys():
print('{}: {}'.format(k, elem.get(k)))
print('')
print(' '.join(get_sentences(elem)))
return
def get_instrument(label: dict) -> dict:
d = dict(scname=None, instrument=None, time=None)
if label is None:
return d
d['scname'] = label.get('SpacecraftName')
d['instrument'] = label.get('InstrumentId')
t = label.get('StartTime')
if t is not None:
if isinstance(t, datetime.datetime):
d['time'] = str(t)
else:
d['time'] = str(t[0])
return d
def get_mapping(label: dict) -> dict:
d = dict(pixelres=None, projection=None)
if label is None:
return d
pr = label.get('PixelResolution')
if pr is not None:
d['pixelres'] = '{} {}'.format(*pr)
d['projection'] = label.get('ProjectionName')
return d
def get_campt(path: os.PathLike) -> dict:
d = dict(northaz=None, subsolargroundaz=None, abovehoriz=None)
try:
import kalasiris as isis
cpvl = pvl.loads(isis.campt(path).stdout)['GroundPoint']
d['northaz'] = cpvl.get('NorthAzimuth')
d['subsolargroundaz'] = cpvl.get('SubSolarGroundAzimuth')
incid = cpvl.get('Incidence')
if incid is not None:
d['abovehoriz'] = pvl.Units(str(90 - float(incid[0])), incid[1])
except subprocess.CalledProcessError:
# Couln't get any data from campt, maybe it was a level 2 image?
pass
except ModuleNotFoundError:
# To get some additional functionality,
# install the kalasiris library.
pass
return d
def inst_sentence(scname=None, instrument=None, time=None, productid=None) -> str:
if all(x is None for x in (scname, instrument, time, productid)):
raise ValueError('All passed elements were None, '
'at least one of them should have a string.')
sent = 'This image contains data acquried'
if scname is not None or instrument is not None:
sent += ' by'
if instrument is not None:
sent += f' {instrument}'
if scname is not None:
sent += f' onboard {scname}'
else:
sent += f' {scname}'
if time is not None:
sent += f' on {time}'
if productid is not None:
sent += f' with the Product ID, {productid}'
sent += '.'
return sent
def mapping_sentence(projection=None, pixelres=None) -> str:
if all(x is None for x in (projection, pixelres)):
raise ValueError('All passed elements were None, '
'at least one of them should have a string.')
sent = 'The image'
prep = 'has'
if projection is not None:
sent += f' is a {projection} projection'
prep = 'with'
if pixelres is not None:
sent += f' {prep} a ground sample distance of {pixelres}'
sent += '.'
return sent
def f_pair(pair: list) -> tuple:
units = pair[1]
if units.casefold().startswith('degree'):
units = 'degrees'
return (float(pair[0]), units)
def campt_sentence(northaz=None, subsolargroundaz=None, abovehoriz=None) -> str:
# Assumes the arguments are either None or pvl.Units objects.
if all(x is None for x in (northaz, subsolargroundaz, abovehoriz)):
raise ValueError('All passed elements were None, '
'at least one of them should have a string.')
sents = list()
if northaz is not None:
s = 'North'
if math.floor(float(northaz[0])) == 270:
s += ' is up.'
else:
units = northaz[1]
if units.casefold().startswith('degree'):
units = 'degrees'
s += ' azimuth is {:.2f} {} from the +x direction.'.format(*f_pair(northaz))
sents.append(s)
if subsolargroundaz is not None or abovehoriz is not None:
s = 'The Sun is'
if abovehoriz is not None:
s += ' {:.2f} {} above the horizon'.format(*f_pair(abovehoriz))
if subsolargroundaz is not None:
s += ' at an azimuth of {:.2f} {} from North'.format(*f_pair(subsolargroundaz))
s += '.'
sents.append(s)
return ' '.join(sents)
def get_sentences(elem: dict) -> list:
s = list()
try:
s.append(inst_sentence(elem.get('scname'),
elem.get('instrument'),
elem.get('time'),
elem.get('productid')))
except ValueError:
pass
try:
s.append(mapping_sentence(elem.get('projection'),
elem.get('pixelres')))
except ValueError:
pass
try:
s.append(campt_sentence(elem.get('northaz'),
elem.get('subsolargroundaz'),
elem.get('abovehoriz')))
except ValueError:
pass
return s
if __name__ == "__main__":
sys.exit(main())