Ejemplo n.º 1
0
from __future__ import division, print_function, absolute_import

from MatchParam import MatchParam
"""
This runs through example code in using MatchParam.py.  This file is Python 3.x compatible.
"""

# Pass in MATCH parameter with 2 filters (1 CMD) with no zinc or background.
no_zinc_2filters_woback = MatchParam("no_zinc_2filters_woback.param")
no_zinc_2filters_woback.printKeys(
)  # to see the keys for you instance use this
print()
print("Current values from the passed in parameter file:")
no_zinc_2filters_woback.print()
print()

# Changing basic values
no_zinc_2filters_woback.change("dAv", 0.1)  # change from 0.2 to 0.1
print("The differential reddening has changed:")
no_zinc_2filters_woback.print()
print()

# Change the list of time bins need to specify my new start and end times
start = [6.6, 6.8, 7.0, 7.2]
end = [6.8, 7.0, 7.2, 7.4]
num = len(start)
no_zinc_2filters_woback.change("Ntbins", num)
no_zinc_2filters_woback.change("tstart", start)
no_zinc_2filters_woback.change("tend", end)
print("Changed time bins:")
no_zinc_2filters_woback.print()
Ejemplo n.º 2
0
def singleRun(args):
    """
    Takes in a string of arguments that are required to have a ".phot" and ".fake" file followed by optional
    MATCH flags or a ".param" file.
    This will return, in a list, a string MATCH command to be sent off.
    """
    args = " ".join(args)
    if ".fake" not in args and ".phot" not in args:
        print("Missing \".phot\" and/or \".fake\" file(s)")
        sys.exit(1)

    fakeFile = None
    photFile = None
    paramFile = None
    fitName = None

    workingD = os.getcwd(
    ) + "/"  # gets the directory the executable has been invoked in

    # parse arguments to extract them
    args = args.split()
    print("Arguements:", args)
    idx = [
    ]  # indices to delete after extracting the file names needed to run MATCH
    for i, arg in enumerate(args):
        if ".fake" in arg:
            print("Found fake file:", arg)
            fakeFile = arg
            idx.append(i)
        if ".phot" in arg:
            print("Found photometry file:", arg)
            photFile = arg
            idx.append(i)
        if ".param" in arg:
            print("Found parameter file:", arg)
            paramFile = arg
            idx.append(i)
        if "fit" in arg:
            print("Found fit name:", arg)
            fitName = arg
            idx.append(i)

    # delete extracted file names in args
    args = [args[i] for i in xrange(len(args)) if i not in set(idx)]
    print("Remaining arguements:", args)

    # process any other arguements
    flags = None
    if len(arg) > 0:
        flags = parse(args)
    print("Retrieved flags:", flags)

    # if there is not passed in ".param" file then generate one based off the default one in the executable directory
    param = None
    if paramFile is None:  # generate ".param" file and save it in working directory.
        # sys.argv[0] gives the location of the executable
        param = MatchParam(toExecutable + "/default.param",
                           workingD + photFile, workingD + fakeFile)
        if "-ssp" in flags:
            param.ssp = True
        background = param.get("background")
        if background is not None and "default" in background:
            back = raw_input("Specify Background: ")
            param.change("background=%s" % (workingD + back))
        if param.get("scale") == "scale":
            scale = raw_input("Specify scale: ")
            try:
                scale = float(scale)
            except ValueError:
                print("Not a float try again...")
                scale = raw_input("Specify scale: ")

            param.change("scale=%f" % float(scale))
        param.save()
        paramFile = param.name
        # make symbolic link here
        if not os.path.isfile(workingD + "parameters.param"):
            subprocess.call(
                ["ln", "-s", param.savedTo, workingD + "parameters.param"])

    else:  # passed in parameter file no need to call a save on a MatchParam object (mostly used to scan for zinc)
        if os.path.isfile(workingD + paramFile):
            param = MatchParam(workingD + paramFile, workingD + photFile,
                               workingD + fakeFile)
            if "-ssp" in flags:
                param.ssp = True
            if param.zinc and param.ssp:  # can't have zinc and ssp both true need to make a new file otherwise
                print(paramFile.split("."))
                newFileName = paramFile.split(".")
                newFileName[0] += "_ssp"
                newFileName = ".".join(newFileName)
                answer = raw_input(
                    "Found zinc and ssp flags does the user want to create a new parameter file with name %s? (y/n) "
                    % newFileName)
                if answer in ['Y', 'y']:
                    # change the logZmin
                    param.change("logZmin=-1.5")
                    param.save(name=newFileName)
                else:
                    print("Zinc and ssp flags both specified...exiting")
                    sys.exit(1)
            if param.calculateMaxOrMin:  # Had to calculate filter mins or maxes and so we save over the file
                param.save(workingD, paramFile)

        else:
            answer = raw_input(
                "User specified parameter file but we did not find it, make one with this name %s? (y/n) "
                % paramFile)
            print("answer:", answer)
            if answer in ['Y', 'y']:
                param = MatchParam(toExecutable + "/default.param",
                                   workingD + photFile, workingD + fakeFile)
                if "-ssp" in flags:
                    param.ssp = True
                background = param.get("background")
                print("BACKGROUND:", background)
                if background is not None and "default" in background:
                    back = raw_input("Specify Background: ")
                    param.change("background=%s" % (workingD + back))
                if param.get("scale") == "scale":
                    scale = raw_input("Specify scale: ")

                    try:
                        scale = float(scale)
                    except ValueError:
                        print("Not a float try again...")
                        scale = raw_input("Specify scale: ")

                    param.change("scale=%f" % float(scale))

                param.save(name=paramFile)
            else:
                print(
                    "Specified parameter name that does not exit in current directory..."
                )
                sys.exit(1)

    if param.zinc and not param.ssp:
        flags.append("-zinc")

    command = ""  # MATCH command that will be sent to server

    # build command (explicitely shown)
    command += "calcsfh "
    command += workingD + paramFile + " "
    command += workingD + photFile + " "
    command += workingD + fakeFile + " "

    # get next fit name
    if fitName is None:
        fitName = getFitName()

    command += workingD + fitName

    # append flags to command
    for flag in flags:
        command += " " + flag

    # add forwarding to file in command
    command += " > " + workingD + fitName + ".co"

    # write in logging

    log = MyLogger.myLogger("generate commands",
                            toExecutable + "logs/generated_commands")
    # create stripped down command (ie no working directory included)
    stripCommand = "calcsfh " + paramFile + " " + photFile + " " + fakeFile + " " + fitName + " " + " ".join(flags) \
                   + " > " + fitName + ".co"
    # create empty file so getFitName can iterated to another fit number
    #subprocess.call('touch %s' % (os.getcwd() + "/" + fitName), shell=True)
    f = open(workingD + fitName, "w")
    f.close()

    log.info("Generated command (%s): %s" % (os.getcwd(), stripCommand))

    #print(command)

    return [command]
