This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
157 lines (128 loc) · 4.28 KB
/
convert.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import fs from 'fs';
const fsPromises = fs.promises;
const readFile = async (path) => {
try {
return fsPromises.readFile(`${path}`);
} catch (error) {
console.error(error);
}
}
const writeFile = async (path, content) => {
try {
return fsPromises.writeFile(`${path}`, content);
} catch (error) {
console.error(error);
}
}
const listDir = async (dir) => {
try {
return fsPromises.readdir(dir);
} catch (err) {
console.error('Error occured while reading directory!', err);
}
}
const readFiles = async (paths) => {
const fileList = await listDir("./input");
let result = {};
// Put all promises in a map so they can be resolved nicely
// while also putting them in the result object.
const fileOps = fileList.map(async (el) => {
// keep out non-csv files
if (!~el.indexOf('.csv')) {
return;
}
const name = el.replace('.csv', '')
const promise = await readFile(`./input/${el}`);
result[name] = promise.toString().split(/\r?\n/);
return promise;
});
// Wait for it...
await Promise.all(fileOps)
// ...and just return the finished object.
return result;
}
const writeFiles = async (files) => {
Object.keys(files).forEach(async el => {
writeFile(`./output/${el}`, files[el]);
})
}
const filterFiles = (parsedFiles) => {
Object.keys(parsedFiles).forEach(file => {
parsedFiles[file].forEach((el, i) => {
parsedFiles[file][i] = el.replace(/\d+,/g, '');
})
})
return parsedFiles;
}
const destructFilename = (filename) => {
// let name,
// measurementTime;
const dataset = filename.split('-t');
if (dataset.length < 2) {
throw new Error("Filename does not contain a measurement time!");
}
return [dataset[0], dataset[1]];
}
const prepareXML = async (filteredFiles) => {
let result = {};
let xmlTemplate = await readFile(`template.xml`);
xmlTemplate = xmlTemplate.toString();
Object.keys(filteredFiles).forEach(el => {
let preparedText = xmlTemplate;
let configCoefficients = '',
formatVersion,
deviceName,
spectrumName,
serialNumber,
channels,
polynomialOrder,
csvDataPoints = '',
measurementTime = 0,
filename;
try {
[filename, measurementTime] = destructFilename(el);
} catch (error) {
console.error(error.message);
}
config.coefficients.forEach(coefficient => {
configCoefficients += `<Coefficient>${coefficient}</Coefficient>\n`;
})
formatVersion = `<FormatVersion>${config.formatversion}</FormatVersion>`;
measurementTime = `<MeasurementTime>${measurementTime}</MeasurementTime>`;
deviceName = `<Name>${config.devicename}</Name>`;
serialNumber = `<SerialNumber>${config.serialnumber}</SerialNumber>`;
channels = `<NumberOfChannels>${config.channels}</NumberOfChannels>`;
polynomialOrder = `<PolynomialOrder>${config.polynomialorder}</PolynomialOrder>`;
spectrumName = `<SpectrumName>${filename}</SpectrumName>`;
filteredFiles[el].forEach(datapoint => {
if (datapoint) {
csvDataPoints += `<DataPoint>${datapoint}</DataPoint>\n`;
}
})
preparedText = preparedText
.replace('<FormatVersion />', formatVersion)
.replace('<MeasurementTime />', measurementTime)
.replace('<Name />', deviceName)
.replace('<NumberOfChannels />', channels)
.replace('<SpectrumName />', spectrumName)
.replace('<SerialNumber />', serialNumber)
.replace('<PolynomialOrder />', polynomialOrder)
.replace('<ConfigCoefficients />', configCoefficients)
.replace('<CSVDataPoints />', csvDataPoints);
result[`${filename}.xml`] = preparedText;
});
return result;
}
let configStream;
try {
configStream = await readFile('config.json');
} catch (error) {
console.info(`Note: Please make sure you created the config.json as defined in the documentation.`);
console.error(`Message: ${error.message}`);
process.exit(1);
}
const config = JSON.parse(configStream.toString());
const files = await readFiles();
const filteredFiles = filterFiles(files);
const preparedXML = await prepareXML(filteredFiles);
writeFiles(preparedXML);