简体   繁体   中英

How to use Getclosest() in a list in Python

I have code which is taking RA and DEC values (some coordinates in str form) from a text file and using astroquery to query objects respect to those RA and DEC data and shows a table for those objects and their attributes. Then I put them into a list. However query sometimes finds more than 1 objects. I want to use GetClosest() commend to find closest object to put only the closest ones into my list. Here is a part of my code:


catalog=[]
catalog2=[]

exception_list = ['b','c','d','e','f','g','h','j','k']

rad1='0.001s'
rad2='0.01s'
rad3='0.1s'
rad4='1s'
rad5='5s'


for i in range(len(RA)):
        result_table = customSimbad.query_region(coord.SkyCoord(ra=RA[i], dec=DEC[i], unit=(u.hourangle, u.deg)), radius= rad1)
        
        if result_table is None:
            result_table = customSimbad.query_region(coord.SkyCoord(ra=RA[i], dec=DEC[i], unit=(u.hourangle, u.deg)), radius= rad2)
            if result_table is None:
                result_table = customSimbad.query_region(coord.SkyCoord(ra=RA[i], dec=DEC[i], unit=(u.hourangle, u.deg)), radius= rad3)
                if result_table is None:
                    result_table = customSimbad.query_region(coord.SkyCoord(ra=RA[i], dec=DEC[i], unit=(u.hourangle, u.deg)), radius= rad4)
                    if result_table is None:
                        result_table = customSimbad.query_region(coord.SkyCoord(ra=RA[i], dec=DEC[i], unit=(u.hourangle, u.deg)), radius= rad5)
        if result_table is None:
            catalog.append([i,None])
        else:
            for obj in result_table:
                catalog.append([i,obj[0],RA[i],DEC[i],str(obj[1]),str(obj[2]),obj[4] if obj[12]!='--' else None,obj[12] if obj[12]!='--' else None,obj[13] if obj[13]!='--' else None,obj[14] if obj[14]!='--' else None,obj[15] if obj[15]!='--' else None,obj[16] if obj[16]!='--' else None,obj[17] if obj[17]!='--' else None,obj[18] if obj[18]!='--' else None,obj[19] if obj[19]!='--' else None,obj[20] if obj[20]!='--' else None,obj[21] if obj[21]!='--' else None,obj[22] if obj[22]!='--' else None,obj[23] if obj[23]!='--' else None])
                if not obj[0][len(obj[0])-1:] in exception_list :
                    catalog2.append([i,obj[0],RA[i],DEC[i],str(obj[1]),str(obj[2]),obj[4] if obj[12]!='--' else None,obj[12] if obj[12]!='--' else None,obj[13] if obj[13]!='--' else None,obj[14] if obj[14]!='--' else None,obj[15] if obj[15]!='--' else None,obj[16] if obj[16]!='--' else None,obj[17] if obj[17]!='--' else None,obj[18] if obj[18]!='--' else None,obj[19] if obj[19]!='--' else None,obj[20] if obj[20]!='--' else None,obj[21] if obj[21]!='--' else None,obj[22] if obj[22]!='--' else None,obj[23] if obj[23]!='--' else None])

So I want to match closest RA[i] with obj[1] and DEC[i] with obj[2]. How can i do that?

You can try this:

def FindClosest(input_list, input_point):
    output_value = min(map(lambda x: [abs(x[0] - input_point[0]), x[1]], input_list), key = lambda k: k[0])
    return [ input_point[0], input_point[1] * output_value[1] ]

result = list(map(lambda x: FindClosest(calculated_list, x), output_list))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM