catalog.py 1.89 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
import grpc
Wei Shoulin's avatar
timeit    
Wei Shoulin committed
2
3
import time
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
ephem    
Wei Shoulin committed
30
31
            
            resp, _ = self.stub.Gaia3Search.with_call(ephem_pb2.EphemSearchRequest(
Shoulin Wei's avatar
init  
Shoulin Wei committed
32
33
34
35
36
37
38
                ra = ra,
                dec = dec,
                radius = radius,
                minMag = min_mag,
                maxMag = max_mag,
                obstime = obstime,
                limit = limit
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
39
            ),metadata = get_auth_headers())
Shoulin Wei's avatar
init  
Shoulin Wei committed
40

Wei Shoulin's avatar
Wei Shoulin committed
41
            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
42
                # t_start = time.time()
Wei Shoulin's avatar
timeit    
Wei Shoulin committed
43
                data = from_proto_model_list(Gaia3Record, resp.records)
Wei Shoulin's avatar
Wei Shoulin committed
44
45
46
                # t_end = time.time()
                # log.info("object deserialization used: %.6f's" %(t_end - t_start,))  
                return Result.ok_data(data = data).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
Wei Shoulin committed
47
            else:
Wei Shoulin's avatar
c3    
Wei Shoulin committed
48
                return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
Wei Shoulin committed
49
50

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