import csv


def MapBasinsToKeysFromJunctionList(DataDirectory,FilenamePrefix):
    """
    Function to map the basins to keys in an order specified by then
    original junction list.

    Args:
        DataDirectory (str): the data directory
        FilenamePrefix (str): prefix of the DEM, should be the same as the
        junctions.list file.

    Returns:
        dictionary where key is the outlet junction and value is the assigned
        basin ID

    Author: FJC
    """

    basin_dict = {}
    key = 0
    # read in the space delimited junctons.list file
    reader = csv.reader(open(DataDirectory+FilenamePrefix+'_junctions.list'), delimiter=" ")

    for row in reader:
        for jn in row:
            basin_dict[jn] = key
            key+=1

    return basin_dict