def plot_matched_separation_hist(d12):
    '''d12 is array of distances in degress between matched objects'''
    #pixscale to convert d12 into N pixels
    pixscale = dict(decam=0.25, bokmos=0.45)
    #sns.set_style('ticks',{"axes.facecolor": ".97"})
    #sns.set_palette('colorblind')
    #setup plot
    fig, ax = plt.subplots()
    #plot
    ax.hist(d12 * 3600, bins=50, color='b', align='mid')
    ax2 = ax.twiny()
    ax2.hist(d12 * 3600. / pixscale['bokmos'],
             bins=50,
             color='g',
             align='mid',
             visible=False)
    xlab = ax.set_xlabel("arcsec")
    xlab = ax2.set_xlabel("pixels [BASS]")
    ylab = ax.set_ylabel("Matched")
    #save
    #sns.despine()
    plt.savefig(os.path.join(get_outdir('bmd'), "separation_hist.png"),
                bbox_extra_artists=[xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_matched_decam_vs_bokmos_psf_fwhm(obj, type="psf"):
    """using matched sample, plot decam psf_fwhm vs. bokmos psf_fwhm 
    obj['m_decam'] is DECaLS() object"""
    # indices
    index = np.all(
        (indices_for_type(b, inst="m_decam", type=type), indices_for_type(b, inst="m_bokmos", type=type)), axis=0
    )  # both bokmos and decam of same type
    # setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    text_args = dict(verticalalignment="center", horizontalalignment="left", fontsize=10)
    # plot
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        ax[cnt].scatter(
            obj["m_bokmos"].data[band + "_psf_fwhm"][index],
            obj["m_decam"].data[band + "_psf_fwhm"][index],
            edgecolor="b",
            c="none",
            lw=1.0,
        )
        ax[cnt].text(0.05, 0.95, band, transform=ax[cnt].transAxes, **text_args)
    # finish
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        ax[cnt].set_xlim(0, 3)
        ax[cnt].set_ylim(0, 3)
    xlab = ax[1].set_xlabel("PSF_FWHM (bokmos)", **laba)
    ylab = ax[0].set_ylabel("PSF_FWHM (decam)", **laba)
    ti = plt.suptitle("%s Objects, Matched" % type.upper())
    plt.savefig(
        os.path.join(get_outdir("bmd"), "decam_vs_bokmos_psf_fwhm_%s.png" % type),
        bbox_extra_artists=[ti, xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_matched_decam_vs_bokmos_psf_fwhm(obj, type='psf'):
    '''using matched sample, plot decam psf_fwhm vs. bokmos psf_fwhm 
    obj['m_decam'] is DECaLS() object'''
    #indices
    index= np.all((indices_for_type(b,inst='m_decam',type=type),\
                    indices_for_type(b,inst='m_bokmos',type=type)), axis=0) #both bokmos and decam of same type
    #setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    text_args = dict(verticalalignment='center',
                     horizontalalignment='left',
                     fontsize=10)
    #plot
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        ax[cnt].scatter(obj['m_bokmos'].data[band+'_psf_fwhm'][index], obj['m_decam'].data[band+'_psf_fwhm'][index],\
                        edgecolor='b',c='none',lw=1.)
        ax[cnt].text(0.05,
                     0.95,
                     band,
                     transform=ax[cnt].transAxes,
                     **text_args)
    #finish
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        ax[cnt].set_xlim(0, 3)
        ax[cnt].set_ylim(0, 3)
    xlab = ax[1].set_xlabel('PSF_FWHM (bokmos)', **laba)
    ylab = ax[0].set_ylabel('PSF_FWHM (decam)', **laba)
    ti = plt.suptitle('%s Objects, Matched' % type.upper())
    plt.savefig(os.path.join(get_outdir('bmd'),
                             'decam_vs_bokmos_psf_fwhm_%s.png' % type),
                bbox_extra_artists=[ti, xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_confusion_matrix(cm, ticknames, addname=''):
    '''cm -- NxN array containing the Confusion Matrix values
    ticknames -- list of strings of length == N, column and row names for cm plot'''
    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues, vmin=0, vmax=1)
    cbar = plt.colorbar()
    plt.xticks(range(len(ticknames)), ticknames)
    plt.yticks(range(len(ticknames)), ticknames)
    ylab = plt.ylabel('True (DECaLS)')
    xlab = plt.xlabel('Predicted (BASS/MzLS)')
    for row in range(len(ticknames)):
        for col in range(len(ticknames)):
            if np.isnan(cm[row, col]):
                plt.text(col, row, 'n/a', va='center', ha='center')
            elif cm[row, col] > 0.5:
                plt.text(col,
                         row,
                         '%.2f' % cm[row, col],
                         va='center',
                         ha='center',
                         color='yellow')
            else:
                plt.text(col,
                         row,
                         '%.2f' % cm[row, col],
                         va='center',
                         ha='center',
                         color='black')
    plt.savefig(os.path.join(get_outdir('bmd'),
                             'confusion_matrix%s.png' % addname),
                bbox_extra_artists=[xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_magRatio_vs_mag(b, type="psf", addname=""):
    # join indices b/c matched
    i_type = np.all(
        (indices_for_type(b, inst="m_decam", type=type), indices_for_type(b, inst="m_bokmos", type=type)), axis=0
    )  # both bokmos and decam of same type
    # plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        magRatio = (
            np.log10(b["m_bokmos"].data[band + "flux"][i_type]) / np.log10(b["m_decam"].data[band + "flux"][i_type])
            - 1.0
        )
        mag = 22.5 - 2.5 * np.log10(b["m_decam"].data[band + "flux"][i_type])
        ax[cnt].scatter(mag, magRatio, c="b", edgecolor="b", s=5)  # ,c='none',lw=2.)
    # labels
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        xlab = ax[cnt].set_xlabel("%s AB" % band, **laba)
        ax[cnt].set_ylim(-0.5, 0.5)
        ax[cnt].set_xlim(18, 26)
    ylab = ax[0].set_ylabel(r"$m_{bm}/m_d - 1$", **laba)
    ti = ax[1].set_title("%s" % type, **laba)
    # put stats in suptitle
    plt.savefig(
        os.path.join(get_outdir("bmd"), "magRatio_vs_mag_%s%s.png" % (type, addname)),
        bbox_extra_artists=[ti, xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_radec(obj, addname=''):
    '''obj[m_types] -- DECaLS() objects with matched OR unmatched indices'''
    #set seaborn panel styles
    #sns.set_style('ticks',{"axes.facecolor": ".97"})
    #sns.set_palette('colorblind')
    #setup plot
    fig, ax = plt.subplots(1, 2, figsize=(9, 6), sharey=True, sharex=True)
    plt.subplots_adjust(wspace=0.25)
    #plt.subplots_adjust(wspace=0.5)
    #plot
    ax[0].scatter(obj['m_decam'].data['ra'], obj['m_decam'].data['dec'], \
                edgecolor='b',c='none',lw=1.)
    ax[1].scatter(obj['u_decam'].data['ra'], obj['u_decam'].data['dec'], \
                edgecolor='b',c='none',lw=1.,label='DECaLS')
    ax[1].scatter(obj['u_bokmos'].data['ra'], obj['u_bokmos'].data['dec'], \
                edgecolor='g',c='none',lw=1.,label='BASS/MzLS')
    for cnt, ti in zip(range(2), ['Matched', 'Unmatched']):
        ti = ax[cnt].set_title(ti, **laba)
        xlab = ax[cnt].set_xlabel('RA', **laba)
    ylab = ax[0].set_ylabel('DEC', **laba)
    ax[0].legend(loc='upper left', **leg_args)
    #save
    #sns.despine()
    plt.savefig(os.path.join(get_outdir('bmd'), 'radec%s.png' % addname),
                bbox_extra_artists=[xlab, ylab, ti],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_magRatio_vs_mag(b, type='psf', addname=''):
    #join indices b/c matched
    i_type= np.all((indices_for_type(b, inst='m_decam',type=type),\
                    indices_for_type(b, inst='m_bokmos',type=type)), axis=0) #both bokmos and decam of same type
    #plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        magRatio = np.log10(
            b['m_bokmos'].data[band + 'flux'][i_type]) / np.log10(
                b['m_decam'].data[band + 'flux'][i_type]) - 1.
        mag = 22.5 - 2.5 * np.log10(b['m_decam'].data[band + 'flux'][i_type])
        ax[cnt].scatter(mag, magRatio, c='b', edgecolor='b',
                        s=5)  #,c='none',lw=2.)
    #labels
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        xlab = ax[cnt].set_xlabel('%s AB' % band, **laba)
        ax[cnt].set_ylim(-0.5, 0.5)
        ax[cnt].set_xlim(18, 26)
    ylab = ax[0].set_ylabel(r'$m_{bm}/m_d - 1$', **laba)
    ti = ax[1].set_title("%s" % type, **laba)
    #put stats in suptitle
    plt.savefig(os.path.join(get_outdir('bmd'),
                             'magRatio_vs_mag_%s%s.png' % (type, addname)),
                bbox_extra_artists=[ti, xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_radec(obj, addname=""):
    """obj[m_types] -- DECaLS() objects with matched OR unmatched indices"""
    # set seaborn panel styles
    # sns.set_style('ticks',{"axes.facecolor": ".97"})
    # sns.set_palette('colorblind')
    # setup plot
    fig, ax = plt.subplots(1, 2, figsize=(9, 6), sharey=True, sharex=True)
    plt.subplots_adjust(wspace=0.25)
    # plt.subplots_adjust(wspace=0.5)
    # plot
    ax[0].scatter(obj["m_decam"].data["ra"], obj["m_decam"].data["dec"], edgecolor="b", c="none", lw=1.0)
    ax[1].scatter(
        obj["u_decam"].data["ra"], obj["u_decam"].data["dec"], edgecolor="b", c="none", lw=1.0, label="DECaLS"
    )
    ax[1].scatter(
        obj["u_bokmos"].data["ra"], obj["u_bokmos"].data["dec"], edgecolor="g", c="none", lw=1.0, label="BASS/MzLS"
    )
    for cnt, ti in zip(range(2), ["Matched", "Unmatched"]):
        ti = ax[cnt].set_title(ti, **laba)
        xlab = ax[cnt].set_xlabel("RA", **laba)
    ylab = ax[0].set_ylabel("DEC", **laba)
    ax[0].legend(loc="upper left", **leg_args)
    # save
    # sns.despine()
    plt.savefig(
        os.path.join(get_outdir("bmd"), "radec%s.png" % addname),
        bbox_extra_artists=[xlab, ylab, ti],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_dflux_chisq(b, type='psf', low=-8., hi=8., addname=''):
    #join indices b/c matched
    i_type= np.all((indices_for_type(b, inst='m_decam',type=type),\
                    indices_for_type(b, inst='m_bokmos',type=type)), axis=0) #both bokmos and decam of same type
    #get flux diff for each band
    hist = dict(g=0, r=0, z=0)
    binc = dict(g=0, r=0, z=0)
    stats = dict(g=0, r=0, z=0)
    #chi
    sample, mag = {}, {}
    for band in ['g', 'r', 'z']:
        sample[band]= (b['m_decam'].data[band+'flux'][i_type]-b['m_bokmos'].data[band+'flux'][i_type])/np.sqrt(\
                    np.power(b['m_decam'].data[band+'flux_ivar'][i_type],-1)+np.power(b['m_bokmos'].data[band+'flux_ivar'][i_type],-1))
        mag[band] = 22.5 - 2.5 * np.log10(
            b['m_decam'].data[band + 'flux'][i_type])
    #loop over mag bins, one 3 panel for each mag bin
    for b_low, b_hi in zip([18, 19, 20, 21, 22, 23], [19, 20, 21, 22, 23, 24]):
        #plot each filter
        for band in ['g', 'r', 'z']:
            imag = np.all((b_low <= mag[band], mag[band] < b_hi), axis=0)
            #print("len(imag)=",len(imag),"len(sample)=",len(sample),"len(sample[imag])=",len(sample[imag]))
            hist[band], bins, junk = plt.hist(sample[band][imag],
                                              range=(low, hi),
                                              bins=50,
                                              normed=True)
            db = (bins[1:] - bins[:-1]) / 2
            binc[band] = bins[:-1] + db
        plt.close()  #b/c plt.hist above
        #for drawing unit gaussian N(0,1)
        G = sp_stats.norm(0, 1)
        xvals = np.linspace(low, hi)
        #plot
        fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
        plt.subplots_adjust(wspace=0.25)
        for cnt, band in zip(range(3), ['g', 'r', 'z']):
            ax[cnt].step(binc[band], hist[band], where='mid', c='b', lw=2)
            ax[cnt].plot(xvals, G.pdf(xvals))
        #labels
        for cnt, band in zip(range(3), ['g', 'r', 'z']):
            if band == 'r':
                xlab = ax[cnt].set_xlabel(
                    r'%s  $(F_{d}-F_{bm})/\sqrt{\sigma^2_{d}+\sigma^2_{bm}}$' %
                    band, **laba)
            else:
                xlab = ax[cnt].set_xlabel('%s' % band, **laba)
            #xlab=ax[cnt].set_xlabel('%s' % band, **laba)
            ax[cnt].set_ylim(0, 0.6)
            ax[cnt].set_xlim(low, hi)
        ylab = ax[0].set_ylabel('PDF', **laba)
        ti = ax[1].set_title(
            "%s (%.1f <= %s < %.1f)" % (type, b_low, band, b_hi), **laba)
        #put stats in suptitle
        plt.savefig(os.path.join(
            get_outdir('bmd'), 'dflux_chisq_%s_%.1f-%s-%.1f%s.png' %
            (type, b_low, band, b_hi, addname)),
                    bbox_extra_artists=[ti, xlab, ylab],
                    bbox_inches='tight',
                    dpi=150)
        plt.close()
def plot_SN_vs_mag(obj, found_by="matched", type="all", addname=""):
    """obj['m_decam'] is DECaLS() object
    found_by -- 'matched' or 'unmatched' 
    type -- all,psf,lrg"""
    # indices for type == all,psf, or lrg
    assert found_by == "matched" or found_by == "unmatched"
    prefix = found_by[0] + "_"  # m_ or u_
    index = {}
    for key in ["decam", "bokmos"]:
        index[key] = indices_for_type(obj, inst=prefix + key, type=type)
    # bin up SN values
    min, max = 18.0, 25.0
    bin_SN = dict(decam={}, bokmos={})
    for key in bin_SN.keys():
        for band in ["g", "r", "z"]:
            bin_SN[key][band] = {}
            i = index[key]
            bin_edges = np.linspace(min, max, num=30)
            bin_SN[key][band]["binc"], count, bin_SN[key][band]["q25"], bin_SN[key][band]["q50"], bin_SN[key][band][
                "q75"
            ] = bin_up(
                obj[prefix + key].data[band + "mag"][i],
                obj[prefix + key].data[band + "flux"][i] * np.sqrt(obj[prefix + key].data[band + "flux_ivar"][i]),
                bin_edges=bin_edges,
            )
    # setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    # plot SN
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        # horiz line at SN = 5
        ax[cnt].plot([1, 40], [5, 5], "k--", lw=2)
        # data
        for inst, color, lab in zip(["decam", "bokmos"], ["b", "g"], ["DECaLS", "BASS/MzLS"]):
            ax[cnt].plot(bin_SN[inst][band]["binc"], bin_SN[inst][band]["q50"], c=color, ls="-", lw=2, label=lab)
            ax[cnt].fill_between(
                bin_SN[inst][band]["binc"],
                bin_SN[inst][band]["q25"],
                bin_SN[inst][band]["q75"],
                color=color,
                alpha=0.25,
            )
    # labels
    ax[2].legend(loc=1, **leg_args)
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        ax[cnt].set_yscale("log")
        xlab = ax[cnt].set_xlabel("%s" % band, **laba)
        ax[cnt].set_ylim(1, 100)
        ax[cnt].set_xlim(20.0, 26.0)
    ylab = ax[0].set_ylabel("S/N", **laba)
    text_args = dict(verticalalignment="bottom", horizontalalignment="right", fontsize=10)
    ax[2].text(26, 5, "S/N = 5  ", **text_args)
    plt.savefig(
        os.path.join(get_outdir("bmd"), "sn_%s_%s%s.png" % (found_by, type, addname)),
        bbox_extra_artists=[xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_dflux_chisq(b, type="psf", low=-8.0, hi=8.0, addname=""):
    # join indices b/c matched
    i_type = np.all(
        (indices_for_type(b, inst="m_decam", type=type), indices_for_type(b, inst="m_bokmos", type=type)), axis=0
    )  # both bokmos and decam of same type
    # get flux diff for each band
    hist = dict(g=0, r=0, z=0)
    binc = dict(g=0, r=0, z=0)
    stats = dict(g=0, r=0, z=0)
    # chi
    sample, mag = {}, {}
    for band in ["g", "r", "z"]:
        sample[band] = (b["m_decam"].data[band + "flux"][i_type] - b["m_bokmos"].data[band + "flux"][i_type]) / np.sqrt(
            np.power(b["m_decam"].data[band + "flux_ivar"][i_type], -1)
            + np.power(b["m_bokmos"].data[band + "flux_ivar"][i_type], -1)
        )
        mag[band] = 22.5 - 2.5 * np.log10(b["m_decam"].data[band + "flux"][i_type])
    # loop over mag bins, one 3 panel for each mag bin
    for b_low, b_hi in zip([18, 19, 20, 21, 22, 23], [19, 20, 21, 22, 23, 24]):
        # plot each filter
        for band in ["g", "r", "z"]:
            imag = np.all((b_low <= mag[band], mag[band] < b_hi), axis=0)
            # print("len(imag)=",len(imag),"len(sample)=",len(sample),"len(sample[imag])=",len(sample[imag]))
            hist[band], bins, junk = plt.hist(sample[band][imag], range=(low, hi), bins=50, normed=True)
            db = (bins[1:] - bins[:-1]) / 2
            binc[band] = bins[:-1] + db
        plt.close()  # b/c plt.hist above
        # for drawing unit gaussian N(0,1)
        G = sp_stats.norm(0, 1)
        xvals = np.linspace(low, hi)
        # plot
        fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
        plt.subplots_adjust(wspace=0.25)
        for cnt, band in zip(range(3), ["g", "r", "z"]):
            ax[cnt].step(binc[band], hist[band], where="mid", c="b", lw=2)
            ax[cnt].plot(xvals, G.pdf(xvals))
        # labels
        for cnt, band in zip(range(3), ["g", "r", "z"]):
            if band == "r":
                xlab = ax[cnt].set_xlabel(r"%s  $(F_{d}-F_{bm})/\sqrt{\sigma^2_{d}+\sigma^2_{bm}}$" % band, **laba)
            else:
                xlab = ax[cnt].set_xlabel("%s" % band, **laba)
            # xlab=ax[cnt].set_xlabel('%s' % band, **laba)
            ax[cnt].set_ylim(0, 0.6)
            ax[cnt].set_xlim(low, hi)
        ylab = ax[0].set_ylabel("PDF", **laba)
        ti = ax[1].set_title("%s (%.1f <= %s < %.1f)" % (type, b_low, band, b_hi), **laba)
        # put stats in suptitle
        plt.savefig(
            os.path.join(get_outdir("bmd"), "dflux_chisq_%s_%.1f-%s-%.1f%s.png" % (type, b_low, band, b_hi, addname)),
            bbox_extra_artists=[ti, xlab, ylab],
            bbox_inches="tight",
            dpi=150,
        )
        plt.close()
def plot_N_per_deg2(obj, type="all", req_mags=[24.0, 23.4, 22.5], addname=""):
    """image requirements grz<=24,23.4,22.5
    compute number density in each bin for each band mag [18,requirement]"""
    # indices for type for matched and unmatched samples
    index = {}
    for inst in ["m_decam", "u_decam", "m_bokmos", "u_bokmos"]:
        index[inst] = indices_for_type(obj, inst=inst, type=type)
    bin_nd = dict(decam={}, bokmos={})
    for inst in ["decam", "bokmos"]:
        bin_nd[inst] = {}
        for band, req in zip(["g", "r", "z"], req_mags):
            bin_nd[inst][band] = {}
            bin_edges = np.linspace(18.0, req, num=15)
            i_m, i_u = index["m_" + inst], index["u_" + inst]  # need m+u
            # join m_decam,u_decam OR m_bokmos,u_bokmos and only with correct all,psf,lrg index
            sample = np.ma.concatenate(
                (obj["m_" + inst].data[band + "mag"][i_m], obj["u_" + inst].data[band + "mag"][i_u]), axis=0
            )
            bin_nd[inst][band]["binc"], bin_nd[inst][band]["cnt"], q25, q50, q75 = bin_up(
                sample, sample, bin_edges=bin_edges
            )
    # plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        for inst, color, lab in zip(["decam", "bokmos"], ["b", "g"], ["DECaLS", "BASS/MzLS"]):
            ax[cnt].step(
                bin_nd[inst][band]["binc"],
                bin_nd[inst][band]["cnt"] / obj["deg2_" + inst],
                where="mid",
                c=color,
                lw=2,
                label=lab,
            )
    # labels
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        xlab = ax[cnt].set_xlabel("%s" % band)  # , **laba)
        # ax[cnt].set_ylim(0,0.6)
        # ax[cnt].set_xlim(maglow,maghi)
    ax[0].legend(loc="upper left", **leg_args)
    ylab = ax[0].set_ylabel("counts/deg2")  # , **laba)
    ti = plt.suptitle("%ss" % type.upper(), **laba)
    # Make space for and rotate the x-axis tick labels
    fig.autofmt_xdate()
    # put stats in suptitle
    plt.savefig(
        os.path.join(get_outdir("bmd"), "n_per_deg2_%s%s.png" % (type, addname)),
        bbox_extra_artists=[ti, xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_matched_dmag_vs_psf_fwhm(obj, type='psf'):
    '''using matched sample, plot diff in mags vs. DECAM psf_fwhm in bins 
    obj['m_decam'] is DECaLS() object'''
    #indices
    index= np.all((indices_for_type(b,inst='m_decam',type=type),\
                    indices_for_type(b,inst='m_bokmos',type=type)), axis=0) #both bokmos and decam of same type
    #bin up by DECAM psf_fwhm
    bin_edges = np.linspace(0, 3, num=6)
    vals = {}
    for band in ['g', 'r', 'z']:
        vals[band] = {}
        vals[band]['binc'],count,vals[band]['q25'],vals[band]['q50'],vals[band]['q75']=\
                bin_up(obj['m_decam'].data[band+'_psf_fwhm'][index], \
                       obj['m_bokmos'].data[band+'mag'][index]- obj['m_decam'].data[band+'mag'][index], \
                            bin_edges=bin_edges)


#setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    text_args = dict(verticalalignment='center',
                     horizontalalignment='left',
                     fontsize=10)
    #plot
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        ax[cnt].plot(vals[band]['binc'],
                     vals[band]['q50'],
                     c='b',
                     ls='-',
                     lw=2)
        ax[cnt].fill_between(vals[band]['binc'],
                             vals[band]['q25'],
                             vals[band]['q75'],
                             color='b',
                             alpha=0.25)
        ax[cnt].text(0.05,
                     0.95,
                     band,
                     transform=ax[cnt].transAxes,
                     **text_args)
    #finish
    xlab = ax[1].set_xlabel('decam PSF_FWHM', **laba)
    ylab = ax[0].set_ylabel(r'Median $\Delta \, m$ (decam - bokmos)', **laba)
    ti = plt.suptitle('%s Objects, Matched' % type.upper())
    plt.savefig(os.path.join(get_outdir('bmd'),
                             'dmag_vs_psf_fwhm_%s.png' % type),
                bbox_extra_artists=[ti, xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_N_per_deg2(obj, type='all', req_mags=[24., 23.4, 22.5], addname=''):
    '''image requirements grz<=24,23.4,22.5
    compute number density in each bin for each band mag [18,requirement]'''
    #indices for type for matched and unmatched samples
    index = {}
    for inst in ['m_decam', 'u_decam', 'm_bokmos', 'u_bokmos']:
        index[inst] = indices_for_type(obj, inst=inst, type=type)
    bin_nd = dict(decam={}, bokmos={})
    for inst in ['decam', 'bokmos']:
        bin_nd[inst] = {}
        for band, req in zip(['g', 'r', 'z'], req_mags):
            bin_nd[inst][band] = {}
            bin_edges = np.linspace(18., req, num=15)
            i_m, i_u = index['m_' + inst], index['u_' + inst]  #need m+u
            #join m_decam,u_decam OR m_bokmos,u_bokmos and only with correct all,psf,lrg index
            sample = np.ma.concatenate(
                (obj['m_' + inst].data[band + 'mag'][i_m],
                 obj['u_' + inst].data[band + 'mag'][i_u]),
                axis=0)
            bin_nd[inst][band]['binc'],bin_nd[inst][band]['cnt'],q25,q50,q75=\
                    bin_up(sample,sample,bin_edges=bin_edges)
    #plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        for inst, color, lab in zip(['decam', 'bokmos'], ['b', 'g'],
                                    ['DECaLS', 'BASS/MzLS']):
            ax[cnt].step(bin_nd[inst][band]['binc'],
                         bin_nd[inst][band]['cnt'] / obj['deg2_' + inst],
                         where='mid',
                         c=color,
                         lw=2,
                         label=lab)
    #labels
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        xlab = ax[cnt].set_xlabel('%s' % band)  #, **laba)
        #ax[cnt].set_ylim(0,0.6)
        #ax[cnt].set_xlim(maglow,maghi)
    ax[0].legend(loc='upper left', **leg_args)
    ylab = ax[0].set_ylabel('counts/deg2')  #, **laba)
    ti = plt.suptitle("%ss" % type.upper(), **laba)
    # Make space for and rotate the x-axis tick labels
    fig.autofmt_xdate()
    #put stats in suptitle
    plt.savefig(os.path.join(get_outdir('bmd'),
                             'n_per_deg2_%s%s.png' % (type, addname)),
                bbox_extra_artists=[ti, xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_HistTypes(obj, m_types=['m_decam', 'm_bokmos'], addname=''):
    '''decam,bokmos -- DECaLS() objects with matched OR unmatched indices'''
    #matched or unmatched objects
    if m_types[0].startswith('m_') and m_types[1].startswith('m_'):
        matched = True
    elif m_types[0].startswith('u_') and m_types[1].startswith('u_'):
        matched = False
    else:
        raise ValueError
    #sns.set_style("whitegrid")
    #sns.set_palette('colorblind')
    #c1=sns.color_palette()[2]
    #c2=sns.color_palette()[0] #'b'
    c1 = 'b'
    c2 = 'r'
    ###
    types = ['PSF', 'SIMP', 'EXP', 'DEV', 'COMP']
    ind = np.arange(len(types))  # the x locations for the groups
    width = 0.35  # the width of the bars
    ###
    ht_decam, ht_bokmos = np.zeros(5, dtype=int), np.zeros(5, dtype=int)
    for cnt, typ in enumerate(types):
        ht_decam[cnt] = np.where(
            obj[m_types[0]].data['type'] == typ)[0].shape[0] / float(
                obj['deg2_decam'])
        ht_bokmos[cnt] = np.where(
            obj[m_types[1]].data['type'] == typ)[0].shape[0] / float(
                obj['deg2_bokmos'])
    ###
    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, ht_decam, width, color=c1)
    rects2 = ax.bar(ind + width, ht_bokmos, width, color=c2)
    ylab = ax.set_ylabel("counts/deg2")
    if matched: ti = ax.set_title('Matched')
    else: ti = ax.set_title('Unmatched')
    ax.set_xticks(ind + width)
    ax.set_xticklabels(types)
    ax.legend((rects1[0], rects2[0]), ('DECaLS', 'BASS/MzLS'), **leg_args)
    #save
    if matched: name = 'hist_types_Matched%s.png' % addname
    else: name = 'hist_types_Unmatched%s.png' % addname
    plt.savefig(os.path.join(get_outdir('bmd'), name),
                bbox_extra_artists=[ylab, ti],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_psf_hists(decam, bokmos, zoom=False):
    '''decam,bokmos are DECaLS() objects matched to decam ra,dec'''
    #divide into samples of 0.25 mag bins, store q50 of each
    width = 0.25  #in mag
    low_vals = np.arange(20., 26., width)
    med = {}
    for b in ['g', 'r', 'z']:
        med[b] = np.zeros(low_vals.size) - 100
    for i, low in enumerate(low_vals):
        for band in ['g', 'r', 'z']:
            ind = np.all((low <= decam[band + 'mag'],
                          decam[band + 'mag'] < low + width),
                         axis=0)
            if np.where(ind)[0].size > 0:
                med[band][i] = np.percentile(bokmos[band + 'mag'][ind] -
                                             decam[band + 'mag'][ind],
                                             q=50)
            else:
                med[band][i] = np.nan
    #make plot
    #set seaborn panel styles
    #sns.set_style('ticks',{"axes.facecolor": ".97"})
    #sns.set_palette('colorblind')
    #setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3))  #,sharey=True)
    plt.subplots_adjust(wspace=0.5)
    #plot
    for cnt, band in zip(range(3), ['r', 'g', 'z']):
        ax[cnt].scatter(low_vals, med[band],\
                       edgecolor='b',c='none',lw=2.) #,label=m_type.split('_')[-1])
        xlab = ax[cnt].set_xlabel('bins of %s (decam)' % band, **laba)
        ylab = ax[cnt].set_ylabel('q50[%s bokmos - decam]' % band, **laba)
        if zoom: ax[cnt].set_ylim(-0.25, 0.25)
    # sup=plt.suptitle('decam with matching bokmos',**laba)
    #save
    #sns.despine()
    if zoom: name = "median_color_diff_zoom.png"
    else: name = "median_color_diff.png"
    plt.savefig(os.path.join(get_outdir('bmd'), name),
                bbox_extra_artists=[xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
def plot_HistTypes(obj, m_types=["m_decam", "m_bokmos"], addname=""):
    """decam,bokmos -- DECaLS() objects with matched OR unmatched indices"""
    # matched or unmatched objects
    if m_types[0].startswith("m_") and m_types[1].startswith("m_"):
        matched = True
    elif m_types[0].startswith("u_") and m_types[1].startswith("u_"):
        matched = False
    else:
        raise ValueError
    # sns.set_style("whitegrid")
    # sns.set_palette('colorblind')
    # c1=sns.color_palette()[2]
    # c2=sns.color_palette()[0] #'b'
    c1 = "b"
    c2 = "r"
    ###
    types = ["PSF", "SIMP", "EXP", "DEV", "COMP"]
    ind = np.arange(len(types))  # the x locations for the groups
    width = 0.35  # the width of the bars
    ###
    ht_decam, ht_bokmos = np.zeros(5, dtype=int), np.zeros(5, dtype=int)
    for cnt, typ in enumerate(types):
        ht_decam[cnt] = np.where(obj[m_types[0]].data["type"] == typ)[0].shape[0] / float(obj["deg2_decam"])
        ht_bokmos[cnt] = np.where(obj[m_types[1]].data["type"] == typ)[0].shape[0] / float(obj["deg2_bokmos"])
    ###
    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, ht_decam, width, color=c1)
    rects2 = ax.bar(ind + width, ht_bokmos, width, color=c2)
    ylab = ax.set_ylabel("counts/deg2")
    if matched:
        ti = ax.set_title("Matched")
    else:
        ti = ax.set_title("Unmatched")
    ax.set_xticks(ind + width)
    ax.set_xticklabels(types)
    ax.legend((rects1[0], rects2[0]), ("DECaLS", "BASS/MzLS"), **leg_args)
    # save
    if matched:
        name = "hist_types_Matched%s.png" % addname
    else:
        name = "hist_types_Unmatched%s.png" % addname
    plt.savefig(os.path.join(get_outdir("bmd"), name), bbox_extra_artists=[ylab, ti], bbox_inches="tight", dpi=150)
    plt.close()
def plot_nobs(b):
    """make histograms of nobs so can compare depths of g,r,z between the two catalogues"""
    hi = 0
    for cam in ["m_decam", "m_bokmos"]:
        for band in "grz":
            hi = np.max((hi, b[cam].data[band + "_nobs"].max()))
    bins = hi
    for cam in ["m_decam", "m_bokmos"]:
        for band in "grz":
            junk = plt.hist(b[cam].data[band + "_nobs"], bins=bins, normed=True, cumulative=True, align="mid")
            xlab = plt.xlabel("nobs %s" % band, **laba)
            ylab = plt.ylabel("CDF", **laba)
            plt.savefig(
                os.path.join(get_outdir("bmd"), "hist_nobs_%s_%s.png" % (band, cam[2:])),
                bbox_extra_artists=[xlab, ylab],
                bbox_inches="tight",
                dpi=150,
            )
            plt.close()
def plot_nobs(b):
    '''make histograms of nobs so can compare depths of g,r,z between the two catalogues'''
    hi = 0
    for cam in ['m_decam', 'm_bokmos']:
        for band in 'grz':
            hi = np.max((hi, b[cam].data[band + '_nobs'].max()))
    bins = hi
    for cam in ['m_decam', 'm_bokmos']:
        for band in 'grz':
            junk = plt.hist(b[cam].data[band + '_nobs'],
                            bins=bins,
                            normed=True,
                            cumulative=True,
                            align='mid')
            xlab = plt.xlabel('nobs %s' % band, **laba)
            ylab = plt.ylabel('CDF', **laba)
            plt.savefig(os.path.join(get_outdir('bmd'),
                                     'hist_nobs_%s_%s.png' % (band, cam[2:])),
                        bbox_extra_artists=[xlab, ylab],
                        bbox_inches='tight',
                        dpi=150)
            plt.close()
def plot_psf_hists(decam, bokmos, zoom=False):
    """decam,bokmos are DECaLS() objects matched to decam ra,dec"""
    # divide into samples of 0.25 mag bins, store q50 of each
    width = 0.25  # in mag
    low_vals = np.arange(20.0, 26.0, width)
    med = {}
    for b in ["g", "r", "z"]:
        med[b] = np.zeros(low_vals.size) - 100
    for i, low in enumerate(low_vals):
        for band in ["g", "r", "z"]:
            ind = np.all((low <= decam[band + "mag"], decam[band + "mag"] < low + width), axis=0)
            if np.where(ind)[0].size > 0:
                med[band][i] = np.percentile(bokmos[band + "mag"][ind] - decam[band + "mag"][ind], q=50)
            else:
                med[band][i] = np.nan
    # make plot
    # set seaborn panel styles
    # sns.set_style('ticks',{"axes.facecolor": ".97"})
    # sns.set_palette('colorblind')
    # setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3))  # ,sharey=True)
    plt.subplots_adjust(wspace=0.5)
    # plot
    for cnt, band in zip(range(3), ["r", "g", "z"]):
        ax[cnt].scatter(low_vals, med[band], edgecolor="b", c="none", lw=2.0)  # ,label=m_type.split('_')[-1])
        xlab = ax[cnt].set_xlabel("bins of %s (decam)" % band, **laba)
        ylab = ax[cnt].set_ylabel("q50[%s bokmos - decam]" % band, **laba)
        if zoom:
            ax[cnt].set_ylim(-0.25, 0.25)
    # sup=plt.suptitle('decam with matching bokmos',**laba)
    # save
    # sns.despine()
    if zoom:
        name = "median_color_diff_zoom.png"
    else:
        name = "median_color_diff.png"
    plt.savefig(os.path.join(get_outdir("bmd"), name), bbox_extra_artists=[xlab, ylab], bbox_inches="tight", dpi=150)
    plt.close()
def plot_matched_dmag_vs_psf_fwhm(obj, type="psf"):
    """using matched sample, plot diff in mags vs. DECAM psf_fwhm in bins 
    obj['m_decam'] is DECaLS() object"""
    # indices
    index = np.all(
        (indices_for_type(b, inst="m_decam", type=type), indices_for_type(b, inst="m_bokmos", type=type)), axis=0
    )  # both bokmos and decam of same type
    # bin up by DECAM psf_fwhm
    bin_edges = np.linspace(0, 3, num=6)
    vals = {}
    for band in ["g", "r", "z"]:
        vals[band] = {}
        vals[band]["binc"], count, vals[band]["q25"], vals[band]["q50"], vals[band]["q75"] = bin_up(
            obj["m_decam"].data[band + "_psf_fwhm"][index],
            obj["m_bokmos"].data[band + "mag"][index] - obj["m_decam"].data[band + "mag"][index],
            bin_edges=bin_edges,
        )
    # setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    text_args = dict(verticalalignment="center", horizontalalignment="left", fontsize=10)
    # plot
    for cnt, band in zip(range(3), ["g", "r", "z"]):
        ax[cnt].plot(vals[band]["binc"], vals[band]["q50"], c="b", ls="-", lw=2)
        ax[cnt].fill_between(vals[band]["binc"], vals[band]["q25"], vals[band]["q75"], color="b", alpha=0.25)
        ax[cnt].text(0.05, 0.95, band, transform=ax[cnt].transAxes, **text_args)
    # finish
    xlab = ax[1].set_xlabel("decam PSF_FWHM", **laba)
    ylab = ax[0].set_ylabel(r"Median $\Delta \, m$ (decam - bokmos)", **laba)
    ti = plt.suptitle("%s Objects, Matched" % type.upper())
    plt.savefig(
        os.path.join(get_outdir("bmd"), "dmag_vs_psf_fwhm_%s.png" % type),
        bbox_extra_artists=[ti, xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_matched_separation_hist(d12):
    """d12 is array of distances in degress between matched objects"""
    # pixscale to convert d12 into N pixels
    pixscale = dict(decam=0.25, bokmos=0.45)
    # sns.set_style('ticks',{"axes.facecolor": ".97"})
    # sns.set_palette('colorblind')
    # setup plot
    fig, ax = plt.subplots()
    # plot
    ax.hist(d12 * 3600, bins=50, color="b", align="mid")
    ax2 = ax.twiny()
    ax2.hist(d12 * 3600.0 / pixscale["bokmos"], bins=50, color="g", align="mid", visible=False)
    xlab = ax.set_xlabel("arcsec")
    xlab = ax2.set_xlabel("pixels [BASS]")
    ylab = ax.set_ylabel("Matched")
    # save
    # sns.despine()
    plt.savefig(
        os.path.join(get_outdir("bmd"), "separation_hist.png"),
        bbox_extra_artists=[xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_confusion_matrix(cm, ticknames, addname=""):
    """cm -- NxN array containing the Confusion Matrix values
    ticknames -- list of strings of length == N, column and row names for cm plot"""
    plt.imshow(cm, interpolation="nearest", cmap=plt.cm.Blues, vmin=0, vmax=1)
    cbar = plt.colorbar()
    plt.xticks(range(len(ticknames)), ticknames)
    plt.yticks(range(len(ticknames)), ticknames)
    ylab = plt.ylabel("True (DECaLS)")
    xlab = plt.xlabel("Predicted (BASS/MzLS)")
    for row in range(len(ticknames)):
        for col in range(len(ticknames)):
            if np.isnan(cm[row, col]):
                plt.text(col, row, "n/a", va="center", ha="center")
            elif cm[row, col] > 0.5:
                plt.text(col, row, "%.2f" % cm[row, col], va="center", ha="center", color="yellow")
            else:
                plt.text(col, row, "%.2f" % cm[row, col], va="center", ha="center", color="black")
    plt.savefig(
        os.path.join(get_outdir("bmd"), "confusion_matrix%s.png" % addname),
        bbox_extra_artists=[xlab, ylab],
        bbox_inches="tight",
        dpi=150,
    )
    plt.close()
def plot_SN_vs_mag(obj, found_by='matched', type='all', addname=''):
    '''obj['m_decam'] is DECaLS() object
    found_by -- 'matched' or 'unmatched' 
    type -- all,psf,lrg'''
    #indices for type == all,psf, or lrg
    assert (found_by == 'matched' or found_by == 'unmatched')
    prefix = found_by[0] + '_'  # m_ or u_
    index = {}
    for key in ['decam', 'bokmos']:
        index[key] = indices_for_type(obj, inst=prefix + key, type=type)
    #bin up SN values
    min, max = 18., 25.
    bin_SN = dict(decam={}, bokmos={})
    for key in bin_SN.keys():
        for band in ['g', 'r', 'z']:
            bin_SN[key][band] = {}
            i = index[key]
            bin_edges = np.linspace(min, max, num=30)
            bin_SN[key][band]['binc'],count,bin_SN[key][band]['q25'],bin_SN[key][band]['q50'],bin_SN[key][band]['q75']=\
                    bin_up(obj[prefix+key].data[band+'mag'][i], \
                           obj[prefix+key].data[band+'flux'][i]*np.sqrt(obj[prefix+key].data[band+'flux_ivar'][i]),\
                                bin_edges=bin_edges)
    #setup plot
    fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    plt.subplots_adjust(wspace=0.25)
    #plot SN
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        #horiz line at SN = 5
        ax[cnt].plot([1, 40], [5, 5], 'k--', lw=2)
        #data
        for inst, color, lab in zip(['decam', 'bokmos'], ['b', 'g'],
                                    ['DECaLS', 'BASS/MzLS']):
            ax[cnt].plot(bin_SN[inst][band]['binc'],
                         bin_SN[inst][band]['q50'],
                         c=color,
                         ls='-',
                         lw=2,
                         label=lab)
            ax[cnt].fill_between(bin_SN[inst][band]['binc'],
                                 bin_SN[inst][band]['q25'],
                                 bin_SN[inst][band]['q75'],
                                 color=color,
                                 alpha=0.25)
    #labels
    ax[2].legend(loc=1, **leg_args)
    for cnt, band in zip(range(3), ['g', 'r', 'z']):
        ax[cnt].set_yscale('log')
        xlab = ax[cnt].set_xlabel('%s' % band, **laba)
        ax[cnt].set_ylim(1, 100)
        ax[cnt].set_xlim(20., 26.)
    ylab = ax[0].set_ylabel('S/N', **laba)
    text_args = dict(verticalalignment='bottom',
                     horizontalalignment='right',
                     fontsize=10)
    ax[2].text(26, 5, 'S/N = 5  ', **text_args)
    plt.savefig(os.path.join(get_outdir('bmd'),
                             'sn_%s_%s%s.png' % (found_by, type, addname)),
                bbox_extra_artists=[xlab, ylab],
                bbox_inches='tight',
                dpi=150)
    plt.close()
import astropy.io.fits as fits
import astropy.cosmology as co
c1 = co.Planck15
import numpy as np
import glob
import os
import sys
import matplotlib.pyplot as plt
from scipy.spatial import KDTree
import scipy.stats as st

from legacyanalysis.pathnames import get_indir,get_outdir,make_dir

f = open(os.path.join(get_outdir('dr23'),"out-ELG-decals.txt"),'w')

f.write("field 242 \n")

cFile1 = os.path.join(get_indir('dr23'),"catalog-EDR-242.2-243.7-8-12.1-DR3.fits")
f.write("total   primary  any mask \n")

hdu = fits.open(cFile1)
dat = hdu[1].data
prim = (dat['brick_primary'])
noJunk = (dat['brick_primary']) & (dat['decam_anymask'].T[1]==0) & (dat['decam_anymask'].T[2]==0) & (dat['decam_anymask'].T[4]==0) 
psf = (noJunk) & (dat['type'] == "PSF")
areaC1 = ( numpy.max(dat['ra']) - numpy.min(dat['ra']) ) * ( numpy.max(dat['dec']) - numpy.min(dat['dec']) )*numpy.cos( numpy.pi * numpy.mean(dat['dec']) / 180. )
f.write("DR3 : "+str(len(dat))+" "+str( len(prim.nonzero()[0]))+" "+str( len(noJunk.nonzero()[0]))+" "+str( len(psf.nonzero()[0]))+" "+str( areaC1)+"\n")
f.write("DR3 : "+str(numpy.round(len(dat)/areaC1))+" "+str( numpy.round(len(prim.nonzero()[0])/areaC1))+" "+str( numpy.round(len(noJunk.nonzero()[0]) /areaC1) )+" "+str( numpy.round(len(psf.nonzero()[0]) /areaC1) )+"\n")

cFile1 =os.path.join(get_indir('dr23'), "catalog-EDR-242.2-243.7-8-12.1-DR2.fits")
indir= get_indir('cosmos')

#CREATES THE CATALOG LIST
catList = ["catalog-R3-R4.fits",
"catalog-R2-R4.fits",
"catalog-R2-R3.fits",
"catalog-R1-R4.fits",
"catalog-R1-R3.fits",
"catalog-R1-R2.fits"]
for cnt,cat in enumerate(catList): catList[cnt]= os.path.join(indir,cat) 
# EACH CATALOG NEEDS TO HAVE THE TYPICAL DECALS CATALOG ENTRIES WITH "_1" AND "_2" APPENDED FOR DR2 and DR3

# DEFINES THE GAUSSIAN FUNCTION
gfun = lambda x, m0, s0 : st.norm.pdf(x,loc=m0,scale=s0)
#OPENS A FILE TO WRITE OUTPUTS
f=open(os.path.join(get_outdir('cosmos'),"depth-comparisonp.txt"),"w")
f.write("20<g<21.5 \n")
# CREATES A FIGURE
plt.figure(2,(5,5))
plt.axes([0.17,0.15,0.75,0.75])
# PLOT THE EXPECTED NORMAL DISTRIBUTION
plt.plot(np.arange(-10,6,0.1), st.norm.pdf(np.arange(-10,6,0.1),loc=0,scale=1), 'k--', lw=2, label='N(0,1)')

# LOOPS OVER MATCHED CATALOGS
for ii, el in enumerate(catList):
    hdu=fits.open(el)
    dr2=hdu[1].data
        # DEFINES MAGNITUDES TO SELECT A MAGNITUDE BIN
    g_mag_dr2 = 22.5 - 2.5 * np.log10(dr2['decam_flux_2'].T[1] / dr2['decam_mw_transmission_2'].T[1])
    r_mag_dr2 = 22.5 - 2.5 * np.log10(dr2['decam_flux_2'].T[2] / dr2['decam_mw_transmission_2'].T[2])
    z_mag_dr2 = 22.5 - 2.5 * np.log10(dr2['decam_flux_2'].T[4] / dr2['decam_mw_transmission_2'].T[4])
from legacyanalysis.pathnames import get_indir, get_outdir, make_dir
indir = get_indir('cosmos')

#CREATES THE CATALOG LIST
catList = [
    "catalog-R3-R4.fits", "catalog-R2-R4.fits", "catalog-R2-R3.fits",
    "catalog-R1-R4.fits", "catalog-R1-R3.fits", "catalog-R1-R2.fits"
]
for cnt, cat in enumerate(catList):
    catList[cnt] = os.path.join(indir, cat)
# EACH CATALOG NEEDS TO HAVE THE TYPICAL DECALS CATALOG ENTRIES WITH "_1" AND "_2" APPENDED FOR DR2 and DR3

# DEFINES THE GAUSSIAN FUNCTION
gfun = lambda x, m0, s0: st.norm.pdf(x, loc=m0, scale=s0)
#OPENS A FILE TO WRITE OUTPUTS
f = open(os.path.join(get_outdir('cosmos'), "depth-comparisonp.txt"), "w")
f.write("20<g<21.5 \n")
# CREATES A FIGURE
plt.figure(2, (5, 5))
plt.axes([0.17, 0.15, 0.75, 0.75])
# PLOT THE EXPECTED NORMAL DISTRIBUTION
plt.plot(np.arange(-10, 6, 0.1),
         st.norm.pdf(np.arange(-10, 6, 0.1), loc=0, scale=1),
         'k--',
         lw=2,
         label='N(0,1)')

# LOOPS OVER MATCHED CATALOGS
for ii, el in enumerate(catList):
    hdu = fits.open(el)
    dr2 = hdu[1].data