import numpy as np
from astropy import units

def check_units_and_type(input, expected_units, num = None,
                         is_scalar = False):
    """Check whether variable has expected units and type.

    If input does not have units and expected units is not None, then the
    output will be assigned those units. If input has units that conflict
    with expected units a ValueError will be raised.
    
    Parameters
    ----------
    input : array_like or float
        Variable that will be checked for units and type. Variable should
        be 1D or scalar.
    expected_units : astropy.units or None
        Unit expected for input.
    num : int, optional
        Length expected for input, if it is an array or list.
    is_scalar : bool, optional
        Sets whether the input is a scalar quantity. Default is False for
        array_like inputs; set is_scalar=True to check scalar units only.

    Returns
    ----------
    ndarray or float, with astropy.units
        Returns the input array or scalar with expected units, unless a
        conflict of units or array length occurs, which raise errors.
    """
    if hasattr(input, 'unit'):    #check units
        if expected_units is None:
            raise ValueError('Expecting dimensionless input')
        elif input.unit != expected_units:
            raise ValueError('Expecting input units of ' + str(expected_units))
        else:
            dimensionless = input.value
        
    else:
        dimensionless = input

    #check its a 1D array and/or convert list to array
    if is_scalar == False:
        dimensionfull = check_array_or_list(dimensionless)
    else:
        dimensionfull = dimensionless

    #include units if appropriate
    if expected_units is not None:
        dimensionfull = dimensionfull * expected_units

    #check array length if appropriate
    if num is not None:
        check_input_size(dimensionfull, num)
        
    return dimensionfull


def check_array_or_list(input):
    """Return 1D ndarray, if input can be converted and elements are
    non-negative."""
    if type(input) != np.ndarray:
        if type(input) == list:
            output = np.array(input)
        else:
            raise TypeError('Expecting input type as ndarray or list.')
    else:
        output = input

    if output.ndim != 1:
        raise ValueError('Input array must have 1 dimension.')
    
    if np.sum(output < 0.) > 0:
            raise ValueError("Input array values cannot be negative.")
    
    return output


def check_input_size(array, num):
    """Check that an array has the expected number of elements."""
    if array.shape[0] != num:
        raise ValueError('Input arrays must have the same length (the \
                          number of clusters).')



@@ -16,33 +16,8 @@
  np%0A
-from astropy import units
 %0A%0Ade
@@ -69,41 +69,14 @@
  num
- = 
+=
 None,
-%0A                        
  is_
@@ -81,19 +81,17 @@
 s_scalar
- = 
+=
 False):%0A
@@ -351,20 +351,16 @@
 raised.%0A
-    
 %0A    Par
@@ -1104,24 +1104,25 @@
 unit'):    #
+ 
 check units%0A
@@ -1395,24 +1395,16 @@
 t.value%0A
-        
 %0A    els
@@ -1438,24 +1438,25 @@
 input%0A%0A    #
+ 
 check its a 
@@ -1510,18 +1510,18 @@
 _scalar 
-==
+is
  False:%0A
@@ -1633,16 +1633,17 @@
 s%0A%0A    #
+ 
 include 
@@ -1759,16 +1759,17 @@
 s%0A%0A    #
+ 
 check ar
@@ -1863,24 +1863,16 @@
 l, num)%0A
-        
 %0A    ret
@@ -2324,20 +2324,16 @@
 sion.')%0A
-    
 %0A    if 
@@ -2428,20 +2428,16 @@
 tive.%22)%0A
-    
 %0A    ret
@@ -2701,10 +2701,8 @@
 ers).')%0A
-%0A%0A

make utils.py flake8 happy