Ejemplo n.º 3
0
def processDAv_general(path, baseName, photFile, paramFile):
    """
    Takes in a path with a baseName that will follow standard dAv naming conventions.
    """
    dAvfile = open(path + "best_dAvs.ls", 'a')
    massFile = open(path + "best_mass.ls", 'a')
    metallicity = "z_0-19"  # Defines the metallicity to use for plotting isochrones

    path = path.strip("")
    print("PATH:", path)
    files = glob.glob(path + baseName + "_dAv_?-??")

    files = np.asarray([file for file in files if "." not in file
                        ])  # get rid of extraneous files ending with a suffix
    #print(files)

    # order files in increasing dAv
    dAvs = np.asarray([
        float(file.split("/")[-1].split("_")[-1].replace("-", "."))
        for file in files
    ])
    #print(dAvs)

    # sort by increasing dAv
    idxs = np.argsort(dAvs)
    files = files[idxs]
    dAvs = dAvs[idxs]
    #print(files, dAvs)

    # get best fits values
    vec_getBestFit = np.vectorize(getBestFit)  # vectorize function
    bestFits, bestAvs = vec_getBestFit(files)
    #print(bestFits)

    # make the fit with the best fit value the fit with the main string name.
    best_idx = np.argmin(bestFits)
    best_file = files[best_idx]
    best_files = glob.glob(best_file + "*")

    # print out the SNR with best dAv
    #print(repr(path),path.split("/"))
    best_dAv = dAvs[best_idx]
    best_Av = bestAvs[best_idx]
    dAvfile.write("%s %f %f\n" % (baseName, best_dAv, best_Av))

    # change the names
    new_names = []
    for file in best_files:
        name = file.split("/")[-1].split("_")
        if "." in name[-1]:
            name[-1] = "." + name[-1].split(".")[-1]
            name.pop(-2)
            name = "_".join(name[:-1]) + name[-1]
        else:
            name.pop(-1)
            name.pop(-1)
            name = "_".join(name)
        new_names.append(path + name)

    for j, file in enumerate(best_files):
        name = new_names[j]
        subprocess.call(['cp', file, name])

    plt.rc('font', family='sans-serif')
    params = {'mathtext.default': 'regular'}
    plt.rcParams.update(params)

    csfs = [SFH(file + ".zc", bins=24) for file in files]

    fig = plt.figure(figsize=(21.0, 9.0))
    #fig = plt.figure()

    # plot Cumulative stellar mass functions
    rainbow = iter(plt.cm.rainbow(np.linspace(0, 1, len(csfs))))
    ax = fig.add_subplot(131)
    for i, csf in enumerate(csfs):
        csf.calculateCSF()
        c = next(rainbow)
        ax.plot(csf.getX(),
                csf.getY(),
                color=c,
                linewidth=1.5,
                zorder=i / 100 + 1)

    ax.set_axis_bgcolor('0.8')
    sm = plt.cm.ScalarMappable(cmap=plt.cm.rainbow,
                               norm=plt.Normalize(vmin=dAvs[0], vmax=dAvs[-1]))
    sm.set_array(dAvs)
    cbar = fig.colorbar(sm)
    cbar.ax.tick_params(labelsize=14)

    plt.ylim([0.0, 1.0])
    plt.xlim([0, 70])
    plt.ylabel(r"Cumulative Stellar Mass", fontsize=20)
    plt.xlabel("Time (Myrs)", fontsize=20)
    ax.tick_params(labelsize=16)

    # get id for title
    split_path = path.split("/")
    split_path = [item for item in split_path if item != '']
    snr_id = split_path[-1]
    print("ID:", snr_id, split_path)
    #plt.suptitle(snr_id + ": " + baseName)

    #### Plot specific parts for the best dAv
    csf = csfs[best_idx]

    ### Add to the top of the first panel the mass derived from the age using
    # get log years
    log_year = np.arange(6.6, 7.8, 0.05)
    log_year_string = [str(year).replace(".", "-") for year in log_year]

    # get the highest initial mass for each age assuming solar of 0.019
    masses = []
    isochrones = {}
    for year in log_year_string:
        iso = pd.read_csv("../isochrones/" + metallicity + "_%s" % year,
                          delim_whitespace=True)
        isochrones[year] = iso
        masses.append(iso['M_ini'].values[-1])  # add the highest mass
    linear_year = 10**log_year / 10**6

    masses = np.asarray(masses)
    plotMasses = np.interp(ax.get_xticks(), linear_year, masses)
    plotMasses = [round(mass, 1) for mass in plotMasses]

    # prepare ticks for top x axis
    ax2_ticks = np.linspace(ax.get_xticks()[0],
                            ax.get_xticks()[-1], 2 * ax.get_xticks().size - 1)
    ax2_mass = np.round(np.interp(ax2_ticks, linear_year, masses), 2)
    ax2 = ax.twiny()
    ax2.set_xticks(ax2_ticks[::2], minor=False)
    ax2.set_xticklabels(ax2_mass[::2], size=16)
    ax2.xaxis.grid(False, which='major')
    # add minor ticks
    ax2.set_xticks(ax2_ticks[1::2], minor=True)
    ax2.set_xticklabels(ax2_mass[1::2], size=13, minor=True)
    ax2.xaxis.grid(False)

    # hand draw minor grid axis
    minor_top_ticks = ax2.get_xticks(minor=True)
    for i, xtick in enumerate(minor_top_ticks):
        ax.axvline(xtick,
                   linestyle='-.',
                   color='w',
                   zorder=(i + 0.1) / 100,
                   linewidth=1.2)
    # adjust padding
    ax2.tick_params(which='major', pad=15)
    ax2.tick_params(which='minor', pad=0)
    ax2.set_xlabel(r"Mass ($M_\odot$)", fontsize=20)
    ax2.set_axisbelow(True)

    # Interpolate the percentiles
    central_mass = None
    if np.all(np.isnan(csf.getY())):
        central_mass = (0, 0, 0)
    else:
        percentiles = interpolate([0.84, 0.5, 0.16], csf.getX(), csf.getY())

        ### plot percentiles with center as dotted line and grey band as area of error
        # plot vertical line for 50th percentile
        ax.axvline(x=percentiles[1], linestyle="--", color='0.0')

        # plot vertical translucent red region spanning the error
        ax.axvspan(xmin=percentiles[0],
                   xmax=percentiles[2],
                   color='r',
                   alpha=0.2)

        # get the actual values of mass and display on the graph
        # get the closest log year to the percentiles
        closest = getClosestLogYearIndex(percentiles, log_year)
        central_mass = (
            isochrones[log_year_string[closest[0]]]['M_ini'].values[-1],
            isochrones[log_year_string[closest[1]]]['M_ini'].values[-1],
            isochrones[log_year_string[closest[2]]]['M_ini'].values[-1])

    ax.text(35, 0.9, snr_id, fontsize=20, zorder=10)
    ax.text(30,
            0.85,
            r"$M=%.1f^{+%.1f}_{-%.1f}$" %
            (central_mass[1], central_mass[0] - central_mass[1],
             central_mass[1] - central_mass[2]),
            fontsize=20,
            zorder=10)
    massFile.write("%s %.2f %.2f %.2f\n" %
                   (baseName, central_mass[1], central_mass[0] -
                    central_mass[1], central_mass[1] - central_mass[2]))

    # plot best fit values vs dAvs
    ax = fig.add_subplot(132)
    ax.scatter(dAvs, bestFits, color='k')
    ax.set_axis_bgcolor('0.8')
    plt.ylabel("Fit value (arbitrary)", fontsize=20)
    plt.xlabel("dAv", fontsize=20)
    plt.gca().tick_params(labelsize=16, which='major')

    # plot cmd with photometry file
    ax = fig.add_subplot(133)

    #### Get data of field and photometry file #####
    # field should be from background which we get from reading the parameter file
    param = MatchParam(path + paramFile, None, None)
    background_path = param.parameters['background']
    background_data = np.loadtxt(background_path, unpack=True)

    # get photometry data
    photemetry_data = np.loadtxt(path + photFile, unpack=True)

    # get the number of columns: 2 - one CMD | 3 - 2 CMDs
    numCols = len(background_data)

    # Blue mag will be the lower filter and red_mag will be the higher filter
    blue_phot_mag = None
    blue_field_mag = None
    red_phot_mag = None
    red_field_mag = None

    limit_colors, limit_mag = None, None
    filters = param.filterSet

    if numCols == 2:
        # Assign field/backgroun data
        blue_field_mag, red_field_mag = background_data[0], background_data[1]
        # Assign photometry data
        blue_phot_mag, red_phot_mag = photemetry_data[0], photemetry_data[1]
        limit_colors, limit_mag = magLimitsGreaterFilter(
            param.parameters[filters[0] + "max"],
            param.parameters[filters[1] + "max"])
    else:  # 2 CMDs which we take the 2nd column as blue and the 3rd column as red
        blue_field_mag, red_field_mag = background_data[1], background_data[2]
        blue_phot_mag, red_phot_mag = photemetry_data[1], photemetry_data[2]
        limit_colors, limit_mag = magLimitsGreaterFilter(
            param.parameters[filters[1] + "max"],
            param.parameters[filters[2] + "max"])

    # Create masks for the field and photemetry data to filter out values of 99.99 or to large of magnitudes
    field_mask = (blue_field_mag < 30.0) & (red_field_mag < 30.0)
    phot_mask = (blue_phot_mag < 30.0) & (red_phot_mag < 30.0)

    # Apply mask
    blue_field_mag, red_field_mag = blue_field_mag[field_mask], red_field_mag[
        field_mask]
    blue_phot_mag, red_phot_mag = blue_phot_mag[phot_mask], red_phot_mag[
        phot_mask]

    # Plot field data as 2d histogram
    H, xedges, yedges = np.histogram2d(blue_field_mag - red_field_mag,
                                       red_field_mag,
                                       bins=[75, 75])
    color = plt.cm.gray
    color.set_bad("w")
    col = ax.pcolormesh(xedges,
                        yedges,
                        np.ma.masked_values(H.T, 0),
                        cmap=color)
    cbar = plt.colorbar(col)

    # Plot SNR data as scatter plot
    ax.scatter(blue_phot_mag - red_phot_mag, red_phot_mag, color='r', s=12)

    ax.plot(limit_colors, limit_mag, linestyle='--', color='green')

    plt.xlabel("F438W - F814W", fontsize=18)
    plt.ylabel("F814W", fontsize=18)
    plt.xlim([xedges.min(), xedges.max()])
    plt.ylim([yedges.min(), yedges.max()])

    ax.invert_yaxis()
    plt.gca().tick_params(labelsize=16, which='major')
    plt.tight_layout()

    plt.savefig(path + baseName + "_testfig", dpi=512)

    dAvfile.close()
    massFile.close()