<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://ocotal.iarc.uaf.edu/index.php?action=history&amp;feed=atom&amp;title=Calculate_Daily_Averages_%2F_Sums_from_hourly_data</id>
	<title>Calculate Daily Averages / Sums from hourly data - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://ocotal.iarc.uaf.edu/index.php?action=history&amp;feed=atom&amp;title=Calculate_Daily_Averages_%2F_Sums_from_hourly_data"/>
	<link rel="alternate" type="text/html" href="http://ocotal.iarc.uaf.edu/index.php?title=Calculate_Daily_Averages_/_Sums_from_hourly_data&amp;action=history"/>
	<updated>2026-04-21T06:22:13Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://ocotal.iarc.uaf.edu/index.php?title=Calculate_Daily_Averages_/_Sums_from_hourly_data&amp;diff=737&amp;oldid=prev</id>
		<title>137.229.92.253 at 18:51, 1 August 2008</title>
		<link rel="alternate" type="text/html" href="http://ocotal.iarc.uaf.edu/index.php?title=Calculate_Daily_Averages_/_Sums_from_hourly_data&amp;diff=737&amp;oldid=prev"/>
		<updated>2008-08-01T18:51:24Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Copy and paste the text in the box into the visual basic editor that comes with Excel.  To get access to personal.xls in the visual basic editor check out this article: [[Save Macros in Excel]]&lt;br /&gt;
