#!/usr/bin/perl
# file: show_samples.pm		G. Moody         28 February 2009
#				Last revised:	 12 December 2010
# _____________________________________________________________________________
# show_samples module for PhysioBank's Automated Teller Machine
# Copyright (C) 2009-2010 George B. Moody

sub mod_show_samples {
    # Set defaults.
    $pflag = "p";	# output in physical units, standard precision
    $tflag = "s";	# elapsed time in seconds

    # Adjust options based on user's choice of time and data formats.
    if ($dfmt eq "high precision") { $pflag = "P"; }
    if ($dfmt eq "raw ADC units") { $pflag = ""; $tfmt = "samples"; }
    if ($tfmt eq "time/date") {	$tflag = "d"; }
    if ($tfmt eq "elapsed time") { $tflag = "e"; }
    if ($tfmt eq "minutes") { $tflag = "m"; }
    if ($tfmt eq "hours") { $tflag = "h"; }
    if ($tfmt eq "samples") { $tflag = "S"; }

    # Set the path and URL for this request.
    if ($pflag) { $wd = "rdsamp/H$pflag$tflag"; }
    else { $wd = "rdsamp"; }
    $wu = "$baseurl/$wd";
    $wp = "$basepath/$wd";
    $samples = "$wp/samples.txt";

    if ($dt * $sfreq > 100000) { # limit output to 100,000 samples per signal
	if ($sfreq > 16.66) { $tend = $tstart + 60; } # 1 minute
	elsif ($sfreq > 1.35) { $tend = $tstart + 3600; } # 1 hour
	else { $tend = $tstart + 43200; } # 12 hours
    }

    # Set up the command for this request.
    @rds_args = ("-r", "$database/$record", "-H", "-f", $tstart, "-t", $tend, "-v");
    if ($pflag) { push(@rds_args, "-$pflag$tflag"); }
    if (defined $signal_num) { push(@rds_args, "-s", $signal_num); }

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

    # Make a temporary filename.
    $txt = "$wp/$$.txt";

    # Convert the selected data and save in a temporary file.
    unless (fork) {
	open(STDOUT, ">$txt");
	exec($RDSAMP, @rds_args);
    }
    # Wait until rdsamp is finished.
    wait;

    if (-s $samples) { # there was a race, and this process lost!
	unlink($txt);
    }
    else {
	rename($txt, $samples);
    }
    mod_show_samples_out();
}

sub mod_show_samples_out {
    if (-s $samples) {
	# if ($signal =~ / /) { pop(@rds_args); push(@rds_args, "'$signal'"); }
	print '<font size=-1><b>The output below was prepared using this',
        ' command:</b><br> <pre><a href=/physiotools/wag/rdsamp-1.htm' .
	    ' title="rdsamp is a WFDB application">rdsamp</a>' .
	    " @rds_args";
	print " &gt;<a href=$wu/samples.txt title='samples.txt is " .
	    "the output shown below'>samples.txt</a>" .
	    "</pre>", "<b>Note:</b> The output that can be obtained using this",
	    " tool is limited to 100000 samples, since greater amounts may",
	    " cause problems for your web browser.  You can run",
	    " <tt>rdsamp</tt> on your own computer with no such",
	    " limitation.</font><hr>";
	print '<pre>';
	show_file($samples);
	print '</pre>';
    }
    else {
	show_html("no-samples");
    }
}

1;
