trinity-users@lists.pearsoncomputing.net

Message: previous - next
Month: December 2016

Re: [trinity-users] "Arbitrary changes to their UI"

From: midi-pascal <midi-pascal@...>
Date: Sat, 10 Dec 2016 07:12:58 -0500
<snip>

On 16-12-09 09:47 PM, Gene Heskett wrote:
>>>> I made a workaround for this problem when gkrellm weather plugin
>>>> stopped working.
>>>> It is only a Perl script (native to the plugin) to modify.
>>>> If   you are interested I can send it with instructions to this
>>>> list.
>>>>
>>>> Cheers.
>>> Please, send away!  And thank you, as a perl geek I am not. I speak
>>> just enough C or assembly(on a 6x09 platform), to be dangerous.
>> Here is  the Perl script in attachment :-)
>> The problem is that the URL to fetch the weather data changed and the
>> data format too...
>>
>> The script name is: GrabWeather
>> To install it, replace the existing one in /usr/share/gkrellm/:
>> sudo cp GrabWeather /usr/share/gkrellm/
>> make sure it is executable:
>> sudo chmod 755 /usr/share/gkrellm/GrabWeather
>>
>> Then restart gkrellm.
>> That's it!
> I didn't kill the old one, but renamed it, And made all the rights &
> owners match the old one. Stopped gkrellm & restarted it several times
> but no difference.  The url address in the old one had been patched
> once, and that is still that address in the "get your station ID at"
> box.
>
> Running as root ./GRabWeather KCKB returns nothing, and as me:
> ./GrabWeather KCKB
> Cannot specify both -k and -O if multiple URLs are given, or in
> combination
> with -p or -r. See the manual for details.
>
> Usage: wget [OPTION]... [URL]...
>
> Manual? I doubt if the old one applies to this code.
>
> Call me puzzled.
Sorry it did not worked the first time, BUT...

Could you try the new script in attachment?

Execute it at the command line with your city code as an argument.
ex: ./GrabWeather KCKB

  If the script worked properly, there should be 2 files in 
~/.wmWeatherReports/ directory:
KCKB.dat
KCKB.txt
Their timestamp must reflect they were just created.

When I execute it, ls -l ~/.wmWeatherReports/ gives me:
-rw-rw-r-- 1 pascal pascal  98 Dec 10 07:10 KCKB.dat
-rw-rw-r-- 1 pascal pascal 601 Dec 10 07:10 KCKB.TXT

If so the script worked as expected and you can install it safely in 
/usr/share/gkrellm/

Hope it will be OK this time :-)

midi-pascal

#!/usr/bin/perl

#
# Grabs the latest local weather conditions from the 
# National Weather Service (NWS).  Uses the decoded METAR reports.
#
# Need to give the 4-character METAR station code on the 
# Command Line. E.g.;
#
#           GrabWeather YSSY
#


use strict;
use IO::File;

my $StationID    = uc shift @ARGV or die "Usage: $0 <station-id>\n";
my $ReportDir  = '.wmWeatherReports';
my $WeatherSrc = 'http://tgftp.nws.noaa.gov/data/observations/metar/decoded/';
my $DataFileName;
my $TxtFileName;
my $home;

sub initialize
{
  #  Change to users home directory.
  $home = $ENV{HOME} || (getpwuid($<))[7];
  chdir() || chdir($home) or die "chdir '$home' failed: $!";
  # Create the report directory if needed
  unless(-e $ReportDir) {
    mkdir $ReportDir, 0755 or die "unable to mkdir '$ReportDir': $!";
  }
  chdir $ReportDir or die "chdir '$ReportDir' failed: $!";
  # Set the file name to get from $WeatherSrc
  $TxtFileName = "$StationID.TXT";
  # Set the file name for results
  $DataFileName = "$StationID.dat";

  # Get the html file from $WeatherSrc

  # Is LWP installed?
  eval { require LWP::UserAgent };
  if ($@) {
    my $cmd = qq{wget --proxy=off --passive-ftp --tries=0 --quiet } .
              qq{--output-document=$home/$ReportDir/$TxtFileName $WeatherSrc/$TxtFileName};
    `$cmd` == 0 or die "unable to fetch weather: $?";
  }
   else {
    $ENV{FTP_PASSIVE} = 1; # LWP uses Net::FTP internally.
    my $ua  = new LWP::UserAgent;
    my $req = new HTTP::Request(GET => "$WeatherSrc/$TxtFileName");
    my $rsp = $ua->request( $req );
    die $rsp->status_line unless $rsp->is_success;
    my $fh = new IO::File "> $home/$ReportDir/$TxtFileName" 
      or die "unable to write '$home/$ReportDir/$TxtFileName': $!";
    print $fh $rsp->content;
    close $fh or die "error closing '$home/$ReportDir/$TxtFileName': $!";
  }
}

