#!/usr/bin/perl
use Data::Dumper;
use JSON;
use Text::CSV;
use utf8;
binmode STDOUT, ":encoding(UTF-8)";
my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", $ARGV[0] or die "$ARGV[0]: $!";
@fields = @{$csv->getline($fh)};
$csv->column_names(@fields);
print "\n\n";
while(my $dive = $csv->getline_hr($fh)) {
# print STDERR "Dive number " . $dive->{"Dive #"} . "\n";
my ($month, $day, $year) = split /\-/, $dive->{"Date"};
print "\n";
print "\n";
print "" . $dive->{"Dive buddy"} . "\n";
print "\n";
print "" . &fix_amp($dive->{"Dive Site"}) .", $dive->{Location}\n";
print "$dive->{lat} $dive->{lng}\n";
print "$dive->{Notes}\n\n" . $dive->{"Marine life sighting"} . "\n\n";
print "\n";
print "";
print "\n";
&samples($dive->{"Dive profile data"});
print "\n\n";
}
print "\n\n";
sub samples {
my $diviac = shift;
# print STDERR $diviac;
my $p = eval {decode_json($diviac)};
# print STDERR Dumper($p);
my $dive, $events;
$events = '';
foreach $line (@$p){
my ($a, $b, $c, $d, $e) = @$line;
my $min = int($a / 60);
my $sec = int($a) - 60 * $min;
my $temp = $c ? "temp = '$c C' " : "";
$dive .= "\n";
if (@$d) {
# print STDERR "Event at $a: ", (join '|', @$d), "\n";
my $ev = join(' ', @$d);
$events .= "\n";
}
}
print "$events $dive\n";
}
sub fix_feet {
my $d = shift;
if ($d =~ /([\d\.]+)\s*ft/) {
return ($1 * 0.3048) . ' m';
} else {
return $d;
}
}
sub fix_lb {
my $d = shift;
if ($d =~ /([\d\.]+)\s*lb/) {
return ($1 * 0.453592) . ' kg';
} else {
return $d;
}
}
sub fix_psi {
my $d = shift;
if ($d =~ /([\d\.]+)\s*psi/) {
return ($1 * 0.0689476) . ' bar';
} else {
return $d;
}
}
sub fix_cuft {
my ($d, $w) = @_;
my $p;
if ($w =~ /([\d\.]+)\s*psi/) {
$p = $1 * 0.0689476;
if ($d =~ /([\d\.]+)\s*ft/) {
return ($1 * 28.3168 / $p) . ' l';
} else {
return $d;
}
} else {
return '';
}
}
sub fix_amp {
my $s = shift;
$s =~ s/\&/\&/g;
return $s;
}