Skip to content

Commit c01eeba

Browse files
committed
Group all ascii io in one file
1 parent a9f3ad2 commit c01eeba

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

stagpy/plates.py

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Plate analysis."""
22

3-
from contextlib import ExitStack, suppress
3+
from contextlib import suppress
44

55
import matplotlib.pyplot as plt
66
from matplotlib import colors
@@ -232,7 +232,7 @@ def plot_at_surface(snap, names, trenches, ridges):
232232
saveplot(fig, fname, snap.isnap)
233233

234234

235-
def _write_trench_diagnostics(step, vrms_surf, itrenches, fids):
235+
def _write_trench_diagnostics(step, vrms_surf, itrenches, fid):
236236
"""Print out some trench diagnostics."""
237237
time = step.time * vrms_surf *\
238238
conf.scaling.ttransit / conf.scaling.yearins / 1.e6
@@ -253,21 +253,10 @@ def _write_trench_diagnostics(step, vrms_surf, itrenches, fids):
253253
else:
254254
agetrenches = np.zeros(len(itrenches))
255255

256-
# writing the output into a file, all time steps are in one file
257-
for itrench in range(len(trenches)):
258-
fids[0].write("%7.0f %11.7f %10.6f %9.2f %9.2f \n" % (
259-
step.isnap,
260-
step.time,
261-
trenches[itrench],
262-
v_trenches[itrench],
263-
agetrenches[itrench]
264-
))
265-
266256
phi_cont = step.geom.p_centers[_continents_location(step)]
267257

268258
distance_subd = []
269259
ph_cont_subd = []
270-
271260
for trench_i in trenches:
272261
# compute distance between subduction and continent
273262
angdistance1 = np.abs(phi_cont - trench_i)
@@ -279,16 +268,17 @@ def _write_trench_diagnostics(step, vrms_surf, itrenches, fids):
279268
distance_subd.append(angdistance[i_closest])
280269

281270
# writing the output into a file, all time steps are in one file
282-
for isubd in range(len(distance_subd)):
283-
fids[1].write("%6.0f %11.7f %11.3f %10.6f %10.6f %10.6f %11.3f\n" % (
284-
step.isnap,
285-
step.time,
286-
time,
287-
distance_subd[isubd],
288-
trenches[isubd],
289-
ph_cont_subd[isubd],
290-
agetrenches[isubd],
291-
))
271+
for isubd in range(len(trenches)):
272+
fid.write(
273+
"%6.0f %11.7f %11.3f %10.6f %10.6f %10.6f %10.6f %11.3f\n" % (
274+
step.isnap,
275+
step.time,
276+
time,
277+
trenches[isubd],
278+
v_trenches[isubd],
279+
distance_subd[isubd],
280+
ph_cont_subd[isubd],
281+
agetrenches[isubd]))
292282

293283

294284
def plot_scalar_field(step, fieldname, ridges, trenches):
@@ -341,20 +331,15 @@ def main_plates(sdat):
341331
vrms_surf = sdat.walk.filter(rprofs=True)\
342332
.rprofs_averaged['vhrms'].values[isurf]
343333

344-
# determine names of files
345-
fnames = ['plate_velocity', 'distance_subd']
346-
fnames = [f'plates_{stem}_{sdat.walk.stepstr}' for stem in fnames]
347-
with ExitStack() as stack:
348-
fids = [stack.enter_context(open(fname, 'w')) for fname in fnames]
349-
fids[0].write('# it time ph_trench vel_trench age_trench\n')
350-
fids[1].write('# it time time [My] distance '
351-
'ph_trench ph_cont age_trench [My]\n')
334+
with open(f'plates_trenches_{sdat.walk.stepstr}.dat', 'w') as fid:
335+
fid.write('# istep time time_My phi_trench vel_trench '
336+
'distance phi_cont age_trench_My\n')
352337

353338
for step in sdat.walk.filter(fields=['T']):
354339
# could check other fields too
355340
trenches, ridges, itrenches = detect_plates(step)
356341

357-
_write_trench_diagnostics(step, vrms_surf, itrenches, fids)
342+
_write_trench_diagnostics(step, vrms_surf, itrenches, fid)
358343
plot_at_surface(step, conf.plates.plot, trenches, ridges)
359344
plot_scalar_field(step, conf.plates.field, ridges, trenches)
360345

0 commit comments

Comments
 (0)