[Next] [Previous] [Top]

Freud User Manual

CHAPTER 10 Command Line Interface

Overview
Invoking the command line interpreter
Tcl
Commands
Variable substitution
Quoting
Comments
Operators
Control structures
Procedures
Basic Freud Tcl extensions
Opening a dataset
Creating an instantiation
Plotting and printing
Deleting instances and datasets
Viewport commands
Programming example
Advanced Freud Tcl extensions
Handles
Information commands
Handle command
Programming example

Overview

Freud supports a full scripting language based on the public domain language Tcl. Tcl (pronounced "tickle") has too many features to detail in this document; instead, I will describe the extensions implemented in Freud, and give some examples of how Tcl can be used to enhance the power of Freud. A document describing answers to some frequently asked questions about Tcl is available at:

http://www.cis.ohio-state.edu/hypertext/faq/usenet/tcl-faq/top.htm
you can also find it using gopher or WAIS servers on the Internet. Several books are also now avaible on Tcl programming.

Invoking the command line interpreter

If you start Freud as a foreground process (from shells that have job control), then the controlling terminal window will be set up as the Tcl interpreter. If Freud is started as a background process, then you don't have access to the interpreter. So, if you start Freud by typing:

freud
and wait for the base window to appear, and then press the return key, you will see the following prompt:

freud>
This indicates that the interpreter is active. You can now access any Tcl commands or Freud extensions to Tcl.

Tcl

Following is a condensed introduction to Tcl; I highly recommend that you obtain and read the documentation listed above to fully utilize the power of Tcl. This introduction consists mostly of information paraphrased from Ousterhout's book.

Commands

A Tcl command consists of a command word followed by a set of arguments. The command word and arguments are separated by spaces. A new line terminates the command. A Tcl command always returns a string. Some examples of commands and the return value (indicated by italicized text):

set a 5
5

expr 8+9
17

In the first example, a new variable, a, is created and assigned the value 5. In the second example, the expression "8+9" is evaluated. Note that there are no spaces between the `8', the `+', and the `9'. The command expr expects only one argument; if there are spaces, then the Tcl interpreter assumes that there are additional arguments. We can get around this limitation by quoting (see below).

Variable substitution

There are three forms of substitution in Tcl; I will only describe two here. The first form, familiar to Unix shell programmers, is variable substitution, and is invoked by the $ character:

set a 5
echo $a
5

The first command sets the variable a to 5; the second command prints out the first argument, which is $a; the value of a is then substituted.

The second type of substitution is command substitution. This can be used to replace an argument with the return value of a command, and is invoked by enclosing the substituted command with square brackets:

set a [expr 10+2]
12

a is set to the result of expr 10+2. Variable and command substitution can be combined:

set a 5
set a [expr $a+2]
7

Quoting

What if you want to pass an argument with spaces to Tcl? You have to quote the argument. There are two types of quoting operators in Tcl. The first is the double-quote character. If you surround an argument with double quotes, spaces, tabs, newlines, or semicolons are treated as ordinary characters:

set a 5
set b "The value of a is $a"
The value of a is 5

Note that variable substitution takes place within double quotes; command substitution also takes place.

If you don't want command or variable substitution to take place, you can use curly braces:

set a 5
set b {The value of a is $a}
The value of a is $a

Curly braces can be nested:

set a {1 {2 3} 4}
1 {2 3} 4

Comments

If the first non blank character on a line is the character # then all characters up to the end of the line are ignored.

Operators

Tcl supports all of the arithmetic and logical operators that are available in the C programming language. For instance:

expr {1 < 2}
1

expr {1 == 2}
0


set a 5; set b 4
expr {($a > 0) && ($b > 0)}
1

Control structures

Tcl supports a number of control structures; the most important are the for, foreach, if, and source commands.

The if command behaves like if in C: it evaluates an expression, tests the result, and conditionally executes a sequence of commands based on the result. For instance

if {$x < 0} {set x 0}
Note that if is just a command with two arguments. The first argument is evaluated; if it is non-zero, than the second command is evaluated. Since curly brackets are used to enclose the arguments, the command can span multiple lines:

if {$x < 0) {
			set x 0
}
This can be used to improve the readability of a script. The second argument to if can consist of more than one command:

if {$x < 0) {
			set x 0
	echo $x
}
The for and foreach commands allow looping. There are four arguments to the for command: an initialization argument, a test argument, and the script of commands to execute. For instance:

