#!/usr/bin/perl
# file: plot_waveforms.pm	G. Moody        11 March 2009
#				Last revised:    8 March 2012
# _____________________________________________________________________________
# plot_waveforms module for PhysioBank's Automated Teller Machine
# Copyright (C) 2009-2012 George B. Moody

sub mod_plot_waveforms {
    # Exit immediately if no output is possible.
    unless ($slist[0]) {
	show_html("no-samples");
	return;
    }

    # Set defaults.
    $gflag = "-G";
    $mflag = "-M";
    $sm = 4;
    $ts = 25;
    $pw = 300;
    $tend = $tstart + 10;
    # Adjust options based on user's choices of signal, length, and time format.
    if ($dt > 10) {
	$ts = 5;
	$pw = 360;
	$tend = $tstart + 60;
    }
    if ($dt > 60) {
	$gflag = "-l";   # suppress grid
	$mflag = "-M0";  # suppress marker bars
	$sm = 0;	 # suppress scales
	$ts = 0.07;
	$tend = $tstart + 3600;
    }
    if ($dt > 3600 && $sfreq <= 2) {
	$ts = 0.005;
	$tend = $tstart + 43200;
    }
    $tm = 2; # default: time/date if available, otherwise elapsed time
    if ($tfmt eq "elapsed time") { $tm = 1; }

    # Set the path, URL, and final output filenames and URLs for this request.
    $wd = "chart/$ts/$tm";
    $wu = "$baseurl/$wd";
    $wp = "$basepath/$wd";
    $script = "$wp/script";
    $ps = "$wp/chart.ps";
    $png = "$wp/chart.png";

    # If results for this request are already in the cache, return them.
    if (-s $png && -s $ps && -s $script) {
	mod_plot_waveforms_out();
	return;
    }
	
    # Create the working directory for this request.
    mkpath($wp);

    # Make temporary filenames.
    $tscript = "$wp/$$.script";
    $tps = "$wp/$$.ps";
    $tpng = "$wp/$$.png";    

    # Create a script for pschart.
    open(SCRIPT, ">$tscript");
    print SCRIPT "$database/$record $tstart-$tend\n";
    close(SCRIPT);

    # Create and save the PostScript chart.
    unless (fork) {
	open(STDOUT, ">$tps");
	if (defined $signal_num) {
	    exec($PSCHART,
	     "-a", $annotator,
	     "-c", "",
	     "-C",
	     "-E",
	     $gflag,
	     "-CG", "1", ".5", ".5",
	     "-Cs", "0", "0", "0",
	     "-H",
	     "-l",
	     "-P", $pw . "x100",  # -P must precede -m
	     "-m", "20", "20", "5", "5",
	     $mflag,
	     "-n", "0",
	     "-s", $signal_num,
	     "-S", $sm, $tm,
	     "-t", $ts,
	     "-T", "",
	     "-v", "10",
	     "-w", "0.5",
	     $tscript);
	}
	else {
	    exec($PSCHART,
	     "-a", $annotator,
	     "-c", "",
	     "-C",
	     "-E",
	     $gflag,
	     "-CG", "1", ".5", ".5",
	     "-Cs", "0", "0", "0",
	     "-H",
	     "-l",
	     "-P", $pw . "x250",  # -P must precede -m
	     "-m", "20", "20", "5", "5",
	     $mflag,
	     "-n", "0",
	     "-S", $sm, $tm,
	     "-t", $ts,
	     "-T", "",
	     "-v", "10",
	     "-w", "0.5",
	     $tscript);
	}
    }

    # When pschart is finished, check if it wrote anything.
    wait;
    if (-s $tps) {  # pschart was successful!
	# Convert the PostScript to PNG.
	unless (fork) {
	    exec($CONVERT, "-density", "100x100", $tps, $tpng);
	}
	# Wait for convert to finish writing the PNG file.
	wait;
    }

    if (-s $script) {	# there was a race, and this process lost!
	unlink($tscript);
    }
    else {
	rename($tscript, $script);
    }
    if (-s $ps) {
	unlink($tps);
    }
    else {
	rename($tps, $ps);
    }
    if (-s $png) {
	unlink($tpng);
    }
    else {
	rename($tpng, $png);
    }
    mod_plot_waveforms_out();
}

sub mod_plot_waveforms_out {
    # Show the commands and the chart.
    if (-s $png) {
	print "\n" . '<font style="font-size: .9em;"><b>The output below was prepared using these',
        ' commands:</b><br>' . "\n" . ' <pre><a href=/physiotools/wag/pschar-1.htm ' .
	    ' title="pschart is a WFDB application">pschart</a> -a ';
	if ($annotator) { print $annotator; }
	else { print '""'; }
	print ' -c "" -C -E ', $gflag, ' -CG 1 .5 .5 -Cs 0 0 0 -H -l',
	      ' -P ', $pw . "x250 -m 20 20 5 5 $mflag -n 0 \\\n";
	if (defined $signal_num) {
	    print " -s '$signal_num' \n";
	}
	print " -S $sm $tm -t $ts" . ' -T "" -v 10 -w 0.5 ' . "\n",
	"<a href=$wu/script title='script is a text input file'>script</a>" . "\n",
	" &gt;<a href='$wu/chart.ps' title='chart.ps is a PostScript file " .
        "generated by pschart, with higher resolution than the image shown " .
	"below'>chart.ps</a><br>" . "\n";
	print '<a href=http://www.imagemagick.org/script/convert.php ' . "\n" .
	    'target="other" title="convert is an ImageMagick application">' . "\n" .
	    'convert</a> -density 100x100 chart.ps' . "\n",
	    " <a href=$wu/chart.png " .
	    "title='chart.png is the image shown below'>chart.png</a>" . "\n" .
	    "</pre></font><hr>";
	print "<center><img src=$wu/chart.png></center>" . "\n";
	
    }
    else {
	show_html("no-samples");
    }
}

1;
