forked from LBNL-ETA/MSWH
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit_converters.py
464 lines (343 loc) · 12.3 KB
/
unit_converters.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import numpy as np
import pandas as pd
import logging
log = logging.getLogger(__name__)
class UnitConv(object):
"""Unit conversions using conversion parameters from
ASHRAE Fundamentals 2017.
Parameters:
x_in: float, array
Input value to be converted to a desired unit
scale_in: str or 1.
Scale of the input value, options: 'k', 'kilo',
'mega', 'million', 'M', 'MM', 'giga', 'G', 'tera', 'T',
'peta', 'P', 'milli', 'micro'.
Default: 1.
scale_out: str or 1.
Scale of the input value, options: 'k', 'kilo',
'mega', 'million', 'M', 'MM', 'giga', 'G', 'tera', 'T',
'peta', 'P', 'milli', 'm', 'micro'.
Default: 1.
Examples:
To convert temperature from degF to degC
>>> t_in_degC = UnitConv(t_in_degF).degF_degC(unit_in='degF')
To convert power in hp to kW:
>>> p_in_kW = UnitConv(p_in_hp, scale_out='kilo').hp_W(unit_in='hp')
To convert energy from GJ to MMBtu:
>>> e_MMBtu = UnitConv(e_GJ, scale_in='G', scale_out='MM').Btu_J(unit_in='J')
"""
def __init__(self, x_in, scale_in=1.0, scale_out=1.0):
self.x_in = x_in
if (scale_in == "k") or (scale_in == "kilo"):
self.scale_in = 1000.0
elif (
(scale_in == "million")
or (scale_in == "M")
or (scale_in == "MM")
or (scale_in == "mega")
):
self.scale_in = 1e6
elif (scale_in == "giga") or (scale_in == "G"):
self.scale_in = 1e9
elif (scale_in == "tera") or (scale_in == "T"):
self.scale_in = 1e12
elif (scale_in == "peta") or (scale_in == "P"):
self.scale_in = 1e15
elif (scale_in == "milli") or (scale_in == "m"):
self.scale_in = 1e-3
elif scale_in == "micro":
self.scale_in = 1e-6
else:
self.scale_in = scale_in
if (scale_out == "k") or (scale_out == "kilo"):
self.scale_out = 1000.0
elif (
(scale_out == "million")
or (scale_out == "M")
or (scale_out == "MM")
or (scale_out == "mega")
):
self.scale_out = 1e6
elif (scale_out == "giga") or (scale_out == "G"):
self.scale_out = 1e9
elif (scale_out == "tera") or (scale_out == "T"):
self.scale_out = 1e12
elif (scale_out == "peta") or (scale_out == "P"):
self.scale_out = 1e15
elif (scale_out == "milli") or (scale_out == "m"):
self.scale_out = 1e-3
elif scale_out == "micro":
self.scale_out = 1e-6
else:
self.scale_out = scale_out
def degF_degC(self, unit_in="degF"):
"""Converts temperature between degree Fahrenheit and Celsius
Parameters:
unit_in: string, options: 'degF', 'degC'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "degF":
x_out = (self.x_in - 32.0) * (5.0 / 9.0)
elif unit_in == "degC":
x_out = (self.x_in * 9.0 / 5.0) + 32.0
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def degC_K(self, unit_in="degC"):
"""Converts temperature between degree Celsius and Kelvin
Parameters:
unit_in: string, options: 'K', 'degC'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
abs_zero = 273.15
if unit_in == "K":
x_out = self.x_in - abs_zero
elif unit_in == "degC":
x_out = self.x_in + abs_zero
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def m3_gal(self, unit_in="gal"):
"""Converts volume between cubic meter and gallon
Parameters:
unit_in: string, options: 'm3', 'gal'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "gal":
x_out = self.x_in * 0.003785412
elif unit_in == "m3":
x_out = self.x_in * (1.0 / 0.003785412)
x_out /= self.scale_out
return x_out
def hp_W(self, unit_in="hp"):
"""Converts power between watt and horsepower
Parameters:
unit_in: string, options: 'hp', 'W'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "hp":
x_out = self.x_in * 745.7
elif unit_in == "W":
x_out = self.x_in * 1.0 / 745.7
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def Btu_J(self, unit_in="Btu"):
"""Converts work / energy / heat content between Btu and joule
Parameters:
x: float, array
Input value
unit_in: string, options: 'Btu', 'J'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "Btu":
x_out = self.x_in * 1055.056
elif unit_in == "J":
x_out = self.x_in * (1.0 / 1055.056)
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def therm_J(self, unit_in="therm"):
"""Converts work / energy / heat content between therm and joule
Parameters:
x: float, array
Input value
unit_in: string, options: 'therm', 'J'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "therm":
x_out = self.x_in * 105.5 * 1e6
elif unit_in == "J":
x_out = self.x_in / (1e6 * 105.5)
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def Wh_J(self, unit_in="J"):
"""Converts work / energy / heat content between watthour and joule
Parameters:
x: float, array
Input value
unit_in: string, options: 'Wh', 'J'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "Wh":
x_out = self.x_in * 3600.0
elif unit_in == "J":
x_out = self.x_in / 3600.0
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def m3perh_m3pers(self, unit_in="m3perh"):
"""Converts volume flow between cubic meter
per hour and cubic meter per second
Parameters:
x: float, array
Input value
unit_in: string, options: 'Wh', 'J'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "m3perh":
x_out = self.x_in / 3600.0
elif unit_in == "m3pers":
x_out = self.x_in * 3600.0
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def sqft_m2(self, unit_in="sqft"):
"""Converts area between square foot
and square meter
Parameters:
x: float, array
Input value
unit_in: string, options: 'Wh', 'J'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "sqft":
x_out = self.x_in * (0.3048 ** 2)
elif unit_in == "m2":
x_out = self.x_in / (0.3048 ** 2)
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
def ft_m(self, unit_in="ft"):
"""Converts length between foot
and meter
Parameters:
x: float, array
Input value
unit_in: string, options: 'Wh', 'J'
Unit of the input value
Returns:
x_out: float, array
Output value
"""
self.x_in *= self.scale_in
if unit_in == "ft":
x_out = self.x_in * 0.3048
elif unit_in == "m":
x_out = self.x_in / 0.3048
else:
msg = "User provided an unsupported input unit {}."
log.error(msg.format(unit_in))
raise ValueError
x_out /= self.scale_out
return x_out
class Utility(object):
"""Converts gas or electricity
consumption into commonly used units.
Parameters:
quantity_in: float, array
Quantity to be converted.
E.g. gas use in kJ
"""
def __init__(self, quantity_in):
self.quantity_in = quantity_in
# Gas properties
# heating value ASHRAE fundamentals 28.3.
# usual range is 37.3 to 39.1 MJ/m3 at sea level
# table 3: methane= 37.7, ethane 66.1 [MJ/m3]
self.hea_val_gas = 38.0 # [MJ/m3]
def gas(self, unit_in="kJ", unit_out="MMBtu"):
"""Converts gas consumption.
Parameters:
unit_in: string
Units of the input quantity that needs
to be converted.
Options: 'kWh', 'kJ'
unit_out: string
Desired output unit
Options: 'm3', 'cf', 'therm', 'MMBtu'
Returns:
gas_use: float
Gas use in output units
"""
if unit_in == "kWh":
gas_use = UnitConv(
self.quantity_in, scale_in="k", scale_out="k"
).Wh_J(unit_in="Wh")
self.quantity_in = gas_use + 0.0
unit_in = "kJ"
if unit_in == "kJ":
if unit_out == "m3":
gas_use = self.quantity_in / (self.hea_val_gas * 1000.0)
elif unit_out == "cf":
gas_use = self.quantity_in / (
0.02832 * self.hea_val_gas * 1000.0
)
elif unit_out == "therm":
gas_use = UnitConv(self.quantity_in, scale_in="k").therm_J(
unit_in="J"
)
elif unit_out == "MMBtu":
gas_use = UnitConv(
self.quantity_in, scale_in="k", scale_out="MM"
).Btu_J(unit_in="J")
else:
raise ValueError(
"{} is not yet supported as output unit".format(unit_out)
)
if unit_in != "kWh" and unit_in != "kJ":
raise ValueError(
"{} is not yet supported as input unit.".format(unit_in)
)
return gas_use