Skip to content
Snippets Groups Projects
Commit cae66a64 authored by David Hoese's avatar David Hoese
Browse files

added a few functions and array_convert function

parent 137ca102
No related branches found
No related tags found
No related merge requests found
...@@ -168,7 +168,7 @@ def dewpoint(tempC, relhum): ...@@ -168,7 +168,7 @@ def dewpoint(tempC, relhum):
def relhum(airTempK, dewpointTempK): def relhum(airTempK, dewpointTempK):
""" """
Algorithm from derived by David Hoese from the above Algorithm derived by David Hoese from the above
dewpoint(tempC, relhum) function, both parameters are in Kelvin units. dewpoint(tempC, relhum) function, both parameters are in Kelvin units.
:param airTempK: air temperature in Kelvin :param airTempK: air temperature in Kelvin
...@@ -242,8 +242,15 @@ def linearConvert(data, factor=1, offset=0): ...@@ -242,8 +242,15 @@ def linearConvert(data, factor=1, offset=0):
return data * float(factor) + offset return data * float(factor) + offset
def mm2in(val): def mm2in(val):
"""Convert millimeters to inches.
"""
return linearConvert(val, 0.0393700787) return linearConvert(val, 0.0393700787)
def in2mm(val):
"""Convert inches to millimeters.
"""
return linearConvert(val, 1/0.0393700787)
def c2f (val): def c2f (val):
"""Degrees celsius to fahrenheit. """Degrees celsius to fahrenheit.
...@@ -254,6 +261,16 @@ def c2f (val): ...@@ -254,6 +261,16 @@ def c2f (val):
""" """
return linearConvert(val, (9/5.), 32) return linearConvert(val, (9/5.), 32)
def f2c(val):
"""Degrees fahrenheit to celsius.
>>> f2c(32)
0.0
>>> f2c(212)
100.0
"""
return linearConvert(val-32, (5/9.), 0)
def mps2mph (val): def mps2mph (val):
"""Speed in meters per second to miles per hour. """Speed in meters per second to miles per hour.
""" """
...@@ -264,6 +281,11 @@ def mps2knots (val): ...@@ -264,6 +281,11 @@ def mps2knots (val):
""" """
return linearConvert(val, 1.9438445) return linearConvert(val, 1.9438445)
def knots2mps(val):
"""Speed in knots to meters per second.
"""
return linearConvert(val, 1/1.9438445)
def altimeter(p, alt): def altimeter(p, alt):
"""Compute altimeter from pressure and altitude. """Compute altimeter from pressure and altitude.
...@@ -308,7 +330,58 @@ def dir2txt (val): ...@@ -308,7 +330,58 @@ def dir2txt (val):
if val >= i and val < (i + 22.5): if val >= i and val < (i + 22.5):
return dir return dir
i += 22.5 i += 22.5
def array_convert(arr, sunits, cunits):
"""Converts the data in array arr from sunits to cunits.
:param arr: Array to be converted
:type arr: ndarray
:param sunits: Starting Units = 'C', 'F', 'm/s', 'knots', 'hpa', 'deg', 'in', 'mm', 'w/m2'
:type sunits: string
:param cunits: Convert to Units = Same choices as sunits
:type cunits: string
"""
if sunits == cunits: return arr
if type(arr) != ndarray: raise ValueError("Array must be of type numpy.ndarray")
shape = arr.shape
a = arr.flatten()
if sunits == 'c':
if cunits == 'f':
for i in range(len(a)):
a[i] = c2f(a[i])
return a.reshape(shape)
elif sunits == 'f':
if cunits == 'c':
for i in range(len(a)):
a[i] = f2c(a[i])
return a.reshape(shape)
elif sunits == 'm/s':
if cunits == 'knots':
for i in range(len(a)):
a[i] = mps2knots(a[i])
return a.reshape(shape)
elif sunits == 'knots':
if cunits == 'm/s':
for i in range(len(a)):
a[i] = knots2mps(a[i])
return a.reshape(shape)
elif sunits == 'hpa':
return a.reshape(a)
elif sunits == 'deg':
return a.reshape(a)
elif sunits == 'in':
if cunits == 'mm':
for i in range(len(a)):
a[i] = in2mm(a[i])
return a.reshape(shape)
elif sunits == 'mm':
if cunits == 'in':
for i in range(len(a)):
a[i] = mm2in(a[i])
return a.reshape(shape)
elif sunits == 'w/m2':
return a.reshape(a)
raise ValueError("sunits or cunits was not an acceptable unit")
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment