Skip to content

Commit

Permalink
fits: fix PCOUNT bug in fits-var writer
Browse files Browse the repository at this point in the history
In the VariableFitsTableSerializer, which uses P and Q output descriptors
to write variable-length arrays into the FITS HDU heap area,
I was writing the wrong value for the PCOUNT header card.
It was using the size of the heap, but it must include also the size
of any gap between the end of the data area and the start of the heap.
Fixed.
  • Loading branch information
mbtaylor committed Dec 16, 2014
1 parent 55202f4 commit 9b6b2cc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
24 changes: 14 additions & 10 deletions fits/src/main/uk/ac/starlink/fits/VariableFitsTableSerializer.java
Expand Up @@ -47,7 +47,7 @@ public VariableFitsTableSerializer( StarTable table,
storagePolicy_ = storagePolicy;
allowSignedByte_ = allowSignedByte;
init( table );
set64BitMode( getPCount() > Integer.MAX_VALUE );
set64BitMode( getHeapSize() > Integer.MAX_VALUE );
}

/**
Expand All @@ -70,9 +70,11 @@ public void set64BitMode( boolean useQ ) {

public Header getHeader() throws HeaderCardException {
Header hdr = super.getHeader();
long pcount = getPCount();
long theap = hdr.getIntValue( "NAXIS1" ) * hdr.getIntValue( "NAXIS2" );
theap = ( ( theap + 2880 - 1 ) / 2880 ) * 2880;
long heapSize = getHeapSize();
long datEnd = hdr.getIntValue( "NAXIS1" ) * hdr.getIntValue( "NAXIS2" );
long theap = ( ( datEnd + 2880 - 1 ) / 2880 ) * 2880;
long gap = theap - datEnd;
long pcount = gap + heapSize;

/* Header manipulation methods leave a bit to be desired.
* It is a bit fiddly to make sure these cards go in the right place. */
Expand All @@ -85,7 +87,8 @@ public Header getHeader() throws HeaderCardException {
HeaderCard card = (HeaderCard) it.next();
String key = card.getKey();
if ( "PCOUNT".equals( key ) ) {
cardList.add( new HeaderCard( "PCOUNT", pcount, "heap size" ) );
cardList.add( new HeaderCard( "PCOUNT", pcount,
"heap size + gap" ) );
}
else if ( "TFIELDS".equals( key ) ) {
cardList.add( card );
Expand Down Expand Up @@ -129,14 +132,14 @@ private VariableArrayColumnWriter[] getVariableArrayColumnWriters() {
*
* @return heap size
*/
private long getPCount() {
long pcount = 0L;
private final long getHeapSize() {
long count = 0L;
VariableArrayColumnWriter[] vcws = getVariableArrayColumnWriters();
for ( int iv = 0; iv < vcws.length; iv++ ) {
VariableArrayColumnWriter vcw = vcws[ iv ];
pcount += vcw.totalElements_ * vcw.arrayWriter_.getByteCount();
count += vcw.totalElements_ * vcw.arrayWriter_.getByteCount();
}
return pcount;
return count;
}

public void writeData( DataOutput out ) throws IOException {
Expand All @@ -161,7 +164,8 @@ public void writeData( DataOutput out ) throws IOException {
finally {
byteStore.close();
}
int over = (int) ( getPCount() % 2880 );
assert byteStore.getLength() == getHeapSize();
int over = (int) ( getHeapSize() % 2880 );
if ( over > 0 ) {
out.write( new byte[ 2880 - over ] );
}
Expand Down
2 changes: 2 additions & 0 deletions topcat/src/docs/sun253.xml
Expand Up @@ -20577,6 +20577,8 @@ introduced since the last version:
<ref id="HistogramPlotWindow">Histogram window</ref>
allow you to export histogram bin data as a new table
(this reinstates behaviour available from old plot windows).</li>
<li>Fixed error in fits-var output
(PCOUNT header card did not include block alignment gap).</li>
</ul>
</p></dd>

Expand Down
2 changes: 2 additions & 0 deletions ttools/src/docs/sun256.xml
Expand Up @@ -10055,6 +10055,8 @@ eds. C. Gabriel et al., ASP Conf. Ser. 351, p. 666 (2006)
<code>plot2plane</code>.</li>
<li>Fix NullPointerException bug when null value was supplied to
multi-word parameter (e.g. <code>tcube</code>).</li>
<li>Fixed error in fits-var output
(PCOUNT header card did not include block alignment gap).</li>
</ul>
</p></dd>

Expand Down

0 comments on commit 9b6b2cc

Please sign in to comment.