level2catalog.py 3.09 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import logging
import numpy as np
from astropy.io import fits

from ..common.delegate import Delegate
from ..common.utils import *
from csst_dfs_commons.models import Result

log = logging.getLogger('csst_api')

class Level2CatalogApi(object):
    """
    Level1 Data Operation Class
    """
    def __init__(self):
        self.pymodule = Delegate().load(sub_module = "msc")
        self.stub = getattr(self.pymodule, "Level2CatalogApi")()
        
    def find(self, **kwargs):
        ''' retrieve level2catalog records from database

        :param kwargs: Parameter dictionary, key items support:
            obs_id: [str]
            detector_no: [str]
            min_mag: [float]
            max_mag: [float]
            obs_time: (start, end),
            limit: limits returns the number of records,default 0:no-limit
        
        :returns: csst_dfs_common.models.Result
        '''
        return self.stub.find(**kwargs)

    def write(self, **kwargs):
        ''' insert a level2 catalog file into database
 
        :param kwargs: Parameter dictionary, key items support:
            file_path: str

        :returns: csst_dfs_common.models.Result
        '''          

        try:
            file_path = get_parameter(kwargs, "file_path", '')
            if not file_path:
                return Result.error(message="file_path is blank")
            if not os.path.exists(file_path):
                return Result.error(message="the file [%s] not existed" % (file_path, )) 
            records = []
            success_num, fail_num = 0, 0

            hdul = fits.open(file_path)
            header = hdul[0].header
            binTable = hdul[1]

            obs_id = header["OBSID"]
            detector_no = header["DETECTOR"][3:5]
            obs_time = f"{header['DATE-OBS']} {header['TIME-OBS']}"
            batch_size = 500
            for tr in binTable.data:
                v_list = [f"'{obs_id}'",f"'{detector_no}'"]
                for td in tr:
                    if type(td) == np.ndarray:
                        v_list.extend(td)
                    else:
                        v_list.append(td)
                v_list.append(f"'{obs_time}'")
                records.append(",".join(['null' if type(v) != str and np.isnan(v) else str(v) for v in v_list]))
            
                if len(records) == batch_size:
                    resp = self.stub.write(records)
                    if resp.success:
                        success_num += len(records)
                    else:
                        log.error(f"{resp.message}")
                        fail_num  += len(records)
                    records.clear()
                    records = []
            if records:
                resp = self.stub.write(records)
                if resp.success:
                    success_num += len(records)
                else:
                    log.error(f"{resp.message}")
                    fail_num  += len(records)                
            return Result.ok_data({"success_num":success_num, "fail_num": fail_num})
        except Exception as e:
            return Result.error(str(e))