remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

UDDF.swift (4266B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 
      4 /// Logbook export in UDDF 3.2 format (import into Subsurface, MacDive, etc.). SI units: m, s, K, Pa.
      5 public enum UDDF {
      6     public static func export(_ dives: [DiveLog], generatorName: String = "Open Rémora",
      7                               generatorVersion: String = "0.1") -> String {
      8         let iso = ISO8601DateFormatter()
      9         var mixes: [Gas] = []
     10         for d in dives where !mixes.contains(d.gas) { mixes.append(d.gas) }
     11         func mixID(_ g: Gas) -> String { "mix\(mixes.firstIndex(of: g)! + 1)" }
     12         func n(_ v: Double, _ digits: Int = 2) -> String { String(format: "%.\(digits)f", v) }
     13         func kelvin(_ c: Double) -> String { n(c + 273.15) }
     14         func esc(_ s: String) -> String {
     15             s.replacingOccurrences(of: "&", with: "&amp;").replacingOccurrences(of: "<", with: "&lt;")
     16                 .replacingOccurrences(of: ">", with: "&gt;").replacingOccurrences(of: "\"", with: "&quot;")
     17         }
     18 
     19         var x = """
     20         <?xml version="1.0" encoding="UTF-8"?>
     21         <uddf xmlns="http://www.streit.cc/uddf/3.2/" version="3.2.3">
     22           <generator>
     23             <name>\(esc(generatorName))</name>
     24             <type>divecomputer</type>
     25             <version>\(generatorVersion)</version>
     26           </generator>
     27           <gasdefinitions>
     28 
     29         """
     30         for g in mixes {
     31             x += """
     32                 <mix id="\(mixID(g))"><name>\(g)</name><o2>\(n(g.o2, 3))</o2><n2>\(n(g.n2, 3))</n2><he>0.000</he></mix>
     33 
     34             """
     35         }
     36         x += "  </gasdefinitions>\n  <profiledata>\n    <repetitiongroup id=\"rg1\">\n"
     37         for d in dives {
     38             x += "      <dive id=\"dive\(d.number)\">\n        <informationbeforedive>\n"
     39             x += "          <divenumber>\(d.number)</divenumber>\n"
     40             x += "          <datetime>\(iso.string(from: d.start))</datetime>\n"
     41             x += "          <atmosphericpressure>\(n(d.surfacePressure * 100_000, 0))</atmosphericpressure>\n"
     42             if let si = d.surfaceIntervalBefore {
     43                 x += "          <surfaceintervalbeforedive><passedtime>\(n(si * 60, 0))</passedtime></surfaceintervalbeforedive>\n"
     44             } else {
     45                 x += "          <surfaceintervalbeforedive><infinity/></surfaceintervalbeforedive>\n"
     46             }
     47             x += "        </informationbeforedive>\n        <samples>\n"
     48             for (i, s) in d.samples.enumerated() {
     49                 x += "          <waypoint><divetime>\(n(s.time, 0))</divetime><depth>\(n(s.depth))</depth>"
     50                 if let t = s.temperature { x += "<temperature>\(kelvin(t))</temperature>" }
     51                 if i == 0 { x += "<switchmix ref=\"\(mixID(d.gas))\"/>" }
     52                 if let sd = s.stopDepth, let sm = s.stopMinutes {
     53                     x += "<decostop kind=\"mandatory\" decodepth=\"\(n(sd, 1))\" duration=\"\(sm * 60)\"/>"
     54                 } else if let ndl = s.noDecompressionLimit {
     55                     x += "<nodecotime>\(n(min(ndl, 999) * 60, 0))</nodecotime>"
     56                 }
     57                 for e in d.events where e.time == s.time { x += "<alarm>\(alarm(e.warning))</alarm>" }
     58                 x += "</waypoint>\n"
     59             }
     60             x += "        </samples>\n        <informationafterdive>\n"
     61             x += "          <greatestdepth>\(n(d.maxDepth))</greatestdepth>\n"
     62             x += "          <averagedepth>\(n(d.averageDepth))</averagedepth>\n"
     63             x += "          <diveduration>\(n(d.duration, 0))</diveduration>\n"
     64             if let t = d.minTemperature { x += "          <lowesttemperature>\(kelvin(t))</lowesttemperature>\n" }
     65             if let notes = d.notes ?? d.site { x += "          <notes><para>\(esc(notes))</para></notes>\n" }
     66             x += "        </informationafterdive>\n      </dive>\n"
     67         }
     68         x += "    </repetitiongroup>\n  </profiledata>\n</uddf>\n"
     69         return x
     70     }
     71 
     72     static func alarm(_ w: DiveComputer.Warning) -> String {
     73         switch w {
     74         case .ascentFast, .ascentTooFast: "ascent"
     75         case .ceilingViolated: "deco"
     76         case .ppO2High, .ppO2Critical: "error"
     77         case .missedSafetyStop: "deco"
     78         case .depthLimit: "depth"
     79         case .lowNoDecompression: "deco"
     80         }
     81     }
     82 }