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
csst-pipeline
csst_proto
Commits
5cbab9d2
Commit
5cbab9d2
authored
Oct 10, 2022
by
BO ZHANG
🏀
Browse files
added FileRecorder to documentation
parent
769ad99b
Pipeline
#214
passed with stages
in 14 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
doc/source/csst_common/csst_common.rst
View file @
5cbab9d2
...
...
@@ -97,6 +97,32 @@ raw 0 ``dm.l0_log(detector=detector)``
========================== ===== ========================================================== ==================
``csst_common.file_recorder.FileRecorder``
------------------------------------------
Get an empty ``FileRecorder``.
This is initially a ``list``-like object.
Use ``FileRecorder.add_record()`` to add file records.
.. code-block::
:linenos:
>>> from csst_common.file_recorder import FileRecorder
>>> fr = FileRecorder()
>>> for i in range(3):
>>> fr.add_record(filepath="test{:03d}.txt".format(i),
>>> db=True,
>>> comment="Test file {:d}".format(i))
>>> fr.pprint_all()
<FileRecorder length=3>
filepath db comment existence
----------- ---- ----------- ---------
test000.txt True Test file 0 False
test001.txt True Test file 1 False
test002.txt True Test file 2 False
``csst_common.logger.get_logger()``
-----------------------------------
...
...
@@ -147,7 +173,7 @@ Source code
.. literalinclude:: example_interface.py
:caption: ``example_interface.py``
:emphasize-lines: 36-40,8
1-82
,8
5
-8
6,89
-9
0
,94-9
7,143-144,14
7,1
5
0-1
5
1,1
5
4-1
5
5
:emphasize-lines:
7-10,
36-40,8
4
,8
6
-8
7,90
-9
1
,94-9
5,99,102,148,152-153,156-15
7,1
6
0-1
6
1,1
6
4-1
6
5
:linenos:
:language: python
...
...
doc/source/csst_common/example_interface.py
View file @
5cbab9d2
...
...
@@ -5,13 +5,14 @@ from typing import Union
import
numpy
as
np
from
astropy.io
import
fits
from
csst_common.data_manager
import
CsstMsDataManager
from
csst_common.file_recorder
import
FileRecorder
from
csst_common.logger
import
get_logger
from
csst_common.status
import
CsstStatus
def
read_image
(
file
name
_input
:
str
)
->
np
.
ndarray
:
def
read_image
(
file
path
_input
:
str
)
->
np
.
ndarray
:
""" Read image. """
return
fits
.
getdata
(
file
name
_input
)
return
fits
.
getdata
(
file
path
_input
)
def
process_data
(
data
:
np
.
ndarray
)
->
np
.
ndarray
:
...
...
@@ -28,16 +29,15 @@ def check_results(dm: CsstMsDataManager, logger: logging.Logger) -> bool:
if
all
(
existence
):
return
True
else
:
logger
.
warning
(
"Not all processed files are generated!"
)
return
False
# process a single image
def
process_single_image
(
file
name
_input
:
str
,
file
name
_output
:
str
,
file
path
_input
:
str
,
file
path
_output
:
str
,
logger
:
Union
[
None
,
logging
.
Logger
]
=
None
)
->
CsstStatus
:
)
->
tuple
[
CsstStatus
,
FileRecorder
]
:
"""
Flip a single image.
...
...
@@ -45,23 +45,23 @@ def process_single_image(
Parameters
----------
file
name
_input : str
The input file
name
.
file
name
_output : str
The output file
name
.
file
path
_input : str
The input file
path
.
file
path
_output : str
The output file
path
.
logger : logging.Logger
The logger.
Returns
-------
CsstStatus
tuple[
CsstStatus
, FileRecorder]
The final status.
Examples
--------
>>> process_single_image(
>>> file
name
_input="input_image.fits",
>>> file
name
_output="output_image.fits",
>>> file
path
_input="input_image.fits",
>>> file
path
_output="output_image.fits",
>>> logger=None
>>> )
"""
...
...
@@ -69,32 +69,37 @@ def process_single_image(
if
logger
is
None
:
logger
=
get_logger
()
# get an empty file recorder
fr
=
FileRecorder
()
# process data
try
:
# this will NOT be written into the log file
logger
.
debug
(
"Reading the image {}"
.
format
(
file
name
_input
))
logger
.
debug
(
"Reading the image {}"
.
format
(
file
path
_input
))
# start processing
data
=
read_image
(
file
name
_input
)
data
=
read_image
(
file
path
_input
)
data_processed
=
process_data
(
data
)
np
.
save
(
filename_output
,
data_processed
)
np
.
save
(
filepath_output
,
data_processed
)
# record file!
fr
.
add_record
(
filepath
=
filepath_output
,
db
=
True
,
comment
=
"the processed image"
)
# this will be written into the log file
logger
.
info
(
"Processed image saved to {}"
.
format
(
file
name
_output
))
return
CsstStatus
.
PERFECT
logger
.
info
(
"Processed image saved to {}"
.
format
(
file
path
_output
))
return
CsstStatus
.
PERFECT
,
fr
except
DeprecationWarning
:
# this will be written into the log file
logger
.
warning
(
"Suffered DeprecationWarning!"
)
return
CsstStatus
.
WARNING
return
CsstStatus
.
WARNING
,
fr
except
IOError
:
# this will be written into the log file
logger
.
error
(
"Suffered IOError!"
)
return
CsstStatus
.
ERROR
return
CsstStatus
.
ERROR
,
fr
# process an exposure (MBI or SLS)
def
process_multiple_images
(
dm
:
CsstMsDataManager
,
logger
:
Union
[
None
,
logging
.
Logger
]
=
None
)
->
CsstStatus
:
)
->
tuple
[
CsstStatus
,
FileRecorder
]
:
"""
Flip all images.
...
...
@@ -109,7 +114,7 @@ def process_multiple_images(
Returns
-------
CsstStatus
tuple[
CsstStatus
, FileRecorder]
The final status.
Examples
...
...
@@ -124,6 +129,9 @@ def process_multiple_images(
if
logger
is
None
:
logger
=
get_logger
()
# get an empty file recorder
fr
=
FileRecorder
()
# process data
try
:
# dm.target_detectors is a list of detector number that should be processed
...
...
@@ -131,25 +139,27 @@ def process_multiple_images(
for
detector
in
dm
.
target_detectors
:
# this will NOT be written into the log file
logger
.
debug
(
"Processing for detector {}"
.
format
(
detector
))
data
=
read_image
(
dm
.
l0_detector
(
detector
=
detector
))
filepath_input
=
dm
.
l0_detector
(
detector
=
detector
)
filepath_output
=
dm
.
l1_detector
(
detector
=
detector
,
post
=
"L1_processed.fits"
)
data
=
read_image
(
filepath_input
)
data_processed
=
process_data
(
data
)
np
.
save
(
dm
.
l1_detector
(
detector
=
detector
,
post
=
"L1_processed.fits"
),
data_processed
)
np
.
save
(
filepath_output
,
data_processed
)
# record file!
fr
.
add_record
(
filepath
=
filepath_output
,
db
=
True
,
comment
=
"processed file for Detector {}"
.
format
(
detector
))
# check results
if
check_results
(
dm
=
dm
,
logger
=
logger
):
# this will be written into the log file
logger
.
info
(
"
P
rocessed
all images
"
)
return
CsstStatus
.
PERFECT
logger
.
info
(
"
All p
rocessed
files are generated!
"
)
return
CsstStatus
.
PERFECT
,
fr
else
:
# not all images are properly processed
return
CsstStatus
.
ERROR
logger
.
warning
(
"Not all processed files are generated!"
)
return
CsstStatus
.
ERROR
,
fr
except
DeprecationWarning
:
# this will be written into the log file
logger
.
warning
(
"Suffered DeprecationWarning!"
)
return
CsstStatus
.
WARNING
return
CsstStatus
.
WARNING
,
fr
except
IOError
:
# this will be written into the log file
logger
.
error
(
"Suffered IOError!"
)
return
CsstStatus
.
ERROR
return
CsstStatus
.
ERROR
,
fr
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