def z6(line,MCD):
    # REPLACED y105 date with j125 since they are close
    # REPLACED I814 data with i775 since they are close
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F125W_MAG"]],x[MCD["F160W_MAG"]],x[MCD["F606W_MAG"]],x[MCD["F775W_MAG"]],
                                           x[MCD["F850L_MAG"]],
                                           x[MCD["F435W_FLUX"]], x[MCD["F435W_FLUXERR"]],
                                           x[MCD["F606W_FLUX"]], x[MCD["F606W_FLUXERR"]],
                                           x[MCD["F775W_FLUX"]], x[MCD["F775W_FLUXERR"]]]]):
            return False
        c1 = (x[MCD["F775W_MAG"]] - x[MCD["F850L_MAG"]]) > (1.0)
        c2 = (x[MCD["F125W_MAG"]] - x[MCD["F160W_MAG"]]) < (1.0)
        c3 = (x[MCD["F775W_MAG"]] - x[MCD["F850L_MAG"]]) > (0.78*(x[MCD["F125W_MAG"]] - x[MCD["F160W_MAG"]]) + 1.2)
        c4 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (2.0)
        c5 = (x[MCD["F606W_MAG"]] - x[MCD["F850L_MAG"]]) > (2.7) or div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) < (2.0)
    except KeyError:
        c1 = (f2m(x[MCD["F775W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]])) \
           > (1.0)
        c2 = (f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) \
           < (1.0)
        c3 = (f2m(x[MCD["F775W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]])) \
           > (0.78*(f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) + 1.2)
        c4 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (2.0)
        c5 = (f2m(x[MCD["F606W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]])) \
           > (2.7) \
          or div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) \
           < (2.0)

    c6 = not z7(line,MCD)
    return all([c1,c2,c3,c4,c5,c6])
def run(matched_catalog,title):
    with open(matched_catalog) as cat_data:
        cat_lines = cat_data.readlines()
        x_ax =  [ ]  # H-Band magnitudes
        y_ax1 = [ ] # Color differences (V-I)
        y_ax2 = [ ] # Color differences (I-Z)
        for obj in cat_lines:
            if obj[0] == "#":
                continue
            myVI_color = float(obj.split()[MASTER_COL_DICT["F606W_MAG"]]) \
                        -float(obj.split()[MASTER_COL_DICT["F775W_MAG"]])
            candelsVI_color = f2m(float(MASTER_CANDELS_DICT["F606W_FLUX"])) \
                             -f2m(float(MASTER_CANDELS_DICT["F775W_FLUX"]))

            myIZ_color = float(obj.split()[MASTER_COL_DICT["F775W_MAG"]]) \
                        -float(obj.split()[MASTER_COL_DICT["F850L_MAG"]])
            candelsIZ_color = f2m(float(MASTER_CANDELS_DICT["F775W_FLUX"])) \
                             -f2m(float(MASTER_CANDELS_DICT["F850L_FLUX"]))

            h_mag = float(obj.split()[MASTER_COL_DICT["F160W_MAG"]])
            cd1   = myVI_color-candelsVI_color
            cd2   = myIZ_color-candelsIZ_color
            if abs(h_mag) > 35 or abs(cd1) > 35 or abs(cd2) > 35:
                continue
            x_ax.append(h_mag)
            y_ax1.append(cd1)
            y_ax2.append(cd2)
        _plot(x_ax,y_ax1,title=title,xlab="H-band Magnitude",ylab="My Colors - Candels Colors\n(V-I)")
        _plot(x_ax,y_ax2,title=title,xlab="H-band Magnitude",ylab="My Colors - Candels Colors\n(I-Z)")
    return
def z7(line,MCD):
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F125W_MAG"]],x[MCD["F160W_MAG"]],x[MCD["F775W_MAG"]],x[MCD["F850L_MAG"]],
                                           x[MCD["F435W_FLUX"]], x[MCD["F435W_FLUXERR"]],
                                           x[MCD["F606W_FLUX"]], x[MCD["F606W_FLUXERR"]],
                                           x[MCD["F775W_FLUX"]], x[MCD["F775W_FLUXERR"]]]]):
            return False

        c1 = (x[MCD["F850L_MAG"]] - x[MCD["F125W_MAG"]]) > (0.7)
        c2 = (x[MCD["F125W_MAG"]] - x[MCD["F160W_MAG"]]) < (0.45)
        c3 = (x[MCD["F850L_MAG"]] - x[MCD["F125W_MAG"]]) < (0.8*((x[MCD["F125W_MAG"]] - x[MCD["F160W_MAG"]])) + 1.0)
        c4 = (x[MCD["F775W_MAG"]] - x[MCD["F125W_MAG"]]) > (1.0) or div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (1.5)
        c5 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (2.0)
        c6 = div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) < (2.0)
        c7 = div(x[MCD["F775W_FLUX"]],x[MCD["F775W_FLUXERR"]]) < (2.0)
        c8 = 1 #chi^2(bvi) < 3
    except KeyError:
        c1 = (f2m(x[MCD["F850L_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) \
           > (0.7)
        c2 = (f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) \
           < (0.45)
        c3 = (f2m(x[MCD["F850L_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) \
           < (0.8*(f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) + 1.0)
        c4 = (f2m(x[MCD["F775W_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) \
           > (1.0) \
          or div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (1.5)
        c5 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (2.0)
        c6 = div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) < (2.0)
        c7 = div(x[MCD["F775W_FLUX"]],x[MCD["F775W_FLUXERR"]]) < (2.0)
        c8 = 1 #chi^2(bvi) < 3
    c9 = not z8(line,MCD)
    return all([c1,c2,c3,c4,c5,c6,c7,c8,c9])
def run(title,catalog_dir,mp,rms):
    mp = mp.lower()
    cats = sorted([catalog_dir + c for c in os.listdir(catalog_dir)])
    for sel,cat in zip(SELECTIONS,cats):
        if "z" in sel or "i" in sel:
            continue
        with open(cat) as cat_data:
            x_ax = []
            y_ax = []
            cat_lines = cat_data.readlines()
            for line in cat_lines:
                if line[0]=="#":
                    continue
                if mp == "ketron":
                    y_ax.append(float(line.split()[MY_COLOR_COLOR_OPS[sel][0][0]]) \
                               -float(line.split()[MY_COLOR_COLOR_OPS[sel][0][1]]))
                    x_ax.append(float(line.split()[MY_COLOR_COLOR_OPS[sel][1][0]]) \
                               -float(line.split()[MY_COLOR_COLOR_OPS[sel][1][1]]))
                elif mp == "candels":
                    y_ax.append(f2m(float(line.split()[CANDELS_COLOR_COLOR_OPS[sel][0][0]])) \
                               -f2m(float(line.split()[CANDELS_COLOR_COLOR_OPS[sel][0][1]])))
                    x_ax.append(f2m(float(line.split()[CANDELS_COLOR_COLOR_OPS[sel][1][0]])) \
                               -f2m(float(line.split()[CANDELS_COLOR_COLOR_OPS[sel][1][1]])))
        r = COLOR_RULES[sel]
        color_color([[[x_ax,y_ax]]],r[0],r[1],title.format(sel,"Using RMS Maps" if rms else "Not Using RMS Maps"),
                    xthresh=r[2],ythresh=r[3],xlim=r[4],ylim=r[5],a=r[6],b=r[7],graphall=r[8])
    return
def z8(line,MCD):
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F125W_MAG"]],x[MCD["F160W_MAG"]],
                                           x[MCD["F435W_FLUX"]], x[MCD["F435W_FLUXERR"]],
                                           x[MCD["F606W_FLUX"]], x[MCD["F606W_FLUXERR"]],
                                           x[MCD["F775W_FLUX"]], x[MCD["F775W_FLUXERR"]]]]):
            return False
        c1 = (x[MCD["F125W_MAG"]] - x[MCD["F125W_MAG"]]) < (0.45)
        c2 = (x[MCD["F125W_MAG"]] - x[MCD["F160W_MAG"]]) < (0.5)
        c3 = (x[MCD["F125W_MAG"]] - x[MCD["F125W_MAG"]]) > (0.75*(x[MCD["F125W_MAG"]] - x[MCD["F160W_MAG"]]) + 0.525)
        c4 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (2.0)
        c5 = div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) < (2.0)
        c6 = div(x[MCD["F775W_FLUX"]],x[MCD["F775W_FLUXERR"]]) < (2.0)
        c7 = c6
        c8 = 1 #chi^2(bviI) < 3
    except KeyError:
        c1 = (f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) \
           < (0.45)
        c2 = (f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) \
           < (0.5)
        c3 = (f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) \
           > (0.75*(f2m(x[MCD["F125W_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) + 0.525)
        c4 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (2.0)
        c5 = div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) < (2.0)
        c6 = div(x[MCD["F775W_FLUX"]],x[MCD["F775W_FLUXERR"]]) < (2.0)
        c7 = c6
        c8 = 1 #chi^2(bviI) < 3
    return all([c1,c2,c3,c4,c5,c6,c7,c8])
def i775_dropout(line,MCD):
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F606W_MAG"]],x[MCD["F775W_MAG"]],x[MCD["F850L_MAG"]],
                                           x[MCD["F850L_FLUX"]],x[MCD["F850L_FLUXERR"]],
                                           x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]]]):
            return False
        c1 = (x[MCD["F775W_MAG" ]] - x[MCD["F850L_MAG"]]) > (1.3)
        c2 = div(x[MCD["F850L_FLUX"]],x[MCD["F850L_FLUXERR"]]) > (5.0)
        c3 = (x[MCD["F606W_MAG"]]-x[MCD["F850L_MAG"]]) > (2.8) # or (x[MCD["F606W_FLUX"]] / x[MCD["F606W_FLUXERR"]]) < (2.0
    except KeyError:
        c1 = (f2m(x[MCD["F775W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]])) \
           > (1.3)
        c2 = div(x[MCD["F850L_FLUX"]],x[MCD["F850L_FLUXERR"]]) > (5.0)
        c3 = (f2m(x[MCD["F606W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]])) \
           > (2.8)
    return all([c1,c2,c3])
def z4(line,MCD):
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F125W_MAG"]],x[MCD["F435W_MAG"]],x[MCD["F606W_MAG"]],x[MCD["F775W_MAG"]]]]):
            return False
        c1 = (x[MCD["F435W_MAG"]] - x[MCD["F606W_MAG"]]) > (1.0)
        c2 = (x[MCD["F775W_MAG"]] - x[MCD["F125W_MAG"]]) < (1.0)
        c3 = (x[MCD["F435W_MAG"]] - x[MCD["F606W_MAG"]]) > (1.6*(x[MCD["F775W_MAG"]] - x[MCD["F125W_MAG"]]) + 1.0)
    except KeyError:
        c1 = (f2m(x[MCD["F435W_FLUX"]]) - f2m(x[MCD["F606W_FLUX"]])) \
           > (1.0)
        c2 = (f2m(x[MCD["F775W_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) \
           < (1.0)
        c3 = (f2m(x[MCD["F435W_FLUX"]]) - f2m(x[MCD["F606W_FLUX"]])) \
           > (1.6*(f2m(x[MCD["F775W_FLUX"]]) - f2m(x[MCD["F125W_FLUX"]])) + 1.0)
    c4 = not z5(line,MCD)
    return all([c1,c2,c3,c4])
def z5(line,MCD):
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F160W_MAG"]],x[MCD["F606W_MAG"]],x[MCD["F775W_MAG"]],x[MCD["F850L_MAG"]],
                                           x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]]]):
            return False
        c1 = (x[MCD["F606W_MAG" ]] - x[MCD["F775W_MAG"]]) > (1.2)
        c2 = (x[MCD["F850L_MAG" ]] - x[MCD["F160W_MAG"]]) < (1.3)
        c3 = (x[MCD["F606W_MAG" ]] - x[MCD["F775W_MAG"]]) > (0.8*(x[MCD["F850L_MAG"]]-x[MCD["F160W_MAG"]]) + 1.2)
        c4 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (1.0)
    except KeyError:
        c1 = (f2m(x[MCD["F606W_FLUX"]]) - f2m(x[MCD["F775W_FLUX"]])) \
           > (1.2)
        c2 = (f2m(x[MCD["F850L_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) \
           < (1.3)
        c3 = (f2m(x[MCD["F606W_FLUX"]]) - f2m(x[MCD["F775W_FLUX"]])) \
           > (0.8*(f2m(x[MCD["F850L_FLUX"]]) - f2m(x[MCD["F160W_FLUX"]])) + 1.2)
        c4 = div(x[MCD["F435W_FLUX"]],x[MCD["F435W_FLUXERR"]]) < (1.0)
    c5 = not z6(line,MCD)
    return all([c1,c2,c3,c4,c5])
def b435_dropout(line,MCD):
    x = _float_split(line)
    try:
        if not all([num != 99. for num in [x[MCD["F435W_MAG"]],x[MCD["F606W_MAG"]],x[MCD["F775W_MAG"]],x[MCD["F850L_MAG"]],
                                          x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]],
                                          x[MCD["F775W_FLUX"]]/x[MCD["F775W_FLUXERR"]]]]):
            return False
        c1 = (x[MCD["F435W_MAG" ]] - x[MCD["F606W_MAG"]]) > (1.1 + x[MCD["F606W_MAG"]] - x[MCD["F850L_MAG"]])
        c2 = (x[MCD["F435W_MAG" ]] - x[MCD["F606W_MAG"]]) > (1.1)
        c3 = (x[MCD["F606W_MAG" ]] - x[MCD["F850L_MAG"]]) < 1.6
        c4 = div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) > (5.0)
        c5 = div(x[MCD["F775W_FLUX"]],x[MCD["F775W_FLUXERR"]]) > (3.0)
    except KeyError:
        # Means there is no Magnitude entries; only flux. Assuming zp=23.9
        c1 = (f2m(x[MCD["F435W_FLUX"]]) - f2m(x[MCD["F606W_FLUX"]])) \
           > (1.1 + f2m(x[MCD["F606W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]]))
        c2 = (f2m(x[MCD["F435W_FLUX" ]]) - f2m(x[MCD["F606W_FLUX"]])) \
           > (1.1)
        c3 = (f2m(x[MCD["F606W_FLUX"]]) - f2m(x[MCD["F850L_FLUX"]])) \
           < (1.6)
        c4 = div(x[MCD["F606W_FLUX"]],x[MCD["F606W_FLUXERR"]]) > (5.0)
        c5 = div(x[MCD["F775W_FLUX"]],x[MCD["F775W_FLUXERR"]]) > (3.0)
    return all([c1,c2,c3,c4,c5])