#read the output from aa.usno.navy.mil; there's a header 
# and footer to ignore, we just want the month data (left col
# has day of month, so from 01 to 31

#
# after that we parse the tabular data and output three arrays
# in java compatible format: how many minutes into each of 365
# days it is for sunrise, same for sunset, and how many days in
# to put the month pointers...

open(READ,"aa.usno.navy.mil_cgi-bin_aa_rstablew.pl.txt");
$startreading = 0; $stopreading = 0;
while(defined($line=<READ>)){
	chomp $line;
	if($line =~ /^01/){
		$startreading = 1;
	}
	
	if($startreading &&(! $stopreading)){
		$line =~ /^(\d\d)/;
		$dayofmonth = $1;
		$dayofmonth =~ s/^0//;
		$linefordayofmonth{$dayofmonth} = $line;
	}
	
	if($line =~ /^31/){
		$stopreading = 1;
	}


}
my $i;

for($i = 1; $i <= 31; $i++){

workline ($i, $linefordayofmonth{$i});	
}




$dayptr = 0;
$daysinmonth[0] = 0;
for($month = 1; $month <= 12; $month++){
	for($day = 1; $day <= 31; $day++){

		$sunrise = $rise[$month][$day];
		$sunset = $set[$month][$day];
		
		if($sunrise.$sunset ne "        " ){
			$sunrise =~ /(\d\d)(\d\d)/;
			$risehh = $1;
			$risemm = $2;
			$risemin = ($risehh*60) + $risemm;

			$sunset =~ /(\d\d)(\d\d)/;
			$sethh = $1;
			$setmm = $2;
			$setmin = ($sethh*60) + $setmm;

			push(@risestore,$risemin);
			push(@setstore,$setmin);
			#print " sunrise[".$dayptr."] = $risemin;\t//from $risehh:$risemm\n";
			#print " sunset[".$dayptr."] = $setmin;\t//from $sethh:$setmm\n";
			$dayptr++;
		} 
	}
	$daysinmonth[$month] = $dayptr;
	
}

print "float sunrisemin[] = {".join(",",@risestore)."};\n";
print "float sunsetmin[] = {".join(",",@setstore)."};\n";

pop(@daysinmonth);
#shift(@daysinmonth);


print "int monthdayptr[] = {".join(",",@daysinmonth)."};\n";
print "int daycount = $dayptr;";


sub workline{
	my($thisday,$thisline) = @_;
	
	
	#print $thisline,"\n";
	
	
	@parts = split(/  /,$thisline);
	my $i;	
	for($i= 1; $i <= 12; $i++){
		$ptrstart = 4 + (($i - 1) * 11);
		$sunrise = (substr($thisline,$ptrstart,4));
		$sunset = (substr($thisline,5+$ptrstart,4));
		
		#print "for $i $thisday sunrise $sunrise sunset $sunset\n";
		
		$rise[$i][$thisday] = $sunrise;
		$set[$i][$thisday] = $sunset;
		
	}
	
	
}