for {set i 0} {$i < 2} {incr i} {
		echo $i
}
0

1

The first argument ({set i 0}) is evaluated before the loop begins; the loop continues as long as the second argument (tested at the top of the loop) is true; the third argument is executed after each completion of a loop. Note the importance of enclosing the arguments in curly brackets; if they were enclosed in double quotes, then they would be evaluated at the time the command was parsed, rather than each time the loop returned to the top.

The foreach command allows looping over a list (see the Tcl book for more info on lists). For example:

set a {This is a list}
foreach i $a {echo $i}
This

is

a

list

The source command allows you to read a set of commands from a file. It takes a single argument that contains the name of a file. For example, the command:

source freud.tcl
will execute the contents of the file freud.tcl.

Procedures

You can add new commands to Tcl by defining procedures. A procedure is similar to a C procedure, or a FORTRAN subroutine. Following is an example of a simple procedure:

proc plus {a b} {expr $a+$b}
If you type in the preceding line, and then the line:

plus 3 4
7

You will see the result 7 displayed. The a parameter is set to the value of 3, the b parameter is set to the value of 4, and then the expression in the third argument to the proc command is executed.

Basic Freud Tcl extensions

Several Freud specific commands were added to the Tcl command set. These commands allow you to open a data set, create instances, load data into those instances, plot and print.

Opening a dataset

fropen allows you to open a dataset, and returns a number that can be used when you load data into an instance. For example, you would type:

fropen testh.cdf
1

to open the dataset named testh.cdf. The returned value of 1 is used later when creating a new instantiation.

Creating an instantiation

There are four commands available for instantiating layers -- frmap, frxy, frcontour, and frvector. Each creates a map, xy, contour, or vector instantiation. An optional defaults file can be specified for all four commands; frcontour and frvector also require that you specify a dataset.

The following will instantiate a generic map background:

frmap
If you have a defaults file named mymap hat you wish to load with the map instantiation, you can type:

frmap ~/.freud/map/mymap
If you want a XY background instead of a map background, use frxy:

frxy ~/.freud/xy/myxy
Suppose you now want to create a contour instantiation. You need to specify a dataset and variable to associate with this instantiation. Assume the file testh.cdf has a variable Z that is a function of four dimensions -- Z(frtime, level, lat, lon). The following command will create an instantiation that will create a contour instantiation at frtime=3 and level=4, if we have already opened the file testh.cdf with the fropen command, and that command returned 1:

	frcontour 1 {Z(frtime=3,level=4)}
the curly brackets{} should be used if you wish to embed spaces in the slice expression.

A defaults file can also be specified for frcontour:

frcontour 1 {Z(frtime=3,level=4)} ~/.freud/contour/mycontour
If the defaults file was saved with data, that data will be used instead of the data specified in the command line.

The frvector command requires two datasets and two variables (one each for the u and v components):

