littleendian.go 2.13 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
7
8
package radolan

import (
	"bufio"
	"io"
)

// parseLittleEndian parses the little endian encoded composite as described in [1] and [3].
9
// Result are written into the previously created PlainData field of the composite.
Jonny Schäfer's avatar
Jonny Schäfer committed
10
func (c *Composite) parseLittleEndian(reader *bufio.Reader) error {
11
12
	last := len(c.PlainData) - 1
	for i := range c.PlainData {
Jonny Schäfer's avatar
Jonny Schäfer committed
13
14
15
16
17
		line, err := c.readLineLittleEndian(reader)
		if err != nil {
			return err
		}

18
		err = c.decodeLittleEndian(c.PlainData[last-i], line) // write vertically flipped
Jonny Schäfer's avatar
Jonny Schäfer committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
		if err != nil {
			return err
		}
	}

	return nil
}

// readLineLittleEndian reads a line until horizontal limit from the given reader
// This method is used to get a line of little endian encoded data.
func (c *Composite) readLineLittleEndian(rd *bufio.Reader) (line []byte, err error) {
	line = make([]byte, c.Dx*2)
	_, err = io.ReadFull(rd, line)
	if err != nil {
		err = newError("readLineLittleEndian", err.Error())
	}
	return
}

// decodeLittleEndian decodes the source line and writes to the given destination.
39
func (c *Composite) decodeLittleEndian(dst []float32, line []byte) error {
Jonny Schäfer's avatar
Jonny Schäfer committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	if len(line)%2 != 0 || len(dst)*2 != len(line) {
		return newError("decodeLittleEndian", "wrong destination or source size")
	}

	for i := range dst {
		tuple := [2]byte{line[2*i], line[2*i+1]}
		dst[i] = c.rvp6LittleEndian(tuple)
	}

	return nil
}

// rvp6LittleEndian converts the raw two byte tuple of little endian encoded composite products
// to radar video processor values (rvp-6). NaN may be returned when the no-data flag is set.
54
func (c *Composite) rvp6LittleEndian(tuple [2]byte) float32 {
Jonny Schäfer's avatar
Jonny Schäfer committed
55
56
57
58
	var value int = 0x0F & int(tuple[1])
	value = (value << 8) + int(tuple[0])

	if tuple[1]&(1<<5) != 0 { // error code: no-data
59
		return NaN
Jonny Schäfer's avatar
Jonny Schäfer committed
60
61
62
63
64
65
	}

	if tuple[1]&(1<<6) != 0 { // flag: negative value
		value *= -1
	}

66
67
68
69
70
71
72
73
74
75
76
	conv := c.rvp6Raw(value) // set decimal point

	// little endian encoded formats are also used for mm/h
	if c.DataUnit != Unit_dBZ {
		return conv
	}

	// Even though this format supports negative values and custom
	// precision they do not make use of this and we still have to subtract
	// the bias and scale it (RADVOR FX, dBZ)
	return toDBZ(conv)
Jonny Schäfer's avatar
Jonny Schäfer committed
77
}