PV-WAVE Foundation > Tutorial > Reading Data into PV-WAVE > Lesson 3: Reading Formatted Data with an Explicit Format
Lesson 3: Reading Formatted Data with an Explicit Format
This example demonstrates how to read formatted data with an explicit format using the data file, example_air_q.dat, which contains air quality data collected over two-hour periods each month of the year. The data file is a combination of data types with every line containing a 3-character string (the month the data was collected), a 2-digit integer, (the date of the month), and three floating-point data values that represent average temperature, carbon monoxide, and sulfur dioxide levels. This file is located in <RW_DIR>\wave\data, where <RW_DIR> is your installation directory.
This example illustrates a key data reading concept in PV-WAVE. That is, because of the way PV-WAVE handles array storage, you cannot read directly into an element of an array. Constants and expressions are passed to all procedures, including READF, by value or as temporary storage. This prevents inadvertently redefining the value of a constant or expression in the called procedure.
In this example, two separate methods will be used to demonstrate how you can read data into PV-WAVE. The first method demonstrates how to use OPENF and READF to read data into PV-WAVE. The second method demonstrates how to use DC_READ_FIXED.
Read the Data Using OPENF and READF
To read formatted data with an explicit format, do the following:
1. Define the variables to hold the temperature, carbon monoxide, and sulfur dioxide values. If you are unsure of the length of the file, select a number that you know is too large, in this case, 2000.
av_temp = FLTARR(2000)
av_co = FLTARR(2000)
av_so2 = FLTARR(2000)
2. Define the variables to hold the string data and the integers:
hold_string = ''
hold_int = 0
3. Open the file for reading. The LUN is 1.
(WIN) OPENR, 1, GETENV("RW_DIR") + '\wave\data\example_air_q.dat'
(UNIX) OPENR, 1, GETENV("RW_DIR") + '\wave\data\example_air_q.dat'
4. Specify how the imported data will be formatted. This is a FORTRAN-style format string that specifies the type, size, and order the values will be read. A denotes character strings; I denotes integer data; 3F denotes three different fields, each one containing floating-point data; and $ supresses carriage returns. The parentheses are part of the string.
format_string = '$(A3, I2, 3F9.4)'
5. Set up the ability to execute a loop without creating a procedure. .RUN puts every line in a macro so that it is compiled and run at the END statement rather than line by line. A dash (-) appears in place of the WAVE> prompt.
.RUN
6. Initialize the counter.
i = 0
7. Create a loop which executes until you reach the end of the file. This method is useful when you don’t know the number of lines in a file.
WHILE NOT EOF(1) DO BEGIN
8. Read the data from the files into temporary variables. Because of the way PV-WAVE handles array storage, you cannot read directly into an element of an array. The temporary variables need not be declared since they are real variables.
READF, 1, format=format_string, hold_string, hold_int, $
temp1, temp2, temp3
9. Assign the values you just read to the data arrays. Notice that you are not using the first two columns of this dataset.
av_temp(i) = temp1
av_co(i) = temp2
av_so2(i) = temp3
10. Update the counter.
i = i + 1
11. End the WHILE loop.
ENDWHILE
12. End the macro. PV-WAVE now compiles and runs the statements.
END
13. The following message appears after you end the macro:
%Compiled module: $MAIN$
WAVE>
If you do not see this message, proceed to step 14 and then re-enter commands starting with step 1.
14. Close the file.
CLOSE, 1
To verify that the data was properly read in, do the following:
1. Determine how many values were read from the file.
PRINT, i
2. Trim the data structure to the length of the dataset.
av_temp = av_temp(0:i-1)
av_co = av_co(0:i-1)
av_so2 = av_so2(0:i-1)
3. Save all current local variables.
SAVE, Filename='air_data.dat', /variables
Read the Data Using DC_READ_FIXED
You can read the same formatted data in fewer steps using the function DC_READ_FIXED. To read the formatted data with an explicit format, do the following:
1. Define the variables to hold the temperature, carbon monoxide, and sulfur dioxide values. If you are unsure of the length of the file, select a number that you know is too large, in this case, 2000.
av_temp = FLTARR(2000)
av_co = FLTARR(2000)
av_so2 = FLTARR(2000)
2. Open the file and read the contents into the variables.
(WIN) status = DC_READ_FIXED( $
'<
RW_DIR>\wave\data\example_air_q.dat', av_temp, $
av_co, av_so2, Fmt='(5X, 3F9.2)', Resize=[1,2,3], $
/Col)
(UNIX) status = DC_READ_FIXED( $
'$RW_DIR/wave/data/example_air_q.dat', $
av_temp, av_co, av_so2, Fmt='(5X, 3F9.2)', $
Resize=[1,2,3], /Col)
3. Save all current local variables.
SAVE, Filename='air_data.dat', /variables
4. Determine how many values were read from the file.
INFO, av_co, av_so2, av_temp