Author Topic: A bit of help again with PJSR  (Read 5908 times)

Offline Ioannis Ioannou

  • PixInsight Addict
  • ***
  • Posts: 202
A bit of help again with PJSR
« on: 2011 June 16 00:13:12 »
Triggered from this http://pixinsight.com/forum/index.php?topic=3139.0 topic and a lot of cloudy nights, I tried to expand my PJSR knowledge for fun. But I need a few hints.
I took the latest script from here : http://pixinsight.com/forum/index.php?topic=806.15 and modified it based on DrawSignature.js in order to add the instance triangle, so that the script can be used with ImageContainer. Questions:

1) If I apply the triangle directly from the GUI to an image it complains that I'm trying to apply a script instance recursively. I have to drop an instance to the desktop , close the GUI and apply the icon on the image. Why this is happening and how to avoid it ?

2) I used Parameters.set( "columns", columns );  (same for rows) to export the arrays and this.columns= Parameters.getString( "columns" ); to get them back. If I open the saved instance I can see that my "columns" is indeed the array (let say "45,33"). If I put a debugging console.writeln I'm getting the same after getString. But if I pass the array to the function that does the job it thinks that it has to operate to e.g. columns 4 and 5 (instead of 45 and I have no idea what happened to 33). I used this.columns.push(Parameters.getString( "columns" ) but this was worse, all of the array passed as a single string, commas included. So, how I'm supposed to export/import arrays? BTW the same code works fine if the parameters are directly passed from the GUI instead via an instance.

3) Not directly related to PJSR. I think there was a logical bug in the original script (functions FixColumn and FixRow) that -for unknown reasons to me- did not made the script complaining before. These functions iterate from position 0 to < image.height (or width). But they also use col-1 and col+1 in order to average the defected column with the ones around. At the limits (0 and image.height) these calculations have no meaning and are out of array range, right? I'm getting errors, while the  "OK" button works without checking. And also, that means that the top-bottom elements can not be really fixed, right ? Any hints?

4) At line 215 the code for the GUI dialog assumed that you already know the max height and width (in order to imply specific limits to the dialog) since the script originally was based on current image. But that means that I need one view open even to start it. Ideas how to make this more "smart"?

Thanks in advance
« Last Edit: 2011 June 16 06:38:29 by Ioannis Ioannou »
Clear Skies
John (Ioannis)

FSQ106N+Robofocus+QHY-22+SX USB wheel+Baader filters
SX OAG+DSI Pro guiding a NEQ6
PI for the rest :)

Offline Ioannis Ioannou

  • PixInsight Addict
  • ***
  • Posts: 202
Re: A bit of help again with PJSR
« Reply #1 on: 2011 June 16 06:38:01 »
Never mind no 3, my mistake - I think.
Clear Skies
John (Ioannis)

FSQ106N+Robofocus+QHY-22+SX USB wheel+Baader filters
SX OAG+DSI Pro guiding a NEQ6
PI for the rest :)

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: A bit of help again with PJSR
« Reply #2 on: 2011 June 18 11:54:20 »
Hi John,

Sorry for the late answer.

Quote
1) If I apply the triangle directly from the GUI to an image it complains that I'm trying to apply a script instance recursively. I have to drop an instance to the desktop , close the GUI and apply the icon on the image. Why this is happening and how to avoid it ?

This is a limitation of the scripting engine; you cannot avoid it. If you have to process an image, you must do that directly from your script, but you cannot rely on generating a script instance and applying it to an image while your script is still running. Note that this would require executing your script recursively, which is not allowed. The main reason is that the scripting engine is not guaranteed to be fully reentrant.

So you have to create an icon, terminate your script and apply it. This is the correct procedure.

Quote
2) I used Parameters.set( "columns", columns );  (same for rows) to export the arrays and this.columns= Parameters.getString( "columns" ); to get them back. If I open the saved instance I can see that my "columns" is indeed the array (let say "45,33"). If I put a debugging console.writeln I'm getting the same after getString. But if I pass the array to the function that does the job it thinks that it has to operate to e.g. columns 4 and 5 (instead of 45 and I have no idea what happened to 33). I used this.columns.push(Parameters.getString( "columns" ) but this was worse, all of the array passed as a single string, commas included. So, how I'm supposed to export/import arrays? BTW the same code works fine if the parameters are directly passed from the GUI instead via an instance.

Bear in mind that for security reasons, all script parameters are always exported as strings. In this way we prevent the possibility of a malicious script to inject JS code directly in the scripting engine (i.e., parameters cannot be used to execute anything).

So when your script does this:

  var columns = new Array;
   ...
   Parameters.set( "columns", columns );


what it is doing is actually:

  var columns = new Array;
   ...
   Parameters.set( "columns", columns.toString() );


irrespective of whether you call the toString method explicitly or not —it is invoked implicitly by internal PJSR routines. Now you know why when you retrieve the 'columns' parameter with:

  this.columns = Parameters.getString( "columns" );

what you get is a String object, rather than an Array object. This is obviously not what you want, so you must devise a way to convert the string back into your original array elements. This is actually quite easy to do with the String.split method:

  Array String.split( [String separator[, uint limit]] )

For example:

Code: [Select]
  var stringList = Parameters.getString( "columns" ).split( ',' );
   for ( var i in stringList )
      columns.push( parseInt( stringList[i] ) );

In this way you get your columns array with its original numerical values.

Quote
4) At line 215 the code for the GUI dialog assumed that you already know the max height and width (in order to imply specific limits to the dialog) since the script originally was based on current image. But that means that I need one view open even to start it. Ideas how to make this more "smart"?

Well, obviously to run this script you need an image (or a set of images, which you are going to process one at a time). Then since you have a target image you already know its geometry (Image.width, Image.height and so on).

You get an image either from a view (that is, an already open image) or by creating a new ImageWindow object; for example:

  var w = ImageWindow.open( "path/to/an/image.fit" );

With the above call you get an array of ImageWindow objects, one for each image in the image file (usually the array w will contain just a single ImageWindow object, unless you load a multiple-image FITS file). Note that the ImageWindow objects won't be visible unless you explicitly call ImageWindow.show().

Since version 1.7.0, PJSR supports direct access to file format support modules through the FileFormat and FileFormatInstance JavaScript objects (sorry, I haven't documented them yet, I haven't had the time). So you no longer need ImageWindow to open and load images, just as happens with PCL C++ modules. I'm going to document these new objects and features as soon as possible. For now, here's a brief but informative example:

Code: [Select]
     try
      {
         // Find a format able to read FITS files
         var fitsFormat = new FileFormat( ".fits", true );

         // Create a format instance
         var myFITSFile = new FileFormatInstance( fitsFormat );

         // Use the instance to open an existing file
         myFITSFile.open( "/home/images/test.fit" );

         // Read an image in 32-bit floating point format
         var img = Image.newFloatImage();
         var readOk = myFITSFile.readImage( img );

         // Close the image stream
         myFITSFile.close();

         if ( readOk )
         {
            // ... do your stuff on img here
         }
         else
         {
            throw Error( "Unable to read image." );
         }
      }
      catch ( x )
      {
         // Handle errors here ...
      }

Hope this helps. Please don't hesitate to ask here, should you need more information.
« Last Edit: 2011 June 18 12:00:11 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Ioannis Ioannou

  • PixInsight Addict
  • ***
  • Posts: 202
Re: A bit of help again with PJSR
« Reply #3 on: 2011 June 18 12:15:56 »
So you have to create an icon, terminate your script and apply it. This is the correct procedure.

OK, got it

Quote
For example:

Code: [Select]
  var stringList = Parameters.getString( "columns" ).split( ',' );
   for ( var i in stringList )
      columns.push( parseInt( stringList[i] ) );

In this way you get your columns array with its original numerical values.

Thank you. Actually I tried split but with incorrect syntax. And I did not thought about parseInt - I'm getting old  ::)

Clear Skies
John (Ioannis)

FSQ106N+Robofocus+QHY-22+SX USB wheel+Baader filters
SX OAG+DSI Pro guiding a NEQ6
PI for the rest :)