catalog.py 1.98 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
import grpc
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
2
import pickle
Wei Shoulin's avatar
timeit    
Wei Shoulin committed
3
import logging
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
4
5
import io

Wei Shoulin's avatar
Wei Shoulin committed
6
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
7

Shoulin Wei's avatar
init  
Shoulin Wei committed
8
from csst_dfs_proto.common.ephem import ephem_pb2, ephem_pb2_grpc
Wei Shoulin's avatar
Wei Shoulin committed
9
from .service import grpc_channel
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
10
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
Wei Shoulin committed
15
16
17
18
        self.stub_class = ephem_pb2_grpc.EphemSearchSrvStub
        self.stub = None

    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
19
    def gaia3_query(self, ra: float, dec: float, radius: float, columns: tuple, min_mag: float,  max_mag: float,  obstime: int, limit: int):
Shoulin Wei's avatar
init  
Shoulin Wei committed
20
21
22
23
24
        ''' retrieval GAIA DR 3
            args:
                ra:  in deg
                dec:  in deg
                radius:  in deg
Wei Shoulin's avatar
Wei Shoulin committed
25
                tuple of str, like ('ra','dec','phot_g_mean_mag')
Shoulin Wei's avatar
init  
Shoulin Wei committed
26
27
28
29
                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
30
            return: csst_dfs_common.models.Result
Shoulin Wei's avatar
init  
Shoulin Wei committed
31
32
        ''' 
        try:
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
33
            datas = io.BytesIO()
Wei Shoulin's avatar
Wei Shoulin committed
34
            totalCount = 0
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
35
            resps = self.stub.Gaia3Search(ephem_pb2.EphemSearchRequest(
Wei Shoulin's avatar
gz    
Wei Shoulin committed
36
37
38
                ra = ra,
                dec = dec,
                radius = radius,
Wei Shoulin's avatar
Wei Shoulin committed
39
                columns = ",".join(columns),
Wei Shoulin's avatar
gz    
Wei Shoulin committed
40
41
42
43
                minMag = min_mag,
                maxMag = max_mag,
                obstime = obstime,
                limit = limit
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
44
45
            ),metadata = get_auth_headers())
            for resp in resps:
Wei Shoulin's avatar
Wei Shoulin committed
46
                if resp.success:
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
47
                    datas.write(resp.records)
Wei Shoulin's avatar
Wei Shoulin committed
48
49
50
                    totalCount = resp.totalCount
                else:
                    return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
51
            datas.flush()
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
52
            records = pickle.loads(datas.getvalue())
Wei Shoulin's avatar
Wei Shoulin committed
53
54

            return Result.ok_data(data = records).append("totalCount", totalCount).append("columns", columns)
Wei Shoulin's avatar
Wei Shoulin committed
55
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
56
            return Result.error(message="%s:%s" % (e.code().value, e.details()))