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

sub mod_plot_rr {
    # Return immediately if no output is possible.
    if (!$annotator) {
	show_html("no-rr");
	return;
    }
    # Set defaults.
    $tend = $tstart + 10;
    # Adjust options based on user's choices of length and time format.
    if ($dt > 10) {
	$tend = $tstart + 60;
    }
    if ($dt > 60) {
	$tend = $tstart + 3600;
    }
    if ($dt > 3600) {
	$tend = $tstart + 43200;
    }
    if ($dt == 0) {
	$tend = "e";
    }

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

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

    # Make temporary filenames.
    $tplt = "$wp/$$.plt";
    $tps = "$wp/$$.ps";
    $tpng = "$wp/$$.png";    

    # Create and save the plot.
    unless (fork) {
	open(STDOUT, ">$tplt");
	exec($RRPLOT,
	     "-r", "$database/$record",
	     "-a", "$annotator",
	     "-f", "$tstart",
	     "-t", "$tend",
	     "-T", "lw");
    }
    # Wait for rrplot to finish writing the plot file.
    wait;

    # Convert the plot to PostScript.
    unless (fork) {
	open(STDOUT, ">$tps");
	exec($LWCAT, "-t", "-eps", "$tplt");
    }
    # Wait for lwcat to finish writing the PostScript file.
    wait;

    # 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 $png) {	# there was a race, and this process lost!
	unlink($tps);
	unlink($tpng);
    }
    else {		# save the PNG and PS files for the cache
	rename($tpng, $png);
	rename($tps, $ps);
    }
    unlink($tplt);	# discard the temporary plt file
    mod_plot_rr_out();
}

sub mod_plot_rr_out {
    # Show the commands and the plot.
    if (-s $png) {
	print '<font size=-1><b>The output below was prepared using these',
        ' commands:</b><br> <pre><a href=/tutorials/hrv/#rrplot' .
	    ' title="rrplot is a shell script">rrplot</a> -r' .
	    " $database/$record -a $annotator -f $tstart -t $tend -T lw |" .
	    " <a href=/physiotools/plt/lwcat" .
	    ' title="lwcat is part of the plt package">lwcat</a> -ps'.
	    " &gt;<a href=$wu/rr.ps title='rr.ps is a PostScript file ' .
        'generated by rrplot, with higher resolution than the image shown ' .
	'below'>rr.ps</a><br>";
	print '<a href=http://www.imagemagick.org/script/convert.php ' .
	    'target="other" title="convert is an ImageMagick application">' .
	    'convert</a> -density 100x100 rr.ps',
	    " <a href=$wu/rr.png " .
	    "title='rr.png is the image shown below'>rr.png</a>" .
	    "</pre></font><hr>";
	print "<img src=$wu/rr.png>";
	
    }
    else {
	show_html("no-rr");
    }
}

1;
