Skip to content

Commit

Permalink
updates for matrix arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmerz committed May 12, 2016
1 parent 42c0e69 commit 600cf22
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
41 changes: 26 additions & 15 deletions nodejs/cmds/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ module.exports = function (callback) {
config.fs = '\t';
}

config.format = config.format || 'csv';
config.ts = config.ts || '@';
config.rs = config.rs || '\n';
config.matrix = config.matrix || 'STDOUT';
config.to = config.to || 'STDOUT';

if( config.outboundPenalty ) {
config.outboundPenalty = JSON.parse(config.outboundPenalty);
}

if (config.verbose) {
console.log('Running matrix command.\n');
Expand All @@ -58,9 +63,9 @@ module.exports = function (callback) {
rows.forEach(function (r) {
node_list[r[0]]=true;
node_list[r[1]]=true;
if (config.max_ub) {
if (config.maxUb) {
if (r[6] === null) {
r[6] = config.max_ub;
r[6] = config.maxUb;
}
}
var line = r.join(config.fs);
Expand All @@ -79,17 +84,23 @@ module.exports = function (callback) {
}
}

if ( config.matrix ) {
if( config.matrix.match(/\.dot$/i) ) {
toDot(matrix_data);
} else if( config.matrix.match(/\.png$/i) ) {
toPng(matrix_data);
} else if( config.matrix === 'STDOUT' ) {
console.log(matrix_output.join(config.rs)+config.rs);
if( config.format === 'png' ) {
toPng(matrix_data);
} else {
var output;
if( config.format === 'dot' ) {
output = toDot(matrix_data);
} else {
output = matrix_output.join(config.rs)+config.rs;
}

if( config.to === 'STDOUT' ) {
console.log(output);
} else {
fs.writeFile(config.matrix,
matrix_output.join(config.rs)+config.rs,'utf8',
(err) => {if (err) { throw err;}});
fs.writeFileSync(
path.join(process.cwd(), `${config.output}.${config.format}`),
output
);
}
}

Expand All @@ -101,12 +112,12 @@ module.exports = function (callback) {

function toPng(matrix) {
var g = createGraph(matrix);
g.output('png', path.join(process.cwd(), config.matrix));
g.output('png', path.join(process.cwd(), `${config.output}.png`));
}

function toDot(matrix) {
var g = createGraph(matrix);
fs.writeFileSync(path.join(process.cwd(), config.matrix), g.to_dot());
return g.to_dot();
}

function createGraph(matrix) {
Expand Down
11 changes: 6 additions & 5 deletions nodejs/cmds/register/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ module.exports = function(program, run) {
.command('matrix')
.description('Create a network matrix')
.option('-f, --format <csv|tsv|dot|png>', 'Output Format, dot | png (graphviz required)',/^(csv|tsv|dot|png)$/,'tsv')
.option('--no-header','Supress Header')
.option('-N, --no-header','Supress Header')
.option('-S, --ts <sep>','Time step separator, default=@')
.option('-F, --fs <sep>','Field Separator, default=,')
.option('-s, --start [YYYY-MM]', 'Specify start date for TimeSeries data')
.option('-t, --stop [YYYY-MM]', 'Specify stop date for TimeSeries data')
.option('--max_ub <number>','Replace null upperbound with a big number. Like 1000000')
.option('--matrix <filename>','Send matrix to filename, default=STDOUT')
.option('--outnodes <filename>','Send list of nodes to filename, default=no output, can use STDOUT')
.option('--outbound_penalty <json>','Specify a penalty function for outbound boundary conditions. eg. [[10000,"-10%"],[0,0],[-10000,"10%"]]')
.option('-M, --max-ub <number>','Replace null upperbound with a big number. Like 1000000')
//.option('-m, --matrix <filename>','Send matrix to filename, default=STDOUT')
.option('-t, --to <filename>','Send matrix to filename, default=STDOUT')
.option('-O, --outnodes <filename>','Send list of nodes to filename, default=no output, can use STDOUT')
.option('-p, --outbound-penalty <json>','Specify a penalty function for outbound boundary conditions. eg. [[10000,"-10%"],[0,0],[-10000,"10%"]]')
.action(run);
}

Expand Down
7 changes: 2 additions & 5 deletions nodejs/matrix/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ module.exports = function(item, subnet) {
step = flow[i][0];
time = new Date(step).getTime();
// Get boundary Conditions
<<<<<<< HEAD

if ((!config.start || config.start < time) &&
(!config.stop || time < config.stop)) {
=======
if ((!config.start || config.start <= time) &&
(!config.end || time <= config.end)) {
>>>>>>> 250dc63dea058bd8f3477cb2e5b825968f93a1db

steps.push(flow[i][0]);

// Add Inflows from edge links
Expand Down
6 changes: 3 additions & 3 deletions tests/all/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ This directory contains some complete output files.

This fist example creates a single water year run for the complete model
```{bash}
cnf --start=2002-09-15 --end=2003-09-15 matrix --ts=. --fs=:tab: --max_ub=1000000 \
cnf --start=2002-09-15 --end=2003-09-15 matrix --ts=. --fs=:tab: --max-ub=1000000 \
--nodes=all_wy2003.node --matrix=all_wy2003.dat
```

This second example creates a decade of data.

```{bash}
cnf --start=1990-09-15 --end=2000-09-15 matrix --ts=. --fs=:tab: --max_ub=1000000 \
cnf --start=1990-09-15 --end=2000-09-15 matrix --ts=. --fs=:tab: --max-ub=1000000 \
--nodes=all_wys90.node --matrix=all_wys90.dat
```

This third example creates 25 years of data.

```{bash}
cnf --start=1975-09-15 --end=2000-09-15 matrix --ts=. --fs=:tab: --max_ub=1000000 \
cnf --start=1975-09-15 --end=2000-09-15 matrix --ts=. --fs=:tab: --max-ub=1000000 \
--nodes=all_25.node --matrix=all_25.dat
```
2 changes: 1 addition & 1 deletion tests/north/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ This is a simple example of a small set of nodes in the North part of the model,
nodes='A101 C2 C5 C87 CVPM01G CVPM01S D5 D73 D74 D94 EXT_REDDIN GW_01
HGP01 HGR01 HNP101 HP101 HSD101 HSU101D5 HSU101D74 HSU101SR3 HU101
HXI101 INT_REDDIN SR_CLE SR_SHA SR_WHI U101 WTP101';
cnf --start=2001-09-15 --end=2002-10-15 matrix --ts=. --fs=:tab: --max_ub=1000000 \
cnf --start=2001-09-15 --end=2002-10-15 matrix --ts=. --fs=:tab: --max-ub=1000000 \
--nodes=north.nodes --matrix=north.dat $nodes;
```
2 changes: 1 addition & 1 deletion tests/one-link/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ These examples can be replicated with the command
```{bash}
for i in SR_WHI-D5 SR_CLE-D94;
do n=`echo $i | tr [:upper:] [:lower:]`;
cnf --start=2001-08-01 --end=2001-10-01 matrix --ts=. --fs=:tab: --max_ub=1000000 \
cnf --start=2001-08-01 --end=2001-10-01 matrix --ts=. --fs=:tab: --max-ub=1000000 \
--nodes=$n.nodes --matrix=$n.dat $i;
done
```
Expand Down
2 changes: 1 addition & 1 deletion tests/one-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ These examples can be replicated with the command
```{bash}
for i in SR_WHI D94;
do n=`echo $i | tr [:upper:] [:lower:]`;
cnf --start=2001-08-01 --end=2001-10-01 matrix --ts=. --fs=:tab: --max_ub=1000000 \
cnf --start=2001-08-01 --end=2001-10-01 matrix --ts=. --fs=:tab: --max-ub=1000000 \
--nodes=$n.nodes --matrix=$n.dat $i;
done
```
Expand Down

0 comments on commit 600cf22

Please sign in to comment.