wmauda: Fix installation dir
[dockapps.git] / wmWeather-1.31 / Src / GrabWeather
blob86dde9cc4c36291f5a72114c73d741355a6277d0
1 #!/usr/bin/perl
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.;
9 # GrabWeather KLAM
13 require "ctime.pl";
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";
53 system "$GrabCmd";
59 # Parse HTML File.
61 $Temp = -999.0;
62 $Chill = -999.0;
63 $DewPnt = -999.0;
64 $Pressure = -999.0;
65 $Hum = -999.0;
66 $Flag = 0;
67 open(TmpFile, "$HTMLFileName");
68 while (<TmpFile>){
69 chop;
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/; }
79 ++$Flag;
81 close(TmpFile);
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/;
97 $VarFlag = 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/;
100 $VarFlag = 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/;
103 $VarFlag = 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/;
106 $VarFlag = 1;
107 } else {
108 $VarWindGroup = "";
109 $VarFlag = 0;
111 $Direction1 = $VarWindGroup; $Direction1 =~ s/(\d{3})V\d{3}/\1/;
112 $Direction2 = $VarWindGroup; $Direction2 =~ s/\d{3}V(\d{3})/\1/;
116 $GustFlag = 0;
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/;
125 $Speed *= 1.942;
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/;
131 $GustFlag = 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;
138 $GustFlag = 1;
139 } else {
140 $WindGroup = "";
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/;
153 } else {
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.
171 if ($Flag > 0){
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";
187 } elsif ($VarFlag) {
188 $Direction += 0;
189 print TmpFile "-$Direction\n";
190 } else {
191 $Direction += 0;
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";
201 } else {
202 $Speed += 0;
203 $Speed *= 1.15155;
204 print TmpFile "$Speed\n";
207 close(TmpFile);