catalog.py 1.33 KB
Newer Older
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
1
import urllib
Wei Shoulin's avatar
Wei Shoulin committed
2
import json
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
3
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
4
from csst_dfs_commons.models.common import from_dict_list, Gaia3Record
Wei Shoulin's avatar
gaia3    
Wei Shoulin committed
5
class CatalogApi(object):
Wei Shoulin's avatar
Wei Shoulin committed
6
7
    def __init__(self):
        self.data = []
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
8
        self.url = r'http://csst.astrolab.cn:30180/search/v2'
Wei Shoulin's avatar
Wei Shoulin committed
9
    
Wei Shoulin's avatar
gaia3    
Wei Shoulin committed
10
    def gaia3_query(self, ra: float, dec: float, radius: float, min_mag: float,  max_mag: float,  obstime: int, limit: int):
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        data = { "ra":ra, "dec":dec, "radius":radius, "catalog_name": "gaia3", "min_mag": min_mag, "max_mag": max_mag, "obstime": obstime, "limit": limit}
        return self.rest_request(data)
    
    def rest_request(self, data):
        try:
            Headers = {"Content-type": "application/x-www-form-urlencoded"}

            data = urllib.parse.urlencode(data).encode("utf-8") 
            req =  urllib.request.Request(self.url,data,Headers)
            response = urllib.request.urlopen(req, timeout=30)
            html = response.read().decode("utf-8") 
            resp = json.loads(html)

            if resp["code"] == 0:
Wei Shoulin's avatar
Wei Shoulin committed
25
                return Result.ok_data(data=from_dict_list(Gaia3Record, resp["data"])).append("totalCount", resp["object"]["totalCount"])
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
26
            else:
Wei Shoulin's avatar
Wei Shoulin committed
27
                return Result.error(message = resp['message'])
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
28
29
30
31

        except Exception as e:
            return Result.error(message=repr(e))