openweather: refactored the OpenWeather source files to relocate normalization logic to internal/normalizers.

This commit is contained in:
2026-01-14 11:59:17 -06:00
parent 0ba2602bcc
commit 759fa31762
5 changed files with 469 additions and 330 deletions

View File

@@ -0,0 +1,206 @@
// FILE: ./internal/normalizers/openweather/observation.go
package openweather
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"gitea.maximumdirect.net/ejr/feedkit/event"
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/model"
normcommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/common"
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
)
// ObservationNormalizer converts:
//
// standards.SchemaRawOpenWeatherCurrentV1 -> standards.SchemaWeatherObservationV1
//
// It interprets OpenWeatherMap "current weather" JSON and maps it into the canonical
// model.WeatherObservation representation.
//
// Caveats / assumptions:
// - Unit system: this normalizer assumes the upstream request used `units=metric`.
// The OpenWeather source enforces this invariant (fails fast otherwise).
// That means:
// - main.temp is °C
// - wind.speed and wind.gust are m/s (we convert to km/h)
// - pressure fields are hPa (we convert to Pa)
//
// Day/night handling:
// - Prefer the OpenWeather icon suffix ("d" / "n") when available.
// - Otherwise fall back to sunrise/sunset bounds (unix seconds).
type ObservationNormalizer struct{}
func (ObservationNormalizer) Match(e event.Event) bool {
return strings.TrimSpace(e.Schema) == standards.SchemaRawOpenWeatherCurrentV1
}
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
rawBytes, err := normcommon.PayloadBytes(in)
if err != nil {
return nil, fmt.Errorf("openweather observation normalize: %w", err)
}
var parsed owmResponse
if err := json.Unmarshal(rawBytes, &parsed); err != nil {
return nil, fmt.Errorf("openweather observation normalize: decode raw payload: %w", err)
}
obs, effectiveAt, err := buildObservation(parsed)
if err != nil {
return nil, err
}
out := in
out.Schema = standards.SchemaWeatherObservationV1
out.Payload = obs
// EffectiveAt is optional; for observations it is naturally the observation timestamp.
if !effectiveAt.IsZero() {
t := effectiveAt.UTC()
out.EffectiveAt = &t
}
if err := out.Validate(); err != nil {
return nil, err
}
return &out, nil
}
// buildObservation contains the domain mapping logic (provider -> canonical model).
func buildObservation(parsed owmResponse) (model.WeatherObservation, time.Time, error) {
// Timestamp: dt is unix seconds, UTC (OpenWeather contract).
var ts time.Time
if parsed.Dt > 0 {
ts = time.Unix(parsed.Dt, 0).UTC()
}
// Primary weather condition: OpenWeather returns an array; treat [0] as primary.
owmID, rawDesc, icon := primaryCondition(parsed.Weather)
// Day/night inference:
// 1) icon suffix "d" or "n"
// 2) sunrise/sunset bounds
isDay := inferIsDay(icon, parsed.Dt, parsed.Sys.Sunrise, parsed.Sys.Sunset)
// Unit policy: metric is enforced by the source, so:
// - temp is already °C
// - wind speed is m/s -> km/h conversion
tempC := parsed.Main.Temp
rh := parsed.Main.Humidity
surfacePa := normcommon.PressurePaFromHPa(parsed.Main.Pressure)
var seaLevelPa *float64
if parsed.Main.SeaLevel != nil {
v := normcommon.PressurePaFromHPa(*parsed.Main.SeaLevel)
seaLevelPa = &v
}
wsKmh := normcommon.SpeedKmhFromMps(parsed.Wind.Speed)
var wgKmh *float64
if parsed.Wind.Gust != nil {
v := normcommon.SpeedKmhFromMps(*parsed.Wind.Gust)
wgKmh = &v
}
var visM *float64
if parsed.Visibility != nil {
v := *parsed.Visibility
visM = &v
}
// Condition mapping: OpenWeather condition IDs -> canonical WMO code vocabulary.
wmo := mapOpenWeatherToWMO(owmID)
canonicalText := standards.WMOText(wmo, isDay)
iconURL := openWeatherIconURL(icon)
stationID := openWeatherStationID(parsed)
stationName := strings.TrimSpace(parsed.Name)
if stationName == "" {
stationName = "OpenWeatherMap"
}
obs := model.WeatherObservation{
StationID: stationID,
StationName: stationName,
Timestamp: ts,
ConditionCode: wmo,
ConditionText: canonicalText,
IsDay: isDay,
ProviderRawDescription: rawDesc,
// Human-facing legacy fields: populate with canonical text for consistency.
TextDescription: canonicalText,
IconURL: iconURL,
TemperatureC: &tempC,
WindDirectionDegrees: parsed.Wind.Deg,
WindSpeedKmh: &wsKmh,
WindGustKmh: wgKmh,
BarometricPressurePa: &surfacePa,
SeaLevelPressurePa: seaLevelPa,
VisibilityMeters: visM,
RelativeHumidityPercent: &rh,
}
return obs, ts, nil
}
func primaryCondition(list []owmWeather) (id int, desc string, icon string) {
if len(list) == 0 {
return 0, "", ""
}
w := list[0]
return w.ID, strings.TrimSpace(w.Description), strings.TrimSpace(w.Icon)
}
func inferIsDay(icon string, dt, sunrise, sunset int64) *bool {
// Prefer icon suffix.
icon = strings.TrimSpace(icon)
if icon != "" {
last := icon[len(icon)-1]
switch last {
case 'd':
v := true
return &v
case 'n':
v := false
return &v
}
}
// Fall back to sunrise/sunset bounds if provided.
if dt > 0 && sunrise > 0 && sunset > 0 {
v := dt >= sunrise && dt < sunset
return &v
}
return nil
}
func openWeatherIconURL(icon string) string {
icon = strings.TrimSpace(icon)
if icon == "" {
return ""
}
return fmt.Sprintf("https://openweathermap.org/img/wn/%s@2x.png", icon)
}
func openWeatherStationID(parsed owmResponse) string {
if parsed.ID != 0 {
return fmt.Sprintf("OPENWEATHER(%d)", parsed.ID)
}
// Fallback: synthesize from coordinates.
return fmt.Sprintf("OPENWEATHER(%.5f,%.5f)", parsed.Coord.Lat, parsed.Coord.Lon)
}

