diff --git a/Controller/common_functions.py b/Controller/common_functions.py new file mode 100644 index 0000000000000000000000000000000000000000..2473b5702edda9d890f93678fb8e2909a0745fcd --- /dev/null +++ b/Controller/common_functions.py @@ -0,0 +1,90 @@ +import numpy as np +import scipy.stats as sp +# from decimal import Decimal +from sigfig import round as sigfig_round + +def units_conversion(units_id="SI"): + """Computes the units conversion from SI units used internally to the + desired display units. + + Parameters + ---------- + units_id: str + String variable identifying units (English, SI) SI is the default. + + Returns + ------- + units: dict + dictionary of unit conversion and labels + """ + + if units_id == "SI": + units = { + "L": 1, + "Q": 1, + "A": 1, + "V": 1, + "label_L": "m", + "label_Q": "m3/s", + "label_A": "m2", + "label_V": "m/s", + "ID": "SI", + } + + else: + units = { + "L": 1.0 / 0.3048, + "Q": (1.0 / 0.3048) ** 3, + "A": (1.0 / 0.3048) ** 2, + "V": 1.0 / 0.3048, + "label_L": "ft", + "label_Q": "ft3/s", + "label_A": "ft2", + "label_V": "ft/s", + "ID": "English", + } + + return units + + +def convert_temperature(temp_in, units_in, units_out) -> float: + """Converts temperature from F to C or C to F. + + Parameters + ========== + temp_in: np.array + temperature in units_in + units_in: str + C for Celcius or F for Fahrenheit + units_out: str + C for Celcius or F for Fahrenheit + + Returns + ======= + temp_out: np.array + temperature in units_out + """ + + temp_out = None + if units_in == "F": + if units_out == "C": + temp_out = (temp_in - 32) * (5.0 / 9.0) + else: + temp_out = temp_in + + elif units_in == "C": + if units_out == "C": + temp_out = temp_in + else: + temp_out = (temp_in * (9.0 / 5.0)) + 32 + + return temp_out + + +def scientific_notation(n, sig: int = 3): + if np.isnan(n): + n = 0 + elif np.isinf(n): + return n + + return sigfig_round(n, sig) \ No newline at end of file