Skip to content

Commit

Permalink
remove options for reading files. update docs and learn more about se…
Browse files Browse the repository at this point in the history
…ction4(surfaces)
  • Loading branch information
nilsmagnus committed Dec 13, 2017
1 parent c914464 commit 21b35ca
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 31 deletions.
6 changes: 1 addition & 5 deletions griblib/data_test.go
@@ -1,7 +1,6 @@
package griblib

import (
"math"
"os"
"testing"
)
Expand All @@ -12,10 +11,7 @@ func Test_read_integrationtest_file(t *testing.T) {
if fileOpenErr != nil {
t.Fatal("Grib file for integration tests not found")
}
messages, messageParseErr := ReadMessages(testFile, Options{
MaximumNumberOfMessages: math.MaxInt32,
Discipline: -1,
})
messages, messageParseErr := ReadMessages(testFile)

if messageParseErr != nil {
t.Fatal("Error reading messages: ", messageParseErr.Error())
Expand Down
3 changes: 2 additions & 1 deletion griblib/filters.go
Expand Up @@ -64,7 +64,8 @@ func filteredGrid(grid0 *Grid0, filter GeoFilter) *Grid0 {
}

func isEmpty(geoFilter GeoFilter) bool {
return geoFilter == GeoFilter{MinLong: LongitudeStart, MaxLong: LongitudeEnd, MinLat: LatitudeNorth, MaxLat: LatitudeSouth}
return geoFilter == GeoFilter{MinLong: LongitudeStart, MaxLong: LongitudeEnd, MinLat: LatitudeNorth, MaxLat: LatitudeSouth} ||
geoFilter == GeoFilter{}
}

func filterValuesFromGeoFilter(message Message, filter GeoFilter) (*[]int64, error) {
Expand Down
36 changes: 36 additions & 0 deletions griblib/filters_test.go
Expand Up @@ -2,6 +2,8 @@ package griblib

import (
"testing"
"os"
"fmt"
)

func Test_calculcate_startStopIndexes(t *testing.T) {
Expand Down Expand Up @@ -91,3 +93,37 @@ func Test_filter_on_category(t *testing.T) {
t.Error("should have filtered when option is different from message")
}
}

func Test_temperature_layers(t *testing.T) {
file, err := os.Open("integrationtestdata/gfs.t00z.pgrb2.2p50.f006")
if err != nil {
t.Fatal("Could not open testfile")
}
messages, msgErr := ReadMessages(file)

if msgErr != nil {
t.Fatal("Error reading messages from testfile")
}
if len(messages) != 77 {
t.Errorf("expected 77 messages, got %d\n", len(messages))
}

filtered := Filter(messages, Options{Discipline: 0 , Category: 0})

if len(filtered) != 11 {
t.Errorf("expected 11 messages, got %d\n", len(filtered))
}

fmt.Println("layers for temperature ")
for _, f := range filtered {
fmt.Printf("s1:%d\ts2:%d\tt1:%d\tt2:%d\tv1:%d(meter over sea-level?)\tv2:%d\n",
f.Section4.ProductDefinitionTemplate.FirstSurface.Scale,
f.Section4.ProductDefinitionTemplate.SecondSurface.Scale,
f.Section4.ProductDefinitionTemplate.FirstSurface.Type,
f.Section4.ProductDefinitionTemplate.SecondSurface.Type,
f.Section4.ProductDefinitionTemplate.FirstSurface.Value,
f.Section4.ProductDefinitionTemplate.SecondSurface.Value,
)
}

}
Binary file not shown.
6 changes: 3 additions & 3 deletions griblib/products.go
Expand Up @@ -75,9 +75,9 @@ type TimeRangeSpecification struct {
TimeIncrementBetweenSuccessiveField uint32 `json:"timeIncrementBetweenSuccessiveField"` // 55-58
}

//Surface describes a surface for a product
//Surface describes a surface for a product, see http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_table4-5.shtml
type Surface struct {
Type uint8 `json:"type"`
Type uint8 `json:"type"` // type 220: Planetary Boundary Layer
Scale uint8 `json:"scale"`
Value uint32 `json:"value"`
Value uint32 `json:"value"` // e.g. meters above sea-level
}
33 changes: 12 additions & 21 deletions griblib/sections.go
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

//Message is poorly documented other than code
//Message is the entire message for a data-layer
type Message struct {
Section0 Section0
Section1 Section1
Expand All @@ -20,17 +20,17 @@ type Message struct {
Section7 Section7
}

//Options is poorly documented other than code
//Options is used to filter messages.
type Options struct {
Operation string `json:"operation"`
Discipline int `json:"discipline"`
Discipline int `json:"discipline"` // -1 means all disciplines
DataExport bool `json:"dataExport"`
Category int `json:"category"`
Category int `json:"category"` // -1 means all categories
Filepath string `json:"filePath"`
ReduceFilePath string `json:"reduceFilePath"`
ExportType int `json:"exportType"`
MaximumNumberOfMessages int `json:"maximumNumberOfMessages"`
GeoFilter GeoFilter `json:"geoFilter"`
GeoFilter GeoFilter `json:"geoFilter"` // empty filter , GeoFilter{}, means no filter
}

const (
Expand All @@ -43,7 +43,7 @@ const (
)

//ReadMessages reads all message from gribFile
func ReadMessages(gribFile io.Reader, options Options) (messages []Message, err error) {
func ReadMessages(gribFile io.Reader) (messages []Message, err error) {

for {
message, messageErr := ReadMessage(gribFile)
Expand All @@ -55,13 +55,10 @@ func ReadMessages(gribFile io.Reader, options Options) (messages []Message, err
return messages, err
}
messages = append(messages, message)
if options.MaximumNumberOfMessages > 0 && len(messages) >= int(options.MaximumNumberOfMessages) {
return messages, nil
}
}
}

//ReadMessage is poorly documented other than code
//ReadMessage reads the actual messages from a gribfile-reader (io.Reader from either file, http or any other io.Reader)
func ReadMessage(gribFile io.Reader) (message Message, err error) {

section0, headError := ReadSection0(gribFile)
Expand All @@ -88,7 +85,6 @@ func ReadMessage(gribFile io.Reader) (message Message, err error) {

}

//readMessage is poorly documented other than code
func readMessage(gribFile io.Reader, section0 Section0) (message Message, err error) {

message.Section0 = section0
Expand Down Expand Up @@ -129,7 +125,7 @@ func readMessage(gribFile io.Reader, section0 Section0) (message Message, err er
}
}

//Section0 is poorly documented other than code
//Section0 is the indicator section http://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_sect0.shtml
type Section0 struct {
Indicator uint32 `json:"indicator"`
Reserved uint16 `json:"reserved"`
Expand All @@ -138,7 +134,7 @@ type Section0 struct {
MessageLength uint64 `json:"messageLength"`
}

//ReadSection0 is poorly documented other than code
//ReadSection0 reads Section0 from an io.reader
func ReadSection0(reader io.Reader) (section0 Section0, err error) {
section0.Indicator = 255
err = binary.Read(reader, binary.BigEndian, &section0)
Expand All @@ -156,13 +152,8 @@ func ReadSection0(reader io.Reader) (section0 Section0, err error) {

}

//Section is poorly documented other than code
type Section interface {
SectionNumber() uint8
String() string
}

//SectionHead is poorly documented other than code
//SectionHead is the common header for each section1-8
type SectionHead struct {
ByteLength uint32 `json:"byteLength"`
Number uint8 `json:"number"`
Expand Down Expand Up @@ -275,7 +266,7 @@ func ReadSection3(f io.Reader) (section Section3, err error) {
type Section4 struct {
CoordinatesCount uint16 `json:"coordinatesCount"`
ProductDefinitionTemplateNumber uint16 `json:"productDefinitionTemplateNumber"`
ProductDefinitionTemplate Product0 `json:"productDefinitionTemplate"` // FIXME
ProductDefinitionTemplate Product0 `json:"productDefinitionTemplate"` // FIXME, support more products
Coordinates []byte `json:"coordinates"`
}

Expand Down Expand Up @@ -306,7 +297,7 @@ func ReadSection4(f io.Reader) (section Section4, err error) {
type Section5 struct {
PointsNumber uint32 `json:"pointsNumber"`
DataTemplateNumber uint16 `json:"dataTemplateNumber"`
DataTemplate Data3 `json:"dataTemplate"` // FIXME
DataTemplate Data3 `json:"dataTemplate"` // FIXME, support more data-types
}

//ReadSection5 is poorly documented other than code
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -112,7 +112,7 @@ func reduceToFile(gribFile io.Reader, options griblib.Options) {
}

func parse(gribFile io.Reader, options griblib.Options) {
messages, err := griblib.ReadMessages(gribFile, options)
messages, err := griblib.ReadMessages(gribFile)

if err != nil {
fmt.Printf("Error reading all messages in gribfile: %s", err.Error())
Expand Down

0 comments on commit 21b35ca

Please sign in to comment.