Package kenozooid :: Module data

Source Code for Module kenozooid.data

 1  # 
 2  # Kenozooid - dive planning and analysis toolbox. 
 3  # 
 4  # Copyright (C) 2009-2019 by Artur Wroblewski <wrobell@riseup.net> 
 5  # 
 6  # This program is free software: you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation, either version 3 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  # 
19   
20  from collections import namedtuple 
21  from operator import attrgetter 
22   
23  import kenozooid.util as ku 
24   
25 -def ntuple(name, fields):
26 """ 27 Create a named tuple with all fields set to ``None`` by default. 28 29 :Parameters: 30 name 31 Name of tuple. 32 fields 33 String containing space separated list of field names. 34 """ 35 t = namedtuple(name, fields) 36 k = len(t._fields) 37 df = k * (None, ) 38 dt = t(*df) 39 return dt._replace
40 41 42 Dive = ntuple('Dive', 'number datetime depth duration temp avg_depth mode profile' \ 43 ' equipment') 44 Sample = ntuple('Sample', 'depth time temp setpoint setpointby' \ 45 ' deco_time deco_depth alarm gas') 46 Gas = ntuple('Gas', 'id name o2 he depth') 47 BinaryData = ntuple('BinaryData', 'datetime data') 48 49
50 -def gas(o2, he, depth=0):
51 """ 52 Convert oxygen and helium values into Gas data record with id and name. 53 """ 54 id = 'air' 55 name = 'Air' 56 if o2 == 100: 57 id = 'o2' 58 name = 'O2' 59 elif o2 > 21 and he == 0: 60 id = 'ean{:02d}'.format(o2) 61 name = 'EAN{:02d}'.format(o2) 62 elif he > 0: 63 id = 'tx{:02d}{:02d}'.format(o2, he) 64 name = 'TX {:02d}/{:02d}'.format(o2, he) 65 return Gas(id=id, name=name, o2=o2, he=he, depth=depth)
66 67
68 -def sort_dives(dives):
69 """ 70 Sort dives by dive datetime. 71 """ 72 return sorted(dives, key=attrgetter('datetime'))
73 74
75 -def uniq_dives(dives):
76 """ 77 Remove duplicated dives from sorted iterator of dives. 78 """ 79 ld = None 80 for d in dives: 81 if ld != d.datetime: 82 yield d 83 ld = d.datetime
84 85 # vim: sw=4:et:ai 86