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

# script: mag_plots.pl
# created 00/12/19

# this perl script takes all the jpg files in the 
# /home/mussel5/ftp/pub/ace/level3/mag/summary
# directory and creates web pages that links to them all

# HISTORY:
#  2003Apr24-GRH-Extend & adj. col headings.
#	-Ensured o/p html filename ref'd to as a variable.
#	-Final preps to switch fm development to production. 
#  2003Apr21-GRH-Mods to table (make uniform cols).
#  2003Apr19-GRH-Mods to fix year end(bottom of each tbl).
#  2003Apr18-GRH-Incorporated HTML table formatted output. 
#  2003Apr17-GHamell-Mods per Dr.Davis:
#	Show most current data first (top of dispayed lists);
#	Add a jump (link) table to head of each year.

$date = `date`;

# FILENAMES of output HTML files

$summary_plots  = "summary_plots.html";
#$summary_plots  = "gh_summary_plots.html";

open(HTML_PG, ">$summary_plots") or die "can't open file $summary_plots";

@fns = glob( "summary/MAGSUMMARY*" );	# get filenames
$i_last  = $#fns ;		# & index of last element of list

#print "@fns\n";

print HTML_PG "<HTML>
<!-- **********************************************************************
This HTML file created with the script: /home/mussel5/ftp/pub/ace/level3/mag/mag_plots.pl
************************************************************************-->
<HEAD>
   <TITLE>ACE/MAG Summary Plots</TITLE>
   <SCRIPT LANGUAGE=\"javascript\" SRC=\"http://www.srl.caltech.edu/ACE/ASC/post.js\" ></SCRIPT>
</HEAD>

<BODY text=\"#FFFFFF\" link=\"#00EE00\" vlink=\"#EE0000\" 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/\" TARGET=\"_top\"><IMG SRC=\"/ACE/ASC/images/asc100x79.gif\" align=right BORDER=0></A>
<BR><BR>
<CENTER>
<H1>ACE/MAG Summary Plots</H1>
</CENTER>
<BR>
<HR>
<H3>Daily Summary plots of ACE/MAG data since launch, in RTN coordinate system</H3>
<P>
These files can be downloaded via <a href=\"ftp://mussel.srl.caltech.edu/pub/ace/level3/mag/summary\">anonymous ftp</a>, from mussel.srl.caltech.edu
<BR>
in the directory: pub/ace/level3/mag/summmary\n<P>\n";


# Find the First & last YEAR values
$fns[ 0 ]       =~ /.*MAGSUMMARY_(\d+)-(\d+).*/ ;
$yr_frst = $1;
$fns[ $i_last ] =~ /.*MAGSUMMARY_(\d+)-(\d+).*/ ;
$yr_last = $1;
#print "yr_last='$yr_last'\n";


# Make Links (jump tbl) to each year
$yr = $yr_last;
print HTML_PG "<HR><UL>\n";

while( $yr_frst <= $yr ) {
  print HTML_PG "<LI><A HREF=\"#$yr\">$yr</A>\n";
  $yr--;
}
print HTML_PG "</UL>\n";


# initialization 
$colmax = 4;
#$yr_check = $yr_last;
print HTML_PG "<table>\n";

$yr = $yr_last;

# Get separate list for each year 

while( $yr >= $yr_frst ) {
  # Get one years worth of filenames
  @fns = glob( "summary/MAGSUMMARY_$yr*" );	# get filenames for this "yr"
  # Calc row num of last full line
  $pitch = int(( $#fns + 1 ) / $colmax );
  if(( $pitch * $colmax ) != ( $#fns + 1 )) {
    $pitch++;
  }
  $last_full_line = $#fns - (( $colmax - 1 ) * $pitch ) ;

#  @fns = prt3colist( "MAG", $yr, $colmax );
  #print "@fns \n";
  # Generate break line and new heading

  print HTML_PG "</table>\n";
  print HTML_PG "<A HREF=\"$summary_plots\"><IMG SRC=\"http://www.srl.caltech.edu/ACE/ASC/images/top.gif\" ALT=\"top\" BORDER=0 HEIGHT=22 WIDTH=36 ALIGN=RIGHT></A>";
  print HTML_PG "<HR  WIDTH=\"100%\"><BR>\n" ;
  print HTML_PG "\n<A name=$yr><BR>\n";
  print HTML_PG "MAG Summary &nbsp;plots  for year <B>$yr</B>";
  print HTML_PG "<BR><table border=0 cellpadding=3 cellspacing=0>\n";
  print HTML_PG "<tr>";
  for( $i=0; $i<$colmax; $i++) {
    print HTML_PG "<td ALIGN=RIGHT>Year/<BR>DOY</td>";
    print HTML_PG "<td ALIGN=CENTER>File<BR>Size</td>";
    print HTML_PG "<td>&nbsp;</td><td>&nbsp;</td>";
  }
  print HTML_PG "</tr><BR><tr>\n";


  $r = 0;
  $c = 0;
  $tmpcolmax = $colmax;
  for( $rk=$#fns; $rk>0; $rk-=$tmpcolmax ) {   # get last/next filename from list of filenames
    for( $ck=0; $ck<$tmpcolmax; $ck++ ) {
    # calc index of fns list
    $i = $#fns - ($r + ( $c * $pitch));
    $fn = $fns[ $i ];
    $file_size = -s $fn;

    $fn =~ /.*MAGSUMMARY_(\d+)-(\d+).*/ ;
    print HTML_PG "<td ALIGN=RIGHT><A href=\"$fns[$i]\">$1/$2</A></td>";
#    print HTML_PG "<td></td>";
    print HTML_PG     "<td align=right>$file_size</td>";
    print HTML_PG "<td>&nbsp;</td><td>&nbsp;</td>";
    $c++;
    # did that use up all the columns?
    if( $c >= $tmpcolmax ) {
      print HTML_PG "</tr>\n<tr>";
      $c = 0;
      $r++;
      # did that fill up the last full row?
      if(( $r > $last_full_line ) && ( $tmpcolmax >= $colmax )) {
	$tmpcolmax--;		# do this only once
      }
    }
  }
  }
  print HTML_PG "</tr></table>\n";
  $yr--;
}
print HTML_PG "<HR>
<A HREF=\"/ACE/ASC/level2/mag_l2desc.html\">MAG Data Description Page</A>
<P>
<A HREF=\"/ACE/ASC/DATA/ftp/pub/ace/level3/index.html\">Contributed Data Page</A>
<P>
<A HREF=\"/ACE/ASC/\" TARGET=\"_top\">Return to ASC Home Page</A>
<ADDRESS>
<SCRIPT LANGUAGE=\"javascript\">
  // Use ASC default addr
  post();
</SCRIPT>
<BR><I>
Last Updated: $date
</I>
</ADDRESS>

</BODY>
</HTML>
";

close(HTML_PG);;

#------------------------------
