ifs_rss_pipeline.py 4.71 KB
Newer Older
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
1
2
3
4
5
6
import os
import numpy as np
import pandas as pd
import logging
from astropy.io import fits

Wei Shoulin's avatar
ephem    
Wei Shoulin committed
7
from csst_dfs_commons.logging import setup_logging
Wei Shoulin's avatar
docs    
Wei Shoulin committed
8
9
from csst_dfs_api.facility import Level0DataApi, CalMergeApi
from csst_dfs_api.ifs import Level1DataApi
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
10
11
12
13
14
15
16
17
setup_logging()

log = logging.getLogger('csst')

class RSS(object):

    def __init__(self, file_name):
        self.root_dir = os.getenv("CSST_LOCAL_FILE_ROOT", "/opt/temp/csst")
Wei Shoulin's avatar
docs    
Wei Shoulin committed
18
19
20
        self.level0Api = Level0DataApi()
        self.calibrationApi = CalMergeApi()
        self.level1Api = Level1DataApi()
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
21
22
        
        try:
Wei Shoulin's avatar
docs    
Wei Shoulin committed
23
24
            self.raw = self.level0Api.find(file_name=file_name)
            self.raw = self.raw.data[0] if self.raw.success else None
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
25

Wei Shoulin's avatar
add rss  
Wei Shoulin committed
26
27
            if self.raw is None:
                log.error('raw %s not found' %(file_name,))
Wei Shoulin's avatar
error    
Wei Shoulin committed
28
29
30
            else:
                log.info("find raw fits: %s,%s"%(self.raw["id"], self.raw["filename"]))

Wei Shoulin's avatar
add rss  
Wei Shoulin committed
31
32
33
34
35
        except Exception as e:
            log.error('raw %s not found' %(file_name,),e)

    def set_bias(self, file_name=None):
        try:
Wei Shoulin's avatar
docs    
Wei Shoulin committed
36
37
            self.bias = self.calibrationApi.find(file_name=file_name, ref_type="bias")
            self.bias = self.bias.data[0] if self.bias.success else None
Wei Shoulin's avatar
error    
Wei Shoulin committed
38
            
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
39
40
            if self.bias is None:
                log.error('bias %s not found' %(file_name,))
Wei Shoulin's avatar
error    
Wei Shoulin committed
41
42
            else:
                log.info("find ref bias fits: %s,%s"%(self.bias["id"], self.bias["filename"]))
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
43
44
45
46
47
        except Exception as e:
            log.error('bias %s not found' %(file_name,),e)

    def set_flat(self, file_name=None):
        try:
Wei Shoulin's avatar
docs    
Wei Shoulin committed
48
49
            self.flat = self.calibrationApi.find(file_name=file_name, ref_type="flat")
            self.flat = self.flat.data[0] if self.flat.success else None
Wei Shoulin's avatar
error    
Wei Shoulin committed
50
            
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
51
52
            if self.flat is None:
                log.error('flat %s not found' %(file_name,))
Wei Shoulin's avatar
error    
Wei Shoulin committed
53
54
            else:
                log.info("find ref flat fits: %s,%s"%(self.flat["id"], self.flat["filename"]))
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
55
56
57
58
59
        except Exception as e:
            log.error('flat %s not found' %(file_name,),e)

    def set_arc(self, file_name = None):
        try:
Wei Shoulin's avatar
docs    
Wei Shoulin committed
60
61
            self.arc = self.calibrationApi.find(file_name=file_name, ref_type="arc")
            self.arc = self.arc.data[0] if self.arc.success else None
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
62
63
64

            if self.arc is None:
                log.error('arc %s not found' %(file_name,))
Wei Shoulin's avatar
error    
Wei Shoulin committed
65
66
            else:
                log.info("find ref arc fits: %s,%s"%(self.arc["id"], self.arc["filename"]))
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
67
68
69
70
71
        except Exception as e:
            log.error('arc %s not found' %(file_name,),e)

    def set_sky(self, file_name = None):
        try:
Wei Shoulin's avatar
docs    
Wei Shoulin committed
72
73
            self.sky = self.calibrationApi.find(file_name=file_name, ref_type="sky")
            self.sky = self.sky.data[0] if self.sky.success else None
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
74
75
76

            if self.sky is None:
                log.error('sky %s not found' %(file_name,))
Wei Shoulin's avatar
error    
Wei Shoulin committed
77
78
            else:
                log.info("find ref sky fits: %s,%s"%(self.sky["id"], self.sky["filename"]))
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
79
80
81
82
        except Exception as e:
            log.error('sky %s not found' %(file_name,),e)

    def makecube(self, outfile):
Wei Shoulin's avatar
error    
Wei Shoulin committed
83
84
85
86
87
88
89
90
91
92
93
94
        if self.raw is None:
            log.error('raw not found')
            return
        if self.arc is None:
            log.error('arc not found')
            return
        if self.flat is None:
            log.error('flat not found')
            return
        if self.sky is None:
            log.error('sky not found')
            return
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
95

Wei Shoulin's avatar
docs    
Wei Shoulin committed
96
97
98
99
        hdul_raw = fits.open(os.path.join(self.raw.file_path))
        hdul_arc = fits.open(os.path.join(self.arc.file_path))
        hdul_flat = fits.open(os.path.join(self.flat.file_path))
        hdul_sky  = fits.open(os.path.join(self.sky.file_path))
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
100
101
102
103
104
105
106

        hdul_raw.append(hdul_arc[0])
        hdul_raw.append(hdul_flat[0])
        hdul_raw.append(hdul_sky[0])

        hdul_raw.writeto(outfile, overwrite=True)

Wei Shoulin's avatar
docs    
Wei Shoulin committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        self.level1Api.write(level0_id = self.raw.id, 
            data_type = "sci",
            cor_sci_id = 2,
            prc_params = "/opt/dddasd.params",
            flat_id = self.flat.id,
            dark_id = -1,
            bias_id = -1,
            lamp_id = -1,
            arc_id = self.arc.id,
            sky_id = self.sky.id,            
            prc_status = 0,
            filename = "rss_demo1",
            file_path = outfile,
            pipeline_id = "P2")
Wei Shoulin's avatar
add rss  
Wei Shoulin committed
121
122
123
124
125
126
127
128
129

if __name__ == '__main__':
    rss1 = RSS('CCD1_ObsTime_300_ObsNum_1.fits')               # raw data
    # rss1.set_bias()                                             # currently no Bias file
    rss1.set_flat(file_name = 'Flat_flux.fits')                 # flat file
    rss1.set_arc(file_name = 'HgAr_flux.fits')                  # arc file
    rss1.set_sky(file_name = 'sky_noise_With_wavelength.fits')  # sky file

    rss1.makecube('/opt/temp/csst_ifs/rss_demo1.fits')