Author Topic: PJSR: FITSKeyword object  (Read 8447 times)

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
PJSR: FITSKeyword object
« on: 2010 May 09 00:17:26 »
Hi,

I'm trying to write a script that will need information from the fits header.  I figured out how to access the "keywords" property of an image and found that the FITSKeyword object has a "name", "value" and "comment" property.  Problem is that when I write out one of these properties the string only contains every other character.  For example,

EXPTIME gets written as EPIE
CCD-TEMP gets written as CDTM
XBINNING gets written as XINN

If I write the whole object the strings looks correct but when I try to access the individual elements they get munged.  I'm probably doing something stupid but I don't see it and I haven't found any other examples of FITSKeyword in PJSR.


Here a snippet of my code,

     for ( var i = 0; i < this.inputFiles.length; ++i ) {
         var image = ImageWindow.open( this.inputFiles);
         
         for ( var j = 0; j < image[0].keywords.length; ++j ) {
            var fitsKW = image[0].keywords[j];
            console.writeln(fitsKW);

            for ( var k in fitsKW ) {
               console.writeln(k+" => "+fitsKW[k]);
            }
         }
         image[0].close
      }


Here is a portion of the output,

     {XPIXSZ,5.4000000000000004,Pixel Width in microns (after binning)}
     name => XIS
     value => 540000000
     comment => PxlWdhi irn atrbnig
     isNull => false
     isString => false
     isBoolean => false
     isNumeric => true
     numericValue => 5.4
     strippedValue => 5.4000000000000004

     {IMAGETYP,'Bias Frame',Type of image}
     name => IAEY
     value => 'isFae
     comment => Tp fiae
     isNull => false
     isString => true
     isBoolean => false
     isNumeric => false
     numericValue => 0
     strippedValue => Bias Frame


Thanks,
Mike

Offline Niall Saunders

  • PTeam Member
  • PixInsight Jedi Knight
  • *****
  • Posts: 1456
  • We have cookies? Where ?
Re: PJSR: FITSKeyword object
« Reply #1 on: 2010 May 09 00:49:53 »
Hi Mike,

Instead of posting a snippet, can you post the whole code - or is it part of a 'massive' script?

I am concerned that something may have happened during the 'transcript'

However, in any case, you do not seem to have a 'correct' construct in the FOR/NEXT loop for var 'k' - or, at least I do not understand the (var k in fitsKW) construct.

I would have used something like

Code: [Select]
for ( var k = 0; k < fitsKW.length; ++k ) {
   console.writeln(k+" => "+fitsKW[k]);
}

