Reading ASCII files in EcosimPro-PROOSIS

Pedro Cobas. EcosimPro Development Team

Two new classes in EL have been created to be able to read ASCII files and strings simply and intuitively. Until now, an EcosimPro/PROOSIS user who wanted to read these files had to create the functionality in FORTRAN or in C/C++ as an external library; as of this new version they can now be read simply and intuitively in components, functions and experiments using the tool.

Two classes were created for that purpose:

PARSER_FILE: to read ASCII files

PARSER_STRING: to read strings

The PARSER_FILE class can be used to read the file token by token and take the results to corresponding EL variables. It of course requires knowing the coding of the file to be read.

The PARSER_STRING class is similar, but instead of associating the reading to a file, it associates it with a STRING/FILEPATH variable. The only difference is that instead of the open() method, it has a set() method to associate it to the string.

For example, here is a complete case of using the PARSER_FILE class. The objective is to read a file called “file3.txt” in the following format:

2 1.45 3 3 1 TXT 3 1

2 2.43 3 3 2 TXT 3 0

2 2.24 2 3 3 ART 3 1

2 3.19 3 3 4 ATR 3 0

Suppose we are only interested in the fields 2 (REAL), 5 (INTEGER), 6 (STRING) and 8 (BOOLEAN). The program to read this file could be modelled in a function as follows:

FUNCTION BOOLEAN parseFile()
DECLS
     REAL vr=0
INTEGER vi=0
BOOLEAN vb= FALSE
     STRING vs=””
OBJECTS
      PARSER_FILE fi
BODY

— set the contents

fi.open(“file3.txt”)

— parse columns 2(REAL) 5(INTEGER) 6(STRING) 8(BOOLEAN)

WHILE( fi.is_end() == FALSE )

fi.skipTokens(1)

fi.getReal(vr)

fi.skipTokens(2)

fi.getInteger(vi)

fi.getString(vs)

fi.skipTokens(1)

fi.getBoolean(vb)

WRITE(“vr=%g vd=%d vs=%s vb=%s\n”,vr,vi,vs,gvalBool(vb))

END WHILE

RETURN TRUE

END FUNCTION

If we run the function we get the following output:

vr=1.45 vd=1 vs=TXT vb=TRUE

vr=2.43 vd=2 vs=TXT vb=FALSE

vr=2.24 vd=3 vs=ART vb=TRUE

vr=3.19 vd=4 vs=ATR vb=FALSE

This is the desired result, since it has correctly read all the fields in the file and has skipped over the fields we were not interested in.

Not only do these classes allow this type of reading, they also allow other more advanced capacities such as:

  • read(). reading a fixed number of bytes
  • seek(). Skip to any position in a file
  • size(): Obtain the size of the file or string
  • nlines(): Obtain the number of lines in a file
  • position(), getLineColumn(): Obtain the current position in the file/string
  • str(), obtain all the contents in one string
  • getLIne(), obtain a complete line
  • getReal(), getInteger(), getBoolean() and getString() to read different types of fields If they cannot be read, they return FALSE
  • skipTokens(), to skip fields in the file/string