DEPRECATED: 2018-05-04
    Mangle must be set up on your system.
    The index argument is a temporary file naming convention to avoid file conflicts.
    Coordinates must be given in the native coordinate system of the Mangle file.

def readMangleFile(infile, lon, lat, index = None):
    """
    DEPRECATED: 2018-05-04
    Mangle must be set up on your system.
    The index argument is a temporary file naming convention to avoid file conflicts.
    Coordinates must be given in the native coordinate system of the Mangle file.
    """
    msg = "'mask.readMangleFile': ADW 2018-05-05"
    DeprecationWarning(msg)

    if index is None:
        index = np.random.randint(0, 1.e10)
    
    coordinate_file = 'temp_coordinate_%010i.dat'%(index)
    maglim_file = 'temp_maglim_%010i.dat'%(index)

    writer = open(coordinate_file, 'w')
    for ii in range(0, len(lon)):
        writer.write('%12.5f%12.5f\n'%(lon[ii], lat[ii]))
    writer.close()

    os.system('polyid -W %s %s %s || exit'%(infile,
                                            coordinate_file,
                                            maglim_file))

    reader = open(maglim_file)
    lines = reader.readlines()
    reader.close()

    os.remove(maglim_file)
    os.remove(coordinate_file)

    maglim = []
    for ii in range(1, len(lines)):
        if len(lines[ii].split()) == 3:
            maglim.append(float(lines[ii].split()[2]))
        elif len(lines[ii].split()) == 2:
            maglim.append(0.) # Coordinates outside of the MANGLE ploygon
        elif len(lines[ii].split()) > 3:
            msg = 'Coordinate inside multiple polygons, masking that coordinate.'
            logger.warning(msg)
            maglim.append(0.)
        else:
            msg = 'Cannot parse maglim file, unexpected number of columns.'
            logger.error(msg)
            break
            
    maglim = np.array(maglim)
    return maglim