# FITS write-back: valid checksums do not establish value preservation

Research continuation of PR #1789, 23 October 2026. Tested software:
**Astropy 8.0.2, NumPy 2.3.4, Python 3.12.3**. Synthetic uncompressed integer
images only. This is a version-scoped software experiment, not a claim about
all releases, astronomical incident prevalence, physical media or a new vulnerability.

## Finding

Reading an array can be enough to change the serialized values when an image is
subsequently saved. A valid output CHECKSUM does not show that the values still
match the input. In our unit-scaling, BLANK-bearing fixtures, the distinction
was not limited to floating-point rounding: some output headers continued to
declare an integer BITPIX while the payload contained floating-point bytes.

The original investigation measured the array returned to the caller. This
continuation measures the **serialized output reopened in a fresh reader**.
The original twenty inputs and their hashes are unchanged. Ten fixture families
are each represented as a primary image and as an IMAGE extension. Eight
workflows make 160 configurations per capture. We retained two memory-stream
captures and a further 160 ordinary-temporary-file controls on the same host.
All semantic outcomes and pixel-payload hashes matched. Output checksum
comments can contain wall time, so whole-file output hashes are not required
to match between captures. Exact bytes from every capture are retained.

No workflow assigns a pixel. The header-only control adds OBSERVER. The
read-only control accesses the array but does not save it. Rewrites write a
new destination with checksums enabled.

## What the controlled matrix shows

Counts below are configurations, **not failure probabilities**. Null-mask and
numeric-error counts may overlap; do not add them as disjoint incidents.
Each workflow has a denominator of twenty.

| Workflow | Operation errors | Payload changes | Null-mask mismatch | Valid-value mismatch |
| --- | ---: | ---: | ---: | ---: |
| Read-only array access | 0 | 0 | 0 | 0 |
| Update, no array access | 0 | 0 | 0 | 0 |
| Update, header-only edit | 0 | 0 | 0 | 0 |
| Update after array access | 0 | 12 | 10 | 12 |
| Update after array access, scale_back=True | 4 | 4 | 2 | 6 |
| Update with unscaled array access | 0 | 0 | 0 | 0 |
| Rewrite after array access | 0 | 12 | 10 | 12 |
| Rewrite with unscaled array access | 0 | 0 | 0 | 0 |

Every inspected input and output HDU passed DATASUM and CHECKSUM. For the four
operations that raised OverflowError, "output" means retained bytes after the
failed attempt, **not a successfully completed save**. Those exceptions are
recorded and must not be accepted as success merely because the retained file
still checks out.

### Three distinct mechanisms

1. **Representation disagreement.** For the signed64_unused_blank fixture,
   the input holds 9007199254740992, 9007199254740993 and 9007199254740994.
   After update-mode array access, the output still declares BITPIX=64 but
   its serialized integers decode as 4845873199050653696,
   4845873199050653696 and 4845873199050653697. Both checksums pass. These
   are the floating-point representations interpreted using the retained
   integer declaration, not a telescope measurement or random bit corruption.

2. **Irrecoverable rounding.** Applying scale_back=True to that same fixture
   produces integer storage again, but the middle value is 9007199254740992.
   Reapplying a storage type cannot recover the distinction already lost in
   the intermediate binary64 value. The no-BLANK control with identical
   original pixel bytes preserves all three integers.

3. **Meaning changes without payload changes.** For unsigned16_zero_blank
   under scale_back=True, the payload hash is unchanged, but BZERO and BLANK
   disappear. The intended physical values 0, 1, missing, 32769 become
   -32768, -32767, 0, 1. A pixel-payload hash alone does not bind the metadata
   that determines the values.

The primary and IMAGE-extension observations agree for these write-back
controls. That does not erase the original experiment's separate
ignore_blank differences.

## Source synthesis and attribution

The [FITS 4.0 standard, sections 4.4.2.5 and 5](https://fits.gsfc.nasa.gov/standard40/fits_standard40aa-le.pdf)
ties interpretation to BITPIX and scaling metadata and defines BLANK in the
stored integer domain. That is why preserving bytes or a checksum is not the
same as preserving the represented measurement.

Astropy's [version-pinned image-data documentation](https://github.com/astropy/astropy/blob/v8.0.2/docs/io/fits/usage/image.rst)
already warns that update-mode access can save rescaled data and documents
do_not_scale_image_data and scale_back. Our cases test the less obvious
BLANK-only and exact-integer boundaries of that workflow.

In the [tested image implementation](https://github.com/astropy/astropy/blob/v8.0.2/astropy/io/fits/hdu/image.py),
BLANK can cause a conversion even with unit scaling. The header-scaling update
returns early for the original BZERO=0/BSCALE=1 case. The pre-write path and
observed bytes explain how a loaded floating array can be written with its
old integer declaration. The [base implementation](https://github.com/astropy/astropy/blob/v8.0.2/astropy/io/fits/hdu/base.py)
treats loaded data as potentially modified and can recompute checksums.
Both installed source files exactly matched the retrieved tagged sources.

The [registered checksum convention](https://fits.gsfc.nasa.gov/registry/checksum/checksum.pdf)
checks the current header/data unit. Recomputing it after a transformation
establishes internal checksum agreement for those bytes, not equivalence to
an earlier file or correctness of a scientific transformation.

Upstream [issue 7789](https://github.com/astropy/astropy/issues/7789) and
[PR 20517](https://github.com/astropy/astropy/pull/20517) predate this continuation.
The latter was merged on 15 October 2026. We rechecked its metadata and source,
but **did not execute a release containing that change**. The
_update_header_scale_info early-return condition is also present in the
inspected merge source; this source observation is not a runtime finding.
No claim is made that this is the first report of the behavior or that current
releases reproduce it.

## Acceptance rules for an archive migration

Keep an immutable input and write to a new destination during evaluation.
Before accepting the destination, compare HDU inventory, intended metadata,
missing-value locations and exact valid values against the source contract.
Verify the output checksums separately. For an identity-preserving workflow,
any value or mask difference, warning requiring investigation, or operation
exception must fail acceptance.

For header-only edits, avoid array conversion and test the documented raw-data
option on the precise release in use. Our raw-update and raw-rewrite controls
preserved values and masks in all twenty fixtures; this is not a guarantee for
arbitrary scaling, tables, compressed HDUs or other versions. A raw integer
array must still carry its original scaling metadata and a separate null mask
when interpreted.

A reader upgrade needs two gates: what the caller sees, and what its next
write serializes. A repaired mask does not, by itself, establish either exact
64-bit arithmetic or a reversible round trip.

## Evidence and reproduction

The adjacent writeback.py and disk_writeback_control.py accept no user input
FITS path. They generate or replay retained synthetic fixtures and refuse to
overwrite capture files. Temporary file controls delete only their own
temporary directories. The comparison decodes payloads using struct, then
applies Fraction arithmetic and the raw-domain mask; it does not use Astropy's
scaled array as the reference answer. FITS header parsing and checksum
verification still use Astropy, so this is **not an independent FITS library**.

Run from the parent research directory with the original pinned requirements:

```sh
python writeback.py --output writeback/reproduced.json
python disk_writeback_control.py --reference writeback/reproduced.json --output writeback/reproduced-disk.json
python -m unittest -v test_writeback test_observe
python -O -m unittest -v test_writeback test_observe
```

The captures include compressed base64 input/output bytes, SHA-256 digests,
headers, exact decimal/rational values, checksums, exceptions and environment
fingerprints. Source receipts are in sources-20261023.json. The test suite
covers exact comparisons, ordinary-file agreement, duplicate/missing cases,
tampered evidence, output protection and original-fixture identity.

## Integration status

Research-only continuation for the existing
[FITS analysis](https://hesela.com/analyses/fits-checksums-pixel-order-and-coverage/),
not a new article. Add a reviewed "Write-back is another acceptance boundary"
section after the read-state discussion; link this versioned evidence and the
prior null/precision analysis. Enrich the existing fits and fits-checksum notes
without changing their IDs or original definitions. Use a matched evidence
package on hesela.dev.

No site changes, full-site build, browser QA, merge or deployment are included.
A tested newer-release comparison remains an explicit next gate. The shared
host's capacity guard also blocks large installs/builds. Existing research
captures and other contributors' work are preserved.
