 |
Showing data as density with contour lines using VisAd
Source code name: "visad_2D_iso2.java"
Programming language: Java
Topic: Plots/Contour
DMelt Version 1.4. Last modified: 07/10/1971. License: Pro
https://datamelt.org/code/cache/visad_2D_iso2_360.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
Show isocontour over image
We have the function temperature = f(latitude, longitude)
represented by the MathType
( (latitude, longitude) -> elevation )
*/
public class visad_2D_iso2{
// 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_temp;
// 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;
// These objects are for drawing isocontours
private RealType isoTemperature;
private FunctionType func_domain_isoTemp;
private FlatField iso_vals_ff;
private DataReferenceImpl iso_data_ref;
public visad_2D_iso2(String []args)
throws RemoteException, VisADException {
// get 2D display
HVisAd c1=new HVisAd("2D example",false);
display = c1.getDisplay();
DisplayRenderer ren=c1.getRender();
ren.setBackgroundColor(Color.white);
display.getGraphicsModeControl().setLineWidth(1.0f);
// 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", SI.kelvin, null);
isoTemperature = RealType.getRealType("isoTemperature", SI.kelvin, null);
// Create a FunctionType (domain_tuple -> temperature )
// Use FunctionType(MathType domain, MathType range)
func_domain_temp = new FunctionType( domain_tuple, temperature);
// ... the same for isoTemperature
func_domain_isoTemp = new FunctionType( domain_tuple, isoTemperature);
// 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 the FlatFields
// Use FlatField(FunctionType type, Set domain_set)
// For the colored image
vals_ff = new FlatField( func_domain_temp, 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 );
// ...and for the isocontours
iso_vals_ff = new FlatField( func_domain_isoTemp, domain_set);
// Get the values from the temperature FlatField
// create flat_isoVals array for clarity's sake
// "false" argument means "don't copy"
float[][] flat_isoVals = vals_ff.getFloats(false);
// ...and put the values above into it
// Note the argument false, meaning that the array won't be copied again
iso_vals_ff.setSamples( flat_isoVals , 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 RGB and
// isoTemperature to IsoContour
// Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)
latMap = new ScalarMap( latitude, Display.YAxis );
lonMap = new ScalarMap( longitude, Display.XAxis );
latMap.getAxisScale().setColor(Color.black);
lonMap.getAxisScale().setColor(Color.black);
latMap.getAxisScale().setLabelSize(18);
lonMap.getAxisScale().setLabelSize(18);
//latMap.getAxisScale().setSnapToBox(true);
//lonMap.getAxisScale().setSnapToBox(true);
tempIsoMap = new ScalarMap( isoTemperature, 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);
// Create data references and set the FlatField as our data
data_ref = new DataReferenceImpl("data_ref");
iso_data_ref = new DataReferenceImpl("iso_data_ref");
data_ref.setData( vals_ff );
iso_data_ref.setData( iso_vals_ff );
// Add reference to display
display.addReference( data_ref );
display.addReference( iso_data_ref );
// show it
c1.visible(true);
}
public static void main(String[] args)
throws RemoteException, VisADException
{
new visad_2D_iso2(args);
}
}
You see the box below because you did not login.