################################################################################
# Creates two data files YYYY_MM_DD_HH_MM_SS_Ap.dat and Ap.dat in the
# current directory.  These two files have the same number of rows.
# The file YYYY_MM_DD_HH_MM_SS_Ap.dat has six columns
# (year,month,day,hour,minute,second,Ap).  The file Ap.dat has only one
# column which is Ap values from column 7 of
# YYYY_MM_DD_HH_MM_SS_Ap.dat.
#
# R.S. Weigel 04/03/2004
################################################################################

#################################################################################
# Load lines from any file of the form YYYYMMAK.txt and the 
# file 7day_AK.txt into array $Line
#################################################################################

$dirname = './';
$k       = 0;
$i       = 0;
opendir(DIR, $dirname) or die "can't opendir $dirname: $!";

while (defined($file = readdir(DIR))) {
    if ($file =~ /[0-9]{6}AK.txt|7day_AK.txt/) {
	open(INFILE,"$file");
	# print "$file\n";
	while ($temp= <INFILE>) {
	    $i        = $i + 1;
	    $line[$i] = $temp;
#	    print "$line[$i]\n"
	}
	close(INFILE);
	$k = $k + 1;
    }
}

print "          parse_Ap_data.pl found $k files with name in the form YYYYMMAK.txt\n";

if ($k =0) {
    die "No files with name in the form YYYYMMAK.txt found in current directory.  Aborting.\n";
}

######################################################################################
# Keep only lines with a date (except lines with word "Issued") or the 
# word "Planetary".
# Change Jan, Feb, etc. to 01, 02, etc.
# Remove the coordinate string "N49 W 42".
# Change 1, 2, 3 etc. in day of month to 01, 02, 03 etc.
######################################################################################
$N     = $i;
$i     = 0;
open(DATAw1,">YYYY_MM_DD_HH_MM_SS_Ap.daily.dat") or die "Can't open $ARGV[0]_processed output file: $!\n";

while ($i < $N){
    $temp = $line[$i];
    if ($temp !~ m/Issued/){
	if ($temp =~ m/ Jan| Feb| Mar| Apr| May| Jun| Jul| Aug| Sep| Oct| Nov| Dec/) {
	    # Add a zero before single digits
	    $temp =~ s/([a-z]) ([0-9]\s)/$1 0$2/;
	    # Change month text to a number
	    #print "$temp";
	    $temp =~ s/Jan/01/; $temp =~ s/Feb/02/; $temp =~ s/Mar/03/;
	    $temp =~ s/Apr/04/; $temp =~ s/May/05/; $temp =~ s/Jun/06/;
	    $temp =~ s/Jul/07/; $temp =~ s/Aug/08/; $temp =~ s/Sep/09/; 
	    $temp =~ s/Oct/10/; $temp =~ s/Nov/11/; $temp =~ s/Dec/12/;
	    $temp_date = $temp;
	}
	# Grab lines with the word Planetary
	if ($temp =~ m/Planetary/){
	    # Remove the word Planetary
	    $temp =~ s/Planetary\(estimated Ap\)//;
	    #print "$temp";
	    $temp_data = $temp;
	    # Remove trailing white space and the trailing newline
	    #$temp_date  =~ s/^\s+//;
	    $temp_date =~ s/\s+$//;
	    $temp_data =~ s/^\s+//;
	    $temp_data =~ s/\s+$//;
	    # Remove extra spaces
	    $temp =~ s/\s+/ /g;
	    @LINE = split(/ /,$temp_data);
	    #print "$temp_date $LINE[0]\n";
	    # Change flag value
	    if ($LINE[0] == -1){
		$LINE[0] = 99999;
	    }
	    printf(DATAw1 "%s 23 59 59 %s\n",$temp_date,$LINE[0]);
	}
    }
    $i = $i + 1;
}

print "          parse_Ap_data.pl wrote file YYYY_MM_DD_HH_MM_SS_Ap.daily.dat\n";
close(DATAw1);
######################################################################################
