Skip to content

cdzombak/libwx

Repository files navigation

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.

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.

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)
Tolerace1      = 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