View File

@@ -1,3 +1,4 @@
// FILE: ./internal/normalizers/openweather/register.go
package openweather
import (
@@ -5,15 +6,11 @@ import (
)
// Register registers OpenWeather normalizers into the provided registry.
//
// This is intentionally empty as a stub. As normalizers are implemented,
// register them here, e.g.:
//
// reg.Register(ObservationNormalizer{})
func Register(reg *fknormalize.Registry) {
if reg == nil {
return
}
// TODO: register OpenWeather normalizers here.
// Observations
reg.Register(ObservationNormalizer{})
}

View File

@@ -0,0 +1,51 @@
// FILE: ./internal/normalizers/openweather/types.go
package openweather
// owmResponse is a minimal-but-sufficient representation of the OpenWeatherMap
// "Current weather" payload needed for mapping into model.WeatherObservation.
//
// NOTE: OpenWeather field presence can vary by location/product tier.
// We keep some fields optional (pointers) where the API commonly omits them.
type owmResponse struct {
Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
} `json:"coord"`
Weather []owmWeather `json:"weather"`
Main struct {
Temp float64 `json:"temp"` // °C when units=metric (enforced by source)
Pressure float64 `json:"pressure"` // hPa
Humidity float64 `json:"humidity"` // %
SeaLevel *float64 `json:"sea_level"`
} `json:"main"`
Visibility *float64 `json:"visibility"` // meters (optional)
Wind struct {
Speed float64 `json:"speed"` // m/s when units=metric (enforced by source)
Deg *float64 `json:"deg"`
Gust *float64 `json:"gust"` // m/s when units=metric (enforced by source)
} `json:"wind"`
Dt int64 `json:"dt"` // unix seconds, UTC
Sys struct {
Country string `json:"country"`
Sunrise int64 `json:"sunrise"` // unix, UTC
Sunset int64 `json:"sunset"` // unix, UTC
} `json:"sys"`
Timezone int `json:"timezone"` // seconds offset from UTC
ID int64 `json:"id"` // city id
Name string `json:"name"` // city name
Cod int `json:"cod"`
}
type owmWeather struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"` // e.g. "04d" or "01n"
}

View File

@@ -0,0 +1,98 @@
// FILE: ./internal/normalizers/openweather/wmo_map.go
package openweather
import "gitea.maximumdirect.net/ejr/weatherfeeder/internal/model"
// mapOpenWeatherToWMO maps OpenWeather weather condition IDs into weatherfeeder's
// canonical WMO code vocabulary.
//
// This is an approximate semantic mapping between two different code systems.
// We map conservatively into the subset currently represented in standards.WMODescriptions.
func mapOpenWeatherToWMO(owmID int) model.WMOCode {
switch {
// 2xx Thunderstorm
case owmID >= 200 && owmID <= 232:
return 95
// 3xx Drizzle
case owmID >= 300 && owmID <= 321:
if owmID == 300 {
return 51
}
if owmID == 302 {
return 55
}
return 53
// 5xx Rain
case owmID >= 500 && owmID <= 531:
// 511 is "freezing rain"
if owmID == 511 {
return 67
}
// showers bucket (520-531)
if owmID >= 520 && owmID <= 531 {
if owmID == 520 {
return 80
}
if owmID == 522 {
return 82
}
return 81
}
// normal rain intensity
if owmID == 500 {
return 61
}
if owmID == 501 {
return 63
}
if owmID >= 502 && owmID <= 504 {
return 65
}
return 63
// 6xx Snow
case owmID >= 600 && owmID <= 622:
if owmID == 600 {
return 71
}
if owmID == 601 {
return 73
}
if owmID == 602 {
return 75
}
// Snow showers bucket (620-622)
if owmID == 620 {
return 85
}
if owmID == 621 || owmID == 622 {
return 86
}
return 73
// 7xx Atmosphere (mist/smoke/haze/dust/fog/etc.)
case owmID >= 701 && owmID <= 781:
return 45
// 800 Clear
case owmID == 800:
return 0
// 80x Clouds
case owmID == 801:
return 1
case owmID == 802:
return 2
case owmID == 803 || owmID == 804:
return 3
default:
return model.WMOUnknown
}
}