Esempio n. 1
0
def table2curves(name, tbl, bands=None):
    time = tbl['time']
    curves = SetLightCurve(name)

    if bands is None:
        bands = [n for n in tbl.dtype.names if band.band_is_exist(n)]

    for bname in bands:
        b = band.band_by_name(bname)
        mag = tbl[bname]
        lc = LightCurve(b, time, mag)
        curves.add(lc)
    return curves
Esempio n. 2
0
def main():
    path = '/home/bakl/Sn/Release/seb_git/res/tt/tanaka'
    mname = 'cat_R500_M15_Ni008_E40'

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hp:i:b:")
    except getopt.GetoptError as err:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-i':
            mname = str(arg)
            break
        if opt == '-b':
            bands = str(arg).split('-')
            for b in bands:
                if not band.band_is_exist(b):
                    print 'No such band: ' + b
                    sys.exit(2)
            continue
        if opt == '-t':
            is_plot_time_points = True
            continue
        if opt == '-p':
            path = str(arg)
            if not (os.path.isdir(path) and os.path.exists(path)):
                print "No such directory: " + path
                sys.exit(2)
            continue
        elif opt == '-h':
            usage()
            sys.exit(2)

    compare_ABzVSugri(mname, path)

    path = '/home/bakl/Sn/Release/seb_git/res/tt'
    compare_ttVSubv(mname, path)
Esempio n. 3
0
def main(name="", path="./"):
    model_ext = ".ph"
    is_time_points = False
    z = 0
    distance = 10.0  # pc

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hwtp:i:b:")
    except getopt.GetoptError as err:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    if not name:
        if len(opts) == 0:
            usage()
            sys.exit(2)
        for opt, arg in opts:
            if opt == "-i":
                path = ROOT_DIRECTORY
                name = str(arg)
                break

    bands = ["UVW1", "U"]

    for opt, arg in opts:
        if opt == "-b":
            bands = str(arg).split("-")
            for b in bands:
                if not band.band_is_exist(b):
                    print "No such band: " + b
                    sys.exit(2)
        elif opt == "-p":
            path = os.path.expanduser(str(arg))
            if not (os.path.isdir(path) and os.path.exists(path)):
                print "No such directory: " + path
                sys.exit(2)
        elif opt == "-t":
            is_time_points = True
        elif opt == "-h":
            usage()
            sys.exit(2)

    names = []
    if name != "":
        names.append(name)
    else:  # run for all files in the path
        files = [f for f in os.listdir(path) if isfile(join(path, f)) and f.endswith(model_ext)]
        for f in files:
            names.append(os.path.splitext(f)[0])

    if len(names) > 0:
        dic_results = {}  # dict((k, None) for k in names)
        i = 0
        for name in names:
            i += 1
            mags = compute_mag(name, path, bands, z=z, distance=distance, t_cut=0.1, t_up=5.0, tdiff=0.5)
            dmdt = compute_dmdt(mags, bands, is_spline=True, s=0.0)
            dic_results[name] = dict(m=mags, d=dmdt)
            print "Finish: %s [%d/%d]" % (name, i, len(names))
        plot_dmdt(dic_results, bands, is_time_points=is_time_points)

    else:
        print "There are no models in the directory: %s with extension: %s " % (path, model_ext)
