#!/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


class p2_09:
    """VisAD Tutorial example 2_09
    Using a Gridded1DSet to demonstrate a irregular
    sampling of points. These are denser near the peak of the curve
    y = exp( -x^2 ), a gaussian distribution curve
    """
    def __init__(self):
        # Create the quantities
        # x and y are measured in SI meters
        # Use RealType(String name, Unit u,  Set set), set is null
        x = getRealType("X")
        y = getRealType("Y")
        
        # Create the x_set, with the following x samples
        # Gridded1DSet(MathType type, samples, length)
        LENGTH = 23
        x_vals  = [[ -4.0, -1.7142857, -1.0285715, -0.9142857,
                     -0.8, -0.6857143, -0.5714286, -0.45714286, -0.34285715, -0.22857143,
                     -0.114285715, 0.0, 0.114285715, 0.22857143,0.34285715, 0.45714286,
                     0.5714286, 0.6857143, 0.8, 0.9142857, 1.0285715, 1.7142857, 4.0]]
        x_set = Gridded1DSet(x, x_vals, LENGTH)
        
        # Create a FunctionType, that is the class which represents the function y = f(x)
        # Use FunctionType(MathType domain, MathType range)
        func_x_y = FunctionType(x, y)
        
        # These are our actual y values
        # Note that these are y values for the curve y = exp( -x^2 )
        y_vals = [[1.12535176E-7, 0.0529305, 0.34716353, 0.4334762, 0.5272924,
                   0.6248747, 0.72142226, 0.8114118, 0.8890951, 0.9490964, 0.9870237,
                   1.0, 0.9870237, 0.9490964, 0.8890951, 0.8114118, 0.72142226, 0.6248747,
                   0.5272924, 0.4334762, 0.34716353, 0.0529305, 1.12535176E-7]]
        
        # Create a FlatField
        # Use FlatField(FunctionType type, Set domain_set)
        vals_ff = FlatField( func_x_y, x_set)
        
        # and put the y values above in it
        vals_ff.setSamples( y_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)
        
        # Create the ScalarMaps: quantity x is to be displayed along XAxis and y along YAxis
        # Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)
        xMap = ScalarMap( x, Display.XAxis )
        yMap = ScalarMap( y, Display.YAxis )
        yRGBMap = ScalarMap( y, Display.RGB )
        
        # Add maps to display, note that w have a RGB ScalarMap for y
        for map in (xMap, yMap, yRGBMap):
            display.addMap( map )
            
        # 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_09")
        jframe.getContentPane().add(display.getComponent())
        
        # Set window size and make it visible
        jframe.setSize(300, 300)
        jframe.setVisible(true)

p2_09()
