-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathautoMarz.js
125 lines (106 loc) · 3.87 KB
/
autoMarz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// iojs head script to run marz from a command line interface
var debug = function (output) {
if (debugFlag) {
if (typeof output == "string") {
console.log(output);
} else {
console.dir(output);
}
}
};
var help = function () {
console.error("Usage: autoMarz.js <FITSfilename>|<FITSfoldername> [--debug] [--outFile <filename>]|[--dir dirname]\n");
console.error("For a full list of options, consult autoConfig.js");
console.error("Examples:");
console.error("Analyse file /tmp/fits.fits and output to stdout:\n\tautoMarz.js /tmp/fits.fits\n");
console.error("Analyse file /tmp/fits.fits and output to /tmp/out.mz:\n\tautoMarz.js /tmp/fits.fits --ooutFile=/tmp/out.mz\n");
console.error("Analyse file /tmp/fits.fits and output to directory /tmp/saves:\n\tautoMarz.js /tmp/fits.fits --dir=/tmp/saves\n");
process.exit();
};
function getOutputFilename(fitsFile, argv) {
var fname = argv["outFile"] || path.basename(fitsFile);
var dname = argv['dir'] || path.dirname(fitsFile);
if (!fname.endsWith(".mz")) {
fname = fname.substring(0, fname.lastIndexOf('.')) + ".mz";
}
var outputFile = path.normalize(path.join(dname, fname));
debug("Input file " + fitsFile + " output going to " + outputFile);
return outputFile;
}
var path = require('path');
var cluster = require('cluster');
var fs = require('fs');
var $q = require('q');
var appPath = __dirname;
eval(fs.readFileSync(path.join(appPath, "./js/extension.js")) + '');
var args = require('minimist')(process.argv.slice(2));
var argv = require(path.join(appPath, './autoConfig.js'));
for (var att in args) {
if (typeof argv[att] == "string" || att == "_") {
argv[att] = args[att];
} else {
argv[att] = JSON.parse(args[att]);
}
}
var debugFlag = Boolean(argv['debug']);
var log = {
"debug": function (e) {
debug(e);
}
};
if (cluster.isMaster) {
var filenames = argv['_'];
debug("Input Parameters:");
debug(argv);
if (filenames == null || filenames.length == 0) {
help();
}
var filename = filenames[0];
n = argv['numCPUs'] || Math.max(1, require('os').cpus().length - 1);
var workers = [];
for (var i = 0; i < n; i++) {
workers.push(cluster.fork());
}
var queue = [];
var totalNum = 0;
for (var i = 0; i < filenames.length; i++) {
var stat = fs.lstatSync(filenames[i]);
if (stat.isDirectory()) {
var files = fs.readdirSync(filenames[i]);
for (var j = 0; j < files.length; j++) {
if (files[j].endsWith(".fits")) {
queue.push(path.normalize(path.resolve(filenames[i], files[j])));
}
}
} else {
queue.push(path.normalize(filenames[i]));
}
}
queue = queue.unique();
debug("Queue contains:");
debug(queue);
var globalStartTime = new Date();
var struct = require(path.join(appPath, './js/nodeMethods'));
struct.init(workers, log, argv);
var counter = 0;
var totalLength = queue.length;
var handleReturn = function (num) {
totalNum += num;
if (queue.length > 0) {
var filename = queue.shift();
counter += 1;
console.log(counter + "/" + totalLength + ": Analysing " + filename);
var outputName = getOutputFilename(filename, argv);
struct.runFitsFile(filename, outputName, debug, debugFlag).then(handleReturn);
} else {
var globalEndTime = new Date();
var elapsed = (globalEndTime - globalStartTime) / 1000;
debug("All files processed in " + elapsed + " seconds, an average of " + (totalNum / elapsed).toFixed(2) + " spectra per second");
cluster.disconnect();
}
};
handleReturn(0);
} else {
debug("Worker spawned");
require('./js/worker2.js')
}