level0.py 7.76 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
17
    def __init__(self, sub_system = "ifs"):
        self.sub_system = sub_system
Wei Shoulin's avatar
c3  
Wei Shoulin committed
18
19
20
21
22
23
24
        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
25
26
27
28
            obs_id: [str],
            detector_no: [str],
            obs_type: [str],
            object_name: [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
29
30
31
            obs_time : (start, end),
            qc0_status : [int],
            prc_status : [int],
Wei Shoulin's avatar
Wei Shoulin committed
32
            file_name: [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
33
34
35
36
37
38
39
40
            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")
Wei Shoulin's avatar
Wei Shoulin committed
41
            object_name = get_parameter(kwargs, "object_name")
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
42
            version = get_parameter(kwargs, "version")
Wei Shoulin's avatar
c3  
Wei Shoulin committed
43
44
45
46
47
48
            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")
            limit = get_parameter(kwargs, "limit", 0)
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
49
50
51
            ra = get_parameter(kwargs, "ra", None)
            dec = get_parameter(kwargs, "limit", None)
            radius = get_parameter(kwargs, "limit", 0)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
52

Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
53
54
            sql_count = 'select count(*) as c from ifs_level0_data d left join ifs_level0_header h on d.id=h.id where 1=1'
            sql_data = 'select d.* from ifs_level0_data d left join ifs_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
75
76
77
78
79
                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}'"  
            if ra:
                sql_condition = f"{sql_condition} and (h.ra <= {ra+radius} and h.ra >={ra-radius})"  
            if dec:
                sql_condition = f"{sql_condition} and (h.dec <= {dec+radius} and h.ra >={dec-radius})"

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
108
109
110
111
112
113
114
115
116
117
        id = get_parameter(kwargs, "id", 0)
        level0_id = get_parameter(kwargs, "level0_id", "")

        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 != "": 
            return self.get_by_level0_id(level0_id)

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

    def get_by_level0_id(self, level0_id: str):
        try:
            r = self.db.select_one(
Wei Shoulin's avatar
Wei Shoulin committed
132
                "select * from ifs_level0_data where level0_id=?", (level0_id,))
Wei Shoulin's avatar
Wei Shoulin committed
133
134
135
136
137
138
139
            if r:
                return Result.ok_data(data=Level0Record().from_dict(r))
            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
140
141
142
143
144

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

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
145
146
            id : [int],
            level0_id : [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
147
148
149
150
            status : [int]

        return csst_dfs_common.models.Result
        '''
Wei Shoulin's avatar
Wei Shoulin committed
151
152
153
154
155
156
157
158
        id = get_parameter(kwargs, "id")
        level0_id = get_parameter(kwargs, "level0_id")
        result = self.get(id = id, level0_id = level0_id)

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

        id = result.data.id
Wei Shoulin's avatar
c3  
Wei Shoulin committed
159
160
161
        status = get_parameter(kwargs, "status")
        try:
            self.db.execute(
Wei Shoulin's avatar
Wei Shoulin committed
162
                'update ifs_level0_data set prc_status=?, prc_time=? where id=?',
Wei Shoulin's avatar
Wei Shoulin committed
163
                (status, format_time_ms(time.time()), id)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
164
165
166
167
168
169
170
171
172
173
174
175
            )  
            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
176
177
            id : [int],
            level0_id : [str],
Wei Shoulin's avatar
c3  
Wei Shoulin committed
178
179
            status : [int]
        '''        
Wei Shoulin's avatar
Wei Shoulin committed
180
181
182
183
184
185
186
187
        id = get_parameter(kwargs, "id")
        level0_id = get_parameter(kwargs, "level0_id")
        result = self.get(id = id, level0_id = level0_id)

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

        id = result.data.id
Wei Shoulin's avatar
c3  
Wei Shoulin committed
188
189
190
        status = get_parameter(kwargs, "status")
        try:
            self.db.execute(
Wei Shoulin's avatar
Wei Shoulin committed
191
                'update ifs_level0_data set qc0_status=?, qc0_time=? where id=?',
Wei Shoulin's avatar
Wei Shoulin committed
192
                (status, format_time_ms(time.time()), id)
Wei Shoulin's avatar
c3  
Wei Shoulin committed
193
194
195
196
197
198
199
200
201
202
203
204
            )  
            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
205
206
            file_path = [str],
            copyfiles = [boolean]
Wei Shoulin's avatar
c3  
Wei Shoulin committed
207
208
209
        return: csst_dfs_common.models.Result
        '''          

Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
210
211
212
213
        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
214
215
216
217
218
219
            return Result.ok_data(data=rec)
        except Exception as e:
            log.error(e)
            return Result.error(message=str(e))