SimplePlotHack.mesa
Last Edited by: SChen, June 15, 1985 7:37:41 pm PDT
Sweetsun Chen, August 2, 1985 1:07:43 pm PDT
DIRECTORY
Basics USING [LowHalf],
BasicTime USING [Now],
Commander USING [CommandProc, Register],
CommandTool USING [ParseToList],
Convert USING [CardFromRope, Error],
FileNames USING [CurrentWorkingDirectory],
FS USING [ComponentPositions, EnumerateForNames, ExpandName, NameProc, Error, StreamOpen],
IO USING [Close, EndOfStream, Error, GetCard, GetReal, GetRopeLiteral, int, PutF, PutFR, STREAM],
List USING [DReverse],
Plot USING [AddVector, CreateViewer, Curves, PlotSpec, PlotSpecRec, RealSequence, RopeSequence, Vector],
Real USING [LargestNumber, SmallestNormalizedNumber],
Rope USING [Cat, Equal, Fetch, Find, IsEmpty, Length, ROPE, Substr],
ViewerClasses USING [Viewer];
SimplePlotHack:
CEDAR
PROGRAM
IMPORTS Basics, BasicTime, Commander, CommandTool, Convert, FileNames, FS, IO, List, Plot, Rope = {
OPEN IO, Plot;
RopeList: TYPE = LIST OF Rope.ROPE;
DataError: SIGNAL[reason: Rope.ROPE ← NIL] = CODE;
SPlot: Commander.CommandProc = {
argList: RopeList;
length: NAT;
[argList, length] ← CommandTool.ParseToList[cmd];
IF length <= 0 THEN msg ← "To plot data in files, type: SPlot <files>"
ELSE {
wDir: Rope.ROPE ← FileNames.CurrentWorkingDirectory[];
nPlots: NAT ← 0;
maxPlots: NAT ← 12;
allVersions: BOOL ← FALSE;
FOR arg: RopeList ← argList, arg.rest
UNTIL arg =
NIL
OR msg #
NIL
DO
IF arg.first.Fetch[0] = '-
THEN {
IF arg.first.Length[] >= 2
THEN
SELECT arg.first.Fetch[1]
FROM
'A, 'a => allVersions ← TRUE;
'H, 'h => allVersions ← FALSE;
IN ['0..'9] => {
max:
NAT ← Convert.CardFromRope[arg.first.Substr[1]
! Convert.Error => max ← maxPlots]; -- don't change it
maxPlots ← max;
};
ENDCASE; -- simply ignors illegal switches
}
ELSE {
fileList: RopeList ← FileListFrom[arg.first, wDir, allVersions];
FOR file: RopeList ← fileList, file.rest
UNTIL file =
NIL
OR msg #
NIL
DO
msg ←
IF nPlots >= maxPlots
THEN
IO.PutFR["Note: max. %g plots for each command.", IO.int[maxPlots]]
ELSE SimplePlot[file.first, nPlots # 0, cmd.out];
nPlots ← nPlots + 1;
ENDLOOP;
};
ENDLOOP;
};
}; -- SPlot
FileListFrom:
PROC [pattern, wDir: Rope.
ROPE ←
NIL, allVersions:
BOOL ←
FALSE]
RETURNS [fileList: RopeList ←
NIL] = {
root, lastRoot: Rope.ROPE ← NIL;
LinkIt:
FS.NameProc
--
PROC [fullFName]
RETURNS [continue:
BOOL] -- = {
excl: INT ← fullFName.Find["!"];
continue ← TRUE;
IF fullFName.Substr[excl-6, 6].Equal[".press", FALSE] THEN RETURN;
IF ~allVersions
THEN {
root ← fullFName.Substr[0, excl];
IF root.Equal[lastRoot,
FALSE]
THEN {
fileList.first ← fullFName;
RETURN;
};
lastRoot ← root;
};
fileList ← CONS[fullFName, fileList];
}; -- LinkIt
FS.EnumerateForNames[pattern, LinkIt, wDir];
TRUSTED {fileList ← LOOPHOLE[List.DReverse[LOOPHOLE[fileList]]]};
}; -- FileListFrom
SimplePlot:
PROC[file: Rope.
ROPE ←
NIL, iconic:
BOOL ←
TRUE, out:
IO.
STREAM]
RETURNS[msg: Rope.ROPE ← NIL] = {
s: IO.STREAM ← NIL;
viewer: ViewerClasses.Viewer;
curves: Curves ← NIL;
plotSpec: PlotSpec;
ok: BOOL ← TRUE;
fullName, status: Rope.ROPE ← NIL;
xmin, xmax, ymin, ymax: REAL;
IF file.IsEmpty[] THEN RETURN["No file name."];
[fullName, ]← FS.ExpandName[file, FileNames.CurrentWorkingDirectory[]];
s ← FS. StreamOpen[fullName ! FS.Error => {status ← error.explanation; ok ← FALSE; CONTINUE} ];
IF ok
THEN {
ENABLE {
DataError => {
msg ← reason;
ok ← FALSE;
CONTINUE;
};
IO.EndOfStream => {
msg ← Rope.Cat["End of file reached", status];
ok ← FALSE;
CONTINUE;
};
IO.Error => {
msg ← Rope.Cat[
SELECT ec
FROM
SyntaxError => "Syntax error",
Overflow => "Overflow in input conversion"
ENDCASE => IO.PutFR["IO Error # %g", IO.int[LOOPHOLE[ec, CARDINAL]]],
status];
ok ← FALSE;
CONTINUE;
};
ABORTED => {
msg ← "SPlot aborted. ... ";
ok ← FALSE;
CONTINUE;
};
};
set plotSpec
nVector: CARDINAL;
r: REAL;
plotSpec ← NEW[PlotSpecRec ← [file: fullName, time: BasicTime.Now[]]];
out.PutF[Rope.Cat["Reading ", fullName, "\n"]];
status ← " in getting the title.";
plotSpec.title ← s.GetRopeLiteral[];
status ← " in getting the number of curves.";
plotSpec.nCurvesMax ← Basics.LowHalf[s.GetCard[]];
plotSpec.legendEntries ← NEW[RopeSequence[plotSpec.nCurvesMax]];
FOR i:
CARDINAL
IN [0..plotSpec.nCurvesMax)
DO
status ← IO.PutFR[" in getting the name for curve #%g.", IO.int[i+1]];
plotSpec.legendEntries[i] ← s.GetRopeLiteral[];
ENDLOOP;
status ← " in getting the number of rows.";
nVector ← Basics.LowHalf[s.GetCard[]];
xmin ← ymin ← Real.LargestNumber;
xmax ← ymax ← Real.SmallestNormalizedNumber;
FOR i:
CARDINAL
IN [0..nVector)
DO
vector: Vector ← NEW[RealSequence[plotSpec.nCurvesMax + 1]];
FOR j:
CARDINAL
IN [0..plotSpec.nCurvesMax]
DO
ENABLE {
IO.Error => {
msg ← Rope.Cat[
SELECT ec
FROM
SyntaxError => "Syntax error",
Overflow => "Overflow"
ENDCASE => IO.PutFR["IO Error # %g", IO.int[LOOPHOLE[ec, CARDINAL]]],
IO.PutFR[" in getting the %g-th number on the %g-th row in the table.",
IO.int[j+1], IO.int[i+1]]];
GOTO exit;
};
IO.EndOfStream => {
msg ← Rope.Cat[
"End of file reached",
IO.PutFR[" in getting the %g-th number on the %g-th row in the table.",
IO.int[j+1], IO.int[i+1]]];
GOTO exit;
};
};
vector[j] ← r ← s.GetReal[];
IF j # 0
THEN {
ymax ← MAX[ymax, r];
ymin ← MIN[ymin, r];
};
ENDLOOP;
IF i = 0 THEN xmin ← vector[0]
ELSE IF i+1 = nVector THEN xmax ← vector[0];
curves ← CONS[vector, curves];
out.PutF["."];
REPEAT
exit => ok ← FALSE;
ENDLOOP;
};
IF ok
THEN {
cv: Curves ← NIL;
IF ymax = ymin
THEN {
IF ymax = 0.0 THEN { ymax ← 1.0; ymin ← -1.0 }
ELSE { ymax ← ymax + ABS[ymax]; ymin ← ymin - ABS[ymin]; };
};
IF xmax = xmin
THEN {
IF xmax = 0.0 THEN { xmax ← 1.0; xmin ← -1.0 }
ELSE { xmax ← xmax + ABS[xmax]; xmin ← xmin - ABS[xmin]; };
};
out.PutF[" plotting ... "];
plotSpec.bounds ← [xmin, ymin, xmax, ymax];
viewer ← CreateViewer[spec: plotSpec, iconic: iconic];
c.first is the last one just added to the list, so we should reverse the order.
FOR c: Curves ← curves, c.rest
UNTIL c =
NIL
DO
cv ← CONS[c.first, cv];
ENDLOOP;
FOR c: Curves ← cv, c.rest
UNTIL c =
NIL
DO
AddVector[viewer, c.first];
ENDLOOP;
out.PutF["done.\n"];
};
IF s # NIL THEN s.Close[];
}; -- SimplePlot
Commander.Register[
"SPlot", SPlot,
"SPlot <data files>, use a simple minded plot routine to plot data in the data files that have the following special format:
-- comment nodes are ignored.
-- carriage returns are treated the same as spaces.
<title> -- a text string enclosed by double quotes.
<xmin> <ymin> <xmax> <ymax> -- four real numbers.
<number of curves> -- Curves are functions of x.
<names of the curves> -- Each name is a text string enclosed by double quotes.
<number of rows>
-- Each row in the table below has a leading x value followed by the corresponding y values.
-- If there are n curves then the table for m different x values will be an m x n+1 matrix as follows.
x1 y1(x1) y2(x1) y3(x1) ... yn(x1)
x2 y1(x2) y2(x2) y3(x2) ... yn(x2)
... ... ... ... ... ... ... ... ... ... ... ... ... ...
xm y1(xm) y2(xm) y3(xm) ... yn(xm)
"];
}.