Package kenozooid :: Package cli :: Module calc

Source Code for Module kenozooid.cli.calc

  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  """ 
 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  } 
32 33 @inject(CLICommand, name='calc') 34 -class Calculate(object):
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
42 - def add_arguments(cls, parser):
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
76 - def __call__(self, args):
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 # vim: sw=4:et:ai 103