catalog.py 1.88 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
import grpc
Wei Shoulin's avatar
Wei Shoulin committed
2
from collections import deque
Wei Shoulin's avatar
timeit    
Wei Shoulin committed
3
import logging
Wei Shoulin's avatar
Wei Shoulin committed
4
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
5
6
from csst_dfs_commons.models.common import from_proto_model_list, Gaia3Record

Shoulin Wei's avatar
init  
Shoulin Wei committed
7
from csst_dfs_proto.common.ephem import ephem_pb2, ephem_pb2_grpc
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
8
9
10
from .service import ServiceProxy
from .constants import *
from .utils import get_auth_headers
Shoulin Wei's avatar
init  
Shoulin Wei committed
11

Wei Shoulin's avatar
timeit    
Wei Shoulin committed
12
log = logging.getLogger('csst')
Shoulin Wei's avatar
init  
Shoulin Wei committed
13
14
class CatalogApi(object):
    def __init__(self):
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
15
        self.stub = ephem_pb2_grpc.EphemSearchSrvStub(ServiceProxy().channel())
Shoulin Wei's avatar
init  
Shoulin Wei committed
16
17
18
19
20
21
22
23
24
25
26
    
    def gaia3_query(self, ra: float, dec: float, radius: float, min_mag: float,  max_mag: float,  obstime: int, limit: int):
        ''' retrieval GAIA DR 3
            args:
                ra:  in deg
                dec:  in deg
                radius:  in deg
                min_mag: minimal magnitude
                max_mag: maximal magnitude
                obstime: seconds  
                limit: limits returns the number of records
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
27
            return: csst_dfs_common.models.Result
Shoulin Wei's avatar
init  
Shoulin Wei committed
28
29
        ''' 
        try:
Wei Shoulin's avatar
Wei Shoulin committed
30
            records = deque([])
Wei Shoulin's avatar
Wei Shoulin committed
31
32
            totalCount = 0
            for resp in self.stub.Gaia3Search(ephem_pb2.EphemSearchRequest(
Wei Shoulin's avatar
gz    
Wei Shoulin committed
33
34
35
36
37
38
39
                ra = ra,
                dec = dec,
                radius = radius,
                minMag = min_mag,
                maxMag = max_mag,
                obstime = obstime,
                limit = limit
Wei Shoulin's avatar
Wei Shoulin committed
40
            ),metadata = get_auth_headers()):
Wei Shoulin's avatar
Wei Shoulin committed
41
42
43
44
45
46
                if resp.success:
                    data = from_proto_model_list(Gaia3Record, resp.records)
                    records.extend(data)
                    totalCount = resp.totalCount
                else:
                    return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
Wei Shoulin committed
47
            return Result.ok_data(data = list(records)).append("totalCount", totalCount)
Wei Shoulin's avatar
Wei Shoulin committed
48
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
49
            return Result.error(message="%s:%s" % (e.code().value, e.details()))