Esempio n. 4
0
def main(name='', path='./'):
    is_silence = False
    is_fit = False
    is_plot_ubv = False
    is_fit_bakl = False
    model_ext = '.tt'
    ubv_args = ''

    try:
        opts, args = getopt.getopt(sys.argv[1:], "fhstp:i:b:o:")
    except getopt.GetoptError as err:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    if not name:
        if len(opts) == 0:
            usage()
            sys.exit(2)
        for opt, arg in opts:
            if opt == '-i':
                path = ROOT_DIRECTORY
                name = str(arg)
                break

    bands = ['V', 'I']
    # set_bands = ['B-V', 'B-V-I', 'V-I', 'J-H-K']
    # set_bands = ['U-B-V-I', 'U-B-V-R-I', 'U-B', 'V-R', 'B-V-I', 'B-V', 'V-I']

    for opt, arg in opts:
        if opt == '-e':
            model_ext = '.' + arg
            continue
        if opt == '-b':
            bands = str(arg).split('-')
            for b in bands:
                if not band.band_is_exist(b):
                    print 'No such band: ' + b
                    sys.exit(2)
            continue
        if opt == '-o':
            ops = str(arg).split(':')
            is_plot_ubv = "ubv" in ops
            is_plot_Tnu = "Tnu" in ops
            is_plot_time_points = "time" in ops
            is_fit = "fit" in ops
            is_fit_bakl = "fitb" in ops
            ubv_args += " %s %s ".format(opt, arg)
            continue
        if opt == '-s':
            is_save = True
            ubv_args += opt + ' '
            continue
        if opt == '-f':
            is_force = True
            is_save = True
            continue
        if opt == '-p':
            path = os.path.expanduser(str(arg))
            if not (os.path.isdir(path) and os.path.exists(path)):
                print "No such directory: " + path
                sys.exit(2)
            continue
        elif opt == '-h':
            usage()
            sys.exit(2)

    names = []
    if name != '':
        names.append(name)
    else:  # run for all files in the path
        files = [f for f in os.listdir(path) if isfile(join(path, f)) and f.endswith(model_ext)]
        for f in files:
            names.append(os.path.splitext(f)[0])

    distance = 10.  # pc for Absolute magnitude
    # distance = 10e6  # pc for Absolute magnitude
    z = phys.H0 * (distance / 1e6) / (phys.c / 1e5)  # convert D to Mpc, c to km/c
    t50 = 50.
    t_beg = max(0., t50 - 10.)
    t_end = t50 + 10.

    if len(names) > 0:
        res = run_scm(bands, distance, names, path, t50, t_beg, t_end, z)
        if len(res) > 0:
            fig = plot_scm(res, names, bands, z, is_fit=True)
            if is_save:
                fsave = os.path.join(os.path.expanduser('~/'), 'scm_' + '_'.join(bands) + '.pdf')
                print "Save plot in %s" % fsave
                fig.savefig(fsave, bbox_inches='tight', format='pdf')
    else:
        print "There are no models in the directory: %s with extension: %s " % (path, model_ext)
