Example 4: Reading a T/HIS Curve File and Operating on it
Example 4: Reading a T/HIS curve file and operating on it
Problem
input variables = CURVE_FILE and GATE_TIME.
read the
T/HIS
curve file, calculate average y-value of all points that occur after x-value=GATE_TIME.
Return the average in a new variable Y_AVERAGE
Solution
JavaScript |
var count, line, x, y, X, Y, ytot, ny; |
Discussion
This example uses the File class which REPORTER defines to read the T/HIS curve file. The function File.Exists() can be used to test if a filename is valid. Then the File constructor, ReadLine() and Close() functions are used to read the data from the file.
To extract the xy data pairs from the file we use a regular expression. This is perhaps the most complicated part of the program. We want to be able to read x and y values that can be separated by a comma, one or more spaces, or both. If we break the expression ([0-9eE+\-\.]+)\s*,?\s*([0-9eE+\-\.]+) into it's constituent parts we get:
([0-9eE+\-\.]+) . The [] groups characters that we allow to match. - and . have special meanings so they have to be escaped with a \ character. So this means we are allowing any of the characters 0123456789eE+-. to match. The [] specifies a single character so we use + to mean one or more. Finally, using () captures the expression so we can extract the value that matched. So this will match values such as '10', '1.2345', '1.0e+05', '-23.4'
\s*,?\s* . The \s matches a single space. A * means that it will try to match 0 or more spaces (as many as are present). The , matches a comma and the ? means match either 0 or 1 of them. So this expression means "Match 0 or more spaces followed by 0 or 1 commas followed by 0 or more spaces".
More details on regular expressions can be found in the Regular expressions as these can use regular expressions.
Once we have extracted the data values with the regular expression we can easily calculate the average and make a new variable using the techniques in the first 3 examples.
The source code for this example is available here .