PixInsight Forum (historical)

Software Development => PCL and PJSR Development => Topic started by: Mike Reid on 2011 December 17 10:40:06

Title: PJSR: ImageWindow open and close
Post by: Mike Reid on 2011 December 17 10:40:06
I've been working on a script that loops through opening and closing a large number of files.  It kept crashing PI and I wasn't sure why.  I reduced the script to just a loop that opens and closes files and does nothing else, like this,

  this.testLoop = function(files) {
    for ( var i = 0; i < files.length; ++i ) {
      var w = ImageWindow.open( files );
      w.close;
    }
  }

If I monitor this while running I see that the memory usage of PI just keeps increasing.  No memory ever gets freed by the close.

What am I doing wrong?

Thanks,
Mike
Title: Re: PJSR: ImageWindow open and close
Post by: Enzo De Bernardini on 2011 December 17 11:00:27
Hello Mike,

Try...

Code: [Select]
w.close();
(notice the parentheses () at the end)

Or if the image has been modified...

Code: [Select]
w.forceClose();
Hope this help,

Enzo.
Title: Re: PJSR: ImageWindow open and close
Post by: Mike Reid on 2011 December 19 08:52:03
Thanks for the reply Enzo but unfortunately when I try close() I get this error,

  TypeError: w.close is not a function

Same with forceClose().

Thanks,
Mike

Title: Re: PJSR: ImageWindow open and close
Post by: Juan Conejero on 2011 December 19 15:32:33
Hi Mike,

Code: [Select]
   var w = ImageWindow.open( files );
   w.close();

ImageWindow.open() returns an *array* of ImageWindow objects. This is because several file formats support multiple images stored in a single file (e.g. the FITS format). So 'w.close()' doesn't work because close() is not a method of the Array object.

This code should work:

Code: [Select]
   var w = ImageWindow.open( "/path/to/my/file.fit" );
   for ( var i in w )
      w[i].close();

Of course this is just an example---opening something to close it immediately isn't a very useful operation. This is a more elaborated example:

Code: [Select]
   var w = ImageWindow.open( "/path/to/my/file.fit" );
   for ( var i in w )
   {
      w[i].show();
      w[i].zoomToOptimalFit();
   }

As Enzo has pointed out, sometimes one wants to close an image unconditionally, even if the image has been modified. In these cases ImageWindow.forceClose() closes an image window immediately without further questions.

Hope this helps
Title: Re: PJSR: ImageWindow open and close
Post by: Mike Reid on 2011 December 20 08:03:46
That fixed it, thanks Juan!

I wrote a script that sorts through a directory full of calibration frames, looks at the fits header, sorts them in memory by type (bias, dark, flat), filter, exposure time and temperature and builds master calibration frames.  It crashed my linux box when I ran it on a directory with a couple of hundred 6 megapixel images because I was not closing the images correctly.

Mike
Title: Re: PJSR: ImageWindow open and close
Post by: sleshin on 2011 December 20 14:56:39
Hi Mike,

Sounds like a great script. Hope you'll share it with the rest of us. Love to give it a try.

Have a great Holiday.

Steve