#!/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 'The output below was prepared using this',
' command:
rdsamp' .
" @rds_args";
print " >samples.csv" .
"
", "Note: 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",
" rdsamp on your own computer with no such",
" limitation.
'; show_file($samples); print ''; } else { show_html("no-samples"); } } 1;