-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made conversion functions to shorten code
- Loading branch information
Showing
2 changed files
with
113 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
def convert_time(data, key): | ||
""" | ||
Convert the time from the input data to hours. | ||
Args: | ||
data (dict): The input data. | ||
key (str): The key for the time in the input data. | ||
Returns: | ||
float: The time in hours. | ||
""" | ||
time = None | ||
for unit in ['sec', 'min', 'hour', 'day']: | ||
if f'{key} [{unit}]' in data: | ||
time_value = float(data[f'{key} [{unit}]']) | ||
if unit == 'sec': | ||
time = time_value / 3600 | ||
elif unit == 'min': | ||
time = time_value / 60 | ||
elif unit == 'hour': | ||
time = time_value | ||
elif unit == 'day': | ||
time = time_value * 24 | ||
break | ||
return time | ||
|
||
def convert_pressure(data, key): | ||
""" | ||
Convert the pressure from the input data to atmospheres. | ||
Args: | ||
data (dict): The input data. | ||
key (str): The key for the pressure in the input data. | ||
Returns: | ||
float: The pressure in atmospheres. | ||
""" | ||
pressure = None | ||
for unit in ['Pa', 'atm', 'bar', 'kPa', 'hPa', 'mbar']: | ||
if f'{key} [{unit}]' in data: | ||
pressure_value = float(data[f'{key} [{unit}]']) | ||
if unit == 'Pa': | ||
pressure = pressure_value / 101325 | ||
elif unit == 'atm': | ||
pressure = pressure_value | ||
elif unit == 'bar': | ||
pressure = pressure_value * 0.986923 | ||
elif unit == 'kPa': | ||
pressure = pressure_value / 101.325 | ||
elif unit == 'hPa' or unit == 'mbar': | ||
pressure = pressure_value / 1013.25 | ||
break | ||
return pressure | ||
|
||
def convert_temperature(data, key): | ||
""" | ||
Convert the temperature from the input data to Kelvin. | ||
Args: | ||
data (dict): The input data. | ||
key (str): The key for the temperature in the input data. | ||
Returns: | ||
float: The temperature in Kelvin. | ||
""" | ||
temperature = None | ||
for unit in ['K', 'C', 'F']: | ||
if f'{key} [{unit}]' in data: | ||
temperature_value = float(data[f'{key} [{unit}]']) | ||
if unit == 'K': | ||
temperature = temperature_value | ||
elif unit == 'C': | ||
temperature = temperature_value + 273.15 | ||
elif unit == 'F': | ||
temperature = (temperature_value - 32) * 5/9 + 273.15 | ||
break | ||
return temperature | ||
|
||
def convert_concentration(data, key): | ||
""" | ||
Convert the concentration from the input data to molecules per cubic centimeter. | ||
Args: | ||
data (dict): The input data. | ||
key (str): The key for the concentration in the input data. | ||
Returns: | ||
float: The concentration in molecules per cubic centimeter. | ||
""" | ||
concentration = None | ||
for unit in ['mol m-3', 'mol cm-3', 'molec m-3', 'molec cm-3']: | ||
if f'{key} [{unit}]' in data: | ||
concentration_value = float(data[f'{key} [{unit}]']) | ||
if unit == 'mol m-3': | ||
concentration = concentration_value | ||
elif unit == 'mol cm-3': | ||
concentration = concentration_value * 1e3 | ||
elif unit == 'molec m-3': | ||
concentration = concentration_value / 6.02214076e23 | ||
elif unit == 'molec cm-3': | ||
concentration = concentration_value * 1e3 / 6.02214076e23 | ||
break | ||
return concentration |