/libwx

Weather-related types and calculations for Golang

Primary LanguageGoMIT LicenseMIT

github.com/cdzombak/libwx

Go Reference

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:

Custom types allow your IDE/build to prevent accidental unit confusion.

Documentation: pkg.go.dev/github.com/cdzombak/libwx

Installation

go get github.com/cdzombak/libwx

Usage

Dew point calculation

DewPointF() and DewPointC() calculate the dew point, given a temperature and relative humidity.

Indoor humidity recommendation

IndoorHumidityRecommendationF() and IndoorHumidityRecommendationC() provide a recommended maximum indoor humidity percentage for the given outdoor temperature.

Wind chill calculation

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.

Wet bulb temperature calculation

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.

Heat index calculation

HeatIndexF() and HeatIndexC() calculate the heat index, given a temperature and relative humidity.

Heat index warning levels

HeatIndexWarningF() and HeatIndexWarningC() provide a warning level based on the heat index. These warning levels are based on the NOAA's heat index table:

  • HeatIndexWarningNone indicates the heat index does not warrant elevated caution.
  • HeatIndexWarningCaution indicates fatigue is possible with prolonged exposure and activity. Continuing activity could result in heat cramps.
  • HeatIndexWarningExtremeCaution indicates heat cramps and heat exhaustion are possible. Continuing activity could result in heat stroke.
  • HeatIndexWarningDanger indicates heat cramps and heat exhaustion are likely; heat stroke is probable with continued activity.
  • HeatIndexWarningExtremeDanger indicates heat stroke is imminent.

Distance types & conversions

The following distance types are provided:

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.

Humidity type

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.

Pressure types and conversions

The following pressure types are provided:

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.

Speed types and conversions

The following speed types are provided:

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.

Temperature types and conversions

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.

Utilities: Comparisons

Finally, libwx provides some utility functions for comparing float64 and int values:

The *Compare(…) functions return:

  • -1 if a < b
  • 0 if a == b
  • 1 if a > 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)

Curried float64 comparisons

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:

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
}

License

MIT; see LICENSE in this repo.

Author

Chris Dzombak