Author Topic: How to generate mosaic from .fits containing multiple planes  (Read 5370 times)

Offline Ginge

  • PixInsight Addict
  • ***
  • Posts: 215
Hi all!

I need help mosaicing .fits containing 8 separate ccd image planes. There is an application called Mosaicator that will create mosaic-files from 4-plane .fits but it doesn't work with 8 image planes. Is there a way to do this quickly in PI? I have lots of files to process so an automated routine would be prefered.

Thanks,
Ginge

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to generate mosaic from .fits containing multiple planes
« Reply #1 on: 2010 October 15 03:30:30 »
Hi Ginge,

By mosaicing, do you mean generating a mosaic by image registration, or are the FITS planes already co-registered and you only have to compose them by just copying them in place over a larger canvas?

In the first case, you must use StarAlignment in mosaic mode. In the second case, this can be easily done with the NewImage and PixelMath tools.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Ginge

  • PixInsight Addict
  • ***
  • Posts: 215
Re: How to generate mosaic from .fits containing multiple planes
« Reply #2 on: 2010 October 15 03:41:41 »
It is an image composed by 8 fits generated from a 8 ccd array (2 rows of 4). They do not overlap at all so the  Pixelmath approach is the only way. What is the correct use of NewImage in this example? Could I also make a script that would convert a batch of MEF (Multi-Extension Fits)?

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to generate mosaic from .fits containing multiple planes
« Reply #3 on: 2010 October 15 07:34:48 »
Hi Ginge,

Quote
They do not overlap at all so the  Pixelmath approach is the only way.

If the CCD array has acquired all the 8 frames simultaneously, then there is no need to register the images. If this is not the case, then it will be necessary to register the images on a synthetic star field (StarGenerator). Another possibility would be creating the mosaic based on plate solves, something that PixInsight cannot do at present (unless you/somebody writes a script or a tool, which wouldn't be too difficult).

We assume also that there are no gaps between CCD elements. This is rare, but we'll make this assumption.

When you open a multiple-image FITS file, PixInsight loads each subimage as an independent image window. Then this is the sequence:

- Use the NewImage tool to generate the mosaic canvas. You should know the final dimensions of the mosaic. Just enter the desired width and height in pixels. I suppose the mosaic elements are grayscale images, so you should also select grayscale as the color space of the newly generated image. NewImage must be executed globally (blue circle icon, or F6).

- Now we can use PixelMath to place each mosaic element at its correct location. The expression looks a bit complicated but it is actually extremely simple; it just distributes all pixels over the canvas, each pixel at its corresponding coordinates. For simplification, let's assume that your eight mosaic frames have the following identifiers:

M1 M2 M3 M4
M5 M6 M7 M8

where I have distributed the elements in two rows and four columns, as you've said. Also let's suppose that the eight frames have exactly the same dimensions in pixels, and that there are no gaps between mosaic elements. Then the PixelMath expression is as folows:

row = y() % height( M1 );
col = x() % width( M1 );
iif( row == 0, iif( col == 0, M1, iif( col == 1, M2, iif( col == 2, M3, M4 ) ) ),
               iif( col == 0, M5, iif( col == 1, M6, iif( col == 2, M7, M8 ) ) ) )


EDITED: The above expression is wrong. See the correct solution to this problem two posts below.

row and col must be declared as symbols in PixelMath. You must also disable the rescale result option.

This will do the trick very easily if you only have a few images. If you have a lot, then the above procedure should be implemented as a batch process with a script. It is not difficult. You can open the images, assign the identifiers and build the PixelMath expressions dynamically from JavaScript code. This is the easiest way, and perhaps the most efficient one, as PixelMath is a parallelized process and runs much faster than the equivalent JavaScript code.

To open one of these multiple FITS files, you must use the ImageWindow.open static method:

Array ImageWindow.open( String url[, String id[, Boolean asCopy]] )

The returned array contains all the subimages exactly in the same order they appear in the disk file. For example, you can use:

var myImages = ImageWindow.open( "/path/to/the/image.fit", "M" );

and then myImages will contain all the subimages in "image.fit". The first image will be at myImages[0] and it will have the identifier "M", the second image will be myImages[1] and will have the identifier "M1", and so on. Building the PixelMath expression is a matter of concatenating a few strings. Put all this together with a file selection dialog (take BatchFormatConversion as an starting example), and you can generate an unlimited number of mosaics in a breeze.
« Last Edit: 2010 October 15 10:20:07 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Ginge

  • PixInsight Addict
  • ***
  • Posts: 215
Re: How to generate mosaic from .fits containing multiple planes
« Reply #4 on: 2010 October 15 09:11:45 »
Thank you very much for helping Juan. I've been downloading Unix type applications that are supposed to automate this kind of procedures but I really don't know squat about how to operate the Terminal.

I feel a bit embarassed that I haven't learned PixelMath but syntax-based processing just don't come easy to me. I've pasted this into the RGB/K expression field:

row = y() % height( M1 );
col = x() % width( M1 );
iif( row == 0, iif( col == 0, M1, iif( col == 1, M2, iif( col == 2, M3, M4 ) ) ),
               iif( col == 0, M5, iif( col == 1, M6, iif( col == 2, M7, M8 ) ) ) )

and written this in the symbols field:

row, col

Maybe this is where I've gone wrong. Is this the correct way of declaring row and col as symbols?
I've renamed the identifiers of the images to M1 through M8 in correct order, Unchecked Rescale result, I've tried creating a w:8184 h:8196 image (4x2046 by 2x4098) and also to check "create new image" with the same dimensions as above.

Whatever way I try the new image only contains the M8 image, stretched to 8184x8196. Any idea what I'm doing wrong?

Again, sorry for being slow, I really want to learn this 8)

