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
Wei Shoulin committed
30
            resps, _ = self.stub.Gaia3Search.with_call(ephem_pb2.EphemSearchRequest(
Wei Shoulin's avatar
gz    
Wei Shoulin committed
31
32
33
34
35
36
37
38
                ra = ra,
                dec = dec,
                radius = radius,
                minMag = min_mag,
                maxMag = max_mag,
                obstime = obstime,
                limit = limit
            ),metadata = get_auth_headers())
Wei Shoulin's avatar
Wei Shoulin committed
39
40
41
42
43
44
45
46
47
48
            records = []
            totalCount = 0
            for resp in resps:
                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))
            return Result.ok_data(data = records).append("totalCount", totalCount)
Wei Shoulin's avatar
Wei Shoulin committed
49
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
50
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Shoulin Wei's avatar
init  
Shoulin Wei committed
51