General HydroStatics
Ship Stability Software
Command of the Week
(New or interesting aspects of GHS that you may not know about)

READing the future
(Requires GHS version 17.50 or later)

Back at the dawn of GHS time when READ was designed, there was this primitive idea that Geometry files and Run files were the only types of input files a reasonable naval architect would want to deal with.

A few years later a forward-looking individual came along who wanted GHS to read data files for other purposes.

"Fine," we said. "We'll give the READ command an alternate mode whereby it will address line-oriented text files." And everyone was happy ... for a while.

Now someone wants GHS to read from two text files simultaneously. Where will it end? It's hard to read the future.

Unless the files are very large, you can multiplex the READ (DATA) command by making a macro that reads one line then closes the file, keeping track of the line number. No doubt you see where this is going: the next Read for that file skips past the lines that have already been read.

The macro would look something like this:
macro ReadIn /Function `%1: file number
 IF {File%1Pos}=-1 THEN EXIT
 READ (DATA) "{File%1}"
 VARIABLE (STR) S
 macro Skip
  READ S /LINE
 //
 .Skip (File%1Pos)
 READ S /LINE
 ReadIn:=S
 IF EOF THEN File%1Pos:=-1 ELSE File%1Pos:=File%1Pos+1
 READ (DATA) OFF
/
Another short macro would be handy for defining and initializing necessary variables:
macro DataIn `%1: file number; %2: file name
 VARIABLE (STR) File%1="%2"
 VARIABLE File%1Pos=0
/
We tested it by making a pair of text files and a macro to read them both:

.DataIn 1, "Soundings.txt"
.DataIn 2, "Headings.txt"

VARIABLE (STR) Sounding, Heading

macro ReadBoth
 IF {File1Pos}<0 THEN EXIT
 Sounding:=ReadIn(1)
 Heading:=ReadIn(2)
 \Sounding={Sounding} Heading={Heading}
 EXIT ReadBoth
/
.ReadBoth
What you lose by always doing READ /LINE is the ability to read items from one line into separate variables. But this is easily overcome using the ITEM function to pick the next item from a string.

Note that the same DataIn and ReadIn macros are good for reading as many files as you like simultaneously. Bring on the future!

Questions, comments, or requests?
Contact Creative Systems, Inc.

support@ghsport.com

USA phone: 360-385-6212 Fax: 360-385-6213
Office hours: 7:00 am - 4:00 pm Pacific Time, Monday - Friday

Mailing address:
PO Box 1910
Port Townsend, WA 98368 USA

www.ghsport.com

Click here for an index to this and previous COWs