#!/usr/local/bin/perl
#-- -*-perl-*-

use Net::FTP;

# define some constant variables here
$localdir ="/Net/data/qscat/data/SW_AMSR/data/L2A/";
#$localdir ="/Net/data/qscat/data/SW_AMSR/data/L2A/";
$remotedir = "/data/amsrTrial/L2A/data/";
#$remotedir = "/data/amsrTrial/L2A/data/";

if ($#ARGV < 1) {
    print "Use:  date.pl yyyy ddd [ddd]...\n";
    exit(0);
}
$year = @ARGV[0]; 
@dates = @ARGV[1];
for ($i = 0; $i <= $#dates; $i++) {
    @dates[$i] = sprintf "%3.3d", @dates[$i];
    #print @dates[$i]."\n";
}


# get each date's files
foreach $date (@dates) {
    $tdate = $date;
    $tyear = $year;

    $tlocaldir = $localdir.$tyear."/".$tdate;
    $tremotedir = $remotedir.$tyear."/".$tdate;
    #print "FLAG\n";
    print $tlocaldir."\n";
    #change local dir
    mkdir $tlocaldir if ! -d $tlocaldir;
    chdir($tlocaldir);

    # connect to the remote ftp site
    $ftp = Net::FTP->new("podaac.jpl.nasa.gov");
    $ftp->login("swswt","Data4us?");
    #$ftp->login("anonymous","nscatauto\@");
    $ftp->binary();  
    $ftp->cwd($tremotedir);

    #retrieve files
    @files = $ftp->ls;    
    foreach $file (@files) {
	$ftp->get($file);
	print "GET  $file.......\n";
    }
    $ftp->quit;# quit ftp
}