Best regards,
Ginge

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to generate mosaic from .fits containing multiple planes
« Reply #5 on: 2010 October 15 10:16:51 »
Ginge,

Who is embarrassed is ME. The expression I wrote before is completely wrong --I don't know what I was thinking on when I wrote such a stupid thing (well, I was working on the documentation GUI for PI Core - it is evident that the documentation is hurting my programming skills severely  :laugh:)

This is the right thing:

h = height( M1 );
w = width( M1 );
row = trunc( y()/h );
col = trunc( x()/w );
x0 = x();
x1 = x0-w;
x2 = x1-w;
x3 = x2-w;
y0 = y();
y1 = y0-h;
iif( row == 0,
iif( col == 0, pixel( M1, x0, y0 ), iif( col == 1, pixel( M2, x1, y0 ), iif( col == 2, pixel( M3, x2, y0 ), pixel( M4, x3, y0 ) ) ) ),
iif( col == 0, pixel( M5, x0, y1 ), iif( col == 1, pixel( M6, x1, y1 ), iif( col == 2, pixel( M7, x2, y1 ), pixel( M8, x3, y1 ) ) ) ) )


and the symbols are (yes, you have to write them separated by commas in the Symbols slot):

h, w, row, col, x0, x1, x2, x3, y0, y1

I'm sorry it is a bit more complex than advertised... but it works fine; I've just checked it.

Another stupid thing I said before is that you need to create a canvas image with NewImage. That is not required at all. You can execute this PixelMath instance globally by just setting the parameters of the target image with the right dimensions. I have attached a screenshot of the PixelMath instance that I've just checked.

Hope this works for you now.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline vicent_peris

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 988
    • http://www.astrofoto.es/
Re: How to generate mosaic from .fits containing multiple planes
« Reply #6 on: 2010 October 15 10:19:16 »
Hi,

I supose the camera has 8 CCDs and you want to make a mosaic (even I would say this is the camera of the 2.2 meter scope at La Silla). This is not an easy case... and we haven't any routine to automatize these things. First, you will always have gaps between the CCDs, and each gap will be different from all the other gaps. Even you must expect some rotation of each CCD.

What kind of data do you have? It's a single image? You have an exposure set made with dithering?

You won't be able to construct a real mosaic if you don't have an exposure set with dithering between frames, because you won't have data to cover the gaps.


Regards,
Vicent.

Offline Ginge

  • PixInsight Addict
  • ***
  • Posts: 215
Re: How to generate mosaic from .fits containing multiple planes
« Reply #7 on: 2010 October 15 10:38:11 »
Thank you Juan! I'm glad it wasn't me 8). Thanks for taking the time in between everything. I'll try the expression as soon as I've made pizza for the kids.  ;D

Vincent: Yes It is data aquired via VirGO from the WFI on the 2,2 meter ESO scope. I have 34 mosaics for this particular object. I'm curious to see if it will be enough to average out the chequers in the ccd array. Have also downloaded other datasets with more subs in case this one doesn't work.

I didn't actually check that they dithered the exposures, kind of took it for granted as it would be the only way to integrate the MEFs. Time will show I guess. Maybe I should check before I mosaic the lot.

I wasn't aware that the only software available for handling MEFs required Terminal/Unix, on the other hand the archives of images aquired by arrayed ccd imagers might not have been open to the public for that long. There probably is no big need for it...

Might it be an idea for a functionality in PI StarAlignment? The FITS Header contains all the information that one needs.

Thanks!
Ginge

Offline Ginge

  • PixInsight Addict
  • ***
  • Posts: 215
Re: How to generate mosaic from .fits containing multiple planes
« Reply #8 on: 2010 October 15 14:01:16 »
It is working Juan, like you said. I didn't quite understand the scripting bit though. Will have to read it once more I think. I'll be converting manually for now.

Ginge

Offline Ginge

  • PixInsight Addict
  • ***
  • Posts: 215
Re: How to generate mosaic from .fits containing multiple planes
« Reply #9 on: 2010 October 15 17:33:27 »
I made a feeble attempt to translate this into a 2 plane mosaicator in the y axis. Failed miserably with this message:

"The iif function requires at least 3 argument(s)" after trying to strip your expression like this:


h = height( M1 );
row = trunc( y()/h );
col = trunc( x()/w );
x0 = x();
y0 = y();
y1 = y0-h;
iif( row == 0, iif( col == 0, pixel( M1, x0, y0 ), iif( col == 0, pixel( M5, x0, y1 ) ) ) )

Well, it was worth a try. What did I do wrong? I suppose I'm complicating things, I just want to add M2 under M1, but now I'm too tired to figure it out.

Best regards,
Ginge