singlebyte.go 1.71 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"
)

// parseSingleByte parses the single byte encoded composite as described in [1] and writes
9
// into the previously created PlainData field of the composite.
Jonny Schäfer's avatar
Jonny Schäfer committed
10
func (c *Composite) parseSingleByte(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.readLineSingleByte(reader)
		if err != nil {
			return err
		}

18
		err = c.decodeSingleByte(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
}

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

// decodeSingleByte decodes the source line and writes to the given destination.
39
func (c *Composite) decodeSingleByte(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(dst) != len(line) {
		return newError("decodeSingleByte", "wrong destination or source size")
	}

	for i, v := range line {
		dst[i] = c.rvp6SingleByte(v)
	}

	return nil
}

// rvp6SingleByte converts the raw byte of single byte 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) rvp6SingleByte(value byte) float32 {
Jonny Schäfer's avatar
Jonny Schäfer committed
55
	if value == 250 { // error code: no-data
56
		return NaN
Jonny Schäfer's avatar
Jonny Schäfer committed
57
58
	}

59
60
61
62
63
64
65
66
	conv := c.rvp6Raw(int(value)) // set decimal point

	// not sure if single byte formats are even used for other things than dBZ (RX, dBZ)
	if c.DataUnit != Unit_dBZ {
		return conv
	}

	return toDBZ(conv)
Jonny Schäfer's avatar
Jonny Schäfer committed
67
}