translate_test.go 5.94 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
package radolan

import (
Jonny Schäfer's avatar
Jonny Schäfer committed
4
5
6
	"bytes"
	"fmt"
	"io/ioutil"
Jonny Schäfer's avatar
Jonny Schäfer committed
7
	"math"
Jonny Schäfer's avatar
Jonny Schäfer committed
8
	"strconv"
Jonny Schäfer's avatar
Jonny Schäfer committed
9
10
11
	"testing"
)

Jonny Schäfer's avatar
Jonny Schäfer committed
12
13
14
15
16
17
18
19
20
func absequal(a, b, epsilon float64) bool {
	return math.Abs(a-b) < epsilon
}

func dist(x1, y1, x2, y2 float64) float64 {
	x := x1 - x2
	y := y1 - y2
	return math.Sqrt(x*x + y*y)
}
Jonny Schäfer's avatar
Jonny Schäfer committed
21

Jonny Schäfer's avatar
Jonny Schäfer committed
22
func TestResolution(t *testing.T) {
Jonny Schäfer's avatar
Jonny Schäfer committed
23
24
25
26
27
	var (
		srcLat, srcLon = 48.173146, 11.546604 // Munich
		dstLat, dstLon = 53.534366, 08.576135 // Bremerhaven
		expDist        = 663.629945199998     // km
	)
Jonny Schäfer's avatar
Jonny Schäfer committed
28

Jonny Schäfer's avatar
Jonny Schäfer committed
29
30
31
32
	dummys := []*Composite{
		NewDummy("SF", 900, 900),
		NewDummy("SF", 450, 450),
		NewDummy("SF", 225, 225),
Jonny Schäfer's avatar
Jonny Schäfer committed
33
34
35
		NewDummy("WX", 900, 1100),
		NewDummy("WX", 450, 550),
		NewDummy("EX", 1400, 1500),
36
		NewDummy("EX", 700, 750),
Jonny Schäfer's avatar
Jonny Schäfer committed
37
38
39
40
41
42
	}

	for _, comp := range dummys {
		srcX, srcY := comp.Translate(srcLat, srcLon)
		dstX, dstY := comp.Translate(dstLat, dstLon)

Jonny Schäfer's avatar
Jonny Schäfer committed
43
		resDist := dist(srcX*comp.Rx, srcY*comp.Ry, dstX*comp.Rx, dstY*comp.Ry)
Jonny Schäfer's avatar
Jonny Schäfer committed
44

Jonny Schäfer's avatar
Jonny Schäfer committed
45
		if !absequal(resDist, expDist, 0.000001) { // inaccuracy by 1mm
Jonny Schäfer's avatar
Jonny Schäfer committed
46
47
48
49
50
51
52
			t.Errorf("dummy.Rx = %#v, dummy.Ry = %#v; distance: %#v expected: %#v)",
				comp.Rx, comp.Ry, resDist, expDist)
		}
	}

}

Jonny Schäfer's avatar
Jonny Schäfer committed
53
func TestTranslate(t *testing.T) {
Jonny Schäfer's avatar
Jonny Schäfer committed
54
	nationalGridPG := [][]float64{
55
56
57
58
59
		[]float64{54.66218275, 1.900684377, 0, 0},
		[]float64{54.81884457, 15.88724008, 460, 0},
		[]float64{51.00000000, 09.00000000, 230, 230},
		[]float64{46.86029310, 3.481345126, 0, 460},
		[]float64{46.98044293, 14.73300934, 460, 460},
Jonny Schäfer's avatar
Jonny Schäfer committed
60
61
	}

Jonny Schäfer's avatar
Jonny Schäfer committed
62
63
64
65
66
67
	nationalGridHalf := [][]float64{
		[]float64{54.5877, 02.0715, 0, 0},
		[]float64{54.7405, 15.7208, 450, 0},
		[]float64{51.0000, 09.0000, 225, 225},
		[]float64{46.9526, 03.5889, 0, 450},
		[]float64{47.0705, 14.6209, 450, 450},
Jonny Schäfer's avatar
Jonny Schäfer committed
68
69
70
	}

	nationalGrid := [][]float64{
Jonny Schäfer's avatar
Jonny Schäfer committed
71
72
73
74
75
		[]float64{54.5877, 02.0715, 0, 0},
		[]float64{54.7405, 15.7208, 900, 0},
		[]float64{51.0000, 09.0000, 450, 450},
		[]float64{46.9526, 03.5889, 0, 900},
		[]float64{47.0705, 14.6209, 900, 900},
Jonny Schäfer's avatar
Jonny Schäfer committed
76
77
	}

Jonny Schäfer's avatar
Jonny Schäfer committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	extendedNationalGrid := [][]float64{
		[]float64{55.5482, 03.0889, 0, 0},
		[]float64{55.5342, 17.1128, 900, 0},
		[]float64{51.0000, 09.0000, 370, 550},
		[]float64{46.1929, 04.6759, 0, 1100},
		[]float64{46.1827, 15.4801, 900, 1100},
	}

	middleEuropeanGrid := [][]float64{
		[]float64{56.5423, -0.8654, 0, 0},
		[]float64{56.4505, 21.6986, 1400, 0},
		[]float64{51.0000, 09.0000, 600, 700},
		[]float64{43.9336, 02.3419, 0, 1500},
		[]float64{43.8736, 18.2536, 1400, 1500},
Jonny Schäfer's avatar
Jonny Schäfer committed
92
93
	}

Jonny Schäfer's avatar
Jonny Schäfer committed
94
95
96
	dummyPG := NewDummy("PG", 460, 460)
	dummyFZ := NewDummy("FZ", 450, 450)
	dummyRX := NewDummy("RX", 900, 900)
Jonny Schäfer's avatar
Jonny Schäfer committed
97
	dummyWX := NewDummy("WX", 900, 1100)
Jonny Schäfer's avatar
Jonny Schäfer committed
98
	dummyEX := NewDummy("EX", 1400, 1500)
Jonny Schäfer's avatar
Jonny Schäfer committed
99
100
101
102
103
104

	testcases := []struct {
		comp *Composite
		edge [][]float64
	}{
		{dummyPG, nationalGridPG},
Jonny Schäfer's avatar
Jonny Schäfer committed
105
		{dummyFZ, nationalGridHalf},
Jonny Schäfer's avatar
Jonny Schäfer committed
106
		{dummyRX, nationalGrid},
Jonny Schäfer's avatar
Jonny Schäfer committed
107
108
		{dummyWX, extendedNationalGrid},
		{dummyEX, middleEuropeanGrid},
Jonny Schäfer's avatar
Jonny Schäfer committed
109
110
111
112
113
114
115
116
117
118
119
120
	}

	for _, test := range testcases {
		t.Logf("dummy%s: Rx = %f; Ry = %f\n",
			test.comp.Product, test.comp.Rx, test.comp.Ry)
		t.Logf("dummy%s: offx = %f; offy = %f\n",
			test.comp.Product, test.comp.offx, test.comp.offy)

		for _, edge := range test.edge {
			// result
			rx, ry := test.comp.Translate(edge[0], edge[1])
			//expected
Jonny Schäfer's avatar
Jonny Schäfer committed
121
122
			ex := edge[2]
			ey := edge[3]
Jonny Schäfer's avatar
Jonny Schäfer committed
123

Jonny Schäfer's avatar
Jonny Schäfer committed
124
125
			// allowed inaccuracy by 100 meters
			if dist(rx, ry, ex, ey) > 0.1 {
Jonny Schäfer's avatar
Jonny Schäfer committed
126
127
128
129
130
131
				t.Errorf("dummy%s.Translate(%#v, %#v) = (%#v, %#v); expected: (%#v, %#v)",
					test.comp.Product, edge[0], edge[1], rx, ry, ex, ey)
			}
		}
	}
}
Jonny Schäfer's avatar
Jonny Schäfer committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

