Printing an Array Slice
The following macro prints a Fortran array slice. This macro, like others shown in this chapter, relies heavily on Tcl and uses unique CLI commands sparingly.
proc pf2Dslice {anArray i1 i2 j1 j2 {i3 1} {j3 1} \
{width 20}} {
for {set i $i1} {$i <= $i2} {incr i $i3} {
set row_out ""
for {set j $j1} {$j <= $j2} {incr j $j3} {
set ij [capture dprint $anArray\($i,$j\)]
set ij [string range $ij \
[expr [string first "=" $ij] + 1] end]
set ij [string trimright $ij]
if {[string first "-" $ij] == 1} {
set ij [string range $ij 1 end]}
append ij " "
append row_out " " \
[string range $ij 0 $width] " "
}
puts $row_out
}
}
*The CLI’s dprint command lets you specify a slice. For example, you can type: dprint a(1:4,1:4).
After invoking this macro, the CLI prints a two-dimensional slice (i1:i2:i3, j1:j2:j3) of a Fortran array to a numeric field whose width is specified by the width argument. This width doesn’t include a leading minus sign (-).
All but one line is standard Tcl. This line uses the dprint command to obtain the value of one array element. This element’s value is then captured into a variable. The CLI capture command allows a value that is normally printed to be sent to a variable. For information on the difference between values being displayed and values being returned, see About CLI Output”.
The following shows how this macro is used:
d1.<> pf2Dslice a 1 4 1 4
0.841470956802 0.909297406673 0.141120001673-0.756802499294
0.909297406673-0.756802499294-0.279415488243 0.989358246326
0.141120001673-0.279415488243 0.412118494510-0.536572933197
-0.756802499294 0.989358246326-0.536572933197-0.287903308868
d1.<> pf2Dslice a 1 4 1 4 1 1 17
0.841470956802 0.909297406673 0.141120001673-0.756802499294
0.909297406673-0.756802499294-0.279415488243 0.989358246326
0.141120001673-0.279415488243 0.412118494510-0.536572933197
-0.756802499294 0.989358246326-0.536572933197-0.287903308868
d1.<> pf2Dslice a 1 4 1 4 2 2 10
0.84147095 0.14112000
0.14112000 0.41211849
d1.<> pf2Dslice a 2 4 2 4 2 2 10
-0.75680249 0.98935824
0.98935824 -0.28790330
d1.<>