(and, please note that I have NOT tested this, and haven't coded in PJSR for about a year now - so I am VERY 'rusty' :footinmouth:)

Cheers,
Cheers,
Niall Saunders
Clinterty Observatories
Aberdeen, UK

Altair Astro GSO 10" f/8 Ritchey Chrétien CF OTA on EQ8 mount with homebrew 3D Balance and Pier
Moonfish ED80 APO & Celestron Omni XLT 120
QHY10 CCD & QHY5L-II Colour
9mm TS-OAG and Meade DSI-IIC

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
Re: PJSR: FITSKeyword object
« Reply #2 on: 2010 May 09 07:35:52 »
Thanks Niall for responding.  I was taking advantage of javascripts ability to treat objects as associative arrays.  Since fitsKW in my example is not a list fitsKW.length is null.  Here's another snippet that avoids this altogether and demonstrates the problem.

    for ( var j = 0; j < image[0].keywords.length; ++j ) {
       var fitsKW = image[0].keywords[j];
       console.writeln("FITSKeyword Object: " + fitsKW);
       console.writeln("Name: " + fitsKW.name);
       console.writeln("Value: " + fitsKW.value);
    }

Output of just one of the loops,

    FITSKeyword Object: {IMAGETYP,'Bias Frame',Type of image}
    Name: IAEY
    Value: 'isFae

You see that when I print the object the values are stored correctly but when I access fitsKW.name and fitsKW.value it's getting every other character.

I figured out the name, value and comment part from reading FITSHeaderParameters.cpp but it would be great to have an example or some documentation of FITSKeyword in PJSR.

Mike

Offline Niall Saunders

  • PTeam Member
  • PixInsight Jedi Knight
  • *****
  • Posts: 1456
  • We have cookies? Where ?
Re: PJSR: FITSKeyword object
« Reply #3 on: 2010 May 09 14:59:55 »
Hi again Mike,

OK - seems that you might need to give Juan time to reply here. Unfortunately I am bogged down in a major uPIC-chip development project that is going to use upp all of my spare time for a couple of weeks, so I don't really want to get side-tracked too much into PJSR.

However, I will still be keeping an eye on the Forum here, and if Juan doesn't chip in, I'll maybe take time out and try and help. Others may also have an idea what is happening.

Cheers,
Cheers,
Niall Saunders
Clinterty Observatories
Aberdeen, UK

Altair Astro GSO 10" f/8 Ritchey Chrétien CF OTA on EQ8 mount with homebrew 3D Balance and Pier
Moonfish ED80 APO & Celestron Omni XLT 120
QHY10 CCD & QHY5L-II Colour
9mm TS-OAG and Meade DSI-IIC

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: FITSKeyword object
« Reply #4 on: 2010 May 10 00:12:10 »
Hi Mike,

Welcome to PixInsight Forum and development.

You've discovered a bug in the FITSKeyword PJSR object. See my next post on the Bug Reports forum board.

Naturally this bug will be fixed in the next release, due in a few days. Sorry for the inconvenience.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Niall Saunders

  • PTeam Member
  • PixInsight Jedi Knight
  • *****
  • Posts: 1456
  • We have cookies? Where ?
Re: PJSR: FITSKeyword object
« Reply #5 on: 2010 May 10 00:59:52 »
Thanks for the feedback Juan, that will save me having to roll-up my 'scripting sleeves' and figure out what was happening!

Cheers, and good luck with the development Mike.
Cheers,
Niall Saunders
Clinterty Observatories
Aberdeen, UK

Altair Astro GSO 10" f/8 Ritchey Chrétien CF OTA on EQ8 mount with homebrew 3D Balance and Pier
Moonfish ED80 APO & Celestron Omni XLT 120
QHY10 CCD & QHY5L-II Colour
9mm TS-OAG and Meade DSI-IIC

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
Re: PJSR: FITSKeyword object
« Reply #6 on: 2010 May 10 07:27:11 »
Thanks guys for taking the time. I guess I'm going a little past "kicking the tires" in my evaluation of PixInsight but it's just so much fun having such a powerful tool that I can actually tinker with myself. 

Mike

Offline Niall Saunders

  • PTeam Member
  • PixInsight Jedi Knight
  • *****
  • Posts: 1456
  • We have cookies? Where ?
Re: PJSR: FITSKeyword object
« Reply #7 on: 2010 May 10 09:08:37 »
Quote
I guess I'm going a little past "kicking the tires"

Not at all Mike - everybody takes up PI at their own speed. And Juan is totally flexible when it comes to users working with PI during the trial period. If they want a high level of support during that period - and if folks have the time and resources to provide that support, then that is what they get.

You have just as much entitlement to full support during the trial period as you do once you have paid your license fee - perhaps even more :angel:

Cheers,
Cheers,
Niall Saunders
Clinterty Observatories
Aberdeen, UK

Altair Astro GSO 10" f/8 Ritchey Chrétien CF OTA on EQ8 mount with homebrew 3D Balance and Pier
Moonfish ED80 APO & Celestron Omni XLT 120
QHY10 CCD & QHY5L-II Colour
9mm TS-OAG and Meade DSI-IIC

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: PJSR: FITSKeyword object
« Reply #8 on: 2010 May 10 10:00:09 »
Hello,
I wanted to read some FITS keyword too to find the resolution of an image, but preferably without reading the whole image.  The idea is to group a list of images by resolution to drive alignment (often the RGB have half the resolution of the L). If we load an image, is the image itself immediately loaded or is it lazily loaded. I would like to avoid loading 20+ images just to read 2 keywords, if at all possible.

-- bitli

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
Re: PJSR: FITSKeyword object
« Reply #9 on: 2010 May 10 10:38:45 »
I think that this is very doable, since FIT's incremental reading property would allow just to read the header. Out of my league, but I think Juan may implement a tool that opens the header and displays it in the console with little effort  ::)  O:)
Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: FITSKeyword object
« Reply #10 on: 2010 May 10 15:39:16 »
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.

Code: [Select]
#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();
« Last Edit: 2010 May 11 03:21:03 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
Re: PJSR: FITSKeyword object
« Reply #11 on: 2010 May 10 16:07:01 »
Didn't I say?... :D :D :D
Thanks Juan ;)
Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
Re: PJSR: FITSKeyword object
« Reply #12 on: 2010 May 10 19:10:06 »
Thanks!

Mike