コード例 #1
0
def test_fnpickling_class(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' ability to pickle
    and unpickle custom classes.
    """
    fn = str(tmpdir.join('test2.pickle'))

    obj1 = 'astring'
    obj2 = ToBePickled(obj1)
    fnpickle(obj2, fn)
    res = fnunpickle(fn)
    assert res == obj2
コード例 #2
0
ファイル: test_pickle_helpers.py プロジェクト: Cadair/astropy
def test_fnpickling_class(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' ability to pickle
    and unpickle custom classes.
    """
    fn = str(tmpdir.join('test2.pickle'))

    obj1 = 'astring'
    obj2 = ToBePickled(obj1)
    fnpickle(obj2, fn)
    res = fnunpickle(fn)
    assert res == obj2
コード例 #3
0
ファイル: __init__.py プロジェクト: waltersmartinsf/ExoTRed
def bkg_info(input_file):
    """
    Obtain the sky backgound for each science image.

    More in: http://photutils.readthedocs.io/en/latest/api/photutils.background.Background.html#photutils.background.Background

    WARNING:

    This routine only need to be run one time for the same set of images.
    ___


    INPUT:
    For obtain this parameters, use the input_info function.

    data_path: string, path where are the images data.
    save_path: string, path where will save all reduced images.
    input_file: dict, with information describe in the YAML file.
    """
    #set the original directory
    original_path = os.getcwd()
    save_path = input_file['save_path']
    os.chdir(save_path)
    planet = input_file['exoplanet']
    tempo = time.time()
    print 'Obtain background data for each image ... \n'
    if not os.path.exists('background'): #if background does not exist, create!
        os.makedirs('background')

    images = sorted(glob.glob('AB'+planet+'*.fits'))
    #if background exist, check if files bkg_data_image_name_.pik exist
    #if not exist, then create, else: get out of here! XD
    if os.path.exists('background') == True :
        value = []
        for i in images:
            value.append(os.path.isfile('./background/'+'bkg_data_'+i+'_.pik'))
    if (False in value) == True:
        print 'Does not exist all files to all images in the sample.'
        print 'Calculating ...'
        print 'This will take some time... go drink some coffe'
        print ' while you wait for the routine finish \n'
        for i in images:
            im = fits.getdata(i,header=False)
            im = np.array(im,dtype='Float64')
            bkg = Background(im,tuple(input_file['skysection'])) #estimating the background using a boxpixel
            fnpickle(bkg,'./background/'+'bkg_data_'+i+'_.pik')
            use.update_progress((float(images.index(i))+1.)/len(images))
    else:
        print 'The sky background files *.pik exist. \n'
    print 'Sky backgound obtained.'
    print 'Total time = ',abs(time.time()-tempo)/60.,' minutes'
    os.chdir(original_path)
    return
コード例 #4
0
def test_fnpickling_protocol(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' ability to pickle
    and unpickle pickle files from all protcols.
    """
    import pickle

    obj1 = 'astring'
    obj2 = ToBePickled(obj1)

    for p in range(pickle.HIGHEST_PROTOCOL + 1):
        fn = str(tmpdir.join('testp{}.pickle'.format(p)))
        fnpickle(obj2, fn, protocol=p)
        res = fnunpickle(fn)
        assert res == obj2
コード例 #5
0
ファイル: test_pickle_helpers.py プロジェクト: Cadair/astropy
def test_fnpickling_protocol(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' ability to pickle
    and unpickle pickle files from all protcols.
    """
    import pickle

    obj1 = 'astring'
    obj2 = ToBePickled(obj1)

    for p in range(pickle.HIGHEST_PROTOCOL + 1):
        fn = str(tmpdir.join('testp{}.pickle'.format(p)))
        fnpickle(obj2, fn, protocol=p)
        res = fnunpickle(fn)
        assert res == obj2
コード例 #6
0
def test_fnpickling_simple(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' basic operation by
    pickling and unpickling a string, using both a filename and a
    file.
    """
    fn = str(tmpdir.join('test1.pickle'))

    obj1 = 'astring'
    fnpickle(obj1, fn)
    res = fnunpickle(fn, 0)
    assert obj1 == res

    # now try with a file-like object instead of a string
    with open(fn, 'wb') as f:
        fnpickle(obj1, f)
    with open(fn, 'rb') as f:
        res = fnunpickle(f)
        assert obj1 == res
コード例 #7
0
def test_fnpickling_many(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' ability to pickle
    and unpickle multiple objects from a single file.
    """

    fn = str(tmpdir.join('test3.pickle'))

    # now try multiples
    obj3 = 328.3432
    obj4 = 'blahblahfoo'
    fnpickle(obj3, fn)
    fnpickle(obj4, fn, append=True)

    res = fnunpickle(fn, number=-1)
    assert len(res) == 2
    assert res[0] == obj3
    assert res[1] == obj4

    fnpickle(obj4, fn, append=True)
    res = fnunpickle(fn, number=2)
    assert len(res) == 2

    with pytest.raises(EOFError):
        fnunpickle(fn, number=5)
コード例 #8
0
ファイル: test_pickle_helpers.py プロジェクト: Cadair/astropy
def test_fnpickling_many(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' ability to pickle
    and unpickle multiple objects from a single file.
    """

    fn = str(tmpdir.join('test3.pickle'))

    # now try multiples
    obj3 = 328.3432
    obj4 = 'blahblahfoo'
    fnpickle(obj3, fn)
    fnpickle(obj4, fn, append=True)

    res = fnunpickle(fn, number=-1)
    assert len(res) == 2
    assert res[0] == obj3
    assert res[1] == obj4

    fnpickle(obj4, fn, append=True)
    res = fnunpickle(fn, number=2)
    assert len(res) == 2

    with pytest.raises(EOFError):
        fnunpickle(fn, number=5)
コード例 #9
0
ファイル: test_pickle_helpers.py プロジェクト: Cadair/astropy
def test_fnpickling_simple(tmpdir):
    """
    Tests the `fnpickle` and `fnupickle` functions' basic operation by
    pickling and unpickling a string, using both a filename and a
    file.
    """
    fn = str(tmpdir.join('test1.pickle'))

    obj1 = 'astring'
    fnpickle(obj1, fn)
    res = fnunpickle(fn, 0)
    assert obj1 == res

    # now try with a file-like object instead of a string
    with open(fn, 'wb') as f:
        fnpickle(obj1, f)
    with open(fn, 'rb') as f:
        res = fnunpickle(f)
        assert obj1 == res

    with catch_warnings(AstropyDeprecationWarning):
        fnunpickle(fn, 0, True)
コード例 #10
0
ファイル: spitzer_targets.py プロジェクト: adrn/streams
def sgr(overwrite=False, seed=42):

    np.random.seed(seed)

    lm10_cache = os.path.join(project_root, "data", "spitzer_targets",
                              "lm10_cache.pickle")
    if os.path.exists(lm10_cache) and overwrite:
        os.remove(lm10_cache)

    if not os.path.exists(lm10_cache):
        # select particle data from the LM10 simulation
        lm10 = particle_table(N=0, expr="(Pcol>-1) & (Pcol<8) & "\
                                        "(abs(Lmflag)==1) & (dist<100)")
        fnpickle(np.array(lm10), lm10_cache)
    else:
        lm10 = Table(fnunpickle(lm10_cache))

    # read in the Catalina RR Lyrae data
    spatial_data = ascii.read(os.path.join(project_root,
                              "data/catalog/Catalina_all_RRLyr.txt"))
    velocity_data = ascii.read(os.path.join(project_root,
                               "data/catalog/Catalina_vgsr_RRLyr.txt"))
    catalina = join(spatial_data, velocity_data, join_type='outer', keys="ID")
    catalina.rename_column("RAdeg", "ra")
    catalina.rename_column("DEdeg", "dec")
    catalina.rename_column("dh", "dist")

    # add Sgr coordinates to the Catalina data
    catalina = add_sgr_coordinates(catalina)

    # add Galactocentric distance to the Catalina and LM10 data
    cat_gc_dist = tbl_to_gc_dist(catalina)
    lm10_gc_dist = tbl_to_gc_dist(lm10)
    catalina.add_column(Column(cat_gc_dist, name="gc_dist"))
    lm10.add_column(Column(lm10_gc_dist, name="gc_dist"))

    # 1) Select stars < 20 kpc from the orbital plane of Sgr
    sgr_catalina = catalina[np.fabs(catalina["Z_sgr"]) < 20.]
    x,y,z = tbl_to_xyz(sgr_catalina)

    # 2) Stars with D > 15 kpc from the Galactic center
    sgr_catalina = sgr_catalina[sgr_catalina["gc_dist"] > 15]
    sgr_catalina_rv = sgr_catalina[~sgr_catalina["Vgsr"].mask]
    print("{0} CSS RRLs have radial velocities.".format(len(sgr_catalina_rv)))

    # plot X-Z plane, data and lm10 particles
    fig,axes = plt.subplots(1,3,figsize=(15,6), sharex=True, sharey=True)
    x,y,z = tbl_to_xyz(catalina)
    axes[0].set_title("All RRL", fontsize=20)
    axes[0].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[0].set_xlabel("$X_{gc}$ kpc")
    axes[0].set_ylabel("$Z_{gc}$ kpc")

    x,y,z = tbl_to_xyz(sgr_catalina)
    axes[1].set_title(r"RRL $|Z-Z_{sgr}|$ $<$ $20$ kpc", fontsize=20)
    axes[1].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[2].plot(lm10["x"], lm10["z"], marker='.',
                 alpha=0.2, linestyle='none')
    axes[2].set_title("LM10", fontsize=20)
    axes[2].set_xlim(-60, 40)
    axes[2].set_ylim(-60, 60)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "catalina_all.pdf"))

    # plot Lambda-dist plane, data and lm10 particles
    fig,ax = plt.subplots(1,1,figsize=(6,6),
                          subplot_kw=dict(projection="polar"))

    ax.set_theta_direction(-1)
    ax.plot(np.radians(lm10["Lambda"]), lm10["dist"],
                       marker='.', alpha=0.2, linestyle='none')
    ax.plot(np.radians(sgr_catalina["Lambda"]), sgr_catalina["dist"],
                       marker='.', alpha=0.75, linestyle='none', ms=6)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "rrl_over_lm10.pdf"))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on radial velocities
    rv_selection_cache = os.path.join(project_root, "data",
                                      "spitzer_targets", "rv.pickle")
    if os.path.exists(rv_selection_cache) and overwrite:
        os.remove(rv_selection_cache)

    if not os.path.exists(rv_selection_cache):
        lmflag_rv_idx = sgr_rv(sgr_catalina_rv, lm10, Nbins=40, sigma_cut=3.)
        fnpickle(lmflag_rv_idx, rv_selection_cache)
    else:
        lmflag_rv_idx = fnunpickle(rv_selection_cache)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on distance
    _selection_cache = os.path.join(project_root, "data",
                                    "spitzer_targets", "dist.pickle")
    if os.path.exists(_selection_cache) and overwrite:
        os.remove(_selection_cache)

    if not os.path.exists(_selection_cache):
        lmflag_dist_idx = sgr_dist(sgr_catalina_rv, lm10,
                                 Nbins=40, sigma_cut=3.)
        fnpickle(lmflag_dist_idx, _selection_cache)
    else:
        lmflag_dist_idx = fnunpickle(_selection_cache)

    ################################################################

    # Make X-Z plot
    fig,ax = plt.subplots(1,1,figsize=(6,6))

    x,y,z = tbl_to_xyz(lm10)
    ax.plot(x, z, marker=',', alpha=0.2,
                linestyle='none')

    for lmflag in [1,-1]:
        ix = lmflag_dist_idx[lmflag] & lmflag_rv_idx[lmflag]
        x,y,z = tbl_to_xyz(sgr_catalina_rv[ix])
        ax.plot(x, z, marker='.', alpha=0.75,
                linestyle='none', ms=6, label="Lmflag={0}".format(lmflag))

    ax.legend(loc='lower right',\
              prop={'size':12})
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$X_{\rm gc}$ kpc")
    ax.set_ylabel(r"$Z_{\rm gc}$ kpc")
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "selected_xz.pdf"))

    ##############################################################
    # Finalize two samples:
    #   - 1 with 10 stars in the nearby trailing wrap
    #   - 1 without these stars

    L = sgr_catalina_rv["Lambda"]
    B = sgr_catalina_rv["Beta"]
    D = sgr_catalina_rv["dist"]
    X,Y = D*np.cos(np.radians(L)),D*np.sin(np.radians(L))

    lead_ix = lmflag_dist_idx[1] & lmflag_rv_idx[1] & (np.fabs(B) < 40)
    trail_ix = lmflag_dist_idx[-1] & lmflag_rv_idx[-1] & (np.fabs(B) < 40)
    trail_ix[L < 180] &= B[L < 180] > -5
    trail_ix = trail_ix & ( ((L > 230) & (L < 315)) | (L < 180) )

    #trail_ix &= np.logical_not((L > 50) & (L < 100) & (sgr_catalina_rv["dist"] < 40))

    # draw a box around some possible bifurcation members
    bif_ix  = (L > 200) & (L < 230) & (B < 2) & (B > -10) & lead_ix

    # deselect stars possibly associated with the bifurcation
    no_bif = (L > 180) & (L < 225) & (B < 20) & (B > 2)
    no_bif |= (L > 225) & (L < 360) & (B < 17) & (B > 2)
    no_bif |= ((L <= 180) & (B < 15) & (B > -8) & (L > 50))
    lead_ix &= no_bif

    print(sum(bif_ix), "bifurcation stars")
    print(sum(lead_ix), "leading arm stars")
    print(sum(trail_ix), "trailing arm stars ")

    # select 3 clumps in the leading arm
    Nclump = 11
    ix_lead_clumps = np.zeros_like(lead_ix).astype(bool)
    for clump in [(215,17), (245,25), (260,40)]:
        l,d = clump
        x,y = d*np.cos(np.radians(l)),d*np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X-x)**2 + (Y-y)**2)
        xxx = np.sort(clump_dist[lead_ix])[Nclump]

        this_ix = lead_ix & (clump_dist <= xxx)
        print("lead",sum(this_ix))
        ix_lead_clumps |= this_ix #select_only(this_ix, 10)
    ix_lead_clumps &= lead_ix

    # all southern leading
    lll = ((L > 45) & (L < 180) & lead_ix)
    print("southern leading", sum(lll))
    ix_lead_clumps |= lll

    ix_trail_clumps = np.zeros_like(trail_ix).astype(bool)
    for clump in [(260,19)]:
        l,d = clump
        x,y = d*np.cos(np.radians(l)),d*np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X-x)**2 + (Y-y)**2)
        xxx = np.sort(clump_dist[trail_ix])[10]

        this_ix = trail_ix & (clump_dist <= xxx)
        #bonus_trail_ix = trail_ix & (clump_dist > xxx)
        ix_trail_clumps |= this_ix #select_only(this_ix, 10)
    ix_trail_clumps &= trail_ix

    # all trailing southern
    ttt = (L > 45) & (L < 180) & (trail_ix)
    print("southern trailing", sum(ttt))
    ix_trail_clumps |= ttt

    i1 = integration_time(sgr_catalina_rv[bif_ix]["dist"])
    i2 = integration_time(sgr_catalina_rv[ix_lead_clumps]["dist"])
    i3 = integration_time(sgr_catalina_rv[ix_trail_clumps]["dist"])

    print("final num bifurcation", len(sgr_catalina_rv[bif_ix]))
    print("final num leading arm", len(sgr_catalina_rv[ix_lead_clumps]))
    print("final num trailing arm",len(sgr_catalina_rv[ix_trail_clumps]))
    print("final total", sum(ix_trail_clumps) + sum(ix_lead_clumps) + sum(bif_ix))
    print("final total", sum(ix_trail_clumps | ix_lead_clumps | bif_ix))

    targets = sgr_catalina_rv[ix_trail_clumps | ix_lead_clumps | bif_ix]
    bonus_targets = sgr_catalina_rv[(lead_ix | trail_ix) & \
                            ~(ix_trail_clumps | ix_lead_clumps | bif_ix)]

    # print()
    # print("bifurcation", np.sum(i1))
    # print("leading",np.sum(i2))
    # print("trailing",np.sum(i3))
    # print("Total:",np.sum(integration_time(targets["dist"])))

    tot_time = 0.
    for t in targets:
        Vmag = t["<Vmag>"]

        if Vmag < 16.6:
            tot_time += 1.286
        elif 16.6 < Vmag < 16.8:
            tot_time += 1.31
        elif 16.8 < Vmag < 17.2:
            tot_time += 1.83
        elif 17.2 < Vmag < 17.8:
            tot_time += 2.52
        elif Vmag > 17.8:
            tot_time += 4.34

    print ("total time: ", tot_time)

    output_file = "sgr.txt"
    output = targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets", output_file), Writer=ascii.Basic)

    output_file = "sgr_bonus.txt"
    output = bonus_targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets", output_file), Writer=ascii.Basic)

    # ----------------------------------
    # Make Lambda-D plot
    fig,ax = plt.subplots(1,1,figsize=(6,6),
                          subplot_kw=dict(projection="polar"))
    ax.set_theta_direction(-1)

    ax.plot(np.radians(lm10["Lambda"]), lm10["dist"],
            marker=',', alpha=0.2, linestyle='none')

    d = sgr_catalina_rv[lead_ix]
    ax.plot(np.radians(d["Lambda"]), d["dist"], marker='.',
            alpha=0.75, linestyle='none', ms=8, c="#CA0020", label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(np.radians(d["Lambda"]), d["dist"], marker='.',
            alpha=0.75, linestyle='none', ms=8, c="#5E3C99", label="trailing")

    ax.plot(np.radians(targets["Lambda"]), targets["dist"], marker='.',
            alpha=0.7, linestyle='none', ms=10, c="#31A354", label="targets",
            mfc='none', mec='k', mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_ylim(0,65)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "xz.pdf"))

    # ---------------------
    # Make Lambda-Beta plot
    fig,ax = plt.subplots(1,1,figsize=(12,5))

    ax.plot(lm10["Lambda"], lm10["Beta"], marker=',', alpha=0.2,
                linestyle='none')

    dd_bif = sgr_catalina_rv[bif_ix]
    ax.plot(dd_bif["Lambda"], dd_bif["Beta"], marker='.', alpha=0.75,
            linestyle='none', ms=6, c="#31A354", label="bifurcation")

    d = sgr_catalina_rv[lead_ix]
    ax.plot(d["Lambda"], d["Beta"], marker='.',
           alpha=0.75, linestyle='none', ms=8, c="#CA0020", label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(d["Lambda"], d["Beta"], marker='.',
            alpha=0.75, linestyle='none', ms=8, c="#5E3C99", label="trailing")

    ax.plot(targets["Lambda"], targets["Beta"], marker='.',
            alpha=0.7, linestyle='none', ms=10, c="#31A354", label="targets",
            mfc='none', mec='k', mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$\Lambda$ [deg]")
    ax.set_ylabel(r"$B$ [deg]")
    ax.set_xlim(360, 0)
    ax.set_ylim(-45, 45)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "LB.pdf"))
コード例 #11
0
 def test_pickle(self):
     particles = self.sgr.particles(n=25, expr="tub==0")
     fnpickle(particles, os.path.join(plot_path, "test.pickle"))
     p = particles.to_frame(heliocentric)
     fnpickle(p, os.path.join(plot_path, "test2.pickle"))
コード例 #12
0
ファイル: test_sgr.py プロジェクト: adrn/streams
 def test_pickle(self):
     particles = self.sgr.particles(n=25, expr="tub==0")
     fnpickle(particles, os.path.join(plot_path, "test.pickle"))
     p = particles.to_frame(heliocentric)
     fnpickle(p, os.path.join(plot_path, "test2.pickle"))
コード例 #13
0
ファイル: arxiv.py プロジェクト: adrn/sandbox
    for k,v in kwargs.items():
        params[k] = v
    
    # send request, use feedparser to parse the ATOM feed response
    resp = requests.get(url=api_url, params=params)
    feed = feedparser.parse(resp.content)
    
    # for each entry, extract ID, abstract, title, authors
    for entry in feed['entries']:
        id = os.path.basename(entry.id) # http://arxiv.org/abs/1305.1919v1
        abstract = entry.summary_detail['value']
        parsed_entries[id] = dict(title=entry.title, 
                                  authors=entry.authors,
                                  abstract=abstract)
    
    return parsed_entries

num = 10000
max_results = 1000

parsed_entries = dict()
for start in range(0, num+max_results, max_results):
    entries = retrieve_abstracts(search_query="abs:galaxy",
                                 start=start, 
                                 max_results=max_results)
    parsed_entries = dict(parsed_entries.items() + entries.items())
    time.sleep(3)
    
fnpickle(parsed_entries, "arxiv.pickle")

コード例 #14
0
ファイル: spitzer_targets.py プロジェクト: adrn/streams
def sgr(overwrite=False, seed=42):

    np.random.seed(seed)

    lm10_cache = os.path.join(project_root, "data", "spitzer_targets",
                              "lm10_cache.pickle")
    if os.path.exists(lm10_cache) and overwrite:
        os.remove(lm10_cache)

    if not os.path.exists(lm10_cache):
        # select particle data from the LM10 simulation
        lm10 = particle_table(N=0, expr="(Pcol>-1) & (Pcol<8) & "\
                                        "(abs(Lmflag)==1) & (dist<100)")
        fnpickle(np.array(lm10), lm10_cache)
    else:
        lm10 = Table(fnunpickle(lm10_cache))

    # read in the Catalina RR Lyrae data
    spatial_data = ascii.read(
        os.path.join(project_root, "data/catalog/Catalina_all_RRLyr.txt"))
    velocity_data = ascii.read(
        os.path.join(project_root, "data/catalog/Catalina_vgsr_RRLyr.txt"))
    catalina = join(spatial_data, velocity_data, join_type='outer', keys="ID")
    catalina.rename_column("RAdeg", "ra")
    catalina.rename_column("DEdeg", "dec")
    catalina.rename_column("dh", "dist")

    # add Sgr coordinates to the Catalina data
    catalina = add_sgr_coordinates(catalina)

    # add Galactocentric distance to the Catalina and LM10 data
    cat_gc_dist = tbl_to_gc_dist(catalina)
    lm10_gc_dist = tbl_to_gc_dist(lm10)
    catalina.add_column(Column(cat_gc_dist, name="gc_dist"))
    lm10.add_column(Column(lm10_gc_dist, name="gc_dist"))

    # 1) Select stars < 20 kpc from the orbital plane of Sgr
    sgr_catalina = catalina[np.fabs(catalina["Z_sgr"]) < 20.]
    x, y, z = tbl_to_xyz(sgr_catalina)

    # 2) Stars with D > 15 kpc from the Galactic center
    sgr_catalina = sgr_catalina[sgr_catalina["gc_dist"] > 15]
    sgr_catalina_rv = sgr_catalina[~sgr_catalina["Vgsr"].mask]
    print("{0} CSS RRLs have radial velocities.".format(len(sgr_catalina_rv)))

    # plot X-Z plane, data and lm10 particles
    fig, axes = plt.subplots(1, 3, figsize=(15, 6), sharex=True, sharey=True)
    x, y, z = tbl_to_xyz(catalina)
    axes[0].set_title("All RRL", fontsize=20)
    axes[0].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[0].set_xlabel("$X_{gc}$ kpc")
    axes[0].set_ylabel("$Z_{gc}$ kpc")

    x, y, z = tbl_to_xyz(sgr_catalina)
    axes[1].set_title(r"RRL $|Z-Z_{sgr}|$ $<$ $20$ kpc", fontsize=20)
    axes[1].plot(x, z, marker='.', alpha=0.2, linestyle='none')
    axes[2].plot(lm10["x"], lm10["z"], marker='.', alpha=0.2, linestyle='none')
    axes[2].set_title("LM10", fontsize=20)
    axes[2].set_xlim(-60, 40)
    axes[2].set_ylim(-60, 60)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "catalina_all.pdf"))

    # plot Lambda-dist plane, data and lm10 particles
    fig, ax = plt.subplots(1,
                           1,
                           figsize=(6, 6),
                           subplot_kw=dict(projection="polar"))

    ax.set_theta_direction(-1)
    ax.plot(np.radians(lm10["Lambda"]),
            lm10["dist"],
            marker='.',
            alpha=0.2,
            linestyle='none')
    ax.plot(np.radians(sgr_catalina["Lambda"]),
            sgr_catalina["dist"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=6)

    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "rrl_over_lm10.pdf"))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on radial velocities
    rv_selection_cache = os.path.join(project_root, "data", "spitzer_targets",
                                      "rv.pickle")
    if os.path.exists(rv_selection_cache) and overwrite:
        os.remove(rv_selection_cache)

    if not os.path.exists(rv_selection_cache):
        lmflag_rv_idx = sgr_rv(sgr_catalina_rv, lm10, Nbins=40, sigma_cut=3.)
        fnpickle(lmflag_rv_idx, rv_selection_cache)
    else:
        lmflag_rv_idx = fnunpickle(rv_selection_cache)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # Select on distance
    _selection_cache = os.path.join(project_root, "data", "spitzer_targets",
                                    "dist.pickle")
    if os.path.exists(_selection_cache) and overwrite:
        os.remove(_selection_cache)

    if not os.path.exists(_selection_cache):
        lmflag_dist_idx = sgr_dist(sgr_catalina_rv,
                                   lm10,
                                   Nbins=40,
                                   sigma_cut=3.)
        fnpickle(lmflag_dist_idx, _selection_cache)
    else:
        lmflag_dist_idx = fnunpickle(_selection_cache)

    ################################################################

    # Make X-Z plot
    fig, ax = plt.subplots(1, 1, figsize=(6, 6))

    x, y, z = tbl_to_xyz(lm10)
    ax.plot(x, z, marker=',', alpha=0.2, linestyle='none')

    for lmflag in [1, -1]:
        ix = lmflag_dist_idx[lmflag] & lmflag_rv_idx[lmflag]
        x, y, z = tbl_to_xyz(sgr_catalina_rv[ix])
        ax.plot(x,
                z,
                marker='.',
                alpha=0.75,
                linestyle='none',
                ms=6,
                label="Lmflag={0}".format(lmflag))

    ax.legend(loc='lower right',\
              prop={'size':12})
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$X_{\rm gc}$ kpc")
    ax.set_ylabel(r"$Z_{\rm gc}$ kpc")
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "selected_xz.pdf"))

    ##############################################################
    # Finalize two samples:
    #   - 1 with 10 stars in the nearby trailing wrap
    #   - 1 without these stars

    L = sgr_catalina_rv["Lambda"]
    B = sgr_catalina_rv["Beta"]
    D = sgr_catalina_rv["dist"]
    X, Y = D * np.cos(np.radians(L)), D * np.sin(np.radians(L))

    lead_ix = lmflag_dist_idx[1] & lmflag_rv_idx[1] & (np.fabs(B) < 40)
    trail_ix = lmflag_dist_idx[-1] & lmflag_rv_idx[-1] & (np.fabs(B) < 40)
    trail_ix[L < 180] &= B[L < 180] > -5
    trail_ix = trail_ix & (((L > 230) & (L < 315)) | (L < 180))

    #trail_ix &= np.logical_not((L > 50) & (L < 100) & (sgr_catalina_rv["dist"] < 40))

    # draw a box around some possible bifurcation members
    bif_ix = (L > 200) & (L < 230) & (B < 2) & (B > -10) & lead_ix

    # deselect stars possibly associated with the bifurcation
    no_bif = (L > 180) & (L < 225) & (B < 20) & (B > 2)
    no_bif |= (L > 225) & (L < 360) & (B < 17) & (B > 2)
    no_bif |= ((L <= 180) & (B < 15) & (B > -8) & (L > 50))
    lead_ix &= no_bif

    print(sum(bif_ix), "bifurcation stars")
    print(sum(lead_ix), "leading arm stars")
    print(sum(trail_ix), "trailing arm stars ")

    # select 3 clumps in the leading arm
    Nclump = 11
    ix_lead_clumps = np.zeros_like(lead_ix).astype(bool)
    for clump in [(215, 17), (245, 25), (260, 40)]:
        l, d = clump
        x, y = d * np.cos(np.radians(l)), d * np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X - x)**2 + (Y - y)**2)
        xxx = np.sort(clump_dist[lead_ix])[Nclump]

        this_ix = lead_ix & (clump_dist <= xxx)
        print("lead", sum(this_ix))
        ix_lead_clumps |= this_ix  #select_only(this_ix, 10)
    ix_lead_clumps &= lead_ix

    # all southern leading
    lll = ((L > 45) & (L < 180) & lead_ix)
    print("southern leading", sum(lll))
    ix_lead_clumps |= lll

    ix_trail_clumps = np.zeros_like(trail_ix).astype(bool)
    for clump in [(260, 19)]:
        l, d = clump
        x, y = d * np.cos(np.radians(l)), d * np.sin(np.radians(l))

        # find N stars closest to the clump
        clump_dist = np.sqrt((X - x)**2 + (Y - y)**2)
        xxx = np.sort(clump_dist[trail_ix])[10]

        this_ix = trail_ix & (clump_dist <= xxx)
        #bonus_trail_ix = trail_ix & (clump_dist > xxx)
        ix_trail_clumps |= this_ix  #select_only(this_ix, 10)
    ix_trail_clumps &= trail_ix

    # all trailing southern
    ttt = (L > 45) & (L < 180) & (trail_ix)
    print("southern trailing", sum(ttt))
    ix_trail_clumps |= ttt

    i1 = integration_time(sgr_catalina_rv[bif_ix]["dist"])
    i2 = integration_time(sgr_catalina_rv[ix_lead_clumps]["dist"])
    i3 = integration_time(sgr_catalina_rv[ix_trail_clumps]["dist"])

    print("final num bifurcation", len(sgr_catalina_rv[bif_ix]))
    print("final num leading arm", len(sgr_catalina_rv[ix_lead_clumps]))
    print("final num trailing arm", len(sgr_catalina_rv[ix_trail_clumps]))
    print("final total",
          sum(ix_trail_clumps) + sum(ix_lead_clumps) + sum(bif_ix))
    print("final total", sum(ix_trail_clumps | ix_lead_clumps | bif_ix))

    targets = sgr_catalina_rv[ix_trail_clumps | ix_lead_clumps | bif_ix]
    bonus_targets = sgr_catalina_rv[(lead_ix | trail_ix) & \
                            ~(ix_trail_clumps | ix_lead_clumps | bif_ix)]

    # print()
    # print("bifurcation", np.sum(i1))
    # print("leading",np.sum(i2))
    # print("trailing",np.sum(i3))
    # print("Total:",np.sum(integration_time(targets["dist"])))

    tot_time = 0.
    for t in targets:
        Vmag = t["<Vmag>"]

        if Vmag < 16.6:
            tot_time += 1.286
        elif 16.6 < Vmag < 16.8:
            tot_time += 1.31
        elif 16.8 < Vmag < 17.2:
            tot_time += 1.83
        elif 17.2 < Vmag < 17.8:
            tot_time += 2.52
        elif Vmag > 17.8:
            tot_time += 4.34

    print("total time: ", tot_time)

    output_file = "sgr.txt"
    output = targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets",
                             output_file),
                Writer=ascii.Basic)

    output_file = "sgr_bonus.txt"
    output = bonus_targets.copy()
    output.rename_column("Eta", "hjd0")
    output.rename_column("<Vmag>", "VMagAvg")
    output.keep_columns(["ID", "ra", "dec", "VMagAvg", "Period", "hjd0"])
    ascii.write(output,
                os.path.join(project_root, "data", "spitzer_targets",
                             output_file),
                Writer=ascii.Basic)

    # ----------------------------------
    # Make Lambda-D plot
    fig, ax = plt.subplots(1,
                           1,
                           figsize=(6, 6),
                           subplot_kw=dict(projection="polar"))
    ax.set_theta_direction(-1)

    ax.plot(np.radians(lm10["Lambda"]),
            lm10["dist"],
            marker=',',
            alpha=0.2,
            linestyle='none')

    d = sgr_catalina_rv[lead_ix]
    ax.plot(np.radians(d["Lambda"]),
            d["dist"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#CA0020",
            label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(np.radians(d["Lambda"]),
            d["dist"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#5E3C99",
            label="trailing")

    ax.plot(np.radians(targets["Lambda"]),
            targets["dist"],
            marker='.',
            alpha=0.7,
            linestyle='none',
            ms=10,
            c="#31A354",
            label="targets",
            mfc='none',
            mec='k',
            mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_ylim(0, 65)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "xz.pdf"))

    # ---------------------
    # Make Lambda-Beta plot
    fig, ax = plt.subplots(1, 1, figsize=(12, 5))

    ax.plot(lm10["Lambda"],
            lm10["Beta"],
            marker=',',
            alpha=0.2,
            linestyle='none')

    dd_bif = sgr_catalina_rv[bif_ix]
    ax.plot(dd_bif["Lambda"],
            dd_bif["Beta"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=6,
            c="#31A354",
            label="bifurcation")

    d = sgr_catalina_rv[lead_ix]
    ax.plot(d["Lambda"],
            d["Beta"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#CA0020",
            label="leading")

    d = sgr_catalina_rv[trail_ix]
    ax.plot(d["Lambda"],
            d["Beta"],
            marker='.',
            alpha=0.75,
            linestyle='none',
            ms=8,
            c="#5E3C99",
            label="trailing")

    ax.plot(targets["Lambda"],
            targets["Beta"],
            marker='.',
            alpha=0.7,
            linestyle='none',
            ms=10,
            c="#31A354",
            label="targets",
            mfc='none',
            mec='k',
            mew=1.5)

    ax.legend(loc="lower right")
    plt.setp(ax.get_legend().get_texts(), fontsize='12')
    ax.set_title("RV-selected CSS RRLs", fontsize=20)
    ax.set_xlabel(r"$\Lambda$ [deg]")
    ax.set_ylabel(r"$B$ [deg]")
    ax.set_xlim(360, 0)
    ax.set_ylim(-45, 45)
    fig.tight_layout()
    fig.savefig(os.path.join(notes_path, "LB.pdf"))
コード例 #15
0
ファイル: util.py プロジェクト: kaashmonee/rM2FS
def save(fits_file):
    save_directory = "fitted_files/"
    fnpickle(fits_file, save_directory + fits_file.get_file_name() + ".pkl")
コード例 #16
0
ファイル: run_nest.py プロジェクト: kbarbary/dessn-analysis
        t0off = m['model'].get('t0') - m['model'].mintime() # t0 offset from
                                                            # mintime

        t0min = dtmin - 30. + t0off
        t0max = dtmax - 30. + t0off
        m['bounds']['t0'] = (t0min, t0max)

        # Set mwebv of model.
        m['model'].set(mwebv=mwebv)

        print name, m['type']
        res = _nest_lc(data, m['model'], m['param_names'],
                       bounds=m['bounds'], tied=m['tied'], nobj=50,
                       verbose=True)

        # Set parameters to those of the model (presumably these are 
        # high-likelihood values), but we're mainly doing this to record
        # the non-varied model parameters (such as mwebv)
        res.param_dict = dict(zip(m['model'].param_names,
                                  m['model'].parameters))

        # record prior and type of the model.
        res.mprior = m['mprior']
        res.type = m['type']

        allresults[name] = res

    fnpickle(allresults, pikname)

f.close()