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

# script: swims_plots.pl
# created 03/02/06

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

$date = `date`;

# Init o/p HTML filename variable
$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( "mth*" );	# get files
$i_last = $#fns;

#print "@fns\n";

print HTML_PG "<HTML>
<!-- **********************************************************************
This HTML file created with the script: /home/mussel5/ftp/pub/ace/level3/swims/ACE_SWIMS_MMTH/swims_plots.pl
************************************************************************-->
<HEAD>
   <TITLE>ACE/SWIMS Daily Solar Wind Mass Distribution Plots</TITLE>
</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/><IMG SRC=\"/ACE/ASC/images/asc100x79.gif\" align=right BORDER=0></A>
<BR><BR>
<CENTER>
<H1>ACE/SWIMS Daily Solar Wind Mass Distribution Plots</H1>
</CENTER>
<BR>
<HR>
These files can be downloaded via
<a href=\"ftp://mussel.srl.caltech.edu/pub/ace/level3/swims/ACE_SWIMS_MMTH\">
anonymous ftp</a>, from mussel.srl.caltech.edu
<BR>
in the directory: pub/ace/level3/swims/ACE_SWIMS_MMTH\n<P>\n";

#print HTML_PG " (Year/DOY &nbsp; .....File Size)<BR>\n";




# initialization 
$colmax = 4;


# Find the First & last YEAR values
$fns[ 0 ]       =~ /mth(\d+).gif/ ; 
$yr_frst = int( $1 / 1000 );
$fns[ $i_last ] =~ /mth(\d+).gif/ ;
$yr_last = int( $1 / 1000 );
#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 

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( "mth$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 ) ;

  # 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 "SWIMS 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;

  # get last/next filename from list of filenames

  for( $rk=$#fns; $rk>0; $rk-=$tmpcolmax ) {
    for( $ck=0; $ck<$tmpcolmax; $ck++ ) {
    # calc index of fns list
    $i = $#fns - ($r + ( $c * $pitch));
    $fn = $fns[ $i ];
    $file_size = -s $fn;

    $fn =~ /mth(\d+).*/ ;
    $year = int( $1 / 1000 );
    $doy  = $1 % 1000; 
    print HTML_PG "<td ALIGN=RIGHT><A href=\"$fns[$i]\">$year/$doy</A></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/\">Return to ASC Home Page</A>
<ADDRESS>
Our Email Address:
<A HREF=\"mailto:asc\@srl.caltech.edu\">asc\@srl.caltech.edu</A>
<BR>
Last Updated: $date
</ADDRESS>

</BODY>
</HTML>
";

close(HTML_PG);
