aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/diviac.pl
blob: 3225f4968339a6069ccbbb868022a11c7c5d5734 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/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 "<divelog program='Diviac' version='42'>\n<dives>\n";

while(my $dive = $csv->getline_hr($fh)) {
#  print STDERR "Dive number " . $dive->{"Dive #"} . "\n";
  my ($month, $day, $year) = split /\-/, $dive->{"Date"};
  print "<dive number='".$dive->{"Dive #"}."' date='$year-$month-$day' time='".$dive->{"Time in"}.":00' duration='".$dive->{"Duration"}.":00 min'>\n";
  
  print "<depth max='".&fix_feet($dive->{"Max depth"})."' mean='".&fix_feet($dive->{"Avg depth"})."' />\n";
  print "<buddy>" . $dive->{"Dive buddy"} . "</buddy>\n";
  print "<temperature air='" . $dive->{"Surface temp"} . "' water='" . $dive->{"Bottom temp"} . "' />\n";
  print "<location>" . &fix_amp($dive->{"Dive Site"}) .", $dive->{Location}</location>\n";
  print "<gps>$dive->{lat} $dive->{lng}</gps>\n";
  print "<notes>$dive->{Notes}\n\n" . $dive->{"Marine life sighting"} . "\n</notes>\n";
  print "<cylinder size='" . &fix_cuft($dive->{"Tank volume"}, $dive->{"Working pressure"}) . "' start='" . &fix_psi($dive->{"Pressure in"}) ."' end='" . &fix_psi($dive->{"Pressure out"}) . "' description='" . $dive->{"Tank type"} . "' />\n";
  print "<weightsystem weight='" . &fix_lb($dive->{"Weight"})  ."' description='unknown' />";
  print "<divecomputer model='Diviac import'>\n";
  &samples($dive->{"Dive profile data"});
  print "</divecomputer>\n</dive>\n";
}

print "</dives>\n</divelog>\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 .=  "<sample time='$min:$sec min' $temp depth='$b m' />\n";
    
    if (@$d) {
 #     print STDERR "Event at $a: ", (join '|', @$d), "\n";
      my $ev = join(' ', @$d);
      $events .= "<event time ='$min:$sec min' name = '$ev' value='' />\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/\&/\&amp;/g;
  return $s;
}