Repository navigation
0.3.0: streamed image writer, band by band (FitsImageWriter<T>) - #14
Merged
Merged
Conversation
- FitsImageWriter<T>.Create(path, header, checksums, mode, token) checks the header (BITPIX of T, BZERO of the unsigned convention, image with pixels), normalizes it as the primary HDU like Save, creates the temporary file next to the target and writes the header. WriteRows(plane, firstRow, rows, token) takes whole rows of one plane in file order and converts them through one 256 KB buffer with the encoder of the plane-by-plane write. Complete(token) pads the data, fills in the fixed-width CHECKSUM/DATASUM cards summed while streaming, flushes and publishes with the path-save code of FitsFile (atomic move; CreateNew with the O_EXCL claim on Unix). - Any failure, cancellation or Dispose before Complete deletes the temporary file and leaves the target untouched; out-of-order rows, wrong lengths, extra or missing rows are explicit errors and publish nothing. - Tests: byte-for-byte identity with the in-memory save for every pixel type, 2-D and 3-plane images, BSCALE/BZERO, BLANK, NaN, checksum policies and band heights 1/5/whole plane; astropy checks; cancellation (between and within bands, at Complete); CreateNew (existing, race, concurrent writers); error cases; allocations independent of the image height. - 0.3.0: version, changelog, package validation against 0.2.0, docs (guide section, example, generated API page; DocGen names generic type pages Type-1.md, and no longer entity-escapes names in code spans of the index), verify_package.py uses the writer. Closes #13 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sputier
added a commit
that referenced
this pull request
Nov 14, 2026
Streamed image writer (FitsImageWriter<T>): header up front, rows written band by band in file order, CHECKSUM/DATASUM accumulated while streaming, same publication guarantees as Save. Release 0.3.0. Closes #13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #13
Public contract (additive only)
BITPIX,NAXIS,NAXISnand any other card (BSCALE,BZERO,BLANK, WCS...). The writer copies it and normalizes it as the primary HDU, exactly asSavedoes for HDU 0.SIMPLEis optional, and anXTENSION = 'IMAGE'header is accepted, soimage.Headerof an existingImageHducan be passed as is.TfollowsImageHdu.Create<T>.BITPIXmust matchT.sbyte,ushort,uintandulongneed theBZEROof the convention (compared exactly) andBSCALE1. Values are stored values:BSCALE,BZEROandBLANKare cards written as given, not applied. The in-memory path does not apply them either.ArgumentException.Completewith rows missing:InvalidOperationException. Any exception, cancellation orDisposebeforeCompletedeletes the temporary file, publishes nothing and leaves an existing target untouched. Every later call then throwsInvalidOperationException.FitsFile.Save(path, ...): a temporary file in the target directory, flush to disk, then an atomic move (File.ReplaceorMove).CreateNewhas the early check plus the final move, which refuses an existing path (with theO_EXCLclaim + rename on Unix from Make CreateNew saves atomic on Unix #11), so a file that appears during the write is kept.CHECKSUMandDATASUMare summed while the bytes are written, including the fill. Placeholder cards are written up front by the sameSetChecksumscode, then rewritten in place before publication. The cards are fixed-width, so the header keeps its length (asserted).FileStreambuffer and the header. The band itself belongs to the caller and is not kept.Design choices
Create/WriteRows/Complete+DisposematchesFitsFile.Open/Save+using. "Disposed withoutComplete" is the natural way to say "abandon", and it composes withusingand exceptions.FitsImageWriter<T>. The value type is fixed once and checked againstBITPIXatCreate, rather than at every call.plane/firstRowonWriteRows. They are redundant with the internal cursor, which makes them an assertion: a caller bug (skipped, repeated or reordered band) becomes an explicit error instead of a silently shifted image.PlaneDataSource<T>.Encode(made static), i.e. the bytes of the plane-by-plane write.ImageHdu.Describe<T>.ImageHdu.Normalize.Hdu.PrepareChecksums/SetChecksums.ChecksumStream.FitsFile.SaveTarget/PublishTemporary/PublishNew/TryDelete, extracted fromBeginPathSaveandPublishSavedwith no behaviour change.Tests (
StreamedWriteTests, + astropy)Byte-for-byte identity with
FitsFile.Saveof the same image and header:byte..double, unsigned conventions);Auto/Always/Never, with and without stale sums in the header;BSCALE/BZERO+BLANKfor the signed integer types,BLANKwithushort, NaN in float/double;XTENSIONheader read from a file, and a header built from scratch withoutSIMPLE.astropy (
verify_with_astropy.py):verify('exception'),verify_checksum/verify_datasum, and values for a float cube with NaN, a scaled int16 image withBLANK(bands of one row), and a uint16 cube written inCreateNewmode.Cancellation:
Complete;Create.Each time: no file left, existing target intact, writer unusable.
Publication:
Complete;DisposebeforeCompleteleaves nothing;CreateNew:Create;Createis kept, andCompletethrowsFitsFileExistsException(innerIOExceptionfrom the move);Errors:
Complete;Complete/Dispose;BITPIXmismatch, missing or inexact unsignedBZERO(incl. 2^63+1 forulong),BSCALE≠ 1, no pixel, missing axis, random groups,BINTABLE, rows longer than an array, unknown enums, unsupportedT.Memory: a 2048-wide float image is written in bands of 16 rows with checksums, at 256 rows and at 4096 rows. Thread-allocated bytes for the 4096-row (32 MB) run are about 108 KB (bound: < 512 KB). The two heights differ by less than 16 KB.
Release 0.3.0
PackageValidationBaselineVersion0.2.0 (dotnet packvalidates against the published 0.2.0); CHANGELOG; RELEASING.docs/images.mdwith a compiled and run example, and a pointer indocs/files.md;Type-1.mdinstead ofType`1.md, and no longer entity-escapes type names inside code spans of the index.tools/verify_package.py: the consumer project also streams an image and checks it is byte-identical toSave.Validation (local, Windows)
dotnet build -c Release: 0 warnings.FITSNET_SKIP_INTEROP=1. Net8 runtime and Python/astropy are not installed locally: the CI matrix runs net8.0, Linux (theO_EXCLpath) and astropy.DocGen --checkup to date.dotnet format --verify-no-changesclean on the touched C# files (the repo has older whitespace findings elsewhere, left untouched).dotnet packwith package validation, and the verify_package consumer program on net10.0 against the packed 0.3.0: OK.dotnet-code-revieweragent; its relevant remarks are applied:BZEROcheck;Autosemantics (sums are always recomputed);CreateNewlimits and open-FitsFilecaveat documented;ObjectDisposedExceptionname.Open questions for the author
WriteRowscurrently discards everything written so far. The alternative is to validate before writing anything and keep the writer usable after anArgumentException.WriteRowsAsync(ReadOnlyMemory<T>)/CompleteAsyncare not included; they could follow additively if Asteria needs them.BITPIXwithBSCALE/BZERO, and NaN →BLANK, is not done. The in-memory path has no such conversion either, so the issue's "BLANK for NaN" is covered as "the caller writes the BLANK value".🤖 Generated with Claude Code