-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpressure
executable file
·242 lines (186 loc) · 6.59 KB
/
pressure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python3
#
# pressure: Show a given pressure in many units.
# 2015-10-28: Written by Steven J. DeRose.
#
import sys
import argparse
__metadata__ = {
"title" : "pressure",
"description" : "Show a given pressure in many units.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2015-10-28",
"modified" : "2020-08-31",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__['modified']
descr = """
=Description=
Given a pressure as a number and a unit name, display the equivalent pressure
in lots of other unitsOfPressure.
==Usage==
pressure 2.0 atmStd
will produce:
You have: 2.000000 atmStd, which equals:
0.202650 MPa
202650.054766 Pa
2.000000 atmStd
2.066463 atmTech
2.026501 bar
2066.512360 cmH2O
152.000005 cmHg
67.797184 ftH2O
4.986873 ftHg
2066.463138 g/cm2
2026.500548 hPa
813.587713 inH2O
59.842521 inHg
202.650055 kPa
2.066463 kg/cm2
2026.500548 mbar
20665.123599 mmH2O
1520.000051 mmHg
1520000.050663 mtorr
4232.427454 psf
29.391906 psi
1520.000051 torr
=Related Commands=
The *nix ''unitsOfPressure'' command can do compound unitsOfPressure like:
unitsOfPressure 9.8 m/s^2 feet/s^2
unitsOfPressure 14 psi cm water
=References=
[http://www.sensorsone.com/kpa-kilopascal-pressure-unit]
=Known bugs and Limitations=
Should probably just learn more other unitsOfPressure; maybe just harvest C<unitsOfPressure>'s
F</usr/share/misc/unitsOfPressure.lib> list.
Contains table of flow rate units, should expose that.
With --format, you may need to double the leading "%".
=To do=
* Option to request a particular output unit(s).
=Licensing=
Copyright 2015 by Steven J. DeRose. This script is licensed under a
Creative Commons Attribution-Share-alike 3.0 unported license.
See http://creativecommons.org/licenses/by-sa/3.0/ for more information.
=History=
* Written 2010-04-01~23 by Steven J. DeRose (originally in Perl).
* 2020-04-08: New laout.
* 2020-05-01: Add --tableFrom.
* 2020-08-31: New layout.
=Rights=
Copyright 2015, Steven J. DeRose. This work is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see [http://creativecommons.org/licenses/by-sa/3.0/].
For the most recent version, see [http://www.derose.net/steve/utilities] or
[http://github/com/sderose].
=Options=
"""
unitsOfPressure = {
# 1.0 kPa is equal to:
"kPa": 1.000,
"MPa": 0.001,
"hPa": 10.000,
"Pa": 1000.000,
"atmTech": 0.0101972,
"atmStd": 0.00986923,
"bar": 0.010,
"mbar": 10.000,
"inHg": 0.2952998,
"ftHg": 0.0246083,
"mmHg": 7.50061505,
"cmHg": 0.750061505,
"inH2O": 4.01474213,
"ftH2O": 0.334553,
"mmH2O": 101.97442889,
"cmH2O": 10.197442889,
"torr": 7.50061505,
"mtorr": 7500.61505,
"psi": 0.14503774,
"psf": 20.8854,
"g/cm2": 10.1972,
"kg/cm2": 0.0101972,
}
unitNames = sorted(unitsOfPressure.keys())
unitsOfFlow = { # 1.0 lpm is equal to:
"lpm": 1.0,
"lps": 0.0167,
"cfm": 0.0353,
"cms": 1.67E-5,
"gpm": 0.26417,
"gph": 0.0044028,
}
def from_kPa(f, targetUnitName):
return f * unitsOfPressure[targetUnitName]
def to_kPa(f, sourceUnitName):
return f / unitsOfPressure[sourceUnitName]
def from_to(f, sourceUnitName, targetUnitName):
m = multiplierForUnit1ToGetUnit2(sourceUnitName, targetUnitName)
return f * m
def multiplierForUnit1ToGetUnit2(u1, u2):
return unitsOfPressure[u2]/unitsOfPressure[u1]
###############################################################################
#
def processOptions():
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--divide", action='store_true',
help='With --tableFrom, show what to divide by, not multiply by.')
parser.add_argument(
"--format", type=str, default="%%16.6f",
help='Use this format to output the numbers. Default: "%%16.6f"')
parser.add_argument(
"--quiet", "-q", action='store_true',
help='Suppress most messages.')
parser.add_argument(
"--tableFrom", action='store_true',
help='Generate a Python dict of multipliers to convert the unit.')
parser.add_argument(
"--to", "-t", type=str, choices=unitsOfPressure.keys(),
help='Only report this target unit.')
parser.add_argument(
"--verbose", "-v", action='count', default=0,
help='Add more messages (repeatable).')
parser.add_argument(
"--version", action='version', version=__version__,
help='Display version information, then exit.')
parser.add_argument(
'p', type=float,
help='pressure')
parser.add_argument(
'unit', type=str, choices=unitNames,
help='pressure unit')
args0 = parser.parse_args()
return(args0)
###############################################################################
# Main
#
args = processOptions()
if (args.tableFrom):
kw = "Divide" if (args.divide) else "Multiply"
print(" # %s %s by these values to get to other units" %
(kw, args.unit))
print(" from_%s_to = {" % (args.unit))
lineFormat = " %-12s : " + args.format + ","
for targetUnit in unitNames:
mfactor = multiplierForUnit1ToGetUnit2(args.unit, targetUnit)
if (args.divide):
print(lineFormat % ('"'+targetUnit+'"', 1/mfactor))
else:
print(lineFormat % ('"'+targetUnit+'"', mfactor))
print(" }")
else:
print("You have: %f %s, which equals:" % (args.p, args.unit))
kPa = to_kPa(args.p, args.unit)
lineFormat = " " + args.format + " %s"
for u in unitNames:
if (not args.to or args.to == u):
print(lineFormat % (kPa*unitsOfPressure[u], u))
sys.exit()