&lt;br /&gt;
This macro and collection of subroutines creates daily averages from hourly data.  It was written originally for data suitable for use with MicroMet but wider application are totally possible.  What's handy about this collection of macros is that Air temperature is averaged, precipitation is totalized, wind speed is converted to vector format and back to radial format and relative humidity is averaged correctly, too.&lt;br /&gt;
&lt;br /&gt;
Any questions check with Bob. &lt;br /&gt;
&lt;br /&gt;
 Option Explicit&lt;br /&gt;
 Private Sub daily_avg_sum_etc() &lt;br /&gt;
   ' worksheet variables&lt;br /&gt;
   Dim inputrow&lt;br /&gt;
   Dim outputrow&lt;br /&gt;
   Dim inputWKS&lt;br /&gt;
   Dim outputWKS&lt;br /&gt;
   Dim tempCOL&lt;br /&gt;
   Dim rhCOl&lt;br /&gt;
   Dim wsCOL&lt;br /&gt;
   Dim wdCOL&lt;br /&gt;
   Dim precipCOL&lt;br /&gt;
  &lt;br /&gt;
   &lt;br /&gt;
   ' date and time variables&lt;br /&gt;
   Dim inputdate&lt;br /&gt;
   Dim olddate&lt;br /&gt;
   Dim outputYEAR&lt;br /&gt;
   Dim outputDAY&lt;br /&gt;
   Dim outputMONTH&lt;br /&gt;
   Dim outputTIME&lt;br /&gt;
   &lt;br /&gt;
   ' position variables&lt;br /&gt;
   Dim easting&lt;br /&gt;
   Dim northing&lt;br /&gt;
   Dim stationID&lt;br /&gt;
   Dim ELevation&lt;br /&gt;
   &lt;br /&gt;
   ' environmental input variables&lt;br /&gt;
   Dim inputWS As Double&lt;br /&gt;
   Dim inputWD As Double&lt;br /&gt;
   Dim inputAT As Double&lt;br /&gt;
   Dim inputRH As Double&lt;br /&gt;
   Dim inputPRECIP As Double&lt;br /&gt;
   &lt;br /&gt;
   ' environmental sums for daily means&lt;br /&gt;
   Dim sumAT As Double&lt;br /&gt;
   Dim sumVP As Double&lt;br /&gt;
   Dim sumxWS As Double&lt;br /&gt;
   Dim sumyWS As Double&lt;br /&gt;
   Dim sumPRECIP As Double&lt;br /&gt;
   Dim countAT As Double&lt;br /&gt;
   Dim countVP As Double&lt;br /&gt;
   Dim countWS As Double&lt;br /&gt;
   Dim countPRECIP As Double&lt;br /&gt;
   Dim zoo As Double&lt;br /&gt;
   &lt;br /&gt;
   ' environmental output variables&lt;br /&gt;
   Dim outputWS As Double&lt;br /&gt;
   Dim outputWD As Double&lt;br /&gt;
   Dim outputAT As Double&lt;br /&gt;
   Dim outputVP As Double&lt;br /&gt;
   Dim outputRH As Double&lt;br /&gt;
   Dim outputPRECIP As Double&lt;br /&gt;
  &lt;br /&gt;
   &lt;br /&gt;
   ' things to do:&lt;br /&gt;
   ' 1) split date string&lt;br /&gt;
   ' 2) add an arbitrary hour to worksheet&lt;br /&gt;
   ' 3) add station ID, easting, &amp;amp; northing to worksheet&lt;br /&gt;
   ' 4) convert station data to metric and output to worksheet&lt;br /&gt;
   ' 5) convert dewpoint &amp;amp; temperature to RH&lt;br /&gt;
   &lt;br /&gt;
   ''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;
   '' things to change before each station process ''&lt;br /&gt;
   ''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;
   easting = 516394&lt;br /&gt;
   northing = 7256371&lt;br /&gt;
   northing = northing - 7000000 ' for micromet format&lt;br /&gt;
   ELevation = 95&lt;br /&gt;
   stationID = 102&lt;br /&gt;
   outputrow = 4&lt;br /&gt;
   inputrow = 1&lt;br /&gt;
   '''''''''''''''''''''''''''''&lt;br /&gt;
   '' set worksheet variables ''&lt;br /&gt;
   '''''''''''''''''''''''''''''&lt;br /&gt;
   inputWKS = 2&lt;br /&gt;
   outputWKS = 1&lt;br /&gt;
   tempCOL = 7&lt;br /&gt;
   rhCOl = 10&lt;br /&gt;
   wsCOL = 14&lt;br /&gt;
   wdCOL = 15&lt;br /&gt;
   precipCOL = 17&lt;br /&gt;
   &lt;br /&gt;
   ''''''''''''''''''''''''''''''''''''&lt;br /&gt;
   '' Start running through the data ''&lt;br /&gt;
   ''''''''''''''''''''''''''''''''''''&lt;br /&gt;
   olddate = Format(Worksheets(inputWKS).Cells(inputrow, 1).Value, &amp;quot;mm/dd/yyyy&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
   Do While Worksheets(inputWKS).Cells(inputrow, 1).Value &amp;lt;&amp;gt; Empty&lt;br /&gt;
     inputdate = Format(Worksheets(inputWKS).Cells(inputrow, 1).Value, &amp;quot;mm/dd/yyyy&amp;quot;)&lt;br /&gt;
     If olddate &amp;lt;&amp;gt; inputdate Then&lt;br /&gt;
       ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;
       '' do final formatting and calculations prior to outputting ''&lt;br /&gt;
       ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;
       outputYEAR = Right(CStr(olddate), 4)&lt;br /&gt;
       outputMONTH = Left(CStr(olddate), 2)&lt;br /&gt;
       outputDAY = Mid(CStr(olddate), 4, 2)&lt;br /&gt;
       outputTIME = 1&lt;br /&gt;
       If countAT &amp;lt;&amp;gt; 0 Then outputAT = sensor_avg(sumAT, countAT) Else outputAT = -9999&lt;br /&gt;
       If countVP &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
         outputVP = sensor_avg(sumVP, countVP)&lt;br /&gt;
         outputRH = VaporpressuretoRH(outputVP, outputAT) * 100&lt;br /&gt;
         If outputRH &amp;gt; 100 Then outputRH = 100&lt;br /&gt;
         &lt;br /&gt;
       Else:&lt;br /&gt;
         outputRH = -9999&lt;br /&gt;
       End If&lt;br /&gt;
       ' note to self: adjust WS to 10m before outputtting&lt;br /&gt;
       ' also, use two functions, one to convert ux, uy into WS and one to convert into WD.&lt;br /&gt;
       ' if there is only WS then output only WS also.&lt;br /&gt;
       If countWS &amp;lt;&amp;gt; 0 And sumxWS &amp;lt;&amp;gt; 0 And sumyWS &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
         ' there's stuff to do.&lt;br /&gt;
         sumxWS = sumxWS / countWS&lt;br /&gt;
         sumyWS = sumyWS / countWS&lt;br /&gt;
         outputWS = uxy_to_WS(sumxWS, sumyWS)&lt;br /&gt;
         If outputMONTH &amp;lt; 6 Or outputMONTH &amp;gt; 8 Then&lt;br /&gt;
           zoo = 0.01&lt;br /&gt;
         Else&lt;br /&gt;
           zoo = 0.03&lt;br /&gt;
         End If&lt;br /&gt;
         outputWS = elevateWS(outputWS, 3, 10, zoo)&lt;br /&gt;
         outputWD = uxy_to_WD(sumxWS, sumyWS)&lt;br /&gt;
       Else&lt;br /&gt;
         outputWS = -9999&lt;br /&gt;
         outputWD = -9999&lt;br /&gt;
       End If&lt;br /&gt;
       If countPRECIP &amp;lt;&amp;gt; 0 Then outputPRECIP = sumPRECIP Else outputPRECIP = -9999&lt;br /&gt;
       &lt;br /&gt;
       &lt;br /&gt;
       '''''''''''''''''''''''''''''''&lt;br /&gt;
       '' dump stuff to spreadsheet ''&lt;br /&gt;
       '''''''''''''''''''''''''''''''&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 1).Value = outputYEAR&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 2).Value = outputMONTH&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 3).Value = outputDAY&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 4).Value = outputTIME&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 5).Value = stationID&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 6).Value = easting&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 7).Value = northing&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 8).Value = ELevation&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 9).Value = outputAT&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 10).Value = outputRH&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 11).Value = outputWS&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 12).Value = outputWD&lt;br /&gt;
       Worksheets(outputWKS).Cells(outputrow, 13).Value = outputPRECIP&lt;br /&gt;
           &lt;br /&gt;
       ''''''''''''''''''''''''''''''''''''&lt;br /&gt;
       '' reset values and increment row ''&lt;br /&gt;
       ''''''''''''''''''''''''''''''''''''&lt;br /&gt;
       sumAT = 0&lt;br /&gt;
       sumVP = 0&lt;br /&gt;
       sumxWS = 0&lt;br /&gt;
       sumyWS = 0&lt;br /&gt;
       outputPRECIP = 0&lt;br /&gt;
       countAT = 0&lt;br /&gt;
       countVP = 0&lt;br /&gt;
       countWS = 0&lt;br /&gt;
       countPRECIP = 0&lt;br /&gt;
       outputrow = outputrow + 1&lt;br /&gt;
       olddate = inputdate&lt;br /&gt;
     End If&lt;br /&gt;
     &lt;br /&gt;
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;
     '' okay with the outputting out of the way we're ready to start reading in the data. ''&lt;br /&gt;
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;
     If tempCOL &amp;lt;&amp;gt; 6999 Then inputAT = Worksheets(inputWKS).Cells(inputrow, tempCOL).Value Else inputAT = -9999&lt;br /&gt;
     If rhCOl &amp;lt;&amp;gt; 6999 Then inputRH = Worksheets(inputWKS).Cells(inputrow, rhCOl).Value Else inputRH = -9999&lt;br /&gt;
     If wsCOL &amp;lt;&amp;gt; 6999 Then inputWS = Worksheets(inputWKS).Cells(inputrow, wsCOL).Value Else inputWS = -9999&lt;br /&gt;
     If wdCOL &amp;lt;&amp;gt; 6999 Then inputWD = Worksheets(inputWKS).Cells(inputrow, wdCOL).Value Else inputWD = -9999&lt;br /&gt;
     If precipCOL &amp;lt;&amp;gt; 6999 Then inputPRECIP = Worksheets(inputWKS).Cells(inputrow, precipCOL).Value Else inputPRECIP = -9999&lt;br /&gt;
     &lt;br /&gt;
     If Abs(inputAT) &amp;lt;&amp;gt; 6999 And inputAT &amp;lt;&amp;gt; -9999 Then&lt;br /&gt;
       sumAT = sumAT + inputAT&lt;br /&gt;
       countAT = countAT + 1&lt;br /&gt;
     End If&lt;br /&gt;
     If Abs(inputRH) &amp;lt;&amp;gt; 6999 And inputRH &amp;lt;&amp;gt; -9999 And Abs(inputAT) &amp;lt;&amp;gt; 6999 And inputAT &amp;lt;&amp;gt; -9999 Then&lt;br /&gt;
       sumVP = sumVP + RHtoVaporpressure(inputRH, inputAT)&lt;br /&gt;
       countVP = countVP + 1&lt;br /&gt;
     End If&lt;br /&gt;
     If Abs(inputWS) &amp;lt;&amp;gt; 6999 And inputWS &amp;lt;&amp;gt; -9999 And Abs(inputWD) &amp;lt;&amp;gt; 6999 And inputWD &amp;lt;&amp;gt; -9999 Then&lt;br /&gt;
       sumxWS = sumxWS + WS_to_ux(inputWS, inputWD)&lt;br /&gt;
       sumyWS = sumyWS + WS_to_uy(inputWS, inputWD)&lt;br /&gt;
       countWS = countWS + 1&lt;br /&gt;
     End If&lt;br /&gt;
     If Abs(inputPRECIP) &amp;lt;&amp;gt; 6999 And inputPRECIP &amp;lt;&amp;gt; -9999 Then&lt;br /&gt;
       sumPRECIP = sumPRECIP + inputPRECIP&lt;br /&gt;
       countPRECIP = countPRECIP + 1&lt;br /&gt;
     End If&lt;br /&gt;
     ' next time step&lt;br /&gt;
     inputrow = inputrow + 1&lt;br /&gt;
   Loop&lt;br /&gt;
 End Sub &lt;br /&gt;
&lt;br /&gt;
 Public Function RHtoVaporpressure(inputRH As Double, inputAT As Double)&lt;br /&gt;
   ' Equations come from Dingman's Physical Hydrology second edition.&lt;br /&gt;
   ' pp 586 - 587&lt;br /&gt;
   ' eq D-7:&lt;br /&gt;
   ' e* = 0.611 * exp( (17.3 * T) / ( T + 237.3 ) )&lt;br /&gt;
   ' e* [kPa], T [Deg C]&lt;br /&gt;
   ' eq D-10:&lt;br /&gt;
   ' Wa = ea / e*a&lt;br /&gt;
   ' Wa = Relative Humidity [%]&lt;br /&gt;
   ' ea = Vapor Pressure [kPa]&lt;br /&gt;
   ' e*a = Saturation Vapor Pressure [kPa]&lt;br /&gt;
   Dim ea As Double&lt;br /&gt;
   Dim e_star_a As Double&lt;br /&gt;
   &lt;br /&gt;
   e_star_a = 0.611 * Exp((17.3 * inputAT) / (inputAT + 237.3))&lt;br /&gt;
   ea = e_star_a * inputRH / 100&lt;br /&gt;
   RHtoVaporpressure = ea&lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Private Function VaporpressuretoRH(VaporPressure As Double, AirTemp As Double)&lt;br /&gt;
   ' Equations come from Dingman's Physical Hydrology second edition.&lt;br /&gt;
   ' pp 586 - 587&lt;br /&gt;
   ' eq D-7:&lt;br /&gt;
   ' e* = 0.611 * exp( (17.3 * T) / ( T + 237.3 ) )&lt;br /&gt;
   ' e* [kPa], T [Deg C]&lt;br /&gt;
   ' eq D-10:&lt;br /&gt;
   ' Wa = ea / e*a&lt;br /&gt;
   ' Wa = Relative Humidity [%]&lt;br /&gt;
   ' ea = Vapor Pressure [kPa]&lt;br /&gt;
   ' e*a = Saturation Vapor Pressure [kPa]&lt;br /&gt;
   Dim ea As Double&lt;br /&gt;
   Dim e_star_a As Double&lt;br /&gt;
  &lt;br /&gt;
   e_star_a = 0.611 * Exp((17.3 * AirTemp) / (AirTemp + 237.3))&lt;br /&gt;
   VaporpressuretoRH = VaporPressure / e_star_a&lt;br /&gt;
  &lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Public Function sensor_avg(total As Double, count As Double)&lt;br /&gt;
   ' this function just returns an average&lt;br /&gt;
   If count &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
     sensor_avg = total / count&lt;br /&gt;
   Else:&lt;br /&gt;
     total = -9999&lt;br /&gt;
   End If&lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Public Function WS_to_ux(inputWS, inputWD)&lt;br /&gt;
   ' assumes WD is in degrees.&lt;br /&gt;
   Dim ux&lt;br /&gt;
   ux = inputWS * Cos(inputWD * 3.14592 / 180)&lt;br /&gt;
   WS_to_ux = ux&lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Public Function WS_to_uy(inputWS, inputWD)&lt;br /&gt;
   ' assumes WD is in degrees.&lt;br /&gt;
   Dim uy&lt;br /&gt;
   uy = inputWS * Sin(inputWD * 3.141592 / 180)&lt;br /&gt;
   WS_to_uy = uy&lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Public Function uxy_to_WS(inputux, inputuy)&lt;br /&gt;
   ' go from vector components back to radial.&lt;br /&gt;
   uxy_to_WS = (inputux ^ 2 + inputuy ^ 2) ^ 0.5&lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Public Function uxy_to_WD(inputux, inputuy)&lt;br /&gt;
   Dim WD&lt;br /&gt;
   ' go from vector components back to radial&lt;br /&gt;
   WD = Atn(inputuy / inputux) * 180 / 3.141592&lt;br /&gt;
   ' now dump it in the right quandrant..&lt;br /&gt;
   If inputux &amp;gt; 0 And inputuy &amp;gt; 0 Then WD = WD + 0&lt;br /&gt;
   If inputux &amp;lt; 0 And inputuy &amp;gt; 0 Then WD = WD + 180&lt;br /&gt;
   If inputux &amp;lt; 0 And inputuy &amp;lt; 0 Then WD = WD + 180&lt;br /&gt;
   If inputux &amp;gt; 0 And inputuy &amp;lt; 0 Then WD = WD + 360&lt;br /&gt;
   uxy_to_WD = WD&lt;br /&gt;
 End Function&lt;br /&gt;
&lt;br /&gt;
 Public Function elevateWS(inputWS, wsheight, reportheight, zoo)&lt;br /&gt;
   ' U(h) = U(H) * ( ln(h/z0) / ln(H/z0) )&lt;br /&gt;
   ' h = target WS height in meters&lt;br /&gt;
   ' H = anemometer height in meters&lt;br /&gt;
   ' z0 = 0.01 September - May&lt;br /&gt;
   ' z0 = 0.03 June - August&lt;br /&gt;
   elevateWS = inputWS * (Log(reportheight / zoo) / Log(wsheight / zoo))&lt;br /&gt;
 End Function&lt;/div&gt;</summary>
		<author><name>137.229.92.253</name></author>
		
	</entry>
</feed>