2020-01-21 14:01:04 -07:00
#!/usr/bin/perl -w
use strict;
use warnings;
use Math::Trig;
2023-02-10 10:54:10 -07:00
use JSON;
2020-01-21 14:01:04 -07:00
2020-01-21 14:50:11 -07:00
my @path = split( "/" , $0 ) ;
pop @path;
my $p = join( "/" , @path) ;
2023-02-10 11:21:34 -07:00
my $config_file = -e " $ENV {HOME}/.location.json " ? " $ENV {HOME}/.location.json " : " $p /location_example.json " ;
2023-02-10 10:54:10 -07:00
# Load up config data
open ( CONFIG, $config_file ) or goto NOCONFIG;
my $raw_config = "" ;
while ( <CONFIG>) { $raw_config .= $_ ; }
my %config = %{ decode_json( $raw_config ) } ;
close( CONFIG) ;
NOCONFIG:
2023-02-10 11:21:34 -07:00
my $lat = ( $config { lat} or 40.48967) ;
my $lon = ( $config { lon} or -111.93882) ;
my $WB_providerID = ( $config { WBproviderID} or 23) ;
my $WB_stationID = ( $config { WBstationID} or "C3430" ) ;
my $WU_stationID = ( $config { WUstationID} or "KUTRIVER63" ) ;
my $city = ( $config { city} or "bluffdale" ) ;
2020-01-21 14:50:11 -07:00
2020-01-21 14:01:04 -07:00
sub checksources {
2023-02-10 10:54:10 -07:00
my $weather = ( ` $p /wttrin.sh $city ` || ` $p /weatherunder.sh $WU_stationID ` || ` $p /weatherbug.sh $lat $lon $WB_providerID $WB_stationID ` || ` $p /weathergov.sh $lat $lon ` ) or print "Not Available\n\n#FF0000" and exit 33;
2020-01-21 14:31:17 -07:00
return $weather ;
2020-01-21 14:01:04 -07:00
}
sub wavegen {
my $x = shift;
my $period = shift;
my $phaseshift = shift;
my $magnitude = shift;
my $y_adjust = shift;
return sin( $x /( $period /pi) +$phaseshift ) *$magnitude +$y_adjust ;
}
sub hexcolor {
my ( $temp ,$cold ,$hot ,$rwave ,$gwave ,$bwave ) = @_;
my $temp_gauge = ( $temp - $cold ) /( $hot - $cold ) *100;
my $r = ( 100 * sin( $temp_gauge / 10) ) + ( $temp_gauge - 30) * 2;
my $g = ( $temp_gauge - 50) * ( $temp_gauge - 75) * ( 0 - 1) / 10 + 100;
my $b = ( $temp_gauge - 25) * ( $temp_gauge - 25) * ( 0 - 1) / 5 + 100;
$r *= 2;
$g *= 2;
$b *= 2;
#brightness
my $brightness = 270; #out of a max of 768 (white)
#sanitize
$r = $r > 0 ? $r : 0;
$g = $g > 0 ? $g : 0;
$b = $b > 0 ? $b : 0;
$r = $r < 255 ? $r : 255;
$g = $g < 255 ? $g : 255;
$b = $b < 255 ? $b : 255;
if ( $r +$g +$b < $brightness ) {
$brightness -= ( $r +$g +$b ) ;
$r += ( $brightness /3) ;
$g += ( $brightness /3) ;
$b += ( $brightness /3) ;
}
if ( $rwave ) { $r = $rwave }
if ( $gwave ) { $g = $gwave }
if ( $bwave ) { $b = $bwave }
my $CR = unpack( "H2" ,pack( "I" ,$r ) ) ;
my $CG = unpack( "H2" ,pack( "I" ,$g ) ) ;
my $CB = unpack( "H2" ,pack( "I" ,$b ) ) ;
return "#" .$CR .$CG .$CB ;
}
sub getdir {
my $degree = shift;
if ( $degree <= 22 ) { return "↑" ; }
if ( $degree <= 67 ) { return "↗" ; }
if ( $degree <= 112 ) { return "→" ; }
if ( $degree <= 157 ) { return "↘" ; }
if ( $degree <= 202 ) { return "↓" ; }
if ( $degree <= 247 ) { return "↙" ; }
if ( $degree <= 292 ) { return "←" ; }
if ( $degree <= 337 ) { return "↖" ; }
return "↑" ;
}
2023-03-08 10:48:20 -07:00
sub c2f {
return ( $_ [ 0] * ( 9/5) ) + 32;
}
2020-01-21 14:01:04 -07:00
my @weather = split( ":" ,checksources) or print "Not Available\n\n#FF0000" and exit 33;
my $type = $weather [ 0] ;
my $temp = sprintf( "%.0f" , ( $weather [ 1] - 32) * ( 5/9) ) ; #converts from F to C
my $wind = $weather [ 2] ;
my $wdir = getdir( $weather [ 3] ) ;
my $color = hexcolor( $temp ,-17,42) ;
if ( $type = ~ /Rain/) { $type = "" ; }
elsif ( $type = ~ /Overcast/) { $type = "" ; }
elsif ( $type = ~ /cloud/) { $type = "" ; }
elsif ( $type = ~ /Clear/) { $type = "" ; }
elsif ( $type = ~ /Sun/) { $type = "" ; }
2020-07-21 10:41:16 -06:00
elsif ( $type eq "NA" ) { $type = "" ; }
2020-01-21 14:01:04 -07:00
elsif ( $type = ~ /Snow/) { $type = "" ; }
else { $type = $type ."..." ; }
2023-02-10 11:21:34 -07:00
my $loc = substr( ( $weather [ 4] or "??????????" ) , 0, 9) ;
2020-07-21 10:17:29 -06:00
2023-03-08 10:48:20 -07:00
my $format = '%10.10s - %5.5s %03dF %s %03dmph' ;
printf( $format , $loc , $type , c2f( $temp ) , $wdir , $wind ) ;
2022-02-16 16:35:20 -07:00
print "\n" ;
2023-03-08 10:48:20 -07:00
printf( $format , $loc , $type , c2f( $temp ) , $wdir , $wind ) ;
2022-02-16 16:35:20 -07:00
print "\n" ;
2020-01-21 14:01:04 -07:00
print $color ."\n" ;