Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bo Zhang
csst
Commits
33f18c07
Commit
33f18c07
authored
Dec 17, 2021
by
BO ZHANG
🏀
Browse files
first commit
parent
497f4a5b
Changes
7
Show whitespace changes
Inline
Side-by-side
csst/common/__init__.py
View file @
33f18c07
__version__
=
"0.0.1"
\ No newline at end of file
csst/common/data.py
View file @
33f18c07
...
...
@@ -3,46 +3,16 @@ from collections import OrderedDict
import
astropy.io.fits
as
fits
from
astropy.io.fits
import
HDUList
,
PrimaryHDU
from
csst.msc.mscdata
import
CsstMscImgData
from
.CsstException
import
CsstException
class
CsstDataFactory
:
"""
This class is designed to create CsstData and its inherited classes according to different kinds of input data format.
"""
INSTRUMENT_LIST
=
[
"MSC"
,
]
def
__init__
(
self
):
pass
def
createData
(
self
,
fitsfilename
)
:
class
CsstData
:
"""
Parameters
----------
fitsfilename:
the file name of fits files
Returns
-------
general CSST data class
"""
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
=
[]
_l0data
=
[]
# HDUList
_l1hdr_global
=
[]
...
...
@@ -50,28 +20,30 @@ class CsstData:
_l2data
=
OrderedDict
()
#
_auxdata
=
OrderedDict
()
def
__init__
(
self
,
primaryHDU
,
imgHDU
):
def
__init__
(
self
,
primaryHDU
,
imgHDU
,
instrument
=
None
,
detector
=
None
):
print
(
'create CsstData'
)
self
.
_primary_hdu
=
primaryHDU
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
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
'''
"""
if
copy
:
return
self
.
_l0data
.
data
.
copy
()
else
:
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
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
'''
"""
if
ext
==
'pri'
:
try
:
value
=
self
.
_primary_hdu
.
header
.
get
(
key
)
...
...
@@ -84,7 +56,6 @@ class CsstData:
print
(
e
)
else
:
raise
CsstException
return
value
def
set_l1keyword
(
self
,
key
,
value
):
print
(
'check out whether '
+
key
+
" is a valid key and "
+
value
+
" is valid value"
)
...
...
@@ -94,13 +65,14 @@ class CsstData:
def
get_auxdata
(
self
,
name
):
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
):
'''
"""
asve level 1 image and auxilary data to data file
imgtype
'''
"""
print
(
"save L1 image to a fits file with name "
+
filename
)
try
:
self
.
_l1hdr_global
.
set
(
'TYPE'
,
imgtype
,
'Type of Level 1 data'
)
...
...
csst/common/factory.py
0 → 100644
View file @
33f18c07
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
)
csst/common/processors.py
View file @
33f18c07
from
abc
import
ABCMeta
,
abstractmethod
from
enum
import
Enum
class
CsstProcStatus
(
Enum
):
empty
=
-
1
normal
=
0
ioerror
=
1
runtimeerror
=
2
# self['empty'].info = 'Not run yet.'
# self['normal'].info = 'This is a normal run.'
# self['ioerror'].info = 'This run is exceptionally stopped due to IO error.'
...
...
@@ -17,11 +18,11 @@ class CsstProcessor(metaclass=ABCMeta):
@
abstractmethod
def
prepare
(
self
,
**
kwargs
):
pass
@
abstractmethod
def
run
(
self
,
data
):
return
self
.
_status
@
abstractmethod
def
cleanup
(
self
):
pass
\ No newline at end of file
csst/msc/__init__.py
View file @
33f18c07
__version__
=
"0.0.1"
\ No newline at end of file
csst/msc/instrument.py
View file @
33f18c07
...
...
@@ -3,11 +3,12 @@ from abc import ABCMeta, abstractmethod
from
csst.common.data
import
CsstData
from
enum
import
Enum
import
numpy
as
np
from
csst.common.processors
import
CsstProcStatus
,
CsstProcessor
from
..common.processors
import
CsstProcStatus
,
CsstProcessor
class
CsstMscInstrumentProc
(
CsstProcessor
):
_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
):
pass
...
...
@@ -34,16 +35,15 @@ class CsstMscInstrumentProc(CsstProcessor):
if
self
.
_switches
[
'brighterfatter'
]:
print
(
'Brighter-Fatter effect correction'
)
def
prepare
(
self
,
**
kwargs
):
for
name
in
kwargs
:
self
.
_switches
[
name
]
=
kwargs
[
name
]
def
run
(
self
,
data
):
if
type
(
data
).
__name__
==
'CsstMscImgData'
or
type
(
data
).
__name__
==
'CsstMscSlsData'
:
self
.
__l1img
=
data
.
get_l0data
(
copy
=
True
)
self
.
__weightimg
=
np
.
random
.
uniform
(
0
,
1
,(
9216
,
9232
))
self
.
__flagimg
=
np
.
random
.
uniform
(
0
,
1
,(
9216
,
9232
))
if
type
(
data
).
__name__
==
'CsstMscImgData'
or
type
(
data
).
__name__
==
'CsstMscSlsData'
:
self
.
__l1img
=
data
.
get_l0data
(
copy
=
True
)
self
.
__weightimg
=
np
.
random
.
uniform
(
0
,
1
,
(
9216
,
9232
))
self
.
__flagimg
=
np
.
random
.
uniform
(
0
,
1
,
(
9216
,
9232
))
flat
=
data
.
get_flat
()
bias
=
data
.
get_bias
()
...
...
@@ -57,12 +57,12 @@ class CsstMscInstrumentProc(CsstProcessor):
print
(
'fake to finish the run and save the results back to CsstData'
)
data
.
set_l1data
(
'sci'
,
self
.
__l1img
)
data
.
set_l1data
(
'weight'
,
self
.
__weightimg
)
data
.
set_l1data
(
'flag'
,
self
.
__flagimg
)
data
.
set_l1data
(
'sci'
,
self
.
__l1img
)
data
.
set_l1data
(
'weight'
,
self
.
__weightimg
)
data
.
set_l1data
(
'flag'
,
self
.
__flagimg
)
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
else
:
...
...
@@ -71,4 +71,3 @@ class CsstMscInstrumentProc(CsstProcessor):
def
cleanup
(
self
):
pass
csst/msc/mscdata.py
View file @
33f18c07
...
...
@@ -2,17 +2,19 @@ from collections import OrderedDict
import
astropy.io.fits
as
fits
from
astropy.io.fits
import
HDUList
,
PrimaryHDU
,
ImageHDU
from
astropy.io.fits.header
import
Header
from
csst.common.data
import
CsstData
from
..common.data
import
CsstData
class
CsstMscData
(
CsstData
):
_l1img_types
=
{
'sci'
:
True
,
'weight'
:
True
,
'flag'
:
True
}
def
__init__
(
self
,
primaryHDU
,
imgHDU
):
_l1img_types
=
{
'sci'
:
True
,
'weight'
:
True
,
'flag'
:
True
}
def
__init__
(
self
,
primaryHDU
,
imgHDU
,
**
kwargs
):
print
(
'create CsstMscData'
)
super
().
__init__
(
primaryHDU
,
imgHDU
)
super
(
CsstData
,
self
).
__init__
(
primaryHDU
,
imgHDU
,
**
kwargs
)
self
.
_l1hdr_global
=
primaryHDU
.
header
.
copy
()
# self._l1hdr_global['SIMPLE'] = 'T' #/ conforms to FITS standard
# self._l1hdr_global['NAXIS'] = 0
self
.
_l1data
[
'sci'
]
=
ImageHDU
()
# self._l1hdr_global['SIMPLE'] = 'T' #/ conforms to FITS standard
# self._l1hdr_global['NAXIS'] = 0
self
.
_l1data
[
'sci'
]
=
ImageHDU
()
self
.
_l1data
[
'weight'
]
=
ImageHDU
()
self
.
_l1data
[
'flag'
]
=
ImageHDU
()
...
...
@@ -42,14 +44,10 @@ class CsstMscData(CsstData):
def
init_l0data
(
self
):
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
=
''
):
print
(
'check out whether '
+
key
+
" is a valid key and "
+
value
+
" is valid value"
)
self
.
_l1hdr_global
.
set
(
key
,
value
,
comment
)
print
(
'check out whether '
+
key
+
" is a valid key and "
+
value
+
" is valid value"
)
self
.
_l1hdr_global
.
set
(
key
,
value
,
comment
)
def
set_l1data
(
self
,
imgtype
,
img
):
try
:
...
...
@@ -60,15 +58,25 @@ class CsstMscData(CsstData):
print
(
'save image data to l1data'
)
def
save_l1data
(
self
,
imgtype
,
filename
):
print
(
'check '
+
imgtype
+
' is validate'
)
print
(
'check '
+
imgtype
+
' is validate'
)
try
:
if
self
.
_l1img_types
[
imgtype
]:
super
().
save_l1data
(
imgtype
,
filename
)
except
Exception
as
e
:
print
(
e
)
class
CsstMscImgData
(
CsstMscData
):
def
__init__
(
self
,
primaryHDU
,
imgHDU
):
def
__init__
(
self
,
primaryHDU
,
imgHDU
,
**
kwargs
):
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__":
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment