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_common
Commits
ceabf442
Commit
ceabf442
authored
Dec 25, 2023
by
BO ZHANG
🏀
Browse files
fixed bug in append_header
parent
0dd6f9d4
Pipeline
#2753
passed with stage
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
csst_common/io.py
View file @
ceabf442
...
@@ -45,7 +45,7 @@ def append_header(
...
@@ -45,7 +45,7 @@ def append_header(
"""
"""
Append h2 to h1.
Append h2 to h1.
Append fits headers, taken into a
a
count duplicated keywords.
Append fits headers, taken into a
c
count duplicated keywords.
Parameters
Parameters
----------
----------
...
@@ -70,20 +70,23 @@ def append_header(
...
@@ -70,20 +70,23 @@ def append_header(
# copy data
# copy data
original_h
=
deepcopy
(
h1
)
original_h
=
deepcopy
(
h1
)
extended_h
=
deepcopy
(
h2
)
extended_h
=
deepcopy
(
h2
)
original_keys
=
original_h
.
keys
()
original_keys
=
tuple
(
original_h
.
keys
())
ignored_keys
=
[
""
,
"COMMENT"
,
"HISTORY"
]
extended_keys
=
tuple
(
extended_h
.
keys
())
ignored_keys
=
(
""
,
"COMMENT"
,
"HISTORY"
)
assert
duplicates
in
(
"delete"
,
"update"
)
assert
duplicates
in
(
"delete"
,
"update"
)
if
duplicates
==
"update"
:
if
duplicates
==
"update"
:
for
card
in
extended_h
.
cards
:
for
k
in
extended_keys
:
if
card
.
keyword
not
in
ignored_keys
and
card
.
keyword
in
original_keys
:
# print(card.keyword)
print
(
f
"Update existing key *
{
card
.
keyword
}
in original fits.Header*"
)
if
k
not
in
ignored_keys
and
k
in
original_keys
:
original_h
.
set
(
card
.
keyword
,
card
.
value
,
card
.
comment
)
print
(
f
"Update existing key *
{
k
}
in original fits.Header*"
)
extended_h
.
remove
(
card
.
keyword
)
original_h
.
set
(
k
,
extended_h
[
k
],
extended_h
.
comments
[
k
])
extended_h
.
remove
(
k
)
elif
duplicates
==
"delete"
:
elif
duplicates
==
"delete"
:
for
card
in
extended_h
.
cards
:
for
k
in
extended_keys
:
if
card
.
keyword
not
in
ignored_keys
and
card
.
keyword
in
original_keys
:
# print(card.keyword)
print
(
f
"Delete existing key *
{
card
.
keyword
}
in original fits.Header*"
)
if
k
not
in
ignored_keys
and
k
in
original_keys
:
original_h
.
remove
(
card
.
keyword
)
print
(
f
"Delete existing key *
{
k
}
in original fits.Header*"
)
original_h
.
remove
(
k
)
original_h
.
extend
(
extended_h
,
bottom
=
True
)
original_h
.
extend
(
extended_h
,
bottom
=
True
)
return
original_h
return
original_h
...
...
tests/test_fits_header_ops.py
View file @
ceabf442
...
@@ -21,34 +21,44 @@ class TestFitsHeaderOps(unittest.TestCase):
...
@@ -21,34 +21,44 @@ class TestFitsHeaderOps(unittest.TestCase):
h2
=
fits
.
Header
()
h2
=
fits
.
Header
()
h1
.
set
(
"A"
,
1
,
"comment"
)
h1
.
set
(
"A"
,
1
,
"comment"
)
h1
.
set
(
"B"
,
2
,
"comment"
)
h1
.
set
(
"B"
,
1
,
"comment"
)
h1
.
set
(
"C"
,
1
,
"comment"
)
h1
.
add_comment
(
"="
*
72
,
before
=
"A"
)
h1
.
add_comment
(
"="
*
72
,
before
=
"A"
)
h1
.
add_comment
(
"one"
,
before
=
"A"
)
h1
.
add_comment
(
"one"
,
before
=
"A"
)
h1
.
add_comment
(
"="
*
72
,
before
=
"A"
)
h1
.
add_comment
(
"="
*
72
,
before
=
"A"
)
h2
.
set
(
"B"
,
3
,
"comment"
)
h2
.
set
(
"B"
,
2
,
"comment"
)
h2
.
set
(
"C"
,
4
,
"comment"
)
h2
.
set
(
"C"
,
2
,
"comment"
)
h2
.
set
(
"D"
,
2
,
"comment"
)
h2
.
add_comment
(
"="
*
72
,
before
=
"B"
)
h2
.
add_comment
(
"="
*
72
,
before
=
"B"
)
h2
.
add_comment
(
"another"
,
before
=
"B"
)
h2
.
add_comment
(
"another"
,
before
=
"B"
)
h2
.
add_comment
(
"="
*
72
,
before
=
"B"
)
h2
.
add_comment
(
"="
*
72
,
before
=
"B"
)
h_update
=
append_header
(
h1
,
h2
,
duplicates
=
"update"
)
self
.
assertEqual
(
self
.
assertEqual
(
tuple
(
append_header
(
h1
,
h2
,
duplicates
=
"
update
"
)
.
keys
()),
tuple
(
h_
update
.
keys
()),
(
(
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"A"
,
"A"
,
"B"
,
"B"
,
"C"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"
C
"
,
"
D
"
,
),
),
"update mode failed"
,
"update mode failed"
,
)
)
self
.
assertEqual
(
h_update
[
"A"
],
1
)
self
.
assertEqual
(
h_update
[
"B"
],
2
)
self
.
assertEqual
(
h_update
[
"C"
],
2
)
self
.
assertEqual
(
h_update
[
"D"
],
2
)
h_delete
=
append_header
(
h1
,
h2
,
duplicates
=
"delete"
)
self
.
assertEqual
(
self
.
assertEqual
(
tuple
(
append_header
(
h1
,
h2
,
duplicates
=
"
delete
"
)
.
keys
()),
tuple
(
h_
delete
.
keys
()),
(
(
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
"COMMENT"
,
...
@@ -59,9 +69,14 @@ class TestFitsHeaderOps(unittest.TestCase):
...
@@ -59,9 +69,14 @@ class TestFitsHeaderOps(unittest.TestCase):
"COMMENT"
,
"COMMENT"
,
"B"
,
"B"
,
"C"
,
"C"
,
"D"
,
),
),
"delete mode failed"
,
"delete mode failed"
,
)
)
self
.
assertEqual
(
h_delete
[
"A"
],
1
)
self
.
assertEqual
(
h_delete
[
"B"
],
2
)
self
.
assertEqual
(
h_delete
[
"C"
],
2
)
self
.
assertEqual
(
h_delete
[
"D"
],
2
)
def
test_reformat_header
(
self
):
def
test_reformat_header
(
self
):
h
=
fits
.
Header
()
h
=
fits
.
Header
()
...
...
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