############################################ main ############################################
initialize();

#
# Parse TXT File. 
#

my %stats = (
  temp           => -99.0, #
  chill          => -99.0,
  dew_point      => -99.0, #
  pressure       => -99.0, #
  humidity       => -99.0, #
  universal_time => '99:99',
);


my $fh = new IO::File $TxtFileName 
  or die "unable to read '$TxtFileName': $!";

my $line = 0;
while (<$fh>) {
  chomp;
  if ($line == 0) {
    $stats{station_info} = $1, next if /^(.*)\s\(/;
    $line++;
  }
  $stats{update_time}  = $1, next if / ([0-2.][0-9.]:[0-5.][0-9.])\s[AP]M/;
  $stats{sky_conditions} = $1, next if /Sky conditions: (.*)/;
  $stats{temp} = $1, next if /Temperature:\s*(\-{0,1}[0-9.]{1,}).*/;
  $stats{chill} = $1, next if /Windchill:\s*(\-{0,1}[0-9.]{1,}).*/;
  $stats{dew_point} = $1, next if /Dew Point:\s*(\-{0,1}[0-9.]{1,}).*/;
  $stats{pressure} = $2, next if /Pressure\s\(altimeter\):\s*(.*)\s*([0-9.]{4,}).*/;
  $stats{humidity} = $1, next if /Relative Humidity:\s*(\d{1,})\%.*/;
  $stats{coded_metar} = $1, next if /ob: (.*)/;
}
close $fh or die "error closing '$TxtFileName': $!";

#
#  Isolate the Wind groups out of the coded METAR report.
#  There may be two groups - the normal one and a variability set.
#
$stats{wind_group} = $stats{coded_metar};
$stats{wind_group} =~ s/ RMK\s.*$//;

$stats{var_flag} = 1 if $stats{wind_group} =~ /\d+(KT|MPS)\s\d+V\d+\s/;
if ($stats{wind_group} =~ /\s(\w{3})(?:(\d+)G)?(\d+)(KT|MPS)\s/) {
  @stats{qw( direction speed1 speed2 )} = ($1, $2, $3);
  if ($4 eq 'MPS') {
    $stats{speed1} *= 1.942 if defined $stats{speed1};
    $stats{speed2} *= 1.942; 
  }
}

#
#  Get the Time out of the coded Metar Report.
#

if ($stats{coded_metar} =~ /$StationID \d+?(\d{2})(\d{2})Z/) {
  $stats{universal_time} = "$1:$2";
}

#
#  Write out the stuff we need to the Data File. This is the file that will
#  be read by GKrellWeather.
#
#print "$DataFileName\n";
my $fh = $STDOUT; my $fh = new IO::File ">$DataFileName" 
  or die "unable to write '$DataFileName': $!";

print $fh
  map { "$stats{$_}\n" }
    qw( station_info update_time sky_conditions universal_time
        temp dew_point chill pressure humidity );

if (not exists $stats{direction}) {
  print $fh "-99\n";
} elsif ($stats{direction} =~ /VRB/) {
  print $fh "99\n";
} elsif ($stats{var_flag}) {
  print $fh $stats{direction} * -1, "\n";
} else {
  print $fh $stats{direction} + 0, "\n";
}

if (not $stats{direction}) {
  print $fh "-99\n";
} elsif (defined $stats{speed1} and defined $stats{speed2}) {
  my $ave_speed = (($stats{speed1} + $stats{speed2})/2.0) * 1.15155;
  print $fh "-$ave_speed\n";
} else {
  print $fh $stats{speed2} * 1.15155, "\n";
}

close $fh or die "error closing '$DataFileName': $!";