# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# Author: Markus Ritschel# eMail: git@markusritschel.de# Date: 2024-03-03# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#importloggingimportpandasaspdimportnumpyasnpfromcopyimportcopyfrom.utilsimportorder_of_magnitudelog=logging.getLogger(__name__)
[docs]defpressure2atm(p):"""Convert pressure given in hPa, Pa or atm into atm. Examples -------- >>> pressure2atm(1013.25) 1.0 >>> pressure2atm(101325) 1.0 >>> pressure2atm(2) 2 >>> pressure2atm(pd.Series([1013.25, 1024.0])) 0 1.000000 1 1.010609 dtype: float64 """p=copy(p)if2<=np.nanmedian(np.rint(order_of_magnitude(p)))<=3:p/=1013.25log.info("Pressure is assumed to be in hPa and was converted to atm")elif4<=np.nanmedian(np.rint(order_of_magnitude(p)))<=5:p/=101325log.info("Pressure is assumed to be in Pa and was converted to atm")elif-1<=np.nanmedian(np.rint(order_of_magnitude(p)))<=1:log.info("Pressure is assumed to be already in atm (no conversion)")else:raiseIOError("Pressure must be given in hPa, Pa or atm")returnp
[docs]defpressure2mbar(p):"""Convert pressure given in hPa, Pa or atm into mbar (or hPa). Examples -------- >>> pressure2mbar(1013) 1013 >>> pressure2mbar(101300) 1013.0 >>> pressure2mbar(1.0) 1013.25 >>> pressure2mbar(pd.Series([1.013, 2.034])) 0 1026.42225 1 2060.95050 dtype: float64 """p=copy(p)if2<=np.nanmedian(np.rint(order_of_magnitude(p)))<=3:log.info("Pressure is assumed to be already in mbar (no conversion)")elif4<=np.nanmedian(np.rint(order_of_magnitude(p)))<=5:p/=100log.info("Pressure is assumed to be in Pa and was converted to mbar (hPa)")elif-1<=np.nanmedian(np.rint(order_of_magnitude(p)))<=1:log.info("Pressure is assumed to be in atm and was converted to mbar (hPa)")p*=1013.25else:raiseIOError("Pressure must be given in hPa, Pa or atm")returnp
[docs]deftemperature2K(T):"""Convert temperatures given in °C into Kelvin. If `T` is a :class:`pandas.Series` object, only values larger than 200 are converted. All others are assumed to be already in Kelvin. Examples -------- >>> temperature2K(10) 283.15 """T=copy(T)ifisinstance(T,pd.Series):ifany(T>200):log.warning("Some values seem to be already in Kelvin")T[T<200]+=273.15elifT<200:T+=273.15returnT
[docs]deftemperature2C(T):"""Convert temperatures given in Kelvin into °C. If `T` is a :class:`pandas.Series` object, only values less than 200 are converted. All others are expected to be already in °C. Examples -------- >>> temperature2C(283.15) 10.0 """T=copy(T)ifisinstance(T,pd.Series):T.loc[T>200]-=273.15elifT>200:T-=273.15returnT