REPORTER 22.1

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;


// Get variable values from template
var curveFile = reporter.currentTemplate.GetVariableValue("CURVE_FILE");
var gateTime = reporter.currentTemplate.GetVariableValue("GATE_TIME");

// Check that the variables exist
if (curveFile == null) throw Error("no CURVE_FILE variable\n");
if (gateTime == null) throw Error("no GATE_TIME variable\n");

// Check curve file exists
if (!File.Exists(curveFile)) throw Error("Curve file " + curveFile + " does not exist\n");

// Check gateTime is a valid number
var t = parseFloat(gateTime);
if (isNaN(t)) throw Error("Gate time " + gateTime + " is not a valid number\n");

// create a new File object
var file = new File(curveFile, File.READ);

// Zero variables
count = 0;
ytot = 0;
ny = 0;

// Keep reading lines from the file until we get to the end of the file
while ( (line = file.ReadLine() ) != File.EOF)
{
if (line.charAt(0) == '$')
continue;
else if (line.match(/CONTINUE/))
break;
else
{
count++;

// Skip the four title lines at the top of the curve file
if (count > 4)
{
// strip leading and trailing apaces
line = line.replace(/^\s+/, "");
line = line.replace(/\s+$/, "");
result = line.match(/([0-9eE+\-\.]+)\s*,?\s*([0-9eE+\-\.]+)/);
if (result != null)
{
x = result[1];
y = result[2];

// Extract numbers
X = parseFloat(x);
Y = parseFloat(y);

// Check that they are valid numbers
if (isNaN(X)) throw Error("X " + x + " is not a valid number\n");
if (isNaN(Y)) throw Error("Y " + y + " is not a valid number\n");

// If greater than gate time then include value
if (X > t)
{
ny++;
ytot += Y;
}
}
}
}
}

// Close the file
file.Close();

// If we have read any values calculate average and set variable
if (ny)
{
ytot /= ny;
// Create new variable LENGTH
var ave = new Variable(reporter.currentTemplate, "Y_AVERAGE",
"average Y value", ytot);
}

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 .