frvector 1 {u(frtime=3,level=4)} 1 {v(frtime=3, level=4)

Plotting and printing

A plot can be viewed by typing:

frplot
Or, if you want to send the plot to a printer:

frprint printer printer
where printer is the name of a printer on your Unix system. To send the plot to a file
type:

frprint file file
where file is the name of the file where you want to save the plot.

Deleting instances and datasets

All current datsets can be deleted by typing:

frclear sources
All current instantiations can be deleted by typing:

frclear instances
Individual instantiations and datsets can be deleted by using more advanced Freud Tcl commands.

Viewport commands

The page can be divided up into m by n nonoverlapping windows by using the frtile command; for instance, the following command:

frtile 1 4
divides the page into four vertical windows.

The active window is set by the frpage command:

frpage active 2
causes plots to be drawn in the second window.

If you wish to reset the display to one window, type:

frclear viewports

Programming example

Following is an example program that is available in the Freud distribution in sample/easy.tcl. If the location of the Freud libraries is in /usr/local/lib/freud, you can execute it by typing:

cd /usr/local/lib/freud/sample
source easy.tcl
The program creates two windows, a map and a contour instantiation, and plots two slices from a netCDF file in the sample distribution. Here is the complete source code:

#
# Simple automated plotting example
#

# Get the current library directory
if [info exists env(FREUD_LIB_DIR)] {
    set mydir $env(FREUD_LIB_DIR)
} else {
    set mydir /usr/local/lib/freud
}

# Clear out any old instantiations, sources or viewports
frclear instances sources viewports

# Create two windows
frtile 1 2

# Open the netCDF file testh.cdf and save the returned dataset number
set dsetNumber [fropen $mydir/sample/testh.cdf]

# Create a contour instantiation use the defaults file ngm_contour 
# and the previously opened dataset; slice variable Z (which is 4d)
# at level 3 and frtime 0

for {set i 0} {$i < 2} {incr i} {

# Clear all previous instantiations

    frclear instances
# Create a contour instantation using the defaults file ngm_contour
# and the dataset number returned by the fropen command; the variable
# Z at level 3 and frtime equal to the loop index is loaded into the
# instantiation

    frcontour $dsetNumber "Z(frtime=$i,level=3)" $mydir/sample/ngm_contour

# Create a map instantiation using the defaults file ngm_map

    frmap $mydir/sample/ngm_map
# Set the current active window to the loop index + 1
    frpage active [expr $i+1]
# Set the label
    frlabel "NGM 500mb heights at time $i" 

# Plot it
    frplot
}

Advanced Freud Tcl extensions

Freud also supports some lower level commands that are more difficult to use. These commands allow you to create instances, load and slice variables, and print or plot the results. A list of all of these extensions and more is in Appendix A.

Handles

Some of the Freud Tcl commands use handles. Handles are names that refer to an object. For instance, there is a command, frinstance, that allows you to instantiate an object. The handle can be used to refer to the newly created object. If you type:

frinstance mycontour contour mydefaults
a new instance of a contour object will be created, and the default file mydefaults will be used to initialize it. The handle mycontour can be used to refer to the object in other commands. For example, the object can be deleted by typing:

frdelete mycontour
Data can be loaded by using the frsource command. If you type:

frsource mydata test.cdf
the file test.cdf will be loaded into Freud. The handle mydata can be used to refer to the data file.

Suppose the file test.cdf contains a variable Z.You can create a handle to this variable by typing:

frvar myvar mydata Z
Note the use of the handle mydata that was created by the frsource command.

This variable may not be in the correct form for plotting--for instance, it might be a four dimensional variable, and Freud can only plot functions of two dimensions. The variable needs to be sliced. To slice a variable, you can use a slice expression. Suppose Z is a function of the dimensions frtime, level, lat, and lon (the names of the dimensions can be determined by using the data browser), and that you want to display the 2D slice of Z at the frtime=0 and level=3, where the numbers are the indices of the dimensions, not the actual values. To create a handle to such a sliced variable, you can type:

frvar myv1 mydata "Z_0=Z(frtime=0,level=3)"
The part of the command line surrounded by double quotes is a slice expression. The quotes are needed if there are any spaces in the expression. A new handle myv1 is created to a new variable Z_0 that is made by slicing Z at the frtime=0 and level=3 dimensions.

Data can be loaded into an instance by using the frload command. For example, the sliced variable Z_0 can be loaded into the contour instance mycontour by typing the line:

frload mycontour myv1

Information commands

The frinfo command can be used to return information on instantiations, sources, and variables. If you have created a contour and a map instantiation type:

frinfo instances
Map-0 Contour-1

If you load the source /usr/local/lib/freud/sample/testh.cdf and the variable Z, slice it at frtime=0, level =0, and type:

frinfo loaded Contour-1
{/usr/local/lib/freud/sample/testh.cdf Z(frtime=0,level=0)}

Note that this is in the form of a Tcl list of lists, and can be manipulated as such. For instance, the command:

	lindex [frinfo loaded Contour-1] 0
returns:

/usr/local/lib/freud/sample/testh.cdf Z(frtime=0,level=0)

and

lindex [lindex [frinfo loaded Contour-1] 0] 0
returns:

/usr/local/lib/freud/sample/testh.cdf

You can return information on all of the loaded sources:

	frinfo sources
/usr/local/lib/freud/sample/testh.cdf /usr/local/lib/freud/sample/testu.cdf

if you have loaded two sources named testh.cdf and testu.cdf.

You can get a list of all of the variables in a source:

frinfo vars /usr/local/lib/freud/sample/testh.cdf
{Z } {lat } {lon } {frtime } {level } {reftime } {grib_center } {grib_model }

You can also return information on a given variable in a source:

	frinfo var /usr/local/lib/freud/sample/testh.cdf Z
{frtime {0 7 } { 0 12 6 18 24 30 36 48 } } {level {0 9 } { 1000 850 700 500 400 300 250 200 150 100 } } {lat {0 32 } { 20 21.25 22.5 23.75 25 26.25 27.5 28.75 30 31.25 32.5 33.75 35 36.25 37.5 38.75 40 41.25 42.5 43.75 45 46.25 47.5 48.75 50 51.25 52.5 53.75 55 56.25 57.5 58.75 60 } } {lon {0 35 } { -140 -137.5 -135 -132.5 -130 -127.5 -125 -122.5 -120 -117.5 -115 -112.5 -110 -107.5 -105 -102.5 -100 -97.5 -95 -92.5 -90 -87.5 -85 -82.5 -80 -77.5 -75 -72.5 -70 -67.5 -65 -62.5 -60 -57.5 -55 -52.5 } }

This command returns the dimensions associated with the variable. Each dimension is a list containing the name of the dimension, the index range of the dimension, and the values associated with the dimension. In this example frtime has a low index of 0, a high index of 7, and contains the values 0, 12, 6, 18, 24, 30, 36, and 48.

Handle command

The TCL extensions to Freud also allow you to return an existing handle to an instance, source, or variable, or create one if it doesn't already exist. So, if you haven't already created a handle to the Contour-1 instantiation in the previous example, and you type the command:

frhandle instance Contour-1
the return value is:

_instance0

If you type the command again, you will see the same return value, since one and only one handle is associated with an instance, source, or variable. If you type:

frhandle source /usr/local/lib/freud/sample/testh.cdf
the return value will be:

_source1

Similarly:

frhandle source /usr/local/lib/freud/sample/testh.cdf Z(frtime=0,level=3)
_var4

if you have already created the slice Z(frtime=0,level=3). You can now use these handles to load data into the contour instantiation:

frload _instance0 _var4

Programming example

Following is an example program that is available in the Freud distribution in sample/sample.tcl. It will work only if the Freud libraries are installed in the standard location (/usr/local/lib/freud). You should delete all instances and all data sources before running it. To execute it, type:

cd /usr/local/lib/freud/sample
source sample.tcl
#
# Display a sequence of slices from the netCDF files testu.cdf, 
# testv.cdf, testh.cdf
#

#
# Procedure to create instances
#

proc create_instances {} {
# Create a contour instance using the defaults file
# ngm_contour
	frinstance c contour /usr/local/lib/freud/sample/ngm_contour
# Create a map instance using the defaults file ngm_map
 	frinstance m map /usr/local/lib/freud/sample/ngm_map
# Create a vector instance using the defaults file ngm_vector
 	frinstance vec vector /usr/local/lib/freud/sample/ngm_vector
}


#
# Procedures to load in data files
#

proc load_files {} {
# Load the file testh.cdf into source handle s
 	frsource s /usr/local/lib/freud/sample/testh.cdf
# Load the file testu.cdf into source handle v1
	frsource v1 /usr/local/lib/freud/sample/testu.cdf
# Load the file testv.cdf into source handle v2
 	frsource v2 /usr/local/lib/freud/sample/testv.cdf
# Create variables
 for { set i 0 } {$i < 4} {incr i} {
 	frvar z_$i s "Z_${i} = Z(frtime=${i}, level=3)"
 	frvar u_$i v1 "u_$i = u(frtime=${i}, level=3)"
 	frvar v_$i v2 "v_$i = v(frtime=${i}, level=3)"
 	}
}

#
# Loop through drawings
#

proc do_work {} {
# Create list of titles
 	set thead {NGM model output 500 MB heights, winds at}
 	set ttimes {{0 hr} {12 hr} {6 hr} {18 hr}}
 	set ttail {from model init}
# Loop and create a series of plots
 	for { set i 0 } {$i < 4} {incr i} {
 		frload c z_$i
 		frload vec u_$i v_$i
 		frlabel "$thead [lindex $ttimes $i] $ttail"
 		frplot
 		echo "Snoozing for five seconds ..."
 		exec sleep 5
 	}
}

# Delete all instances and sources (which implicitly deletes 
# variables)

proc delete_all {} {frdelete c m vec s v1 v2}

create_instances
load_files
do_work
This program defines four procedures (create_instances, load_files, do_work, and delete_all). The first procedure, create_instances, is used to create three new instances--a contour, map, and vector instance. The procedure load_files loads and slices the data files and variables used by the instances. The procedure do_work does the actuall plotting. The program can be interrupted at any time by typing control-C. Once the program has completed, you may delete the sources and instances by typing:

	delete_all

Freud User Manual - 02 FEB 96
[Next] [Previous] [Top]

Generated with CERN WebMaker