#!/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_03
#  Data are organized as MathType ( index  -> ( time, height )  ) and
#  represent some points from the parabola of the previous example
#  Data are indexed (time, height) points and get depicted as such.


class p2_03:

    def __init__(self):
        """.."""
        
        # The quantities to be displayed in x- and y-axes: time and height, respectively
        # Our index is also a RealType
        time = getRealType("time")
        height = getRealType("height")
        
        # Organize time and height in a Tuple
        t_h_tuple = RealTupleType( time, height)
        
        # Index has no unit, just a name
        index = RealType("index")
        
        # Create a FunctionType ( index -> ( time, height) )
        # Use FunctionType(MathType domain, MathType range)
        # The function ( time(i), height(i) ), where i = index,
        # represented by ( index -> ( time, height) )
        # ( time, height) are a Tuple, so we have a FunctionType
        # from index to a tuple
        func_i_tuple = FunctionType( index, t_h_tuple)
        
        # Create the x_set, with 5 values, but this time using a
        # Integer1DSet(MathType type, int length)
        index_set = Integer1DSet(index, 5)
        
        # These are our actual data values for time and height
        # Note that these values correspond to the parabola of the
        # previous examples. The y (height) values are the same, but the x (time)
        # are now given given.
        point_vals = [[-3.0, -1.5, 0.0, 1.5, 3.0], 
                      [0.0, 33.75, 45.0, 33.75, 0.0]]
        
        # Create a FlatField, that is the Data class for the samples
        # Use FlatField(FunctionType type, Set domain_set)
        vals_ff = FlatField( func_i_tuple, index_set)
        
        # and put the height values above in it
        vals_ff.setSamples( point_vals )
        
        # Create a 2D display and its maps
        display = DisplayImplJ2D("display1")
        
        # Get display's graphics mode control and draw scales
        dispGMC = display.getGraphicsModeControl()
        dispGMC.setScaleEnable(true)
        
        # Create the ScalarMaps: quantity time is to be displayed along XAxis
        # and height along YAxis
        # 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 )
        
        # Scale heightMap. This will scale the y-axis,
        # because heightMap has DisplayRealType YAXIS
        # We simply choose the range from -4 to 4 for the x-axis
        # and -10.0 to 50.0 for
        timeMap.setRange( -4.0, 4.0)
        heightMap.setRange( -10.0, 50.0)
        
        # 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 )
        
        # Create application window, put display into it
        jframe = JFrame("VisAD Tutorial example 2_03")
        jframe.getContentPane().add(display.getComponent())
        
        # Set window size and make it visible
        jframe.setSize(300, 300)
        jframe.setVisible(true)
        
        
p2_03()
