#!/afs/isis/pkg/perl-582/bin/perl
#raw2proc.perl
#Time-stamp: <2006-03-28 15:23:52 lstearns>
#
#Abstract: Take raw tower/roof/buoy data and convert to monthly netcdf files
#  of converted data!
#
#Usage: 
#####!/afs/isis/pkg/isis/bin/perl


##########################
# Luke Stearns           #
# lstearns@email.unc.edu #
#   work: (919)-962-0301 #
# mobile: (919)-619-5057 #
##########################
use Date::Manip;

$starttime = time;

if (1){
    $data_dir = '/seacoos/data/nc-coos/tower/';
    $raw_dir2 = $data_dir.'raw_data/';
    $proc_dir = $data_dir.'proc_data/';
    $perl_dir = '/opt/local/seacoos/bin/';
}

$now_LOCAL = &ParseDate("today");
$now_LOCAL_str = &UnixDate($now_LOCAL, "%Y:%m:%d %H:%M:%S (%Z)");
print "\n========= Starting: $now_LOCAL_str ========= Perl Version: $]\n"; 


#$data_dir = '/afs/isis.unc.edu/depts/marine/workspace/hseim/sablam4/'.
#    'lstearns/perl/test_nemo/';
#$raw_dir  = $data_dir.'raw_data/';

#$perl_dir = '/afs/isis.unc.edu/depts/marine/workspace/hseim/sablam4/'.
#	'lstearns/perl/test_nemo/';
#$perl_dir = "$data_dir";


#Directory where files are pushed from iridium! (soon will move to nemo)
#$raw_dir  = '/afs/isis/depts/marine/workspace/haines/nc-coos/'.
#    'test_tower_push_data/';
$raw_dir  = '/seacoos/ftp/';

@towers = ('R4','LSRB','T1');

foreach $tower_id (@towers){

    #See if there are any new files.
    @new_files = glob("${raw_dir}$tower_id/$tower_id*.txt");
    if ($#new_files>=0) {

	#Move files from nemo ftp to nemo raw dir directory
	system("mv ${raw_dir}$tower_id/$tower_id*.txt ${raw_dir2}$tower_id/");
	#system("cp ${raw_dir}$tower_id/$tower_id*.txt ${raw_dir2}$tower_id/");
	
	@new_file_list = glob("${raw_dir2}$tower_id/$tower_id*.txt");

	foreach $new_file (@new_file_list) {
	    #Loop through files and pick out month list.
	    $new_file =~m/${raw_dir2}$tower_id\/$tower_id\_(.*)\_.*/;
	    $date_stamp = $1;
	    @date_stamp = split('',$date_stamp);
	    $month_st1  = join('',@date_stamp[0..5]);
	    $month_st2  = join('',@date_stamp[0..3]).'_'.
		join('',@date_stamp[4..5]);

	    $months{$month_st1} = $month_st2;#.".nc";
	    #print "input: $month_st1 output: $month_st2\n";
	}

	foreach $month (keys %months) {
	    #print "$month and $months{$month}\n";
	    mkdir("${raw_dir2}$tower_id/$months{$month}/");
	    system("mv -f ${raw_dir2}$tower_id/${tower_id}_${month}*.txt ".
		   "${raw_dir2}$tower_id/$months{$month}/");
#	    system("mv ${raw_dir2}$tower_id/nccoos_${tower_id}_*_${month}.txt ".
#		   "${raw_dir2}$tower_id/$months{$month}/");
	    
	    print "${perl_dir}tower_parse_dev.perl $month  -d 1 -t $tower_id\n";
	    #system("${perl_dir}tower_parse.perl $month -out ".
	    #	   "${tower_id}_$months{$month}.nc -d 1 -t $tower_id");
	    system("${perl_dir}tower_parse_dev.perl $month ".
		   "-d 1 -t $tower_id");
	}
    }
    else {
	print "No new files for $tower_id\n";
    }
}


print "elapsed time ". time - $start_time. " seconds\n";
