
import cds.aladin.*;

public class CatalogCreationPlugV2 extends AladinPlugin {
   
   public String menu() { return "Catalog creation v2"; }
   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 50, 100, 150, 200 or 250";
   }   
   public String author()        { return "Pierre Fernique [CDS]"; }
   public String version()       { return "v2 - June 2009 - required Aladin v5.426"; }
   public String url() { return "http://aladin.u-strasbg.fr/java/Plugins/CatalogCreationPlugV2.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 source catalog plane for all image pixel fifty multiple
         AladinData cat = aladin.createAladinCatalog("MySources");
         
         // Header generation
         cat.setName(new String[]     { "X",     "Y",     "Pixel class", "Link"});
         cat.setDatatype(new String[] { "double","double","char",        "char"});
         cat.setWidth(new String[]    { "5"     ,"5"     ,"10"           ,"40" });
//       cat.setUcd(...);
//       cat.setUnit(...);
         
         // Source generation
         int n=0;
         double coo[]=null;
         for( int i=0; i<pix.length; i++ ) {
            int p = 0xFF & pix[i];
            if( p%50!=0 ) continue;
            n++;
            
            double x = (double) i%w;
            double y = (double) h - i/w;
            coo = sd.getCoord(x,y);
            String categorie = p==0 ? "A" : p==50 ? "B" : p==100 ? "C" : p==150 ? "D" : p==200 ? "E" : "F";
            
            cat.addSource("Src_"+n, coo[0],coo[1], new String[]{x+"",y+"",categorie,"http://aladin.u-strasbg.fr"});
         }
         
         // Removing some sources ("B" category)
         // Modifying some fields ("A" category with X>100 => "A bis")
//         Obj obj[] = cat.seeObj();
//         for( int i=0; i<cat.getNbObj(); i++ ) {
//            String [] values = obj[i].getValues();
//            if( values[2].equals("B") ) { cat.rmObj(obj[i]); i--; }
//            else if( values[2].equals("A") && Double.parseDouble(values[0])>100 ) obj[i].setValue(2, "A bis");
//         }
         
         // Creating of another table in the same source plane
//         cat.setName(new String[]{"Col1","Col2"});
//         cat.addSource("FooID",coo[0],coo[1],new String[]{"Foo1","Foo2"});
         
         // Notify Aladin that something has changed for this plane
         cat.objModified();
         
      } catch( Exception e ) { e.printStackTrace(); aladin.warning("Plugin error: "+e.getMessage()); }
   } 
}