19991202: Understanding VisAD

>From: Doug Lindholm <lind@xxxxxxxx>
>Organization: NCAR
>Keywords: 199912021946.MAA18812

I should probably stay out of this, but...

Doug Linholm wrote:
>I agree that some convention is useful but I prefer something a bit more
>readable. I try to use the following suffixes:
>
>_type for RealTypes or Tuples
>_ftype for FunctionTypes
>_field for FieldImpls
>_ref for DataReferences
>_set for Sets
>_samples for primative arrays

I agree, but try to avoid using underscores and name variables
things like latlonType, latlonSet, latlonDomainSet - more in line with
the coding conventions employed in Java itself. See:

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367

Where I vary from Sun is that they say variable names should be short. I
find descriptive names like verticalDomainSet more useful than zDomSet
(and I use an indent of 4 not 2!). 

Of course this is a religious war as Steve Emmerson pointed out to me. I
think the main point James is trying to get across is that the examples
would be easier to use if the names of variables related more to their
use. At least they are not i, ii, and iii, like some code I've had to
decipher in the past. ;-) I've taken the bull by the horns and redone
SatDisplay.java in the examples directory (since I wrote it originally
hacking from other people's code before I took on some standards). A new
version is attached and the repository will eventually be updated with
this.  It will not please everyone, but maybe it will help some.

Of course, javadoc is always a good thing. ;-)

Don
*************************************************************
Don Murray                               UCAR Unidata Program
dmurray@xxxxxxxxxxxxxxxx                        P.O. Box 3000
(303) 497-8628                              Boulder, CO 80307
*************************************************************
Unidata WWW Server               http://www.unidata.ucar.edu/
McIDAS Demonstration Machine  http://mcdemo.unidata.ucar.edu/
*************************************************************
/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 1999 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

//
// SatDisplay.java by Don Murray of Unidata
//

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
import visad.ColorControl;
import visad.CoordinateSystem;
import visad.ConstantMap;
import visad.Data;
import visad.DataReference;
import visad.DataReferenceImpl;
import visad.Display;
import visad.DisplayImpl;
import visad.FieldImpl;
import visad.FlatField;
import visad.FunctionType;
import visad.Linear2DSet;
import visad.RealTupleType;
import visad.RealType;
import visad.ScalarMap;
import visad.data.mcidas.AreaAdapter;
import visad.data.mcidas.BaseMapAdapter;
import visad.java3d.DisplayImplJ3D;
import visad.java3d.TwoDDisplayRendererJ3D;

/**
 * Example class to display a satellite image in VisAD.
 *
 * @author  Don Murray - Unidata
 */
public class SatDisplay 
{

    private DisplayImpl display;

