2017-11-07 5 views
0

私は、データベース内の各星のRa(座標)値を、同じリスト内の他の星のRaすべてと比較しようとしています。 1度以内である。データをうまくアップロードして変換しました。私の問題は実際には他のすべての値とすべての値を比較しています。 ループにブール値を使用しています。リスト内の1つの値とリスト内のすべての値を比較する

for i,r in enumerate(lyrae): 
    q=r['ra']-lyrae['ra'] 
    m=(q<1) & (q>0) 
    c=q[m] 
    print(r,c) 
    for i,r in enumerate(lyrae): 
     v=r['dec']-lyrae['dec'] 
     n=(v<1) & (v>0) 
     b=v[n] 
     print(i,b) 

は動作していないようです。また、ロードに永遠にかかる。

+0

「lyrae」の大きさはどれくらいですか?いくつかのサンプルデータを提供できますか? –

+0

今は700種類の星ですが、それほど問題にならないようにしようとしています。すぐにデータのサイズが大きくなります。 –

+0

最初のいくつかの要素を表示して、データの構造について考えてみてください。 –

答えて

0

これがあなたが意図したことをしているかどうかを確認してください。効率的な組み込み/ numpy/scipy関数を使用すると、多くの計算時間を節約できます。たとえばm=(q<1) & (q>0)abs(q) < 1に置き換えることができます(また、すべてのオブジェクトを1度以内で検索する場合は、m=(q<1) & (q>0)m=(q<1) & (q>-1))。

球の極座標でオブジェクトを正しく検索するには、大きな円の距離を見つける必要があります。そうでないと、極と周回境界(つまり(0.1,10.2)と(359.9,10.1) )。

2つの巨大なカタログ(たとえば両方とも10^9行)をクロスマッチさせたい場合は、非効率的なコードのために行く方法ではありません。しかし、あなたのカタログのうちの1つが〜700行しかない場合、効率的な検索機能の開発時間はおそらく検索自体よりもはるかに長くなるでしょう。

import numpy as np 

# Set arbitrary size for a reference catalogue and a RR Lyrae cataloge 
cat_size = 1000000 
lyrae_size = 1000 

# Draw sample randomly in a cartesian plane for the catalogue 
# (1) RA, (2) Dec and (3) unique ID 
ra_cat = np.random.random(cat_size) * 360. 
dec_cat = np.random.random(cat_size) * 180. - 90. 
id_cat = np.arange(cat_size) 

# Draw sample randomly in a cartesian plane for the RR Lyrae catalogue 
ra_lyrae = np.random.random(lyrae_size) * 360. 
dec_lyrae = np.random.random(lyrae_size) * 180. - 90. 
id_lyrae = np.arange(lyrae_size) 

def great_circle_distance(ra1, dec1, ra_list, dec_list): 
    ''' 
    Finding the great circle distance between given points and the list of 
    points given in equatorial coordinates. Note: this from cosine rule, which 
    is not the most accurate way to find the great circle distance. 
    Input: 
    ra1, dec1 - the reference point in degrees 
    ra_list, dec_list - is the list of object positions in degrees 
    Output: 
    list of distances between the reference point and the list of objects 
    in units of degrees. 
    ''' 
    ra1 = np.radians(ra1) 
    dec1 = np.radians(dec1) 
    ra2 = np.radians(ra_list) 
    dec2 = np.radians(dec_list) 
    # Great circle distance in radians 
    dist = (np.arccos(np.sin(dec1) * np.sin(dec2) + 
         np.cos(dec1) * np.cos(dec2) * np.cos(ra1 - ra2))) 
    return np.degrees(dist) 

# Loop through the RR Lyrae catalogue 
for i, position in enumerate(zip(ra_lyrae, dec_lyrae)): 
    # Get the (RA, Dec) from zipped position 
    ra, dec = position 
    # Consider only objects within a 2x2 degree box 
    mask_candidate = (np.abs(ra_cat - ra) < 1.) | (np.abs(dec_cat - dec) < 1.) 
    # Get the distance between the RR Lyrae star to the objects in the box 
    distance = great_circle_distance(
     ra, dec, ra_cat[mask_candidate], dec_cat[mask_candidate] 
     ) 
    # Select all objects within 1 degree radius 
    mask_final = (distance < 1.) 
    # Get the unique ID of the catalogue objects 
    id_cat_matched = id_cat[mask_candidate][mask_final] 
    # print/save/further analysis with the list of matched objects 
    print 'lyrae id: ' + str(i+1) + ' matched with catalogue id: ' + str(id_cat_matched) 
関連する問題