-
Notifications
You must be signed in to change notification settings - Fork 31
/
surfaceslip.py
704 lines (549 loc) · 19.5 KB
/
surfaceslip.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
'''
A class that deals with surface slip data
Written by R. Jolivet in 2021
'''
# Externals
import numpy as np
import pyproj as pp
import matplotlib.pyplot as plt
import matplotlib.path as path
import scipy.spatial.distance as scidis
import copy
import sys, os
# Personals
from .SourceInv import SourceInv
from .geodeticplot import geodeticplot as geoplot
from . import csiutils as utils
class surfaceslip(SourceInv):
'''
Args:
* name : Name of the surfaceslip dataset
Kwargs:
* utmzone : UTM zone. (optional, default is 10 (Western US))
* lon0 : Longitude of the utmzone
* lat0 : Latitude of the utmzone
* ellps : ellipsoid (optional, default='WGS84')
Returns:
* None
'''
def __init__(self, name, utmzone=None, ellps='WGS84', verbose=True, lon0=None, lat0=None):
# Base class init
super(surfaceslip,self).__init__(name,
utmzone=utmzone,
ellps=ellps,
lon0=lon0,
lat0=lat0)
# Initialize the data set
self.dtype = 'surfaceslip'
if verbose:
print ("---------------------------------")
print ("---------------------------------")
print ("Initialize Surface Slip data set {}".format(self.name))
self.verbose = verbose
# Initialize some things
self.vel = None
self.synth = None
self.err = None
self.lon = None
self.lat = None
self.Cd = None
# This is in case surface slip is in the LOS of the satellite
self.los = None
# All done
return
def checkZeros(self):
'''
Checks and remove data points that have Zeros in vel, lon or lat
'''
# Check
if self.vel is not None:
uVel = np.flatnonzero(self.vel==0.)
else:
uVel = np.array([])
# Reject data
self.reject(uVel)
# All done
return
def checkNaNs(self):
'''
Checks and remove data points that have NaNs in vel, err, lon, lat.
'''
# Check
if self.vel is not None:
uVel = np.flatnonzero(np.isnan(self.vel))
else:
uVel = np.array([])
if self.err is not None:
uErr = np.flatnonzero(np.isnan(self.err))
else:
uErr = np.array([])
if self.lon is not None:
uLon = np.flatnonzero(np.isnan(self.lon))
else:
uLon = np.array([])
if self.lat is not None:
uLat = np.flatnonzero(np.isnan(self.lat))
else:
uLat = np.array([])
if self.los is not None:
uLos, toto = np.where(np.isnan(self.los))
uLos = np.unique(uLos.flatten())
else:
uLos = np.array([])
# Concatenate all these guys
uRemove = np.concatenate((uVel, uErr, uLon, uLat, uLos)).astype(int)
uRemove = np.unique(uRemove)
# Reject pixels
self.deletePixels(uRemove)
# All done
return
def read_from_binary(self, data, lon, lat, err=None, factor=1.0, downsample=1,
step=0.0, los=None, dtype=np.float32):
'''
Read from binary file or from array.
Args:
* data : binary array containing the data or binary file
* lon : binary arrau containing the longitude or binary file
* lat : binary array containing the latitude or binary file
Kwargs:
* err : Uncertainty (array)
* factor : multiplication factor (default is 1.0)
* step : constant added to the data (default is 0.0)
* los : LOS unit vector 3 component array (3-column array)
* dtype : data type (default is np.float32 if data is a file)
Return:
* None
'''
# Get the data
if type(data) is str:
vel = np.fromfile(data, dtype=dtype)[::downsample]*factor + step
else:
vel = data.flatten()[::downsample]*factor + step
# Get the lon
if type(lon) is str:
lon = np.fromfile(lon, dtype=dtype)[::downsample]
else:
lon = lon.flatten()[::downsample]
# Get the lat
if type(lat) is str:
lat = np.fromfile(lat, dtype=dtype)[::downsample]
else:
lat = lat.flatten()[::downsample]
# Check sizes
assert vel.shape==lon.shape, 'Something wrong with the sizes: {} {} {} '.format(vel.shape, lon.shape, lat.shape)
assert vel.shape==lat.shape, 'Something wrong with the sizes: {} {} {} '.format(vel.shape, lon.shape, lat.shape)
# Get the error
if err is not None:
if type(err) is str:
err = np.fromfile(err, dtype=dtype)[::downsample]*np.abs(factor)
else:
err = err.flatten()[::downsample]*np.abs(factor)
assert vel.shape==err.shape, 'Something wrong with the sizes: {} {}'.format(vel.shape, err.shape)
# Get the LOS
if los is not None:
if type(los) is str:
los = np.fromfile(los, dtype=dtype).reshape((vel.shape[0], 3))
assert los.shape[0]==vel.shape[0] and los.shape[1]==3, 'Something wrong with the sizes: {} {}'.format(los.shape, vel.shape)
# Set things in self
self.vel = vel
self.err = err
self.lon = lon
self.lat = lat
self.los = los
# Keep track of factor
self.factor = factor
# set lon to (0, 360.)
self._checkLongitude()
# compute x, y
self.x, self.y = self.ll2xy(self.lon, self.lat)
# All done
return
def resample(self, nSamples, method='linear', axis='lon'):
'''
Linear resampling as a function of longitude or latitude.
'''
raise NotImplemented
return
def buildCd(self):
'''
Builds a full Covariance matrix from the uncertainties. The Matrix is just a diagonal matrix.
'''
# Assert
assert self.err is not None, 'Need some uncertainties on the LOS displacements...'
# Get some size
nd = self.vel.shape[0]
# Fill Cd
self.Cd = np.diag(self.err**2)
# All done
return
def distance2point(self, lon, lat):
'''
Returns the distance of all pixels to a point.
Args:
* lon : Longitude of a point
* lat : Latitude of a point
Returns:
* array
'''
# Get coordinates
x = self.x
y = self.y
# Get point coordinates
xp, yp = self.ll2xy(lon, lat)
# compute distance
return np.sqrt( (x-xp)**2 + (y-yp)**2 )
def keepWithin(self, minlon, maxlon, minlat, maxlat):
'''
Select the pixels in a box defined by min and max, lat and lon.
Args:
* minlon : Minimum longitude.
* maxlon : Maximum longitude.
* minlat : Minimum latitude.
* maxlat : Maximum latitude.
Retunrs:
* None
'''
# Store the corners
self.minlon = minlon
self.maxlon = maxlon
self.minlat = minlat
self.maxlat = maxlat
# Select on latitude and longitude
u = np.flatnonzero((self.lat>minlat) & (self.lat<maxlat) & (self.lon>minlon) & (self.lon<maxlon))
# Do it
self.keepDatas(u)
# All done
return
def keepDatas(self, u):
'''
Keep the datas indexed u and ditch the other ones
Args:
* u : array of indexes
Returns:
* None
'''
# Select the stations
self.lon = self.lon[u]
self.lat = self.lat[u]
self.x = self.x[u]
self.y = self.y[u]
self.vel = self.vel[u]
if self.err is not None:
self.err = self.err[u]
if self.los is not None:
self.los = self.los[u]
if self.synth is not None:
self.synth = self.synth[u]
# Deal with the covariance matrix
if self.Cd is not None:
Cdt = self.Cd[u,:]
self.Cd = Cdt[:,u]
# All done
return
def deleteDatas(self, u):
'''
Delete the datas indicated by index in u.
Args:
* u : array of indexes
Returns:
* None
'''
# Select the stations
self.lon = np.delete(self.lon,u)
self.lat = np.delete(self.lat,u)
self.x = np.delete(self.x,u)
self.y = np.delete(self.y,u)
self.vel = np.delete(self.vel,u)
if self.err is not None:
self.err = np.delete(self.err,u)
if self.los is not None:
self.los = np.delete(self.los,u, axis=0)
if self.synth is not None:
self.synth = np.delete(self.synth, u)
# Deal with the covariance matrix
if self.Cd is not None:
self.Cd = np.delete(np.delete(Cd ,u, axis=0), u, axis=1)
# All done
return
def getTransformEstimator(self, trans, computeNormFact=True):
'''
Returns the Estimator for the transformation to estimate in the surfaceslip data.
The estimator is only zeros
Args:
* trans : useless
Kwargs:
* computeNormFact : useless
Returns:
* None
'''
# One case
T = np.zeros((len(self.vel), 1))
# All done
return T
def setGFsInFault(self, fault, G, vertical=True):
'''
From a dictionary of Green's functions, sets these correctly into the fault
object fault for future computation.
Args:
* fault : Instance of Fault
* G : Dictionary with 3 entries 'strikeslip', 'dipslip' and 'tensile'. These can be a matrix or None.
Kwargs:
* vertical : Set here for consistency with other data objects, but will always be set to True, whatever you do.
Returns:
* None
'''
if fault.type == 'Fault':
# Get the values
Gss = G['strikeslip']
Gds = G['dipslip']
# Here coupling and tensile make no sense
fault.setGFs(self, strikeslip=[Gss], dipslip=[Gds], tensile=[None],
coupling=[None], vertical=True)
elif fault.type == 'Pressure':
try:
GpLOS = G['pressure']
except:
GpLOS = None
try:
GdvxLOS = G['pressureDVx']
except:
GdvxLOS = None
try:
GdvyLOS = G['pressureDVy']
except:
GdvyLOS = None
try:
GdvzLOS = G['pressureDVz']
except:
GdvzLOS = None
fault.setGFs(self, deltapressure=[GpLOS],
GDVx=[GdvxLOS] , GDVy=[GdvyLOS], GDVz =[GdvzLOS],
vertical=True)
# All done
return
def removeTransformation(self, fault, verbose=False, custom=False):
'''
Wrapper to ensure consistency between data sets.
Args:
* fault : a fault instance
Kwargs:
* verbose : talk to us
* custom : Remove custom GFs
Returns:
* None
'''
# No transformation is implemented, nothing to do
# All done
return
def removeSynth(self, faults, direction='sd', poly=None, vertical=True, custom=False, computeNormFact=True):
'''
Removes the synthetics using the faults and the slip distributions that are in there.
Args:
* faults : List of faults.
Kwargs:
* direction : Direction of slip to use.
* poly : if a polynomial function has been estimated, build and/or include
* vertical : always True - used here for consistency among data types
* custom : if True, uses the fault.custom and fault.G[data.name]['custom'] to correct
* computeNormFact : if False, uses TransformNormalizingFactor set with self.setTransformNormalizingFactor
Returns:
* None
'''
# Build synthetics
self.buildsynth(faults, direction=direction, poly=poly,
custom=custom, computeNormFact=computeNormFact)
# Correct
self.vel -= self.synth
# All done
return
def buildsynth(self, faults, direction='sd', poly=None, vertical=True, custom=False, computeNormFact=True):
'''
Computes the synthetic data using either the faults and the associated slip distributions or the pressure sources.
Args:
* faults : List of faults or pressure sources.
Kwargs:
* direction : Direction of slip to use or None for pressure sources.
* poly : if a polynomial function has been estimated, build and/or include
* vertical : always True. Used here for consistency among data types
* custom : if True, uses the fault.custom and fault.G[data.name]['custom'] to correct
* computeNormFact : if False, uses TransformNormalizingFactor set with self.setTransformNormalizingFactor
Returns:
* None
'''
# Check list
if type(faults) is not list:
faults = [faults]
# Number of data
Nd = self.vel.shape[0]
# Clean synth
self.synth = np.zeros((self.vel.shape))
# Loop on each fault
for fault in faults:
if fault.type=="Fault":
# Get the good part of G
G = fault.G[self.name]
if ('s' in direction) and ('strikeslip' in G.keys()):
Gs = G['strikeslip']
Ss = fault.slip[:,0]
synth = np.dot(Gs,Ss)
self.synth += synth
if ('d' in direction) and ('dipslip' in G.keys()):
Gd = G['dipslip']
Sd = fault.slip[:,1]
synth = np.dot(Gd, Sd)
self.synth += synth
# All done
return
def getRMS(self):
'''
Computes the RMS of the data and if synthetics are computed, the RMS of the residuals
Returns:
* float, float
'''
# Get the number of points
N = self.vel.shape[0]
# RMS of the data
dataRMS = np.sqrt( 1./N * sum(self.vel**2) )
# Synthetics
values = copy.deepcopy(self.vel)
if self.synth is not None:
values -= self.synth
#obsolete if self.orbit is not None:
# values -= self.orbit
synthRMS = np.sqrt( 1./N *sum( (values)**2 ) )
# All done
return dataRMS, synthRMS
def getVariance(self):
'''
Computes the Variance of the data and if synthetics are computed, the RMS of the residuals
Returns:
* float, float
'''
# Get the number of points
N = self.vel.shape[0]
# Varianceof the data
dmean = self.vel.mean()
dataVariance = ( 1./N * sum((self.vel-dmean)**2) )
# Synthetics
values = copy.deepcopy(self.vel)
if self.synth is not None:
values -= self.synth
if self.orbit is not None:
values -= self.orbit
synthVariance = ( 1./N *sum( (values - values.mean())**2 ) )
# All done
return dataVariance, synthVariance
def getMisfit(self):
'''
Computes the Summed Misfit of the data and if synthetics are computed, the RMS of the residuals
Returns:
* float, float
'''
# Misfit of the data
dataMisfit = sum((self.vel))
# Synthetics
if self.synth is not None:
synthMisfit = sum( (self.vel - self.synth) )
return dataMisfit, synthMisfit
else:
return dataMisfit, 0.
# All done
def plot(self, show=True, figsize=None, axis='lon'):
'''
Plot the data set, together with fault slip if asked.
Kwargs:
* show : bool. Show on screen?
* figsize : tuple of figure sizes
* axis : which quantity to use as x-axis
Returns:
* None
'''
# X-xaxis
if axis == 'lon':
x = self.lon
elif axis == 'lat':
x = self.lat
else:
print('Unkown axis type: {}'.format(axis))
return
# Create a figure
if figsize is None:
figsize=(10,3)
fig,ax = plt.subplots(1,1,figsize=figsize)
# Plot the data
u = np.argsort(x)
if self.err is None:
ax.plot(x[u], self.vel[u], '.-', color='k', label='Data', markersize=5)
else:
ax.fill_between(x[u], self.vel[u]+self.err[u], self.vel[u]-self.err[u],
color='k', alpha=0.3, zorder=1)
ax.plot(x[u], self.vel[u], '.-', color='k', zorder=2, label='Data')
# Synthetics
if self.synth is not None:
ax.plot(x[u], self.synth[u], '.-', color='r', label='Synthetics', markersize=5, zorder=3)
ax.legend()
# Title
ax.set_title('{}'.format(self.name))
# Show
if show: plt.show()
# Save the whole thing
self.fig = fig
self.ax = ax
# All done
return
def write2file(self, fname, data='data', outDir='./'):
'''
Write to an ascii file
Args:
* fname : Filename
Kwargs:
* data : can be 'data', 'synth' or 'resid'
* outDir : output Directory
Returns:
* None
'''
# Get variables
x = self.lon
y = self.lat
if data=='data':
z = self.vel
elif data=='synth':
z = self.synth
elif data=='resid':
z = self.vel - self.synth
# Write these to a file
fout = open(os.path.join(outDir, fname), 'w')
for i in range(x.shape[0]):
fout.write('{} {} {} \n'.format(x[i], y[i], z[i]))
fout.close()
return
def checkLOS(self, figure=1, factor=100., decim=1):
'''
Plots the LOS vectors in a 3D plot.
Kwargs:
* figure: Figure number.
* factor: Increases the size of the vectors.
* decim : Do not plot all the pixels (takes way too much time)
Returns:
* None
'''
# Display
print('Checks the LOS orientation')
# Create a figure
fig = plt.figure(figure)
# Create an axis instance
ax = fig.add_subplot(111, projection='3d')
# Loop over the LOS
for i in range(0,self.vel.shape[0],decim):
x = [self.x[i], self.x[i]+self.los[i,0]*factor]
y = [self.y[i], self.y[i]+self.los[i,1]*factor]
z = [0, self.los[i,2]*factor]
ax.plot3D(x, y, z, '-k')
ax.set_xlabel('Easting')
ax.set_ylabel('Northing')
ax.set_zlabel('Up')
# Show it
plt.show()
# All done
return
#EOF