Esempio n. 5
0
def main(name='', model_ext='.ph'):
    is_quiet = True
    is_save_mags = False
    is_save_plot = False
    is_plot_time_points = False
    is_extinction = False
    is_vel = False

    label = None
    fsave = None
    t_diff = 1.00001
    # path = ''
    path = os.getcwd()
    z = 0
    e = 0.
    magnification = 1.
    distance = 10.  # pc
    callback = None
    xlim = None
    ylim = None
    # bshift = None
    bshift = {}

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hqwtc:d:p:e:i:b:l:m:vs:x:y:z:")
    except getopt.GetoptError as err:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    if len(args) > 0:
        path, name = os.path.split(str(args[0]))
        path = os.path.expanduser(path)
        name = name.replace('.ph', '')
    elif len(opts) == 0:
        usage()
        sys.exit(2)

    if not name:
        for opt, arg in opts:
            if opt == '-i':
                name = os.path.splitext(os.path.basename(str(arg)))[0]
                break

    bands = ['U', 'B', 'V', 'R', "I"]
    # bands = ['U', 'B', 'V', 'R', "I", 'UVM2', "UVW1", "UVW2", 'g', "r", "i"]

    for opt, arg in opts:
        if opt == '-e':
            e = float(arg)
            is_extinction = True
            continue
        if opt == '-b':
            bands = []
            band.Band.load_settings()
            for b in str(arg).split('-'):
                # extract band shift
                if ':' in b:
                    bname, shift = b.split(':')
                    if '_' in shift:
                        bshift[bname] = -float(shift.replace('_', ''))
                    else:
                        bshift[bname] = float(shift)
                else:
                    bname = b
                if not band.band_is_exist(bname):
                    print 'No such band: ' + bname
                    sys.exit(2)
                bands.append(bname)
            continue
        if opt == '-c':
            c = lc_wrapper(str(arg))
            if callback is not None:
                c = cb.CallBackArray((callback, c))
            callback = c
            continue
        if opt == '-q':
            is_quiet = False
            continue
        if opt == '-s':
            is_save_plot = True
            fsave = str.strip(arg)
            continue
        if opt == '-w':
            is_save_mags = True
            continue
        if opt == '-t':
            is_plot_time_points = True
            continue
        if opt == '-v':
            is_vel = True
            continue
        if opt == '-m':
            magnification = float(arg)
            continue
        if opt == '-z':
            z = float(arg)
            continue
        if opt == '-d':
            distance = float(arg)
            continue
        if opt == '-l':
            label = str.strip(arg)
            continue
        if opt == '-x':
            xlim = map(float, str(arg).split(':'))
            continue
        if opt == '-y':
            ylim = map(float, str(arg).split(':'))
            continue
        if opt == '-p':
            path = os.path.expanduser(str(arg))
            if not (os.path.isdir(path) and os.path.exists(path)):
                print "No such directory: " + path
                sys.exit(2)
            continue
        elif opt == '-h':
            usage()
            sys.exit(2)

    print "Plot magnitudes on z=%f at distance=%e [cosmology D(z)=%s Mpc]" % (z, distance, cosmology_D_by_z(z))

    names = []
    if name != '':
        names.append(name)
    else:  # run for all files in the path
        names = get_model_names(path, model_ext)

    if len(names) > 0:
        models_mags = {}  # dict((k, None) for k in names)
        models_vels = {}  # dict((k, None) for k in names)
        i = 0
        for name in names:
            i += 1
            # mags = lcf.compute_mag(name, path, bands, ext=ext, z=z, distance=distance, magnification=magnification,
            #                        t_diff=t_diff, is_show_info=not is_quiet, is_save=is_save_mags)
            curves = lcf.curves_compute(name, path, bands, z=z, distance=distance, magnification=magnification,
                                        is_save=is_save_mags)
            if is_extinction:
                lcf.curves_reddening(curves, ebv=e, z=z)
            # lcf.plot_curves(curves)
            # exit()
            # models_mags[name] = mags
            models_mags[name] = curves

            if not is_quiet:
                # z, distance = 0.145, 687.7e6  # pc for comparison with Maria
                # lcf.plot_bands(mags, bands, title=name, fname='', is_time_points=is_plot_time_points)
                lcf.plot_bands(curves, bands, title=name, fname='', is_time_points=is_plot_time_points)

            if is_vel:
                vels = vel.compute_vel(name, path, z=z)
                if vels is None:
                    sys.exit("No data for: %s in %s" % (name, path))
                models_vels[name] = vels
                print "Finish velocity: %s [%d/%d]" % (name, i, len(names))
            else:
                models_vels = None
                print "Finish mags: %s [%d/%d] in %s" % (name, i, len(names), path)

        if label is None:
            if callback is not None:
                label = "ts=%s z=%4.2f D=%6.2e mu=%3.1f ebv=%4.2f" % (
                    callback.arg_totext(0), z, distance, magnification, e)
            else:
                label = "z=%4.2f D=%6.2e mu=%3.1f ebv=%4.2f" % (z, distance, magnification, e)

        if is_save_plot:
            if len(fsave) == 0:
                if is_vel:
                    fsave = "ubv_vel_%s" % name
                else:
                    fsave = "ubv_%s" % name

            if is_extinction and e > 0:
                fsave = "%s_e0%2d" % (fsave, int(e * 100))  # bad formula for name

            d = os.path.expanduser('~/')
            # d = '/home/bakl/Sn/my/conf/2016/snrefsdal/img'
            fsave = os.path.join(d, fsave) + '.pdf'

        plot_all(models_vels, models_mags, bands, call=callback, xlim=xlim, ylim=ylim,
                 is_time_points=is_plot_time_points, title=label, fsave=fsave, bshift=bshift)
        # plot_all(dic_results, bands,  xlim=(-10, 410), is_time_points=is_plot_time_points)
        # plot_all(dic_results, bands, xlim=(-10, 410), callback=callback, is_time_points=is_plot_time_points)
        # plot_all(dic_results, bands,  ylim=(40, 23),  is_time_points=is_plot_time_points)
    else:
        print "There are no models in the directory: %s with extension: %s " % (path, model_ext)
