Commit 33f18c07 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

first commit

parent 497f4a5b
__version__ = "0.0.1"
\ No newline at end of file
...@@ -3,46 +3,16 @@ from collections import OrderedDict ...@@ -3,46 +3,16 @@ from collections import OrderedDict
import astropy.io.fits as fits import astropy.io.fits as fits
from astropy.io.fits import HDUList, PrimaryHDU from astropy.io.fits import HDUList, PrimaryHDU
from csst.msc.mscdata import CsstMscImgData
from .CsstException import CsstException from .CsstException import CsstException
class CsstDataFactory: INSTRUMENT_LIST = ["MSC", ]
"""
This class is designed to create CsstData and its inherited classes according to different kinds of input data format.
"""
def __init__(self):
pass
def createData(self, fitsfilename): class CsstData:
""" """
general CSST data class
Parameters
----------
fitsfilename:
the file name of fits files
Returns
-------
""" """
try:
hl = fits.open(fitsfilename)
instrument = hl[0].header.get('INSTRUME') # strip or not?
detector = hl[0].header.get('DETECTOR') # strip or not?
print(instrument, detector)
if instrument == 'MSC' and int(detector[3:5]) >= 6 and int(detector[3:5]) <= 25:
# multi-band imaging
data = CsstMscImgData(hl[0], hl[1])
except Exception as e:
print(e)
return data
class CsstData:
_primary_hdu = [] _primary_hdu = []
_l0data = [] # HDUList _l0data = [] # HDUList
_l1hdr_global = [] _l1hdr_global = []
...@@ -50,28 +20,30 @@ class CsstData: ...@@ -50,28 +20,30 @@ class CsstData:
_l2data = OrderedDict() # _l2data = OrderedDict() #
_auxdata = OrderedDict() _auxdata = OrderedDict()
def __init__(self, primaryHDU, imgHDU): def __init__(self, primaryHDU, imgHDU, instrument=None, detector=None):
print('create CsstData') print('create CsstData')
self._primary_hdu = primaryHDU self._primary_hdu = primaryHDU
self._l0data = imgHDU self._l0data = imgHDU
self.instrument = instrument
self.detector = detector
def get_l0data(self, *, copy): def get_l0data(self, copy=True):
''' """
obtain level 0 data from CsstData class obtain level 0 data from CsstData class
copy: True: if the user want to copy the memory of the data to the new class; copy: True: if the user want to copy the memory of the data to the new class;
False: only reference of the data memory is written to the new class False: only reference of the data memory is written to the new class
''' """
if copy: if copy:
return self._l0data.data.copy() return self._l0data.data.copy()
else: else:
return self._l0data.data return self._l0data.data
def get_l0keyword(self, ext, key): def get_l0keyword(self, ext="pri", key="INSTRUME"):
''' """
obtain keywords of the fits header of level 0 image data from the CsstData class obtain keywords of the fits header of level 0 image data from the CsstData class
ext: the index of extension. if it equals to 'pri', looking up keywords from primary session, otherwise from extension sessions ext: the index of extension. if it equals to 'pri', looking up keywords from primary session, otherwise from extension sessions
key: the name of the key key: the name of the key
''' """
if ext == 'pri': if ext == 'pri':
try: try:
value = self._primary_hdu.header.get(key) value = self._primary_hdu.header.get(key)
...@@ -84,7 +56,6 @@ class CsstData: ...@@ -84,7 +56,6 @@ class CsstData:
print(e) print(e)
else: else:
raise CsstException raise CsstException
return value
def set_l1keyword(self, key, value): def set_l1keyword(self, key, value):
print('check out whether ' + key + " is a valid key and " + value + " is valid value") print('check out whether ' + key + " is a valid key and " + value + " is valid value")
...@@ -94,13 +65,14 @@ class CsstData: ...@@ -94,13 +65,14 @@ class CsstData:
def get_auxdata(self, name): def get_auxdata(self, name):
print('Parent class returns zero image.') print('Parent class returns zero image.')
return np.zeros_like(get_l0data()) # return np.zeros_like(self.get_l0data())
return
def save_l1data(self, imgtype, filename): def save_l1data(self, imgtype, filename):
''' """
asve level 1 image and auxilary data to data file asve level 1 image and auxilary data to data file
imgtype imgtype
''' """
print("save L1 image to a fits file with name " + filename) print("save L1 image to a fits file with name " + filename)
try: try:
self._l1hdr_global.set('TYPE', imgtype, 'Type of Level 1 data') self._l1hdr_global.set('TYPE', imgtype, 'Type of Level 1 data')
......
from astropy.io import fits
from .data import INSTRUMENT_LIST
from ..msc.mscdata import CsstMscImgData
class CsstDataFactory:
"""
This class is designed to create CsstData and its inherited classes according to different kinds of input data format.
"""
def __init__(self):
pass
@staticmethod
def createData(fitsfilename):
""" create CSST Data instances
Parameters
----------
fitsfilename:
the file name of fits files
Returns
-------
"""
try:
hl = fits.open(fitsfilename)
instrument = hl[0].header.get('INSTRUME') # strip or not?
detector = hl[0].header.get('DETECTOR') # strip or not?
print(instrument, detector)
assert instrument in INSTRUMENT_LIST
if instrument == 'MSC' and 6 <= int(detector[3:5]) <= 25:
# multi-band imaging
data = CsstMscImgData(hl[0], hl[1], instrument=instrument, detector=detector)
return data
except Exception as e:
print(e)
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from enum import Enum from enum import Enum
class CsstProcStatus(Enum): class CsstProcStatus(Enum):
empty = -1 empty = -1
normal = 0 normal = 0
ioerror = 1 ioerror = 1
runtimeerror = 2 runtimeerror = 2
# self['empty'].info = 'Not run yet.' # self['empty'].info = 'Not run yet.'
# self['normal'].info = 'This is a normal run.' # self['normal'].info = 'This is a normal run.'
# self['ioerror'].info = 'This run is exceptionally stopped due to IO error.' # self['ioerror'].info = 'This run is exceptionally stopped due to IO error.'
...@@ -17,11 +18,11 @@ class CsstProcessor(metaclass=ABCMeta): ...@@ -17,11 +18,11 @@ class CsstProcessor(metaclass=ABCMeta):
@abstractmethod @abstractmethod
def prepare(self, **kwargs): def prepare(self, **kwargs):
pass pass
@abstractmethod @abstractmethod
def run(self, data): def run(self, data):
return self._status return self._status
@abstractmethod @abstractmethod
def cleanup(self): def cleanup(self):
pass pass
\ No newline at end of file
__version__ = "0.0.1"
\ No newline at end of file
...@@ -3,11 +3,12 @@ from abc import ABCMeta, abstractmethod ...@@ -3,11 +3,12 @@ from abc import ABCMeta, abstractmethod
from csst.common.data import CsstData from csst.common.data import CsstData
from enum import Enum from enum import Enum
import numpy as np import numpy as np
from csst.common.processors import CsstProcStatus, CsstProcessor from ..common.processors import CsstProcStatus, CsstProcessor
class CsstMscInstrumentProc(CsstProcessor): class CsstMscInstrumentProc(CsstProcessor):
_status = CsstProcStatus.empty _status = CsstProcStatus.empty
_switches = {'crosstalk':False,'nonlinear':False,'deepcr':False,'cti':False, 'brighterfatter':False} _switches = {'crosstalk': False, 'nonlinear': False, 'deepcr': False, 'cti': False, 'brighterfatter': False}
def __init__(self): def __init__(self):
pass pass
...@@ -34,16 +35,15 @@ class CsstMscInstrumentProc(CsstProcessor): ...@@ -34,16 +35,15 @@ class CsstMscInstrumentProc(CsstProcessor):
if self._switches['brighterfatter']: if self._switches['brighterfatter']:
print('Brighter-Fatter effect correction') print('Brighter-Fatter effect correction')
def prepare(self, **kwargs): def prepare(self, **kwargs):
for name in kwargs: for name in kwargs:
self._switches[name] = kwargs[name] self._switches[name] = kwargs[name]
def run(self, data): def run(self, data):
if type(data).__name__=='CsstMscImgData' or type(data).__name__=='CsstMscSlsData': if type(data).__name__ == 'CsstMscImgData' or type(data).__name__ == 'CsstMscSlsData':
self.__l1img = data.get_l0data(copy = True) self.__l1img = data.get_l0data(copy=True)
self.__weightimg = np.random.uniform(0,1,(9216,9232)) self.__weightimg = np.random.uniform(0, 1, (9216, 9232))
self.__flagimg = np.random.uniform(0,1,(9216,9232)) self.__flagimg = np.random.uniform(0, 1, (9216, 9232))
flat = data.get_flat() flat = data.get_flat()
bias = data.get_bias() bias = data.get_bias()
...@@ -57,12 +57,12 @@ class CsstMscInstrumentProc(CsstProcessor): ...@@ -57,12 +57,12 @@ class CsstMscInstrumentProc(CsstProcessor):
print('fake to finish the run and save the results back to CsstData') print('fake to finish the run and save the results back to CsstData')
data.set_l1data('sci',self.__l1img) data.set_l1data('sci', self.__l1img)
data.set_l1data('weight',self.__weightimg) data.set_l1data('weight', self.__weightimg)
data.set_l1data('flag',self.__flagimg) data.set_l1data('flag', self.__flagimg)
print('Update keywords') print('Update keywords')
data.set_l1keyword('SOMEKEY','some value','Test if I can append the header') data.set_l1keyword('SOMEKEY', 'some value', 'Test if I can append the header')
self._status = CsstProcStatus.normal self._status = CsstProcStatus.normal
else: else:
...@@ -71,4 +71,3 @@ class CsstMscInstrumentProc(CsstProcessor): ...@@ -71,4 +71,3 @@ class CsstMscInstrumentProc(CsstProcessor):
def cleanup(self): def cleanup(self):
pass pass
...@@ -2,17 +2,19 @@ from collections import OrderedDict ...@@ -2,17 +2,19 @@ from collections import OrderedDict
import astropy.io.fits as fits import astropy.io.fits as fits
from astropy.io.fits import HDUList, PrimaryHDU, ImageHDU from astropy.io.fits import HDUList, PrimaryHDU, ImageHDU
from astropy.io.fits.header import Header from astropy.io.fits.header import Header
from csst.common.data import CsstData from ..common.data import CsstData
class CsstMscData(CsstData): class CsstMscData(CsstData):
_l1img_types = {'sci':True,'weight':True,'flag':True} _l1img_types = {'sci': True, 'weight': True, 'flag': True}
def __init__(self, primaryHDU, imgHDU):
def __init__(self, primaryHDU, imgHDU, **kwargs):
print('create CsstMscData') print('create CsstMscData')
super().__init__(primaryHDU, imgHDU) super(CsstData, self).__init__(primaryHDU, imgHDU, **kwargs)
self._l1hdr_global = primaryHDU.header.copy() self._l1hdr_global = primaryHDU.header.copy()
# self._l1hdr_global['SIMPLE'] = 'T' #/ conforms to FITS standard # self._l1hdr_global['SIMPLE'] = 'T' #/ conforms to FITS standard
# self._l1hdr_global['NAXIS'] = 0 # self._l1hdr_global['NAXIS'] = 0
self._l1data['sci'] =ImageHDU() self._l1data['sci'] = ImageHDU()
self._l1data['weight'] = ImageHDU() self._l1data['weight'] = ImageHDU()
self._l1data['flag'] = ImageHDU() self._l1data['flag'] = ImageHDU()
...@@ -42,14 +44,10 @@ class CsstMscData(CsstData): ...@@ -42,14 +44,10 @@ class CsstMscData(CsstData):
def init_l0data(self): def init_l0data(self):
pass pass
# hdr_global = Header(self._primary_hdu.header, copy = True)
# hdr_l1 = Header(self._l0data.header, copy = True)
# hdu_pri = PrimaryHDU(hdr = hdr_global)
# hdu_img = self._l0data.copy()
def set_l1keyword(self, key, value, comment=''): def set_l1keyword(self, key, value, comment=''):
print('check out whether '+key+" is a valid key and "+value+" is valid value") print('check out whether ' + key + " is a valid key and " + value + " is valid value")
self._l1hdr_global.set(key,value,comment) self._l1hdr_global.set(key, value, comment)
def set_l1data(self, imgtype, img): def set_l1data(self, imgtype, img):
try: try:
...@@ -60,15 +58,25 @@ class CsstMscData(CsstData): ...@@ -60,15 +58,25 @@ class CsstMscData(CsstData):
print('save image data to l1data') print('save image data to l1data')
def save_l1data(self, imgtype, filename): def save_l1data(self, imgtype, filename):
print('check '+imgtype+' is validate') print('check ' + imgtype + ' is validate')
try: try:
if self._l1img_types[imgtype]: if self._l1img_types[imgtype]:
super().save_l1data(imgtype, filename) super().save_l1data(imgtype, filename)
except Exception as e: except Exception as e:
print(e) print(e)
class CsstMscImgData(CsstMscData): class CsstMscImgData(CsstMscData):
def __init__(self, primaryHDU, imgHDU): def __init__(self, primaryHDU, imgHDU, **kwargs):
print('create CsstMscImgData') print('create CsstMscImgData')
super().__init__(primaryHDU, imgHDU) super(CsstMscData, self).__init__(primaryHDU, imgHDU, **kwargs)
def __repr__(self):
return "<CsstMscImgData: {} {}>".format(self.instrument, self.detector)
# def test():
# fp = MSC_MS_210527171000_100000279_16_raw.fits
#
#
# if __name__=="__main__":
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment