changeset 491:f5c023538e2a

fetch_weather, weather: Remove certain "data translation" from fetch_weather and move that into weather.tcl. Also rename some tokens.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jan 2020 05:01:37 +0200
parents 0d6dfcc1c957
children 172bd0045aff
files config.weather.example fetch_weather.pl weather.tcl
diffstat 3 files changed, 45 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/config.weather.example	Tue Jan 21 22:56:37 2020 +0200
+++ b/config.weather.example	Thu Jan 23 05:01:37 2020 +0200
@@ -38,15 +38,17 @@
 set weather_msg_list_station [list "\002@station@\002 ( http://www.google.fi/maps/@@c_lat@,@c_lng@,18z )"]
 
 set weather_msg_wind_directions [list "pohjoinen" "koillinen" "itä" "kaakko" "etelä" "lounas" "länsi" "luode" ]
+set weather_msg_cloudiness_status [list "selkeää" "melkein selkeää" "verrattain selkeää" "verrattain selkeää" "puolipilvistä" "verrattain pilvistä" "verrattain pilvistä" "melkein pilvistä" "pilvistä" ]
 
 
 set weather_msg_result [list "\002@station@\002, mitattu klo @ctime@: \002@temp@°C\002"]
-lappend weather_msg_result ", tien pinta @rv_2@°C"
+lappend weather_msg_result ", tien pinta @road_surface_temp@°C"
 lappend weather_msg_result ", kosteus \002@humidity@%\002"
 lappend weather_msg_result ", tuuli \002@wind_speed@\002 m/s"
+#lappend weather_msg_result " (suunta \002@wind_direction@\002 - @wind_direction_deg@°)"
 lappend weather_msg_result " (suunta \002@wind_direction@\002)"
-lappend weather_msg_result ", pilvipeite \002@rv_1@\002"
-lappend weather_msg_result ", näkyvyys \002@rv_4@ km\002"
+lappend weather_msg_result ", pilvipeite \002@cloudiness@\002"
+lappend weather_msg_result ", näkyvyys \002@visibility@ km\002"
 lappend weather_msg_result "."
 
 
--- a/fetch_weather.pl	Tue Jan 21 22:56:37 2020 +0200
+++ b/fetch_weather.pl	Thu Jan 23 05:01:37 2020 +0200
@@ -119,49 +119,6 @@
 }
 
 
-my %th_rain_states =
-(
-  "Pouta" => "poutaa",
-  "Heikko" => "heikkoa sadetta",
-  "Kohtalainen" => "kohtalaista sadetta",
-  "Voimakas" => "voimakasta sadetta",
-);
-
-my $th_rain_states_k = join("|", map {quotemeta} sort { length($b)<=>length($a) } keys %th_rain_states);
-
-sub translate_rain($)
-{
-  my $tmp = $_[0];
-  $tmp =~ s/($th_rain_states_k)/$th_rain_states{$1}/igo;
-  return $tmp;
-}
-
-
-my %th_cloud_states =
-(
-  0 => "selkeää",
-  1 => "melkein selkeää",
-  2 => "verrattain selkeää",
-  3 => "verrattain selkeää",
-  4 => "puolipilvistä",
-  5 => "verrattain pilvistä",
-  6 => "verrattain pilvistä",
-  7 => "melkein pilvistä",
-  8 => "pilvistä",
-);
-
-sub translate_clouds($)
-{
-  return "" if ($_[0] eq "NaN" || $_[0] eq "");
-  my $tmp = int($_[0]);
-  foreach my $n (sort { $a <=> $b } keys %th_cloud_states)
-  {
-    return $th_cloud_states{$n}." (".$n."/8)" if ($tmp == $n);
-  }
-  return $tmp;
-}
-
-
 sub force_decode_utf8($)
 {
   if (!utf8::is_utf8($_[0]))
@@ -492,7 +449,7 @@
     "&storedquery_id=fmi::observations::weather::multipointcoverage".
     "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()).
     "&parameters=".join(",", @fmitems).
-    "&maxlocations=200&bbox=19,59,32,75";
+    "&maxlocations=300&bbox=19,59,32,75";
 
   print STDERR "FMI URI: ".$uri."\n" if (opt_get_int("debug") > 0);
 
@@ -588,7 +545,7 @@
                 plonk_data($frec->{"winddirection"}),
 
                 # Station type dependant data
-                translate_clouds(plonk_data($frec->{"totalcloudcover"})),
+                plonk_data($frec->{"totalcloudcover"}),
               ];
             }
           }
--- a/weather.tcl	Tue Jan 21 22:56:37 2020 +0200
+++ b/weather.tcl	Thu Jan 23 05:01:37 2020 +0200
@@ -127,14 +127,41 @@
 
 
 # Translate wind direction compass degree to name
-proc weather_get_wind_direction {udegree} {
+proc weather_get_wind_direction {uangle} {
   global weather_msg_wind_directions
 
-  set udir [expr int(floor((fmod($udegree, 360.0) + 45.0/2) / 45.0))]
-  if {$udir < [llength $weather_msg_wind_directions]} {
-    return [lindex $weather_msg_wind_directions $udir]
+  # If the data was not got, return empty value
+  if {$uangle == ""} {
+    return ""
+  }
+
+  # Calculate index to array of 8 compass direction names based on the angle we have
+  set uvalue [expr int(floor(fmod($uangle + 45.0/2, 360.0) / 45.0))]
+  if {$uvalue >= 0 && $uvalue < [llength $weather_msg_wind_directions]} {
+    return [lindex $weather_msg_wind_directions $uvalue]
   } else {
-    return "ERROR"
+    return "ERROR ($udir)"
+  }
+}
+
+
+# Translate cloudiness status
+proc weather_get_cloudiness_status {uvalue} {
+  global weather_msg_cloudiness_status
+
+  # If the data was not got, return empty value
+  if {$uvalue == "" || $uvalue == "NaN"} {
+    return ""
+  }
+
+  set uvalue [expr int($uvalue)]
+
+  set ulen [llength $weather_msg_cloudiness_status]
+  if {$uvalue >= 0 && $uvalue < $ulen} {
+    set umsg [lindex $weather_msg_cloudiness_status $uvalue]
+    return "$umsg ($uvalue/[expr $ulen - 1])"
+  } else {
+    return "ERROR ($uvalue)"
   }
 }
 
@@ -153,8 +180,12 @@
   set uvals(temp) [lindex $udata 6]
   set uvals(humidity) [lindex $udata 7]
   set uvals(wind_speed) [lindex $udata 8]
-  set wdeg [lindex $udata 9]
-  set uvals(wind_direction) "[weather_get_wind_direction $wdeg] ($wdeg)"
+  set uvals(wind_direction) [weather_get_wind_direction [lindex $udata 9]]
+  set uvals(wind_direction_deg) [lindex $udata 9]
+  set uvals(cloudiness) [weather_get_cloudiness_status [lindex $udata 10]]
+  set uvals(road_surface_temp) [lindex $udata 11]
+  set uvals(precipitation) [lindex $udata 12]
+  set uvals(visibility) [lindex $udata 13]
 
   if {[expr [clock seconds] - $uvals(vtime)] < 3600} {
     set uvals(ctime) [clock format $uvals(vtime) -format "%H:%M"]
@@ -162,12 +193,6 @@
     set uvals(ctime) [clock format $uvals(vtime) -format "%H:%M (%d.%m.%Y)"]
   }
 
-  set mid 1
-  for {set id 10} {$id < [llength $udata]} {incr id} {
-    set uvals(rv_$mid) [lindex $udata $id]
-    incr mid
-  }
-
   set astr ""
   foreach aitem $umsg {
     set atmp $aitem