make-graphs-wlinks.pl


#!/usr/bin/perl
#
# make-graphs-wlinks.pl
# 
# by Rick Egeland <Ricky.Egeland@cern.ch>
# Takes the GOH eye scan data and creates plots and html with links to eye 
# diagrams.

use warnings;
use strict;
use GD::Graph::points;
$| = 1;

my %data;

# format the data file (goh-all.csv)
while (<>) {
  chomp;
  my @cols = split ",";
  if ($#cols < 14) { next;}
  next unless $cols[0];
    
  my ($goh, $cur, $pic, $powlo, $powhi) = @cols[0..4];
  my $minutes = $cols[12];
  my $dose = $cols[14];
  print join "\t", ($goh, $cur, $pic, $powlo, $powhi, $minutes, $dose);
  print "\n";

  # here is the data structure we will sort out
  $data{$goh}->{$cur}->{$dose} = {
				  pic => $pic,
				  powlo => $powlo,
				  powhi => $powhi
				 };
  
}

open HTML, ">", "graphs.html";

print HTML <<STOP;
<html><body>
<head>
<title>GOH Irradiation Eye Scan Results</title>
<link rel="stylesheet" type="text/css" href="../style/optical-links.css"/>
<base target="_self"/>
</head>
<h1>GOH Irradiation Eye Scan Results</h1>
<p>Data was taken 2004-02-27 to 2004-02-28.
Click on the data points to see the eye diagram.</p>
<p><a href="GOH_irrad_at_PSI_040227.xls">Excel data file</a></p>
<p><a href="goh-all.csv">Text Data file (CSV)</a></p>
<p><a href="make-graphs-wlinks.pl.html">Code that made this page</a></p>
STOP

foreach (sort {$a<=>$b} keys %data) { # for every GOH
  my $goh = $_;
  print "GOH $goh\n";
  print HTML "<h3>GOH $goh</h3>\n";
  print HTML "<table>\n";
  print HTML "<tr>\n";

  foreach (sort {$a<=>$b} keys %{ $data{$goh} }) { # for every curent
    my $cur = $_;

    print "GOH $goh CUR $cur\n";
    
    # ready our plotted data (and picture links)
    my @doseary = undef;
    my @hiary = undef;
    my @loary = undef;
    my @picary = undef;

    my $count = 0;
    foreach (sort {$a<=>$b} keys %{ $data{$goh}->{$cur}}) { # sort by dose
      my $dose = $_;
      my %data = %{ $data{$goh}->{$cur}->{$dose} };
      print "DOSE $dose HI $data{powhi} LO $data{powlo} PIC $data{pic}\n";

      # fill data arrays
      push (@doseary, $dose);
      push (@hiary, $data{powhi});
      push (@loary, $data{powlo});
      push (@picary, $data{pic});

    }

    # make a graph of data, with many options to make it less ugly
    my $graph = GD::Graph::points->new(300,300);
    my @data = (\@doseary, \@loary, \@hiary);
    $graph->set(x_label => 'Dose (p/cm^2)',
		y_label => 'Power (microwatts)',
		title   => "GOH $goh cur $cur",
		r_margin => 5,
		y_min_value => 0,
		y_max_value => 500,
		x_tick_number => 9,
		x_min_value => 0,
		x_max_value => 9E13,
		marker_size => 3,
		markers => [5, 5],
		dclrs => [ qw(green lblue) ],
		transparent => 0,
		x_number_format => "%0.2e",
		x_labels_vertical => 1) or die $graph->error;
    my $file = "plots/goh-$goh-cur-$cur.png";
    open(IMG, ">", $file) or die $!;
    my $gd = $graph->plot(\@data);
    print IMG $gd->png;
    close(IMG);
    
    my $mapname = "goh${goh}cur${cur}";
    print HTML ("<td><img src=\"$file\" width=\"300\" height=\"300\" ",
		"border =\"0\" ismap usemap=\"#$mapname\">\n");
    
    # hotspots to make our image map
    print HTML "<map id=\"$mapname\" name=\"$mapname\">\n";
    foreach (1..2) {
	my @arearefs = $graph->get_hotspot($_);
	for (my $i=1; $i < scalar @arearefs; $i++) {
	    my ($shape, $x1, $x2, $y1, $y2) = @{$arearefs[$i]};
	    #print "COORDS GOH $goh CUR $cur (x1, y1, x2, y2) ($x1, $y1, $x2, $y2)\n";
	    my $piclink = "eye-pics/GOH_irrad_test_$picary[$i].jpg";
	    print HTML ("<area shape=\"rect\" coords=\"$x1,$y2,$x2,$y1\" \n",
			"href=\"$piclink\" alt=\"eye diagram\">\n");
	}
    }

    # # animated gif (not useful)
#     my @fileary;
#     foreach (my $i=0; $i < scalar @picary; $i++) {
# 	my $piclink = "eye-pics/GOH_irrad_test_$picary[$i].jpg";
# 	push @fileary, $piclink;
#     }
#     my $list = join " ", @fileary;
#     `convert -delay 200 $list eye-anims/$mapname.gif`;

    print HTML "</map>\n";
    print HTML "</td>\n";

  }
  print HTML "</tr>\n";
  print HTML "</table>\n";
}

print HTML "</body></html>\n";
close HTML;