-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc_humid.ncl
206 lines (151 loc) · 4.97 KB
/
calc_humid.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
;;; NCL script to calculate relative humidity and dewpoint from huss, tas, and ps
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;; Command-line arguments:
;; psfile required: name of input file with pressure
;; tasfile required: name of input file with temperature
;; hussfile required: name of input file with specific humidity
;; hursfile required: name of output file for relative humidity
;; dewptfile required: name of output file for dewpoint temperature
;; verbose boolean option: print progress indicators?
if (.not. isvar("verbose")) then
verbose = False
end if
finp = addfile(psfile, "r")
fint = addfile(tasfile, "r")
finh = addfile(hussfile, "r")
system("rm -f "+hursfile+" "+dewptfile)
fouth = addfile(hursfile, "c")
foutd = addfile(dewptfile, "c")
filedimdef(fouth,"time",-1,True) ;; make time dimension unlimited
filedimdef(foutd,"time",-1,True) ;; make time dimension unlimited
;; use tfile as template, since it has a reference height
;; copy global attributes
att_names = getvaratts(fint)
do i = 0,dimsizes(att_names)-1
if(att_names(i) .eq. "history_of_appended_files") then
;; skip
else if(att_names(i) .eq. "title") then
fouth@title = str_sub_str(fint@title, "Temperature", "Relative Humidity")
fouth@title = str_sub_str(fint@title, "Temperature", "Dew Point Temperature")
else
fouth@$att_names(i)$ = fint@$att_names(i)$
foutd@$att_names(i)$ = fint@$att_names(i)$
end if
end if
end do
;; To track the full history of the file, we need histories of all
;; the ancestors.
BR = tochar(10)
history = "###################################"+BR
history = history + "History of parent file " + hussfile + ":"+BR
history = history + finh@history+BR
history = history + "-------------------------"+BR
history = history + "History of parent file " + psfile + ":"+BR
history = history + finp@history+BR
history = history + "-------------------------"+BR
history = history + "History of parent file " + tasfile + ":"+BR
history = history + fint@history+BR
history = history + "#########################"+BR
history = history + "Created " + systemfunc("date")
history = history + " by "+systemfunc("whoami")+"@"+systemfunc("hostname")
history = history + " using NCL script calc_humid.ncl from parent files listed above."+BR
fouth@history = history
foutd@history = history
;; update unique tracking_ids
fouth@tracking_id = systemfunc("uuidgen")
foutd@tracking_id = systemfunc("uuidgen")
; copy variables
var_names = getfilevarnames (fint) ;
do i = 0,dimsizes(var_names)-1
if (var_names(i) .ne. "tas") then
fouth->$var_names(i)$ = fint->$var_names(i)$
foutd->$var_names(i)$ = fint->$var_names(i)$
end if
end do
;; check units
if(fint->tas@units .ne. "K") then
print("tas has units '"+fint->tas@units+"'; needs to be 'K'")
exit
end if
if(finh->huss@units .ne. "kg kg-1") then
print("huss has units '"+finh->huss@units+"'; needs to be 'kg kg-1'")
exit
end if
if(finp->ps@units .ne. "Pa") then
print("ps has units '"+finp->ps@units+"'; needs to be 'Pa'")
exit
end if
;; Calculate relative humidity
;; Because there's not enough room to fit everything in memory at
;; once, we loop on time
;; initialize this way to get correct structure
hurs = fint->tas*0
dewpt = hurs
nt = dimsizes(fint->time)
do i=0, nt-1
t = fint->tas(i,:,:)
p = finp->ps(i,:,:)
h = finh->huss(i,:,:)
;; x = mixing ratio
x = h/(1-h)
temphurs = relhum(t,x,p)
;; NB: function output is % RH; dewtemp_trh wants % RH, but CF unit is 1
tempdewpt = dewtemp_trh(t,temphurs)
temphurs = temphurs/100
hurs(i,:,:) = (/temphurs/)
dewpt(i,:,:) = (/tempdewpt/)
if (verbose .and. (i%100) .eq. 0) then print(""+i) end if
end do
xdim = fint->tas!2
hurs!2 = xdim
hurs&$xdim$=fint->$xdim$
ydim = fint->tas!1
hurs!1 = ydim
hurs&$ydim$=fint->$ydim$
hurs!0 = "time"
;hurs!1 = "yc"
;hurs!2 = "xc"
;hurs!1 = "lat"
;hurs!2 = "lon"
hurs&time = fint->time
;hurs&xc = fint->xc
;hurs&yc = fint->yc
;hurs&lon = fint->lon
;hurs&lat = fint->lat
hurs@units = "1"
hurs@long_name = "Near-Surface Relative Humidity"
hurs@standard_name = "relative_humidity"
hurs@missing_value = 1.e+20
hurs@_FillValue = 1.e+20
hurs@coordinates = t@coordinates
hurs@grid_mapping = t@grid_mapping
fouth->hurs = hurs
xdim = fint->tas!2
dewpt!2 = xdim
dewpt&$xdim$=fint->$xdim$
ydim = fint->tas!1
dewpt!1 = ydim
dewpt&$ydim$=fint->$ydim$
dewpt!0 = "time"
;dewpt!1 = "yc"
;dewpt!2 = "xc"
;dewpt!1 = "lat"
;dewpt!2 = "lon"
dewpt&time = fint->time
;dewpt&xc = fint->xc
;dewpt&yc = fint->yc
;dewpt&xc = fint->lon
;dewpt&yc = fint->lat
dewpt@units = "K"
dewpt@long_name = "Dew Point Temperature"
dewpt@standard_name = "dew_point_temperature"
dewpt@missing_value = 1.e+20
dewpt@_FillValue = 1.e+20
dewpt@coordinates = t@coordinates
dewpt@grid_mapping = t@grid_mapping
foutd->dewpt = dewpt
exit
;; Copyright 2009-2012 Univ. Corp. for Atmos. Research
;; Author: Seth McGinnis, [email protected]