Skip to content

Commit

Permalink
make all fields json exportable
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Krivosheev committed May 28, 2023
1 parent 326510c commit a4bbed4
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 56 deletions.
18 changes: 9 additions & 9 deletions clouds/clouds.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

// Cloud - cloud representation
type Cloud struct {
Type CloudType
Type CloudType `json:"cloud_type"`
// the height is stored in hundreds of feet, as Flight Level
height int
HeightNotDefined bool
Cumulonimbus bool
ToweringCumulus bool
CBNotDefined bool
Height int `json:"height"`
HeightNotDefined bool `json:"height_not_defined"`
Cumulonimbus bool `json:"cumulonimbus"`
ToweringCumulus bool `json:"towering_cumulus"`
CBNotDefined bool `json:"cb_not_defined"`
}

// Clouds - an array of heights and types of clouds
Expand Down Expand Up @@ -50,12 +50,12 @@ const (

// HeightM - returns height above surface of the lower base of cloudiness in meters
func (cl Cloud) HeightM() int {
return cnv.FtToM(cl.height * 100)
return cnv.FtToM(cl.Height * 100)
}

// HeightFt - returns height above surface of the lower base of cloudiness in feet
func (cl Cloud) HeightFt() int {
return cl.height * 100
return cl.Height * 100
}

// ParseCloud - identify and parses the representation of cloudiness in the string
Expand All @@ -74,7 +74,7 @@ func ParseCloud(token string) (cl Cloud, ok bool) {
}

if matches[2] != "///" && matches[2] != "" {
cl.height, _ = strconv.Atoi(matches[2])
cl.Height, _ = strconv.Atoi(matches[2])
} else {
cl.HeightNotDefined = true
}
Expand Down
4 changes: 2 additions & 2 deletions clouds/clouds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func TestParseCloud(t *testing.T) {
Convey("height in feet must calculated correctly", func() {
for _, pair := range tests {
cloud, _ := ParseCloud(pair.input)
So(cloud.HeightFt(), ShouldEqual, pair.expected.height*100)
So(cloud.HeightFt(), ShouldEqual, pair.expected.Height*100)
}
})

Convey("height in meters must calculated correctly", func() {
for _, pair := range tests {
cloud, _ := ParseCloud(pair.input)
So(cloud.HeightM(), ShouldEqual, FtToM(pair.expected.height*100))
So(cloud.HeightM(), ShouldEqual, FtToM(pair.expected.Height*100))
}
})

Expand Down
6 changes: 3 additions & 3 deletions phenomena/phenomena.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const (
// Phenomenon - Weather phenomenon with intensity/proximity indicator
type Phenomenon struct {
// proximity indicator
Vicinity bool
Intensity Intensity
Abbreviation string
Vicinity bool `json:"vicinity"`
Intensity Intensity `json:"intensity"`
Abbreviation string `json:"abbreviation"`
}

// Phenomena - List of current or past weather events
Expand Down
22 changes: 14 additions & 8 deletions trend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metar

import (
"fmt"
"log"
"regexp"
"strings"
Expand All @@ -26,18 +27,22 @@ const (

// Trend - forecast of changes for a specified period
type Trend struct {
Type TypeTrend
Probability int // used only in TAFs. Maybe only 30 or 40. The PROBdd group is not used in conjunction with BECMG and FM
Type TypeTrend `json:"type_trend"`
Probability int `json:"probability"`
// used only in TAFs. Maybe only 30 or 40. The PROBdd group is not used in conjunction with BECMG and FM
// In case of in metar use values indicated time of changes. hh:mm (BECMG FM1030 TL1130)
// In TAFs used from - until fields as date/time. ddhh/ddhh (TEMPO 2208/2218)
FM time.Time // FroM (time)
TL time.Time // unTiL (time)
AT time.Time // AT time
FM time.Time `json:"from_time"`
// FroM (time)
TL time.Time `json:"till_time"`
// unTiL (time)
AT time.Time `json:"at_time"`
// AT time
v.Visibility
VerticalVisibility int
VerticalVisibilityNotDefined bool
VerticalVisibility int `json:"vertical_visibility"`
VerticalVisibilityNotDefined bool `json:"vertical_visibility_not_defined"`
wind.Wind
CAVOK bool
CAVOK bool `json:"cavok"`
ph.Phenomena
clouds.Clouds
}
Expand Down Expand Up @@ -127,6 +132,7 @@ func (trend *Trend) setProbability(input string) bool {
}

func (trend *Trend) setPeriodOfChanges(input string) (ok bool) {
fmt.Println(input)
switch input[0:2] {
case "AT":
timeofaction, err := time.Parse("200601021504", CurYearStr+CurMonthStr+CurDayStr+input[2:])
Expand Down
14 changes: 8 additions & 6 deletions visibility/visibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,27 @@ const (
// Distance in units of measure
type Distance struct {
// By default, meters. Or feet in US RVR. Both integer
Value int
Value int `json:"value"`
// Used only for horizontal visibility in miles
FractionValue float64
Unit
FractionValue float64 `json:"fraction_value"`
Unit `json:"unit"`
}

// BaseVisibility - the basis of visibility: one measurement
type BaseVisibility struct {
Distance
AboveMax bool // more than reported value (P5000, P6SM...)
BelowMin bool // less than reported value (M1/4SM, M0050...)
// more than reported value (P5000, P6SM...)
AboveMax bool `json:"above_max"`
// less than reported value (M1/4SM, M0050...)
BelowMin bool `json:"below_min"`
}

// Visibility - prevailing visibility
type Visibility struct {
BaseVisibility
// sector visibility
LowerDistance Distance
LowerDirection string
LowerDirection string `json:"lower_direction"`
}

// Meters - returns the distance in meters
Expand Down
32 changes: 16 additions & 16 deletions wind/wind.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,36 @@ const (

// Wind - wind on surface representation
type Wind struct {
WindDirection int
speed int
gustsSpeed int
Variable bool
VariableFrom int
VariableTo int
Above50MPS bool
SpeedNotDefined bool
DirectionNotDefined bool
unit speedUnit
WindDirection int `json:"wind_direction"`
Speed int `json:"speed"`
GustsSpeed int `json:"gusts_speed"`
Variable bool `json:"variable"`
VariableFrom int `json:"variable_from"`
VariableTo int `json:"variable_to"`
Above50MPS bool `json:"above50mps"`
SpeedNotDefined bool `json:"speed_not_defined"`
DirectionNotDefined bool `json:"direction_not_defined"`
unit speedUnit `json:"unit"`
}

// SpeedKt - returns wind speed in knots. In Russian messages, the speed is specified in m/s, but it makes sense to receive it in knots for aviation purposes
func (w *Wind) SpeedKt() (speed int) {
return getSpeedValue(w.speed, w.unit, KT)
return getSpeedValue(w.Speed, w.unit, KT)
}

// SpeedMps - returns wind speed in meters per second.
func (w *Wind) SpeedMps() (speed int) {
return getSpeedValue(w.speed, w.unit, MPS)
return getSpeedValue(w.Speed, w.unit, MPS)
}

// GustsSpeedKt - returns gusts speed in knots.
func (w *Wind) GustsSpeedKt() (speed int) {
return getSpeedValue(w.gustsSpeed, w.unit, KT)
return getSpeedValue(w.GustsSpeed, w.unit, KT)
}

// GustsSpeedMps - returns gusts speed in meters per second.
func (w *Wind) GustsSpeedMps() (speed int) {
return getSpeedValue(w.gustsSpeed, w.unit, MPS)
return getSpeedValue(w.GustsSpeed, w.unit, MPS)
}

func getSpeedValue(speed int, unit speedUnit, needUnit speedUnit) (result int) {
Expand Down Expand Up @@ -91,10 +91,10 @@ func (w *Wind) ParseWind(token string) (tokensused int) {
w.Above50MPS = matches[2] != ""
w.SpeedNotDefined = matches[3] == "//"
if matches[3] != "" && !w.SpeedNotDefined {
w.speed, _ = strconv.Atoi(matches[3])
w.Speed, _ = strconv.Atoi(matches[3])
}
if matches[4] != "" {
w.gustsSpeed, _ = strconv.Atoi(matches[4][1:])
w.GustsSpeed, _ = strconv.Atoi(matches[4][1:])
}
w.unit = speedUnit(matches[5])
// Two tokens have been used
Expand Down
24 changes: 12 additions & 12 deletions wind/wind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func TestParseWind(t *testing.T) {
wnd.ParseWind(pair.input)
switch wnd.unit {
case KPH, KMH:
So(wnd.SpeedMps(), ShouldAlmostEqual, KphToMps(wnd.speed), .5)
So(wnd.GustsSpeedMps(), ShouldAlmostEqual, KphToMps(wnd.gustsSpeed), .5)
So(wnd.SpeedMps(), ShouldAlmostEqual, KphToMps(wnd.Speed), .5)
So(wnd.GustsSpeedMps(), ShouldAlmostEqual, KphToMps(wnd.GustsSpeed), .5)
case KT:
So(wnd.SpeedMps(), ShouldAlmostEqual, KtsToMps(float64(wnd.speed)), .5)
So(wnd.GustsSpeedMps(), ShouldAlmostEqual, KtsToMps(float64(wnd.gustsSpeed)), .5)
So(wnd.SpeedMps(), ShouldAlmostEqual, KtsToMps(float64(wnd.Speed)), .5)
So(wnd.GustsSpeedMps(), ShouldAlmostEqual, KtsToMps(float64(wnd.GustsSpeed)), .5)
case MPS:
So(wnd.SpeedMps(), ShouldEqual, wnd.speed)
So(wnd.GustsSpeedMps(), ShouldEqual, wnd.gustsSpeed)
So(wnd.SpeedMps(), ShouldEqual, wnd.Speed)
So(wnd.GustsSpeedMps(), ShouldEqual, wnd.GustsSpeed)
}
}
})
Expand All @@ -64,14 +64,14 @@ func TestParseWind(t *testing.T) {
wnd.ParseWind(pair.input)
switch wnd.unit {
case MPS:
So(wnd.SpeedKt(), ShouldAlmostEqual, MpsToKts(float64(wnd.speed)), .5)
So(wnd.GustsSpeedKt(), ShouldAlmostEqual, MpsToKts(float64(wnd.gustsSpeed)), .5)
So(wnd.SpeedKt(), ShouldAlmostEqual, MpsToKts(float64(wnd.Speed)), .5)
So(wnd.GustsSpeedKt(), ShouldAlmostEqual, MpsToKts(float64(wnd.GustsSpeed)), .5)
case KPH, KMH:
So(wnd.SpeedKt(), ShouldAlmostEqual, KphToKts(wnd.speed), .5)
So(wnd.GustsSpeedKt(), ShouldAlmostEqual, KphToKts(wnd.gustsSpeed), .5)
So(wnd.SpeedKt(), ShouldAlmostEqual, KphToKts(wnd.Speed), .5)
So(wnd.GustsSpeedKt(), ShouldAlmostEqual, KphToKts(wnd.GustsSpeed), .5)
case KT:
So(wnd.SpeedKt(), ShouldEqual, wnd.speed)
So(wnd.GustsSpeedKt(), ShouldEqual, wnd.gustsSpeed)
So(wnd.SpeedKt(), ShouldEqual, wnd.Speed)
So(wnd.GustsSpeedKt(), ShouldEqual, wnd.GustsSpeed)
}

}
Expand Down

0 comments on commit a4bbed4

Please sign in to comment.