level0.py 8.26 KB
Newer Older
Wei Shoulin's avatar
c3  
Wei Shoulin committed
1
2
3
4
5
6
7
8
import os
import logging
import time, datetime
import shutil

from ..common.db import DBClient
from ..common.utils import *
from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
9
from csst_dfs_commons.models.ifs import Level0Record
Wei Shoulin's avatar
c3  
Wei Shoulin committed
10
from csst_dfs_commons.models.common import from_dict_list
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
11
from .ingest import ingest_one
Wei Shoulin's avatar
c3  
Wei Shoulin committed
12
13
14
15

log = logging.getLogger('csst')

class Level0DataApi(object):
Wei Shoulin's avatar
Wei Shoulin committed
16
    def __init__(self):
Wei Shoulin's avatar
c3  
Wei Shoulin committed
17
18
19
20
21
22
23
        self.root_dir = os.getenv("CSST_LOCAL_FILE_ROOT", "/opt/temp/csst")
        self.db = DBClient()

    def find(self, **kwargs):
        ''' retrieve level0 records from database

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
24
25
26
27
            obs_id: [str],
            detector_no: [str],
            obs_type: [str],
            object_name: [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
28
29
30
            obs_time : (start, end),
            qc0_status : [int],
            prc_status : [int],
Wei Shoulin's avatar
Wei Shoulin committed
31
            file_name: [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
32
33
34
35
36
37
38
39
40
41
42
43
44
            limit: limits returns the number of records,default 0:no-limit

        return: csst_dfs_common.models.Result
        '''
        try:
            obs_id = get_parameter(kwargs, "obs_id")
            detector_no = get_parameter(kwargs, "detector_no")
            obs_type = get_parameter(kwargs, "obs_type")
            exp_time_start = get_parameter(kwargs, "obs_time", [None, None])[0]
            exp_time_end = get_parameter(kwargs, "obs_time", [None, None])[1]
            qc0_status = get_parameter(kwargs, "qc0_status")
            prc_status = get_parameter(kwargs, "prc_status")
            file_name = get_parameter(kwargs, "file_name")
Wei Shoulin's avatar
Wei Shoulin committed
45
            
Wei Shoulin's avatar
Wei Shoulin committed
46
47
            ra_obj  = get_parameter(kwargs, "ra_obj", None)
            dec_obj = get_parameter(kwargs, "dec_obj", None)
48
            radius = get_parameter(kwargs, "radius", 0)
Wei Shoulin's avatar
Wei Shoulin committed
49
50
51
            object_name = get_parameter(kwargs, "object_name")
            version = get_parameter(kwargs, "version", None)
            limit = get_parameter(kwargs, "limit", 0)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
52

Wei Shoulin's avatar
Wei Shoulin committed
53
54
            sql_count = 'select count(*) as c from t_level0_data d left join t_level0_header h on d.id=h.id where 1=1'
            sql_data = 'select d.* from t_level0_data d left join t_level0_header h on d.id=h.id where 1=1'
Wei Shoulin's avatar
c3  
Wei Shoulin committed
55
56

            sql_condition = "" 
Wei Shoulin's avatar
Wei Shoulin committed
57
            if obs_id:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
58
                sql_condition = f"{sql_condition} and d.obs_id='{obs_id}'"              
Wei Shoulin's avatar
c3  
Wei Shoulin committed
59
            if detector_no:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
60
                sql_condition = f"{sql_condition} and d.detector_no='{detector_no}'"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
61
            if obs_type:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
62
                sql_condition = f"{sql_condition} and d.obs_type='{obs_type}'"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
63
            if exp_time_start:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
64
                sql_condition = f"{sql_condition} and d.obs_time >='{exp_time_start}'"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
65
            if exp_time_end:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
66
                sql_condition = f"{sql_condition} and d.obs_time <='{exp_time_end}'"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
67
            if qc0_status:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
68
                sql_condition = f"{sql_condition} and d.qc0_status={qc0_status}"
Wei Shoulin's avatar
c3  
Wei Shoulin committed
69
            if prc_status:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
70
71
72
73
74
                sql_condition = f"{sql_condition} and d.prc_status={prc_status}"  
            if object_name:
                sql_condition = f"{sql_condition} and h.object_name='{object_name}'"
            if version:
                sql_condition = f"{sql_condition} and h.version='{version}'"  
Wei Shoulin's avatar
Wei Shoulin committed
75
76
77
78
            if ra_obj:
                sql_condition = f"{sql_condition} and (h.ra <= {ra_obj + 2*radius} and h.ra >={ra_obj - 2*radius})"  
            if dec_obj:
                sql_condition = f"{sql_condition} and (h.dec <= {dec_obj + 2*radius} and h.ra >={dec_obj - 2*radius})"
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
79

Wei Shoulin's avatar
c3  
Wei Shoulin committed
80
            if file_name:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
81
                sql_condition = f" and d.filename='{file_name}'"  
Wei Shoulin's avatar
c3  
Wei Shoulin committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

            sql_count = f"{sql_count} {sql_condition}"
            sql_data = f"{sql_data} {sql_condition}"

            if limit > 0:
                sql_data = f"{sql_data} limit {limit}"   

            totalCount = self.db.select_one(sql_count)
            _, records = self.db.select_many(sql_data)

            return Result.ok_data(data=from_dict_list(Level0Record, records)).append("totalCount", totalCount['c'])

        except Exception as e:
            return Result.error(message=str(e))

    def get(self, **kwargs):
        '''  fetch a record from database

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
101
102
            id : [int],
            level0_id : [str]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
103
104
105

        return csst_dfs_common.models.Result
        '''
Wei Shoulin's avatar
Wei Shoulin committed
106
107
        id = get_parameter(kwargs, "id", 0)
        level0_id = get_parameter(kwargs, "level0_id", "")
Wei Shoulin's avatar
Wei Shoulin committed
108
        obs_type = get_parameter(kwargs, "obs_type")
Wei Shoulin's avatar
Wei Shoulin committed
109
110
111
112
113
114
115

        if id == 0 and level0_id == "":
            return Result.error(message="at least define id or level0_id") 

        if id != 0: 
            return self.get_by_id(id)
        if level0_id != "": 
Wei Shoulin's avatar
Wei Shoulin committed
116
            return self.get_by_level0_id(level0_id, obs_type)
Wei Shoulin's avatar
Wei Shoulin committed
117
118

    def get_by_id(self, id: int):
Wei Shoulin's avatar
c3  
Wei Shoulin committed
119
120
        try:
            r = self.db.select_one(
Wei Shoulin's avatar
Wei Shoulin committed
121
                "select * from t_level0_data where id=?", (id,))
Wei Shoulin's avatar
c3  
Wei Shoulin committed
122
            if r:
Wei Shoulin's avatar
Wei Shoulin committed
123
                data = Level0Record().from_dict(r)
Wei Shoulin's avatar
Wei Shoulin committed
124
                data.header = self.db.select_one("select * from t_level0_header where id=?", (id,))
Wei Shoulin's avatar
Wei Shoulin committed
125
                return Result.ok_data(data = data)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
126
            else:
Wei Shoulin's avatar
Wei Shoulin committed
127
                return Result.error(message=f"id:{id} not found")  
Wei Shoulin's avatar
c3  
Wei Shoulin committed
128
129
        except Exception as e:
            log.error(e)
Wei Shoulin's avatar
Wei Shoulin committed
130
131
            return Result.error(message=str(e))  

Wei Shoulin's avatar
Wei Shoulin committed
132
    def get_by_level0_id(self, level0_id: str, obs_type: str):
Wei Shoulin's avatar
Wei Shoulin committed
133
134
        try:
            r = self.db.select_one(
Wei Shoulin's avatar
Wei Shoulin committed
135
                "select * from t_level0_data where level0_id=? and obs_type=?", (level0_id, obs_type,))
Wei Shoulin's avatar
Wei Shoulin committed
136
            if r:
Wei Shoulin's avatar
Wei Shoulin committed
137
                data = Level0Record().from_dict(r)
Wei Shoulin's avatar
Wei Shoulin committed
138
                data.header = self.db.select_one("select * from t_level0_header where id=?", (data.id,)) 
Wei Shoulin's avatar
Wei Shoulin committed
139
                return Result.ok_data(data = data)
Wei Shoulin's avatar
Wei Shoulin committed
140
141
142
143
144
            else:
                return Result.error(message=f"level0_id:{level0_id} not found")  
        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))  
Wei Shoulin's avatar
c3  
Wei Shoulin committed
145
146
147
148
149

    def update_proc_status(self, **kwargs):
        ''' update the status of reduction

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
150
151
            id : [int],
            level0_id : [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
152
153
154
155
            status : [int]

        return csst_dfs_common.models.Result
        '''
Wei Shoulin's avatar
Wei Shoulin committed
156
157
        id = get_parameter(kwargs, "id")
        level0_id = get_parameter(kwargs, "level0_id")
Wei Shoulin's avatar
Wei Shoulin committed
158
159
160
        obs_type = get_parameter(kwargs, "obs_type")

        result = self.get(id = id, level0_id = level0_id, obs_type = obs_type)
Wei Shoulin's avatar
Wei Shoulin committed
161
162
163
164
165

        if not result.success:
            return Result.error(message="not found")

        id = result.data.id
Wei Shoulin's avatar
c3  
Wei Shoulin committed
166
167
168
        status = get_parameter(kwargs, "status")
        try:
            self.db.execute(
Wei Shoulin's avatar
Wei Shoulin committed
169
                'update t_level0_data set prc_status=?, prc_time=? where id=?',
Wei Shoulin's avatar
Wei Shoulin committed
170
                (status, format_time_ms(time.time()), id)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
171
172
173
174
175
176
177
178
179
180
181
182
            )  
            self.db.end() 
            return Result.ok_data()
           
        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))

    def update_qc0_status(self, **kwargs):
        ''' update the status of QC0
        
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
183
184
            id : [int],
            level0_id : [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
185
186
            status : [int]
        '''        
Wei Shoulin's avatar
Wei Shoulin committed
187
188
        id = get_parameter(kwargs, "id")
        level0_id = get_parameter(kwargs, "level0_id")
Wei Shoulin's avatar
Wei Shoulin committed
189
190
        obs_type = get_parameter(kwargs, "obs_type")
        result = self.get(id = id, level0_id = level0_id, obs_type = obs_type)
Wei Shoulin's avatar
Wei Shoulin committed
191
192
193
194
195

        if not result.success:
            return Result.error(message="not found")

        id = result.data.id
Wei Shoulin's avatar
c3  
Wei Shoulin committed
196
197
198
        status = get_parameter(kwargs, "status")
        try:
            self.db.execute(
Wei Shoulin's avatar
Wei Shoulin committed
199
                'update t_level0_data set qc0_status=?, qc0_time=? where id=?',
Wei Shoulin's avatar
Wei Shoulin committed
200
                (status, format_time_ms(time.time()), id)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
201
202
203
204
205
206
207
208
209
210
211
212
            )  
            self.db.end() 
            return Result.ok_data()
           
        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))

    def write(self, **kwargs):
        ''' insert a level0 data record into database
 
        parameter kwargs:
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
213
214
            file_path = [str],
            copyfiles = [boolean]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
215
216
217
        return: csst_dfs_common.models.Result
        '''          

Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
218
219
220
221
        file_path = get_parameter(kwargs, "file_path")
        copyfiles = get_parameter(kwargs, "copyfiles", False)
        try:
            rec = ingest_one(file_path, self.db, copyfiles)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
222
223
224
225
226
227
            return Result.ok_data(data=rec)
        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))