Ejemplo n.º 1
0
 def test_reddening_shift(self):
     """
     see extinction_schlafly.csv
     Bandpass       λeff      2.1     3.1     4.1     5.1
     Landolt_U     3508.2    5.614   4.334   3.773   3.460
     Landolt_B     4329.0    4.355   3.626   3.290   3.096
     Landolt_V     5421.7    2.953   2.742   2.645   2.589
     Landolt_R     6427.8    2.124   2.169   2.189   2.201
     Landolt_I     8048.4    1.410   1.505   1.548   1.573
     :return:
     """
     # band, mshift = 'V', 2.742
     band, mshift = 'U', 4.334
     ebv = 1.
     lc = lc_create(band)
     magExpect = lc.Mag + mshift  # Av for e=1.
     curves_reddening(lc, ebv=ebv)
     magRed = lc.Mag
     res = filter(lambda x: abs(x) > 1e-4, magExpect - magRed)
     self.assertTrue(len(res) == 0,
                     msg="Some mags were shifted more then %f." % mshift)
Ejemplo n.º 2
0
 def test_reddening_shift(self):
     """
     see extinction_schlafly.csv
     Bandpass       λeff      2.1     3.1     4.1     5.1
     Landolt_U     3508.2    5.614   4.334   3.773   3.460
     Landolt_B     4329.0    4.355   3.626   3.290   3.096
     Landolt_V     5421.7    2.953   2.742   2.645   2.589
     Landolt_R     6427.8    2.124   2.169   2.189   2.201
     Landolt_I     8048.4    1.410   1.505   1.548   1.573
     :return:
     """
     # band, mshift = 'V', 2.742
     bname, mshift = 'U', 4.334
     ebv = 1.
     lc = lc_create(bname)
     magExpect = lc.Mag + mshift  # Av for e=1.
     lc = curves_reddening(lc, ebv=ebv)
     magRed = lc.Mag
     res = list(filter(lambda x: abs(x) > 1e-4, magExpect - magRed))
     self.assertTrue(len(res) == 0,
                     msg="Some mags were shifted more then %f." % mshift)
Ejemplo n.º 3
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)