diff --git a/src/header/index.ts b/src/header/index.ts index 2555307..df889f3 100644 --- a/src/header/index.ts +++ b/src/header/index.ts @@ -1,3 +1,5 @@ export { getFITSHeaders } from './getFITSHeaders' +export { parseFITSHeaderBlock } from './parseFITSHeaderBlock' + export { parseFITSHeaderRow } from './parseFITSHeaderRow' diff --git a/src/header/parseFITSHeaderBlock.ts b/src/header/parseFITSHeaderBlock.ts new file mode 100644 index 0000000..88ac72f --- /dev/null +++ b/src/header/parseFITSHeaderBlock.ts @@ -0,0 +1,61 @@ +import { FITS_BLOCK_LENGTH, FITS_ROW_LENGTH } from '../fits/constants' + +/** + * + * parseFITSHeaderBlock() + * + * parses a FITS block specifically as a header (looking for the "END" of the + * FITS header) + * + * @param block + * @returns the raw parsed string value for the ArrayBuffer, and two booleans + * specifying that the block has both been parsed and the END has been located. + */ +export const parseFITSHeaderBlock = ( + block: ArrayBuffer +): { s: string; end: boolean; parsed: boolean } => { + const arr = new Uint8Array(block) + + let rows = FITS_BLOCK_LENGTH / FITS_ROW_LENGTH + + let s = '' + + // We need to mark when we are at the END of a FITS header: + let end = false + + // Just a flag to distinguish between a block that has been parsed and a block + // that hasn't: + let parsed = false + + // Check the current block one row at a time starting from the end: + while (rows--) { + const rowIndex = rows * FITS_ROW_LENGTH + + const cchar = arr[rowIndex] + + if (cchar === 32) { + continue + } + + // Check for END keyword with trailing space (69 "E", 78 "N", 68 "D", 32 " ") + if ( + cchar === 69 && + arr[rowIndex + 1] === 78 && + arr[rowIndex + 2] === 68 && + arr[rowIndex + 3] === 32 + ) { + for (let value of Array.from(arr)) { + s += String.fromCharCode(value) + } + end = true + parsed = true + break + } + } + + return { + s, + end, + parsed + } +} diff --git a/src/index.ts b/src/index.ts index 0569d05..a6c7069 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ export { B, I, J, hasDataUnit, swopEndian } from './dataunit' // Flexible Image Transport System (FITS) Header -export { getFITSHeaders, parseFITSHeaderRow } from './header' +export { getFITSHeaders, parseFITSHeaderBlock, parseFITSHeaderRow } from './header' // Flexible Image Transport System (FITS) export { getFITSBlocks } from './fits' diff --git a/src/tests/header.test.ts b/src/tests/header.test.ts index 794750f..1f988c8 100644 --- a/src/tests/header.test.ts +++ b/src/tests/header.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, suite } from 'vitest' -import { getFITSHeaders, parseFITSHeaderRow } from '../header' +import { getFITSHeaders, parseFITSHeaderBlock, parseFITSHeaderRow } from '../header' suite('@observerly/fits Header', () => { describe('FITS File Header Extraction', () => { @@ -27,7 +27,13 @@ suite('@observerly/fits Header', () => { }) }) - describe('FITS File Header Parsing', () => { + describe('FITS File Header Block Parsing', () => { + it('parseFITSHeaderBlock should be defined', () => { + expect(parseFITSHeaderBlock).toBeDefined() + }) + }) + + describe('FITS File Header Row Parsing', () => { it('parseFITSHeaderRow should be defined', () => { expect(parseFITSHeaderRow).toBeDefined() })