Last active
August 29, 2015 14:24
-
-
Save disassemblyline/96f29340bf5ccb0c2684 to your computer and use it in GitHub Desktop.
some of the vital components behind noodles weather
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use feature qw(switch); | |
use Fcntl ':flock'; | |
sub numeric_sort | |
{ | |
if ($a < $b) { return -1; } | |
elsif ($a == $b) { return 0;} | |
elsif ($a > $b) { return 1; } | |
} | |
sub save_file($$) | |
{ | |
my ($file, $content) = @_; | |
open my $write_file, ">:utf8", "$file" or die "Cannot write file: $!"; | |
flock $write_file, LOCK_EX; | |
print $write_file $content; | |
close $write_file; | |
} | |
sub return_file | |
{ | |
my $file = shift; | |
open my $read, '<:utf8', "$file"; | |
my $content = do { local $/; <$read> }; | |
close $read; | |
$content =~ s/[\r\n]//g; | |
return $content; | |
} | |
sub is_numeric | |
{ | |
my $str = shift; | |
my $rc = 0; | |
if ($str =~ m|^[0-9]+$|) | |
{ | |
$rc = 1; | |
} | |
return $rc; | |
} | |
sub is_float | |
{ | |
my $str = shift; | |
my $rc = 0; | |
if ($str =~ m|^[0-9\.]+$|) | |
{ | |
$rc = 1; | |
} | |
return $rc; | |
} | |
sub median | |
{ | |
my @a = sort {$a <=> $b} @_; | |
return ($a[$#a/2] + $a[@a/2]) / 2; | |
} | |
sub round | |
{ | |
my $number = shift; | |
return int($number + .5 * ($number <=> 0)); | |
} | |
sub millibars_to_inches | |
{ | |
my $millibars = shift; | |
return $millibars if !is_float($millibars); | |
return sprintf "%.2f", ($millibars * 0.0295301); | |
} | |
sub degrees_to_cardinal | |
{ | |
my $degrees = shift; | |
return $degrees if !is_numeric($degrees); | |
my @cardinal_arr = qw(N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW); | |
my $val = int(($degrees / 22.5) + .5); | |
my $idx = $val % 16; | |
return $cardinal_arr[$idx]; | |
} | |
sub meters_to_feet | |
{ | |
my $meters = shift; | |
my $feet = sprintf "%0.1f", ($meters / 0.3048); | |
return $feet; | |
} | |
sub meters_per_second_to_mph | |
{ | |
my $ms = shift; | |
return $ms if !is_numeric($ms); | |
my $mph = $ms * 2.23694; | |
return int($mph + $mph / abs($mph * 2)); | |
} | |
sub cloud_cover_description | |
{ | |
my $cloud_cover = shift; | |
my $str = "clear"; | |
return $cloud_cover if !$cloud_cover or !is_float($cloud_cover); | |
if ($cloud_cover < 0.12) | |
{ | |
$str = "clear"; | |
} | |
elsif ($cloud_cover >= 0.12 and $cloud_cover < 0.3) | |
{ | |
$str = "mostly clear"; | |
} | |
elsif ($cloud_cover >= 0.3 and $cloud_cover < 0.625) | |
{ | |
$str = "partly cloudy"; | |
} | |
elsif ($cloud_cover >= 0.625 and $cloud_cover <= 0.875) | |
{ | |
$str = "mostly cloudy"; | |
} | |
elsif ($cloud_cover > 0.875 and $cloud_cover <= 0.095) | |
{ | |
$str = "cloudy"; | |
} | |
elsif ($cloud_cover > .095) | |
{ | |
$str = "overcast"; | |
} | |
return $str; | |
} | |
sub calc_intensity | |
{ | |
my $intensity = shift; | |
my $str = "sprinkling / fog / mist"; | |
return $intensity if !$intensity; | |
$intensity = $intensity * 1000; | |
if ($intensity > 0 and $intensity < 17) | |
{ | |
$str = "very light"; | |
} | |
elsif ($intensity >= 17 and $intensity < 50) | |
{ | |
$str = "light"; | |
} | |
elsif ($intensity >= 50 and $intensity < 75) | |
{ | |
$str = "light to moderate"; | |
} | |
elsif ($intensity >= 75 and $intensity < 125) | |
{ | |
$str = "moderate"; | |
} | |
elsif ($intensity >= 125 and $intensity < 200) | |
{ | |
$str = "moderate to heavy"; | |
} | |
elsif ($intensity >= 200 and $intensity < 299) | |
{ | |
$str = "heavy"; | |
} | |
elsif ($intensity >= 300 and $intensity < 400) | |
{ | |
$str = "heavy to very heavy"; | |
} | |
elsif ($intensity >= 400) | |
{ | |
$str = "very heavy"; | |
} | |
return $str; | |
} | |
sub calc_beaufort_scale | |
{ | |
my $windspeed = shift; | |
my $num; | |
return $windspeed if !$windspeed or !is_float($windspeed); | |
if ($windspeed > 0 and $windspeed < 0.7) | |
{ | |
$num = 0; | |
} | |
elsif ($windspeed >= 0.7 and $windspeed < 3.4) | |
{ | |
$num = 1; | |
} | |
elsif ($windspeed >= 3.4 and $windspeed < 7.4) | |
{ | |
$num = 2; | |
} | |
elsif ($windspeed >= 7.4 and $windspeed < 12.2) | |
{ | |
$num = 3; | |
} | |
elsif ($windspeed >= 12.2 and $windspeed < 17.9) | |
{ | |
$num = 4; | |
} | |
elsif ($windspeed >= 17.9 and $windspeed < 21.4) | |
{ | |
$num = 5; | |
} | |
elsif ($windspeed >= 21.4 and $windspeed < 31) | |
{ | |
$num = 6; | |
} | |
elsif ($windspeed >= 31 and $windspeed < 38.4) | |
{ | |
$num = 7; | |
} | |
elsif ($windspeed >= 38.4 and $windspeed < 46.3) | |
{ | |
$num = 8; | |
} | |
elsif ($windspeed >= 46.3 and $windspeed < 54.8) | |
{ | |
$num = 9; | |
} | |
elsif ($windspeed >= 54.8 and $windspeed < 63.6) | |
{ | |
$num = 10; | |
} | |
elsif ($windspeed >= 63.6 and $windspeed < 72.9) | |
{ | |
$num = 11; | |
} | |
elsif ($windspeed >= 72.9) | |
{ | |
$num = 12; | |
} | |
return $num; | |
} | |
sub calc_beaufort_desc | |
{ | |
my $beaufort_scale = shift; | |
my $description; | |
given ($beaufort_scale) | |
{ | |
when(0) | |
{ | |
$description = "calm air"; | |
} | |
when(1) | |
{ | |
$description = "light air"; | |
} | |
when(2) | |
{ | |
$description = "light breeze"; | |
} | |
when(3) | |
{ | |
$description = "gentle breeze"; | |
} | |
when(4) | |
{ | |
$description = "moderate breeze"; | |
} | |
when(5) | |
{ | |
$description = "fresh breeze"; | |
} | |
when(6) | |
{ | |
$description = "strong breeze"; | |
} | |
when(7) | |
{ | |
$description = "near gale winds"; | |
} | |
when(8) | |
{ | |
$description = "gale winds"; | |
} | |
when(9) | |
{ | |
$description = "strong gale winds"; | |
} | |
when(10) | |
{ | |
$description = "storm winds"; | |
} | |
when(11) | |
{ | |
$description = "violent storm winds"; | |
} | |
when(12) | |
{ | |
$description = "hurricane winds"; | |
} | |
} | |
return $description; | |
} | |
sub calc_intensity_color | |
{ | |
my $intensity = shift; | |
my $str = "#000000;"; | |
return $str if !$intensity; | |
$intensity = $intensity * 1000; | |
if ($intensity > 0 and $intensity < 17) | |
{ | |
$str = "#c0c0c0;"; | |
} | |
elsif ($intensity >= 17 and $intensity < 50) | |
{ | |
$str = "#888888;"; | |
} | |
elsif ($intensity >= 50 and $intensity < 75) | |
{ | |
$str = "#006600;"; | |
} | |
elsif ($intensity >= 75 and $intensity < 125) | |
{ | |
$str = "#cccc00;"; | |
} | |
elsif ($intensity >= 125 and $intensity < 200) | |
{ | |
$str = "#cc6600;"; | |
} | |
elsif ($intensity >= 200 and $intensity < 299) | |
{ | |
$str = "#cc0000;"; | |
} | |
elsif ($intensity >= 300 and $intensity < 400) | |
{ | |
$str = "#990066;"; | |
} | |
elsif ($intensity >= 400) | |
{ | |
$str = "#000099;"; | |
} | |
return $str; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment