Skip to content

Commit

Permalink
fits: honour UBYTE_FLAG_INFO column annotation
Browse files Browse the repository at this point in the history
The FITS I/O table handlers now mark TFORMn=rB columns with
UBYTE_FLAG_INFO on read, and if it's present on write the
column is written with TFORMn=rB rather than rI.
  • Loading branch information
mbtaylor committed Feb 26, 2015
1 parent cc7abd5 commit d041658
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 61 deletions.
13 changes: 13 additions & 0 deletions fits/src/main/uk/ac/starlink/fits/AbstractFitsTableWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
/**
* Abstract table writer superclass designed for writing FITS tables.
*
* <p>A couple of Auxiliary metadata items of the ColumnInfo metadata
* from written tables are respected:
* <ul>
* <li>{@link uk.ac.starlink.table.Tables#NULL_VALUE_INFO}:
* sets the value of <code>TNULLn</code> "magic" blank value for
* integer columns</li>
* <li>{@link uk.ac.starlink.table.Tables#UBYTE_FLAG_INFO}:
* if set to <code>Boolean.TRUE</code> and if the column has content class
* <code>Short</code> or <code>short[]</code>, the data will be written
* as unsigned bytes (<code>TFORMn='B'</code>)
* not 16-bit signed integers (<code>TFORMn='I'</code>).</li>
* </ul>
*
* @author Mark Taylor
* @since 27 Jun 2006
*/
Expand Down
36 changes: 32 additions & 4 deletions fits/src/main/uk/ac/starlink/fits/ArrayWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.logging.Logger;
import uk.ac.starlink.table.ColumnInfo;
import uk.ac.starlink.table.Tables;

/**
* Object which knows how to write array data for a particular type
Expand All @@ -16,6 +19,9 @@ abstract class ArrayWriter {
private final char formatChar_;
private final int nByte_;

private static final Logger logger_ =
Logger.getLogger( "uk.ac.starlink.fits" );

/**
* Constructor.
*
Expand Down Expand Up @@ -74,14 +80,36 @@ public abstract void writeElement( DataOutput out, Object array,
/**
* Constructs a new ArrayWriter for a given array class.
*
* @param clazz array class which this writer should be able to write
* @param cinfo column metadata describing the data
* which this writer should be able to write
* @param allowSignedByte if true, bytes written as FITS signed bytes
* (TZERO=-128), if false bytes written as signed shorts
* @return new ArrayWriter, or null if <code>clazz</code> can't be handled
* @return new ArrayWriter, or null if <code>cinfo</code> can't be handled
*/
public static ArrayWriter createArrayWriter( Class clazz,
public static ArrayWriter createArrayWriter( ColumnInfo cinfo,
boolean allowSignedByte ) {

Class clazz = cinfo.getContentClass();
final boolean isUbyte =
Boolean.TRUE
.equals( cinfo.getAuxDatumValue( Tables.UBYTE_FLAG_INFO,
Boolean.class ) );

if ( isUbyte ) {
if ( clazz == short[].class ) {
return new NormalArrayWriter( 'B', 1,
new short[] { (short) 0 } ) {
public void writeElement( DataOutput out, Object array,
int ix )
throws IOException {
out.writeByte( ((short[]) array)[ ix ] );
}
};
}
else {
logger_.warning( "Ignoring " + Tables.UBYTE_FLAG_INFO
+ " on non-short[] column " + cinfo );
}
}
if ( clazz == boolean[].class ) {
return new ArrayWriter( 'L', 1 ) {
public void writeElement( DataOutput out, Object array, int ix )
Expand Down
4 changes: 4 additions & 0 deletions fits/src/main/uk/ac/starlink/fits/BintableStarTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ else if ( zIsInt && zInLongRange ) {
cinfo.setContentClass( reader.getContentClass() );
cinfo.setShape( reader.getShape() );
cinfo.setElementSize( reader.getElementSize() );
if ( reader.isUnsignedByte() ) {
cinfo.setAuxDatum( new DescribedValue( Tables.UBYTE_FLAG_INFO,
Boolean.TRUE ) );
}
colReaders_[ icol ] = reader;
}

Expand Down

0 comments on commit d041658

Please sign in to comment.