Хранилища Subversion geo-modmetar

Редакция

Редакция 3 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 3 Редакция 4
1
#!/usr/bin/perl -w
1
#!/usr/bin/perl -w
2
2
3
# $Id: fetch_temp.pl,v 1.1 2007/11/13 21:19:27 koos Exp $
-
 
4
-
 
5
# Brief Description
3
# Brief Description
6
# =================
4
# =================
7
#
5
#
8
# fetch_temp.pl is a program that demonstrates how to get the current
6
# fetch_temp.pl is a program that demonstrates how to get the current
9
# temperature from a nearby (or not) airport using Geo::METAR and the
7
# temperature from a nearby (or not) airport using Geo::ModMETAR and the
10
# LWP modules.
8
# LWP modules.
11
#
9
#
12
# Given an airport site code on the command line, fetch_temp.pl
10
# Given an airport site code on the command line, fetch_temp.pl
13
# fetches the current temperature and displays it on the
11
# fetches the current temperature and displays it on the
14
# command-line. For fun, here are some example airports:
12
# command-line. For fun, here are some example airports:
15
#
13
#
16
# LA     : KLAX
14
# LA     : KLAX
17
# Dallas : KDFW
15
# Dallas : KDFW
18
# Detroit: KDTW
16
# Detroit: KDTW
19
# Chicago: KMDW
17
# Chicago: KMDW
20
18
21
# Get the site code.
19
# Get the site code.
22
20
23
my $site_code = shift @ARGV;
21
my $site_code = shift @ARGV;
24
22
25
die "Usage: $0 <site_code>\n" unless $site_code;
23
die "Usage: $0 <site_code>\n" unless $site_code;
26
24
27
# Get the modules we need.
25
# Get the modules we need.
28
26
29
use Geo::ModMETAR;
27
use Geo::ModMETAR;
30
use LWP::UserAgent;
28
use LWP::UserAgent;
31
use strict;
29
use strict;
32
30
33
my $ua = new LWP::UserAgent;
31
my $ua = new LWP::UserAgent;
34
32
35
my $req = new HTTP::Request GET =>
33
my $req = new HTTP::Request GET =>
36
  "http://weather.noaa.gov/cgi-bin/mgetmetar.pl?cccc=$site_code";
34
  "http://weather.noaa.gov/cgi-bin/mgetmetar.pl?cccc=$site_code";
37
35
38
my $response = $ua->request($req);
36
my $response = $ua->request($req);
39
37
40
if (!$response->is_success) {
38
if (!$response->is_success) {
41
39
42
    print $response->error_as_HTML;
40
    print $response->error_as_HTML;
43
    my $err_msg = $response->error_as_HTML;
41
    my $err_msg = $response->error_as_HTML;
44
    warn "$err_msg\n\n";
42
    warn "$err_msg\n\n";
45
    die "$!";
43
    die "$!";
46
44
47
} else {
45
} else {
48
46
49
    # Yep, get the data and find the METAR.
47
    # Yep, get the data and find the METAR.
50
48
51
    my $m = new Geo::ModMETAR;
49
    my $m = new Geo::ModMETAR;
52
    my $data;
50
    my $data;
53
    $data = $response->as_string;               # grap response
51
    $data = $response->as_string;               # grap response
54
    $data =~ s/\n//go;                          # remove newlines
52
    $data =~ s/\n//go;                          # remove newlines
55
    $data =~ m/($site_code\s\d+Z.*?)</go;       # find the METAR string
53
    $data =~ m/($site_code\s\d+Z.*?)</go;       # find the METAR string
56
    my $metar = $1;                             # keep it
54
    my $metar = $1;                             # keep it
57
55
58
    # Sanity check
56
    # Sanity check
59
57
60
    if (length($metar)<10) {
58
    if (length($metar)<10) {
61
        die "METAR is too short! Something went wrong.";
59
        die "METAR is too short! Something went wrong.";
62
    }
60
    }
63
61
64
    # pass the data to the METAR module.
62
    # pass the data to the METAR module.
65
    $m->metar($metar);
63
    $m->metar($metar);
66
64
67
    # ask for the temperature(s)
65
    # ask for the temperature(s)
68
    my $f_temp = $m->TEMP_F;
66
    my $f_temp = $m->TEMP_F;
69
    my $c_temp = $m->TEMP_C;
67
    my $c_temp = $m->TEMP_C;
70
68
71
    my $time = localtime(time);
69
    my $time = localtime(time);
72
    print "The temperature at $site_code is $f_temp F ($c_temp C) as of $time.\n";
70
    print "The temperature at $site_code is $f_temp F ($c_temp C) as of $time.\n";
73
71
74
} # end else
72
} # end else
75
73
76
exit;
74
exit;
77
75
78
__END__
76
__END__
79
77
80
78
81
 
79