wandb
Version:
Weight and balance calculation for airplane flying
68 lines (66 loc) • 4.16 kB
JavaScript
// Weight and balance calculation
// Public domain
;
const leftpad=(x,y)=>(" ".repeat(y)+x).slice(-y);
const rightpad=(x,y)=>(x+" ".repeat(y)).slice(0,y);
const fs=require("fs");
if(process.argv.length<3) {
console.error("ERROR: Aircraft type missing");
process.exit(1);
}
const ac_file=fs.readFileSync(process.argv[2].includes("/")?process.argv[2]:__dirname+"/typedata/"+process.argv[2].toLowerCase()+".typ","ascii");
const numeric={};
const ac={sfront:0,fuelld:0};
["emwt","emcg","seatrow1arm","seatrow2arm","seatrows","bag1max","bag2max","bag1arm",
"bag2arm","fuelmax","fuelarm","iminweight","imaxweight","imincg","imaxcg",
"sfront","srear","cargo1","cargo2","fuelto","fuelld","plot"].forEach(x=>(numeric[x]=true));
ac_file.trim().split("\n").map(x=>x.trim().split("=")).forEach(x=>x.length>1&&(ac[x[0]]=numeric[x[0]]?parseFloat(x[1]):x[1]));
process.argv.slice(3).map(x=>x.trim().split("=")).forEach(x=>(ac[x[0]]=numeric[x[0]]?parseFloat(x[1]):x[1]));
if(!ac.bag1max) ac.bag1max=ac.cargo1=0;
if(!ac.bag2max) ac.bag2max=ac.cargo2=0;
if(ac.seatrows<2) ac.srear=ac.seatrow2arm=0;
const calc=()=>{
if(ac.fuelto>ac.fuelmax) return "Maximum fuel load exceeded";
if(ac.cargo1>ac.bag1max) return "Cargo area 1 maximum exceeded";
if(ac.cargo2>ac.bag2max) return "Cargo area 2 maximum exceeded";
let empty_moment=ac.emwt*ac.emcg;
let front_seat_moment=ac.sfront*ac.seatrow1arm;
let rear_seat_moment=ac.srear*ac.seatrow2arm;
let cargo1_moment=ac.cargo1*ac.bag1arm;
let cargo2_moment=ac.cargo2*ac.bag2arm;
let total_empty_moment=empty_moment+front_seat_moment+rear_seat_moment+cargo1_moment+cargo2_moment;
let total_empty_weight=ac.emwt+ac.sfront+ac.srear+ac.cargo1+ac.cargo2;
let total_empty_cg=total_empty_moment/total_empty_weight;
let fuel_moment=ac.fuelto*ac.fuelarm;
let total_to_moment=total_empty_moment+fuel_moment;
let total_to_weight=total_empty_weight+ac.fuelto;
let total_to_cg=total_to_moment/total_to_weight;
let fuel_moment_ld=ac.fuelld*ac.fuelarm;
let total_ld_moment=total_empty_moment+fuel_moment_ld;
let total_ld_weight=total_empty_weight+ac.fuelld;
let total_ld_cg=total_ld_moment/total_ld_weight;
return [
/* WEIGHT ARM MOMENT MAXIMUM */
[ "Empty Weight", ac.emwt, ac.emcg, empty_moment, Infinity, ],
[ "Front Seats", ac.sfront, ac.seatrow1arm, front_seat_moment, Infinity, ],
[ "Rear Seats", ac.srear, ac.seatrow2arm, rear_seat_moment, ac.seatrows>1?Infinity:0, ],
[ "Cargo Area 1", ac.cargo1, ac.bag1arm, cargo1_moment, ac.bag1max, ],
[ "Cargo Area 2", ac.cargo2, ac.bag2arm, cargo2_moment, ac.bag2max, ],
[ "Zero Fuel Totals", total_empty_weight, total_empty_cg, total_empty_moment, Infinity, ],
[ "Fuel at Takeoff", ac.fuelto, ac.fuelarm, fuel_moment, ac.fuelmax, ],
[ "Takeoff Totals", total_to_weight, total_to_cg, total_to_moment, Infinity, ],
[ "Fuel at Landing", ac.fuelld, ac.fuelarm, fuel_moment_ld, ac.fuelmax, ],
[ "Landing Totals", total_ld_weight, total_ld_cg, total_ld_moment, Infinity, ],
];
};
console.log("Aircraft type: %s",ac.typename);
console.log(" | WEIGHT | MAXIMUM | ARM | MOMENT |");
console.log("------------------------------------------------------------");
const res=calc();
if(typeof res==="string") {
console.error("ERROR: %s",res);
process.exit(1);
}
res.forEach(x=>x[4]&&console.log(
rightpad(x[0],19)+"|"+leftpad(x[1].toFixed(0),8)+" |"+leftpad(x[4]==Infinity?"":x[4].toFixed(0),8)+" |"+leftpad(x[2].toFixed(1),8)+" |"+leftpad(x[3].toFixed(0),8)+" |"
));