Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added parseFITSHeaderBlock() to header module. #14

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/header/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { getFITSHeaders } from './getFITSHeaders'

export { parseFITSHeaderBlock } from './parseFITSHeaderBlock'

export { parseFITSHeaderRow } from './parseFITSHeaderRow'
61 changes: 61 additions & 0 deletions src/header/parseFITSHeaderBlock.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 8 additions & 2 deletions src/tests/header.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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()
})
Expand Down