Desnsity 2D plot using VisAd
Source code name: "visad_2D_iso.java"
Programming language: Java
Topic: Plots/Contour
DMelt Version 1.9. Last modified: 07/08/2017. License: Free
https://datamelt.org/code/cache/visad_2D_iso_396.java
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


/*
VisAD Tutorial
Copyright (C) 2000 Ugo Taddei
*/


// Import needed classes

import visad.*;
import visad.util.*;
import visad.java2d.DisplayImplJ2D;
import java.rmi.RemoteException;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import jhplot.*;

  /**
  VisAD Tutorial example with denser contours
  We have the function temperature = f(latitude, longitude)
  represented by the MathType
  ( (latitude, longitude) -> elevation )
  Use ContourControl
 */

public class visad_2D_iso{

// Declare variables
  // The domain quantities longitude and latitude
  // and the dependent quantity temperature

  private RealType longitude, latitude;
  private RealType temperature;

  // Tuple to pack longitude and latitude together, as the domain
  private RealTupleType domain_tuple;


  // The function (domain_tuple -> temperature )
  // Remeber, range is only "temperature"
  private FunctionType func_domain_range;


   // Our Data values for the domain are represented by the Set
  private Set domain_set;


  // The Data class FlatField
  private FlatField vals_ff;

  // The DataReference from data to display
  private DataReferenceImpl data_ref;

  // The 2D display, and its the maps
  private DisplayImpl display;
  private ScalarMap latMap, lonMap;
  private ScalarMap tempIsoMap, tempRGBMap;


  public visad_2D_iso(String []args)
    throws RemoteException, VisADException {


    // get 2D display
    HVisAd c1=new HVisAd("2D example",false);
    display = c1.getDisplay();


    // Create the quantities
    // Use RealType(String name);
    latitude = RealType.getRealType("latitude");
    longitude = RealType.getRealType("longitude");

    domain_tuple = new RealTupleType(latitude, longitude);

    temperature = RealType.getRealType("temperature");

    // Create a FunctionType (domain_tuple -> temperature )
    // Use FunctionType(MathType domain, MathType range)
    func_domain_range = new FunctionType( domain_tuple, temperature);

    // Create the domain Set
    // Use LinearDSet(MathType type, double first1, double last1, int lengthX,
    //				     double first2, double last2, int lengthY)

    int NCOLS = 50;
    int NROWS = NCOLS;

    domain_set = new Linear2DSet(domain_tuple, -Math.PI, Math.PI, NROWS,
    					       -Math.PI, Math.PI, NCOLS);



    // Get the Set samples to facilitate the calculations
    float[][] set_samples = domain_set.getSamples( true );


    // The actual temperature values are stored in this array
    // float[1][ number_of_samples ]
    float[][] flat_samples = new float[1][NCOLS * NROWS];

    // We fill our 'flat' array with the generated values
    // by looping over NCOLS and NROWS
    for(int c = 0; c < NCOLS; c++)

      for(int r = 0; r < NROWS; r++){

	// ...temperature
	flat_samples[0][ c * NROWS + r ] = (float)( (Math.sin( 0.50*(double) set_samples[0][ c * NROWS + r ])  ) * Math.cos( (double) set_samples[1][ c * NROWS + r ] ) ) ;


    }

    // Create a FlatField
    // Use FlatField(FunctionType type, Set domain_set)
    vals_ff = new FlatField( func_domain_range, domain_set);

    // ...and put the values above into it
    // Note the argument false, meaning that the array won't be copied
    vals_ff.setSamples( flat_samples , false );

    // Create Display and its maps
    // Get display's graphics mode control and draw scales
    GraphicsModeControl dispGMC = (GraphicsModeControl)  display.getGraphicsModeControl();
    dispGMC.setScaleEnable(true);


    // Create the ScalarMaps: latitude to YAxis, longitude to XAxis and
    // temperature to IsoContour and eventualy to RGB
    // Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)
    latMap = new ScalarMap( latitude,    Display.YAxis );
    lonMap = new ScalarMap( longitude, Display.XAxis );
    tempIsoMap = new ScalarMap( temperature,  Display.IsoContour );
    tempRGBMap = new ScalarMap( temperature,  Display.RGB );

    // Add maps to display
    display.addMap( latMap );
    display.addMap( lonMap );
    display.addMap( tempIsoMap );
    display.addMap( tempRGBMap );

    // The ContourControl
    // Note that we get the control from the IsoContour map
    ContourControl isoControl = (ContourControl) tempIsoMap.getControl();

    // Define some parameters for contour lines

    float interval = 0.1250f;  // interval between lines
    float lowValue = -0.50f;   // lowest value
    float highValue = 1.0f;    // highest value
    float base = -1.0f;        //  starting at this base value

    // ...and set the lines with the method
    isoControl.setContourInterval(interval, lowValue, highValue, base);
    isoControl.enableLabels(true);

    // why not try to fill the contours? uncomment as needed
    // note: labels won't be draw and there must be the maps
    // someRealType -> IsoContour AND
    // someRealType -> RGB
    //isoControl.setContourFill(true);

    // Create a data reference and set the FlatField as our data
    data_ref = new DataReferenceImpl("data_ref");
    data_ref.setData( vals_ff );

    // Add reference to display
    display.addReference( data_ref );


    // show it
    c1.visible(true);

  }


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

} //end of Visad Tutorial Program 3_06