def bytscl(array, maximum=None , minimum=None , nan=0, top=255 ):
    """
    see http://star.pst.qub.ac.uk/idl/BYTSCL.html
    note that IDL uses slightly different formulae for bytscaling floats and ints. 
    here we apply only the FLOAT formula...
    """
    if maximum is None: maximum = np.nanmax(array)
    if minimum is None: minimum = np.nanmin(array)
    return np.maximum(np.minimum(((top+1.0)*(array-minimum)/(maximum-minimum)).astype(np.int16), top),0)