Added SnowAmount

This commit is contained in:
Winni Neessen 2023-06-23 16:38:56 +02:00
parent fee0bc6795
commit 5dee24573a
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
4 changed files with 132 additions and 2 deletions

View file

@ -48,6 +48,8 @@ type APICurrentWeatherData struct {
PressureMSL *APIFloat `json:"pressureMsl,omitempty"`
// PressureQFE represents the pressure at station level (QFE) in hPa
PressureQFE *APIFloat `json:"pressure,omitempty"`
// SnowAmount represents the the amount of snow in kg/m3
SnowAmount *APIFloat `json:"snowAmount,omitempty"`
// Temperature represents the temperature in °C
Temperature *APIFloat `json:"temp,omitempty,omitempty"`
// WindDirection represents the direction from which the wind
@ -72,8 +74,6 @@ type APICurrentWeatherData struct {
// GlobalRadiation24h represents the sum of global radiation over the last
// 24 hour in kJ/m²
GlobalRadiation24h *APIFloat `json:"globalRadiation24h,omitempty"`
// PressureMSL represents the pressure at station level (QFE) in hPa
PressureQFE *APIFloat `json:"pressure"`
// TemperatureMax represents the maximum temperature in °C
TemperatureMax *APIFloat `json:"tempMax,omitempty"`
// TemperatureMean represents the mean temperature in °C
@ -249,6 +249,25 @@ func (cw CurrentWeather) PressureQFE() Pressure {
return v
}
// SnowAmount returns the temperature data point as Density.
// If the data point is not available in the CurrentWeather it will return
// Density in which the "not available" field will be true.
func (cw CurrentWeather) SnowAmount() Density {
if cw.Data.SnowAmount == nil {
return Density{na: true}
}
v := Density{
dt: cw.Data.SnowAmount.DateTime,
n: FieldSnowAmount,
s: SourceUnknown,
fv: cw.Data.SnowAmount.Value,
}
if cw.Data.SnowAmount.Source != nil {
v.s = StringToSource(*cw.Data.SnowAmount.Source)
}
return v
}
// Temperature returns the temperature data point as Temperature.
// If the data point is not available in the CurrentWeather it will return
// Temperature in which the "not available" field will be true.

View file

@ -565,6 +565,67 @@ func TestClient_CurrentWeatherByLocation_PressureQFE(t *testing.T) {
}
}
func TestClient_CurrentWeatherByLocation_SnowAmount(t *testing.T) {
tt := []struct {
// Location name
loc string
// CurWeather pressure
d *Density
}{
{"Ehrenfeld, Germany", &Density{
dt: time.Date(2023, 5, 23, 6, 0, 0, 0, time.UTC),
s: SourceAnalysis,
fv: 0,
}},
{"Berlin, Germany", &Density{
dt: time.Date(2023, 5, 23, 8, 0, 0, 0, time.UTC),
s: SourceAnalysis,
fv: 21.1,
}},
{"Neermoor, Germany", nil},
}
c := New(withMockAPI())
if c == nil {
t.Errorf("failed to create new Client, got nil")
return
}
for _, tc := range tt {
t.Run(tc.loc, func(t *testing.T) {
cw, err := c.CurrentWeatherByLocation(tc.loc)
if err != nil {
t.Errorf("CurrentWeatherByLocation failed: %s", err)
return
}
if tc.d != nil && tc.d.String() != cw.SnowAmount().String() {
t.Errorf("CurrentWeatherByLocation failed, expected snow amount "+
"string: %s, got: %s", tc.d.String(), cw.SnowAmount())
}
if tc.d != nil && tc.d.Value() != cw.SnowAmount().Value() {
t.Errorf("CurrentWeatherByLocation failed, expected snow amount "+
"float: %f, got: %f", tc.d.Value(), cw.SnowAmount().Value())
}
if tc.d != nil && cw.SnowAmount().Source() != tc.d.s {
t.Errorf("CurrentWeatherByLocation failed, expected source: %s, but got: %s",
tc.d.s, cw.SnowAmount().Source())
}
if tc.d != nil && tc.d.dt.Unix() != cw.SnowAmount().DateTime().Unix() {
t.Errorf("CurrentWeatherByLocation failed, expected datetime: %s, got: %s",
tc.d.dt.Format(time.RFC3339), cw.SnowAmount().DateTime().Format(time.RFC3339))
}
if tc.d == nil {
if cw.SnowAmount().IsAvailable() {
t.Errorf("CurrentWeatherByLocation failed, expected snow amount "+
"to have no data, but got: %s", cw.SnowAmount())
}
if !math.IsNaN(cw.SnowAmount().Value()) {
t.Errorf("CurrentWeatherByLocation failed, expected snow amount "+
"to return NaN, but got: %s", cw.SnowAmount().String())
}
}
})
}
}
func TestClient_CurrentWeatherByLocation_Temperature(t *testing.T) {
tt := []struct {
// Location name

View file

@ -44,6 +44,8 @@ const (
FieldPressureMSL
// FieldPressureQFE represents the PressureQFE data point
FieldPressureQFE
// FieldSnowAmount represents the SnowAmount data point
FieldSnowAmount
// FieldSunrise represents the Sunrise data point
FieldSunrise
// FieldSunset represents the Sunset data point

48
density.go Normal file
View file

@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package meteologix
import (
"fmt"
"math"
"time"
)
// Density is a type wrapper of an WeatherData for holding density
// values in WeatherData
type Density WeatherData
// IsAvailable returns true if an Density value was
// available at time of query
func (d Density) IsAvailable() bool {
return !d.na
}
// DateTime returns true if an Density value was
// available at time of query
func (d Density) DateTime() time.Time {
return d.dt
}
// String satisfies the fmt.Stringer interface for the Density type
func (d Density) String() string {
return fmt.Sprintf("%.1fkg/m³", d.fv)
}
// Source returns the Source of Density
// If the Source is not available it will return SourceUnknown
func (d Density) Source() Source {
return d.s
}
// Value returns the float64 value of an Density
// If the Density is not available in the Observation
// Vaule will return math.NaN instead.
func (d Density) Value() float64 {
if d.na {
return math.NaN()
}
return d.fv
}