-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCalc_Btp_Ks_daily_generic.ncl
executable file
·138 lines (107 loc) · 3.65 KB
/
Calc_Btp_Ks_daily_generic.ncl
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
load "$NCARG_ROOT/lib/ncarg/nclex/gsun/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
; Code to calculate Rossby stationary wave number, following Hoskins and Ambrizzi 1993.
; Rossby waves are refracted in latitude towards regions of higher Ks
; As dl/dt = cg dKs/dy
; Ks is defined as (Beta*/Uzm)^0.5
; Or on a Mercator coordinate:
; Ks = (2Omega - 1/a(1/cos(phi) d/dphi(1/cos(phi) d/dphi(cos(phi)U))))*cos2(phi)/a
begin
; Get arguments
argDir = getenv("NCL_ARG_dir")
file_prefix = getenv("NCL_ARG_filename")
Dir = argDir + "/daily_data/"
; Define some constants
a = 6.37122e06 ; radius of Earth
PI = 3.14159265358979
omega = 7.2921e-5 ; rotation speed
g = 9.80616 ; gravitational constant
filename = (Dir + file_prefix)
print(filename)
cdf_filepl = addfile(filename,"r")
; get coordinate data
nlat = filevardimsizes(cdf_filepl,"latitude")
nlon = filevardimsizes(cdf_filepl,"longitude")
ntimes = filevardimsizes(cdf_filepl,"time")
lat = cdf_filepl->latitude
lon = cdf_filepl->longitude
times = cdf_filepl->time
; get U wind data
U = cdf_filepl->u(:,:,:)
;U = cdf_filepl->u(:,0,:,:)
; Set up some trig constants
phi = lat*PI/180.0 ; Get latitude in radians
cphi = cos(phi)
c2phi = cphi * cphi
acphi = a * cos(phi)
asphi = a * sin(phi)
f = 2*omega*sin(phi)
a2 = a*a
f2 = f * f
; Calculate Ucos(phi)
Ucphi = U * conform(U,cphi,1)
copy_VarCoords(U,Ucphi)
; Calculate d/dphi (Ucphi)
dUcphidphi = center_finite_diff_n(Ucphi(time|:,latitude|:,longitude|:),phi,False,0,1)
; Calculate 1/cphi * dUcphi/dphi
tempdUcphidphi = dUcphidphi / conform(dUcphidphi,cphi,1)
; Calculate meridional gradient of this
d2Uetcdphi = center_finite_diff_n(tempdUcphidphi,phi,False,0,1)
; Calculate BetaM
BetaM1 = 2.0 * omega * c2phi / a
; Correction on 22nd Aug 2019: c2phi to cphi
; Mostly this only impacts higher latitudes
;BetaM2 = d2Uetcdphi * conform(d2Uetcdphi,c2phi,1) / a2
BetaM2 = d2Uetcdphi * conform(d2Uetcdphi,cphi,1) / a2
BetaM = conform(BetaM2,BetaM1,1) - BetaM2
; Calculate inverse of U
Uinv = 1. / where(U.ne.0, U, U@_FillValue)
; Calculate Ks = (a^2 cos(phi) * BetaM / U)^0.5
Ks2 = conform(BetaM,a2,-1) * conform(BetaM,cphi,1) * BetaM * Uinv
Ks = sqrt(Ks2)
delete("BetaM1")
delete("BetaM2")
delete("Uinv")
delete("d2Uetcdphi")
delete("tempdUcphidphi")
delete("Ucphi")
delete("U")
;Ks!0 = "time"
;Ks!1 = "lat"
;Ks!2 = "lon"
;Ks@time = times
;Ks@lat = lat
;Ks@lon = lon
copy_VarCoords(U,Ks)
copy_VarCoords(U,Ks2)
copy_VarCoords(Ks,BetaM)
;----------------------------------------------------------------------
; Write out results to a new netcdf file
;-----------------------------------------------------------------------
; allow large variables
setfileoption("nc","format","netcdf4")
filo = "Ks_" + file_prefix
system("/bin/rm -f " + Dir + filo)
fout_std = addfile(Dir + filo, "c")
setfileoption(fout_std,"DefineMode",True)
;set attributes of output file
fAtt = True
fAtt@creation_date = systemfunc("date")
fileattdef(fout_std,fAtt)
print("created it")
filedimdef(fout_std,(/"time","latitude","longitude"/),(/ntimes,nlat,nlon/),(/True,False,False/))
filevardef(fout_std,"Ks",typeof(Ks),getvardims(Ks))
filevardef(fout_std,"Ks2",typeof(Ks2),getvardims(Ks2))
filevardef(fout_std,"latitude",typeof(lat),(/"latitude"/))
filevardef(fout_std,"longitude",typeof(lon),(/"longitude"/))
filevardef(fout_std,"time",typeof(times),(/"time"/))
fAtt@history = "created by Calc_Btp_Ks_daily_generic.ncl on " + systemfunc("date")
;filevardef(fout_std,"U",typeof(U),getvardims(U))
fout_std->Ks = (/Ks/)
fout_std->Ks2 = (/Ks2/)
fout_std->latitude = (/lat/)
fout_std->longitude = (/lon/)
fout_std->time = (/times/)
print("printed it")
end