def readimg(filename, rows, cols):
    """
    Attempts to read the .img file filename (with or without '.img')
    assuming it was read with rows rows and cols cols.
    """
    import numpy as np
    dt = np.dtype(np.uint16)
    dt = dt.newbyteorder('>')
    filename = filename if filename.endswith('.img') else filename + '.img'
    with open(filename, 'rb') as f:
        ret = np.reshape(np.fromfile(f, dtype=dt), (rows, cols))
    return np.array(ret, dtype=np.float64)
