-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtools.py
More file actions
168 lines (128 loc) · 5.19 KB
/
tools.py
File metadata and controls
168 lines (128 loc) · 5.19 KB
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
from __future__ import division
import numpy as np
import os
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import giapy.plot_tools.interp_path
from amrfile import io as amrio
from giapy.giaflat import thickness_above_floating
from intersection import intersection
from scipy.interpolate import interp2d
def gen_basemap():
"""Generate the standard projection."""
m = Basemap(resolution='i',projection='spstere',\
lat_ts=-71,lon_0=180, boundinglat=-64, ellps='WGS84')
return m
def pig2proj(x, y, xmin=-5905823.470038407, xmax=0,
ymin=-5905823.470038407,ymax=0,
xoff=1707000, yoff=384000):
"""Transform coordinates for Pine Island Glacier example to the standard
projection coordinates."""
return -x-0.5*(xmax-xmin)+xoff, -y-0.5*(ymax-ymin)+yoff
def genpigplot(m, xs, ys):
xmin=-xs.max()+0.5*m.xmin+1707000
ymin=ys.max()
xmax=xs.min()
ymax=ys.min()
fig, ax = plt.subplots(1,1)
ax.set_xlim(xmin,xmax)
ax.set_ylim(ymin,ymax)
return plt.gca()
def get_fnames(outpath, basename='plot'):
# List of all output files, following basename
fnames = [i for i in os.listdir(outpath) if basename in i]
# Extract the timestep associated, for sorting
steps = np.array([int(i.split('.')[-3]) for i in fnames])
# Sort the fnames by timestep
fnames = [fnames[i] for i in np.argsort(steps)]
return fnames
def extract_ts_and_vols(outpath,skip=1,taf=False):
fnames = get_fnames(outpath)[::skip]
vols = []
ts = []
for f in fnames:
amrID = amrio.load(outpath+f)
lo,hi = amrio.queryDomainCorners(amrID, 0)
xh,yh,bas1 = amrio.readBox2D(amrID, 0, lo, hi, "Z_base", 0)
xh,yh,thk1 = amrio.readBox2D(amrID, 0, lo, hi, "thickness", 0)
ts.append(amrio.queryTime(amrID))
amrio.freeAll()
if taf:
thk1 = thickness_above_floating(thk1, bas1)
vol = np.trapz(np.trapz(thk1, axis=1, x=xh), x=yh)
vols.append(vol)
vols = np.array(vols)
return ts, vols
def collect_field(fnames, field_name, outpath='./', return_ts=False):
flist = []
ts = []
for f in fnames:
amrID = amrio.load(outpath+f)
lo,hi = amrio.queryDomainCorners(amrID, 0)
xh,yh,z = amrio.readBox2D(amrID, 0, lo, hi, field_name, 0)
ts.append(amrio.queryTime(amrID))
flist.append(z)
amrio.free(amrID)
returnset= np.array(flist),
if return_ts: returnset+= np.array(ts),
return returnset
def intersect_grounding_and_center(fnames, centerline, outpath='./',
return_depths=False, return_thks=False):
"""
Finds the intersection between the grounding lines in fnames and a
centerline, using the intersection tool from Sukhbinder Singh.
"""
xints, yints, depths, thks, ts, slps = [], [], [], [], [], []
thetas = np.arctan(np.gradient(centerline.xs)/np.gradient(centerline.ys))
for f in fnames:
amrID = amrio.load(outpath+f)
lo,hi = amrio.queryDomainCorners(amrID, 0)
xh,yh,bas = amrio.readBox2D(amrID, 0, lo, hi, "Z_base", 0)
xh,yh,bot = amrio.readBox2D(amrID, 0, lo, hi, "Z_bottom", 0)
xh,yh,thk = amrio.readBox2D(amrID, 0, lo, hi, "thickness", 0)
p = plt.contour(xh, yh, np.abs(bas-bot), levels=[0.1], colors='r');
glx, gly = p.allsegs[0][0].T
xint, yint = intersection.intersection(glx, gly, centerline.xs, centerline.ys)
xint = np.mean(xint)
yint=np.mean(yint)
xints.append(xint)
yints.append(yint)
ts.append(amrio.queryTime(amrID))
depths.append(interp2d(xh, yh, bas)(xint, yint))
thks.append(interp2d(xh, yh, thk)(xint, yint))
xi = np.argmin(np.abs(xh-xint))
yi = np.argmin(np.abs(yh-yint))
slpx = bas[yi, xi+1]-bas[yi, xi-1]/(2*(xh[1]-xh[0]))
slpy = bas[yi+1, xi]-bas[yi-1, xi]/(2*(yh[1]-yh[0]))
thi = np.argmin((centerline.xs-xint)**2 + (centerline.ys-yint)**2)
theta = thetas[thi]
slp = slpx*np.cos(theta) + slpy*np.sin(theta)
slp = np.sqrt(slpx**2 + slpy**2)
slps.append(slp)
amrio.free(amrID)
xints = np.array(xints)
yints = np.array(yints)
depths = np.array(depths)
ts = np.array(ts)
plt.close()
return_set = ts, xints, yints
if return_depths: return_set+=depths,
if return_thks: return_set+=thks,
return_set += slps,
return_set = [np.asarray(rs) for rs in return_set]
return return_set
def find_centerline():
xh,yh,xvel = amrio.readBox2D(amrID, 0, lo, hi, "xVel", 0)
xh,yh,yvel = amrio.readBox2D(amrID, 0, lo, hi, "yVel", 0)
points = np.array([[0,0]])
for l in p.lines.get_paths():
i = 0
for s in l.iter_segments():
if i == 0:
if s[0] not in points:
points = np.vstack([points, s[0]])
i += 1
center_x, center_y = points.T[0,sl], points.T[1,sl]
center_d = np.r_[0,np.cumsum(np.sqrt((center_x[1:] - center_x[:-1])**2 + (center_y[1:] - center_y[:-1])**2))]/1000.
centerline = giapy.plot_tools.interp_path.TransectPath(center_x, center_y, center_d)
return centerline