Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import cayenne.client
  3. import time
  4. import logging
  5. import board
  6. import busio
  7. import digitalio
  8. import adafruit_bme280
  9. import math
  10. import scipy
  11.  
  12. # Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
  13. MQTT_USERNAME = "19cfb8a0-82e7-11e8-9464-a9a522c157c5"
  14. MQTT_PASSWORD = "cc4752158e665ff01ab325271ff06017c200a457"
  15. MQTT_CLIENT_ID = "489fe300-d6ea-11e9-a38a-d57172a4b4d4"
  16.  
  17. client = cayenne.client.CayenneMQTTClient()
  18. client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
  19. # For a secure connection use port 8883 when calling client.begin:
  20. # client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)
  21.  
  22. #Defining VPD calculation function
  23. def ea_calc(airtemp= scipy.array([]),\
  24. rh= scipy.array([])):
  25. '''
  26. Function to calculate actual vapour pressure from relative humidity:
  27.  
  28. .. math::
  29. e_a = \\frac{rh \\cdot e_s}{100}
  30.  
  31. where es is the saturated vapour pressure at temperature T.
  32. Parameters:
  33. - airtemp: array of measured air temperatures [Celsius].
  34. - rh: Relative humidity [%].
  35. Returns:
  36. - ea: array of actual vapour pressure [Pa].
  37. Examples
  38. --------
  39.  
  40. >>> ea_calc(25,60)
  41. 1900.0946514729308
  42. '''
  43.  
  44. # Test input array/value
  45. airtemp,rh = _arraytest(airtemp, rh)
  46.  
  47. # Calculate saturation vapour pressures
  48. es = es_calc(airtemp)
  49. # Calculate actual vapour pressure
  50. eact = rh / 100.0 * es
  51. return eact # in Pa
  52.  
  53. def es_calc(airtemp= scipy.array([])):
  54. '''
  55. Function to calculate saturated vapour pressure from temperature.
  56. For T<0 C the saturation vapour pressure equation for ice is used
  57. accoring to Goff and Gratch (1946), whereas for T>=0 C that of
  58. Goff (1957) is used.
  59.  
  60. Parameters:
  61. - airtemp : (data-type) measured air temperature [Celsius].
  62.  
  63. Returns:
  64. - es : (data-type) saturated vapour pressure [Pa].
  65. References
  66. ----------
  67.  
  68. - Goff, J.A.,and S. Gratch, Low-pressure properties of water from -160 \
  69. to 212 F. Transactions of the American society of heating and \
  70. ventilating engineers, p. 95-122, presented at the 52nd annual \
  71. meeting of the American society of \
  72. heating and ventilating engineers, New York, 1946.
  73. - Goff, J. A. Saturation pressure of water on the new Kelvin \
  74. temperature scale, Transactions of the American \
  75. society of heating and ventilating engineers, pp 347-354, \
  76. presented at the semi-annual meeting of the American \
  77. society of heating and ventilating engineers, Murray Bay, \
  78. Quebec. Canada, 1957.
  79. Examples
  80. --------
  81. >>> es_calc(30.0)
  82. 4242.725994656632
  83. >>> x = [20, 25]
  84. >>> es_calc(x)
  85. array([ 2337.08019792, 3166.82441912])
  86.  
  87. '''
  88.  
  89. # Test input array/value
  90. airtemp = _arraytest(airtemp)
  91.  
  92. # Determine length of array
  93. n = scipy.size(airtemp)
  94. # Check if we have a single (array) value or an array
  95. if n < 2:
  96. # Calculate saturated vapour pressures, distinguish between water/ice
  97. if airtemp < 0:
  98. # Calculate saturation vapour pressure for ice
  99. log_pi = - 9.09718 * (273.16 / (airtemp + 273.15) - 1.0) \
  100. - 3.56654 * math.log10(273.16 / (airtemp + 273.15)) \
  101. + 0.876793 * (1.0 - (airtemp + 273.15) / 273.16) \
  102. + math.log10(6.1071)
  103. es = math.pow(10, log_pi)
  104. else:
  105. # Calculate saturation vapour pressure for water
  106. log_pw = 10.79574 * (1.0 - 273.16 / (airtemp + 273.15)) \
  107. - 5.02800 * math.log10((airtemp + 273.15) / 273.16) \
  108. + 1.50475E-4 * (1 - math.pow(10, (-8.2969 * ((airtemp +\
  109. 273.15) / 273.16 - 1.0)))) + 0.42873E-3 * \
  110. (math.pow(10, (+4.76955 * (1.0 - 273.16\
  111. / (airtemp + 273.15)))) - 1) + 0.78614
  112. es = math.pow(10, log_pw)
  113. else: # Dealing with an array
  114. # Initiate the output array
  115. es = scipy.zeros(n)
  116. # Calculate saturated vapour pressures, distinguish between water/ice
  117. for i in range(0, n):
  118. if airtemp[i] < 0:
  119. # Saturation vapour pressure equation for ice
  120. log_pi = - 9.09718 * (273.16 / (airtemp[i] + 273.15) - 1.0) \
  121. - 3.56654 * math.log10(273.16 / (airtemp[i] + 273.15)) \
  122. + 0.876793 * (1.0 - (airtemp[i] + 273.15) / 273.16) \
  123. + math.log10(6.1071)
  124. es[i] = math.pow(10, log_pi)
  125. else:
  126. # Calculate saturation vapour pressure for water
  127. log_pw = 10.79574 * (1.0 - 273.16 / (airtemp[i] + 273.15)) \
  128. - 5.02800 * math.log10((airtemp[i] + 273.15) / 273.16) \
  129. + 1.50475E-4 * (1 - math.pow(10, (-8.2969\
  130. * ((airtemp[i] + 273.15) / 273.16 - 1.0)))) + 0.42873E-3\
  131. * (math.pow(10, (+4.76955 * (1.0 - 273.16\
  132. / (airtemp[i] + 273.15)))) - 1) + 0.78614
  133. es[i] = pow(10, log_pw)
  134. # Convert from hPa to Pa
  135. es = es * 100.0
  136. return es # in Pa
  137.  
  138.  
  139. def _arraytest(*args):
  140. '''
  141. Function to convert input parameters in as lists or tuples to
  142. arrays, while leaving single values intact.
  143. Test function for single values or valid array parameter input
  144. (J. Delsman).
  145. Parameters:
  146. args (array, list, tuple, int, float): Input values for functions.
  147. Returns:
  148. rargs (array, int, float): Valid single value or array function input.
  149. Examples
  150. --------
  151.  
  152. >>> _arraytest(12.76)
  153. 12.76
  154. >>> _arraytest([(1,2,3,4,5),(6,7,8,9)])
  155. array([(1, 2, 3, 4, 5), (6, 7, 8, 9)], dtype=object)
  156. >>> x=[1.2,3.6,0.8,1.7]
  157. >>> _arraytest(x)
  158. array([ 1.2, 3.6, 0.8, 1.7])
  159. >>> _arraytest('This is a string')
  160. 'This is a string'
  161. '''
  162.  
  163. rargs=[]
  164. for a in args:
  165. if isinstance(a, (list, tuple)):
  166. rargs.append(scipy.array(a))
  167. else:
  168. rargs.append(a)
  169. if len(rargs) == 1:
  170. return rargs[0] # no unpacking if single value, return value i/o list
  171. else:
  172. return rargs
  173.  
  174. def vpd_calc(airtemp= scipy.array([]),\
  175. rh= scipy.array([])):
  176. '''
  177. Function to calculate vapour pressure deficit.
  178. Parameters:
  179. - airtemp: measured air temperatures [Celsius].
  180. - rh: (array of) rRelative humidity [%].
  181.  
  182. Returns:
  183. - vpd: (array of) vapour pressure deficits [Pa].
  184.  
  185. Examples
  186. --------
  187.  
  188. >>> vpd_calc(30,60)
  189. 1697.090397862653
  190. >>> T=[20,25]
  191. >>> RH=[50,100]
  192. >>> vpd_calc(T,RH)
  193. array([ 1168.54009896, 0. ])
  194.  
  195. '''
  196.  
  197. # Test input array/value
  198. airtemp,rh = _arraytest(airtemp, rh)
  199.  
  200. # Calculate saturation vapour pressures
  201. es = es_calc(airtemp)
  202. eact = ea_calc(airtemp, rh)
  203. # Calculate vapour pressure deficit
  204. vpd = es - eact
  205. return vpd # in hPa
  206.  
  207.  
  208.  
  209. #initiate SPI bus
  210. spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
  211. cs = digitalio.DigitalInOut(board.D5)
  212. bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)
  213.  
  214. i=0
  215. timestamp = 0
  216.  
  217. #calculating vpd
  218. #vpd = vpd_calc(bme280.temperature, bme280.humidity)/100
  219.  
  220. while True:
  221. client.loop()
  222.  
  223. if (time.time() > timestamp + 10):
  224. client.celsiusWrite(1, bme280.temperature)
  225. client.virtualWrite(2, bme280.humidity, "rel_hum", "p")
  226. client.hectoPascalWrite(4, (vpd_calc(bme280.temperature, bme280.humidity)/100))
  227. client.hectoPascalWrite(3, bme280.pressure)
  228. timestamp = time.time()
  229. i = i+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement