# Usage 1: perl cat_ace_rt_files.pl or perl cat_ace_rt_files.pl all
#
# Concatenates all files matching _swepam_ (_mag_) in current
# directory and all subdirectories.  Concatenated file named
# swepam.dat (mag.dat) has only numeric data.  See the top of any data
# file for information on the columns. Data is not sorted with respect
# to time.
#
# Usage 2: perl cat_ace_rt_files.pl recent
#
# Concatenates files with dates in name corresponding to the most
# recent 3 months matching *MM*_ace_swepam_ (*MM*_ace_mag_) in current
# directory and all subdirectories.  Concatenated file named
# swepam_recent.dat (mag_recent.dat) has only numeric data.  See the
# top of any data file for information on the columns.  Data is not
# sorted with respect to time.

# R.S. Weigel 04/03/2005.

# To change: Large chunk of code is common in wanted subroutines should
#            be made into a subroutine.
#            Allow input of number of months for "recent" option.

use File::Find;

################################################################################
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);

$mon   = $mon+1;
$mon   = $mon-2;
$year  = $year+1900;
$year2 = $year;
$year3 = $year;

if ($mon == 1) {$mon1= 11;$mon2 = 12;$mon3=01;$year1=$year-1;$year2 = $year1;}
if ($mon == 2) {$mon1 = 12     ;$mon2 = 1      ; $mon3 = 2   ; $year1=$year-1;}
if ($mon > 2 ) {$mon1 = $mon-2 ;$mon2 = $mon-1 ; $mon3 = $mon; $year1=$year;}

if ($mon1 < 10) { $mon1 = "0$mon1" }
if ($mon2 < 10) { $mon2 = "0$mon2" }
if ($mon3 < 10) { $mon3 = "0$mon3" }
################################################################################

$dir1      = "./";
@directory = ($dir1);

####################################################
# Do only last 3 months
####################################################
if ($ARGV[0] !~ m/recent/) {
    open(OUTFILE_swepam,">swepam.dat");
    open(OUTFILE_mag,">mag.dat");
    
    find(\&wanted_all, @directory);

    close(OUTFILE_swepam);
    close(OUTFILE_mag);

    print "\n\ncat_ace_rt_files.pl: Wrote swepam.dat\n";
    print "cat_ace_rt_files.pl: Wrote mag.dat\n\n";
}
####################################################

####################################################
# Do all files
####################################################
if ($ARGV[0] =~ m/recent/) {
    printf "Local time is %4d-%02d-%02d %02d:%02d:%02d\n",
    $year,$mon+1,$mday,$hour,$min,$sec;

    $name1 = $year1 . $mon1;
    $name2 = $year2 . $mon2;
    $name3 = $year3 . $mon3;
    print "Matching files starting with $name1, $name2, or $name3\n";


    open(OUTFILE_swepam,">swepam_recent.dat");
    open(OUTFILE_mag,">mag_recent.dat");

    find(\&wanted_recent, @directory);

    close(OUTFILE_swepam);
    close(OUTFILE_mag);

    print "\n\ncat_ace_rt_files.pl: Wrote swepam_recent.dat\n";
    print "cat_ace_rt_files.pl: Wrote mag_recent.dat\n\n";
}

sub wanted_recent {

    if ( ($_ =~ m/$name1[0-9][0-9]_ace_swepam/) || 
	 ($_ =~ m/$name2[0-9][0-9]_ace_swepam/) ||
	 ($_ =~ m/$name3[0-9][0-9]_ace_swepam/)) {

	if ( $_ =~ m/_swepam_/ ) {
	    print "cat_ace_rt_files.pl: Reading $_\n";
	    
	    open(INFILE,"$_");
	    while ($line = <INFILE>) {
		if ($line !~ m/\#|[A-Z]/){
		    printf(OUTFILE_swepam "%s", $line);
		}
	    }
	    close(INFILE);
	    
	}
	
	if ( $_ =~ m/_mag_/ ) {
	    print "cat_ace_rt_files.pl: Reading $_\n";

	    open(INFILE,"$_");
	    while ($line = <INFILE>) {
		if ($line !~ m/\#|[A-Z]/){
		    printf(OUTFILE_mag "%s", $line);
		}
	    }
	    close(INFILE);
	    
	}

    }	
    
}


sub wanted_all {

    if ( $_ =~ m/_swepam_/ ) {

	print "cat_ace_rt_files.pl: Reading $_\n";

	open(INFILE,"$_");
	while ($line = <INFILE>) {
	    if ($line !~ m/\#|[A-Z]/){
		printf(OUTFILE_swepam "%s", $line);
	    }
	}
	close(INFILE);

    }

    if ( $_ =~ m/_mag_/ ) {
	print "cat_ace_rt_files.pl: Reading $_\n";

	open(INFILE,"$_");
	while ($line = <INFILE>) {
	    if ($line !~ m/\#|[A-Z]/){
		printf(OUTFILE_mag "%s", $line);
	    }
	}
	close(INFILE);

    }



}

