Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some lint #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions griblib/bitgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func (bitGroup *bitGroupParameter) zeroGroup() []int64 {
func (bitGroup *bitGroupParameter) readData(bitReader *reader.BitReader) ([]int64, error) {
var err error
if bitGroup.Width != 0 {
uintArray, err := bitReader.ReadUintsBlock(int(bitGroup.Width), int64(bitGroup.Length), false)
uintArray, err2 := bitReader.ReadUintsBlock(int(bitGroup.Width), int64(bitGroup.Length), false)
output := make([]int64, len(uintArray))
for idx, val := range uintArray {
output[idx] = int64(val)
}

return output, err
return output, err2
}

return bitGroup.zeroGroup(), err
Expand All @@ -43,7 +43,7 @@ func checkLengths(bitGroups []bitGroupParameter, dataLength int) error {
}

if totBit/8 > int(dataLength) {
return fmt.Errorf("Checksum err %d - %d", dataLength, totBit/8)
return fmt.Errorf("checksum err %d - %d", dataLength, totBit/8)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions griblib/calculations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

// AverageValueBasic takes a GeoFilter, Grid0 and data to calculate the average value within that area.
// AverageValueBasic takes a GeoFilter, Grid0 and data to calculate the average value within that area.
// See GeoFilter for how to define an area
func AverageValueBasic(filter GeoFilter, grid0 *Grid0, data []float64) (float64, error) {
startNi, stopNi, startNj, stopNj := StartStopIndexes(filter, *grid0)
Expand All @@ -23,9 +23,9 @@ func AverageValueBasic(filter GeoFilter, grid0 *Grid0, data []float64) (float64,

func AverageValue(filter GeoFilter, message *Message) (float64, error) {
grid0, ok := message.Section3.Definition.(*Grid0)
data := message.Section7.Data
data := message.Section7.Data
if ok {
return AverageValueBasic(filter, grid0, data)
}
return -1, fmt.Errorf("grid not of wanted type (wanted Grid0), was %v.", reflect.TypeOf(message.Section3.Definition))
return -1, fmt.Errorf("grid not of wanted type (wanted Grid0), was %v", reflect.TypeOf(message.Section3.Definition))
}
6 changes: 3 additions & 3 deletions griblib/data2.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,16 @@ func ParseData2(dataReader io.Reader, dataLength int, template *Data2) ([]float6
// Test to see if the group widths and lengths are consistent with number of
// values, and length of section 7.
//
if err := checkLengths(bitGroups, dataLength); err != nil {
return nil, err
if err2 := checkLengths(bitGroups, dataLength); err2 != nil {
return nil, err2
}

//
// For each group, unpack data values
//
section7Data, ifldmiss, err := template.extractData(bitReader, bitGroups)
if err != nil {
return nil, fmt.Errorf("Data extract: %s", err.Error())
return nil, fmt.Errorf("data extract: %s", err.Error())
}

return template.scaleValues(section7Data, ifldmiss), nil
Expand Down
16 changes: 8 additions & 8 deletions griblib/data3.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ func (template *Data3) extractSpacingDifferentialValues(bitReader *reader.BitRea
var err error
ival1, err = bitReader.ReadInt(rc)
if err != nil {
return minsd, ival1, ival2, fmt.Errorf("Spacial differencing Value 1: %s", err.Error())
return minsd, ival1, ival2, fmt.Errorf("spacial differencing Value 1: %s", err.Error())
}

if template.SpatialOrderDifference == 2 {
ival2, err = bitReader.ReadInt(rc)
if err != nil {
return minsd, ival1, ival2, fmt.Errorf("Spacial differencing Value 2: %s", err.Error())
return minsd, ival1, ival2, fmt.Errorf("spacial differencing Value 2: %s", err.Error())
}
}

minsd, err = bitReader.ReadInt(rc)
if err != nil {
return minsd, ival1, ival2, fmt.Errorf("Spacial differencing Reference: %s", err.Error())
return minsd, ival1, ival2, fmt.Errorf("spacial differencing Reference: %s", err.Error())
}
}

Expand All @@ -91,31 +91,31 @@ func ParseData3(dataReader io.Reader, dataLength int, template *Data3) ([]float6
//
minsd, ival1, ival2, err := template.extractSpacingDifferentialValues(bitReader)
if err != nil {
return nil, fmt.Errorf("Spacial differencing Value 1: %s", err.Error())
return nil, fmt.Errorf("spacial differencing Value 1: %s", err.Error())
}

//
// Extract Bit Group Parameters
//
bitGroups, err := template.extractBitGroupParameters(bitReader)
if err != nil {
return nil, fmt.Errorf("Groups: %s", err.Error())
return nil, fmt.Errorf("groups: %s", err.Error())
}

//
// Test to see if the group widths and lengths are consistent with number of
// values, and length of section 7.
//
if err := checkLengths(bitGroups, dataLength); err != nil {
return nil, fmt.Errorf("Check length: %s", err.Error())
if err2 := checkLengths(bitGroups, dataLength); err2 != nil {
return nil, fmt.Errorf("check length: %s", err2.Error())
}

//
// For each group, unpack data values
//
section7Data, ifldmiss, err := template.extractData(bitReader, bitGroups)
if err != nil {
return nil, fmt.Errorf("Data extract: %s", err.Error())
return nil, fmt.Errorf("data extract: %s", err.Error())
}

//
Expand Down
9 changes: 6 additions & 3 deletions griblib/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Export(messages []*Message, options Options) {
case ExportToPNG:
ExportMessagesAsPngs(messages)
default:
log.Printf("Error: Export type %d not supported. \n", options.ExportType)
log.Fatalf("Error: Export type %d not supported. \n", options.ExportType)
}
}

Expand Down Expand Up @@ -65,7 +65,10 @@ func export(m *Message) {
// json print
js, _ := json.Marshal(m)
var out bytes.Buffer
json.Compact(&out, js)
out.WriteTo(os.Stdout)
err := json.Compact(&out, js)
if err != nil {
log.Fatalf("Error compacting json: %v\n", err)
}
_, _ = out.WriteTo(os.Stdout)
log.Println("")
}
7 changes: 4 additions & 3 deletions griblib/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// note that latitude 90 is considered lesser than latitude 85 in calculations.
// an example for a valid filter is
//
// filter := griblib.GeoFilter{MinLong: 10_000_000, MinLat: 85_000_000, MaxLat: 70_000_000, MaxLong: 15_000_000}
// filter := griblib.GeoFilter{MinLong: 10_000_000, MinLat: 85_000_000, MaxLat: 70_000_000, MaxLong: 15_000_000}
//
// note than MinLat has higher integer value than MaxLat
type GeoFilter struct {
Expand All @@ -38,7 +38,9 @@ const (
func Filter(messages []*Message, options Options) []*Message {

filtered := make([]*Message, 0)

if !isEmpty(options.GeoFilter) && len(messages) > 0 {
log.Printf("Using GeoFilter %v\n", options.GeoFilter)
}
for _, message := range messages {
discipline := satisfiesDiscipline(options.Discipline, message)
category := satisfiesCategory(options.Category, message)
Expand All @@ -47,7 +49,6 @@ func Filter(messages []*Message, options Options) []*Message {
continue
}
if !isEmpty(options.GeoFilter) {
log.Printf("Using GeoFilter %v\n", options.GeoFilter)
if data, err := FilterValuesFromGeoFilter(message, options.GeoFilter); err == nil {
message.Section7.Data = *data
if grid0, ok := message.Section3.Definition.(*Grid0); ok {
Expand Down
14 changes: 7 additions & 7 deletions griblib/gribtest/data3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ func Test_read_integrationtest_file(t *testing.T) {
if surface.Type == 1 && // ground surface
m.Section0.Discipline == 0 && // meterology
m.Section4.ProductDefinitionTemplate.ParameterCategory == 0 { // temperature
var max float64 = 00
var min float64 = 1000
var maxTemp float64 = 00
var minTemp float64 = 1000
for _, kelvin := range m.Section7.Data {
if kelvin < 197 || kelvin > 350 {
t.Errorf("Got kelvin out of range: %f\n", kelvin)
}
if kelvin > max {
max = kelvin
if kelvin > maxTemp {
maxTemp = kelvin
}
if kelvin < min {
min = kelvin
if kelvin < minTemp {
minTemp = kelvin
}
}
log.Printf("category number %v,", m.Section4.ProductDefinitionTemplate.ParameterCategory)
log.Printf("parameter number %v,", m.Section4.ProductDefinitionTemplate.ParameterNumber)
log.Printf("surface type %v, surface value %v max: %f min: %f\n", surface.Type, surface.Value, max, min)
log.Printf("surface type %v, surface value %v maxTemp: %f minTemp: %f\n", surface.Type, surface.Value, maxTemp, minTemp)

}
}
Expand Down
5 changes: 4 additions & 1 deletion griblib/gribtest/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func BenchmarkReadMessages(b *testing.B) {
if err != nil {
b.Fatalf("Could not open test-file %v", err)
}
griblib.ReadMessages(f)
_, err = griblib.ReadMessages(f)
if err != nil {
b.Fatalf("Could not read messages %v", err)
}
}
}
22 changes: 15 additions & 7 deletions griblib/gribtest/pngExport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ import (
)

func beforeTests(t *testing.T) func(t *testing.T) {
os.MkdirAll("testoutput", os.ModePerm)
err := os.MkdirAll("testoutput", 0750)
if err != nil {
t.Errorf("Could not create testoutput directory: %v", err)
t.Fail()
}

return func(t *testing.T) {
os.RemoveAll("testoutput")
err2 := os.RemoveAll("testoutput")
if err2 != nil {
t.Errorf("Could not remove testoutput directory: %v", err2)
t.Fail()
}
}
}

Expand Down Expand Up @@ -49,12 +57,12 @@ func Test_message_to_png(t *testing.T) {
}

func Test_maxmin(t *testing.T) {
max, min := griblib.MaxMin([]float64{0, -154, 54, 64, -10})
if max != 64.0 {
t.Errorf("Expected max to be 64, was %f", max)
biggest, smallest := griblib.MaxMin([]float64{0, -154, 54, 64, -10})
if biggest != 64.0 {
t.Errorf("Expected biggest to be 64, was %f", biggest)
}
if min != -154.0 {
t.Errorf("Expected min to be -154, was %f", min)
if smallest != -154.0 {
t.Errorf("Expected smallest to be -154, was %f", smallest)
}
}

Expand Down
23 changes: 18 additions & 5 deletions griblib/gribtest/sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ func Test_read_section0(t *testing.T) {
Reserved: 6,
}

section0, readError := griblib.ReadSection0(toIoReader(testData))
ioReader, err := toIoReader(testData)
if err != nil {
t.Fatal(err)
}

section0, readError := griblib.ReadSection0(ioReader)

if readError != nil {
t.Fatal(readError)
Expand Down Expand Up @@ -49,25 +54,33 @@ func Test_read_section1(t *testing.T) {
Type: 8,
}

section1, readError := griblib.ReadSection1(toIoReader(testData), binary.Size(testData))
ioReader, err := toIoReader(testData)
if err != nil {
t.Fatal(err)
}

section1, readError := griblib.ReadSection1(ioReader, binary.Size(testData))

if readError != nil {
t.Fatal(readError)
}

if testData != section1 {
t.Error("Deserialized section1 struct is not equal to original struct")
t.Fail()
}
}

// create a reader from a struct for testing purposes
func toIoReader(data interface{}) (reader io.Reader) {
func toIoReader(data interface{}) (reader io.Reader, err error) {
var binBuf bytes.Buffer

binary.Write(&binBuf, binary.BigEndian, data)
err = binary.Write(&binBuf, binary.BigEndian, data)
if err != nil {
return
}

reader = bytes.NewReader(binBuf.Bytes())

return

}
Loading