XR06 recJs Example: Reading another CSV file
Outline
This recJs example script reads data from a another file (CSV like).
Platforms |
|
|||
Requirements |
|
|||
Parameters: |
|
|||
Content of example record files |
|
Output
Usage
- Copy the script code onto a file resource with "recJs" ending or use the impulse record examples.
- Open the impulse viewer with the file resource (e.g. double-click the file in the eclipse explorer).
R003 recJs Reader 10 Signal Script Script Examples impulse JDK Open JavaDoc
Script
The script creates 4 integer signals and fills them with the content of the external file.
//-recjs (keep this line) // generator : Record generator (de.toem.impulse.samples.ISingleDomainRecordGenerator) // file : File object of the executed recJs file (java.io.File) // p0..p9 : Parameters from the reader configuration (java.lang.String) // progress : Progress control (de.toem.impulse.cells.ports.IPortProgress) // console : Console output (de.toem.impulse.scripting.IScriptConsole) // Init the record generator.initRecord("Example Record", TimeBase.ns); var a = generator.addSignal(null, "a", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT); var b = generator.addSignal(null, "b", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT); var c = generator.addSignal(null, "c", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT); var d = generator.addSignal(null, "d", "", ProcessType.Discrete, SignalType.Integer, SignalDescriptor.DEFAULT); var wa /*:IIntegerSamplesWriter:*/ = generator.getWriter(a); var wb /*:IIntegerSamplesWriter:*/ = generator.getWriter(b); var wc /*:IIntegerSamplesWriter:*/ = generator.getWriter(c); var wd /*:IIntegerSamplesWriter:*/ = generator.getWriter(d); generator.open(0); // parameter p0 : delta var delta; if (typeof(p0) !== 'undefined') { delta = parseInt(p0); } if (isNaN(delta)) delta = 100; var current = 0; try { var br /*:BufferedReader:*/ = new BufferedReader(new FileReader(new File(file.getParent(), "test.csv"))); /* test.csv > a;b;c;f 1;1;2;3 2;3;4;5 1;1;2;3 2;3;4;5 */ var line /*:String:*/ = br.readLine(); // skip header while ((line = br.readLine()) != null) { // console.println(current+" "+line+" "+line.length()); var splitted = line.split(";"); wa.writeInt(current, false, splitted[0]); wb.writeInt(current, false, splitted[1]); wc.writeInt(current, false, splitted[2]); wd.writeInt(current, false, splitted[3]); current += delta; } } catch (e) { console.println(e); // pass exception object to error handler } generator.close(current + 1);