catalog.py 1.64 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
import grpc
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
3
4
from csst_dfs_commons.models.common import from_proto_model_list, Gaia3Record

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

class CatalogApi(object):
    def __init__(self):
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
12
        self.stub = ephem_pb2_grpc.EphemSearchSrvStub(ServiceProxy().channel())
Shoulin Wei's avatar
init  
Shoulin Wei committed
13
14
15
16
17
18
19
20
21
22
23
    
    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
24
            return: csst_dfs_common.models.Result
Shoulin Wei's avatar
init  
Shoulin Wei committed
25
26
        ''' 
        try:
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
27
28
            
            resp, _ = self.stub.Gaia3Search.with_call(ephem_pb2.EphemSearchRequest(
Shoulin Wei's avatar
init  
Shoulin Wei committed
29
30
31
32
33
34
35
                ra = ra,
                dec = dec,
                radius = radius,
                minMag = min_mag,
                maxMag = max_mag,
                obstime = obstime,
                limit = limit
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
36
            ),metadata = get_auth_headers())
Shoulin Wei's avatar
init  
Shoulin Wei committed
37

Wei Shoulin's avatar
Wei Shoulin committed
38
            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
39
                return Result.ok_data(data=from_proto_model_list(Gaia3Record, resp.records)).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
Wei Shoulin committed
40
            else:
Wei Shoulin's avatar
c3    
Wei Shoulin committed
41
                return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
Wei Shoulin committed
42
43
44

        except grpc.RpcError as e:
            return Result.error(message="%s:%s" % (e.code().value, e.details))
Shoulin Wei's avatar
init  
Shoulin Wei committed
45