
import java.io.ByteArrayInputStream;

import cds.aladin.*;

public class CatalogCreationPlug extends AladinPlugin {
   
   public String menu() { return "Catalog creation"; }
   public String description()   {
      return "PLUGIN TUTORIAL:\n" +
             "This plugin gives an example of a catalog creation.\n" +
             "It will scan the current image and create on the fly a catalog " +
             "corresponding to all pixel equals to 255.";
   }   
   public String author()        { return "Pierre Fernique [CDS]"; }
   public String version()       { return "1.1 - January 2007"; }
   public String url() { return "http://aladin.u-strasbg.fr/java/Plugins/CatalogCreationPlug.java"; }
   public String category()      { return "Plugin tutorial/Catalog"; }
   
   /**
    * Catalog creation by VOApp interface
    * 1) access to the byte pixel buffer of the current image (line inversed compareTo FITS)
    * 2) create on the fly a simple TSV catalog where ra,dec positions have pixel==255
    * 3) Ask Aladin to display this catalog via the VOApp.putVOTable() method
    */
   public void exec() {
      try {
         AladinData sd = aladin.getAladinImage();
         
         // Access to the Aladin byte pixel buffer
         int w = sd.getWidth();
         int h = sd.getHeight();
         byte [] pix = sd.seeBytePixels();
         
         // Creating a catalog plane via a simple
         // Tab-Separated-Valut format
         int n=0;
         StringBuffer s = new StringBuffer();
         for( int i=0; i<pix.length; i++ ) {
            int p = 0xFF & pix[i];
            if( p<255 ) continue;
            
            double x = (double) i%w;
            double y = (double) h - i/w;
            double coo[] = sd.getCoord(x,y);
            
            s.append( coo[0]+"\t"+coo[1]+"\t"+"obj "+(++n)+"\t"+p+"\n" );
         }
         
         // VOApp interface
         aladin.putVOTable(new ByteArrayInputStream( s.toString().getBytes() ), "MyCatalog");

      } catch( Exception e ) { aladin.warning("Plugin error: "+e.getMessage()); }
   } 
}