Spaces:
Sleeping
Sleeping
| """Unit conversion utilities for TSU-WAVE""" | |
| import numpy as np | |
| # Length conversions | |
| def m_to_km(meters: float) -> float: | |
| """Convert meters to kilometers""" | |
| return meters / 1000.0 | |
| def km_to_m(kilometers: float) -> float: | |
| """Convert kilometers to meters""" | |
| return kilometers * 1000.0 | |
| def m_to_ft(meters: float) -> float: | |
| """Convert meters to feet""" | |
| return meters * 3.28084 | |
| def ft_to_m(feet: float) -> float: | |
| """Convert feet to meters""" | |
| return feet / 3.28084 | |
| def m_to_miles(meters: float) -> float: | |
| """Convert meters to miles""" | |
| return meters / 1609.34 | |
| def miles_to_m(miles: float) -> float: | |
| """Convert miles to meters""" | |
| return miles * 1609.34 | |
| def m_to_nm(meters: float) -> float: | |
| """Convert meters to nautical miles""" | |
| return meters / 1852.0 | |
| def nm_to_m(nautical_miles: float) -> float: | |
| """Convert nautical miles to meters""" | |
| return nautical_miles * 1852.0 | |
| # Speed conversions | |
| def ms_to_knots(m_per_s: float) -> float: | |
| """Convert meters per second to knots""" | |
| return m_per_s * 1.94384 | |
| def knots_to_ms(knots: float) -> float: | |
| """Convert knots to meters per second""" | |
| return knots / 1.94384 | |
| def ms_to_kmh(m_per_s: float) -> float: | |
| """Convert meters per second to kilometers per hour""" | |
| return m_per_s * 3.6 | |
| def kmh_to_ms(km_per_h: float) -> float: | |
| """Convert kilometers per hour to meters per second""" | |
| return km_per_h / 3.6 | |
| def ms_to_mph(m_per_s: float) -> float: | |
| """Convert meters per second to miles per hour""" | |
| return m_per_s * 2.23694 | |
| def mph_to_ms(mph: float) -> float: | |
| """Convert miles per hour to meters per second""" | |
| return mph / 2.23694 | |
| # Temperature conversions | |
| def celsius_to_kelvin(celsius: float) -> float: | |
| """Convert Celsius to Kelvin""" | |
| return celsius + 273.15 | |
| def kelvin_to_celsius(kelvin: float) -> float: | |
| """Convert Kelvin to Celsius""" | |
| return kelvin - 273.15 | |
| def celsius_to_fahrenheit(celsius: float) -> float: | |
| """Convert Celsius to Fahrenheit""" | |
| return celsius * 9/5 + 32 | |
| def fahrenheit_to_celsius(fahrenheit: float) -> float: | |
| """Convert Fahrenheit to Celsius""" | |
| return (fahrenheit - 32) * 5/9 | |
| # Pressure conversions | |
| def pa_to_hpa(pascals: float) -> float: | |
| """Convert Pascals to hectopascals""" | |
| return pascals / 100.0 | |
| def hpa_to_pa(hectopascals: float) -> float: | |
| """Convert hectopascals to Pascals""" | |
| return hectopascals * 100.0 | |
| def pa_to_mbar(pascals: float) -> float: | |
| """Convert Pascals to millibars""" | |
| return pascals / 100.0 # 1 mbar = 1 hPa | |
| def mbar_to_pa(millibars: float) -> float: | |
| """Convert millibars to Pascals""" | |
| return millibars * 100.0 | |
| def pa_to_atm(pascals: float) -> float: | |
| """Convert Pascals to atmospheres""" | |
| return pascals / 101325.0 | |
| def atm_to_pa(atmospheres: float) -> float: | |
| """Convert atmospheres to Pascals""" | |
| return atmospheres * 101325.0 | |
| # Depth conversions (pressure to depth) | |
| def pressure_to_depth(pressure_pa: float, latitude: float = 45.0) -> float: | |
| """Convert pressure to ocean depth (simplified) | |
| Args: | |
| pressure_pa: pressure in Pascals | |
| latitude: latitude in degrees (for gravity correction) | |
| Returns: | |
| depth in meters | |
| """ | |
| g = 9.780327 * (1 + 0.0053024 * np.sin(np.radians(latitude))**2 - | |
| 0.0000058 * np.sin(2 * np.radians(latitude))**2) | |
| rho = 1025.0 # seawater density | |
| return pressure_pa / (rho * g) | |
| def depth_to_pressure(depth_m: float, latitude: float = 45.0) -> float: | |
| """Convert ocean depth to pressure | |
| Args: | |
| depth_m: depth in meters | |
| latitude: latitude in degrees | |
| Returns: | |
| pressure in Pascals | |
| """ | |
| g = 9.780327 * (1 + 0.0053024 * np.sin(np.radians(latitude))**2 - | |
| 0.0000058 * np.sin(2 * np.radians(latitude))**2) | |
| rho = 1025.0 | |
| return depth_m * rho * g | |
| # Time conversions | |
| def seconds_to_minutes(seconds: float) -> float: | |
| """Convert seconds to minutes""" | |
| return seconds / 60.0 | |
| def minutes_to_seconds(minutes: float) -> float: | |
| """Convert minutes to seconds""" | |
| return minutes * 60.0 | |
| def seconds_to_hours(seconds: float) -> float: | |
| """Convert seconds to hours""" | |
| return seconds / 3600.0 | |
| def hours_to_seconds(hours: float) -> float: | |
| """Convert hours to seconds""" | |
| return hours * 3600.0 | |
| # Wave parameters | |
| def period_to_frequency(period_s: float) -> float: | |
| """Convert wave period to frequency""" | |
| return 1.0 / period_s | |
| def frequency_to_period(frequency_hz: float) -> float: | |
| """Convert frequency to wave period""" | |
| return 1.0 / frequency_hz | |
| def wavelength_from_period(period_s: float, depth_m: float, g: float = 9.81) -> float: | |
| """Estimate wavelength from period and depth (shallow water approximation)""" | |
| return period_s * np.sqrt(g * depth_m) | |
| def celerity_from_depth(depth_m: float, g: float = 9.81) -> float: | |
| """Compute shallow water wave celerity""" | |
| return np.sqrt(g * depth_m) | |
| # Energy conversions | |
| def joules_to_kwh(joules: float) -> float: | |
| """Convert Joules to kilowatt-hours""" | |
| return joules / 3.6e6 | |
| def kwh_to_joules(kwh: float) -> float: | |
| """Convert kilowatt-hours to Joules""" | |
| return kwh * 3.6e6 | |
| # Unit testing | |
| def test_conversions(): | |
| """Test unit conversions""" | |
| assert abs(m_to_km(1000) - 1.0) < 1e-6 | |
| assert abs(km_to_m(1) - 1000) < 1e-6 | |
| assert abs(m_to_ft(1) - 3.28084) < 1e-5 | |
| assert abs(ft_to_m(3.28084) - 1) < 1e-5 | |
| assert abs(ms_to_knots(1) - 1.94384) < 1e-5 | |
| assert abs(knots_to_ms(1.94384) - 1) < 1e-5 | |
| assert abs(celsius_to_kelvin(0) - 273.15) < 1e-6 | |
| print("All unit tests passed!") | |
| if __name__ == "__main__": | |
| test_conversions() | |