forked from mdekauwe/big_leaf_vs_two_leaf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
110 lines (85 loc) · 2.48 KB
/
utils.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
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
#!/usr/bin/env python
"""
A series of miscellaneous funcs
That's all folks.
"""
__author__ = "Martin De Kauwe"
__version__ = "1.0 (16.04.2017)"
__email__ = "[email protected]"
import numpy as np
import constants as c
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
def vpd_to_rh(vpd, tair, pressure):
"""
Convert from VPD to RH.
Parameters:
----------
tair : float
air temperature (deg C)
vpd : float
Vapour pressure deficit (kPa, needs to be in Pa, see conversion
below)
Returns:
--------
rh : float
relative humidity (fraction)
"""
rh = 1.0 - (vpd * c.KPA_2_PA) / calc_esat(tair)
return max(0.0, min(1.0, rh))
def calc_esat(tair):
"""
Saturation vapour pressure or saturation partial pressure of water vapour
Calculated following Tetens forumula based on Buck.
Parameters:
----------
tair : float
air temperature (deg C)
Returns:
--------
esat : float
Saturation vapor pressure (Pa K-1)
References:
* Buck, A. (1981) New equations for computing vapor pressure and
enhancement factor. Journal of Applied Meteorology, 20, 1527-1532
but also see...
* Stull 2000 Meteorology for Scientist and Engineers
* Jones, H. G. (1992) Plants and microclimate. Second edition, pg 110
(note error in a - wrong units)
"""
a = 613.75 # correct units
b = 17.502
c = 240.97
esat = a * np.exp( (b * tair) / (c + tair) )
# Buck...
#kpa_2_pa = 1000.
#a = 0.61121 # kPa
#b = 17.502
#c = 240.97 # deg C
#esat = a * (math.exp(b * tair / (c + tair))) * kpa_2_pa
return esat
def get_dewpoint(tair, rh):
"""
The air is saturated when it reaches maximum water holding capacity at a
given temperature, the dew point
Formula is apparently relatively accurate for relative humidity values
above 50%.
Parameters:
----------
tair : float
air temperature (deg C)
RH : float
relative humidity (percent)
Returns:
--------
Td : float
Dew point temp (deg C)
Reference:
----------
* Lawrence, Mark G., 2005: The relationship between relative humidity and
the dewpoint temperature in moist air: A simple conversion and
applications. Bull. Amer. Meteor. Soc., 86, 225-233.
doi: http;//dx.doi.org/10.1175/BAMS-86-2-225
"""
Td = tair - ((100.0 - rh) / 5.)
return Td