#!/usr/bin/env jython
# VisAD Tutorial
# Copyright (C) 2000 Ugo Taddei
#
# Jython translation
# Copyright (C) 2002 Frank Gibbons.

try:
    import java
    from javax.swing import *
    from visad import *
    from visad.python.JPythonMethods import *
    from visad.java2d import DisplayImplJ2D
except:
    pass

false = 0
true = not false

#  VisAD Tutorial example 2_07
#  Almost like example 2_07, but this time we map height to color (RGB)
#  and and speed to YAXIS.
#  We define a Unit, mps, which stands for meters per second and which
#  is Unit SI.meter divided by Unit SI.second
#  The same old parabola, but with a different look.
#  We use a MathType ( time -> (height, speed) ),
#  draw time and height along the x- and y-axis, respectively, and
#  map the speed to the line colour. For that we use a ScalarMap with
#  speed as RealType and Display.RGB as DisplayRealType
#  Use the GraphicsModeControl to change line thickness of
#  display

class p2_07:
  def __init__(self):
    # Create the quantities
    # Use RealType(String name, Unit u,  Set set), set is null
    time = getRealType("time")
    height = getRealType("height")

    # A user-defined unit
    # Define unit...
    mps = SI.meter.divide( SI.second )

    # ...and define speed with unit
    speed = RealType("speed", mps)

    # Pack height and speed in a Tuple
    h_s_tuple = RealTupleType(height, speed)

   # Create a FunctionType, that is the class which represents the function y = f(x)
   # Use FunctionType(MathType domain, MathType range)
    func_t_tuple = FunctionType(time, h_s_tuple)

    # Create the x_set, with 5 values, but this time using a
    # Linear1DSet(MathType type, double first, double last, int length)
    LENGTH = 32
    time_set = Linear1DSet(time, -3.0, 3.0, LENGTH)

    # Generate some points 
    # Note that we have the parabola height = 45 - 5 * time^2
    # But first we create a list of (two) lists for the data
    h_s_vals = [[], []] 

    # ...then we use a method of Set to get the samples from time_set
    # this call will get the time values
    # "true" means we get a copy from the samples
    t_vals  = time_set.getSamples( true)

    # finally generate height and speed values
    # height values...
    h_s_vals[0] =  [ 45.0 - 5.0 * t_vals[0][i]*t_vals[0][i] for i in range(LENGTH) ]
    # ...and speed values: the derivative of the above function
    h_s_vals[1] = [ - 10.0 * t_vals[0][i] for i in range(LENGTH) ]
    
    # Create a FlatField
    # Use FlatField(FunctionType type, Set domain_set)
    h_s_ff = FlatField( func_t_tuple, time_set)
    
    # and put the height values above in it
    h_s_ff.setSamples(h_s_vals )
    
    # Create a 2D display
    display = DisplayImplJ2D("display1")
    
    # Get display's graphics mode control draw scales
    # and change line thickness
    dispGMC = display.getGraphicsModeControl()
    dispGMC.setScaleEnable(true)
    dispGMC.setLineWidth( 3.0 )

    # Create the ScalarMaps: quantity time is to be displayed along XAxis
    # and height along YAxis; speed is mapped to RGB color
    # Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)
    timeMap = ScalarMap( time, Display.XAxis )
    speedYMap = ScalarMap( speed, Display.YAxis )
    heightRGBMap = ScalarMap( height, Display.RGB )

    # Add maps to display
    for map in (timeMap, speedYMap, heightRGBMap):
        display.addMap( map )

    # Create a data reference and set the FlatField as our data
    h_s_ref = DataReferenceImpl("h_s_ref")
    h_s_ref.setData( h_s_ff )

    # Add reference to display
    display.addReference( h_s_ref )

    # Create application window, put display into it
    jframe = JFrame("VisAD Tutorial example 2_07")
    jframe.getContentPane().add(display.getComponent())

    # Set window size and make it visible
    jframe.setSize(300, 300)
    jframe.setVisible(true)


p2_07()
