Exemple #1
0
    def iter_dives(self):
        """
        Add all known dives to the UDCF document.
        """
        previous_divetime = datetime.min
        group = self._add(self.doc, 'repgroup')

        for dive in self.divelog.dives(numbers=self.args):
            # Compute the SI and start a new group if INF
            divetime = datetime.strptime(dive.dive_datetime, '%Y-%m-%d %H:%M:%S')
            surfaceinterval = divetime - previous_divetime
            dive_group = self._add(group, 'dive')

            self._add(dive_group, 'date', subfields={'year': divetime.year, 'month': divetime.month, 'day': divetime.day})
            self._add(dive_group, 'time', subfields={'hour': divetime.hour, 'minute': divetime.minute})

            if surfaceinterval > SI_INF:
                self._add(dive_group, 'surface_interval', subfields={'infinity': None})
            else:
                self._add(dive_group, 'surface_interval', subfields={'passedtime': surfaceinterval.days * 24 * 60 * 60 + surfaceinterval.seconds}) # .total_seconds in 2.7...

            if celcius_to_kelvin(dive.dive_mintemp) > 0:
                if self.preferences.depth_unit == 'm':
                    self._add(dive_group, 'temperature', dive.dive_mintemp)
                else:
                    self._add(dive_group, 'temperature', celcius_to_fahrenheit(dive.dive_mintemp))
            self._add(dive_group, 'density', text=1030.0)
            self._add(dive_group, 'altitude', text=0.0)

            # FIXME:...
            gases_group = self._add(dive_group, 'gases')
            mix_group = self._add(gases_group, 'mix', subfields={'mixname': 1, 'o2': 0.21, 'n2': 0.79, 'he': 0.0})
            tank_group = self._add(mix_group, 'tank', subfields={'tankvolume': 10, 'pstart': 250, 'pend': 30})

            if dive.site_id > 0:
                self._add(dive_group, 'place', text=self.divelog.site_name(dive.site_id))

            self._add(dive_group, 'timedepthmode')
            sample_group = self._add(dive_group, 'samples', subfields={'switch': 1})
            self._add(sample_group, 't', text=0)
            self._add(sample_group, 'd', text=0)
            for sample in self.divelog.samples(dive.dive_id):
                self._add(sample_group, 't', text=sample.profile_time)
                self._add(sample_group, 'd', text=sample.profile_depth)
            self._add(sample_group, 't')
            self._add(sample_group, 'd', text=0)

            previous_divetime = divetime

        yield self.top
Exemple #2
0
    def _add_dive(self, repititongroup, surfaceinterval, dive, dive_trips):
        """
        This adds a single <dive> tag to the <repetitiongroup> given.
        """
        divetime = datetime.strptime(dive.dive_datetime, '%Y-%m-%d %H:%M:%S')
        dive_group = self._add(repititongroup, 'dive', attr={'id': _dive_ref(dive.dive_id)})
        pre_info_group = self._add(dive_group, 'informationbeforedive')
        post_info_group = self._add(dive_group, 'informationafterdive')
        self._add(pre_info_group, 'dive_number', text=dive.dive_number)
        if self.options.trip_si_threshold:
            if surfaceinterval > timedelta(days=self.options.trip_si_threshold):
                dive_trips.append([dive.dive_id])
            else:
                dive_trips[-1].append(dive.dive_id)

        self._add(pre_info_group, 'datetime', divetime.isoformat())
        if surfaceinterval > SI_INF:
            self._add(pre_info_group, 'surfaceintervalbeforedive', subfields={'infinity': None})
        else:
            self._add(pre_info_group, 'surfaceintervalbeforedive', subfields={'passedtime': surfaceinterval.days * 24 * 60 * 60 + surfaceinterval.seconds}) # .total_seconds in 2.7...
        self._add(pre_info_group, 'apparatus', 'open-scuba') # gdivelog doesn't do anything else...

        self._add(dive_group, 'altitude', text=0)
        self._add(dive_group, 'density', text=1030)

        if dive.dive_mintemp:
            # FIXME: check temperature units
            self._add(post_info_group, 'lowesttemperature', celcius_to_kelvin(dive.dive_mintemp))
        self._add_text_paragraphs(post_info_group, 'notes', dive.dive_notes)
        self._add(post_info_group, 'diveduration', dive.dive_duration)
        self._add(post_info_group, 'greatestdepth', dive.dive_maxdepth)

        # mix_switch_times is a list of (starttime, mixref), so while traversing dive times for the waypoint samples, we can pop off elements as switches are made.
        mix_switch_times = []
        for dive_tank in self.db.dive_tanks(diveid=dive.dive_id):
            if dive_tank.dive_tank_stime >= 0 and dive_tank.dive_tank_etime > 0:
                mix_switch_times.append((dive_tank.dive_tank_stime, _mix_ref(dive_tank)))
            tank_group = self._add(dive_group, 'tankdata')
            self._add(tank_group, 'link', attr={'ref': _tank_ref(dive_tank.tank_id)})
            self._add(tank_group, 'link', attr={'ref': _mix_ref(dive_tank)})
            tank = self.db.tank_by_id(dive_tank.tank_id)
            self._add(tank_group, 'volume', _volume_for_tank(self.preferences, tank))
            # FIXME: convert to pascal
            self._add(tank_group, 'tankpressurebegin', dive_tank.dive_tank_spressure)
            self._add(tank_group, 'tankpressureend', dive_tank.dive_tank_epressure)
        # Ensure they are sorted by divetime.
        mix_switch_times = sorted(mix_switch_times, key=lambda e: e[0])

        if mix_switch_times:
            mix_switch_times[0] = (0, mix_switch_times[0][1])
        else:
            # http://www.streit.cc/extern/uddf_v320/en/waypoint.html. First waypoint must have a
            # switchmix, so if we have no switchtimes, add a mix_air switch.
            mix_switch_times = [(0, 'mix_air')]

        if dive.site_id > 0:
            self._add(dive_group, 'link', attr={'ref': _site_ref(dive.site_id)})

        for buddy in self.db.buddies(diveid=dive.dive_id):
            self._add(dive_group, 'link', attr={'ref': _buddy_ref(buddy.buddy_id)})

        equipment_group = self._add(dive_group, 'equipmentused')
        if dive.dive_weight > 0.0:
            self._add(equipment_group, 'leadquantity', dive.dive_weight)
        for equipment in self.db.equipment(diveid=dive.dive_id):
            self._add(equipment_group, 'link', attr={'ref': _equipment_ref(equipment.equipment_id)})

        sample_group = self._add(dive_group, 'samples')
        for sample in self.db.samples(dive.dive_id):
            waypoint = self._add(sample_group, 'waypoint', subfields={'divetime': sample.profile_time, 'depth': sample.profile_depth})
            if mix_switch_times and sample.profile_time >= mix_switch_times[0][0]:
                self._add(waypoint, 'switchmix', attr={'ref': mix_switch_times[0][1]})
                mix_switch_times.pop(0)
            # FIXME: check temperature units
            k = celcius_to_kelvin(sample.profile_temperature)
            if k > 0:
                self._add(waypoint, 'temperature', k)