This package primarily provides some weather/atmospheric-related calculations. It also provides types for these calculations to traffic, helping to reduce the chance of mixing up units.
For example, these types prevent accidentally using a Celsius temperature as a Fahrenheit temperature:
Documentation: pkg.go.dev/github.com/cdzombak/libwx
go get github.com/cdzombak/libwxDewPointF() and DewPointC() calculate the dew point, given a temperature and relative humidity.
IndoorHumidityRecommendationF() and IndoorHumidityRecommendationC() provide a recommended maximum indoor humidity percentage for the given outdoor temperature.
WindChillF() and WindChillC() calculate the wind chill, given the outdoor temperature and wind speed.
The wind chill formula works given temperatures less than 50ºF and wind speeds greater than 3 mph. To calculate wind chill but return an error if the input is outside this range, use WindChillFWithValidation() and WindChillCWithValidation(). These functions return ErrInputRange if the input is out of the formula's input range.
WetBulbF() and WetBulbC() calculate the wet bulb temperature, given the dry bulb temperature and relative humidity.
This formula is taken from "Wet-Bulb Temperature from Relative Humidity and Air Temperature" (Roland Stull, Journal of Applied Meteorology and Climatology, 2011) and assumes standard sea level pressure.
These functions return ErrInputRange if the input is out of the formula's input range.
HeatIndexFWithValidation() and HeatIndexCWithValidation() calculate the heat index, given a temperature and relative humidity.
HeatIndexWarningF() and HeatIndexWarningC() provide a warning level based on the heat index. These warning levels are based on the NOAA's heat index table:
HeatIndexWarningNoneindicates the heat index does not warrant elevated caution.HeatIndexWarningCautionindicates fatigue is possible with prolonged exposure and activity. Continuing activity could result in heat cramps.HeatIndexWarningExtremeCautionindicates heat cramps and heat exhaustion are possible. Continuing activity could result in heat stroke.HeatIndexWarningDangerindicates heat cramps and heat exhaustion are likely; heat stroke is probable with continued activity.HeatIndexWarningExtremeDangerindicates heat stroke is imminent.
Three functions are provided that perform circular statistics on a slice of Degree values:
AvgDirectionDegcalculates the circular mean of the given set of angles (in degrees).WeightedAvgDirectionDegcalculates the weighted circular mean of the given set of angles (in degrees).StdDevDirectionDegcalculates the circular standard deviation of the given set of angles (in degrees).WeightedStdDevDirectionDegcalculates the weighted circular standard deviation of the given set of angles (in degrees).
These can be used to calculate the average and standard deviation of a set of wind directions, for example.
Note that variance == (standard deviation)^2, but standard deviation of a dataset is in the dataset's units (degrees, in this case). Variance of this dataset would have the unit degrees^2.
DirectionStr returns a string representation of the given compass direction (in degrees).
The following distance types are provided:
MileMeterKm(kilometer)NauticalMile
Each type provides methods to convert to the other types (e.g. NauticalMile.Meters()). An Unwrap() method also exists to get the raw value as a float64.
The RelHumidity type is an integer type representing a relative humidity percentage from 0-100, inclusive. A clamping method and function for this range are provided.
An Unwrap() method also exists to get the raw value as an int; UnwrapFloat64() returns the value as a float64.
The AbsHumidity type represents absolute humidity in grams per cubic meter (g/m³).
An Unwrap() method also exists to get the raw value as a float64.
AbsHumidityFromRelF() and AbsHumidityFromRelC() calculate absolute humidity from relative humidity and temperature.
RelHumidityFromAbsF() and RelHumidityFromAbsC() calculate relative humidity from absolute humidity and temperature.
These conversions use the Antoine equation for water vapor pressure and assume standard atmospheric pressure. The calculations are valid for temperatures from -20°C to 100°C (-4°F to 212°F).
The following pressure types are provided:
PressureInHg(inches of mercury)PressureMb(millibars)
Each type provides methods to convert to the other type (e.g. PressureInHg.Mb()). An Unwrap() method also exists to get the raw value as a float64.
The following speed types are provided:
SpeedMph(miles per hour)SpeedKmh(kilometers per hour)SpeedKnots(knots)
Each type provides methods to convert to the other types (e.g. SpeedKnots.Mph()). An Unwrap() method also exists to get the raw value as a float64.
The following temperature types are provided:
Each type provides methods to convert to the other type (e.g. TempF.C()). An Unwrap() method also exists to get the raw value as a float64.
The following direction type is provided:
Degree(angular degrees)
Finally, libwx provides some utility functions for comparing float64 and int values:
IntCompare(a, b int) intFloat64Compare(a, b, tolerance float64) intFloat64Equal(a, b, tolerance float64) bool
The *Compare(…) functions return:
-1ifa < b0ifa == b1ifa > b
For float64 comparisons functions that accept a tolerance, convenience tolerance constants are provided:
ToleranceExact = float64(0.0)
Tolerance0 = float64(1.0)
Tolerance1 = float64(0.1)
Tolerance01 = float64(0.01)
Tolerance001 = float64(0.001)When making repeated comparisons with the same tolerance, having to pass the tolerance each time is tedious and increases room for human error. To help with this, curried versions of Float64Compare() and Float64Equal() are provided. These return a comparison function with the specified tolerance baked-in:
CurriedFloat64Compare(tolerance float64) func(float64, float64) intCurriedFloat64Equal(tolerance float64) func(float64, float64) bool
For example:
package main
import (
wx "github.com/cdzombak/libwx"
)
function main() {
equalityChecker := wx.CurriedFloat64Equal(Tolerance1)
equalityChecker(1.0, 1.11) // => false
equalityChecker(1.0, 1.01) // => true
}MIT; see LICENSE in this repo.
Chris Dzombak