From 19990cc66bd7c388f3dedd75b93a57d7e4df290e Mon Sep 17 00:00:00 2001
From: Since <ax20yhum@cip.cs.fau.de>
Date: Wed, 14 Sep 2016 12:33:24 +0200
Subject: [PATCH] Add Reflectivity function

---
 conversion.go      |  8 +++++++-
 conversion_test.go | 13 +++++++++----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/conversion.go b/conversion.go
index bc000ee..8358edb 100644
--- a/conversion.go
+++ b/conversion.go
@@ -11,11 +11,17 @@ type DBZ float64
 type RVP6 float64
 
 // PrecipitationRate returns the estimated precipitation rate in mm/h for the given
-// reflectivity factor. The used Z-R relation is descibed in [6].
+// reflectivity factor. The used Z-R relation is described in [6].
 func (z DBZ) PrecipitationRate() float64 {
 	return math.Pow(math.Pow(10, float64(z)/10)/256, 1/1.42)
 }
 
+// Reflectivity returns the estimated reflectivity factor for the given precipitation
+// rate (mm/h). The used Z-R relation is described in [6].
+func Reflectivity(rate float64) DBZ {
+	return DBZ(10 * math.Log10(256 * math.Pow(rate, 1.42)))
+}
+
 // ToDBZ converts the given radar video processor values (rvp-6) to radar reflectivity
 // factors in decibel relative to Z (dBZ).
 func (r RVP6) ToDBZ() DBZ {
diff --git a/conversion_test.go b/conversion_test.go
index ab74d93..a6ea91d 100644
--- a/conversion_test.go
+++ b/conversion_test.go
@@ -9,7 +9,7 @@ func TestConversion(t *testing.T) {
 	testcases := []struct {
 		rvp  RVP6
 		dbz  DBZ
-		rate float64
+		zr float64
 	}{
 		{0, -32.5, 0.0001},
 		{65, 0, 0.0201},
@@ -19,7 +19,8 @@ func TestConversion(t *testing.T) {
 
 	for _, test := range testcases {
 		dbz := test.rvp.ToDBZ()
-		rate := dbz.PrecipitationRate()
+		zr := dbz.PrecipitationRate()
+		rz := Reflectivity(zr)
 		rvp := dbz.ToRVP6()
 
 		if dbz != test.dbz {
@@ -28,8 +29,12 @@ func TestConversion(t *testing.T) {
 		if rvp != test.rvp {
 			t.Errorf("RVP6(%f).ToDBZ().ToRVP6() = %f; expected: %f", test.rvp, rvp, test.rvp)
 		}
-		if math.Abs(test.rate-rate) > 0.0001 {
-			t.Errorf("RVP6(%f).ToDBZ().PrecipitationRate() = %f; expected: %f", test.rvp, rate, test.rate)
+		if math.Abs(test.zr-zr) > 0.0001 {
+			t.Errorf("RVP6(%f).ToDBZ().PrecipitationRate() = %f; expected: %f", test.rvp, zr, test.zr)
+		}
+		if math.Abs(float64(test.dbz - rz)) > 0.0000001 {
+			t.Errorf("Reflectivity(RVP6(%f).ToDBZ().PrecipitationRate()) = %f; expected: %f",
+				test.rvp, rz, test.dbz)
 		}
 	}
 }
-- 
GitLab