From 486688e72a178430afe854f9cacb1fb7a44936d3 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sat, 26 Aug 2023 10:22:42 -0700 Subject: [PATCH] Fix U.S. PROB group handling and trim RMK from remark --- internal/parser/lexer.go | 2 +- internal/parser/parser.go | 4 ++-- taf.go | 29 +++-------------------------- 3 files changed, 6 insertions(+), 29 deletions(-) diff --git a/internal/parser/lexer.go b/internal/parser/lexer.go index 5da8719..7ae7e3b 100644 --- a/internal/parser/lexer.go +++ b/internal/parser/lexer.go @@ -6,7 +6,7 @@ import ( var lex = lexer.MustSimple([]lexer.SimpleRule{ {Name: "header", Pattern: `TAF (AMD|COR)? ?`}, - {Name: "Remark", Pattern: `RMK .*`}, + {Name: "Remark", Pattern: `RMK[^\n]*`}, {Name: "Number", Pattern: `\d+`}, {Name: "Modifier", Pattern: `[+-]|VC`}, {Name: "Prob", Pattern: "PROB"}, diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 73fcdb4..575e3e4 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -34,8 +34,8 @@ type ValidPair struct { type Probability struct { Pos lexer.Position - Value string `Prob @Number` - Time string `(WS @Number)?` + Value string `Prob @Number` + Valid ValidPair `(WS @@)?` } type WindSpeed struct { diff --git a/taf.go b/taf.go index 73098d0..2b7dea5 100644 --- a/taf.go +++ b/taf.go @@ -322,7 +322,7 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { case item.Probability != nil: // If the time is empty, this probability belongs to the // next change. - if item.Probability.Time == "" { + if item.Probability.Valid.Start == "" { prob, err := strconv.Atoi(item.Probability.Value) if err != nil { return nil, participle.Errorf(item.Probability.Pos, "prob: %s", err) @@ -332,34 +332,11 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { } else { pr := &Probability{} - // The probability time string must have 4 characters, - // 2 for starting time and 2 for ending time - if len(item.Probability.Time) < 4 { - return nil, participle.Errorf(item.Probability.Pos, "prob: invalid time %q", item.Probability.Time) - } - - startStr := item.Probability.Time[:2] - endStr := item.Probability.Time[2:] - - start, err := strconv.Atoi(startStr) + pr.Valid, err = parseValid(&item.Probability.Valid, opts.Month, opts.Year) if err != nil { return nil, participle.Errorf(item.Probability.Pos, "prob: %s", err) } - end, err := strconv.Atoi(endStr) - if err != nil { - return nil, participle.Errorf(item.Probability.Pos, "prob: %s", err) - } - - t := fc.PublishTime - pr.Valid = ValidPair{ - From: time.Date(t.Year(), t.Month(), t.Day(), start, 0, 0, 0, time.UTC), - To: time.Date(t.Year(), t.Month(), t.Day(), end, 0, 0, 0, time.UTC), - } - - // Get the duration by subtracting the from time from the to time - pr.Valid.Duration = pr.Valid.To.Sub(pr.Valid.From) - fc.Probabilities = append(fc.Probabilities, pr) // Set out to the probability value so that future mutations @@ -367,7 +344,7 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { out = reflect.ValueOf(pr).Elem() } case item.Remark != nil: - fc.Remark = *item.Remark + fc.Remark = strings.TrimSpace(strings.TrimPrefix(*item.Remark, "RMK")) } }