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

sub mod_plot_rr_histogram {
    # 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/rrhist";
    $wu = "$baseurl/$wd";
    $wp = "$basepath/$wd";
    $ps = "$wp/rrhist.ps";
    $png = "$wp/rrhist.png";

    # If results for this request are already in the cache, return them.
    if (-s $png) {
	mod_plot_rrhist_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 PostScript plot.
    unless (fork) {
	open(STDOUT, ">$tplt");
	exec($RRHIST,
	     "-r", "$database/$record",
	     "-a", $annotator,
	     "-f", $tstart,
	     "-t", $tend,
	     "-T", "lw");
    }

    # Wait for rrhist 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 {
	rename($tpng, $png);
	rename($tps, $ps);
    }
    unlink($tplt);	# discard the temporary plt file
    mod_plot_rrhist_out();
}

sub mod_plot_rrhist_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="rrhist is a shell script">rrhist</a> -r' .
	    " $database/$record -a $annotator -f $tstart -t $tend -T lw" .
	    " &gt;<a href=$wu/rrhist.ps title='rrhist.ps is a PostScript file' .
            ' generated by rrhist and lwcat, with higher resolution than the' .
            ' image shown below'>rrhist.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 rrhist.ps',
	    " <a href=$wu/rrhist.png " .
	    "title='rrhist.png is the image shown below'>rrhist.png</a>" .
	    "</pre></font><hr>";
	print "<img src=$wu/rrhist.png>";
	
    }
    else {
	show_html("no-rr");
    }
}

1;
