-
Notifications
You must be signed in to change notification settings - Fork 32
/
multigps.py
318 lines (245 loc) · 9.78 KB
/
multigps.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
'''
A class that deals with multiple gps objects
Written by R. Jolivet, May 2014.
'''
# Externals
import numpy as np
import pyproj as pp
import matplotlib.pyplot as plt
import os
import copy
import sys
# Personals
from .gps import gps
from .gpstimeseries import gpstimeseries
class multigps(gps):
'''
A class that handles several networks of gps stations
Args:
* name : Name of the dataset.
Kwargs:
* gpsobjects: list of instances of the gps class
* obs_per_station: how many data points per site
* utmzone : UTM zone (optional, default=None)
* lon0 : Longitude of the center of the UTM zone
* lat0 : Latitude of the center of the UTM zone
* ellps : ellipsoid (optional, default='WGS84')
* verbose : Speak to me (default=True)
'''
def __init__(self, name, gpsobjects=None, utmzone=None, ellps='WGS84', obs_per_station=2, lon0=None, lat0=None):
# Base class init
super(multigps,self).__init__(name,
utmzone=utmzone,
ellps=ellps,
lon0=lon0,
lat0=lat0,
verbose=False)
# Set things
self.dtype = 'multigps'
self.obs_per_station = obs_per_station
# print
print ("---------------------------------")
print ("---------------------------------")
print ("Initialize Multiple GPS array {}".format(self.name))
# Initialize things
self.vel_enu = None
self.err_enu = None
self.rot_enu = None
self.synth = None
# Set objects
if gpsobjects is not None:
self.setgps(gpsobjects)
# All done
return
def setgps(self, gpsobjects):
'''
Takes list of gps and set it
Args:
* gpsobjects : List of gps objects.
Returns:
* None
'''
# Get the list
self.gpsobjects = gpsobjects
# Loop over the stations to get the number of stations
ns = 0
for gp in gpsobjects:
ns += gp.station.shape[0]
# Get the factor
self.factor = gpsobjects[0].factor
# Create the arrays
self.station = np.zeros(ns).astype('|S4')
self.lon = np.zeros(ns)
self.lat = np.zeros(ns)
self.x = np.zeros(ns)
self.y = np.zeros(ns)
self.vel_enu = np.zeros((ns,3))
self.err_enu = np.zeros((ns,3))
# obs per stations
obs_per_station = []
# Loop over the gps objects to feed self
ns = 0
for gp in gpsobjects:
# Assert
assert gp.factor==self.factor, 'GPS object have a different factor: Object {}'.format(gp.name)
assert gp.utmzone==self.utmzone, 'UTM zone is not compatible: Object {}'.format(gp.utmzone)
# Set starting and ending points
st = ns
ed = ns + gp.station.shape[0]
# Get stations
self.station[st:ed] = gp.station
# Feed in
self.lon[st:ed] = gp.lon
self.lat[st:ed] = gp.lat
self.x[st:ed] = gp.x
self.y[st:ed] = gp.y
self.vel_enu[st:ed,:] = gp.vel_enu
self.err_enu[st:ed,:] = gp.err_enu
# Force number of observation per station
gp.obs_per_station = self.obs_per_station
# Update ns
ns += gp.station.shape[0]
# All done
return
def getNumberOfTransformParameters(self, transformation):
'''
Returns the number of transform parameters for the given transformation.
Args:
* transformation: List [main_transformation, [transfo_subnet1, transfo_subnet2, ....] ]. Each can be 'strain', 'full', 'strainnorotation', 'strainnotranslation', 'strainonly'
Returns:
* int
'''
# Separate
mainTrans = transformation[0]
subTrans = transformation[1]
# Assert
assert type(mainTrans) in (str, type(None)), 'First Item of transformation list needs to be string'
assert type (subTrans) is list, 'Second Item of transformation list needs to be a list'
# Nhere
nMain = super(multigps,self).getNumberOfTransformParameters(transformation[0])
# Each subnetwork
nSub = 0
for trans, gp in zip(subTrans, self.gpsobjects):
nSub += gp.getNumberOfTransformParameters(trans)
# Sum
Npo = nSub + nMain
# All done
return Npo
def getTransformEstimator(self, transformation, computeNormFact=True):
'''
Returns the estimator for the transform.
Args:
* transformation : List [main_transformation, [transfo_subnet1, transfo_subnet2, ....] ]. Each item can be 'strain', 'full', 'strainnorotation', 'strainnotranslation', 'strainonly'
Kwargs:
* computeNormFact: compute and store the normalizing factor
Returns:
* 2D array
'''
# Separate
mainTrans = transformation[0]
subTrans = transformation[1]
# Assert
assert type(mainTrans) in (str, type(None)), 'First Item of transformation list needs to be string'
assert type (subTrans) is list, 'Second Item of transformation list needs to be a list: {}'.format(subTrans)
# Need the number of columns and lines
nc = self.getNumberOfTransformParameters(transformation)
ns = self.station.shape[0]*self.obs_per_station
# Create the big holder
orb = np.zeros((ns,nc))
# Get the main transforms
Morb = super(multigps,self).getTransformEstimator(mainTrans,
computeNormFact=computeNormFact)
# Put it where it should be
if Morb is not None:
cst = Morb.shape[1]
orb[:, :cst] = Morb
else:
cst = 0
# Loop over the subnetworks
lst_east = 0
lst_north = self.station.shape[0]
lst_up = 2*self.station.shape[0]
for trans, gp in zip(subTrans, self.gpsobjects):
# Get the transform
Sorb = gp.getTransformEstimator(trans, computeNormFact=computeNormFact)
if Sorb is not None:
# Set the indexes right
ced = cst + Sorb.shape[1]
led_east = lst_east + gp.station.shape[0]
led_north = lst_north + gp.station.shape[0]
# Put it where it should be
orb[lst_east:led_east, cst:ced] = Sorb[:gp.station.shape[0],:]
orb[lst_north:led_north, cst:ced] = Sorb[gp.station.shape[0]:2*gp.station.shape[0],:]
# Deal with verticals if needed
if self.obs_per_station==3:
led_up = lst_up + gp.station.shape[0]
orb[lst_up:led_up, cst:ced] = Sorb[2*gp.station.shape[0]:,:]
# Update column
cst += Sorb.shape[1]
# update lines
lst_east += gp.station.shape[0]
lst_north += gp.station.shape[0]
# All done
return orb
def computeTransformation(self, fault, custom=False):
'''
Computes the transformation that is stored with a particular fault.
Args:
* fault: instance of a fault class that has a {polysol} attribute
Kwargs:
* custom: True/False. Do we have to account for custom GFs?
Returns:
* None. Transformation is stroed in attribute {transformation}
'''
# Get the transformation
transformation = fault.poly[self.name]
# Separate
mainTrans = transformation[0]
subTrans = transformation[1]
# Get the solution
Tvec = fault.polysol[self.name]
if type(Tvec) is not np.ndarray:
Tvec = Tvec['{}'.format(transformation)]
# Assert
assert type(mainTrans) in (str, type(None)), 'First Item of transformation list needs to be string'
assert type(subTrans) is list, 'Second Item of transformation list needs to be a list'
# Get the estimator
orb = self.getTransformEstimator(transformation)
# Get the starting point
st = self.getNumberOfTransformParameters([mainTrans,[None for i in range(len(subTrans))]])
# Loop over the transformations
for trans, gp in zip(subTrans, self.gpsobjects):
# Put the transform in the fault
fault.poly[gp.name] = trans
# Put the solution in the fault
nP = gp.getNumberOfTransformParameters(trans)
ed = st + nP
fault.polysol[gp.name] = Tvec[st:ed]
# Edit st
st += nP
# make the array
self.transformation = np.zeros(self.vel_enu.shape)
# Check
if orb is None:
return
# Compute the synthetics
tmpsynth = np.dot(orb, Tvec)
# Fill it
no = self.vel_enu.shape[0]
self.transformation[:,0] = tmpsynth[:no]
self.transformation[:,1] = tmpsynth[no:2*no]
if self.obs_per_station==3:
self.transformation[:,2] = tmpsynth[2*no:]
# Add custom
if custom:
self.computeCustom(fault)
if self.obs_per_station==1:
self.transformation[:,2] += self.custompred[:,2]
if self.obs_per_station>=2:
self.transformation[:,0] += self.custompred[:,0]
self.transformation[:,1] += self.custompred[:,1]
if self.obs_per_station==3:
self.transformation[:,2] += self.custompred[:,2]
# All done
return
#EOF