Re: Program to visualize layers

Bill says some people might have trouble reading attachments; so here is the 
layer program.

-- mohamad

//
// Simple VisAD Test program for visualizing surfaces given by (X, Y) --> (Z, 
Propertry).
// M. Ijadi
// Los Alamos National Laboratory
// 3/23/1999
//

import java.util.*;
import java.io.*;
import java.rmi.RemoteException;

import visad.*;
import visad.java3d.DisplayImplJ3D;
import visad.util.ContourWidget;
import java.awt.Component;

public class PropertyTest2
 extends UISkeleton
{
  ContourWidget cw = null;
  Component getSpecialComponent() { return cw; }

  public PropertyTest2() { }

  public PropertyTest2(String args[])
 throws VisADException, RemoteException
  {
    super(args);
  }

  DisplayImpl[] setupData()
 throws VisADException, RemoteException
  {
    RealType[] domain2d = {RealType.XAxis, RealType.YAxis};
    RealTupleType earth_location = new RealTupleType(domain2d);
    RealType depth = new RealType("depth", null, null);
    RealType property = new RealType("property", null, null);
    RealType[] range2d = {depth, property};
    RealTupleType property_desc = new RealTupleType(range2d);
    FunctionType property_tuple = new FunctionType(earth_location, 
property_desc);

    FlatField[] layers = importProperty(property_tuple, ".", 
"F1_PHIE_021899.txt");

    DisplayImpl display1;
    display1 = new DisplayImplJ3D("display1", DisplayImplJ3D.APPLETFRAME);
    ScalarMap xMap = new ScalarMap(RealType.XAxis, Display.XAxis);
    ScalarMap yMap = new ScalarMap(RealType.YAxis, Display.YAxis);
    ScalarMap zMap = new ScalarMap(depth, Display.ZAxis);
    ScalarMap propertyColorMap = new ScalarMap(property, Display.Green);
    ScalarMap isoMap = new ScalarMap(property, Display.IsoContour);

    display1.addMap(xMap);
    display1.addMap(yMap);
    display1.addMap(zMap);
    display1.addMap(isoMap);
    cw = new ContourWidget(isoMap);
    display1.addMap(propertyColorMap);
    //display1.addMap(new ConstantMap(0.5, Display.Alpha));
    display1.addMap(new ConstantMap(0.5, Display.Blue));
    display1.addMap(new ConstantMap(0.5, Display.Red));
    GraphicsModeControl mode = display1.getGraphicsModeControl();
    mode.setScaleEnable(true);

    for (int i=0; i < layers.length; i++) {
      DataReferenceImpl ref_layer = new DataReferenceImpl("ref_layer"+i);
      ref_layer.setData(layers[i]);
      display1.addReference(ref_layer, null);
    }

    DisplayImpl[] dpys = new DisplayImpl[1];
    dpys[0] = display1;

    return dpys;
  }

  public String toString() { return ": 2-D regular surfaces for layers"; }

  public static void main(String args[])
 throws VisADException, RemoteException
  {
    PropertyTest2 t = new PropertyTest2(args);
  }


  public static FlatField[] importProperty(FunctionType type, String dir, 
String name)
          throws VisADException, RemoteException
  {
    // Parses file containing one or more layers and creates FlatFields using
    // Linear2DSets for them.  Expected format of the file:
    // First line indicates grid information like:
    //   XGridSize YGridSize NumberOfLayers XOrig YOrig XLength YLength
    // Then each layer is given by rows of x y z prop values (x changes first 
then y).
    //
    File inputFile;
    FileReader fileReader;
    BufferedReader data;
    String line;
    StringTokenizer tokens;
    int i, j, k;

    try {
        System.out.println("Parsing data file " + name + "...");
        inputFile = new File(dir, name);
        fileReader = new FileReader(inputFile);
        data = new BufferedReader(fileReader);
    } catch(FileNotFoundException ex) {
        System.out.println("    " + ex);
        return null;
    }

    try {
        line = data.readLine();
        tokens = new StringTokenizer(line);
        int xGridSize = (Integer.valueOf(tokens.nextToken())).intValue();
        int yGridSize = Integer.valueOf(tokens.nextToken()).intValue();
        int numLayers = Integer.valueOf(tokens.nextToken()).intValue();
        Float xOrig = Float.valueOf(tokens.nextToken());
        Float yOrig = Float.valueOf(tokens.nextToken());
        // NOTE: get grid steps like this?
        Float width = Float.valueOf(tokens.nextToken());
        Float height = Float.valueOf(tokens.nextToken());
        Float xGridStep = new Float(width.floatValue() / xGridSize);
        Float yGridStep = new Float(height.floatValue() / yGridSize);

        // Setup VisAD stuff
        double first1 = xOrig.doubleValue();
        int length1 = xGridSize;
        double last1 = first1 + width.doubleValue();
        double first2 = yOrig.doubleValue();
        int length2 = yGridSize;;
        double last2 = first2 + height.doubleValue();
        double step1 = (last1 - first1) / (length1 - 1);
        double step2 = (last2 - first2) / (length2 - 1);
        step1 = xGridSize;
        step2 = yGridSize;

        float x, y; double z;
        double p;
        FlatField[] images = new FlatField[numLayers];

        for (k=0; k < numLayers; k++) {
          double[][] dataArr = new double[2][xGridSize*yGridSize];
          for (j=0; j < yGridSize; j++) {
            for (i=0; i < xGridSize; i++) {
                line = data.readLine();
                tokens = new StringTokenizer(line);
                x = Float.valueOf(tokens.nextToken()).floatValue();
                y = Float.valueOf(tokens.nextToken()).floatValue();
                z = Float.valueOf(tokens.nextToken()).doubleValue();
                p = Float.valueOf(tokens.nextToken()).doubleValue();
                dataArr[0][j*xGridSize + i] = z;
                dataArr[1][j*xGridSize + i] = p;
            }
          }
          Linear2DSet domain_set = new Linear2DSet(type.getDomain(), first1, 
last1, length1,
                                                   first2, last2, length2);
          FlatField image = new FlatField(type, domain_set);
          image.setSamples(dataArr, false);  // no need for VisAD to copy our 
data
          System.out.println("Done Creating field data for layer " + k);
          images[k] = image;
        }

        return images;

    } catch (NoSuchElementException ex) {
        System.out.println("    Bad layer data format");
    } catch(IOException e){
        System.out.println("    Error reading input file: " + e);
    }
    return null;
  }


}