    /**
     * Construct a satellite display using the specified McIDAS map file and
     * image source.  The image can be displayed on a 3D globe or on a
     * flat rectillinear projection.  It can also be remapped/subsampled
     * to values over North America
     *
     * @param  mapFile      location of the McIDAS map file (path or URL)
     * @param  imageSource  location of the image source (path or URL)
     * @param  display3D    if true, use 3D display, otherwise flat rectillinear
     * @param  remap        remap the image into a domain over North America
     */
    public SatDisplay(String mapFile, String imageSource,
                      boolean display3D, boolean remap) 
    {
        try 
        {
            //  Read in the map file
            BaseMapAdapter baseMapAdapter;
            if (mapFile.indexOf("://") > 0)   // URL specified
            {
               baseMapAdapter = new BaseMapAdapter(new URL(mapFile) );
            } 
            else   // local disk file
            {
               baseMapAdapter = new BaseMapAdapter(mapFile);
            }

            // Create the display and set up the scalar maps to map
            // data to the display 
            ScalarMap latMap;     // latitude  -> YAxis
            ScalarMap lonMap;     // longitude -> XAxis
            if (display3D) 
            {
                display = new DisplayImplJ3D("display");
                latMap = new ScalarMap(RealType.Latitude, Display.Latitude);
                lonMap = new ScalarMap(RealType.Longitude, Display.Longitude);
            }
            else
            {
                display = new DisplayImplJ3D("display",
                                               new TwoDDisplayRendererJ3D());
                latMap = new ScalarMap(RealType.Latitude, Display.YAxis);
                lonMap = new ScalarMap(RealType.Longitude, Display.XAxis);
            }
            display.addMap(latMap);
            display.addMap(lonMap);

            // set the display to a global scale
            latMap.setRange(-90.0, 90.0);
            lonMap.setRange(-180.0, 180.0);
  
            // create a reference for the map line
            DataReference maplinesRef = new DataReferenceImpl("MapLines");
            maplinesRef.setData(baseMapAdapter.getData());
        
            // set the attributes of the map lines (color, location)
            ConstantMap[] maplinesConstantMap = new ConstantMap[4];
            maplinesConstantMap[0] = new ConstantMap(0., Display.Blue);
            maplinesConstantMap[1] = new ConstantMap(1., Display.Red);
            maplinesConstantMap[2] = new ConstantMap(0., Display.Green);
            maplinesConstantMap[3] = 
                new ConstantMap(1.001, Display.Radius); // just above the image

            // read in the image
            AreaAdapter areaAdapter = new AreaAdapter(imageSource);
            FlatField image = areaAdapter.getData();

            // Extract the metadata from the image
            FunctionType imageFunctionType = 
                (FunctionType) image.getType();
            RealTupleType imageDomainType = imageFunctionType.getDomain();
            RealTupleType imageRangeType = 
                (RealTupleType) imageFunctionType.getRange();

            // remap and resample the image
            if (remap) 
            {
                int SIZE = 256;
                RealTupleType latlonType
                  ((CoordinateSystem) 
                      imageDomainType.getCoordinateSystem()).getReference();
                Linear2DSet remapDomainSet = 
                    new Linear2DSet(
                        latlonType, -4.0, 70.0, SIZE, -150.0, 5.0, SIZE);
                image = 
                    (FlatField) image.resample(
                        remapDomainSet, Data.NEAREST_NEIGHBOR, Data.NO_ERRORS);
            }

            // select which band to show...
            ScalarMap rgbMap = 
                new ScalarMap( 
                    (RealType) imageRangeType.getComponent(0), Display.RGB);
            display.addMap(rgbMap);

            // set the enhancement to a grey scale
            ColorControl colorControl = (ColorControl) rgbMap.getControl();
            colorControl.initGreyWedge();

            // create a data reference for the image
            DataReferenceImpl imageRef = new DataReferenceImpl("ImageRef");
            imageRef.setData(image);

            // add the data references to the display
            display.disableAction();
            display.addReference(imageRef,null);
            display.addReference(maplinesRef, maplinesConstantMap);
            display.enableAction();
        } 
        catch (Exception ne) 
        {
            ne.printStackTrace(); System.exit(1); 
        }

    }

    /**
     * <UL>
     * <LI>run 'java -mx64m SatDisplay' for globe display
     * <LI>run 'java -mx64m SatDisplay X remap' for remapped globe display
     * <LI>run 'java -mx64m SatDisplay X 2D' for flat display
     * </UL>
     */
    public static void main (String[] args) {

        String mapFile = "ftp://www.ssec.wisc.edu/pub/visad-2.0/OUTLSUPW";;
        String imageSource = "ftp://www.ssec.wisc.edu/pub/visad-2.0/AREA2001";;
        boolean use3D = true;
        boolean remap = false;

        JFrame frame = new JFrame("Satellite Display");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        if (args.length > 0 && !args[0].equals("X")) {
           imageSource = args[0];
           // mapFile = args[0];
        }
        if (args.length == 2) {
           use3D = (args[1].indexOf("2") >= 0) ? false : true;
           remap = (args[1].indexOf("2") >= 0) ? false : true;
        }

        SatDisplay satDisplay = 
            new SatDisplay(mapFile, imageSource, use3D, remap);
        frame.getContentPane().add(satDisplay.display.getComponent());
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}