type gridMode string

const (
	gridBottom gridMode = "bottom"
	gridCenter          = "center"
)

func TestGrid(t *testing.T) {
	testGrid(t, gridCenter,
		NewDummy("SF", 900, 900),
		NewDummy("EX", 1400, 1500),
		NewDummy("WX", 900, 1100),
	)

	testGrid(t, gridBottom,
		NewDummy("SF", 900, 900),
		NewDummy("EX", 1400, 1500),
		// NewDummy("WX", 900, 1100), testdata unavailable
	)
}

func testGrid(t *testing.T, mode gridMode, dummys ...*Composite) {
	t.Helper()

	var offx, offy float64
	switch mode {
	case gridBottom:
		offx, offy = 0.0, 1.0
	case gridCenter:
		offx, offy = 0.5, 0.5
	default:
		t.Fatalf("unknown grid mode %#v", mode)
	}

	for _, comp := range dummys {
		lname := fmt.Sprintf("testdata/lambda_%s_%dx%d.txt", mode, comp.Dy, comp.Dx)
		pname := fmt.Sprintf("testdata/phi_%s_%dx%d.txt", mode, comp.Dy, comp.Dx)

		lbuf, err := ioutil.ReadFile(lname)
		if err != nil {
			t.Fatal(err)
		}
		pbuf, err := ioutil.ReadFile(pname)
		if err != nil {
			t.Fatal(err)
		}

		// fortran format F8.5 means read 8 bytes, the last 5 bytes are decimal places
		const length = 8

		var l, p int64

		// Beschreibung-E-Produkte-Raster.pdf Radolan-Cons Version 1.0:
		// "Die Dateien beginnen mit dem Referenzwert des Datenelements in der linken unteren Ecke des
		//  Komposits, spaltenweise von Westen nach Osten und zeilenweise von Süden nach Norden."
		//
		// "Für die Dateien mit der Bezeichnung _bottom beziehen sich die Koordinaten
		//  jeweils auf die linke untere Ecke jedes Datenelements. Für Dateien mit der Bezeichnung
		//  _center auf den Zentralpunkt."

		for y := comp.Dy - 1; y >= 0; y-- {
			for x := 0; x < comp.Dx; x++ {

				// newlines can occur in input files.
				for lbuf[l] == '\n' {
					l++
				}
				for pbuf[p] == '\n' {
					p++
				}

				lstring := string(bytes.TrimSpace(lbuf[l : l+length]))
				lamda, err := strconv.ParseFloat(lstring, 64)
				if err != nil {
					t.Fatalf("invalid grid coordinate at (%d, %d): %#v %s", x, y, lstring, lname)
				}
				l += length

				pstring := string(bytes.TrimSpace(pbuf[p : p+length]))
				phi, err := strconv.ParseFloat(pstring, 64)
				if err != nil {
					t.Fatalf("invalid grid coordinate at (%d, %d): %#v %s", x, y, pstring, pname)
				}
				p += length

				tx, ty := comp.Translate(phi, lamda)
				ex, ey := float64(x)+offx, float64(y)+offy
				if dist(tx, ty, ex, ey) > 0.01 { // 10m
					t.Errorf("dummy%s.Translate(%#v, %#v) = (%#v, %#v); expected: (%#v, %#v)",
						comp.Product, phi, lamda, tx, ty, ex, ey)
				}
			}
		}

		if len(bytes.TrimSpace(lbuf[l:])) > 0 {
			t.Fatalf("unprocessed data remaining in %s", lname)
		}
		if len(bytes.TrimSpace(pbuf[p:])) > 0 {
			t.Fatalf("unprocessed data remaining in %s", pname)
		}
	}
}