Package nom.tam.fits
Class HeaderCard
- java.lang.Object
-
- nom.tam.fits.HeaderCard
-
- All Implemented Interfaces:
CursorValue<java.lang.String>
public class HeaderCard extends java.lang.Object implements CursorValue<java.lang.String>
This class describes methods to access and manipulate the individual cards for a FITS Header.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
comment
The comment part of the card (set to null if there's no comment)private static java.lang.String
CONTINUE_CARD_PREFIX
static int
FITS_HEADER_CARD_SIZE
private static java.lang.String
HIERARCH_WITH_BLANK
private static int
HIERARCH_WITH_BLANK_LENGTH
private static java.lang.String
HIERARCH_WITH_DOT
private static java.util.regex.Pattern
IEEE_REGEX
regexp for IEEE floatsprivate boolean
isString
A flag indicating whether or not this is a string valueprivate java.lang.String
key
The keyword part of the card (set to null if there's no keyword)private static java.util.logging.Logger
LOG
private static java.math.BigDecimal
LONG_MAX_VALUE_AS_BIG_DECIMAL
private static java.util.regex.Pattern
LONG_REGEX
regexp for numbers.private static double
MAX_DECIMAL_VALUE_TO_USE_PLAIN_STRING
private static int
MAX_INTEGER_STRING_SIZE
max number of characters an integer can have.static int
MAX_KEYWORD_LENGTH
Maximum length of a FITS keyword fieldstatic int
MAX_LONG_STRING_CONTINUE_OVERHEAD
the start and end quotes of the string and the ampasant to continue the string.private static int
MAX_LONG_STRING_SIZE
max number of characters a long can have.static int
MAX_LONG_STRING_VALUE_LENGTH
Maximum length of a FITS long string value field.static int
MAX_LONG_STRING_VALUE_WITH_COMMENT_LENGTH
if a commend needs the be specified 2 extra chars are needed to start the commentstatic int
MAX_STRING_VALUE_LENGTH
Maximum length of a FITS string value field.static int
MAX_VALUE_LENGTH
Maximum length of a FITS value field.private static int
NORMAL_ALIGN_POSITION
private static int
NORMAL_SMALL_STRING_ALIGN_POSITION
private boolean
nullable
Does this card represent a nullable field.private static int
SPACE_NEEDED_FOR_EQUAL_AND_TWO_BLANKS
private static int
STRING_SPLIT_POSITION_FOR_EXTRA_COMMENT_SPACE
private java.lang.String
value
The value part of the card (set to null if there's no value)
-
Constructor Summary
Constructors Modifier Constructor Description HeaderCard(java.lang.String key, boolean value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, double value, int precision, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, double value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, float value, int precision, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, float value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, int value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, long value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, java.lang.String comment, boolean nullable)
Create a comment style card.HeaderCard(java.lang.String key, java.lang.String value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, java.lang.String value, java.lang.String comment, boolean nullable)
Create a HeaderCard from its component partsprivate
HeaderCard(java.lang.String key, java.lang.String value, java.lang.String comment, boolean nullable, boolean isString)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, java.math.BigDecimal value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(java.lang.String key, java.math.BigInteger value, java.lang.String comment)
Create a HeaderCard from its component partsHeaderCard(HeaderCardCountingArrayDataInput dis)
HeaderCard(ArrayDataInput dis)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
cardSize()
HeaderCard
copy()
static HeaderCard
create(java.lang.String card)
private static java.lang.String
dblString(double decimalValue, int availableSpace)
Create a string from a BigDecimal making sure that it's not longer than the available space.private static java.lang.String
dblString(double input, int precision, int availableSpace)
private static java.lang.String
dblString(java.math.BigDecimal decimalValue, int availableSpace)
Create a string from a BigDecimal making sure that it's not longer than the available space.private static java.lang.String
dblString(java.math.BigDecimal decimalValue, int precision, int availableSpace)
Create a string from a BigDecimal making sure that it's not longer than the available space.private void
extractValueCommentFromString(HeaderCardCountingArrayDataInput dis, java.lang.String card)
private static java.math.BigDecimal
floatToBigDecimal(float floatValue)
attention float to double cases are very lossy so a toString is needed to keep the precision.private java.lang.Boolean
getBooleanValue(java.lang.Boolean defaultValue)
java.lang.String
getComment()
private static java.lang.Class<?>
getDecimalNumberType(java.lang.String value)
detect the decimal type of the value, does it fit in a Double/BigInteger or must it be a BigDecimal to keep the needed precission.private static java.lang.Class<?>
getIntegerNumberType(java.lang.String value)
java.lang.String
getKey()
java.lang.String
getValue()
<T> T
getValue(java.lang.Class<T> clazz, T defaultValue)
private void
hierarchCard(java.lang.String card, HeaderCardCountingArrayDataInput dis)
Process HIERARCH style cards...boolean
isKeyValuePair()
boolean
isStringValue()
private void
longStringCard(HeaderCardCountingArrayDataInput dis, FitsHeaderCardParser.ParsedValue parsedValue)
private int
maxStringValueLength()
private static java.lang.String
readOneHeaderLine(HeaderCardCountingArrayDataInput dis)
Read exactly one complete fits header line from the input.protected static HeaderCard
saveNewHeaderCard(java.lang.String key, java.lang.String comment, boolean isString)
This method is only used internally when it is sure that the creation of the card is granted not to throw an exceptionvoid
setComment(java.lang.String comment)
set the comment of a card.(package private) void
setKey(java.lang.String newKey)
Set the key.HeaderCard
setValue(boolean update)
Set the value for this card.HeaderCard
setValue(double update)
Set the value for this card.HeaderCard
setValue(double update, int precision)
Set the value for this card.HeaderCard
setValue(float update)
Set the value for this card.HeaderCard
setValue(float update, int precision)
Set the value for this card.HeaderCard
setValue(int update)
Set the value for this card.HeaderCard
setValue(long update)
Set the value for this card.HeaderCard
setValue(java.lang.String update)
Set the value for this card.HeaderCard
setValue(java.math.BigDecimal update)
Set the value for this card.private static int
spaceAvailableForValue(java.lang.String key)
private static ArrayDataInput
stringToArrayInputStream(java.lang.String card)
private boolean
stringValueToString(int alignSmallString, int alignPosition, FitsLineAppender buf, boolean commentHandled)
java.lang.String
toString()
Return the modulo 80 character card image, the toString tries to preserve as much as possible of the comment value by reducing the alignment of the Strings if the comment is longer and if longString is enabled the string can be split into one more card to have more space for the comment.protected java.lang.String
toString(FitsFactory.FitsSettings settings)
Same astoString()
just with a prefetched settings objectjava.lang.Class<?>
valueType()
private void
writeLongStringValue(FitsLineAppender buf, java.lang.String stringValueString)
-
-
-
Field Detail
-
SPACE_NEEDED_FOR_EQUAL_AND_TWO_BLANKS
private static final int SPACE_NEEDED_FOR_EQUAL_AND_TWO_BLANKS
- See Also:
- Constant Field Values
-
MAX_DECIMAL_VALUE_TO_USE_PLAIN_STRING
private static final double MAX_DECIMAL_VALUE_TO_USE_PLAIN_STRING
- See Also:
- Constant Field Values
-
LOG
private static final java.util.logging.Logger LOG
-
CONTINUE_CARD_PREFIX
private static final java.lang.String CONTINUE_CARD_PREFIX
-
FITS_HEADER_CARD_SIZE
public static final int FITS_HEADER_CARD_SIZE
- See Also:
- Constant Field Values
-
HIERARCH_WITH_BLANK
private static final java.lang.String HIERARCH_WITH_BLANK
-
HIERARCH_WITH_BLANK_LENGTH
private static final int HIERARCH_WITH_BLANK_LENGTH
-
HIERARCH_WITH_DOT
private static final java.lang.String HIERARCH_WITH_DOT
-
IEEE_REGEX
private static final java.util.regex.Pattern IEEE_REGEX
regexp for IEEE floats
-
LONG_MAX_VALUE_AS_BIG_DECIMAL
private static final java.math.BigDecimal LONG_MAX_VALUE_AS_BIG_DECIMAL
-
LONG_REGEX
private static final java.util.regex.Pattern LONG_REGEX
regexp for numbers.
-
MAX_INTEGER_STRING_SIZE
private static final int MAX_INTEGER_STRING_SIZE
max number of characters an integer can have.
-
MAX_KEYWORD_LENGTH
public static final int MAX_KEYWORD_LENGTH
Maximum length of a FITS keyword field- See Also:
- Constant Field Values
-
MAX_LONG_STRING_CONTINUE_OVERHEAD
public static final int MAX_LONG_STRING_CONTINUE_OVERHEAD
the start and end quotes of the string and the ampasant to continue the string.- See Also:
- Constant Field Values
-
MAX_LONG_STRING_SIZE
private static final int MAX_LONG_STRING_SIZE
max number of characters a long can have.
-
MAX_LONG_STRING_VALUE_LENGTH
public static final int MAX_LONG_STRING_VALUE_LENGTH
Maximum length of a FITS long string value field. the & for the continuation needs one char.- See Also:
- Constant Field Values
-
MAX_LONG_STRING_VALUE_WITH_COMMENT_LENGTH
public static final int MAX_LONG_STRING_VALUE_WITH_COMMENT_LENGTH
if a commend needs the be specified 2 extra chars are needed to start the comment- See Also:
- Constant Field Values
-
MAX_STRING_VALUE_LENGTH
public static final int MAX_STRING_VALUE_LENGTH
Maximum length of a FITS string value field.- See Also:
- Constant Field Values
-
MAX_VALUE_LENGTH
public static final int MAX_VALUE_LENGTH
Maximum length of a FITS value field.- See Also:
- Constant Field Values
-
NORMAL_ALIGN_POSITION
private static final int NORMAL_ALIGN_POSITION
- See Also:
- Constant Field Values
-
NORMAL_SMALL_STRING_ALIGN_POSITION
private static final int NORMAL_SMALL_STRING_ALIGN_POSITION
- See Also:
- Constant Field Values
-
STRING_SPLIT_POSITION_FOR_EXTRA_COMMENT_SPACE
private static final int STRING_SPLIT_POSITION_FOR_EXTRA_COMMENT_SPACE
- See Also:
- Constant Field Values
-
comment
private java.lang.String comment
The comment part of the card (set to null if there's no comment)
-
isString
private boolean isString
A flag indicating whether or not this is a string value
-
key
private java.lang.String key
The keyword part of the card (set to null if there's no keyword)
-
nullable
private boolean nullable
Does this card represent a nullable field. ?
-
value
private java.lang.String value
The value part of the card (set to null if there's no value)
-
-
Constructor Detail
-
HeaderCard
public HeaderCard(ArrayDataInput dis) throws TruncatedFileException, java.io.IOException
- Throws:
TruncatedFileException
java.io.IOException
-
HeaderCard
public HeaderCard(HeaderCardCountingArrayDataInput dis) throws TruncatedFileException, java.io.IOException
- Throws:
TruncatedFileException
java.io.IOException
-
HeaderCard
public HeaderCard(java.lang.String key, java.math.BigDecimal value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, java.math.BigInteger value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, boolean value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, double value, int precision, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')precision
- Number of decimal places (fixed format).comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, double value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, float value, int precision, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')precision
- Number of decimal places (fixed format).comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, float value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, int value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, long value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword
-
HeaderCard
public HeaderCard(java.lang.String key, java.lang.String comment, boolean nullable) throws HeaderCardException
Create a comment style card. This constructor builds a card which has no value. This may be either a comment style card in which case the nullable field should be false, or a value field which has a null value, in which case the nullable field should be true.- Parameters:
key
- The key for the comment or nullable field.comment
- The commentnullable
- Is this a nullable field or a comment-style card?- Throws:
HeaderCardException
- for any invalid keyword or value
-
HeaderCard
public HeaderCard(java.lang.String key, java.lang.String value, java.lang.String comment) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- keyword (null for a comment)value
- value (null for a comment or keyword without an '=')comment
- comment- Throws:
HeaderCardException
- for any invalid keyword or value
-
HeaderCard
public HeaderCard(java.lang.String key, java.lang.String value, java.lang.String comment, boolean nullable) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- Keyword (null for a COMMENT)value
- Valuecomment
- Commentnullable
- Is this a nullable value card?- Throws:
HeaderCardException
- for any invalid keyword or value
-
HeaderCard
private HeaderCard(java.lang.String key, java.lang.String value, java.lang.String comment, boolean nullable, boolean isString) throws HeaderCardException
Create a HeaderCard from its component parts- Parameters:
key
- Keyword (null for a COMMENT)value
- Valuecomment
- Commentnullable
- Is this a nullable value card?- Throws:
HeaderCardException
- for any invalid keyword or value
-
-
Method Detail
-
create
public static HeaderCard create(java.lang.String card)
- Parameters:
card
- the 80 character card image- Returns:
- a created HeaderCard from a FITS card string.
-
dblString
private static java.lang.String dblString(java.math.BigDecimal decimalValue, int availableSpace)
Create a string from a BigDecimal making sure that it's not longer than the available space.- Parameters:
decimalValue
- the decimal value to printavailableSpace
- the space available for the value- Returns:
- the string representing the value.
-
dblString
private static java.lang.String dblString(java.math.BigDecimal decimalValue, int precision, int availableSpace)
Create a string from a BigDecimal making sure that it's not longer than the available space.- Parameters:
decimalValue
- the decimal value to printprecision
- the precision to useavailableSpace
- the space available for the value- Returns:
- the string representing the value.
-
dblString
private static java.lang.String dblString(double decimalValue, int availableSpace)
Create a string from a BigDecimal making sure that it's not longer than the available space.- Parameters:
decimalValue
- the decimal value to printavailableSpace
- the space available for the value- Returns:
- the string representing the value.
-
dblString
private static java.lang.String dblString(double input, int precision, int availableSpace)
- Parameters:
input
- float value being convertedprecision
- the number of decimal places to show- Returns:
- Create a fixed decimal string from a double with the specified precision.
-
floatToBigDecimal
private static java.math.BigDecimal floatToBigDecimal(float floatValue)
attention float to double cases are very lossy so a toString is needed to keep the precision. proof (double)500.055f = 500.05499267578125d- Parameters:
floatValue
- the float value- Returns:
- the BigDecimal as close to the value of the float as possible
-
getDecimalNumberType
private static java.lang.Class<?> getDecimalNumberType(java.lang.String value)
detect the decimal type of the value, does it fit in a Double/BigInteger or must it be a BigDecimal to keep the needed precission.- Parameters:
value
- the String value to check.- Returns:
- the type to fit the value
-
getIntegerNumberType
private static java.lang.Class<?> getIntegerNumberType(java.lang.String value)
-
readOneHeaderLine
private static java.lang.String readOneHeaderLine(HeaderCardCountingArrayDataInput dis) throws java.io.IOException, TruncatedFileException
Read exactly one complete fits header line from the input.- Parameters:
dis
- the data input stream to read the line- Returns:
- a string of exactly 80 characters
- Throws:
java.io.IOException
- if the input stream could not be readTruncatedFileException
- is there was not a complete line available in the input.
-
spaceAvailableForValue
private static int spaceAvailableForValue(java.lang.String key)
-
stringToArrayInputStream
private static ArrayDataInput stringToArrayInputStream(java.lang.String card)
-
saveNewHeaderCard
protected static HeaderCard saveNewHeaderCard(java.lang.String key, java.lang.String comment, boolean isString)
This method is only used internally when it is sure that the creation of the card is granted not to throw an exception- Parameters:
key
- the key for the cardcomment
- the comment for the cardisString
- is this a string value card?- Returns:
- the new HeaderCard
-
cardSize
public int cardSize()
- Returns:
- the size of the card in blocks of 80 bytes. So normally every card will return 1. only long stings can return more than one.
-
copy
public HeaderCard copy() throws HeaderCardException
- Throws:
HeaderCardException
-
getComment
public java.lang.String getComment()
- Returns:
- the comment from this card
-
getKey
public java.lang.String getKey()
- Specified by:
getKey
in interfaceCursorValue<java.lang.String>
- Returns:
- the keyword from this card
-
getValue
public java.lang.String getValue()
- Returns:
- the value from this card
-
getValue
public <T> T getValue(java.lang.Class<T> clazz, T defaultValue)
- Type Parameters:
T
- the type of the requested class- Parameters:
clazz
- the requested class of the valuedefaultValue
- the value if the card was not present.- Returns:
- the value from this card as a specific type
-
isKeyValuePair
public boolean isKeyValuePair()
- Returns:
- Is this a key/value card?
-
isStringValue
public boolean isStringValue()
- Returns:
- if this card contain does a string value?
-
setComment
public void setComment(java.lang.String comment)
set the comment of a card.- Parameters:
comment
- the comment to set.
-
setValue
public HeaderCard setValue(java.math.BigDecimal update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(boolean update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(double update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(double update, int precision)
Set the value for this card.- Parameters:
update
- the new value to setprecision
- the number of decimal places to show- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(float update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(float update, int precision)
Set the value for this card.- Parameters:
update
- the new value to setprecision
- the number of decimal places to show- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(int update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(long update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
setValue
public HeaderCard setValue(java.lang.String update)
Set the value for this card.- Parameters:
update
- the new value to set- Returns:
- the HeaderCard itself
-
toString
public java.lang.String toString()
Return the modulo 80 character card image, the toString tries to preserve as much as possible of the comment value by reducing the alignment of the Strings if the comment is longer and if longString is enabled the string can be split into one more card to have more space for the comment.- Overrides:
toString
in classjava.lang.Object
-
toString
protected java.lang.String toString(FitsFactory.FitsSettings settings)
Same astoString()
just with a prefetched settings object- Parameters:
settings
- the settings to use for writing the header card- Returns:
- the string representing the card.
-
valueType
public java.lang.Class<?> valueType()
- Returns:
- the type of the value.
-
extractValueCommentFromString
private void extractValueCommentFromString(HeaderCardCountingArrayDataInput dis, java.lang.String card) throws java.io.IOException, TruncatedFileException
- Throws:
java.io.IOException
TruncatedFileException
-
getBooleanValue
private java.lang.Boolean getBooleanValue(java.lang.Boolean defaultValue)
-
hierarchCard
private void hierarchCard(java.lang.String card, HeaderCardCountingArrayDataInput dis) throws java.io.IOException, TruncatedFileException
Process HIERARCH style cards... HIERARCH LEV1 LEV2 ... = value / comment The keyword for the card will be "HIERARCH.LEV1.LEV2..." A '/' is assumed to start a comment.- Parameters:
dis
-- Throws:
java.io.IOException
TruncatedFileException
-
longStringCard
private void longStringCard(HeaderCardCountingArrayDataInput dis, FitsHeaderCardParser.ParsedValue parsedValue) throws java.io.IOException, TruncatedFileException
- Throws:
java.io.IOException
TruncatedFileException
-
maxStringValueLength
private int maxStringValueLength()
-
stringValueToString
private boolean stringValueToString(int alignSmallString, int alignPosition, FitsLineAppender buf, boolean commentHandled)
-
writeLongStringValue
private void writeLongStringValue(FitsLineAppender buf, java.lang.String stringValueString)
-
setKey
void setKey(java.lang.String newKey)
Set the key.
-
-