Skip to content

Commit

Permalink
first working example of complete pri/dss build
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmerz committed Nov 13, 2015
1 parent f9a304c commit 299b7f8
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 155 deletions.
45 changes: 13 additions & 32 deletions export/nodejs/dss/cost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,17 @@
var fs = require('fs');
var path = require('./path');

module.exports = function(properties) {
var costs = properties.costs;
var results = [];

if( !costs ) {
return results;
}

if( costs.type === 'Monthly Variable' ) {
for( var month in costs.costs ) {
var file = costs.costs[month];

if( !fs.existsSync(file) ) {
console.log('WARNING: '+file+' does not exist');
}

results.push({
csvFilePath : file,
type : 'paired',
label : month,
date : month,
location : properties.prmname,
xunits : 'KAF',
xtype : 'DIVR',
yunits : 'Penalty',
ytype : '',
path : path.monthlyPq(properties.prmname, month, 'dss')
});
}
}

return results;
module.exports = function(prmname, month, file) {
return {
csvFilePath : file,
type : 'paired',
label : month,
date : month,
location : prmname,
xunits : 'KAF',
xtype : 'DIVR',
yunits : 'Penalty',
ytype : '',
path : path.monthlyPq(prmname, month, 'dss')
};
};
18 changes: 4 additions & 14 deletions export/nodejs/dss/elArCap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@
var fs = require('fs');
var path = require('./path');

module.exports = function(properties) {
var el_ar_cap = properties.el_ar_cap;

if( !el_ar_cap ) {
return {};
}

if( !fs.existsSync(el_ar_cap) ) {
console.log('WARNING: '+el_ar_cap+' does not exist');
}

module.exports = function(prmname, file) {
return {
csvFilePath : el_ar_cap,
csvFilePath : file,
type : 'paired',
label : 'EL',
location : properties.prmname,
location : prmname,
xunits : 'FT',
xtype : 'UNT',
yunits : 'KA',
ytype : '',
path : path.eac(properties.prmname, 'dss')
path : path.eac(prmname, 'dss')
};
};
16 changes: 16 additions & 0 deletions export/nodejs/dss/evaporation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var fs = require('fs');
var path = require('./path');

module.exports = function(prmname, file) {
return {
csvFilePath : file,
type : 'timeseries',
parameter : 'EVAP_RATE(FT)',
location : prmname,
units : 'KAF',
xtype : 'PER-AVER',
path : path.evapo(prmname, 'dss')
};
};
17 changes: 17 additions & 0 deletions export/nodejs/dss/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var fs = require('fs');
var path = require('./path');

module.exports = function(prmname, file) {

return {
csvFilePath : file,
type : 'timeseries',
parameter : 'FLOW_LOSS(KAF)',
location : prmname,
units : 'KAF',
xtype : 'PER-AVER',
path : path.flow(prmname, 'dss')
};
};
6 changes: 5 additions & 1 deletion export/nodejs/dss/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = {
path : require('./path'),
eac : require('./elArCap'),
cost : require('./cost')
cost : require('./cost'),
flow : require('./flow'),
inflow : require('./inflow'),
evapo: require('./evaporation'),
bound : require('./timeBound')
};
17 changes: 17 additions & 0 deletions export/nodejs/dss/inflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var fs = require('fs');
var path = require('./path');

module.exports = function(prmname, name, file) {

return {
csvFilePath : file,
type : 'timeseries',
parameter : 'FLOW_LOC(KAF)',
location : prmname,
units : 'KAF',
xtype : 'PER-AVER',
path : path.in(prmname, name, 'dss')
};
};
30 changes: 24 additions & 6 deletions export/nodejs/dss/path.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
'use strict';
var utils = require('../pri/format/utils');

function writeTimeBound(type, prmname) {
/**
outputType == 'dss' || 'pri'
**/

// Bound types:
// STOR: Storage
// FLOW: flow
function writeTimeBound(type, prmname, boundType, outputType) {
return utils.parts(type, {
B : prmname,
// TODO: is this correct?
C : 'STOR_'+(type === 'UB' ? 'UBT' : 'LBT')+'(KAF)'
});
C : boundType+'_'+(type === 'QU' ? 'UBT' : 'LBT')+'(KAF)',
E : '1MON'
}, outputType);
//A=HEXT2014 B=SR-CMN_SR-CMN C=STOR_UBT(KAF) E=1MON F=CAMANCHE R FLOOD CAP
}

function writeFlow(prmname) {
return utils.parts('', {
B : prmname,
C : 'FLOW_LOSS(KAF)',
E : '1MON'
});
}

function writeMonthlyPq(prmname, month, outputType) {
return utils.parts('PQ',{
MO : month,
Expand All @@ -23,22 +39,23 @@ function writeIn(prmname, name, outputType) {
return utils.parts('IN',{
B : prmname,
C : 'FLOW_LOC(KAF)',
// E : '1MON' ... assumed
E : '1MON',
F: name
}, outputType);
}

function writeEvapo(prmname, outputType) {
return utils.parts('EV',{
B : prmname,
C : 'EL-AR-CAP'
C : 'EVAP_RATE(FT)',
E : '1MON'
}, outputType);
}

function writeEAC(prmname, outputType) {
return utils.parts('EAC',{
B : prmname,
C : 'EVAP_RATE(FT)'
C : 'EL-AR-CAP'
}, outputType);
}

Expand All @@ -54,6 +71,7 @@ function writeEmptyPq() {
module.exports = {
timeBound : writeTimeBound,
monthlyPq : writeMonthlyPq,
flow : writeFlow,
in : writeIn,
evapo : writeEvapo,
eac : writeEAC,
Expand Down
9 changes: 4 additions & 5 deletions export/nodejs/dss/timeBound.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
var fs = require('fs');
var path = require('./path');

module.exports = function(prmname, file) {
module.exports = function(type, prmname, boundType, file) {

return {
csvFilePath : file,
type : 'timeseries',
parameter : '1MON',
xunits : 'FT',
xunits : 'KAF',
location : prmname,
xtype : 'UNT',
yunits : 'KA',
ytype : '',
path : path.timeBound(prmname, 'dss')
path : path.timeBound(type, prmname, boundType, 'dss')
};
};
79 changes: 62 additions & 17 deletions export/nodejs/pri/format/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var sprintf = require('sprintf-js').sprintf;
var utils = require('./utils');
var fs = require('fs');
var dss = require('../../dss');

// from page 58 of manual
Expand All @@ -24,8 +25,7 @@ for( var i = 0; i < LINK_SPACING.length; i++ ) {

module.exports = function(config, node, type) {
var np = node.properties;
var link = writeLink(config, np, type || 'DIVR');
return link;
writeLink(config, np, type || 'DIVR');
};

function writeLink(config, np, type) {
Expand All @@ -41,8 +41,21 @@ function writeLink(config, np, type) {
var inf = '';
var cost = '', lowerBound = '', upperBound = '', constantBound = '';

var prmname = np.prmname;
if( np.type === 'Surface Storage' ) {
prmname = prmname+'_'+prmname;
}

// do we have bounds
if( np.bounds ) {

var boundType;
if( np.type === 'Surface Storage' ) {
boundType = 'STOR';
} else {
boundType = 'FLOW';
}

// add constant bounds
for( var i = 0; i < np.bounds.length; i++ ) {
var bound = np.bounds[i];
Expand All @@ -58,11 +71,21 @@ function writeLink(config, np, type) {
} else if( bound.type === 'LBM' ) {
b += writeMonthlyBound('LB', bound)+'\n';
} else if( bound.type === 'UBT' ) {
b += dss.path.timeBound('QU', np.prmname)+'\n';
if( !fs.existsSync(bound.bound) ) {
console.log('File not found, ignoring: '+bound.bound);
continue;
}
// set pri path
b += dss.path.timeBound('QU', prmname, boundType)+'\n';
// set dss writer json object
config.ts.data.push(dss.bound('QU', prmname, boundType, bound.bound));



} else if( bound.type === 'LBT' ) {
b += dss.path.timeBound('QL', np.prmname)+'\n';
// set pri path
b += dss.path.timeBound('QL', prmname, boundType)+'\n';
// set dss writer json object
config.ts.data.push(dss.bound('QL', prmname, boundType, bound.bound));
}
}
}
Expand All @@ -71,36 +94,58 @@ function writeLink(config, np, type) {
// Monthly Variable Types Require a PQ
if( np.costs.type === 'Monthly Variable' ) {
for( var month in np.costs.costs ){
pq += dss.path.monthlyPq(month, np.prmname)+'\n';
if( !fs.existsSync(np.costs.costs[month]) ) {
console.log('File not found, ignoring: '+np.costs.costs[month]);
continue;
}

// set pri path
pq += dss.path.monthlyPq(month, prmname)+'\n';
// set dss writer json object
config.pd.data.push(dss.cost(prmname, month, np.costs.costs[month]));
}

} else if( np.costs.cost >= 0 ) {
cost = np.costs.cost.toFixed(4);

//IF COST IS ZERO, we need a PQ
} else {
pq += dss.path.emptyPq()+'\n';
}

var dssEntries = dss.costs(np);
for( var i = 0; i < dssEntries.length; i++ ) {
config.pri.pd.data.push(dssEntries[i]);
pq += dss.path.empty()+'\n';
}
}

if( np.el_ar_cap ) {
eac = dss.path.eac(np.prmname)+'\n';
config.pri.pd.data.push(dss.eac(np));
if( !fs.existsSync(np.el_ar_cap) ) {
console.log('File not found, ignoring: '+np.el_ar_cap);
} else {
eac = dss.path.eac(prmname)+'\n';
config.pd.data.push(dss.eac(np.prmname, np.el_ar_cap));
}
}


if( np.inflows ) {
for( var name in np.inflows ) {
config.pri.inflowlist.push(writeIn(np.prmname, name));
if( !fs.existsSync(np.inflows[name].inflow) ) {
console.log('File not found, ignoring: '+np.inflows[name].inflow);
continue;
}

config.pri.inflowlist.push(writeIn(prmname, name));

// set dss writer json object
config.ts.data.push(dss.inflow(prmname, name, np.inflows[name].inflow));
}
}

if( np.evaporation ) {
ev = dss.path.evapo(np.prmname)+'\n';
if( !fs.existsSync(np.evaporation) ) {
console.log('File not found, ignoring: '+np.evaporation);
} else {
ev = dss.path.evapo(prmname)+'\n';
// set dss writer json object
config.ts.data.push(dss.evapo(prmname, np.evaporation));
}
}

var link = '';
Expand All @@ -116,7 +161,7 @@ function writeLink(config, np, type) {
}

if( np.type === 'Surface Storage' ) {
link = sprintf(LINK_FORMAT, 'LINK', '', 'RSTO', np.prmname, np.prmname, amplitude, cost, lowerBound, upperBound, constantBound)+'\n';
link = sprintf(LINK_FORMAT, 'LINK', '', 'RSTO', prmname, prmname, amplitude, cost, lowerBound, upperBound, constantBound)+'\n';
link += sprintf('%-8.8s %-80.80s', 'LD', np.description || '')+'\n';
link += b;
link += ev;
Expand Down
2 changes: 1 addition & 1 deletion export/nodejs/pri/format/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports.months = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','

module.exports.parts = function(type, p, outputType) {
if( outputType === 'dss' ) {
return sprintf('/%s/%s/%s/%s/%s/%s',
return sprintf('/%s/%s/%s/%s/%s/%s/',
(p.A) ? p.A : '',
(p.B) ? p.B : '',
(p.C) ? p.C : '',
Expand Down
Loading

0 comments on commit 299b7f8

Please sign in to comment.