-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_cmr_response.py
300 lines (245 loc) · 10.4 KB
/
parse_cmr_response.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
import logging
from defusedxml.lxml import fromstring
import datetime
from .fields import get_field_paths, attr_path
import re
def parse_cmr_response(r, req_fields):
"""
Convert echo10 xml to results list used by output translators
"""
logging.debug('parsing CMR results')
try:
root = fromstring(r.text.encode('latin-1'))
except Exception as e:
logging.error(f'CMR parsing error: {e} when parsing: {r.text}')
return
num_results = len(root.xpath('/results/result/Granule'))
logging.debug(f'Found {num_results} results in this page')
for granule in root.xpath('/results/result/Granule'):
yield parse_granule(granule, req_fields)
def parse_granule(granule, req_fields):
req_fields = req_fields.copy() # gotta copy this list because we're gonna thrash it
def get_val(path):
r = granule.xpath(path)
if r is not None and len(r) > 0:
return r[0].text
else:
return None
def get_all_vals(path):
r = granule.xpath(path)
if r is not None and len(r) > 0:
return [v.text for v in r]
else:
return None
def remove_field(f):
try:
req_fields.remove(f)
except ValueError:
pass
field_paths = get_field_paths()
result = {}
# Handle a few special cases
if any(field in req_fields for field in ['shape', 'stringFootprint']):
shape, wkt_shape = wkt_from_gpolygon(
granule.xpath('./Spatial/HorizontalSpatialDomain/Geometry/GPolygon')
)
result['shape'] = shape
result['stringFootprint'] = wkt_shape
remove_field('shape')
remove_field('stringFootprint')
if any(field in req_fields for field in ['platform', 'frameNumber', 'canInsar']):
platform = get_val(attr_path('ASF_PLATFORM'))
if platform is None:
platform = get_val('./Platforms/Platform/ShortName')
result['platform'] = platform
remove_field('platform')
if 'frameNumber' in req_fields:
asf_frame_platforms = [
'Sentinel-1A', 'Sentinel-1B', 'ALOS', 'SENTINEL-1A', 'SENTINEL-1B',
'ERS-1', 'ERS-2', 'JERS-1', 'RADARSAT-1'
]
if result['platform'] in asf_frame_platforms:
frame_type = 'FRAME_NUMBER'
else:
frame_type = 'CENTER_ESA_FRAME'
result['frameNumber'] = get_val(attr_path(frame_type))
remove_field('frameNumber')
if 'browse' in req_fields:
result['browse'] = get_browse_urls(granule, './AssociatedBrowseImageUrls')
remove_field('browse')
if 'fileName' in req_fields:
file_name = get_val("./OnlineAccessURLs/OnlineAccessURL/URL")
result['fileName'] = file_name.split('/')[-1] if file_name else None
remove_field('fileName')
if 'stateVectors' in req_fields or ('canInsar' in req_fields and result['platform'] not in ['ALOS', 'RADARSAT-1', 'JERS-1', 'ERS-1', 'ERS-2']):
def parse_sv(sv):
def float_or_none(a):
try:
return float(a)
except ValueError:
return None
if sv is None:
return (None, None)
(x, y, z, t) = sv.split(',')
v = [float_or_none(x), float_or_none(y), float_or_none(z)]
if None not in v:
return (v, t if datetime.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f') is not None else None)
else:
return (None, None)
result['sv_pos_pre'], result['sv_t_pos_pre'] = parse_sv(get_val(attr_path('SV_POSITION_PRE')))
result['sv_pos_post'], result['sv_t_pos_post'] = parse_sv(get_val(attr_path('SV_POSITION_POST')))
result['sv_vel_pre'], result['sv_t_vel_pre'] = parse_sv(get_val(attr_path('SV_VELOCITY_PRE')))
result['sv_vel_post'], result['sv_t_vel_post'] = parse_sv(get_val(attr_path('SV_VELOCITY_POST')))
result['baseline'] = {
'stateVectors': {
'positions': {
'prePosition': result['sv_pos_pre'],
'postPosition': result['sv_pos_post'],
'prePositionTime': result['sv_t_pos_pre'],
'postPositionTime': result['sv_t_pos_post']
},
'velocities': {
'preVelocity': result['sv_vel_pre'],
'postVelocity': result['sv_vel_post'],
'preVelocityTime': result['sv_t_vel_pre'],
'postVelocityTime': result['sv_t_vel_post']
},
},
'ascendingNodeTime': get_val(attr_path('ASC_NODE_TIME')),
}
remove_field('stateVectors')
if 'canInsar' in req_fields:
if result['platform'] in ['ALOS', 'RADARSAT-1', 'JERS-1', 'ERS-1', 'ERS-2']:
result['insarGrouping'] = get_val(field_paths['insarGrouping'])
insarBaseline = get_val(field_paths['insarBaseline'])
if insarBaseline is not None:
insarBaseline = float(insarBaseline)
result['baseline'] = {
'insarBaseline': insarBaseline
}
remove_field('insarGrouping')
if result['insarGrouping'] not in [None, 0, '0', 'NA', 'NULL']:
result['canInsar'] = True
else:
result['canInsar'] = False
elif None not in [
result['sv_pos_pre'], result['sv_pos_post'],
result['sv_vel_pre'], result['sv_vel_post'],
result['sv_t_pos_pre'], result['sv_t_pos_post'],
result['sv_t_vel_pre'], result['sv_t_vel_post']]:
result['canInsar'] = True
else:
result['canInsar'] = False
remove_field('canInsar')
# These fields are always None or NA and should be fully deprecated/removed in the future
deprecated_fields = [
'beamSwath',
'catSceneId',
'formatName',
'frequency',
'incidenceAngle',
'masterGranule',
'percentCoherence',
'percentTroposphere',
'percentUnwrapped',
'sarSceneId',
'sceneDateString',
'slaveGranule',
'status',
'varianceTroposphere'
]
for f in deprecated_fields:
if f in req_fields:
result[f] = None
try:
req_fields.remove(f)
except ValueError:
pass
# Parse any remaining needed fields from the CMR response
multi_fields = [
'absoluteOrbit',
'baseline'
]
for field in req_fields:
if field in multi_fields:
result[field] = get_all_vals(field_paths[field])
else:
result[field] = get_val(field_paths[field])
for k in result:
if result[k] in ['NULL', 'NA', 'None']:
result[k] = None
if get_val(field_paths['processingLevel']) == 'BURST':
result['granuleName'] = get_val(field_paths['product_file_id'])
result['beamMode'] = get_val(attr_path('BEAM_MODE'))
result['sizeMB'] = float(get_val(attr_path('BYTE_LENGTH'))) / 1000000
urls = get_all_vals('./OnlineResources/OnlineResource/URL')
if len(urls):
result['downloadUrl'] = urls[0]
result['fileName'] = result['granuleName'] + '.' + urls[0].split('.')[-1]
def get_all_urls():
accessPath = './OnlineAccessURLs/OnlineAccessURL/URL'
resourcesPath = './OnlineResources/OnlineResource/URL'
access_urls = get_all_vals(accessPath)
if access_urls is None:
access_urls = []
resource_urls = get_all_vals(resourcesPath)
if resource_urls is None:
resource_urls = []
return list(set([*access_urls, *resource_urls]))
def get_http_urls():
return [url for url in get_all_urls() if not url.endswith('.md5') and not url.startswith('s3://') and not 's3credentials' in url]
def get_s3_urls():
return [url for url in get_all_urls() if not url.endswith('.md5') and (url.startswith('s3://') or 's3credentials' in url)]
if result.get('product_file_id', '').startswith('OPERA'):
result['beamMode'] = get_val(attr_path('BEAM_MODE'))
result['additionalUrls'] = get_http_urls()
result['configurationName'] = "Interferometric Wide. 250 km swath, 5 m x 20 m spatial resolution and burst synchronization for interferometry. IW is considered to be the standard mode over land masses."
if (providerbrowseUrls := get_all_vals('./AssociatedBrowseImageUrls/ProviderBrowseUrl/URL')):
result['browse'] = [url for url in providerbrowseUrls if not url.startswith('s3://')]
if 'STATIC' in result['processingLevel']:
result['validityStartDate'] = get_val('./Temporal/SingleDateTime')
elif result.get('product_file_id', '').startswith('S1-GUNW') and result.get('ariaVersion') is None:
version_unformatted = result.get('granuleName').split('v')[-1]
result['ariaVersion'] = re.sub(r'[^0-9\.]', '', version_unformatted.replace("_", '.'))
if result.get('platform', '') == 'NISAR':
result['additionalUrls'] = get_http_urls()
result['s3Urls'] = get_s3_urls()
return result
def wkt_from_gpolygon(gpoly):
"""
for kml generation
"""
shapes = []
for g in gpoly:
shapes.append([])
for point in g.iter('Point'):
shapes[-1].append({
'lon': point.findtext('PointLongitude'),
'lat': point.findtext('PointLatitude')
})
if shape_not_closed(shapes):
# Close the shape if needed
shapes[-1].append(shapes[-1][0])
if len(shapes):
longest = shapes[0]
for shape in shapes:
if len(shape) > len(longest):
longest = shape
wkt_shape = 'POLYGON(({0}))'.format(
','.join(['{0} {1}'.format(x['lon'], x['lat']) for x in longest])
)
return longest, wkt_shape
return '', ''
def shape_not_closed(shapes):
return (
shapes[-1][0]['lat'] != shapes[-1][-1]['lat'] or
shapes[-1][0]['lon'] != shapes[-1][-1]['lon']
)
def get_browse_urls(granule, browse_path):
browse_elems = granule.xpath(browse_path)
browseList = []
for b in browse_elems:
browseList.extend(b.findall('ProviderBrowseUrl'))
browseUrls = [''.join(b.itertext()).strip() for b in browseList]
browseUrls.sort()
return browseUrls