[jonah] FYI PERL script for jonah_weatherstations.tsv

Todd A. Kopas toddk at lonestar.utsa.edu
Mon Mar 24 11:03:20 PST 2003


-------------- next part --------------
#!/usr/local/bin/perl

# WGET LOCATION
$WGET = "/usr/local/bin/wget";
$URL = "http://weather.interceptvector.com/cities/cities.zip";
$UNZIP = "/usr/bin/unzip";
$OUTFILE = "./jonah_weatherstations.tsv";

if ( ! (-f $UNZIP) ) {
	print "ERROR: This program requires the unzip command.\n";
	exit;
} 

# Downloading city list
if ( !(-f $WGET) && ! (-f "./cities.zip") )  {
	print "-----------------------------------------------------------\n";
	print "ERROR:\n";
        print "Please edit the \$WGET variable in \"create_weatherstations_tsv.pl\"\n";
	print "or download cities.zip from:\n";
	print "\n";
	print "\thttp://weather.interceptvector.com/cities/cities.zip\n";
	print "\n";
        print "and put the \"cities.zip\" file in this directory.\n";
	print "-----------------------------------------------------------\n";
        exit;
}
elsif ( $flag_cities_zip )  {
	system "$UNZIP cities.zip";
        start();
        exit;
}
elsif ( $WGET )  {
        system "$WGET $URL";
	system "$UNZIP cities.zip";
        start();
        exit;
}

sub start {
	# Opening jonah_weatherstations.tsv for write.
	open (OUT, ">$OUTFILE");

	# Getting a list of states including foreign.
        @LS = `ls -d *.xml | grep -v index.xml`;
        chop @LS;
	
        foreach $state (@LS)  {
		print "State $state ...";
                @CAT = `grep 'city name' $state`;
                $state =~ s/.xml$//g;
                foreach $cat (@CAT)  {
                        ($j1,$city,$j2,$url,) = split (/"/, $cat);
                        print OUT "$url\t$state\t$city\tgraphics/noradar.gif\t\n";
                }
		print "Complete.\n";
        }

	close OUT;
	print "./jonah_weatherstations.tsv has been created.\n";
}
-------------- next part --------------
To create jonah_weatherstations.tsv:
1.	Create a new directory.
2.	Put ./create_jonahweatherstations_tsv.pl in the new directory.
3.	Edit ./create_jonahweatherstations_tsv.pl with the appropriate
	wget location.
4.	Execute ./create_jonahweatherstations_tsv.pl.

Here's are some points in the code:

$state =~ s/.xml$//g;  <-- gets rid of xml suffic to find state
($j1,$city,$j2,$url,) = split (/"/, $cat); <-- parses file using " $j=junk1 


More information about the jonah mailing list