-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc_rel_hum.py
More file actions
57 lines (37 loc) · 1.3 KB
/
Copy pathcalc_rel_hum.py
File metadata and controls
57 lines (37 loc) · 1.3 KB
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
def calc_humidity(temp, dewpoint):
'''
calculates the humidity via the formula from weatherwise.org
return the relative humidity
'''
t = fahrenheit_to_celsius(temp)
td = fahrenheit_to_celsius(dewpoint)
num = 112 - (0.1 * t) + td
denom = 112 + (0.9 * t)
rh = math.pow((num / denom), 8)
return rh
def kelvin_to_fahrenheit(k):
'Degrees Kelvin (K) to degrees Fahrenheit (F)'
return (k - 255.37) * 1.8
def fahrenheit_to_celsius(f):
'Degrees Fahrenheit (F) to degrees Celsius (C)'
return (f - 32.0) * 0.555556
import math
import netCDF4 as nc
import numpy as np
infile = nc.Dataset('/home/rsgadmin/Downloads/humtest_comp.nc')
outfile = nc.Dataset('/home/rsgadmin/jan_rel_humid.nc','r+')
dew = infile.variables['d2m'][:]
t = infile.variables['t2m'][:]
time = infile.variables['time'][:]
print infile.dimensions.keys()
outVar = outfile.createVariable("RH", infile.variables['d2m'].datatype, ("time", "latitude", "longitude"))
# Copy variable attributes
print dew.shape
print t.shape
outarr = np.empty(dew.shape)
for a in range(dew.shape[0]):
for x in range(dew.shape[1]):
for y in range(dew.shape[2]):
outarr[a,x,y] = 100*calc_humidity(kelvin_to_fahrenheit(t[a,x,y]),kelvin_to_fahrenheit(dew[a,x,y]))
outVar[:]=outarr[:]
outfile.close()