radolan.go 5.59 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Package radolan parses the DWD RADOLAN / RADVOR radar composite format. This data
// is available at the Global Basic Dataset (http://www.dwd.de/DE/leistungen/gds/gds.html).
// The obtained results can be processed and visualized with additional functions.
//
// Currently the national grid [1][4] and the extended european grid [5] are supported.
// Tested input products are PG, FZ, SF, RW, RX and EX. Those can be considered working with
// sufficient accuracy.
//
// In cases, where the publicly available format specification is unprecise or contradictory,
// reverse engineering was used to obtain reasonable approaches.
//	Used references:
//
//	[1] https://www.dwd.de/DE/leistungen/radolan/radolan_info/radolan_radvor_op_komposit_format_pdf.pdf
//	[2] https://www.dwd.de/DE/leistungen/gds/weiterfuehrende_informationen.zip
//	[3]  - legend_radar_products_fz_forecast.pdf
//	[4]  - legend_radar_products_pg_coordinates.pdf
//	[5]  - legend_radar_products_radolan_rw_sf.pdf
//	[6] https://www.dwd.de/DE/leistungen/radarniederschlag/rn_info/download_niederschlagsbestimmung.pdf
package radolan

import (
Jonny Schäfer's avatar
Jonny Schäfer committed
22
	"archive/tar"
Jonny Schäfer's avatar
Jonny Schäfer committed
23
	"bufio"
Jonny Schäfer's avatar
Jonny Schäfer committed
24
	"compress/gzip"
Jonny Schäfer's avatar
Jonny Schäfer committed
25
26
27
	"fmt"
	"io"
	"math"
Jonny Schäfer's avatar
Jonny Schäfer committed
28
	"sort"
Jonny Schäfer's avatar
Jonny Schäfer committed
29
30
31
32
33
34
35
36
37
38
39
	"time"
)

// Radolan radar data is provided in so called composite formats. Each composite is a combined
// image consisting of mulitiple radar sweeps spread over the composite area.
// The composite c has a an internal resolution of c.Dx (horizontal) * c.Dy (vertical) records
// covering a real surface of c.Dx * c.Rx * c.Dy * c.Dy square kilometers.
// The pixel value at the position (x, y) is represented by c.Data[ y ][ x ] and is stored as
// raw rvp-6 value (NaN if the no-data flag is set). This rvp-6 value is used differently
// depending on the product type:
//
40
41
42
43
44
45
46
47
48
49
50
// The rvp-6 value is used differently depending on the product type:
//
//	Product label            | rvp-6 value represents   | unit
//	-------------------------+--------------------------+------------------------
//	 PG, PC, PX*, ...        | cloud reflectivity       | dBZ
//	 RX, WX, EX, FZ, FX, ... | raw value		    | convert to dBZ with ToDBZ()
//	 RW, SF,  ...            | aggregated precipitation | mm/h or mm/d
//	 PR*, ...                | doppler radial velocity  | m/s
//
// The cloud reflectivity (in dBZ) can be converted to rainfall rate (in mm/h)
// via PrecipitationRate().
Jonny Schäfer's avatar
Jonny Schäfer committed
51
52
53
54
55
//
// The cloud reflectivity factor Z is stored in its logarithmic representation dBZ:
//	dBZ = 10 * log(Z)
// Real world geographical coordinates (latitude, longitude) can be projected into the
// coordinate system of the composite by using the translation method:
Jonny Schäfer's avatar
Jonny Schäfer committed
56
//	// if c.HasProjection
Jonny Schäfer's avatar
Jonny Schäfer committed
57
58
//	x, y := c.Translate(52.51861, 13.40833)	// Berlin (lat, lon)
//
Jonny Schäfer's avatar
Jonny Schäfer committed
59
60
61
//	rvp := c.At(int(x), int(y))				// Raw value (rvp-6)
//	dbz := rvp.ToDBZ()					// Cloud reflectivity (dBZ)
//	rat := dbz.PrecipitationRate(radolan.Doelling98)	// Rainfall rate (mm/h) using Doelling98 as Z-R relationship
Jonny Schäfer's avatar
Jonny Schäfer committed
62
63
64
65
66
67
//
//	fmt.Println("Rainfall in Berlin [mm/h]:", rat)
//
type Composite struct {
	Product string // composite product label

68
69
70
	CaptureTime  time.Time     // time of source data capture used for forcasting
	ForecastTime time.Time     // data represents conditions predicted for this time
	Interval     time.Duration // time duration until next forecast
Jonny Schäfer's avatar
Jonny Schäfer committed
71
72
73
74
75
76
77
78
79

	Data [][]RVP6 // rvp-6 data for each point [y][x]

	Dx int // data width
	Dy int // data height

	Rx float64 // horizontal resolution in km/px
	Ry float64 // vertical resolution in km/px

Jonny Schäfer's avatar
Jonny Schäfer committed
80
81
	HasProjection bool // coordinate translation available

Jonny Schäfer's avatar
Jonny Schäfer committed
82
83
84
	dataLength int // length of binary section in bytes

	precision int   // multiplicator 10^precision for each raw value
85
	level     []RVP6 // maps data value to corresponding rvp-6 value in runlength based formats
Jonny Schäfer's avatar
Jonny Schäfer committed
86
87
88
89
90

	offx float64 // horizontal projection offset
	offy float64 // vertical projection offset
}

Jonny Schäfer's avatar
Jonny Schäfer committed
91
// NewComposite reads binary data from rd and parses the composite.
Jonny Schäfer's avatar
Jonny Schäfer committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
func NewComposite(rd io.Reader) (comp *Composite, err error) {
	reader := bufio.NewReader(rd)
	comp = &Composite{}

	err = comp.parseHeader(reader)
	if err != nil {
		return
	}

	err = comp.parseData(reader)
	if err != nil {
		return
	}

	comp.calibrateProjection()

	return
}

Jonny Schäfer's avatar
Jonny Schäfer committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// NewComposites reads tar gz data from rd and returns the parsed composites sorted by
// ForecastTime in ascending order.
func NewComposites(rd io.Reader) ([]*Composite, error) {
	gzipReader, err := gzip.NewReader(rd)
	if err != nil {
		return nil, err
	}
	defer gzipReader.Close()

	tarReader := tar.NewReader(gzipReader)

	var cs []*Composite
	for {
		_, err := tarReader.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, err
		}

		c, err := NewComposite(tarReader)
		if err != nil {
			return nil, err
		}
		cs = append(cs, c)
	}

	// sort composites in chronological order
	sort.Slice(cs, func(i, j int) bool { return cs[i].ForecastTime.Before(cs[j].ForecastTime) })
	return cs, nil
}

Jonny Schäfer's avatar
Jonny Schäfer committed
144
145
146
147
148
149
150
151
// NewDummy creates a blank dummy composite with the given product label and dimensions. It can
// be used for generic coordinate translation.
func NewDummy(product string, dx, dy int) (comp *Composite) {
	comp = &Composite{Product: product, Dx: dx, Dy: dy}
	comp.calibrateProjection()
	return
}

Jonny Schäfer's avatar
Jonny Schäfer committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// At is shorthand for c.Data[y][x] and returns the radar video processor value (rvp-6) at
// the given point. NaN is returned, if no data is available or the requested point is located
// outside the scanned area.
func (c *Composite) At(x, y int) RVP6 {
	if x < 0 || y < 0 || x >= c.Dx || y >= c.Dy {
		return RVP6(math.NaN())
	}
	return c.Data[y][x]
}

// newError returns an error indicating the failed function and reason
func newError(function, reason string) error {
	return fmt.Errorf("radolan.%s: %s", function, reason)
}