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

# created 00/7/26

# it creates a web page (4day_plot.html) that
# shows the latest ACE 4 day browse plot

# script: 4day_plot.pl

#chdir("/home/mussel5/browse-plots/");
$year_in = `tail -1 /users/asc/aceprog/browse_proc/rbflist.all`;
#print "$year_in\n";
$year_in =~ s/.*BRW_(\d+)-(\d+)T.*\n/$1/;
$doy_in = $2;
#print "$year_in  $doy_in\n";

($day, $month, $year) = &convert_yrdoy2dy_mon_yr($year_in, $doy_in);

$DATE = `date`;
chop($DATE);

#open(LAST_DAY2, "/home/mussel5/browse-plots/last_day_processed.txt") or die "can't open file last_day_processed.txt - $!";

open(HTML_PG, ">/home/mussel5/browse-plots/4day_plot.html") or die "can't open file 4day_plot.html - $!";

$month < 10 and $month = "0"."$month";
$day = $day + 0;
$day < 10 and $day = "0"."$day";

	# move old 4 day plot to archive
`mv /home/mussel5/browse-plots/4day_plot*.gif /home/mussel5/browse-plots/4day_plot_archive/`;

	# add date to end of plot filename
`cp /home/mussel5/browse-plots/mag_swe_epam_sis.gif /home/mussel5/browse-plots/4day_plot$year$month$day.gif`;

%months = ( "01"  => "Jan",  "02"  => "Feb",  "03"  => "Mar",  "04"  => "Apr",		    "05"  => "May",  "06"  => "Jun",  "07"  => "Jul",  "08"  => "Aug",		    "09"  => "Sep",  "10"  => "Oct",  "11"  => "Nov",  "12"  => "Dec");

$day_display = $day + 0;
print HTML_PG "<HTML>
<!-- **************************************************************************
This HTML file created from the script: /home/mussel5/browse-plots/4day_plot.pl
****************************************************************************-->
<HEAD>
   <TITLE>ACE 4-day Browse Plot</TITLE>
</HEAD>

<BODY text=\"#FFFFFF\" link=\"#00EE00\" vlink=\"#FF0000\" bgcolor=\"#000000\" background=\"/ACE/ASC/images/ace12.jpg\">

<IMG src=\"/ACE/ASC/images/empty.gif\" width=100 height=79 border=0 align=left>

<A href=/ACE/ASC/><IMG src=\"/ACE/ASC/images/asc100x79.gif\" align=right border=0></A>
<BR><BR>
<CENTER>
<H1>ACE 4-day Browse Plot</H1>
<FONT size=+1>plot updated through</FONT>
<BR>
<B><FONT size=+2>$day_display $months{$month} $year</FONT></B>
<BR>
(DOY $doy_in)
</CENTER>
<HR>

<CENTER>
<A href=/ACE/ASC/DATA/browse-plots/4day_plot$year$month$day.gif><IMG src=\"http://www.srl.caltech.edu/ACE/ASC/DATA/browse-plots/4day_plot$year$month$day.gif\" align=left></A>
<A href=\"/ACE/ASC/browse/view_browse_data.html\">Browse&nbsp;Data</A>
<HR>
<BR>
<A href=\"/ACE/ASC/browse/mag_brws_plts.html\">MAG<BR>Browse&nbsp;Data</A>
<BR><BR>
<HR>
<BR><BR><BR><BR><BR><BR>
<A href=\"/ACE/ASC/browse/swp_brws_plts.html\">SWEPAM<BR>Browse&nbsp;Data</A>
<BR><BR><BR><BR><BR><BR>
<HR>
<BR><BR><BR><BR><BR><BR><BR><BR>
<A href=\"/ACE/ASC/browse/epam_brws_plts.html\">EPAM<BR>Browse&nbsp;Data</A>
<BR><BR><BR><BR><BR><BR><BR><BR><BR>
<HR>
<BR><BR>
<A href=\"/ACE/ASC/browse/sis_brws_plts.html\">SIS<BR>Browse&nbsp;Data</A>
<BR clear=\"left\">
</CENTER>
";

#########################################################################
#########################################################################
#########################################################################

print HTML_PG "<BR>\n<A HREF=\"/ACE/ASC/DATA/browse-plots/4day_plot$year$month$day.gif\">Click for Printable Page</A>\n";

print HTML_PG "<BR>\n<A HREF=\"/ACE/ASC/DATA/browse-plots/4day_plot_archive\">4 Day Plot Archive</A>\n";

print HTML_PG "<CENTER>\n";
print HTML_PG "<P>\n<A HREF=\"/ACE/ASC/browse/view_browse_data.html\">Complete ACE Browse Data</A>\n";

print HTML_PG "<P>\n<A HREF=\"/ACE/ASC/level2\">ACE Level 2 Data</A><BR>(Publication Quality)\n";
print HTML_PG "</CENTER>\n";

print HTML_PG "<HR>\n";
print HTML_PG "<P>\n<A HREF=\"/ACE/ASC/\">Return to ASC Home Page</A>
<ADDRESS>
Our Email Address:
<A HREF=\"mailto:asc\@srl.caltech.edu\">asc\@srl.caltech.edu</A>
<BR>";
print HTML_PG "html updated: $DATE\n";
print HTML_PG "</BODY>\n</HTML>";

###########################################################

sub convert_yrdoy2dy_mon_yr {
### convert YEAR DOY ---> day month year 
### input: year and DOY 
### output: day  month  year 

  my ($year_in, $doy_in) = @_;
  $doy_in > 366 and die "No year has more than 366 days - $!";

  ### initialize all 4 local variables to 0 -- Pg. 82 Perl 5 book
  my ($num, $month, $leap_day, $day) = (0) x 4;
  my $remainder = $year_in / 4;
  my $remainder2 = int($year_in / 4);

  ### if remainders are equal then year is evenly divisible by 4 
  ### and is a leap year. (caution will only work until year 2100 :)
  $remainder == $remainder2 and $leap_day = 1;
  
  ### DOY's for a non leap year for each month
  my @doys = ("31", "59", "90", "120", "151", "181", "212", "243", "273", "304", "334", "365");
  
  ### start with Mar since Jan & Feb are special cases (months=0-11)
  for ($num=2; $num<=11; $num++) {
        
    ### january is a special case
    $doy_in <= $doys[0] and ($day=$doy_in) and ($month=1) and last;
    ### february is a special case
    $doy_in <= ($doys[1]+$leap_day) and ($day=$doy_in-$doys[0]) and ($month=2) and last;

    ### for all other months
    ($doy_in <= $doys[$num]+$leap_day) and ($day=$doy_in-$doys[$num-1]-$leap_day) and ($month=$num+1) and last;
  }
  return ($day, $month, $year_in);
}
