+ my $avhdg = r2d(atan2($sindir, $cosdir));
+ $avhdg += 360 if $avhdg < 0;
+ return {w => $wind / $count, d => $avhdg};
+}
+
+# radians to degrees
+sub r2d
+{
+ my $n = shift;
+ return ($n / pi) * 180;
+}
+
+# degrees to radians
+sub d2r
+{
+ my $n = shift;
+ return ($n / 180) * pi;
+}
+
+sub calc_rain
+{
+ my $rain = shift;
+
+ my $Rain_1h = nearest(0.1, $rain >= $last_rain_hour ? $rain - $last_rain_hour : $rain); # this is the rate for this hour, so far
+ my $rm = $rain >= $last_rain_min ? $rain - $last_rain_min : $rain;
+ my $Rain_1m = nearest(0.1, $rm);
+ push @rain24, $rm;
+ $rain24 += $rm;
+ while (@rain24 > 24*60) {
+ $rain24 -= shift @rain24;
+ }
+ my $Rain_24h = nearest(0.1, $rain24);
+ return ($Rain_1m, $Rain_1h, $Rain_24h);