summaryrefslogtreecommitdiffstats
path: root/xslt/SuuntoSDM.xslt
blob: 8c03b98c8ec04482e4c65eed33acd6a32487544c (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" encoding="ASCII"/>

  <xsl:template match="/">
    <divelog program="subsurface-import" version="2">
      <dives>
        <divecomputer>
          <xsl:attribute name="model">
            <xsl:value-of select="concat('Suunto ', /SUUNTO/MSG/DEVICEMODEL)" />
          </xsl:attribute>
          <xsl:attribute name="deviceid">
            <xsl:value-of select="/SUUNTO/MSG/WRISTOPID" />
          </xsl:attribute>
          <xsl:attribute name="serial">
            <xsl:value-of select="/SUUNTO/MSG/WRISTOPID" />
          </xsl:attribute>
        </divecomputer>
        <xsl:apply-templates select="/SUUNTO/MSG"/>
      </dives>
    </divelog>
  </xsl:template>

  <xsl:template match="MSG">
    <xsl:variable name="units" select="'si'"/>

    <dive number="{DIVENUMBER}">
      <xsl:attribute name="date">
        <xsl:call-template name="dateConvert">
          <xsl:with-param name="date" select="DATE"/>
        </xsl:call-template>
      </xsl:attribute>

      <xsl:attribute name="time">
        <xsl:value-of select="TIME"/>
      </xsl:attribute>

      <xsl:attribute name="duration">
        <xsl:choose>
          <xsl:when test="DIVETIMESEC != ''">
            <xsl:call-template name="timeConvert">
              <xsl:with-param name="timeSec" select="DIVETIMESEC"/>
              <xsl:with-param name="units" select="$units"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="timeConvert">
              <xsl:with-param name="timeSec" select="SAMPLECNT * SAMPLEINTERVAL"/>
              <xsl:with-param name="units" select="$units"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>

      <!-- Custom fields to tags -->
      <xsl:if test="CUSTOM1|CUSTOM2|CUSTOM3|CUSTOM4|CUSTOM5 != ''">
        <xsl:attribute name="tags">
          <xsl:if test="CUSTOM1 != ''">
            <xsl:value-of select="concat(CUSTOM1, ',')" />
          </xsl:if>
          <xsl:if test="CUSTOM2 != ''">
            <xsl:value-of select="concat(CUSTOM2, ',')" />
          </xsl:if>
          <xsl:if test="CUSTOM3 != ''">
            <xsl:value-of select="concat(CUSTOM3, ',')" />
          </xsl:if>
          <xsl:if test="CUSTOM4 != ''">
            <xsl:value-of select="concat(CUSTOM4, ',')" />
          </xsl:if>
          <xsl:if test="CUSTOM5 != ''">
            <xsl:value-of select="concat(CUSTOM5, ',')" />
          </xsl:if>
        </xsl:attribute>
      </xsl:if>

      <xsl:choose>
        <xsl:when test="MEANDEPTH != ''">
          <depth max="{concat(translate(MAXDEPTH, ',', '.'),' m')}" mean="{concat(translate(MEANDEPTH, ',', '.'), ' m')}"/>
        </xsl:when>
        <xsl:otherwise>
          <depth max="{concat(translate(MAXDEPTH, ',', '.'),' m')}"/>
        </xsl:otherwise>
      </xsl:choose>

      <temperature air="{concat(AIRTEMP, ' C')}" water="{concat(WATERTEMPMAXDEPTH, ' C')}"/>
      <xsl:if test="SURFACETIME != '0'">
        <surfacetime>
          <xsl:call-template name="timeConvert">
            <xsl:with-param name="timeSec" select="SURFACETIME" />
            <xsl:with-param name="units" select="$units" />
          </xsl:call-template>
        </surfacetime>
      </xsl:if>
      <divemaster><xsl:value-of select="DIVEMASTER" /></divemaster>
      <buddy><xsl:value-of select="PARTNER" /></buddy>
      <xsl:choose>
        <xsl:when test="LOCATION != ''">
          <location><xsl:value-of select="normalize-space(concat(LOCATION, ' ' , SITE))" /></location>
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="SITE != ''">
            <location><xsl:value-of select="SITE" /></location>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:if test="WEIGHT|WEIGTH != ''">
        <weightsystem description='imported'>
          <xsl:attribute name="weight">
            <xsl:value-of select="concat(translate(WEIGHT|WEIGTH, ',', '.'), ' kg')"/>
          </xsl:attribute>
        </weightsystem>
      </xsl:if>

      <notes>
      <xsl:if test="LOGNOTES != ''">
        <xsl:value-of select="LOGNOTES" />
      </xsl:if>
      <xsl:if test="WEATHER != ''">
        Weather: <xsl:value-of select="WEATHER" />
      </xsl:if>
      <xsl:if test="WATERVISIBILITYDESC != ''">
        Visibility: <xsl:value-of select="WATERVISIBILITYDESC" />
      </xsl:if>
      <xsl:if test="BOATNAME != ''">
        Boat name: <xsl:value-of select="BOATNAME" />
      </xsl:if>
      </notes>

<!-- FIXME: add support for multiple cylinders, need sample data -->
      <cylinder>
      <xsl:attribute name="o2">
        <xsl:choose>
          <xsl:when test="O2PCT != ''">
            <xsl:value-of select="concat(O2PCT, '%')"/>
          </xsl:when>
          <xsl:otherwise>21.0%</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:if test="HEPCT != ''">
        <xsl:attribute name="he">
          <xsl:value-of select="concat(HEPCT, '%')"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="CYLINDERDESCRIPTION != ''">
        <xsl:attribute name="description">
          <xsl:value-of select="CYLINDERDESCRIPTION"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="CYLINDERSIZE != ''">
        <xsl:attribute name="size">
            <xsl:choose>
              <xsl:when test="CYLINDERUNITS = '0'">
                <xsl:value-of select="concat(CYLINDERSIZE, ' l')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="cuft2l">
                  <xsl:with-param name="size" select="CYLINDERSIZE"/>
                  <xsl:with-param name="pressure" select="CYLINDERWORKPRESSURE"/>
                </xsl:call-template>
              </xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
      </xsl:if>
      <xsl:attribute name="start">
        <xsl:variable name="number" select="CYLINDERSTARTPRESSURE"/>
        <xsl:call-template name="pressure">
          <xsl:with-param name="number" select="$number"/>
          <xsl:with-param name="units" select="$units"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="end">
        <xsl:variable name="number" select="CYLINDERENDPRESSURE"/>
        <xsl:call-template name="pressure">
          <xsl:with-param name="number" select="$number"/>
          <xsl:with-param name="units" select="$units"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:if test="CYLINDERWORKPRESSURE != '0'">
        <xsl:attribute name="workpressure">
          <xsl:variable name="number" select="CYLINDERWORKPRESSURE"/>
          <xsl:call-template name="pressure">
            <xsl:with-param name="number" select="$number"/>
            <xsl:with-param name="units" select="$units"/>
          </xsl:call-template>
        </xsl:attribute>
      </xsl:if>
      </cylinder>

      <!-- DELTA is the sample interval -->
      <xsl:variable name="delta">
        <xsl:choose>
          <xsl:when test="SAMPLEINTERVAL != ''">
            <xsl:value-of select="SAMPLEINTERVAL"/>
          </xsl:when>
          <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <!-- end delta -->

      <!-- gas change -->
<!-- FIXME: test this with proper data -->
      <xsl:for-each select="GASCHANGE">
        <xsl:if test="MIXTIME != '0'">
          <event name="gaschange">
            <xsl:attribute name="value">
              <xsl:value-of select="MIXVALUE" />
            </xsl:attribute>
            <xsl:attribute name="time">
              <xsl:call-template name="timeConvert">
                <xsl:with-param name="timeSec" select="MIXTIME"/>
                <xsl:with-param name="units" select="'si'"/>
              </xsl:call-template>
            </xsl:attribute>
          </event>
        </xsl:if>
      </xsl:for-each>
      <!-- end gas change-->

      <!-- dive sample - all the depth and temp readings -->
      <xsl:for-each select="SAMPLE">
        <xsl:choose>
          <xsl:when test="BOOKMARK != ''">
            <xsl:choose>
              <xsl:when test="substring-before(BOOKMARK, ':') = 'Heading'">
                <event name="heading">
                  <xsl:attribute name="value">
                    <xsl:value-of select="substring-before(substring-after(BOOKMARK, ': '), '°')"/>
                  </xsl:attribute>
                  <xsl:attribute name="time">
                    <xsl:call-template name="timeConvert">
                      <xsl:with-param name="timeSec" select="SAMPLETIME"/>
                      <xsl:with-param name="units" select="'si'"/>
                    </xsl:call-template>
                  </xsl:attribute>
                </event>
              </xsl:when>
              <xsl:otherwise>
                <xsl:if test="BOOKMARK != 'Surface'">
                  <event name="{BOOKMARK}">
                    <xsl:attribute name="time">
                      <xsl:call-template name="timeConvert">
                        <xsl:with-param name="timeSec" select="SAMPLETIME"/>
                        <xsl:with-param name="units" select="'si'"/>
                      </xsl:call-template>
                    </xsl:attribute>
                  </event>
                </xsl:if>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <sample>
              <xsl:attribute name="time">
                <xsl:call-template name="timeConvert">
                  <xsl:with-param name="timeSec" select="SAMPLETIME"/>
                  <xsl:with-param name="units" select="'si'"/>
                </xsl:call-template>
              </xsl:attribute>
              <xsl:attribute name="depth">
                <xsl:value-of select="concat(translate(DEPTH, ',', '.'), ' m')"/>
              </xsl:attribute>
              <xsl:if test="TEMPERATURE &gt; 0">
                <xsl:attribute name="temp">
                  <xsl:value-of select="TEMPERATURE"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:attribute name="pressure">
                <xsl:call-template name="pressure">
                  <xsl:with-param name="number" select="PRESSURE"/>
                  <xsl:with-param name="units" select="$units"/>
                </xsl:call-template>
              </xsl:attribute>
            </sample>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
      <!-- dive sample -->

    </dive>
  </xsl:template>
  <!-- end JDive -->

  <!-- convert pressure to bars -->
  <xsl:template name="pressure">
    <xsl:param name="number"/>
    <xsl:param name="units"/>

    <xsl:choose>
      <xsl:when test="$number != ''">
        <xsl:value-of select="concat(format-number(($number div 1000), '#.##'), ' bar')"/>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- end convert pressure -->

  <!-- convert cuft to liters-->
  <xsl:template name="cuft2l">
    <xsl:param name="size"/>
    <xsl:param name="pressure"/>
    <xsl:choose>
      <xsl:when test="$pressure != '0'">
        <xsl:value-of select="concat(format-number((($size*28.3168466) div ($pressure div 1013.25)), '0.000'), ' l')" />
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- end cuft2l -->

  <!-- convert time in seconds to minutes:seconds -->
  <xsl:template name="timeConvert">
    <xsl:param name="timeSec"/>
    <xsl:param name="units"/>

    <xsl:choose>
      <xsl:when test="$timeSec = '0'">
        <xsl:value-of select="'0:00 min'" />
      </xsl:when>
      <xsl:when test="$units = 'si'">
        <xsl:value-of select="concat(floor(number($timeSec) div 60), ':', format-number(floor(number($timeSec) mod 60), '00'), ' min')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat(substring-before($timeSec, '.'), ':', format-number(substring-after($timeSec, '.'), '00'), ' min')"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- end convert time -->

  <!-- convert date from dotted notation to dash notation -->
  <xsl:template name="dateConvert">
    <xsl:param name="date" />
    <xsl:value-of select="concat(substring($date,7,4), '-', substring($date,4,2), '-', substring($date,1,2))" />
  </xsl:template>
</xsl:stylesheet>