Esempio n. 6
0
def main(name="", path="./", is_force=False, is_save=False, is_plot_Tnu=False, is_plot_time_points=False):
    is_info = False
    is_fit = False
    is_save_plot = False
    is_fit_bakl = False
    model_ext = ".tt"
    theta_dic = None
    distance = rf.pc_to_cm(10.0)  # pc
    z = 0.0

    band.Band.load_settings()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "fhswtb:d:e:i:p:o:z:")
    except getopt.GetoptError as err:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    if len(args) > 0:
        path, name = os.path.split(str(args[0]))
        path = os.path.expanduser(path)
    elif len(opts) == 0:
        usage()
        sys.exit(2)

    if not name:
        for opt, arg in opts:
            if opt == "-i":
                name = os.path.splitext(os.path.basename(str(arg)))[0]
                break

    set_bands = ["B-V", "B-V-I", "V-I", "J-H-K"]
    # set_bands = ['B-V', 'B-V-I', 'V-I', 'J-H-K']
    # set_bands = ['U-B-V-I', 'U-B-V-R-I', 'U-B', 'V-R', 'B-V-I', 'B-V', 'V-I']

    for opt, arg in opts:
        if opt == "-e":
            model_ext = "." + arg
            continue
        if opt == "-b":
            set_bands = str(arg).split("_")
            for bset in set_bands:
                for b in bset.split("-"):
                    if not band.band_is_exist(b):
                        print "No such band: " + b
                        sys.exit(2)
            continue
        if opt == "-z":
            z = float(arg)
            continue
        if opt == "-d":
            distance = rf.pc_to_cm(float(arg))
            continue
        if opt == "-s":
            is_save_plot = True
            continue
        if opt == "-o":
            ops = str(arg).split(":")
            is_plot_Tnu = "Tnu" in ops
            is_plot_time_points = "time" in ops
            is_fit = "fit" in ops
            is_fit_bakl = "fitb" in ops
            continue
        if opt == "-w":
            is_save = True
            continue
        if opt == "-f":
            is_force = True
            is_save = True
            continue
        if opt == "-p":
            path = os.path.expanduser(str(arg))
            if not (os.path.isdir(path) and os.path.exists(path)):
                print "No such directory: " + path
                sys.exit(2)
            continue
        elif opt == "-h":
            usage()
            sys.exit(2)

    names = []
    if name != "":
        names.append(name)
    else:  # run for all files in the path
        names = get_model_names(path, model_ext)

    if len(names) > 0:
        results = {}  # dict((k, None) for k in names)
        ib = 0
        for bset in set_bands:
            ib += 1
            im = 0
            dic = {}
            print "\nRun: %s [%d/%d], z=%.2f, d=%.2e" % (bset, ib, len(set_bands), z, distance)
            for name in names:
                res = None
                im += 1
                print "Run: %s [%d/%d]" % (name, im, len(names))
                is_err = False
                fname = cache_name(name, path, bset)
                if not is_force and os.path.exists(fname):
                    res = cache_load(fname)
                else:
                    res = compute_tcolor(name, path, bset.split("-"), d=distance, z=z, t_cut=0.9)
                    if is_save and res is not None:
                        print "Save Tcolor & Zeta for %s in %s" % (bset, fname)
                        cache_save(res, fname=fname)

                dic[name] = res
                # check for errors
                idx = np.argmin(np.abs(res["Tcol"] - 1.0e4))
                if abs(res["zeta"][idx] - 1.0) <= 1e-3:
                    is_err = True
            if is_err:
                print " ERROR for %s in %s" % (name, bset)

            results[bset] = dic
            print "Finish: %s" % name
        if is_fit:
            theta_dic = {}
            im = 0
            results_flter = {}
            for bset in set_bands:
                im += 1
                models = results[bset]
                tlim = (7.0, 80)
                if tlim is not None:
                    models_zt_new = {}
                    for mname, tbl in models.iteritems():
                        models_zt_new[mname] = table_cut_by_col(tbl, tlim, "time")
                    models = models_zt_new

                # templim = (4.e3, 12e3)
                # if templim is not None:
                #     models_zt_new = {}
                #     for mname, tbl in models.iteritems():
                #         models_zt_new[mname] = table_cut_by_col(tbl, templim, 'Tcol')
                #     models = models_zt_new

                print "\nFit: %s [%d/%d]" % (bset, im, len(set_bands))
                theta = fit_bayesian(models, is_debug=True, is_info=is_info, title=bset)
                results_flter[bset] = models
                theta_dic[bset] = theta
                print_coef(theta)

        # fig = plot_zeta(results, set_bands, theta_dic, t_cut=1.9, is_fit=is_fit, is_fit_bakl=is_fit_bakl,
        fig = plot_zeta(
            results_flter,
            set_bands,
            theta_dic,
            t_cut=1.9,
            is_fit=is_fit,
            is_fit_bakl=is_fit_bakl,
            is_plot_Tnu=is_plot_Tnu,
            is_time_points=is_plot_time_points,
        )
        if is_save_plot and len(results) > 0:
            fsave = os.path.join(os.path.expanduser("~/"), "epm_" + "_".join(set_bands) + ".pdf")
            print "Save plot in %s" % fsave
            fig.savefig(fsave, bbox_inches="tight", format="pdf")

    else:
        print "There are no models in the directory: %s with extension: %s " % (path, model_ext)