FWIW, here is a script that loads all keywords from the primary HDU of any FITS file and shows them on the console. No need to open the image.
More information as source code comments. BTW, this script provides a bug-free replacement for FITSKeyword. This is a temporary workaround until we publish PI 1.6.1.
UPDATED 2010 MAY 11: Code replaced to fix a few bugs.
#include <pjsr/DataType.jsh>
function trim( s )
{
return s.replace( /^\s*|\s*$/g, '' );
}
/**
* Bug-free replacement for the FITSKeyword PJSR object.
* Use it for PI version <= 1.6.0.
*/
function Keyword( name, value, comment )
{
this.name = trim( name );
this.value = value ? trim( value ) : new String;
this.comment = comment ? trim( comment ) : new String;
this.isString = function()
{
return this.value.charCodeAt( 0 ) == 39;
};
this.isBoolean = function()
{
return this.value == 'T' || this.value == 'F';
};
this.isNumeric = function()
{
var c0 = this.value.charCodeAt( 0 );
return c0 >= 48 && c0 <= 57 || c0 == 46 || c0 == 43 || c0 == 45;
};
this.numericValue = function()
{
return parseFloat( this.value );
};
this.strippedValue = function()
{
var stripped;
if ( this.value.length > 0 && this.value.charCodeAt( 0 ) == 39 ) // leading delimiter ?
{
var n = this.value.length-1;
if ( this.value.charCodeAt( n ) == 39 ) // trailing delimiter ?
--n;
stripped = this.value.substr( 1, n );
}
else
stripped = this.value;
return trim( stripped );
};
this.trim = function()
{
this.name = trim( this.name );
this.value = trim( this.value );
this.comment = trim( this.comment );
};
}
/**
* Opens a FITS file at the specified fitsFilePath path and reads all keywords
* from the primary HDU. Returns an array of FITS keywords.
*
* Limitations:
* - HIERARCH convention not supported, i.e. all keyword names must
* be strings of (possibly padded with white spaces) eight characters.
* - Reads only the first HDU; extension HDUs are ignored.
*/
function LoadFITSKeywords( fitsFilePath )
{
function searchCommentSeparator( b )
{
var inString = false;
for ( var i = 9; i < 80; ++i )
switch ( b.at( i ) )
{
case 39: // single quote
inString ^= true;
break;
case 47: // slash
if ( !inString )
return i;
break;
}
return -1;
}
var f = new File;
f.openForReading( fitsFilePath );
var keywords = new Array;
for ( ;; )
{
var rawData = f.read( DataType_ByteArray, 80 );
var name = rawData.toString( 0, 8 );
if ( name.toUpperCase() == "END " ) // end of HDU keyword list?
break;
if ( f.isEOF )
throw new Error( "Unexpected end of file: " + fitsFilePath );
var value;
var comment;
if ( rawData.at( 8 ) == 61 ) // value separator (an equal sign at byte 8) present?
{
// This is a valued keyword
var cmtPos = searchCommentSeparator( rawData ); // find comment separator slash
if ( cmtPos < 0 ) // no comment separator?
cmtPos = 80;
value = rawData.toString( 9, cmtPos-9 ); // value substring
if ( cmtPos < 80 )
comment = rawData.toString( cmtPos+1, 80-cmtPos-1 ); // comment substring
else
comment = new String;
}
else
{
// No value in this keyword
value = new String;
comment = rawData.toString( 8, 80-8 );
}
// Perform a naive sanity check: a valid FITS file must begin with a SIMPLE=T keyword.
if ( keywords.length == 0 )
if ( name != "SIMPLE " && trim( value ) != 'T' )
throw new Error( "File does not seem a valid FITS file: " + fitsFilePath );
// Add new keyword. Note: use FITSKeyword with PI >= 1.6.1
keywords.push( new Keyword( name, value, comment ) );
}
f.close();
return keywords;
}
function main()
{
var ofd = new OpenFileDialog;
ofd.multipleSelections = true;
ofd.caption = "LoadFITSKeywords: Select FITS Files";
if ( ofd.execute() )
for ( var i in ofd.fileNames )
{
console.writeln( "<end><cbr><br><b>", ofd.fileNames[i], "</b>" );
var keywords = LoadFITSKeywords( ofd.fileNames[i] );
for ( var i in keywords )
console.writeln( keywords[i].name, ',', keywords[i].value, ',', keywords[i].comment );
}
}
main();