#!/usr/bin/env jython

# VisAD Tutorial
# Copyright (C) 2000 Ugo Taddei
#
# Jython translation
# Copyright (C) 2002 Frank Gibbons.

from  visad import *
from visad.python.JPythonMethods import *
from  visad.java2d import DisplayImplJ2D
import java.rmi.RemoteException
from java.awt import *
from javax.swing import *

#
#  Somewhat different version of program P2_07
#  We reorganize the MathType of example P2_07 ( time -> (height, speed) )
#  as
#   ( time -> height )
#  and
#   ( time -> speed )
#  When then plot time (along x-axis) against height and speed, both (along y-axis)
#  The color of speed display is changed to match the line color os speed

# Create the quantities
# x and y are measured in SI meters
# Use getRealType(String name, Unit u,  Set set), set is null

time = getRealType("time", SI.second)
height = getRealType("height", SI.meter)

# Create a new unit for speed, meters per seconds
mps = SI.meter.divide( SI.second )
speed = getRealType("speed", mps)

# Create a FunctionType, that is the class which represents the function y = f(x)
# Use FunctionType(MathType domain, MathType range)
func_t_h = FunctionType( time, height )
func_t_s = FunctionType( time, speed )

# Create the time_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 with a for-loop for the line
# Note that we have the parabola height = 45 - 5 * time^2
# But first we create a float array for the values
h_vals = []; h_vals.append([])
s_vals = []; s_vals.append([])

# ...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( 1 )

# finally generate height and speed values
# height is given by the parabola height = 45 - 5 * time^2
# and speed by its first derivative speed = -10 * time
for i in range(LENGTH):
    h_vals[0].append(45.0 - 5.0 * t_vals[0][i]*t_vals[0][i])
    s_vals[0].append( - 10.0 * t_vals[0][i] )


height_ff = FlatField( func_t_h, time_set)
speed_ff = FlatField( func_t_s, time_set)

# and put the y values above in it
height_ff.setSamples( h_vals )
speed_ff.setSamples( s_vals )

display = DisplayImplJ2D("display1")

dispGMC = display.getGraphicsModeControl()
dispGMC.setScaleEnable(1)
dispGMC.setLineWidth( 2.0 )

timeMap = ScalarMap( time, Display.XAxis )
heightYMap = ScalarMap( height, Display.YAxis )
speedYMap = ScalarMap( speed, Display.YAxis )

display.addMap( timeMap )
display.addMap( heightYMap )
display.addMap( speedYMap )
heightYMap.setRange( 0.0, 50.0)

speedRed = 1.0
speedGreen = 1.0
speedBlue = 0.0

speedColor = [speedRed, speedGreen, speedBlue]
speedYMap.setScaleColor( speedColor )

#  uncomment the following line if you don't want speed axis to be drawn
# speedYMap.setScaleEnable(0)

t_h_ref = DataReferenceImpl("t_h_ref")
t_s_ref = DataReferenceImpl("t_s_ref")

t_h_ref.setData( height_ff )
t_s_ref.setData( speed_ff )

display.addReference( t_h_ref )

speedCMap = [ ConstantMap(speedRed, Display.Red),
              ConstantMap(speedGreen, Display.Green),
              ConstantMap(speedBlue, Display.Blue),
              ConstantMap(1.50, Display.LineWidth) ]

display.addReference( t_s_ref, speedCMap )

jframe = JFrame("VisAD Tutorial example 2_08")
jframe.getContentPane().add(display.getComponent())

jframe.setSize(300, 300)
jframe.setVisible(1)



