forked from knaughten/roms_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_zeta.py
50 lines (37 loc) · 1.31 KB
/
avg_zeta.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
from netCDF4 import Dataset
from numpy import *
from matplotlib.pyplot import *
from cartesian_grid_2d import *
# Calculate the average sea surface height (zeta) at each timestep of the given
# ocean history file.
def avg_zeta (file_path):
# Read time and grid variables
file = Dataset(file_path, 'r')
time = file.variables['ocean_time'][:]
# Convert time from seconds to years
time = time/(365*24*60*60)
lon = file.variables['lon_rho'][:-15,1:-1]
lat = file.variables['lat_rho'][:-15,1:-1]
mask = file.variables['mask_rho'][:-15,1:-1]
avg_zeta = []
# Calculate dx and dy in another script
dx, dy = cartesian_grid_2d(lon, lat)
# Calculate dA and mask with land mask
dA = ma.masked_where(mask==0, dx*dy)
for l in range(size(time)):
print 'Processing timestep ' + str(l+1) + ' of ' + str(size(time))
# Read zeta at this timestep
zeta = file.variables['zeta'][l,:-15,1:-1]
# Calculate area-weighted average
avg_zeta.append(sum(zeta*dA)/sum(dA))
file.close()
# Plot results
clf()
plot(time, avg_zeta)
xlabel('Years')
ylabel('Average sea surface height (m)')
show()
# Command-line interface
if __name__ == "__main__":
file_path = raw_input("Path to ocean history file: ")
avg_zeta(file_path)