-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate average value for a geo-filter
- Loading branch information
1 parent
64b1962
commit cdd410b
Showing
6 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package griblib | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
//AverageValue takes a geofilter and calculates the average value within that area | ||
func AverageValueBasic(filter GeoFilter, grid0 *Grid0, data []float64) (float64, error) { | ||
startNi, stopNi, startNj, stopNj := StartStopIndexes(filter, *grid0) | ||
|
||
numberOfDataPoints := (stopNi - startNi) * (stopNj - startNj) | ||
value := 0.0 | ||
|
||
for j := startNj; j < stopNj; j++ { | ||
for i := startNi; i < stopNi; i++ { | ||
value += data[j*grid0.Nj+i] | ||
} | ||
} | ||
return value / float64(numberOfDataPoints), nil | ||
|
||
} | ||
func AverageValue(filter GeoFilter, message *Message) (float64, error) { | ||
grid0, ok := message.Section3.Definition.(*Grid0) | ||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package gribtest | ||
|
||
import ( | ||
"github.com/nilsmagnus/grib/griblib" | ||
"testing" | ||
) | ||
|
||
func TestCalculateAverageValue_0_values(t *testing.T) { | ||
|
||
filter := griblib.GeoFilter{MinLong: 10_000_000, MinLat: 10_000_000, MaxLat: 20_000_000, MaxLong: 20_000_000} | ||
grid := griblib.Grid0{Di: 2_500_000, Dj: 2_500_000} | ||
data := make([]float64, 100) | ||
|
||
calculatedValue, err := griblib.AverageValueBasic(filter, &grid, data) | ||
|
||
if err != nil { | ||
t.Fatalf("Error calculating value: %v", err) | ||
} | ||
|
||
if calculatedValue != 0.0 { | ||
t.Errorf("Average value should have been 0.0, was %f", calculatedValue) | ||
} | ||
} | ||
|
||
func TestCalculateAverageValue_incrementing_values(t *testing.T) { | ||
|
||
filter := griblib.GeoFilter{MinLong: 10_000_000, MinLat: 85_000_000, MaxLat: 70_000_000, MaxLong: 15_000_000} | ||
|
||
grid := griblib.Grid0{Di: 2_500_000, Dj: 2_500_000, Lo1: 0, Lo2: 357_500_000, La1: 90_000_000, La2: -2057483648, Ni: 144, Nj: 73} | ||
data := make([]float64, grid.Ni*grid.Nj) | ||
|
||
for i := 0; i < int(grid.Ni); i++ { | ||
for j := 0; j < int(grid.Nj); j++ { | ||
index := i*int(grid.Nj) + j | ||
data[index] = float64(index) | ||
} | ||
} | ||
/* | ||
[]data is now | ||
0 1 2 3 4 5 6 7 8 9 ... | ||
10 11 12 13 14 15 16 17 18 19 ... | ||
20 21 22 23' 24' 25 26 27 28 29 ... | ||
30 31 32 33' 34' 35 36 37 38 39 ... | ||
40 41 42 43 44 45 46 47 48 49 ... | ||
50 51 52 53 54 55 56 57 58 59 ... | ||
60 61 62 63 64 65 66 67 68 69 ... | ||
70 71 72 73 74 75 76 77 78 79 ... | ||
80 81 82 83 84 85 86 87 88 89 ... | ||
90 91 92 93 94 95 96 97 98 99 ... | ||
... | ||
*/ | ||
|
||
calculatedValue, err := griblib.AverageValueBasic(filter, &grid, data) | ||
|
||
if err != nil { | ||
t.Fatalf("Error calculating value: %v", err) | ||
} | ||
|
||
if calculatedValue != 333 { | ||
t.Errorf("Average value should have been 333, was %f", calculatedValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters