forked from matthewhoffman/e3sm-cryo-analysis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_transects.py
executable file
·178 lines (155 loc) · 7.6 KB
/
compute_transects.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
#!/usr/bin/env python
"""
Name: compute_transects.py
Author: Phillip J. Wolfram, Mark Petersen, Luke Van Roekel
Computes transport through sections.
Example call:
./compute_transects.py
-k /lustre/scratch3/turquoise/mpeterse/runs/c62n/ocean/global_ocean/EC_60to30km/spin_up/init_step2/EC60to30v3_transect_masks.nc
-m /lustre/scratch2/turquoise/mpeterse/runs/c69z/init.nc
-t '/lustre/scratch2/turquoise/mpeterse/runs/c69z/analysis_members/timeSeriesStatsMonthly.*.nc'
-n 'Drake Passage'
"""
# ensure plots are rendered on ICC
import matplotlib as mpl
mpl.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
from netCDF4 import Dataset
import glob
import platform
m3ps_to_Sv = 1e-6 # m^3/sec flux to Sverdrups
def get_mask_short_names(mask):
shortnames = [str(aname.values)[:str(aname.values).find(',')].strip()
for aname in mask.transectNames]
mask['shortNames'] = xr.DataArray(shortnames, dims='nTransects')
mask = mask.set_index(nTransects=['transectNames', 'shortNames'])
return mask
def compute_transport(timeavg, mesh, mask, name='Drake Passage',output='transport.nc'):
mesh = xr.open_dataset(mesh)
mask = get_mask_short_names(xr.open_dataset(mask))
if name.lower() == 'all':
transectList = mask.shortNames[:].values
condition = transectList != "Atlantic Transec"
transectList = np.extract(condition, transectList)
else:
transectList = name.split(',')
if platform.release()[0] == '3':
for i in range(len(transectList)):
transectList[i] = "b'" + transectList[i]
print('Computing Transport for the following transects ',transectList)
nTransects = len(transectList)
maxEdges = mask.dims['maxEdgesInTransect']
# create empty t list for time
t = []
# Compute refLayerThickness to avoid need for hist file
refBottom = mesh.refBottomDepth.values
nz = mesh.dims['nVertLevels']
h = np.zeros(nz)
h[0] = refBottom[0]
for i in range(1,nz):
h[i] = refBottom[i] - refBottom[i-1]
# Get a list of edges and total edges in each transect
nEdgesInTransect = np.zeros(nTransects)
edgeVals = np.zeros((nTransects,maxEdges))
for i in range(nTransects):
amask = mask.sel(shortNames=transectList[i]).squeeze()
transectEdges = amask.transectEdgeGlobalIDs.values
inds = np.where(transectEdges > 0)[0]
nEdgesInTransect[i] = len(inds)
transectEdges = transectEdges[inds]
edgeVals[i,:len(inds)] = np.asarray(transectEdges-1, dtype='i')
nEdgesInTransect = np.asarray(nEdgesInTransect, dtype='i')
# Create a list with the start and stop for transect bounds
nTransectStartStop = np.zeros(nTransects+1)
for j in range(1,nTransects+1):
nTransectStartStop[j] = nTransectStartStop[j-1] + nEdgesInTransect[j-1]
edgesToRead = edgeVals[0,:nEdgesInTransect[0]]
for i in range(1,nTransects):
edgesToRead = np.hstack([edgesToRead,edgeVals[i,:nEdgesInTransect[i]]])
edgesToRead = np.asarray(edgesToRead, dtype='i')
dvEdge = mesh.dvEdge.sel(nEdges=edgesToRead).values
edgeSigns = np.zeros((nTransects,len(edgesToRead)))
for i in range(nTransects):
edgeSigns[i,:] = mask.sel(nEdges=edgesToRead, shortNames=transectList[i]).squeeze().transectEdgeMaskSigns.values
# Read time average files one at a time and slice
fileList = sorted(glob.glob(timeavg))
transport = np.zeros((len(fileList),nTransects))
t = np.zeros(len(fileList))
for i,fname in enumerate(fileList):
ncid = Dataset(fname,'r')
if 'timeMonthly_avg_normalTransportVelocity' in ncid.variables.keys():
vel = ncid.variables['timeMonthly_avg_normalTransportVelocity'][0,edgesToRead,:]
elif 'timeMonthly_avg_normalVelocity' in ncid.variables.keys():
vel = ncid.variables['timeMonthly_avg_normalVelocity'][0,edgesToRead,:]
if 'timeMonthly_avg_normalGMBolusVelocity' in ncid.variables.keys():
vel += ncid.variables['timeMonthly_avg_normalGMBolusVelocity'][0,edgesToRead,:]
else:
raise KeyError('no appropriate normalVelocity variable found')
t[i] = ncid.variables['timeMonthly_avg_daysSinceStartOfSim'][:] / 365.
ncid.close()
# Compute transport for each transect
for j in range(nTransects):
start = int(nTransectStartStop[j])
stop = int(nTransectStartStop[j+1])
transport[i,j] = (dvEdge[start:stop,np.newaxis]*h[np.newaxis,:]*vel[start:stop,:] \
*edgeSigns[j,start:stop,np.newaxis]).sum()*m3ps_to_Sv
# Define some dictionaries for transect plotting
obsDict = {'Drake Passage':[120,175],'Tasmania-Ant':[147,167],'Africa-Ant':None,'Antilles Inflow':[-23.1,-13.7], \
'Mona Passage':[-3.8,-1.4],'Windward Passage':[-7.2,-6.8],'Florida-Cuba':[30,33],'Florida-Bahamas':[30,33], \
'Indonesian Throughflow':[-21,-11],'Agulhas':[-90,-50],'Mozambique Channel':[-20,-8], \
'Bering Strait':[0.17,1.49],'Lancaster Sound':None,'Fram Strait':None,'Robeson Channel':None,'Nares Strait':None}
labelDict = {'Drake Passage':'drake','Tasmania-Ant':'tasmania','Africa-Ant':'africaAnt','Antilles Inflow':'Antilles', \
'Mona Passage':'monaPassage','Windward Passage':'windwardPassage','Florida-Cuba':'floridaCuba',\
'Florida-Bahamas':'floridaBahamas', \
'Indonesian Throughflow':'indonesia','Agulhas':'agulhas','Mozambique Channel':'mozambique', \
'Bering Strait':'beringstrait','Lancaster Sound':'lancaster','Fram Strait':'fram','Robeson Channel':'robeson','Nares Strait':'nares'}
for i in range(nTransects):
plt.figure()
if platform.release()[0]=='3':
searchString = transectList[i][2:]
else:
searchString = transectList[i]
bounds = obsDict[searchString]
title = 'Transport for '+searchString
plt.plot(t,transport[:,i],'k',linewidth=2)
if bounds is not None:
plt.gca().fill_between(t, bounds[0]*np.ones_like(t), bounds[1]*np.ones_like(t), alpha=0.3, label='observations')
plt.ylabel('Transport (Sv)',fontsize=32)
plt.xlabel('Time (Years)',fontsize=32)
plt.title(title,fontsize=32)
plt.savefig('transport_'+labelDict[searchString]+'.png')
# Add calls to save transport and then can build up
ncid=Dataset(output,mode='w',clobber=True, format='NETCDF3_CLASSIC')
ncid.createDimension('Time',None)
ncid.createDimension('nTransects',nTransects)
ncid.createDimension('StrLen',64)
transectNames=ncid.createVariable('TransectNames','c',('nTransects','StrLen'))
times=ncid.createVariable('Time','f8','Time')
transportOut=ncid.createVariable('Transport','f8',('Time','nTransects'))
times[:] = t
transportOut[:,:] = transport
for i in range(nTransects):
nLetters = len(transectList[i])
transectNames[i,:nLetters] = transectList[i]
ncid.close()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-o", "--output_file_pattern", dest="output_filename_pattern",
help="MPAS Filename pattern for transport output.", metavar="NAME")
parser.add_argument("-t", "--time_avg_file_pattern", dest="time_avg_filename_pattern",
help="MPAS Filename pattern for time averaged AM output.", metavar="FILE",
required=True)
parser.add_argument("-m", "--mesh_file", dest="mesh_filename",
help="MPAS Mesh filename.", required=True)
parser.add_argument("-k", "--mask_file", dest="mask_filename",
help="MPAS mask filename.", required=True)
parser.add_argument("-n", "--name", dest="name",
help="Transect name for computation", metavar="NAME")
args = parser.parse_args()
compute_transport(timeavg=args.time_avg_filename_pattern,
mesh=args.mesh_filename, mask=args.mask_filename, name=args.name,
output=args.output_filename_pattern)