-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreadFiles.py
291 lines (228 loc) · 9.93 KB
/
readFiles.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
#-------------------------------------------------
#-------------------------------------------------
#
# Class readFiles
#
#-------------------------------------------------
#-------------------------------------------------
# Import all
import os
import numpy as np
import matplotlib.pyplot as plt
from numpy.lib.stride_tricks import as_strided
#--------------------
# Class definition
#--------------------
class readFiles(object):
'''
A class to read:
- interferogram
- mask
Implemented for:
- ISCE
- Roi_pac
Feel free to add your own reader.
Written by A. Benoit, B. Pinel-Puyssegur and R. Jolivet 2019
Licence:
PhaCo: Phase unwrapping errors Correction
Copyright (C) 2019 <Angelique Benoit, Beatrice Pinel-Puyssegur and Romain Jolivet>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
#--------------------
# Initialize
#--------------------
def __init__(self):
'''
Initialize the class.
'''
# All done
return
#--------------------
# Read ISCE files
#--------------------
def readIsce(self, dates, filesDir, igramName):
'''
Read files from ISCE software.
Args:
* dates : Dates of the interferogram (list)
* filesDir : Files directory (str)
* igramName : Interferogram filename (str)
'''
# Import ISCE stuff
import isce
import isceobj
from isceobj.Util.ImageUtil import ImageLib as IML
#-------------------------------------------------
# READ IGRAM
# Igram filenames (wrapped, unwrapped and corrected)
ifgInt = '{}/{}_{}/{}.int'.format(filesDir,dates[0],dates[1],os.path.splitext(os.path.basename(igramName))[0])
ifgFile = '{}/{}_{}/{}'.format(filesDir,dates[0],dates[1],igramName)
ifgFile_corrected = '{}_corrected{}'.format(os.path.splitext(ifgFile)[0],os.path.splitext(ifgFile)[1])
# Igram.unw xml infos
imgIfgFile = isceobj.createImage()
imgIfgFile.load(ifgFile + '.xml')
bands = imgIfgFile.bands
length = imgIfgFile.getLength()
width = imgIfgFile.getWidth()
scheme = imgIfgFile.scheme
# Igram.int xml infos
imgInt = isceobj.createImage()
imgInt.load(ifgInt + '.xml')
bandsInt = imgInt.bands
schemeInt = imgInt.scheme
datatypeInt = IML.NUMPY_type(imgInt.dataType)
# Igram not yet corrected
if not os.path.exists(ifgFile_corrected):
# Read igram.unw
data = IML.memmap(ifgFile, mode='readonly', nchannels=bands, nyy=length, nxx=width, scheme=scheme)
# Create the corrected igram
data_corrected = IML.memmap(ifgFile_corrected, mode='write', nchannels=bands, nyy=length, nxx=width, scheme=scheme)
# Copy igram.unw in the new file
data_corrected.bands[0][:,:] = data.bands[0][:,:]
data_corrected.bands[1][:,:] = data.bands[1][:,:]
# New xml infos
data_correctedXml = isceobj.Image.createUnwImage()
data_correctedXml.bands = 2
data_correctedXml.setWidth(width)
data_correctedXml.setLength(length)
data_correctedXml.setFilename(ifgFile_corrected)
data_correctedXml.renderHdr()
data_correctedXml.renderVRT()
# Flag
flag_corr = True
# Igram already corrected, start from it
else:
# Read igram.unw already corrected
data = IML.memmap(ifgFile_corrected, mode='readwrite', nchannels=bands, nyy=length, nxx=width, scheme=scheme)
# Flag
flag_corr = False
# Read wrap igram where to compute misclosure
data_int = IML.memmap(ifgInt, mode='readonly', nchannels=bandsInt, dataType=datatypeInt, nyy=length, nxx=width, scheme=schemeInt)
#-------------------------------------------------
# READ MASK
# Initialize the mask
mask = np.zeros((length, width))
#-------------------------------------------------
# SAVE
# Save things
if flag_corr:
self.phase = data_corrected.bands[1]
else:
self.phase = data.bands[1]
self.phase_int = data_int.bands[0]
self.dates = dates
self.igramsDir = filesDir
self.igramName = igramName
self.width = width
self.length = length
self.ifgFile_corrected = ifgFile_corrected
# Close files
del data
del data_int
# All done
return
#--------------------
# Read ROI_PAC files
#--------------------
def readRoiPac(self, dates, filesDir, igramName, length_max):
'''
Read files from roi_pac software.
Args:
* dates : Dates of the interferogram (list)
* filesDir : Files directory (str)
* igramName : Interferogram filename, without the date (str)
* length_max : Maximum length of igrams (int)
'''
from osgeo import gdal
#-------------------------------------------------
# READ IGRAM
# Format of the igramName (add the date in the name)
b = igramName.split('_')[:]
b.insert(2, "{}-{}".format(dates[0],dates[1]))
igramName_tmp = "_".join(b)
igramName_tmp_int = "_".join(b[:-1])
# Igram filenames (wrapped, unwrapped and corrected)
ifgInt = '{}/{}_{}/{}.int'.format(filesDir,dates[0],dates[1],os.path.splitext(os.path.basename(igramName_tmp_int))[0])
ifgFile = '{}/{}_{}/{}'.format(filesDir,dates[0],dates[1],igramName_tmp)
ifgFile_corrected = '{}_corrected{}'.format(os.path.splitext(ifgFile)[0],os.path.splitext(ifgFile)[1])
# CREATING EVEN INDICES
even_ind = np.arange(length_max) * 2
# List of memmap objects (amplitude and phase)
acc = []
# Igram not yet corrected
if not os.path.exists(ifgFile_corrected):
# Read .unw and copy into the new file
ds = gdal.Open(r'{}'.format(ifgFile))
# The 2 following lines create the .rsc file associated to the corrected interferogram
driver = ds.GetDriver()
outDs = driver.Create(ifgFile_corrected,ds.RasterXSize,length_max, 2, gdal.GDT_Float32)
# Reshape with max length
newrows = np.zeros((2*(length_max - ds.RasterYSize), ds.RasterXSize))
data = np.memmap(ifgFile, dtype='float32', mode='r', shape=(2 * ds.RasterYSize, ds.RasterXSize)) # lecture du BIL
# Create the corrected igram
nshape = 2*length_max*ds.RasterXSize
data_corrected = np.memmap(ifgFile_corrected, dtype='float32', mode='readwrite', shape = (nshape,))
data_corrected[2*ds.RasterYSize*ds.RasterXSize:2*length_max*ds.RasterXSize] = np.zeros(2*(length_max - ds.RasterYSize)*ds.RasterXSize)
data_corrected[0:2*ds.RasterYSize*ds.RasterXSize] = np.reshape(data, 2*ds.RasterYSize*ds.RasterXSize)
# Cut data_corrected in 2 bands for np.memmap
fsize = np.zeros(1, dtype='float32').itemsize
nstrides = (2*ds.RasterXSize*fsize, fsize)
for band in range(2):
noffset = band*ds.RasterXSize
tmap = data_corrected[noffset:]
fmap = as_strided(tmap, shape=(length_max,ds.RasterXSize), strides=nstrides)
acc.append(fmap)
# Close files
del data
else:
# Read igram.unw already corrected
ds = gdal.Open(r'{}'.format(ifgFile_corrected))
nshape = 2*length_max*ds.RasterXSize
data_corrected = np.memmap(ifgFile_corrected, dtype='float32', mode='readwrite', shape = (nshape,))
# Cut data_corrected in 2 bands for np.memmap
fsize = np.zeros(1, dtype='float32').itemsize
nstrides = (2*ds.RasterXSize*fsize, fsize)
for band in range(2):
noffset = band*ds.RasterXSize
tmap = data_corrected[noffset:]
fmap = as_strided(tmap, shape=(length_max,ds.RasterXSize), strides=nstrides)
acc.append(fmap)
# Read .int
ds_int = gdal.Open(r'{}'.format(ifgInt))
data_int = ds_int.GetRasterBand(1).ReadAsArray()
if ds_int.RasterYSize != length_max:
newrows = np.zeros(((length_max - ds_int.RasterYSize), ds_int.RasterXSize))
data_int = np.vstack([data_int, newrows])
#-------------------------------------------------
# CREATE MASK
# Initialize the mask
mask = np.zeros((length_max, ds.RasterXSize))
# Mask constructed from the .unw
positions_1 = np.where(acc[1] != 0.)
for x,y in zip(positions_1[0],positions_1[1]):
mask[x,y] = 1
#-------------------------------------------------
# SAVE
# Save things
self.phase = acc[1]
self.phase_int = data_int
self.dates = dates
self.igramsDir = filesDir
self.igramName = igramName
self.mask = mask
self.width = ds.RasterXSize
self.length = length_max
self.ifgFile_corrected = ifgFile_corrected
# Close files
del data_int
# All done
return