Skip to content

Commit

Permalink
add validation for heat index fns
Browse files Browse the repository at this point in the history
  • Loading branch information
cdzombak committed Oct 9, 2024
1 parent 00338ae commit dc2bf13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ These functions return [`ErrInputRange`](https://pkg.go.dev/github.com/cdzombak/

### Heat index calculation

[`HeatIndexF()`](https://pkg.go.dev/github.com/cdzombak/libwx#HeatIndexF) and [`HeatIndexC()`](https://pkg.go.dev/github.com/cdzombak/libwx#HeatIndexC) calculate the [heat index](https://en.wikipedia.org/wiki/Heat_index), given a temperature and relative humidity.
[`HeatIndexFWithValidation()`](https://pkg.go.dev/github.com/cdzombak/libwx#HeatIndexFWithValidation) and [`HeatIndexCWithValidation()`](https://pkg.go.dev/github.com/cdzombak/libwx#HeatIndexCWithValidation) calculate the [heat index](https://en.wikipedia.org/wiki/Heat_index), given a temperature and relative humidity.

#### Heat index warning levels

Expand Down
33 changes: 27 additions & 6 deletions libwx.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func rawHeatIndex(c [9]float64, rawTemp, rawRelH float64) float64 {
// captured on 2024-07-17
// note that constants on that page are 1-indexed, while the constants
// array here is 0-indexed
// see also: https://www.weather.gov/media/ffc/ta_htindx.PDF
return c[0] +
c[1]*rawTemp +
c[2]*rawRelH +
Expand All @@ -197,24 +198,44 @@ func rawHeatIndex(c [9]float64, rawTemp, rawRelH float64) float64 {
c[8]*math.Pow(rawTemp, 2)*math.Pow(rawRelH, 2)
}

// HeatIndexF calculates the heat index for the given temperature (in Fahrenheit)
// and relative humidity percentage.
// HeatIndexF is deprecated; use HeatIndexFWithValidation
func HeatIndexF(temp TempF, rh RelHumidity) TempF {
retv, _ := HeatIndexFWithValidation(temp, rh)
return retv
}

// HeatIndexC is deprecated; use HeatIndexCWithValidation
func HeatIndexC(temp TempC, rh RelHumidity) TempC {
retv, _ := HeatIndexCWithValidation(temp, rh)
return retv
}

// HeatIndexFWithValidation calculates the heat index for the given temperature (in Fahrenheit)
// and relative humidity percentage.
func HeatIndexFWithValidation(temp TempF, rh RelHumidity) (TempF, error) {
var err error
if temp < TempC(25).F() {
err = ErrInputRange
}
return TempF(rawHeatIndex(
heatIndexConstantsF(),
temp.Unwrap(),
rh.Clamped().UnwrapFloat64(),
))
)), err
}

// HeatIndexC calculates the heat index for the given temperature (in Celsius)
// HeatIndexCWithValidation calculates the heat index for the given temperature (in Celsius)
// and relative humidity percentage.
func HeatIndexC(temp TempC, rh RelHumidity) TempC {
func HeatIndexCWithValidation(temp TempC, rh RelHumidity) (TempC, error) {
var err error
if temp < TempC(25) {
err = ErrInputRange
}
return TempC(rawHeatIndex(
heatIndexConstantsC(),
temp.Unwrap(),
rh.Clamped().UnwrapFloat64(),
))
)), err
}

// HeatIndexWarningF returns a heat index warning level for the
Expand Down

0 comments on commit dc2bf13

Please sign in to comment.