Skip to content

Commit 5889137

Browse files
committed
updates to occam and ws3din
v
1 parent 050b731 commit 5889137

File tree

3 files changed

+102
-69
lines changed

3 files changed

+102
-69
lines changed

mtpy/modeling/occam2d/data.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ def __init__(self, dataframe=None, center_point=None, **kwargs):
102102
self.occam_dict = {
103103
"1": "res_xy",
104104
"2": "phase_xy",
105-
"3": "tzx_real",
106-
"4": "tzx_imag",
105+
"3": "t_zx_real",
106+
"4": "t_zx_imag",
107107
"5": "res_yx",
108108
"6": "phase_yx",
109109
"9": "res_xy",
@@ -113,7 +113,8 @@ def __init__(self, dataframe=None, center_point=None, **kwargs):
113113
self.df_dict = {
114114
"1": "res_xy",
115115
"2": "phase_xy",
116-
"3": "tzx",
116+
"3": "t_zx",
117+
"4": "t_zy",
117118
"5": "res_yx",
118119
"6": "phase_yx",
119120
}
@@ -437,26 +438,48 @@ def read_data_file(self, data_fn=None):
437438

438439
# format dataframe
439440
df = pd.DataFrame(entries)
440-
df["tzx"] = df.tzx_real + 1j * df.tzx_imag
441-
df["tzx_model_error"] = df.tzx_real_model_error
441+
if 't_zy_real' in list(df):
442+
df['t_zy_real'] = df['t_zy_real'].fillna(0)
443+
df['t_zy_imag'] = df['t_zy_imag'].fillna(0)
444+
df['t_zy'] = df['t_zy_real'] + 1j*df['t_zy_imag']
442445
df["period"] = 1.0 / df.frequency
443-
df = df.drop(
444-
columns=[
445-
"tzx_real",
446-
"tzx_imag",
447-
"tzx_real_model_error",
448-
"tzx_imag_model_error",
449-
"frequency",
450-
],
451-
axis=1,
452-
)
453-
454-
df = df.groupby(["station", "period"]).agg("first")
446+
# df = df.drop(
447+
# columns=[
448+
# "tzx_real",
449+
# "tzx_imag",
450+
# "tzx_real_model_error",
451+
# "tzx_imag_model_error",
452+
# "frequency",
453+
# ],
454+
# axis=1,
455+
# )
456+
457+
# df = df.groupby(["station", "period"]).agg("first")
455458
df = df.sort_values("profile_offset").reset_index()
456459
self.dataframe = df
460+
461+
# use workaround function to group
462+
self._group_df()
457463

458464
self.model_mode = self._get_model_mode_from_data(res_log)
459465

466+
def _group_df(self):
467+
for station in np.unique(self.dataframe['station']):
468+
for period in np.unique(self.dataframe['period']):
469+
filt = np.all([self.dataframe['station']==station,
470+
self.dataframe['period']==period],axis=0)
471+
if np.any(filt):
472+
for key in ['res_xy','res_yx','phase_xy','phase_yx']:
473+
self.dataframe[key][filt] = np.unique(self.dataframe[key][filt])[0]
474+
475+
tx_real_vals = np.unique(np.real(self.dataframe['t_zy'][filt]))
476+
tx_imag_vals = np.unique(np.imag(self.dataframe['t_zy'][filt]))
477+
if np.any(tx_real_vals != 0):
478+
self.dataframe['t_zy'][filt] = tx_real_vals[tx_real_vals != 0][0] +\
479+
1j * tx_imag_vals[tx_imag_vals != 0][0]
480+
481+
self.dataframe.drop(self.dataframe[filt].index[1:],inplace=True)
482+
460483
def _get_model_mode_from_data(self, res_log):
461484
"""Get inversion mode from the data.
462485
:param res_log: DESCRIPTION.
@@ -477,9 +500,9 @@ def _get_model_mode_from_data(self, res_log):
477500
inv_list.append(5)
478501
else:
479502
inv_list.append(10)
480-
elif comp == "tzx":
481-
inv_list.append(3)
482-
inv_list.append(4)
503+
# elif comp == "tzx":
504+
# inv_list.append(3)
505+
# inv_list.append(4)
483506
else:
484507
inv_list.append(int(inv_mode))
485508

@@ -526,7 +549,8 @@ def _get_data_block(self):
526549
value = value.imag
527550
error_value = fdf[f"{comp}_model_error"].values[0]
528551
elif comp_number in [6]:
529-
value = value + 180
552+
if -180 <= value <= -90:
553+
value = value + 180
530554
error_value = fdf[f"{comp}_model_error"].values[0]
531555
else:
532556
value = value

mtpy/modeling/structured_mesh_3d.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,17 +2755,18 @@ def to_ws3dinv_intial(self, initial_fn, res_list=None):
27552755
l1 = 0
27562756
layers = []
27572757
for zz in range(self.nodes_z.shape[0] - 1):
2758-
if not (
2759-
write_res_model[:, :, zz] == write_res_model[:, :, zz + 1]
2760-
).all():
2758+
# if not (
2759+
# write_res_model[:, :, zz] == write_res_model[:, :, zz + 1]
2760+
# ).all():
27612761
layers.append((l1, zz))
27622762
l1 = zz + 1
27632763
# need to add on the bottom layers
27642764
layers.append((l1, self.nodes_z.shape[0] - 1))
27652765

27662766
# write out the layers from resmodel
27672767
for ll in layers:
2768-
lines.append(f"{ll[0] + 1} {ll[1] + 1}\n")
2768+
if nr > 0:
2769+
lines.append(f"{ll[0] + 1} {ll[1] + 1}\n")
27692770
for nn in range(self.nodes_north.shape[0]):
27702771
for ee in range(self.nodes_east.shape[0]):
27712772
if nr > 0:

mtpy/modeling/ws3dinv/data.py

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ def get_n_stations(self):
205205
if self.dataframe is not None:
206206
return (
207207
self.dataframe.loc[
208-
(self.dataframe.zxx != 0)
209-
| (self.dataframe.zxy != 0)
210-
| (self.dataframe.zyx != 0)
211-
| (self.dataframe.zyy != 0),
208+
(self.dataframe.z_xx != 0)
209+
| (self.dataframe.z_xy != 0)
210+
| (self.dataframe.z_yx != 0)
211+
| (self.dataframe.z_yy != 0),
212212
"station",
213213
]
214214
.unique()
@@ -256,7 +256,7 @@ def write_data_file(self, **kwargs):
256256
# -----Write data file--------------------------------------------------
257257
n_stations = self.get_n_stations()
258258
n_periods = self.period.size
259-
station_locations = self.dataframe.station_locations.copy()
259+
station_locations = self.station_locations.copy()
260260

261261
lines = []
262262
data_lines = []
@@ -266,56 +266,62 @@ def write_data_file(self, **kwargs):
266266

267267
# write N-S locations
268268
lines.append("Station_Location: N-S \n")
269-
for ii in range(n_stations / self.n_z + 1):
270-
for ll in range(self.n_z):
271-
index = ii * self.n_z + ll
272-
try:
273-
lines.append(
274-
f"{station_locations.model_north[index]:+.4e} "
275-
)
276-
except IndexError:
277-
pass
278-
lines.append("\n")
269+
for ii in range(n_stations):# / self.n_z + 1
270+
# for ll in range(self.n_z):
271+
# index = ii * self.n_z + ll
272+
try:
273+
lines.append(
274+
f"{station_locations.model_north[ii]:+.4e} "
275+
)
276+
except IndexError:
277+
pass
278+
if ii % 8 == 7:
279+
lines.append("\n")
280+
lines.append("\n")
279281

280282
# write E-W locations
281283
lines.append("Station_Location: E-W \n")
282-
for ii in range(n_stations / self.n_z + 1):
283-
for ll in range(self.n_z):
284-
index = ii * self.n_z + ll
285-
try:
286-
lines.append(
287-
f"{station_locations.model_east[index]:+.4e} "
288-
)
289-
except IndexError:
290-
pass
291-
lines.append("\n")
284+
for ii in range(n_stations):# / self.n_z + 1
285+
# for ll in range(self.n_z):
286+
# index = ii * self.n_z + ll
287+
try:
288+
lines.append(
289+
f"{station_locations.model_east[ii]:+.4e} "
290+
)
291+
except IndexError:
292+
pass
293+
294+
if ii % 8 == 7:
295+
lines.append("\n")
296+
lines.append("\n")
292297

293298
# write impedance tensor components
294299
for ii, p1 in enumerate(self.period):
295-
pdf = self.get_period_df(p1)
300+
# pdf = self.get_period_df(p1)
301+
pdf = self.dataframe[self.dataframe['period']==p1]
296302
data_lines.append(f"DATA_Period: {p1:3.6f}\n")
297303
error_lines.append(f"ERROR_Period: {p1:3.6f}\n")
298304
error_map_lines.append(f"ERMAP_Period: {p1:3.6f}\n")
299305
for row in pdf.itertuples():
300306
data_lines.append(
301-
f"{row.zxx.real * zconv:+.4e} "
302-
f"{row.zxx.imag * zconv:+.4e} "
303-
f"{row.zxy.real * zconv:+.4e} "
304-
f"{row.zxy.imag * zconv:+.4e} "
305-
f"{row.zyx.real * zconv:+.4e} "
306-
f"{row.zyx.imag * zconv:+.4e} "
307-
f"{row.zyy.real * zconv:+.4e} "
308-
f"{row.zyy.imag * zconv:+.4e}\n"
307+
f"{row.z_xx.real * zconv:+.4e} "
308+
f"{row.z_xx.imag * zconv:+.4e} "
309+
f"{row.z_xy.real * zconv:+.4e} "
310+
f"{row.z_xy.imag * zconv:+.4e} "
311+
f"{row.z_yx.real * zconv:+.4e} "
312+
f"{row.z_yx.imag * zconv:+.4e} "
313+
f"{row.z_yy.real * zconv:+.4e} "
314+
f"{row.z_yy.imag * zconv:+.4e}\n"
309315
)
310316
error_lines.append(
311-
f"{row.zxx.real * self.z_error:+.4e} "
312-
f"{row.zxx.imag * self.z_error:+.4e} "
313-
f"{row.zxy.real * self.z_error:+.4e} "
314-
f"{row.zxy.imag * self.z_error:+.4e} "
315-
f"{row.zyx.real * self.z_error:+.4e} "
316-
f"{row.zyx.imag * self.z_error:+.4e} "
317-
f"{row.zyy.real * self.z_error:+.4e} "
318-
f"{row.zyy.imag * self.z_error:+.4e} "
317+
f"{row.z_xx.real * self.z_error:+.4e} "
318+
f"{row.z_xx.imag * self.z_error:+.4e} "
319+
f"{row.z_xy.real * self.z_error:+.4e} "
320+
f"{row.z_xy.imag * self.z_error:+.4e} "
321+
f"{row.z_yx.real * self.z_error:+.4e} "
322+
f"{row.z_yx.imag * self.z_error:+.4e} "
323+
f"{row.z_yy.real * self.z_error:+.4e} "
324+
f"{row.z_yy.imag * self.z_error:+.4e} "
319325
)
320326

321327
error_map_lines.append(
@@ -326,11 +332,13 @@ def write_data_file(self, **kwargs):
326332
f"{self.z_error_map[2]:+.4e} "
327333
f"{self.z_error_map[2]:+.4e} "
328334
f"{self.z_error_map[3]:+.4e} "
329-
f"{self.z_error_map[3]:+.4e} "
335+
f"{self.z_error_map[3]:+.4e}\n"
330336
)
331337

332338
with open(self.data_filename, "w") as fid:
333-
fid.write("".join(lines, data_lines, error_lines, error_map_lines))
339+
for dlist in [lines, data_lines, error_lines, error_map_lines]:
340+
for line in dlist:
341+
fid.write(line)
334342

335343
self.logger.info(f"Wrote WS3DINV file to {self.data_filename}")
336344
return self.data_filename

0 commit comments

Comments
 (0)