1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """
21 Kenozooid calculator commands.
22 """
23
24 from kenozooid.cli import CLICommand, ArgumentError, NoCommandError
25 from kenozooid.component import inject
26
27 COMMANDS = {
28 'ppO2': ('pp_o2', 'calculate O2 partial pressure (ppO2)'),
29 'ppN2': ('pp_n2', 'calculate Nitrogen partial pressure (ppN2)'),
30 'ead': ('ead', 'calculate equivalent air depth (EAD)'),
31 }
35 """
36 Kenozooid calculator command implementing various diving related
37 calculators.
38 """
39 description = 'air and nitrox calculations (partial pressure, EAD, MOD); metric units'
40
41 @classmethod
43 """
44 Parse calculator commands arguments.
45
46 TODO: Add imperial/metric units support.
47 """
48 subp = parser.add_subparsers(title='Calculator commands')
49
50 for cmd, (_, d) in COMMANDS.items():
51 p = subp.add_parser(cmd, help=d)
52 p.set_defaults(calc=cmd, subcmd='calc', parser=p)
53 p.add_argument('depth', type=float, nargs=1)
54 p.add_argument('ean', type=float, nargs=1)
55
56 p = subp.add_parser('mod',
57 help='calculate maximum operating depth (MOD)')
58 p.set_defaults(calc='mod')
59 p.add_argument('pp', type=float, nargs='?', default=1.4)
60 p.add_argument('ean', type=float, nargs=1)
61
62 p = subp.add_parser('rmv',
63 help='calculate respiratory minute volume (RMV)')
64 p.set_defaults(calc='rmv')
65 p.add_argument('tank', type=float, nargs=1,
66 help='tank size in liters, i.e. 12, 15')
67 p.add_argument('pressure', type=float, nargs=1,
68 help='pressure difference between start and end of dive,' \
69 ' i.e. 170 (220 - 50)')
70 p.add_argument('depth', type=float, nargs=1,
71 help='average depth of dive')
72 p.add_argument('duration', type=float, nargs=1,
73 help='duration of dive in minutes')
74
75
77 """
78 Execute Kenozooid calculator command.
79 """
80 import kenozooid.calc as kcalc
81
82 if not hasattr(args, 'calc'):
83 raise NoCommandError(args.parser)
84
85 calc = args.calc
86
87 if calc in COMMANDS:
88 f = getattr(kcalc, COMMANDS[calc][0])
89 result = f(args.depth[0], args.ean[0])
90 elif calc == 'mod':
91 result = kcalc.mod(args.ean[0], args.pp)
92 elif calc == 'rmv':
93 result = kcalc.rmv(
94 args.tank[0], args.pressure[0], args.depth[0], args.duration[0]
95 )
96 else:
97 assert False
98
99 print('{0:.2f}'.format(result))
100
101
102
103