Skip to content

Commit

Permalink
fix: read CSV file from a path relative to the model directory (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToddFincannon committed Jul 24, 2021
1 parent cf15d97 commit c05f2f9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/CodeGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export let codeGenerator = (parseTree, opts) => {
outputAllVars = true
}
// Function to generate a section of the code
let generateSection = R.map(v => new EquationGen(v, extData, directData, mode).generate())
let generateSection = R.map(v => new EquationGen(v, extData, directData, mode, modelDirname).generate())
let section = R.pipe(generateSection, R.flatten, lines)
function generate() {
// Read variables and subscript ranges from the model parse tree.
Expand Down
8 changes: 6 additions & 2 deletions src/EquationGen.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import R from 'ramda'
import XLSX from 'xlsx'
import { ModelLexer, ModelParser } from 'antlr4-vensim'
Expand Down Expand Up @@ -34,7 +35,7 @@ import {
} from './Helpers.js'

export default class EquationGen extends ModelReader {
constructor(variable, extData, directData, mode) {
constructor(variable, extData, directData, mode, modelDirname) {
super()
// the variable we are generating code for
this.var = variable
Expand All @@ -44,6 +45,8 @@ export default class EquationGen extends ModelReader {
this.directData = directData
// set to 'decl', 'init-lookups', 'eval', etc depending on the section being generated
this.mode = mode
// The model directory is required when reading data files for GET DIRECT DATA.
this.modelDirname = modelDirname
// Maps of LHS subscript families to loop index vars for lookup on the RHS
this.loopIndexVars = new LoopIndexVars(['i', 'j', 'k'])
this.arrayIndexVars = new LoopIndexVars(['v', 'w'])
Expand Down Expand Up @@ -351,7 +354,8 @@ export default class EquationGen extends ModelReader {
}
} else {
// The file is a CSV pathname. Read it now.
let data = readCsv(file, tab)
let csvPathname = path.resolve(this.modelDirname, file)
let data = readCsv(csvPathname, tab)
if (data) {
getCellValue = (c, r) => (data[r] != null ? cdbl(data[r][c]) : null)
}
Expand Down

0 comments on commit c05f2f9

Please sign in to comment.