Simple Examples of Input and Output
PV-WAVE variables point to portions of memory that are set aside during a session to store data. The first step in analyzing data is usually to transfer it into PV-WAVE variables.
This section provides a “birds-eye view” of how PV-WAVE I/O (Input/Output) works by providing some examples showing how data is transferred in and out of variables.
Example 1—Input
The following example illustrates how easy it is to read a single column of data points contained in the file data1.dat into a variable flow. The data points can then be plotted. The file data1.dat contains the data points:
23.2
34.7
78.1
46.5
44.4
Try entering the following commands to read and plot the data points:
; DC_READ_FREE handles the opening and closing of the file. It
; takes the values in the file “data1.dat” and places them into a
; floating-point variable named flow. The variable flow is
; dimensioned to match the number of points read from the file.
; The returned value status can be checked to see if the process
; completed successfully.
status = DC_READ_FREE('data1.dat', flow)
; Display the variable flow in a window.
PLOT, flow
With two commands, the data is transferred from the file into the variable flow and displayed in a window.
An alternate set of commands that achieves a similar result is shown below.
; Define a variable that holds a single column
; of data containing 5 data points.
flow = FLTARR(5)
; Open the file "data1.dat" for reading.
OPENR, 1, 'data1.dat'
; Read the data from the file into the variable flow.
READF, 1, flow
; Close the file.
CLOSE, 1
; Display the variable flow in a window.
PLOT, flow
Example 2—Output
Here’s a simple example showing how you can transfer data from a variable to a file:
; Define ylow to be a vector of integers.
ylow = [77, 63, 42, 56]
; DC_WRITE_FREE handles the opening and closing of the file. It
; takes the values in "ylow" and stores them in a file named
; “data2.dat”. Because the Column keyword was used, each value is
; written on different lines. The returned value status can be
; checked to see if the process completed successfully.
status = DC_WRITE_FREE('data2.dat', ylow, /Column)
Or you can use the OPENW command to create a new file that contains these same values:
; Open the file “data3.dat” for writing.
OPENW, 2, 'data3.dat'
; Write the values to the file, each value on a new line.
PRINTF, 2, '77'
PRINTF, 2, '63'
PRINTF, 2, '42'
PRINTF, 2, '56'
; Close the file.
CLOSE, 2
Now use the following commands to change a data point in the existing file data3.dat:
; Open the file “data3.dat” for updating.
OPENU, 1, 'data3.dat'
; Replaces the value 77 with the new value 89.
PRINTF, 1, '89'
; Close the file.
CLOSE, 1
Now the contents of data3.dat look like:
89
63
42
56
Conclusion
These two examples have introduced you to a few of the commands that are available for reading and writing data. The rest of this chapter elaborates on the various commands and concepts that you need to know to confidently transfer data in and out of PV-WAVE.
Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.