Skip to content

Commit 833fc3e

Browse files
committed
Fix for issue astropy#5874 for fitscheck script not deleting CHECKSUM and DATASUM kwrds
1 parent 6c61675 commit 833fc3e

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎astropy/io/fits/hdu/base.py‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def block_iter(nbytes):
457457
hdu._data_size = size + _pad_length(size)
458458

459459
# Checksums are not checked on invalid HDU types
460-
if checksum and checksum != 'remove' and isinstance(hdu, _ValidHDU):
460+
if checksum and isinstance(hdu, _ValidHDU):
461461
hdu._verify_checksum_datasum(checksum)
462462

463463
return hdu
@@ -1338,22 +1338,23 @@ def _verify_checksum_datasum(self, blocking):
13381338
if 'CHECKSUM' in self._header:
13391339
self._checksum = self._header['CHECKSUM']
13401340
self._checksum_comment = self._header.comments['CHECKSUM']
1341-
if not self.verify_checksum(blocking):
1342-
warnings.warn(
1343-
'Checksum verification failed for HDU {0}.\n'.format(
1344-
(self.name, self.ver)), AstropyUserWarning)
1341+
if blocking != 'remove':
1342+
if not self.verify_checksum(blocking):
1343+
warnings.warn(
1344+
'Checksum verification failed for HDU {0}.\n'.format(
1345+
(self.name, self.ver)), AstropyUserWarning)
13451346
else:
13461347
self._checksum = None
13471348
self._checksum_comment = None
13481349

13491350
if 'DATASUM' in self._header:
13501351
self._datasum = self._header['DATASUM']
13511352
self._datasum_comment = self._header.comments['DATASUM']
1352-
1353-
if not self.verify_datasum(blocking):
1354-
warnings.warn(
1355-
'Datasum verification failed for HDU {0}.\n'.format(
1356-
(self.name, self.ver)), AstropyUserWarning)
1353+
if blocking != 'remove':
1354+
if not self.verify_datasum(blocking):
1355+
warnings.warn(
1356+
'Datasum verification failed for HDU {0}.\n'.format(
1357+
(self.name, self.ver)), AstropyUserWarning)
13571358
else:
13581359
self._checksum = None
13591360
self._checksum_comment = None

‎astropy/io/fits/scripts/fitscheck.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ def handle_options(args):
109109
OPTIONS, fits_files = parser.parse_args(args)
110110

111111
if OPTIONS.checksum_kind == 'none':
112-
OPTIONS.checksum_kind = False
112+
# Handle use case 7
113+
if not OPTIONS.compliance and OPTIONS.write_file:
114+
OPTIONS.checksum_kind = 'remove'
115+
else:
116+
# Handle use case 5
117+
OPTIONS.checksum_kind = False
113118

114119
return fits_files
115120

@@ -181,6 +186,10 @@ def update(filename):
181186
except fits.VerifyError:
182187
pass # unfixable errors already noted during verification phase
183188
finally:
189+
if OPTIONS.checksum_kind == 'remove':
190+
log.info('OK {!r} .. CHECKSUM and DATASUM keywords removed from all HDUs'.format(filename))
191+
else:
192+
log.info('OK {!r} .. All changes written'.format(filename))
184193
hdulist.close()
185194

186195

@@ -197,6 +206,8 @@ def process_file(filename):
197206
else:
198207
compliance_errors = 0
199208
if OPTIONS.write_file and checksum_errors == 0 or OPTIONS.force:
209+
if OPTIONS.force:
210+
log.warn('WARNING {!r}.. Ignoring errors and updating CHECKSUM and DATASUM forcibly'.format(filename))
200211
update(filename)
201212
return checksum_errors + compliance_errors
202213
except Exception as e:

0 commit comments

Comments
 (0)