catalog.py 2 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
time it    
Wei Shoulin committed
30
            t_start = time.time()
Wei Shoulin's avatar
gz    
Wei Shoulin committed
31
32
33
34
35
36
37
38
39
            resp, _ = self.stub.Gaia3Search.with_call(ephem_pb2.EphemSearchRequest(
                ra = ra,
                dec = dec,
                radius = radius,
                minMag = min_mag,
                maxMag = max_mag,
                obstime = obstime,
                limit = limit
            ),metadata = get_auth_headers())
Wei Shoulin's avatar
time it    
Wei Shoulin committed
40
41
            t_end = time.time()
            log.info("gaia3_query used: %.6f's" %(t_end - t_start,)) 
Wei Shoulin's avatar
Wei Shoulin committed
42
            if resp.success:
Wei Shoulin's avatar
time it    
Wei Shoulin committed
43
                t_start = time.time()
Wei Shoulin's avatar
timeit    
Wei Shoulin committed
44
                data = from_proto_model_list(Gaia3Record, resp.records)
Wei Shoulin's avatar
time it    
Wei Shoulin committed
45
                t_end = time.time()
Wei Shoulin's avatar
gz    
Wei Shoulin committed
46
                # log.info("object deserialization used: %.6f's" %(t_end - t_start,))  
Wei Shoulin's avatar
Wei Shoulin committed
47
                return Result.ok_data(data = data).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
Wei Shoulin committed
48
            else:
Wei Shoulin's avatar
c3    
Wei Shoulin committed
49
                return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
Wei Shoulin committed
50
51

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