Author Topic: PJSR: ImageWindow open and close  (Read 4897 times)

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
PJSR: ImageWindow open and close
« 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

Offline Enzo De Bernardini

  • PTeam Member
  • PixInsight Addict
  • ***
  • Posts: 274
  • Resistance is futile.
    • Astronomí­a Sur
Re: PJSR: ImageWindow open and close
« Reply #1 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.

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
Re: PJSR: ImageWindow open and close
« Reply #2 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


Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR: ImageWindow open and close
« Reply #3 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
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Mike Reid

  • Newcomer
  • Posts: 47
    • Mike's Astro
Re: PJSR: ImageWindow open and close
« Reply #4 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

Offline sleshin

  • PixInsight Old Hand
  • ****
  • Posts: 431
Re: PJSR: ImageWindow open and close
« Reply #5 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
Steve Leshin

Stargazer Observatory
Sedona, Arizona