DMelt:Plots/VisAd
VisAd
DataMelt includes VisAd, which was destined by programmers at the University of Wisconsin Space Science and Engineering Center (SSEC). We reproduce its guide in VisAd guide section
The Java API is given by visad package.
VisAd tutorial
Here is a short tutorial how to visualize numerical information using interactive canvas called jhplot.HVisAd. We will show step-by step example, starting from elementary operation, such as opening the canvas and creating a 3D scene.
from jhplot import * from java.awt import * c1 = HVisAd("VisAd",600,600) # make 600x600 canvas display = c1.getDisplay() # get display ren=c1.getRender() # get display render ren.setBoxOn(True) # draw a box ren.setBackgroundColor(Color.white) # make it white c1.visible() # show it
Next, we will try to manipulate with the box. For example, we can zooming (or zoom out), move it and rotate:
from jhplot import * c1 = HVisAd("VisAd",600,600) ren=c1.getRender() ren.setBoxOn(True) c1.setScaling(0.8) c1.setRotation(0,20,0) # rotation around Y in 20 deg c1.setTranslation(0.1,0.0,0.0) # shift in X by 10% c1.visible() # show it
You can use full Java API of VisAd to archive scaling and rotations using the "ProjectionControl" method:
from jhplot import * from java.awt import * c1 = HVisAd("test",600,500) display = c1.getDisplay() ren=c1.getRender() proj = ren.getDisplay().getProjectionControl(); mouseBehavior = ren.getMouseBehavior() tstart = proj.getMatrix() scale=0.5 t1 = mouseBehavior.make_matrix(0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0) t1 = mouseBehavior.multiply_matrix(t1, tstart) proj.setMatrix(t1) ren.setBoxOn( True ) c1.visible() # show canvas
So far we had just a box. Now we can draw axises. Here is an example how to set axis, define their ranges
from jhplot import * c1 = HVisAd("VisAd") display=c1.getDisplay() cont=display.getGraphicsModeControl() cont.setSceneAntialiasingEnable(True) cont.setLineWidth(1) ren=c1.getRender(); ren.setBoxOn(True) # draw a box c1.setAxes("X",0,10,"Y",0,10,"Z",0,10) c1.visible() # show canvas
The method setAxes() takes the name of the axis, min and max value for X,Y,Z axis. Here is a more refined example that shows how to make custom frame with axis:
from jhplot import * from java.awt import Font from visad import Display,ScalarMap,RealType c1 = HVisAd("VisAd",600,600) display=c1.getDisplay() cont=display.getGraphicsModeControl() cont.setSceneAntialiasingEnable(True) cont.setLineWidth(2) ren=c1.getRender(); ren.setBoxOn(True) # draw a box x=RealType("x"); y=RealType("y"); z=RealType("z") # make X,Y,Z axis rX = ScalarMap(x,Display.XAxis) rY = ScalarMap(y,Display.YAxis) rZ = ScalarMap(z,Display.ZAxis) rX.setScalarName("X axis") rY.setScalarName("Y axis") rZ.setScalarName("Z axis") col=[0.1, 0.1, 0.1] # change color rX.setScaleColor(col) rY.setScaleColor(col) rZ.setScaleColor(col) # set min and max ranges rX.setRange(0,10) rY.setRange(0,20) rZ.setRange(0,100) axisX=rX.getAxisScale() # this is visad.AxisScale object axisX.setFont(Font("Arial", Font.BOLD, 16)) axisX.setLabelSize(16) axisX.setGridLinesVisible(True) # attach to the box? # axisX.setSnapToBox(True) # axisX.setLabelBothSides(True) display.addMap( rX ) display.addMap( rY ) display.addMap( rZ ) c1.visible() # show canvas
VisAd code examples
Using full VisAd Java api
Here is a Jython macro which shows ho to visualize 2D data calling Java classes directly:
from visad import SI,Integer1DSet,RealType from visad import FunctionType, FieldImpl, ScalarMap, Display from visad import Display,FlatField,DataReferenceImpl from visad.java2d import DisplayImplJ2D time = RealType("time",SI.second) height = RealType("height",SI.meter) # Create a FunctionType, that is the class which represents our function # This is the MathType ( time -> height ) # Use FunctionType(MathType domain, MathType range) func_time_height = FunctionType(time, height) # Those are our actual height values # Note the dimensions of the array: # float[ number_of_range_components ][ number_of_range_samples] h_vals =[[0.0, 33.75, 45.0, 33.75, 0.0]] # Create the time_set, with 5 integer values, ranging from 0 to 4. # That means, that there should be 5 values for height. # Use Integer1DSet(MathType type, int length) time_set = Integer1DSet(time, 5); vals_ff = FlatField( func_time_height, time_set); # and put the height values above in it vals_ff.setSamples( h_vals ); # create canvas display = DisplayImplJ2D("display1"); # Create the ScalarMaps: quantity time is to be displayed along x-axis # and height along y-axis # Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar) timeMap = ScalarMap( time, Display.XAxis ); heightMap = ScalarMap( height, Display.YAxis ); # Add maps to display display.addMap( timeMap ); display.addMap( heightMap ); # Create a data reference and set the FlatField as our data data_ref = DataReferenceImpl("data_ref"); data_ref.setData( vals_ff ); # Add reference to display display.addReference( data_ref ); from javax.swing import JFrame # Create application window, put display into it jf=JFrame("VisAD application"); jf.getContentPane().add(display.getComponent()) # Set window size and make it visible jf.setSize(600, 400); jf.setVisible(True);