-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.mjs
347 lines (290 loc) · 12.4 KB
/
parse.mjs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { readdirSync, readFileSync, writeFileSync, rmSync, mkdirSync, createWriteStream } from 'fs';
import { join, basename, dirname } from 'path';
import { standards, writeFB } from 'spacedatastandards.org';
import cluster from 'cluster';
import os from 'os';
import readline from 'readline';
import cliProgress from 'cli-progress';
import archiver from 'archiver';
import { fileURLToPath } from 'url';
import { createBrotliCompress, constants as zlibConstants } from 'zlib';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const { OEMT, CATT, objectType, opsStatusCode, ephemerisDataBlockT, ephemerisDataLineT, covarianceMatrixLineT, RFMT, refFrame } = standards.OEM;
const promptUser = (question) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim().toLowerCase());
});
});
};
const clearDirectory = (dir) => {
if (readdirSync(dir).length) {
rmSync(dir, { recursive: true, force: true });
}
mkdirSync(dir, { recursive: true });
};
export const parseEphemerisFile = (filePath) => {
const fileContent = readFileSync(filePath, 'utf8');
const lines = fileContent.split('\n').filter(line => line.trim() !== '');
const filename = basename(filePath, '.txt');
const [_, noradId, objectName, satelliteId, operationalStatus, startTimeUnix, classification] = filename.split('_');
const nextOEMT = new OEMT();
//nextOEMT.CLASSIFICATION = classification;
// Set parsed values
const nextCAT = new CATT();
nextCAT.NORAD_CAT_ID = noradId;
//nextCAT.OBJECT_NAME = objectName;
nextCAT.OBJECT_TYPE = objectType.PAYLOAD;
nextCAT.OPS_STATUS_CODE = opsStatusCode[operationalStatus.toUpperCase()];
nextOEMT.OBJECT = nextCAT;
//nextOEMT.ORIGINATOR = 'SPACEX';
const nextEDB = new ephemerisDataBlockT();
nextEDB.START_TIME = '';
nextEDB.STOP_TIME = '';
nextEDB.STEP_SIZE = 0;
nextEDB.REFERENCE_FRAME = refFrame.J2000; // This is not stated in the file or metadata
let dataStartIndex = -1;
// Parse header
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.includes('created:')) {
nextOEMT.CREATION_DATE = line.split('created:')[1].trim();
}
if (line.includes('ephemeris_start:')) {
nextEDB.START_TIME = line.split('ephemeris_start:')[1].split('ephemeris_stop:')[0].trim();
}
if (line.includes('ephemeris_stop:')) {
nextEDB.STOP_TIME = line.split('ephemeris_stop:')[1].split('step_size:')[0].trim();
}
if (line.includes('step_size:')) {
nextEDB.STEP_SIZE = parseFloat(line.split('step_size:')[1].trim());
}
if (line.includes('ephemeris_source:')) {
}
if (line.trim() === 'UVW') { //HAAAAAAAAAACK
let covariance_frame = line.trim();
dataStartIndex = i + 1;
const rF = new RFMT();
if (refFrame[covariance_frame]) {
rF.REFERENCE_FRAME = refFrame[covariance_frame];
} else {
throw Error(`Reference Frame Not Found: '${covariance_frame}'`);
}
nextEDB.COV_REFERENCE_FRAME = rF;
}
}
// Check that the steps are consistent
let lastEpoch = null;
// Parse data section
for (let i = dataStartIndex; i < lines.length; i += 4) {
let ephemDataBlock = new ephemerisDataBlockT();
if (i + 3 < lines.length) {
const stateLine = lines[i].split(/\s+/);
const covLine1 = lines[i + 1].split(/\s+/);
const covLine2 = lines[i + 2].split(/\s+/);
const covLine3 = lines[i + 3].split(/\s+/);
const epoch = stateLine[0];
const [year, doy, time] = [epoch.slice(0, 4), epoch.slice(4, 7), epoch.slice(7, 13)];
const date = new Date(Date.UTC(parseInt(year), 0, parseInt(doy),
parseInt(time.slice(0, 2)),
parseInt(time.slice(2, 4)),
parseInt(time.slice(4, 6))));
if (lastEpoch) {
const expectedDate = new Date(lastEpoch.getTime() + nextEDB.STEP_SIZE * 1000);
if (Math.abs(date - expectedDate) > 1000) {
throw new Error(`Inconsistent step size at line ${i}: expected ${expectedDate.toISOString()}, but got ${date.toISOString()}`);
}
}
lastEpoch = date;
let ephemLine = new ephemerisDataLineT();
//ephemLine.EPOCH = date.toISOString();
ephemLine.X = parseFloat(stateLine[1]);
ephemLine.Y = parseFloat(stateLine[2]);
ephemLine.Z = parseFloat(stateLine[3]);
ephemLine.X_DOT = parseFloat(stateLine[4]);
ephemLine.Y_DOT = parseFloat(stateLine[5]);
ephemLine.Z_DOT = parseFloat(stateLine[6]);
ephemDataBlock.EPHEMERIS_DATA_LINES.push(ephemLine);
let covLine = new covarianceMatrixLineT();
//covLine.EPOCH = date.toISOString();
covLine.CX_X = parseFloat(covLine1[0]);
covLine.CY_X = parseFloat(covLine1[1]);
covLine.CY_Y = parseFloat(covLine1[2]);
covLine.CZ_X = parseFloat(covLine1[3]);
covLine.CZ_Y = parseFloat(covLine1[4]);
covLine.CZ_Z = parseFloat(covLine1[5]);
covLine.CX_DOT_X = parseFloat(covLine1[6]);
covLine.CX_DOT_Y = parseFloat(covLine2[0]);
covLine.CX_DOT_Z = parseFloat(covLine2[1]);
covLine.CX_DOT_X_DOT = parseFloat(covLine2[2]);
covLine.CY_DOT_X = parseFloat(covLine2[3]);
covLine.CY_DOT_Y = parseFloat(covLine2[4]);
covLine.CY_DOT_Z = parseFloat(covLine2[5]);
covLine.CY_DOT_X_DOT = parseFloat(covLine2[6]);
covLine.CY_DOT_Y_DOT = parseFloat(covLine3[0]);
covLine.CZ_DOT_X = parseFloat(covLine3[1]);
covLine.CZ_DOT_Y = parseFloat(covLine3[2]);
covLine.CZ_DOT_Z = parseFloat(covLine3[3]);
covLine.CZ_DOT_X_DOT = parseFloat(covLine3[4]);
covLine.CZ_DOT_Y_DOT = parseFloat(covLine3[5]);
covLine.CZ_DOT_Z_DOT = parseFloat(covLine3[6]);
ephemDataBlock.COVARIANCE_MATRIX_LINES.push(covLine);
}
nextOEMT.EPHEMERIS_DATA_BLOCK.push(ephemDataBlock);
}
return nextOEMT;
};
const processFile = (file, inputDir, outputDir) => {
const inputPath = join(inputDir, file);
const outputPath = join(outputDir, `${basename(file, '.txt')}.oem`);
const nextOEM = parseEphemerisFile(inputPath);
writeFileSync(outputPath, writeFB(nextOEM));
};
const zipDirectory = async (source, out) => {
const output = createWriteStream(out);
const archive = archiver('tar', {
gzip: true,
gzipOptions: {
level: 9
}
});
output.on('close', () => {
console.log(`${archive.pointer()} total bytes`);
console.log('Archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', (err) => {
throw err;
});
archive.pipe(output);
archive.directory(source, false);
await archive.finalize();
console.log(`Zipped to ${out}`);
};
const compressWithBrotli = async (sourceDir, outFile) => {
const archive = archiver('tar');
const output = createWriteStream(outFile);
const brotli = createBrotliCompress({ params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 11 } });
output.on('close', () => {
console.log(`${archive.pointer()} total bytes`);
console.log('Brotli compression has been finalized and the output file descriptor has closed.');
});
archive.on('error', (err) => {
throw err;
});
archive.pipe(brotli).pipe(output);
archive.directory(sourceDir, false);
await archive.finalize();
console.log(`Compressed to ${outFile}`);
};
export const generateOEMTFiles = (inputDir, outputDir, maxCores = 64) => {
return new Promise((resolve, reject) => {
const files = readdirSync(inputDir).filter(file => file.endsWith('.txt'));
const numCPUs = Math.min(os.cpus().length, maxCores);
if (files.length === 0) {
console.log("No files found.");
process.exit(0);
}
if (cluster.isPrimary) {
console.log(`Master ${process.pid} is running`);
console.log(`Spinning up ${numCPUs} workers...`);
let completedFiles = 0;
let fileIndex = 0;
// Create a new progress bar instance
const progressBar = new cliProgress.SingleBar({
format: 'Processing |{bar}| {percentage}% | {value}/{total} Files',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
// Initialize the progress bar
progressBar.start(files.length, 0);
// Fork workers
for (let i = 0; i < numCPUs; i++) {
const worker = cluster.fork();
worker.on('message', message => {
if (message.type === 'done') {
completedFiles++;
progressBar.update(completedFiles);
if (fileIndex < files.length) {
worker.send({ file: files[fileIndex], inputDir, outputDir });
fileIndex++;
} else if (completedFiles === files.length) {
progressBar.stop();
console.log('All files processed');
resolve();
}
}
});
if (fileIndex < files.length) {
const currentIndex = fileIndex;
setTimeout(() => {
worker.send({ file: files[currentIndex], inputDir, outputDir });
}, 2000)
fileIndex++;
}
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} finished`);
});
} else {
process.on('message', message => {
const { file, inputDir, outputDir } = message;
processFile(file, inputDir, outputDir);
process.send({ type: 'done' });
});
}
});
};
const runScript = async () => {
const inputDir = join(__dirname, 'ephemerides');
const outputDir = join(__dirname, 'oems');
const args = process.argv.slice(2);
const noPrompt = args.includes('--no-prompt');
let shouldClear = false;
let shouldCompress = false;
if (cluster.isPrimary) {
console.log(`Input directory: ${inputDir}`);
console.log(`Output directory: ${outputDir}`);
if (!noPrompt) {
const shouldClearInput = await promptUser('Do you want to clear the output directory before processing? (y/n): ');
shouldClear = shouldClearInput === 'y';
}
if (shouldClear) {
console.log('Clearing output directory...');
clearDirectory(outputDir);
} else {
console.log('Skipping directory clear. Files may be overwritten.');
}
console.log('Starting file processing...');
}
await generateOEMTFiles(inputDir, outputDir);
if (cluster.isPrimary) {
console.log('Processing complete.');
if (!noPrompt) {
const shouldCompressInput = await promptUser('Do you want to compress the output directory? (y/n): ');
shouldCompress = shouldCompressInput === 'y';
}
if (shouldCompress) {
const compressionType = noPrompt ? null : await promptUser('Choose compression type: gzip or brotli (g/b): ');
const zipFilePath = join(__dirname, 'oems.tar.gz');
const brotliFilePath = join(__dirname, 'oems.tar.br');
if (compressionType === 'g') {
await compressWithGzip(outputDir, zipFilePath);
} else if (compressionType === 'b') {
await compressWithBrotli(outputDir, brotliFilePath);
} else if (!noPrompt) {
console.log('Invalid compression type chosen. Skipping compression.');
}
}
process.exit(0);
}
};
if (import.meta.url === `file://${process.argv[1]}`) {
runScript();
}