catalog.py 2.39 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
Wei Shoulin committed
3
from collections import deque
Wei Shoulin's avatar
timeit    
Wei Shoulin committed
4
import logging
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
5
import io
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
6
import time
Wei Shoulin's avatar
bytes    
Wei Shoulin committed
7

Wei Shoulin's avatar
Wei Shoulin committed
8
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
9
10
from csst_dfs_commons.models.common import from_proto_model_list, Gaia3Record

Shoulin Wei's avatar
init  
Shoulin Wei committed
11
from csst_dfs_proto.common.ephem import ephem_pb2, ephem_pb2_grpc
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
12
13
14
from .service import ServiceProxy
from .constants import *
from .utils import get_auth_headers
Shoulin Wei's avatar
init  
Shoulin Wei committed
15

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

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