Skip to content

Commit

Permalink
start of issue #25
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmerz committed May 11, 2016
1 parent 0a0ad46 commit 68a5cb9
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 67 deletions.
7 changes: 4 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"program": "${workspaceRoot}/bin/cnf.bin.js",
"stopOnEntry": false,
"args": [
"--start=2003-09-15",
"--end=2003-11-15",
"--bs=X",
"matrix",
"--start=2003-09-15",
"--stop=2003-11-15",
//"--bs=X",

"--ts=.",
"--fs=\t",
"SR_WHI"
Expand Down
111 changes: 69 additions & 42 deletions nodejs/cmds/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,24 @@
var matrix = require('../matrix');
var config = require('../config').get();
var fs = require('fs');
var graphviz = require('graphviz');
var path = require('path');

module.exports = function (callback) {
var cmd_opts;
var matrix_output=[];
var matrix_data=[];
var nodes_output=[];
var node_list = {};

function command_options(program, cmd) {
var opts;
var i;
for (i = 0; i <= program.commands.length; i++) {
opts = program.commands[i];
if (opts._name === cmd) {
return opts;
}
}
return {};
}
// There has got to be a better way :)
cmd_opts = command_options(config, 'matrix');

// Defaults
cmd_opts.fs = cmd_opts.fs || ',';
if (cmd_opts.fs === ':tab:') {
cmd_opts.fs = "\t";

config.fs = config.fs || ',';
if (config.fs === ':tab:') {
config.fs = '\t';
}
config.ts = cmd_opts.ts || '@';
cmd_opts.rs = cmd_opts.rs || "\n";
cmd_opts.matrix = cmd_opts.matrix || "STDOUT";
cmd_opts.nodes = cmd_opts.nodes || null;

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

if (config.verbose) {
console.log('Running matrix command.\n');
Expand All @@ -59,46 +46,86 @@ module.exports = function (callback) {
console.log('Please provide a data repo location');
return callback();
}

matrix(config, function (rows) {

var header = ["i", "j", "k", "cost", "amplitude", "lower_bound", "upper_bound"];
if (!cmd_opts.no-header) {
matrix_output.push(header.join(cmd_opts.rs));
node_output.push("node");
if (!config['no-header']) {
matrix_output.push(header.join(config.fs));
nodes_output.push("node");
}

rows.forEach(function (r) {
node_list[r[0]]=true;
node_list[r[1]]=true;
if (cmd_opts.max_ub) {
if (config.max_ub) {
if (r[6] === null) {
r[6] = cmd_opts.max_ub;
r[6] = config.max_ub;
}
}
var line=r.join(cmd_opts.fs);
var line = r.join(config.fs);
matrix_output.push(line);
matrix_data.push(r);
});
if (cmd_opts.nodes !== null) {

if( config.outnodes ) {
nodes_output=Object.keys(node_list).sort();
if (cmd_opts.nodes === "STDOUT") {
console.log(nodes_output.join(cmd_opts.rs) + cmd_opts.rs);
if (config.nodes === "STDOUT") {
console.log(nodes_output.join(config.rs) + config.rs);
} else {
fs.writeFile(cmd_opts.nodes,
nodes_output.join(cmd_opts.rs)+cmd_opts.rs,'utf8',
fs.writeFile(config.outnodes,
nodes_output.join(config.rs)+config.rs,'utf8',
(err) => { if (err) {throw err;}});
}
}

if (cmd_opts.matrix !== null) {
if (cmd_opts.matrix==="STDOUT") {
console.log(matrix_output.join(cmd_opts.rs)+cmd_opts.rs);
} else {
fs.writeFile(cmd_opts.matrix,
matrix_output.join(cmd_opts.rs)+cmd_opts.rs,'utf8',
(err) => {if (err) { throw err;}});
}
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);
} else {
fs.writeFile(config.matrix,
matrix_output.join(config.rs)+config.rs,'utf8',
(err) => {if (err) { throw err;}});
}
}


callback();
});
// TODO: Should I catch errors here?
};

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

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

function createGraph(matrix) {
var nodes = {};
var g = graphviz.digraph('G');

matrix.forEach((link) => {
if( !nodes[link[0]] ) {
nodes[link[0]] = g.addNode(link[0], {});
}
if( !nodes[link[1]] ) {
nodes[link[1]] = g.addNode(link[1], {});
}

var origin = nodes[link[0]];
var terminal = nodes[link[1]];

g.addEdge(origin, terminal, {label: link[2]+':'+link[3]});
});

return g;
}
15 changes: 6 additions & 9 deletions nodejs/cmds/register/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
module.exports = function(program, run) {
program
.option('-s, --start <date>','Starting Date')
.option('-e, --end <date>','Ending Date of Interest')
.option('-B, --bs <sep>','Time step separator, default=@')
.on('--help', function(){
console.log('');
console.log(' More Info:');
console.log(' See the github repo & README: https://github.com/ucd-cws/calvin-network-tools');
console.log('');
});
.on('--help', function(){
console.log('');
console.log(' More Info:');
console.log(' See the github repo & README: https://github.com/ucd-cws/calvin-network-tools');
console.log('');
});

require('./build')(program, run);
require('./showBuild')(program, run);
Expand Down
6 changes: 4 additions & 2 deletions nodejs/cmds/register/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ module.exports = function(program, run) {
var cmd = program
.command('matrix')
.description('Create a network matrix')
.option('-f, --format <csv|tsv>', 'Output Format',/^(csv|tsv)$/,'tsv')
.option('-f, --format <csv|tsv|dot|png>', 'Output Format, dot | png (graphviz required)',/^(csv|tsv|dot|png)$/,'tsv')
.option('--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('--nodes <filename>','Send list of nodes to filename, default=no output, can use 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%"]]')
.action(run);
}
Expand Down
15 changes: 14 additions & 1 deletion nodejs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ module.exports = {
return config;
},
set : function(commander) {
config = commander;
config = commander;
commander.args.forEach(mergeSubCommandOptions);
}
};

function mergeSubCommandOptions(arg) {
if( typeof arg === 'string' ) {
return;
}

for( var key in arg ) {
if( key.match(/^_/) ) continue;
if( typeof arg[key] !== 'string' ) continue;
config[key] = arg[key];
}
}
12 changes: 6 additions & 6 deletions nodejs/matrix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ function matrix(config, callback) {
if( config.start ) {
config.start = new Date(config.start).getTime();
}
if( config.end ) {
config.end = new Date(config.end).getTime();
if( config.stop ) {
config.stop = new Date(config.stop).getTime();
}

hnf.split(config.data, {}, config.nodes, function (subnet) {
hnf.split(config.data, {id:'prmname'}, config.nodes, function (subnet) {
if (subnet.in.length === 0) {
subnet.in = subnet.out;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ function onSubnetReady(subnet, config, callback) {

for( i in rows_for ) {
rows_for[i].forEach(function(r) {
if ( r[0].indexOf("INITIAL")===0 && ! initial) {
if ( r[0].indexOf("INITIAL")===0 && ! initial) {
rows.push(['SOURCE',r[0],0,0,1,0,null]);
initial=true;
}
Expand All @@ -76,12 +76,12 @@ function onSubnetReady(subnet, config, callback) {
}

if ( (r[0].indexOf("INFLOW")===0 ||
r[0].indexOf("INBOUND")===0 ) && ! inbound[r[0]]) {
r[0].indexOf("INBOUND")===0 ) && ! inbound[r[0]]) {
rows.push(['SOURCE',r[0],0,0,1,0,null]);
inbound[r[0]]=true;
}
if ((r[1].indexOf("OUTBOUND")===0 ||
r[1].indexOf("SINK")===0 ) && ! outbound[r[1]]) {
r[1].indexOf("SINK")===0 ) && ! outbound[r[1]]) {
rows.push([r[1],'SINK',0,0,1,0,null]);
outbound[r[1]]=true;
}
Expand Down
6 changes: 3 additions & 3 deletions nodejs/matrix/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ module.exports = function(link, subnet) {
step = flow[i][0];
time = new Date(step).getTime();
// Get boundary Conditions
if( ( !config.start || config.start < time) && ( !config.end || time < config.end) ) {
if( ( !config.start || config.start < time) && ( !config.stop || time < config.stop) ) {
steps.push(flow[i][0]);
debugger;

if (netu.is_inbound(subnet,p.origin)) {
rows.push([
u.id('INBOUND',step),
Expand All @@ -56,7 +56,7 @@ module.exports = function(link, subnet) {
lb = step_bounds[i][0];
ub = step_bounds[i][1];
costs = step_costs[i];
debugger;

for( c = 0; c < costs.length; c++ ){
//console.log(i+"/"+c+":"+costs[c]);
// clb is greatest of link lower bound and cost lower bound
Expand Down
2 changes: 1 addition & 1 deletion nodejs/matrix/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function(item, subnet) {
time = new Date(step).getTime();
// Get boundary Conditions
if ((!config.start || config.start < time) &&
(!config.end || time < config.end)) {
(!config.stop || time < config.stop)) {
steps.push(flow[i][0]);

// Add Inflows from edge links
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"csv-stringify": "0.0.8",
"deep-diff": "^0.3.3",
"extend": "^3.0.0",
"graphviz": "0.0.8",
"hobbes-network-format": "0.0.6",
"minimist": "^1.2.0",
"node-uuid": "^1.4.7",
Expand Down

0 comments on commit 68a5cb9

Please sign in to comment.