Package uk.ac.starlink.fits
Class FitsUtil
java.lang.Object
uk.ac.starlink.fits.FitsUtil
Utilities for working with FITS files.
- Since:
- 4 Mar 2022
- Author:
- Mark Taylor
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
FITS block length in bytes (2880).static final int
FITS header card length in bytes (80).static final int
Number of header cards per FITS block (36).static final String
Regex pattern matching floating point value, no grouping.static final int
Maximum No. -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
asNumericArray
(String txt) Attempts to interpret a string as a formatted numeric array.static void
checkColumnCount
(WideFits wide, int ncol) Checks that a table with the given number of columns can be written.static boolean
isFitsCharacter
(int ch) Indicates whether a given character is a legal FITS header character (0x20..0x7e inclusive).static boolean
isMagic
(byte[] buffer) Indicates whether the supplied buffer is the start of a FITS file.static ParsedCard
<?> parseCard
(byte[] buf80) Turns an 80-byte array into a ParsedCard.static FitsHeader
Reads a FITS header from an input stream.static FitsHeader
Reads a FITS header from an input stream if the stream has content.static long
roundUp
(long value, int blockSize) Utility method to round an integer value up to a multiple of a given block size.static long
skipHDUs
(InputStream in, int nskip) Skips forward over a given number of HDUs in the supplied stream.static void
Writes a data-less Primary HDU.static int
writeHeader
(CardImage[] cards, OutputStream out) Writes a FITS header whose content is supplied by an array of cards.
-
Field Details
-
BLOCK_LENG
public static final int BLOCK_LENGFITS block length in bytes (2880).- See Also:
-
CARD_LENG
public static final int CARD_LENGFITS header card length in bytes (80).- See Also:
-
CARDS_PER_BLOCK
public static final int CARDS_PER_BLOCKNumber of header cards per FITS block (36).- See Also:
-
MAX_NCOLSTD
public static final int MAX_NCOLSTDMaximum No. of columns in standard FITS BINTABLE extension (999).- See Also:
-
FLOAT_REGEX
Regex pattern matching floating point value, no grouping.- See Also:
-
-
Method Details
-
isMagic
public static boolean isMagic(byte[] buffer) Indicates whether the supplied buffer is the start of a FITS file. Its contents is checked against the FITS 'magic number', which is the ASCII string "SIMPLE =
".- Parameters:
buffer
- a byte buffer containing the start of a file to test- Returns:
true
iff the bytes inbuffer
look like the start of a FITS file
-
isFitsCharacter
public static boolean isFitsCharacter(int ch) Indicates whether a given character is a legal FITS header character (0x20..0x7e inclusive).- Parameters:
ch
- character to check- Returns:
- true iff ch is legal for inclusion in a FITS header
-
readHeader
Reads a FITS header from an input stream. The stream is read until the end of the last header block. If the stream is positioned at its end on entry, or if there is any error in reading or parsing the header, an IOException is raised.- Parameters:
in
- input stream positioned at start of HDU- Returns:
- header, not null
- Throws:
IOException
-
readHeaderIfPresent
Reads a FITS header from an input stream if the stream has content. The stream is read until the end of the last header block. If the stream is positioned at its end on entry, null is returned.- Parameters:
in
- input stream positioned where an HDU may start- Returns:
- header, or null if the stream has no content
- Throws:
IOException
-
parseCard
Turns an 80-byte array into a ParsedCard. This will always succeed, but if the card doesn't look like a FITS header, the result will have CardType.UNKNOWN.- Parameters:
buf80
- 80-byte array giving card image
-
skipHDUs
Skips forward over a given number of HDUs in the supplied stream. If it reaches the end of the stream, it throws an IOException with a Cause of a TruncatedFileException.- Parameters:
in
- the stream to skip through, positioned at start of HDUnskip
- the number of HDUs to skip- Returns:
- the number of bytes the stream was advanced
- Throws:
IOException
-
roundUp
public static long roundUp(long value, int blockSize) Utility method to round an integer value up to a multiple of a given block size.- Parameters:
value
- non-negative countblockSize
- non-negative size of block- Returns:
- smallest integer that is >=
count
and a multiple ofblockSize
-
writeHeader
Writes a FITS header whose content is supplied by an array of cards. No checks are performed on the card content. An END card must be included in the supplied array if required. Padding is written to advance to a whole number of FITS blocks.- Parameters:
cards
- cards forming content of headerout
- destination stream- Returns:
- number of bytes written, including padding
- Throws:
IOException
-
writeEmptyPrimary
Writes a data-less Primary HDU. It declares EXTEND = T, indicating that extension HDUs will follow.- Parameters:
out
- destination stream- Throws:
IOException
-
checkColumnCount
Checks that a table with the given number of columns can be written. If the column count is not exceeded, nothing happens, but if there are too many columns an informative IOException is thrown.- Parameters:
wide
- extended column convention - may be null for FITS standard behaviour onlyncol
- number of columns to write- Throws:
IOException
- if there are too many columns
-
asNumericArray
Attempts to interpret a string as a formatted numeric array. The string has to be of the form "(x, x, ...)", where x has the same form as a floating point header value. Whitespace is permitted. The output will be an int[] array if the tokens all look like 32-bit integers, or a double[] array if the tokens all look like floating point numbers, or null otherwise.This is a bit hacky, it doesn't correspond to prescriptions in the FITS stanard, but it's useful for some purposes.
- Parameters:
txt
- string- Returns:
- int[] array or double[] array or null
-