Skip to content

Instantly share code, notes, and snippets.

@cjtheham
Last active August 5, 2023 04:04
Show Gist options
  • Select an option

  • Save cjtheham/a8a53eb9f4c59e926eb3bb8d8fc1fc76 to your computer and use it in GitHub Desktop.

Select an option

Save cjtheham/a8a53eb9f4c59e926eb3bb8d8fc1fc76 to your computer and use it in GitHub Desktop.
RHR ADIF File Splitter (Split by Location) by K4BEN
/**
* @file RemoteHamRadio ADIF File Splitter Command Line Tool
* @copyright Ben Eastwood K4BEN 2022 (@k4ben on Github)
*/
const fs = require('fs');
const USAGE = "usage: node adif-splitter.js <file>\n\n" +
"split an RHR ADIF file by the \"APP_RHR_STATION_NAME\" field.\n";
function throwError(msg) {
console.log(`ERROR: ${msg}\n\n${USAGE}`);
process.exit();
}
function parseFile(contents) {
let lines = contents.split("\n");
let contacts = lines.filter(l => l.startsWith("<CALL"));
let stations = {};
contacts.forEach(c => {
c.split("<").forEach(dirtyField => {
if (dirtyField.split(":")[0] == "APP_RHR_STATION_NAME" && dirtyField.split(">")[1] !== null) {
const stationName = dirtyField.split(">")[1];
if (stations[stationName] == null) {
stations[stationName] = [c];
} else {
stations[stationName].push(c);
}
}
});
});
return stations;
}
const file = process.argv?.[2];
if (file == null) {
throwError("no input file specified.");
return;
}
fs.readFile(file, 'utf8' , (err, data) => {
if (err) throwError(err.message.split(": ")[1]);
const stationArrays = parseFile(data);
console.log("Generated:");
for (key in stationArrays) {
const filename = key.replace("/", "_") + ".adi"
const contents = stationArrays[key].reduce((prev, curr) => prev + "\n" + curr, "");
fs.writeFile(filename, contents, err => { if (err) throw err });
console.log(filename);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment