ingest.py 4.79 KB
Newer Older
Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
1
2
3
4
5
6
7
8
import os, sys
import argparse
import logging
from astropy.io import fits
import datetime
import shutil

from csst_dfs_api_local.common.db import DBClient
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
9
from csst_dfs_commons.utils.fits import get_header_value
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
10
from csst_dfs_commons.models.mci import Level0Record
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
11

Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
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
log = logging.getLogger('csst-dfs-api-local')

def ingest():
    
    parser = argparse.ArgumentParser(prog=f"{sys.argv[0]}", description="ingest the local files")
    parser.add_argument('-i','--infile', dest="infile", help="a file or a directory")
    parser.add_argument('-m', '--copyfiles', dest="copyfiles", action='store_true', default=False, help="whether copy files after import")
    args = parser.parse_args(sys.argv[1:])

    import_root_dir = args.infile
    if import_root_dir is None or (not os.path.isfile(import_root_dir) and not os.path.isdir(import_root_dir)):
        parser.print_help()
        sys.exit(0)

    db = DBClient()
    if os.path.isfile(import_root_dir):
        log.info(f"prepare import {import_root_dir}")
        ingest_one(import_root_dir, db, args.copyfiles)
    if os.path.isdir(import_root_dir):
        for (path, _, file_names) in os.walk(import_root_dir):
            for filename in file_names:
                if filename.find(".fits") > 0:
                    file_full_path = os.path.join(path, filename)
                    log.info(f"prepare import {file_full_path}")
                    try:
                        ingest_one(file_full_path, db, args.copyfiles)
                    except Exception as e:
                        print(f"{file_full_path} import error!!!")
                        log.error(e)

    db.close()

def ingest_one(file_path, db, copyfiles):
    dest_root_dir = os.getenv("CSST_LOCAL_FILE_ROOT", "/opt/temp/csst")

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

    obs_id = header["OBSID"]
    exp_start_time = f"{header['DATE-OBS']}"
    exp_time = header['EXPTIME']
    
    module_id = header["INSTRUME"]
    obs_type = header["OBSTYPE"]
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
57
    object_name = get_header_value("OBJECT", header, "-")
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
58

Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    qc0_status = -1
    prc_status = -1
    time_now = datetime.datetime.now()
    create_time = time_now.strftime('%Y-%m-%d %H:%M:%S')

    facility_status_id = 0
    module_status_id = 0

    existed = db.exists("select * from t_observation where obs_id=?", (obs_id,))
    if not existed:
        db.execute("insert into t_observation \
            (obs_id,obs_time,exp_time,module_id,obs_type,facility_status_id, module_status_id, qc0_status, prc_status,create_time) \
            values (?,?,?,?,?,?,?,?,?,?)",
        (obs_id,exp_start_time,exp_time,module_id,obs_type,facility_status_id,module_status_id,qc0_status, prc_status,create_time))
        db.end()
    #level0
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
75
    detector = get_header_value("DETNAM", header1, "-")
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
76
77
    filename = get_header_value("FILENAME", header, os.path.basename(file_path))
    version = get_header_value("IMG_VER", header1, "-")
Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    existed = db.exists(
            "select * from mci_level0_data where filename=?",
            (filename,)
        )
    if existed:
        log.warning('%s has already been imported' %(file_path, ))
        db.end()
        return    

    detector_status_id = 0

    file_full_path = file_path

    if copyfiles:
        file_dir = f"{dest_root_dir}/{module_id}/{obs_type.upper()}/{header['EXPSTART']}/{obs_id}/MS"
        if not os.path.exists(file_dir):
            os.makedirs(file_dir)
        file_full_path = f"{file_dir}/{filename}.fits"

    level0_id = f"{obs_id}{detector}"  

    c = db.execute("insert into mci_level0_data \
        (level0_id, obs_id, detector_no, obs_type, obs_time, exp_time,detector_status_id, filename, file_path,qc0_status, prc_status,create_time) \
        values (?,?,?,?,?,?,?,?,?,?,?,?)",
        (level0_id, obs_id, detector, obs_type, exp_start_time, exp_time, detector_status_id, filename, file_full_path, qc0_status, prc_status,create_time))
    db.end()
    level0_id_id = db.last_row_id()
    #level0-header
    ra_obj = header["OBJ_RA"]
    dec_obj = header["OBJ_DEC"]
    db.execute("delete from mci_level0_header where id=?",(level0_id_id,))    
    db.execute("insert into mci_level0_header \
Wei Shoulin's avatar
Wei Shoulin committed
110
        (id, ra_obj, dec_obj, object_name, version) \
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
111
112
        values (?,?,?,?,?)",
        (level0_id_id, ra_obj, dec_obj, object_name, version))
Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
113
114
115
116
117
118
119
    
    if copyfiles:
        #copy files
        shutil.copyfile(file_path, file_full_path)

    db.end()

Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
120
121
122
123
124
125
126
127
128
129
130
131
    rec = Level0Record(
        id = level0_id_id,
        level0_id = level0_id,
        obs_id = obs_id,
        detector_no = detector,
        obs_type = obs_type,
        obs_time = exp_start_time,
        exp_time = exp_time,
        detector_status_id = detector_status_id,
        filename = filename,
        file_path = file_full_path
    )
Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
132
    print(f"{file_path} imported")
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
133
    return rec
Wei Shoulin's avatar
mci 0-1  
Wei Shoulin committed
134
135
136

if __name__ == "__main__":
    ingest()