Newer
Older
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
#!/usr/bin/python
import sys
import numpy as np
import netCDF4 as nc
from numpy import arange, dtype
#*******************************************************************************
def main(args):
"""USE
{0} mask.nc input.nc output.nc
DESCRIPTION
Computes pressure from density and e3t (that changes with SSH).
"""
if len(args)<4 or args[1]=='-h' or args[1]=='--help':
print( main.func_doc.format(args[0]) )
return 0
# ===============================================================================
# mask file
# ===============================================================================
fichiermask=args[1]
mask_file=nc.Dataset(fichiermask)
depth=mask_file.variables['gdept_1d'][:]
tmask=mask_file.variables['tmask'][0,:,:,:]
e3t = mask_file.variables['e3t'][:,:,:,:]
e3w = mask_file.variables['e3w'][:,:,:,:]
mask_file.close()
#===============================================================================
# input
#===============================================================================
fichierjulien=args[2]
lefichier = nc.Dataset(fichierjulien)
rhopv = lefichier.variables['rhop']
ji=rhopv.shape[3]
jj=rhopv.shape[2]
jk=rhopv.shape[1]
jl=rhopv.shape[0]
ssh1 = lefichier.variables['ssh']
itv=lefichier.variables['time_counter']
lon=lefichier.variables['nav_lon_grid_T'][:]
lat=lefichier.variables['nav_lat_grid_T'][:]
#===============================================================================
# output
#===============================================================================
p_file=args[3]
print('Creating '+p_file)
# this will overwrite the file
ncfile = nc.Dataset(p_file,'w', format='NETCDF3_CLASSIC')
setattr(ncfile,'history',' '.join(args))
ncfile.createDimension('x',ji)
ncfile.createDimension('y',jj)
ncfile.createDimension('dept',jk)
ncfile.createDimension('time') # unlimited
data = ncfile.createVariable('lont',dtype('float64').char,('y','x'))
data[:] = lon
setattr(data, 'units',"degrees_north")
setattr(data, 'standard_name',"longitude_at_T_location")
data = ncfile.createVariable('latt',dtype('float64').char,('y','x'))
data[:] = lat
setattr(data, 'units',"degrees_east")
setattr(data, 'standard_name',"latitude_at_T_location")
data = ncfile.createVariable('dept',dtype('float64').char,('dept'))
data[:] = depth
setattr(data, 'units',"m")
setattr(data, 'positive',"down")
setattr(data, 'standard_name',"depth_at_T_location")
setattr(data, 'axis',"Z")
otv = ncfile.createVariable('time',dtype('float64').char,('time'))
setattr(otv, 'units',itv.units)
setattr(otv, 'standard_name',"time")
setattr(otv, 'axis',"T")
pv = ncfile.createVariable('Ptot',dtype('float32').char,('time','dept','y','x'))
setattr(pv, 'standard_name',"Amplitude Of Pressure Total")
setattr(pv, 'units',"m-1.kg.s-2")
setattr(pv, 'coordinates', "time dept latt lont")
#===============================================================================
# process
#===============================================================================
print( 'Dimensions: jl={} jk={} jj={} ji={}'.format(jl,jk,jj,ji) )
p_tot=np.zeros(shape=(jk,jj,ji))
grav=9.81
for l in range(0,jl):
time=itv[l]
print( 'Time frame {}/{}'.format(l,jl) )
e3t1=e3t[0, 0, :, :]
e3w1=e3w[0, :, :, :]
rhop=rhopv[l, :, :, :]
ssh=ssh1[l, :, :]
for k in range(0,jk):
if k==0:
p0=0.5*grav*e3t1*rhop[k,:,:]+(rhop[k,:,:]*grav*ssh[:,:])
else:
p0+=0.5*grav*e3w1[k,:,:]*(rhop[k,:,:]+rhop[k-1,:,:])
p0*=tmask[k,:,:]
p_tot[k,:,:]=p0
p_tot[tmask==0]=nc.default_fillvals['f4']
pv[l,:,:,:] = p_tot
otv[l] = time
#===============================================================================
# clean-up
#===============================================================================
lefichier.close()
ncfile.close()
#*******************************************************************************
if __name__ == '__main__':
#file:///usr/share/doc/packages/python/html/python-2.7-docs-html/library/sys.html#sys.exit
sys.exit(main(sys.argv))