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
ephem    
Wei Shoulin committed
9
10
11
from .service import ServiceProxy
from .constants import *
from .utils import get_auth_headers
Shoulin Wei's avatar
init  
Shoulin Wei committed
12

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

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