From cae66a64e71428bf202e3411cf016346e75a9211 Mon Sep 17 00:00:00 2001
From: David Hoese <davidh@ssec.wisc.edu>
Date: Wed, 30 Sep 2009 21:18:26 +0000
Subject: [PATCH] added a few functions  and array_convert function

---
 metobs/data/__init__.py | 77 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 2 deletions(-)

diff --git a/metobs/data/__init__.py b/metobs/data/__init__.py
index 3a71b29..a42806e 100644
--- a/metobs/data/__init__.py
+++ b/metobs/data/__init__.py
@@ -168,7 +168,7 @@ def dewpoint(tempC, relhum):
 
 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.
 
     :param airTempK: air temperature in Kelvin
@@ -242,8 +242,15 @@ def linearConvert(data, factor=1, offset=0):
     return data * float(factor) + offset
 
 def mm2in(val):
+    """Convert millimeters to inches.
+    """
     return linearConvert(val, 0.0393700787)
 
+def in2mm(val):
+    """Convert inches to millimeters.
+    """
+    return linearConvert(val, 1/0.0393700787)
+
 def c2f (val):
     """Degrees celsius to fahrenheit.
     
@@ -254,6 +261,16 @@ def c2f (val):
     """
     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):
     """Speed in meters per second to miles per hour.
     """
@@ -264,6 +281,11 @@ def mps2knots (val):
     """
     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):
     """Compute altimeter from pressure and altitude.
     
@@ -308,7 +330,58 @@ def dir2txt (val):
         if val >= i and val < (i + 22.5):
             return dir
         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__':
     import doctest
     doctest.testmod()
-- 
GitLab