4 # Grabs the latest local weather conditions from the National Weather Service (NWS).
5 # Uses the decoded METAR reports.
7 # Need to give the 4-character METAR station code on the Command Line. E.g.;
17 # Change to users home directory. We used to dump into /tmp
18 # but using home dir instead avoids multiple users interfering
19 # with one another. (Yeah, we could "unique-ize" the filenames, but
20 # this is easier for now...)
22 $home = (getpwuid($<))[7];
23 $ok = chdir() || chdir($home);
27 # Check to see if .wmWeatherReports exists.
28 # If not, make it and move to it.
30 if ( !(-e
".wmWeatherReports") ){
31 mkdir(".wmWeatherReports", 0775);
33 chdir(".wmWeatherReports");
39 $StationID = $ARGV[0];
40 $HTMLFileName = "$StationID.TXT";
41 $URL = "ftp://weather.noaa.gov/data/observations/metar/decoded/$HTMLFileName";
42 $DataFileName = "$StationID.dat";
48 # I think some of these wget command line options may cause problems
49 # for some people? Dont know why... (Perhaps they have a ~/.wgetrc file
50 # that overrides command line options?).
52 $GrabCmd = "wget --proxy=off --passive-ftp --tries 0 -q -O $home/.wmWeatherReports/$StationID.TXT $URL";
67 open(TmpFile
, "$HTMLFileName");
70 if ($Flag == 0) { $StationInfo = $_; };
71 if ($Flag == 1) { $UpdateTime = $_; };
72 if ($_ =~ /^Sky conditions:/ ){ $SkyConditions = $_; $SkyConditions =~ s/Sky conditions: (.*)/\1/; }
73 if ($_ =~ /^Temperature:/ ){ $Temp = $_; $Temp =~ s/Temperature:\s*(\-{0,1}[0-9.]{1,}).*/\1/; }
74 if ($_ =~ /^Windchill:/ ){ $Chill = $_; $Chill =~ s/Windchill:\s*(\-{0,1}[0-9.]{1,}).*/\1/; }
75 if ($_ =~ /^Dew Point:/ ){ $DewPnt = $_; $DewPnt =~ s/Dew Point:\s*(\-{0,1}[0-9.]{1,}).*/\1/; }
76 if ($_ =~ /^Pressure \(.*\):/ ){ $Pressure = $_; $Pressure =~ s/Pressure \(.*\):\s*([0-9.]{2,}).*/\1/; }
77 if ($_ =~ /^Relative Humidity:/ ){ $Hum = $_; $Hum =~ s/Relative Humidity:\s*(\d{1,})\%.*/\1/; }
78 if ($_ =~ /^ob: / ){ $CodedMETAR = $_; $CodedMETAR =~ s/ob: (.*)/\1/; }
87 # Isolate the Wind groups out of the coded METAR report.
88 # There may be two groups - the normal one and a variability set.
90 $WindGroup = $CodedMETAR;
91 $WindGroup =~ s/ RMK\s.*$//;
92 $VarWindGroup = $WindGroup;
95 if ($WindGroup =~ /.*\s\w{3}\d{2,3}KT\s\d{3}V\d{3}\s.*/ ) {
96 $VarWindGroup =~ s/.*\s\w{3}\d{2,3}KT\s(\d{3}V\d{3})\s.*/\1/;
98 } elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}MPS\s\d{3}V\d{3}\s.*/ ) {
99 $VarWindGroup =~ s/.*\s\w{3}\d{2,3}MPS\s(\d{3}V\d{3})\s.*/\1/;
101 } elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}KT\s(\d{2,3}V\d{2,3})\s.*/ ) {
102 $VarWindGroup =~ s/.*\s\w{3}\d{2,3}G\d{2,3}KT\s(\d{2,3}V\d{2,3})\s.*/\1/;
104 } elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}MPS\s(\d{2,3}V\d{2,3})\s.*/ ) {
105 $VarWindGroup =~ s/.*\s\w{3}\d{2,3}G\d{2,3}MPS\s(\d{2,3}V\d{2,3})\s.*/\1/;
111 $Direction1 = $VarWindGroup; $Direction1 =~ s/(\d{3})V\d{3}/\1/;
112 $Direction2 = $VarWindGroup; $Direction2 =~ s/\d{3}V(\d{3})/\1/;
117 if ($WindGroup =~ /.*\s\w{3}\d{2,3}KT\s.*/ ) {
118 $WindGroup =~ s/.*\s(\w{3}\d{2,3}KT)\s.*/\1/;
119 $Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}KT/\1/;
120 $Speed = $WindGroup; $Speed =~ s/\w{3}(\d{2,3})KT/\1/;
121 } elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}MPS\s.*/ ) {
122 $WindGroup =~ s/.*\s(\w{3}\d{2,3}MPS)\s.*/\1/;
123 $Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}MPS/\1/;
124 $Speed = $WindGroup; $Speed =~ s/\w{3}(\d{2,3})MPS/\1/;
126 } elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}KT\s.*/ ) {
127 $WindGroup =~ s/.*\s(\w{3}\d{2,3}G\d{2,3}KT)\s.*/\1/;
128 $Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}G\d{2,3}KT/\1/;
129 $Speed1 = $WindGroup; $Speed1 =~ s/\w{3}(\d{2,3})G\d{2,3}KT/\1/;
130 $Speed2 = $WindGroup; $Speed2 =~ s/\w{3}\d{2,3}G(\d{2,3})KT/\1/;
132 } elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}MPS\s.*/ ) {
133 $WindGroup =~ s/.*\s(\w{3}\d{2,3}G\d{2,3}MPS)\s.*/\1/;
134 $Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}G\d{2,3}MPS/\1/;
135 $Speed1 = $WindGroup; $Speed1 =~ s/\w{3}(\d{2,3})G\d{2,3}MPS/\1/;
136 $Speed2 = $WindGroup; $Speed2 =~ s/\w{3}\d{2,3}G(\d{2,3})MPS/\1/;
137 $Speed1 *= 1.942; $Speed2 *= 1.942;
146 # Get the Time out of the coded Metar Report.
148 $UniversalTime = $CodedMETAR;
149 if ($UniversalTime =~ /$StationID \d{1,2}\d{2}\d{2}Z/ ){
151 $UniversalTime =~ s/$StationID (\d{1,2})(\d{2})(\d{2})Z .*/\2:\3/;
155 $UniversalTime = "99:99";
168 # Write out the stuff we need to the Data File. This is the file that will
169 # be read by wmWeather.
172 open(TmpFile
, ">$DataFileName");
173 print TmpFile
"$StationInfo\n";
174 print TmpFile
"$UpdateTime\n";
175 print TmpFile
"$SkyConditions\n";
176 print TmpFile
"$UniversalTime\n";
177 print TmpFile
"$Temp\n";
178 print TmpFile
"$DewPnt\n";
179 print TmpFile
"$Chill\n";
180 print TmpFile
"$Pressure\n";
181 print TmpFile
"$Hum\n";
183 if ($Direction eq "" ){
184 print TmpFile
"-9999\n";
185 } elsif ($Direction =~ /VRB/ ){
186 print TmpFile
"999\n";
189 print TmpFile
"-$Direction\n";
192 print TmpFile
"$Direction\n";
195 if ($WindGroup eq "" ) {
196 print TmpFile
"-9999\n";
197 } elsif ($GustFlag) {
198 $AvgSpeed = ($Speed1 + $Speed2)/2.0;
199 $AvgSpeed *= 1.15155;
200 print TmpFile
"-$AvgSpeed\n";
204 print TmpFile
"$Speed\n";