import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import visad.*;
import visad.java3d.DisplayImplJ3D;

public class Velocity {
   RealType lon, lat, u1, v1;
   RealTupleType domain, velocity;
   FunctionType function_type;
   Integer2DSet domain_set;
   FlatField field;
   FlowControl controlFlow;
   DisplayImplJ3D display;
   DataReference ref;

   Velocity(String[] args) throws RemoteException, VisADException {
      int rows = 337, cols = 468;
      float[][] vx = new float[rows][cols];
      float[][] vy = new float[rows][cols];
      lon = new RealType("lon");
      lat = new RealType("lat");
      u1 = new RealType("u1");
      v1 = new RealType("v1");
      domain = new RealTupleType(lat, lon);
      velocity = new RealTupleType(u1, v1);
      function_type = new FunctionType(domain, velocity);
      domain_set = new Integer2DSet(rows, cols);
      field = new FlatField(function_type, domain_set);
      float[][] field_data = new float[1][cols * rows];

//      field = FlatField.makeField(function_type, 32, false);
      
      try {
         FileInputStream fileInVx = new
FileInputStream("h123_u1_1983_1step.d");
         FileInputStream fileInVy = new
FileInputStream("h123_v1_1983_1step.d");
         BufferedInputStream buffInVx = new
BufferedInputStream(fileInVx);
         BufferedInputStream buffInVy = new
BufferedInputStream(fileInVy);
         DataInputStream dataVx = new DataInputStream(buffInVx);
         DataInputStream dataVy = new DataInputStream(buffInVy);
         for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++) {
                vx[i][j] = dataVx.readFloat();
                vy[i][j] = dataVy.readFloat();
                }
      }
      catch (IOException e) {
         System.out.println("Error: " + e.toString());
      }

/*  I tried to set samples here, but failed.    
      int k = 0;
      for (int j = 0; j < cols; j++)
         for (int i = 0; i < rows; i++) {
            field_data[0][k] = vx[i][j];
            k++;
            field_data[0][k] = vy[i][j];
            k++;
         }
     for (int j = 0; j < cols; j++)
         for (int i = 0; i < rows; i++) {         
            field_data[0][k] = vy[i][j];
            k++;
         }      
      
      field.setSamples(field_data);

*/
         
      display = new DisplayImplJ3D("velocity display");
      display.addMap(new ScalarMap(lon, Display.XAxis));
      display.addMap(new ScalarMap(lat, Display.YAxis));
      ScalarMap flow = new ScalarMap(u1, Display.Flow1X);
      display.addMap(flow);
      display.addMap(new ScalarMap(v1, Display.Flow1Y));

      controlFlow = (FlowControl) flow.getControl();
      controlFlow.setFlowScale(0.06f);
                
      ref = new DataReferenceImpl("thickness data");
      ref.setData(field);
      display.addReference(ref);

      JFrame jframe = new JFrame("Northeast Pacific Data");
       
      jframe.getContentPane().add(display.getComponent());

      jframe.setSize(300, 300);
      jframe.setVisible(true);
      
      WindowListener l = new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      };
      
      jframe.addWindowListener(l);
   }
      
   public static void main(String[] arguments) throws RemoteException, 
      VisADException {
      new Velocity(arguments);   
   } 
}
