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

sub mod_samples_to_csv {
    # 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/csv/$pflag$tflag"; }
    else { $wd = "rdsamp/csv"; }
    $wu = "$baseurl/$wd";
    $wp = "$basepath/$wd";
    $samples = "$wp/samples.csv";

    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", "-c", "-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_samples_to_csv_out();
	return;
    }
	
    # Create the working directory for this request.
    mkpath($wp);

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

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

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

sub mod_samples_to_csv_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.csv title='samples.csv is " .
	    "the output shown below'>samples.csv</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;
