-
Notifications
You must be signed in to change notification settings - Fork 2
/
s1_isce2.py
506 lines (401 loc) · 19.9 KB
/
s1_isce2.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import copy
import logging
import os
import sys
import textwrap
from datetime import datetime, timedelta
from pathlib import Path
from typing import List
import numpy as np
from autoRIFT import __version__ as version
from hyp3lib.fetch import download_file
from hyp3lib.scene import get_download_url
from netCDF4 import Dataset
from osgeo import gdal, osr
from s1_orbits import fetch_for_scene
from hyp3_autorift import geometry, utils
from hyp3_autorift.process import DEFAULT_PARAMETER_FILE
log = logging.getLogger(__name__)
def get_s1_primary_polarization(granule_name):
polarization = granule_name[14:16]
if polarization in ['SV', 'DV']:
return 'vv'
if polarization in ['SH', 'DH']:
return 'hh'
raise ValueError(f'Cannot determine co-polarization of granule {granule_name}')
def process_sentinel1_with_isce2(reference, secondary, parameter_file):
import isce # noqa
from topsApp import TopsInSAR
from hyp3_autorift.vend.testGeogrid_ISCE import loadMetadata, runGeogrid
from hyp3_autorift.vend.testautoRIFT_ISCE import generateAutoriftProduct
for scene in [reference, secondary]:
scene_url = get_download_url(scene)
download_file(scene_url, chunk_size=5242880)
orbits = Path('Orbits').resolve()
orbits.mkdir(parents=True, exist_ok=True)
reference_state_vec = fetch_for_scene(reference, dir=orbits)
log.info(f'Downloaded orbit file {reference_state_vec} from s1-orbits')
secondary_state_vec = fetch_for_scene(secondary, dir=orbits)
log.info(f'Downloaded orbit file {secondary_state_vec} from s1-orbits')
polarization = get_s1_primary_polarization(reference)
lat_limits, lon_limits = bounding_box(f'{reference}.zip', polarization=polarization, orbits=str(orbits))
scene_poly = geometry.polygon_from_bbox(x_limits=lat_limits, y_limits=lon_limits)
parameter_info = utils.find_jpl_parameter_info(scene_poly, parameter_file)
isce_dem = prep_isce_dem(parameter_info['geogrid']['dem'], lat_limits, lon_limits)
format_tops_xml(reference, secondary, polarization, isce_dem, orbits)
insar = TopsInSAR(name='topsApp', cmdline=['topsApp.xml', '--end=mergebursts'])
insar.configure()
insar.run()
reference_path = os.path.join(os.getcwd(), 'merged', 'reference.slc.full')
secondary_path = os.path.join(os.getcwd(), 'merged', 'secondary.slc.full')
for slc in [reference_path, secondary_path]:
gdal.Translate(slc, f'{slc}.vrt', format='ENVI')
meta_r = loadMetadata('fine_coreg')
meta_s = loadMetadata('secondary')
geogrid_info = runGeogrid(meta_r, meta_s, epsg=parameter_info['epsg'], **parameter_info['geogrid'])
# NOTE: After Geogrid is run, all drivers are no longer registered.
# I've got no idea why, or if there are other affects...
gdal.AllRegister()
netcdf_file = generateAutoriftProduct(
reference_path, secondary_path, nc_sensor='S1', optical_flag=False, ncname=None,
geogrid_run_info=geogrid_info, **parameter_info['autorift'],
parameter_file=parameter_file.replace('/vsicurl/', ''),
)
return netcdf_file
def write_conversion_file(
*,
file_name: str,
srs: osr.SpatialReference,
epsg: int,
tran: List[float],
x: np.ndarray,
y: np.ndarray,
M11: np.ndarray,
M12: np.ndarray,
dr_2_vr_factor: float,
ChunkSize: List[int],
NoDataValue: int = -32767,
noDataMask: np.ndarray,
parameter_file: str
) -> str:
nc_outfile = Dataset(file_name, 'w', clobber=True, format='NETCDF4')
nc_outfile.setncattr('GDAL_AREA_OR_POINT', 'Area')
nc_outfile.setncattr('Conventions', 'CF-1.8')
nc_outfile.setncattr('date_created', datetime.now().strftime("%d-%b-%Y %H:%M:%S"))
nc_outfile.setncattr('title', 'autoRIFT S1 Corrections')
nc_outfile.setncattr('autoRIFT_software_version', version)
nc_outfile.setncattr('autoRIFT_parameter_file', parameter_file)
nc_outfile.createDimension('x', len(x))
nc_outfile.createDimension('y', len(y))
var = nc_outfile.createVariable('x', np.dtype('float64'), 'x', fill_value=None)
var.setncattr('standard_name', 'projection_x_coordinate')
var.setncattr('description', 'x coordinate of projection')
var.setncattr('units', 'm')
var[:] = x
var = nc_outfile.createVariable('y', np.dtype('float64'), 'y', fill_value=None)
var.setncattr('standard_name', 'projection_y_coordinate')
var.setncattr('description', 'y coordinate of projection')
var.setncattr('units', 'm')
var[:] = y
mapping_var_name = 'mapping'
var = nc_outfile.createVariable(mapping_var_name, 'U1', (), fill_value=None)
if srs.GetAttrValue('PROJECTION') == 'Polar_Stereographic':
var.setncattr('grid_mapping_name', 'polar_stereographic')
var.setncattr('straight_vertical_longitude_from_pole', srs.GetProjParm('central_meridian'))
var.setncattr('false_easting', srs.GetProjParm('false_easting'))
var.setncattr('false_northing', srs.GetProjParm('false_northing'))
var.setncattr('latitude_of_projection_origin', np.sign(srs.GetProjParm('latitude_of_origin')) * 90.0)
var.setncattr('latitude_of_origin', srs.GetProjParm('latitude_of_origin'))
var.setncattr('semi_major_axis', float(srs.GetAttrValue('GEOGCS|SPHEROID', 1)))
var.setncattr('scale_factor_at_projection_origin', 1)
var.setncattr('inverse_flattening', float(srs.GetAttrValue('GEOGCS|SPHEROID', 2)))
var.setncattr('spatial_ref', srs.ExportToWkt())
var.setncattr('crs_wkt', srs.ExportToWkt())
var.setncattr('proj4text', srs.ExportToProj4())
var.setncattr('spatial_epsg', epsg)
var.setncattr('GeoTransform', ' '.join(str(x) for x in tran))
elif srs.GetAttrValue('PROJECTION') == 'Transverse_Mercator':
var.setncattr('grid_mapping_name', 'universal_transverse_mercator')
zone = epsg - np.floor(epsg / 100) * 100
var.setncattr('utm_zone_number', zone)
var.setncattr('longitude_of_central_meridian', srs.GetProjParm('central_meridian'))
var.setncattr('false_easting', srs.GetProjParm('false_easting'))
var.setncattr('false_northing', srs.GetProjParm('false_northing'))
var.setncattr('latitude_of_projection_origin', srs.GetProjParm('latitude_of_origin'))
var.setncattr('semi_major_axis', float(srs.GetAttrValue('GEOGCS|SPHEROID', 1)))
var.setncattr('scale_factor_at_central_meridian', srs.GetProjParm('scale_factor'))
var.setncattr('inverse_flattening', float(srs.GetAttrValue('GEOGCS|SPHEROID', 2)))
var.setncattr('spatial_ref', srs.ExportToWkt())
var.setncattr('crs_wkt', srs.ExportToWkt())
var.setncattr('proj4text', srs.ExportToProj4())
var.setncattr('spatial_epsg', epsg)
var.setncattr('GeoTransform', ' '.join(str(x) for x in tran))
else:
raise Exception(f'Projection {srs.GetAttrValue("PROJECTION")} not recognized for this program')
var = nc_outfile.createVariable('M11', np.dtype('float32'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'conversion_matrix_element_11')
var.setncattr(
'description',
'conversion matrix element (1st row, 1st column) that can be multiplied with vx to give range pixel '
'displacement dr (see Eq. A18 in https://www.mdpi.com/2072-4292/13/4/749)'
)
var.setncattr('units', 'pixel/(meter/year)')
var.setncattr('grid_mapping', mapping_var_name)
var.setncattr('dr_to_vr_factor', dr_2_vr_factor)
var.setncattr('dr_to_vr_factor_description', 'multiplicative factor that converts slant range '
'pixel displacement dr to slant range velocity vr')
M11[noDataMask] = NoDataValue
var[:] = M11
var = nc_outfile.createVariable('M12', np.dtype('float32'), ('y', 'x'), fill_value=NoDataValue,
zlib=True, complevel=2, shuffle=True, chunksizes=ChunkSize)
var.setncattr('standard_name', 'conversion_matrix_element_12')
var.setncattr(
'description',
'conversion matrix element (1st row, 2nd column) that can be multiplied with vy to give range pixel '
'displacement dr (see Eq. A18 in https://www.mdpi.com/2072-4292/13/4/749)'
)
var.setncattr('units', 'pixel/(meter/year)')
var.setncattr('grid_mapping', mapping_var_name)
var.setncattr('dr_to_vr_factor', dr_2_vr_factor)
var.setncattr('dr_to_vr_factor_description',
'multiplicative factor that converts slant range pixel displacement dr to slant range velocity vr')
M12[noDataMask] = NoDataValue
var[:] = M12
nc_outfile.sync()
nc_outfile.close()
return file_name
def create_conversion_matrices(
*,
scene: str,
grid_location: str = 'window_location.tif',
offset2vx: str = 'window_rdr_off2vel_x_vec.tif',
offset2vy: str = 'window_rdr_off2vel_y_vec.tif',
scale_factor: str = 'window_scale_factor.tif',
epsg: int = 4326,
parameter_file: str = DEFAULT_PARAMETER_FILE,
**kwargs,
) -> Path:
xGrid, tran, _, srs, nodata = utils.load_geospatial(grid_location, band=1)
offset2vy_1, _, _, _, _ = utils.load_geospatial(offset2vy, band=1)
offset2vy_1[offset2vy_1 == nodata] = np.nan
offset2vy_2, _, _, _, _ = utils.load_geospatial(offset2vy, band=2)
offset2vy_2[offset2vy_2 == nodata] = np.nan
offset2vx_1, _, _, _, _ = utils.load_geospatial(offset2vx, band=1)
offset2vx_1[offset2vx_1 == nodata] = np.nan
offset2vx_2, _, _, _, _ = utils.load_geospatial(offset2vx, band=2)
offset2vx_2[offset2vx_2 == nodata] = np.nan
offset2vr, _, _, _, _ = utils.load_geospatial(offset2vx, band=3)
offset2vr[offset2vr == nodata] = np.nan
scale_factor_1, _, _, _, _ = utils.load_geospatial(scale_factor, band=1)
scale_factor_1[scale_factor_1 == nodata] = np.nan
# GDAL using upper-left of pixel -> netCDF using center of pixel
tran = [tran[0] + tran[1] / 2, tran[1], 0.0, tran[3] + tran[5] / 2, 0.0, tran[5]]
dimidY, dimidX = xGrid.shape
noDataMask = xGrid == nodata
y = np.arange(tran[3], tran[3] + tran[5] * dimidY, tran[5])
x = np.arange(tran[0], tran[0] + tran[1] * dimidX, tran[1])
chunk_lines = np.min([np.ceil(8192 / dimidY) * 128, dimidY])
ChunkSize = [chunk_lines, dimidX]
M11 = offset2vy_2 / (offset2vx_1 * offset2vy_2 - offset2vx_2 * offset2vy_1) / scale_factor_1
M12 = -offset2vx_2 / (offset2vx_1 * offset2vy_2 - offset2vx_2 * offset2vy_1) / scale_factor_1
dr_2_vr_factor = np.median(offset2vr[np.logical_not(np.isnan(offset2vr))])
conversion_nc = write_conversion_file(
file_name='conversion_matrices.nc', srs=srs, epsg=epsg, tran=tran, x=x, y=y, M11=M11, M12=M12,
dr_2_vr_factor=dr_2_vr_factor, ChunkSize=ChunkSize, noDataMask=noDataMask, parameter_file=parameter_file,
)
return Path(conversion_nc)
def generate_correction_data(
scene: str,
buffer: int = 0,
parameter_file: str = DEFAULT_PARAMETER_FILE,
) -> (dict, Path):
from hyp3_autorift.vend.testGeogrid_ISCE import loadParsedata, runGeogrid
scene_path = Path(f'{scene}.zip')
if not scene_path.exists():
scene_url = get_download_url(scene)
scene_path = download_file(scene_url, chunk_size=5242880)
orbits = Path('Orbits').resolve()
orbits.mkdir(parents=True, exist_ok=True)
state_vec = fetch_for_scene(scene, dir=orbits)
log.info(f'Downloaded orbit file {state_vec} from s1-orbits')
polarization = get_s1_primary_polarization(scene)
lat_limits, lon_limits = bounding_box(f'{scene}.zip', polarization=polarization, orbits=str(orbits))
scene_poly = geometry.polygon_from_bbox(x_limits=lat_limits, y_limits=lon_limits)
parameter_info = utils.find_jpl_parameter_info(scene_poly, parameter_file)
isce_dem = prep_isce_dem(parameter_info['geogrid']['dem'], lat_limits, lon_limits)
format_tops_xml(scene, scene, polarization, isce_dem, orbits)
reference_meta = loadParsedata(str(scene_path), orbit_dir=orbits, aux_dir=orbits, buffer=buffer)
secondary_meta = copy.deepcopy(reference_meta)
spoof_dt = timedelta(days=1)
secondary_meta.sensingStart += spoof_dt
secondary_meta.sensingStop += spoof_dt
geogrid_info = runGeogrid(reference_meta, secondary_meta, epsg=parameter_info['epsg'], **parameter_info['geogrid'])
# NOTE: After Geogrid is run, all drivers are no longer registered.
# I've got no idea why, or if there are other effects...
gdal.AllRegister()
conversion_nc = create_conversion_matrices(
scene=scene, epsg=parameter_info['epsg'], parameter_file=parameter_file, **parameter_info['autorift']
)
return geogrid_info, conversion_nc
class SysArgvManager:
"""Context manager to clear and reset sys.argv
A bug in the ISCE2 Application class causes sys.argv to always be parsed when
no options are proved, even when setting `cmdline=[]`, preventing programmatic use.
"""
def __init__(self):
self.argv = sys.argv.copy()
def __enter__(self):
sys.argv = sys.argv[:1]
def __exit__(self, exc_type, exc_val, exc_tb):
sys.argv = self.argv
def get_topsinsar_config():
from isce.applications.topsApp import TopsInSAR
with SysArgvManager():
insar = TopsInSAR(name="topsApp")
insar.configure()
config_data = {}
for name in ['reference', 'secondary']:
scene = insar.__getattribute__(name)
sensing_times = []
for swath in range(1, 4):
scene.configure()
scene.swathNumber = swath
scene.parse()
sensing_times.append(
(scene.product.sensingStart, scene.product.sensingStop)
)
sensing_start = min([sensing_time[0] for sensing_time in sensing_times])
sensing_stop = max([sensing_time[1] for sensing_time in sensing_times])
sensing_dt = (sensing_stop - sensing_start) / 2 + sensing_start
config_data[f'{name}_filename'] = Path(scene.safe[0]).name
config_data[f'{name}_dt'] = sensing_dt.strftime("%Y%m%dT%H:%M:%S.%f").rstrip('0')
return config_data
def format_tops_xml(reference, secondary, polarization, dem, orbits, xml_file='topsApp.xml'):
xml_template = f""" <?xml version="1.0" encoding="UTF-8"?>
<topsApp>
<component name="topsinsar">
<component name="reference">
<property name="orbit directory">{orbits}</property>
<property name="auxiliary data directory">{orbits}</property>
<property name="output directory">reference</property>
<property name="safe">['{reference}.zip']</property>
<property name="polarization">{polarization}</property>
</component>
<component name="secondary">
<property name="orbit directory">{orbits}</property>
<property name="auxiliary data directory">{orbits}</property>
<property name="output directory">secondary</property>
<property name="safe">['{secondary}.zip']</property>
<property name="polarization">{polarization}</property>
</component>
<property name="demfilename">{dem}</property>
<property name="do interferogram">False</property>
<property name="do dense offsets">True</property>
<property name="do ESD">False</property>
<property name="do unwrap">False</property>
<property name="do unwrap 2 stage">False</property>
<property name="ampcor skip width">32</property>
<property name="ampcor skip height">32</property>
<property name="ampcor search window width">51</property>
<property name="ampcor search window height">51</property>
<property name="ampcor window width">32</property>
<property name="ampcor window height">32</property>
</component>
</topsApp>
"""
with open(xml_file, 'w') as f:
f.write(textwrap.dedent(xml_template))
def bounding_box(safe, priority='reference', polarization='hh', orbits='Orbits', epsg=4326):
"""Determine the geometric bounding box of a Sentinel-1 image
:param safe: Path to the Sentinel-1 SAFE zip archive
:param priority: Image priority, either 'reference' (default) or 'secondary'
:param polarization: Image polarization (default: 'hh')
:param orbits: Path to the orbital files (default: './Orbits')
:param epsg: Projection EPSG code (default: 4326)
:return: lat_limits (list), lon_limits (list)
lat_limits: list containing the [minimum, maximum] latitudes
lat_limits: list containing the [minimum, maximum] longitudes
"""
import isce # noqa: F401
from contrib.geo_autoRIFT.geogrid import Geogrid
from isceobj.Orbit.Orbit import Orbit
from isceobj.Sensor.TOPS.Sentinel1 import Sentinel1
frames = []
for swath in range(1, 4):
rdr = Sentinel1()
rdr.configure()
rdr.safe = [os.path.abspath(safe)]
rdr.output = priority
rdr.orbitDir = os.path.abspath(orbits)
rdr.auxDir = os.path.abspath(orbits)
rdr.swathNumber = swath
rdr.polarization = polarization
rdr.parse()
frames.append(rdr.product)
first_burst = frames[0].bursts[0]
sensing_start = min([x.sensingStart for x in frames])
sensing_stop = max([x.sensingStop for x in frames])
starting_range = min([x.startingRange for x in frames])
far_range = max([x.farRange for x in frames])
range_pixel_size = first_burst.rangePixelSize
prf = 1.0 / first_burst.azimuthTimeInterval
orb = Orbit()
orb.configure()
for state_vector in first_burst.orbit:
orb.addStateVector(state_vector)
for frame in frames:
for burst in frame.bursts:
for state_vector in burst.orbit:
if state_vector.time < orb.minTime or state_vector.time > orb.maxTime:
orb.addStateVector(state_vector)
obj = Geogrid()
obj.configure()
obj.startingRange = starting_range
obj.rangePixelSize = range_pixel_size
obj.sensingStart = sensing_start
obj.prf = prf
obj.lookSide = -1
obj.numberOfLines = int(np.round((sensing_stop - sensing_start).total_seconds() * prf))
obj.numberOfSamples = int(np.round((far_range - starting_range)/range_pixel_size))
obj.orbit = orb
obj.epsg = epsg
obj.determineBbox()
lat_limits = obj._xlim
lon_limits = obj._ylim
log.info(f'Latitude limits [min, max]: {lat_limits}')
log.info(f'Longitude limits [min, max]: {lon_limits}')
return lat_limits, lon_limits
def prep_isce_dem(input_dem, lat_limits, lon_limits, isce_dem=None):
import isce # noqa: F401
import isceobj
from contrib.demUtils import createDemStitcher
if isce_dem is None:
seamstress = createDemStitcher()
isce_dem = seamstress.defaultName([*lat_limits, *lon_limits])
isce_dem = os.path.abspath(isce_dem + '.wgs84')
log.info(f'ISCE dem is: {isce_dem}')
in_ds = gdal.OpenShared(input_dem, gdal.GA_ReadOnly)
warp_options = gdal.WarpOptions(
format='ENVI', outputType=gdal.GDT_Int16, resampleAlg='cubic',
xRes=0.001, yRes=0.001, dstSRS='EPSG:4326', dstNodata=0,
outputBounds=[lon_limits[0], lat_limits[0], lon_limits[1], lat_limits[1]]
)
gdal.Warp(isce_dem, in_ds, options=warp_options)
del in_ds
isce_ds = gdal.Open(isce_dem, gdal.GA_ReadOnly)
isce_trans = isce_ds.GetGeoTransform()
img = isceobj.createDemImage()
img.width = isce_ds.RasterXSize
img.length = isce_ds.RasterYSize
img.bands = 1
img.dataType = 'SHORT'
img.scheme = 'BIL'
img.setAccessMode('READ')
img.filename = isce_dem
img.firstLongitude = isce_trans[0] + 0.5 * isce_trans[1]
img.deltaLongitude = isce_trans[1]
img.firstLatitude = isce_trans[3] + 0.5 * isce_trans[5]
img.deltaLatitude = isce_trans[5]
img.renderHdr()
return isce_dem