Author Topic: PJSR: some questions (creating dir, writing FITS key)  (Read 5077 times)

Offline kwiechen

  • PixInsight Addict
  • ***
  • Posts: 186
PJSR: some questions (creating dir, writing FITS key)
« on: 2011 November 20 23:54:50 »
Hello,

I have two questions regarding PJSR programming:

1) Is there an OS-independent way to check if a directory exists and to create one if not?

2) Is it possible to write FITS keywords and values back when saving an ImageWindow?

Best,

Kai

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #1 on: 2011 November 21 00:30:01 »
Hi Kai,

Quote
1) Is there an OS-independent way to check if a directory exists and to create one if not?

These global methods of the File object provide the functionality you're looking for:

Boolean File.directoryExists( String dirPath )
void File.createDirectory( String dirPath[, Boolean createIntermediateDirs=true] )

Example:

var pathToMyDir = "/path/to/mydir";
if ( !File.directoryExists( pathToMyDir ) )
   File.createDirectory( pathToMyDir );


Quote
2) Is it possible to write FITS keywords and values back when saving an ImageWindow?

The ImageWindow.keywords property is an array of FITSKeyword objects:

Array ImageWindow.keywords

The FITSKeyword object has name, value and comment String properties that you can read and write. It also has a constructor that allows you to set all of these properties at once:

new FITSKeyword( String name, String value[, String comment] )

Using the keywords property you can get and set FITS keywords for an image window before saving it. For example:

ImageWindow window = ImageWindow.activeWindow;
if ( !window.isNull )
   window.keywords.push( new FITSKeyword( "MYKWD", 123.456, "my private keyword" ) );


Hope this helps.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline kwiechen

  • PixInsight Addict
  • ***
  • Posts: 186
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #2 on: 2011 November 24 09:54:49 »
Thank you! Is a PJSR documentation available?

Best,

Kai


Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2132
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #3 on: 2011 November 24 10:13:13 »
Thank you! Is a PJSR documentation available?

Not really. Your best bet is the bubble help that appear when you hover above function calls in the script editor browser window, and trying to derive the functionality from the PCL documentation http://pixinsight.com/developer/pcl/doc/index.html . Some of the Core Javascript Objects are also documented in Javascript tutorials (e.g. Array).

Of course, you can always ask in the forum, and Juan will usually help within in couple of days.
Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline zvrastil

  • PixInsight Addict
  • ***
  • Posts: 179
    • Astrophotography
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #4 on: 2011 November 24 10:37:52 »
Just to make sure it is not forgot, I'd like to cite Juan's word from this recent post: http://pixinsight.com/forum/index.php?topic=3585.msg24965#msg24965 >:D.
Quote

Then we'll have to start documenting the PJSR, which is another important pending task, but that's a different story.

cheers, Zbynek

Offline kwiechen

  • PixInsight Addict
  • ***
  • Posts: 186
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #5 on: 2011 November 25 12:32:51 »

/*
The ImageWindow.keywords property is an array of FITSKeyword objects:

Array ImageWindow.keywords

The FITSKeyword object has name, value and comment String properties that you can read and write. It also has a constructor that allows you to set all of these properties at once:

new FITSKeyword( String name, String value[, String comment] )

Using the keywords property you can get and set FITS keywords for an image window before saving it. For example:

ImageWindow window = ImageWindow.activeWindow;
if ( !window.isNull )
   window.keywords.push( new FITSKeyword( "MYKWD", 123.456, "my private keyword" ) );


*/

I have tried for example this:

var img = ImageWindow.windowById(id);
var kw = new FITSKeyword( "MyKW", "MyValue");
img.keywords.push( kw  ) ;
img.saveAs(savePath, false, false, false, false);
img.forceClose();

The FITSKeyword object is correctly constructed. The resulting FITS file does not contain the FITSKeyword and trying to query img.keywords.length gives 0

What is going wrong here?

Best,

Kai


Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #6 on: 2011 November 26 01:39:08 »
Hi Kai,

The problem is here:

img.keywords.push( kw  ) ;

The ImageWindow.keywords property yields a *new* Array object that is a copy of the current list of FITS keywords in the image window. It does not give a reference to the current list. Hence, the above code is functionally equivalent to:

var a = img.keywords;
a.push( kw );


Note that this is a feature of the JavaScript language, not of the PJSR: properties are in general not mutable. The correct way to do what you want is this:

var a = img.keywords;
a.push( kw );
img.keywords = a;


Hope this helps.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline kwiechen

  • PixInsight Addict
  • ***
  • Posts: 186
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #7 on: 2011 November 28 11:42:50 »
Thank you very much - this does work! Sorry for these questions and problems - its my first try with javascript.

Best

Kai


Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: some questions (creating dir, writing FITS key)
« Reply #8 on: 2011 November 29 01:05:01 »
Kai, thank *you* for your interest in PI and in scripting on PI, and sorry for not having a complete documentation for PJSR. Hopefully we'll be able to fix this lack in a near future.

Please keep all the questions coming; I'll always be happy to answer everything related to PJSR and PCL development.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/