#!/usr/bin/perl -w
# $HeadURL: file:///afs/sipb.mit.edu/contrib/weather/SVNRep/tools/extract-station.pl $
# $Id: extract-station.pl 17 2007-09-18 21:57:29Z foley $
# Perl wrapper for NOAAfilter and StationDB
# By Joe Foley<foley@mit.edu>
#

use strict;

use Data::Dumper;
use Carp;

use NOAAFilter;
use StationDB;
use WeatherDB;

my $def = { station_field => "Call Sign",
	    fields => ["Wind Speed (kt)", "Wind Direction"],	   
	   };
my $f_str = join("\" \"",@{$def->{fields}});
#print Data::Dumper->Dump([$def],[qw($def)]);

(@ARGV >= 1) or die "$0 <station_value> [station_field] [field1 field2 ...]
i.e. $0  \"BOS\" \"Call Sign\" \"Wind Speed (kt)\"

Note: You will need to quote values with spaces

\tstation_value is the ID we look for in stations.txt
\tstation_field is which column we look under 
\t    default: \"$def->{station_field}\"
\tfields are a list of which NOAA fields we wish to extract
\t    default: \"$f_str\"\n";


my $station_value = shift;
my $station_field = shift || $def->{station_field};
my $fields = @ARGV || $def->{fields};

my $db = StationDB->new();
my $wban = $db->search($station_value, $station_field);
!$wban and croak "Error: unable to find any WBAN for $station_field $station_value";
print STDERR "$station_field $station_value is WBAN Number $wban\n";
my $filter = NOAAFilter->new();
my $dumpfile = "dump-$station_value.txt";
print STDERR "Dumping  data for WBAN Number $wban into $dumpfile\n";
$filter->extract($wban, $dumpfile);
my $filterfile = "filtered-$station_value.csv";
my $wdb = WeatherDB->new(weatherfile=>$dumpfile);
$wdb->filter($fields,
	     "filtered-$station_value.csv");
print STDERR "Filtered data into $filterfile\n";
