// CLEAN
// LISTCLEAN

/***************************************************************************
                          row.cpp  -  description
                             -------------------
    begin                : Wed Jul 10 2002
    copyright            : (C) 2002 by Ross Andrews
    email                : randrews@geekfu.org
***************************************************************************/

#define REAL

#ifdef REAL
#define QOUT *(outputstream)
#endif

#ifndef REAL
#define QOUT cout
#define ONEROW
#endif

#include "row.h"

const int mods=12;
const string modnames[mods]={"AIR","APM","CAM","HAB","JEM","JEMEF",
			     "JEMELMPS","LAB","NOD1","NOD2","NOD3","TRUSS"};


row::row(){
  stn=0;
}

row::~row(){
  delete stn;
}

row::row(MYSQL connp,int r,string db){
#ifndef RELEASE
  cout<<"Entered row::row(MYSQL connp,int r,string db)\n"<<flush;
#endif
  conn=connp;
  stn=new station(conn);
  ResultSet res(&conn);
  res.query<<"select * from runs where idx="<<r;
  res.store();res.next();
  run.MLE=res["MLE"];
  run.stowage=res["stowage"];
  run.upmass=res["upmass"];
  run.bulk=res["bulk"];
  run.rack_overhead=res["rack_overhead"];
  run.crewtrng=res["crewtrng"];
  run.crewtime=res["crewtime"];
  run.power=res["power"];
  run.keepalive=res["keepalive"];
  run.avg_uplink=res["avg_uplink"];
  run.avg_ethernet=res["avg_ethernet"];
  run.avg_1553=res["avg_1553"];
  run.avg_HRL=res["avg_HRL"];
  run.avg_downlink=res["avg_downlink"];
  run.peak_uplink=res["peak_uplink"];
  run.peak_ethernet=res["peak_ethernet"];
  run.peak_1553=res["peak_1553"];
  run.peak_HRL=res["peak_HRL"];
  run.peak_downlink=res["peak_downlink"];
  run.inclength=res["inclength"];
  run.middeckmass=res["middeckmass"];
  run.hskpg=res["hskpg"];
  run.ku_power=res["ku_power"];
  run.pl_power=res["pl_power"];
  run.sams_power=res["sams_power"];
  run.newsams=res["newsams"];
  run.tblname=(string)res["tblname"];
  run.peak_uplink_duration=res["peak_uplink_duration"];
  run.peak_downlink_duration=res["peak_downlink_duration"];
  run.peak_ethernet_duration=res["peak_ethernet_duration"];
  run.peak_1553_duration=res["peak_1553_duration"];
  run.peak_HRL_duration=res["peak_HRL_duration"];
  run.video_uplink=res["video_uplink"];
  run.video_downlink=res["video_downlink"];
  run.video_rate=res["video_rate"];

  run.cs_p4_asc=res["cs_p4_asc"];
  run.cs_p4_orb=res["cs_p4_orb"];
  run.cs_p4_desc=res["cs_p4_desc"];
  run.cs_m20_asc=res["cs_m20_asc"];
  run.cs_m20_orb=res["cs_m20_orb"];
  run.cs_m20_desc=res["cs_m20_desc"];
  run.cs_m80_asc=res["cs_m80_asc"];
  run.cs_m80_orb=res["cs_m80_orb"];
  run.cs_m80_desc=res["cs_m80_desc"];
  run.cs_m180_asc=res["cs_m180_asc"];
  run.cs_m180_orb=res["cs_m180_orb"];
  run.cs_m180_desc=res["cs_m180_desc"];

  run.ignore=res["ignorezeros"];
  run.stowage_MLEs=res["stowage_MLEs"];

  if(run.newsams)run.samsmod=findsams(db);
  stn->inclength=run.inclength;
  stn->rack_overhead=run.rack_overhead;
  stn->middeckmass=run.middeckmass;
  //Sums
  res.query<<"select * from sums order by name asc";
  res.store();
  ResultSet limitsres(&conn);
  if(res.size()!=0)
    while(res.next())
      {
	if((string)res["limitfield"]!="")
	  {
	    limitsres.query<<"select "<<res["limitfield"]<<" as limitfield from runs where idx="<<r;
	    limitsres.store();
	    limitsres.next();
	    run.limits.push_back(limitsres["limitfield"]);
	  }
      }
  //
#ifndef RELEASE
  cout<<"Returned from row::row(MYSQL connp,int r,string db)\n"<<flush;
#endif
}

row::row(const row& other){
  stn=new station(*(other.stn));
  conn=other.conn;
  cols=other.cols;
  run=other.run;
}

const row& row::operator= (const row& other){
  delete stn;
  stn=new station(*(other.stn));
  conn=other.conn;
  cols=other.cols;
  run=other.run;
  return *this;
}


int row::findsams(string db){
  ResultSet res(&conn);
  res.query<<"select count(idx) as count from "<<db<<".racks where sams=2";
  res.store();res.next();
  int count=(int)res["count"];
  if(count==0){run.newsams=0;return 0;}
  
  res.query<<"select idx as rack from "<<db<<".racks where sams=2";
  res.store();res.next();
  int rack=(int)res["rack"];
  res.query<<"select modules.idx as modidx from "<<db<<".racks,"<<db<<".locations,"<<db<<".modules where racks.idx="<<rack<<" and racks.location=locations.idx and locations.module=modules.idx";
  res.store();res.next();
  switch((int)res["modidx"]) // This is to convert from modules index into an acronym-ordered array of modules.
    { // FIXME
    case 10:return 0;
    case 4:return 1;
    case 5:return 2;
    case 6:return 3;
    case 2:return 4;
    case 3:return 5;
    case 12:return 6;
    case 1:return 7;
    case 7:return 8;
    case 8:return 9;
    case 9:return 10;
    case 11:return 11;
    default:run.newsams=0;return 0;
    }
}



void row::calc(){
  calc_power();
  calc_energy();
  calc_energy_crewtend();
  calc_MTL();
  calc_LTL();
  calc_MTL_simul();
  calc_LTL_simul();
  calc_MTL_crewtend();
  calc_LTL_crewtend();
  calc_acoustics();
  calc_acoustics_simul();
  calc_acoustics_crewtend();
  calc_keepalive();
  calc_middecks();
  calc_stowage();
  calc_upmass();
  calc_crewtrng();
  calc_crewtime();
  calc_runtime();
  calc_networking();
  calc_coldStowage();
  calc_sums();
  calc_any();
}

void row::dump(ostream& out){
  outputstream=&out;
  separator="\n";

  generic_print(cols.power_ttl,cols.power_xcd,cols.power_ttl_mod,cols.power_xcd_mod,"power");
  QOUT<<separator;
  generic_print(cols.energy_ttl,cols.energy_xcd,cols.energy_ttl_mod,cols.energy_xcd_mod,"energy");
  QOUT<<separator;
  generic_print(cols.energy_crewtend_ttl,cols.energy_crewtend_xcd,cols.energy_crewtend_ttl_mod,cols.energy_crewtend_xcd_mod,"energy_crewtend");
  QOUT<<separator;
  print_MTL();
  QOUT<<separator;
  print_LTL();
  QOUT<<separator;
  print_MTL_simul();
  QOUT<<separator;
  print_LTL_simul();
  QOUT<<separator;
  print_MTL_crewtend();
  QOUT<<separator;
  print_LTL_crewtend();
  QOUT<<separator;
  print_acoustics();
  QOUT<<separator;
  print_acoustics_simul();
  QOUT<<separator;
  print_acoustics_crewtend();
  QOUT<<separator;
  print_keepalive();
  QOUT<<separator;
  print_middecks();
  QOUT<<separator;
  print_stowage();
  QOUT<<separator;
  print_upmass();
  QOUT<<separator;
  print_crewtrng();
  QOUT<<separator;
  print_crewtime();
  QOUT<<separator;
  print_runtime();
  QOUT<<separator;
  print_networking();
  QOUT<<separator;
  print_coldStowage();
  QOUT<<separator;
  print_sums();
  print_any();
}

void row::store(int idx,string db){
  ResultSet res(&conn);
  outputstream=&(res.query);
  separator=", \n";

  QOUT<<"update "<<db<<"."<<run.tblname<<" set finished=1, ";

  generic_print(cols.power_ttl,cols.power_xcd,cols.power_ttl_mod,cols.power_xcd_mod,"power");
  QOUT<<separator;
  generic_print(cols.energy_ttl,cols.energy_xcd,cols.energy_ttl_mod,cols.energy_xcd_mod,"energy");
  QOUT<<separator;
  generic_print(cols.energy_crewtend_ttl,cols.energy_crewtend_xcd,cols.energy_crewtend_ttl_mod,cols.energy_crewtend_xcd_mod,"energy_crewtend");
  QOUT<<separator;
  print_MTL();
  QOUT<<separator;
  print_LTL();
  QOUT<<separator;
  print_MTL_simul();
  QOUT<<separator;
  print_LTL_simul();
  QOUT<<separator;
  print_MTL_crewtend();
  QOUT<<separator;
  print_LTL_crewtend();
  QOUT<<separator;
  print_acoustics();
  QOUT<<separator;
  print_acoustics_simul();
  QOUT<<separator;
  print_acoustics_crewtend();
  QOUT<<separator;
  print_keepalive();
  QOUT<<separator;
  print_middecks();
  QOUT<<separator;
  print_stowage();
  QOUT<<separator;
  print_upmass();
  QOUT<<separator;
  print_crewtrng();
  QOUT<<separator;
  print_crewtime();
  QOUT<<separator;
  print_runtime();
  QOUT<<separator;
  print_networking();
  QOUT<<separator;
  print_coldStowage();
  QOUT<<separator;
  print_sums(); // This puts a trailing separator if it prints anything.
  print_any();

  QOUT<<' ';

  QOUT<<"where idx="<<idx<<'\n';
#ifdef REAL
  res.store();
#endif
#ifdef ONEROW
  exit(0);
#endif
}



void row::print_any(){
  QOUT<<"any_exceeded_inc="<<cols.any_exceeded_inc<<separator;
  QOUT<<"any_exceeded_simul="<<cols.any_exceeded_simul;
}


void row::print_sums(){
  ResultSet names(&conn);
  names.query<<"select outputfield from sums order by name asc";
  names.store();
  int n=0;
  if(names.size()!=0)
    while(names.next())
      {
	QOUT<<names["outputfield"]<<"_ttl="<<cols.sums_ttl[n]<<separator;
	QOUT<<names["outputfield"]<<"_xcd="<<cols.sums_xcd[n]<<separator;
	n++;
      }
}


void row::print_acoustics(){
  for(int n=0;n!=mods;n++)
    {
      string mn=modnames[n];
      if(mn=="JEMELMPS")mn="JEMEF";
      else if(mn=="JEMEF")mn="JEMELMPS";
      QOUT<<"acoustics_ttl_"<<mn<<"="<<cols.acoustics_ttl_mod[n]<<separator;
      QOUT<<"acoustics_xcd_"<<modnames[n]<<"="<<cols.acoustics_xcd_mod[n]<<separator;
    }
  QOUT<<"acoustics_xcd="<<cols.acoustics_xcd;
}



void row::print_acoustics_simul(){
  for(int n=0;n!=mods;n++)
    {
      string mn=modnames[n];
      if(mn=="JEMELMPS")mn="JEMEF";
      else if(mn=="JEMEF")mn="JEMELMPS";
      QOUT<<"acoustics_ttl_simul_"<<mn<<"="<<cols.acoustics_ttl_simul_mod[n]<<separator<<"acoustics_xcd_simul_"<<mn<<"="<<cols.acoustics_xcd_simul_mod[n]<<separator;
    }
  QOUT<<"acoustics_xcd_simul="<<cols.acoustics_xcd_simul;
}



void row::print_acoustics_crewtend(){
  for(int n=0;n!=mods;n++)
    QOUT<<"acoustics_crewtend_ttl_"<<modnames[n]<<"="<<cols.acoustics_crewtend_ttl_mod[n]<<separator<<"acoustics_crewtend_xcd_"<<modnames[n]<<"="<<cols.acoustics_crewtend_xcd_mod[n]<<separator;
  QOUT<<"acoustics_crewtend_xcd="<<cols.acoustics_crewtend_xcd;
}



void row::print_keepalive(){
  QOUT<<"keepalive_ttl="<<cols.keepalive_ttl;
  QOUT<<separator<<"keepalive_xcd="<<cols.keepalive_xcd;
}



void row::print_middecks(){
  QOUT<<"middecks_ttl="<<cols.middecks_ttl;
  QOUT<<separator<<"middecks_xcd="<<cols.middecks_xcd;
}



void row::print_stowage(){
  QOUT<<"stowage_ttl="<<cols.stowage_ttl;
  QOUT<<separator<<"stowage_xcd="<<cols.stowage_xcd;
}



void row::print_upmass(){
  QOUT<<"upmass_ttl="<<cols.upmass_ttl;
  QOUT<<separator<<"upmass_xcd="<<cols.upmass_xcd;
}



void row::print_crewtrng(){
  QOUT<<"crewtrng_ttl="<<cols.crewtrng_ttl;
  QOUT<<separator<<"crewtrng_xcd="<<cols.crewtrng_xcd;
}

void row::print_runtime(){  
  QOUT<<"runtime_ttl="<<cols.runtime_ttl;
}

void row::print_crewtime(){
  QOUT<<"crewtime_ttl="<<cols.crewtime_ttl;
  QOUT<<separator<<"crewtime_xcd="<<cols.crewtime_xcd;
}

void row::print_coldStowage(){
//   QOUT<<"nominal_uplink_ttl="<<cols.nominal_uplink;
//   QOUT<<separator<<"nominal_uplink_xcd="<<cols.nominal_uplink_xcd;
  
  QOUT<<"cs_p4_asc_ttl="<<cols.cs_p4_asc_ttl
      <<separator<<"cs_p4_asc_xcd="<<cols.cs_p4_asc_xcd<<separator;
  QOUT<<"cs_p4_orb_ttl="<<cols.cs_p4_orb_ttl
      <<separator<<"cs_p4_orb_xcd="<<cols.cs_p4_orb_xcd<<separator;
  QOUT<<"cs_p4_desc_ttl="<<cols.cs_p4_desc_ttl
      <<separator<<"cs_p4_desc_xcd="<<cols.cs_p4_desc_xcd<<separator;
  QOUT<<"cs_m20_asc_ttl="<<cols.cs_m20_asc_ttl
      <<separator<<"cs_m20_asc_xcd="<<cols.cs_m20_asc_xcd<<separator;
  QOUT<<"cs_m20_orb_ttl="<<cols.cs_m20_orb_ttl
      <<separator<<"cs_m20_orb_xcd="<<cols.cs_m20_orb_xcd<<separator;
  QOUT<<"cs_m20_desc_ttl="<<cols.cs_m20_desc_ttl
      <<separator<<"cs_m20_desc_xcd="<<cols.cs_m20_desc_xcd<<separator;
  QOUT<<"cs_m80_asc_ttl="<<cols.cs_m80_asc_ttl
      <<separator<<"cs_m80_asc_xcd="<<cols.cs_m80_asc_xcd<<separator;
  QOUT<<"cs_m80_orb_ttl="<<cols.cs_m80_orb_ttl
      <<separator<<"cs_m80_orb_xcd="<<cols.cs_m80_orb_xcd<<separator;
  QOUT<<"cs_m80_desc_ttl="<<cols.cs_m80_desc_ttl
      <<separator<<"cs_m80_desc_xcd="<<cols.cs_m80_desc_xcd<<separator;
  QOUT<<"cs_m180_asc_ttl="<<cols.cs_m180_asc_ttl
      <<separator<<"cs_m180_asc_xcd="<<cols.cs_m180_asc_xcd<<separator;
  QOUT<<"cs_m180_orb_ttl="<<cols.cs_m180_orb_ttl
      <<separator<<"cs_m180_orb_xcd="<<cols.cs_m180_orb_xcd<<separator;
  QOUT<<"cs_m180_desc_ttl="<<cols.cs_m180_desc_ttl
      <<separator<<"cs_m180_desc_xcd="<<cols.cs_m180_desc_xcd;
}


void row::print_networking(){
  QOUT<<"nominal_uplink_ttl="<<cols.nominal_uplink;
  QOUT<<separator<<"nominal_uplink_xcd="<<cols.nominal_uplink_xcd;
  QOUT<<separator<<"peak_uplink_ttl="<<cols.peak_uplink;
  QOUT<<separator<<"peak_uplink_xcd="<<cols.peak_uplink_xcd;
  QOUT<<separator<<"peak_uplink_duration_ttl="<<cols.peak_uplink_duration;
  QOUT<<separator<<"peak_uplink_duration_xcd="<<cols.peak_uplink_duration_xcd;

  QOUT<<separator<<"nominal_downlink_ttl="<<cols.nominal_downlink;
  QOUT<<separator<<"nominal_downlink_xcd="<<cols.nominal_downlink_xcd;
  QOUT<<separator<<"peak_downlink_ttl="<<cols.peak_downlink;
  QOUT<<separator<<"peak_downlink_xcd="<<cols.peak_downlink_xcd;
  QOUT<<separator<<"peak_downlink_duration_ttl="<<cols.peak_downlink_duration;
  QOUT<<separator<<"peak_downlink_duration_xcd="<<cols.peak_downlink_duration_xcd;

  QOUT<<separator<<"nominal_ethernet_ttl="<<cols.nominal_ethernet;
  QOUT<<separator<<"nominal_ethernet_xcd="<<cols.nominal_ethernet_xcd;
  QOUT<<separator<<"peak_ethernet_ttl="<<cols.peak_ethernet;
  QOUT<<separator<<"peak_ethernet_xcd="<<cols.peak_ethernet_xcd;
  QOUT<<separator<<"peak_ethernet_duration_ttl="<<cols.peak_ethernet_duration;
  QOUT<<separator<<"peak_ethernet_duration_xcd="<<cols.peak_ethernet_duration_xcd;

  QOUT<<separator<<"nominal_1553_ttl="<<cols.nominal_1553;
  QOUT<<separator<<"nominal_1553_xcd="<<cols.nominal_1553_xcd;
  QOUT<<separator<<"peak_1553_ttl="<<cols.peak_1553;
  QOUT<<separator<<"peak_1553_xcd="<<cols.peak_1553_xcd;
  QOUT<<separator<<"peak_1553_duration_ttl="<<cols.peak_1553_duration;
  QOUT<<separator<<"peak_1553_duration_xcd="<<cols.peak_1553_duration_xcd;

  QOUT<<separator<<"nominal_HRL_ttl="<<cols.nominal_HRL;
  QOUT<<separator<<"nominal_HRL_xcd="<<cols.nominal_HRL_xcd;
  QOUT<<separator<<"peak_HRL_ttl="<<cols.peak_HRL;
  QOUT<<separator<<"peak_HRL_xcd="<<cols.peak_HRL_xcd;
  QOUT<<separator<<"peak_HRL_duration_ttl="<<cols.peak_HRL_duration;
  QOUT<<separator<<"peak_HRL_duration_xcd="<<cols.peak_HRL_duration_xcd;

  QOUT<<separator<<"video_uplink_ttl="<<cols.video_uplink;
  QOUT<<separator<<"video_uplink_xcd="<<cols.video_uplink_xcd;
  QOUT<<separator<<"video_downlink_ttl="<<cols.video_downlink;
  QOUT<<separator<<"video_downlink_xcd="<<cols.video_downlink_xcd;
  QOUT<<separator<<"video_rate_ttl="<<cols.video_rate;
  QOUT<<separator<<"video_rate_xcd="<<cols.video_rate_xcd;
}


void row::print_MTL(){
  for(int n=0;n!=mods;n++)
    QOUT<<"MTL_ttl_"<<modnames[n]<<"="<<cols.MTL_ttl_mod[n]<<separator<<"MTL_xcd_"<<modnames[n]<<"="<<cols.MTL_xcd_mod[n]<<separator;
  QOUT<<"MTL_xcd="<<cols.MTL_xcd;
}


void row::print_LTL(){
  for(int n=0;n!=mods;n++)
    QOUT<<"LTL_ttl_"<<modnames[n]<<"="<<cols.LTL_ttl_mod[n]<<separator<<"LTL_xcd_"<<modnames[n]<<"="<<cols.LTL_xcd_mod[n]<<separator;
  QOUT<<"LTL_xcd="<<cols.LTL_xcd;
}



void row::print_MTL_crewtend(){
  for(int n=0;n!=mods;n++)
    QOUT<<"MTL_crewtend_ttl_"<<modnames[n]<<"="<<cols.MTL_crewtend_ttl_mod[n]<<separator<<"MTL_crewtend_xcd_"<<modnames[n]<<"="<<cols.MTL_crewtend_xcd_mod[n]<<separator;
  QOUT<<"MTL_crewtend_xcd="<<cols.MTL_crewtend_xcd;
}


void row::print_LTL_crewtend(){
  for(int n=0;n!=mods;n++)
    QOUT<<"LTL_crewtend_ttl_"<<modnames[n]<<"="<<cols.LTL_crewtend_ttl_mod[n]<<separator<<"LTL_crewtend_xcd_"<<modnames[n]<<"="<<cols.LTL_crewtend_xcd_mod[n]<<separator;
  QOUT<<"LTL_crewtend_xcd="<<cols.LTL_crewtend_xcd;
}



void row::print_MTL_simul(){
  for(int n=0;n!=mods;n++)
    QOUT<<"MTL_ttl_simul_"<<modnames[n]<<"="<<cols.MTL_ttl_simul_mod[n]<<separator<<"MTL_xcd_simul_"<<modnames[n]<<"="<<cols.MTL_xcd_simul_mod[n]<<separator;
  QOUT<<"MTL_simul_xcd="<<cols.MTL_simul_xcd;
}


void row::print_LTL_simul(){
  for(int n=0;n!=mods;n++)
    QOUT<<"LTL_ttl_simul_"<<modnames[n]<<"="<<cols.LTL_ttl_simul_mod[n]<<separator<<"LTL_xcd_simul_"<<modnames[n]<<"="<<cols.LTL_xcd_simul_mod[n]<<separator;
  QOUT<<"LTL_simul_xcd="<<cols.LTL_simul_xcd;
}


void row::generic_print(double ttl,double xcd,double* ttls,double *xcds,string name){
  for(int n=0;n!=mods;n++)
    {
      string mn=modnames[n];
      if(mn=="JEMELMPS")mn="JEMEF";
      else if(mn=="JEMEF")mn="JEMELMPS";
      QOUT<<name<<"_ttl_"<<mn<<"="<<ttls[n]<<separator;
      QOUT<<name<<"_xcd_"<<mn<<"="<<xcds[n]<<separator;
    }
  QOUT<<name<<"_ttl="<<ttl<<separator<<name<<"_xcd="<<xcd;
}



void row::calc_any(){
  if(cols.energy_xcd || cols.energy_crewtend_xcd ||
     cols.MTL_xcd || cols.LTL_xcd ||
     cols.MTL_crewtend_xcd || cols.LTL_crewtend_xcd ||
     cols.acoustics_xcd || cols.acoustics_crewtend_xcd ||
     cols.keepalive_xcd || cols.middecks_xcd || cols.stowage_xcd ||
     cols.upmass_xcd || cols.crewtrng_xcd || cols.crewtime_xcd ||
     cols.nominal_uplink_xcd || cols.peak_uplink_xcd || cols.peak_uplink_duration_xcd ||
     cols.nominal_downlink_xcd || cols.peak_downlink_xcd || cols.peak_downlink_duration_xcd ||
     cols.nominal_ethernet_xcd || cols.peak_ethernet_xcd || cols.peak_ethernet_duration_xcd ||
     cols.nominal_1553_xcd || cols.peak_1553_xcd || cols.peak_1553_duration_xcd ||
     cols.nominal_HRL_xcd || cols.peak_HRL_xcd || cols.peak_HRL_duration_xcd ||
     cols.video_uplink_xcd || cols.video_downlink_xcd || cols.video_rate_xcd)
    cols.any_exceeded_inc=1;
  else
    cols.any_exceeded_inc=0;

  if(cols.power_xcd ||
     cols.MTL_simul_xcd || cols.LTL_simul_xcd ||
     cols.keepalive_xcd || cols.stowage_xcd ||
     cols.acoustics_xcd_simul)
    cols.any_exceeded_simul=1;
  else
    cols.any_exceeded_simul=0;

  ResultSet any(&conn);
  any.query<<"select increment,simultaneous from sums order by name asc";
  any.store();
  int n=0;
  if(any.size()!=0)
    while(any.next())
      {
	if(any["increment"]==1 && cols.sums_xcd[n]==1)
	  cols.any_exceeded_inc=1;
	if(any["simultaneous"]==1 && cols.sums_xcd[n]==1)
	  cols.any_exceeded_simul=1;
	n++;
      }
}


void row::calc_sums(){
  for(int s=0;s!=run.limits.size();s++)
    {
      double ttl=0.0;
      for(int n=0;n!=stn->modules.size();n++)
	for(int r=0;r!=stn->modules[n].racks.size();r++)
	    ttl+=stn->modules[n].racks[r].sums[s];
      cols.sums_ttl.push_back(ttl);
      if(run.limits[s]<ttl)
	cols.sums_xcd.push_back(1);
      else
	cols.sums_xcd.push_back(0);
      }
}


void row::calc_coldStowage(){

  cols.cs_p4_asc_ttl=cols.cs_p4_orb_ttl=cols.cs_p4_desc_ttl=
   cols.cs_m20_asc_ttl=cols.cs_m20_orb_ttl=cols.cs_m20_desc_ttl=
   cols.cs_m80_asc_ttl=cols.cs_m80_orb_ttl=cols.cs_m80_desc_ttl=
   cols.cs_m180_asc_ttl=cols.cs_m180_orb_ttl=cols.cs_m180_desc_ttl=0;
 
  cols.cs_p4_asc_xcd=cols.cs_p4_orb_xcd=cols.cs_p4_desc_xcd=
    cols.cs_m20_asc_xcd=cols.cs_m20_orb_xcd=cols.cs_m20_desc_xcd=
    cols.cs_m80_asc_xcd=cols.cs_m80_orb_xcd=cols.cs_m80_desc_xcd=
    cols.cs_m180_asc_xcd=cols.cs_m180_orb_xcd=cols.cs_m180_desc_xcd=0;
  
   for(int n=0;n!=stn->modules.size();n++)
    {
      cols.cs_p4_asc_ttl+=stn->modules[n].calc_cs_p4_asc_ttl();
      cols.cs_p4_orb_ttl+=stn->modules[n].calc_cs_p4_orb_ttl();
      cols.cs_p4_desc_ttl+=stn->modules[n].calc_cs_p4_desc_ttl();
      cols.cs_m20_asc_ttl+=stn->modules[n].calc_cs_m20_asc_ttl();
      cols.cs_m20_orb_ttl+=stn->modules[n].calc_cs_m20_orb_ttl();
      cols.cs_m20_desc_ttl+=stn->modules[n].calc_cs_m20_desc_ttl();
      cols.cs_m80_asc_ttl+=stn->modules[n].calc_cs_m80_asc_ttl();
      cols.cs_m80_orb_ttl+=stn->modules[n].calc_cs_m80_orb_ttl();
      cols.cs_m80_desc_ttl+=stn->modules[n].calc_cs_m80_desc_ttl();
      cols.cs_m180_asc_ttl+=stn->modules[n].calc_cs_m180_asc_ttl();
      cols.cs_m180_orb_ttl+=stn->modules[n].calc_cs_m180_orb_ttl();
      cols.cs_m180_desc_ttl+=stn->modules[n].calc_cs_m180_desc_ttl();
    }

 if(cols.cs_p4_asc_ttl > run.cs_p4_asc && (run.cs_p4_asc!=0 || run.ignore==0))
   cols.cs_p4_asc_xcd=1;
 if(cols.cs_p4_orb_ttl > run.cs_p4_orb && (run.cs_p4_orb!=0 || run.ignore==0))
   cols.cs_p4_orb_xcd=1;
 if(cols.cs_p4_desc_ttl  > run.cs_p4_desc  && (run.cs_p4_desc !=0 || run.ignore==0))
   cols.cs_p4_desc_xcd=1;
 if(cols.cs_m20_asc_ttl > run.cs_m20_asc && (run.cs_m20_asc!=0 || run.ignore==0))
   cols.cs_m20_asc_xcd=1;
 if(cols.cs_m20_orb_ttl > run.cs_m20_orb && (run.cs_m20_orb!=0 || run.ignore==0))
   cols.cs_m20_orb_xcd=1;
 if(cols.cs_m20_desc_ttl > run.cs_m20_desc && (run.cs_m20_desc!=0 || run.ignore==0))
   cols.cs_m20_desc_xcd=1;
 if(cols.cs_m80_asc_ttl > run.cs_m80_asc && (run.cs_m80_asc!=0 || run.ignore==0))
   cols.cs_m80_asc_xcd=1;
 if(cols.cs_m80_orb_ttl > run.cs_m80_orb && (run.cs_m80_orb!=0 || run.ignore==0))
   cols.cs_m80_orb_xcd=1;
 if(cols.cs_m80_desc_ttl > run.cs_m80_desc && (run.cs_m80_desc!=0 || run.ignore==0))
   cols.cs_m80_desc_xcd=1;
 if(cols.cs_m180_asc_ttl > run.cs_m180_asc && (run.cs_m180_asc!=0 || run.ignore==0))
   cols.cs_m180_asc_xcd=1;
 if(cols.cs_m180_orb_ttl > run.cs_m180_orb && (run.cs_m180_orb!=0 || run.ignore==0))
   cols.cs_m180_orb_xcd=1;
 if(cols.cs_m180_desc_ttl > run.cs_m180_desc && (run.cs_m180_desc!=0 || run.ignore==0))
   cols.cs_m180_desc_xcd=1;

}


void row::calc_networking(){
  cols.nominal_uplink=cols.peak_uplink=cols.peak_uplink_duration=0;
  cols.nominal_downlink=cols.peak_downlink=cols.peak_downlink_duration=0;
  cols.nominal_ethernet=cols.peak_ethernet=cols.peak_ethernet_duration=0;
  cols.nominal_1553=cols.peak_1553=cols.peak_1553_duration=0;
  cols.nominal_HRL=cols.peak_HRL=cols.peak_HRL_duration=0;
  cols.video_uplink=cols.video_downlink=cols.video_rate=0;

  cols.nominal_uplink_xcd=cols.peak_uplink_xcd=cols.peak_uplink_duration_xcd=0;
  cols.nominal_downlink_xcd=cols.peak_downlink_xcd=cols.peak_downlink_duration_xcd=0;
  cols.nominal_ethernet_xcd=cols.peak_ethernet_xcd=cols.peak_ethernet_duration_xcd=0;
  cols.nominal_1553_xcd=cols.peak_1553_xcd=cols.peak_1553_duration_xcd=0;
  cols.nominal_HRL_xcd=cols.peak_HRL_xcd=cols.peak_HRL_duration_xcd=0;
  cols.video_uplink_xcd=cols.video_downlink_xcd=cols.video_rate_xcd=0;

  for(int n=0;n!=stn->modules.size();n++)
    {
      cols.nominal_uplink+=stn->modules[n].calc_nominal_uplink_ttl();
      cols.peak_uplink+=stn->modules[n].calc_peak_uplink_ttl();
      cols.peak_uplink_duration+=stn->modules[n].calc_peak_uplink_duration_ttl();
      
      cols.nominal_downlink+=stn->modules[n].calc_nominal_downlink_ttl();
      cols.peak_downlink+=stn->modules[n].calc_peak_downlink_ttl();
      cols.peak_downlink_duration+=stn->modules[n].calc_peak_downlink_duration_ttl();
      
      cols.nominal_ethernet+=stn->modules[n].calc_nominal_ethernet_ttl();
      cols.peak_ethernet+=stn->modules[n].calc_peak_ethernet_ttl();
      cols.peak_ethernet_duration+=stn->modules[n].calc_peak_ethernet_duration_ttl();
      
      cols.nominal_1553+=stn->modules[n].calc_nominal_1553_ttl();
      cols.peak_1553+=stn->modules[n].calc_peak_1553_ttl();
      cols.peak_1553_duration+=stn->modules[n].calc_peak_1553_duration_ttl();
      
      cols.nominal_HRL+=stn->modules[n].calc_nominal_HRL_ttl();
      cols.peak_HRL+=stn->modules[n].calc_peak_HRL_ttl();
      cols.peak_HRL_duration+=stn->modules[n].calc_peak_HRL_duration_ttl();
      
      cols.video_uplink+=stn->modules[n].calc_video_uplink_ttl();
      cols.video_downlink+=stn->modules[n].calc_video_downlink_ttl();
      cols.video_rate+=stn->modules[n].calc_video_rate_ttl();
    }
  if(cols.nominal_uplink > run.avg_uplink && (run.avg_uplink!=0 || run.ignore==0))
    cols.nominal_uplink_xcd=1;
  if(cols.peak_uplink > run.peak_uplink && (run.peak_uplink!=0 || run.ignore==0))
    cols.peak_uplink_xcd=1;
  if(cols.peak_uplink_duration > run.peak_uplink_duration && (run.peak_uplink_duration!=0 || run.ignore==0))
    cols.peak_uplink_duration_xcd=1;

  if(cols.nominal_downlink > run.avg_downlink && (run.avg_downlink!=0 || run.ignore==0))
    cols.nominal_downlink_xcd=1;
  if(cols.peak_downlink > run.peak_downlink && (run.peak_downlink!=0 || run.ignore==0))
    cols.peak_downlink_xcd=1;
  if(cols.peak_downlink_duration > run.peak_downlink_duration && (run.peak_downlink_duration!=0 || run.ignore==0))
    cols.peak_downlink_duration_xcd=1;

  if(cols.nominal_ethernet > run.avg_ethernet && (run.avg_ethernet!=0 || run.ignore==0))
    cols.nominal_ethernet_xcd=1;
  if(cols.peak_ethernet > run.peak_ethernet && (run.peak_ethernet!=0 | run.ignore==0))
    cols.peak_ethernet_xcd=1;
  if(cols.peak_ethernet_duration > run.peak_ethernet_duration && (run.peak_ethernet_duration!=0 || run.ignore==0))
    cols.peak_ethernet_duration_xcd=1;

  if(cols.nominal_1553 > run.avg_1553 && (run.avg_1553!=0 || run.ignore==0))
    cols.nominal_1553_xcd=1;
  if(cols.peak_1553 > run.peak_1553 && (run.peak_1553!=0 || run.ignore==0))
    cols.peak_1553_xcd=1;
  if(cols.peak_1553_duration > run.peak_1553_duration && (run.peak_1553_duration!=0 || run.ignore==0))
    cols.peak_1553_duration_xcd=1;

  if(cols.nominal_HRL > run.avg_HRL && (run.avg_HRL!=0 || run.ignore==0))
    cols.nominal_HRL_xcd=1;
  if(cols.peak_HRL > run.peak_HRL && (run.peak_HRL!=0 || run.ignore==0))
    cols.peak_HRL_xcd=1;
  if(cols.peak_HRL_duration > run.peak_HRL_duration && (run.peak_HRL_duration!=0 || run.ignore==0))
    cols.peak_HRL_duration_xcd=1;

  if(cols.video_uplink > run.video_uplink && (run.video_uplink!=0 || run.ignore==0))
    cols.video_uplink_xcd=1;
  if(cols.video_downlink > run.video_downlink && (run.video_downlink!=0 || run.ignore==0))
    cols.video_downlink_xcd=1;
  if(cols.video_rate > run.video_rate && (run.video_rate!=0 || run.ignore==0))
    cols.video_rate_xcd=1;  
}


void row::calc_middecks(){
  cols.middecks_ttl=0;
  cols.middecks_xcd=0;
  for(int n=0;n!=stn->modules.size();n++)
      cols.middecks_ttl+=stn->modules[n].calc_middecks_ttl();
  if(cols.middecks_ttl > run.MLE && (run.MLE!=0 || run.ignore==0))cols.middecks_xcd=1;
}




void row::calc_stowage(){
  cols.stowage_ttl=0;
  cols.stowage_xcd=0;
  for(int n=0;n!=stn->modules.size();n++)
    cols.stowage_ttl+=stn->modules[n].calc_stowage_ttl();
  if(cols.stowage_ttl > run.stowage*run.stowage_MLEs && (run.stowage!=0 || run.ignore==0))cols.stowage_xcd=1;
}




void row::calc_upmass(){
  cols.upmass_ttl=0;
  cols.upmass_xcd=0;
  for(int n=0;n!=stn->modules.size();n++)
    cols.upmass_ttl+=stn->modules[n].calc_upmass_ttl();
  if(cols.upmass_ttl > run.upmass-run.bulk && (run.upmass!=0 || run.ignore==0))cols.upmass_xcd=1;
}




void row::calc_crewtrng(){
  cols.crewtrng_ttl=0;
  cols.crewtrng_xcd=0;
  for(int n=0;n!=stn->modules.size();n++)
    cols.crewtrng_ttl+=stn->modules[n].calc_crewtrng_ttl();
  if(cols.crewtrng_ttl > run.crewtrng && (run.crewtrng!=0 || run.ignore==0))cols.crewtrng_xcd=1;
}





void row::calc_crewtime(){
  cols.crewtime_ttl=0;
  cols.crewtime_xcd=0;
  for(int n=0;n!=stn->modules.size();n++)
    cols.crewtime_ttl+=stn->modules[n].calc_crewtime_ttl();
  if(cols.crewtime_ttl > run.crewtime * run.inclength/7 && (run.crewtime!=0 || run.ignore==0))cols.crewtime_xcd=1;
}


void row::calc_runtime(){
  cols.runtime_ttl=0;
  for(int n=0;n!=stn->modules.size();n++)
    cols.runtime_ttl+=stn->modules[n].calc_runtime_ttl();
}


void row::calc_power(){
  int n=0;
  cols.power_ttl=0;
  cols.power_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.power_ttl_mod[n]=stn->modules[j].calc_power_ttl();
      if(run.newsams==1 && n==run.samsmod)
	{
	  cols.power_ttl_mod[run.samsmod]+=run.ku_power+run.pl_power;
	  if(run.needssams==1)
	    cols.power_ttl_mod[run.samsmod]+=run.sams_power;
	}
      cols.power_ttl+=cols.power_ttl_mod[n];
      if(cols.power_ttl_mod[n] > stn->modules[j].power && (stn->modules[j].power!=0 || run.ignore==0))
	{
	  cols.power_xcd_mod[n]=1;
	  cols.power_xcd=1;
	}
      else
	cols.power_xcd_mod[n]=0;
      n++;
    }
  if(cols.power_ttl > run.power && (run.power!=0 || run.ignore==0))cols.power_xcd=1;
}





void row::calc_energy(){
  int n=0;
  cols.energy_ttl=0;
  cols.energy_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.energy_ttl_mod[n]=stn->modules[j].calc_energy_ttl();
      if(run.newsams==1 && n==run.samsmod)
	{
	  cols.energy_ttl_mod[n]+=(run.ku_power+run.pl_power)*run.inclength*24;
	  if(run.needssams==1)
	    cols.energy_ttl_mod[n]+=run.sams_power*run.inclength*24;
	}
      cols.energy_ttl+=cols.energy_ttl_mod[n];
      if(cols.energy_ttl_mod[n] > (stn->modules[j].power)*run.inclength*24 && (stn->modules[j].power!=0 || run.ignore==0))
	{
	  cols.energy_xcd_mod[n]=1;
	  cols.energy_xcd=1;
	}
      else
	cols.energy_xcd_mod[n]=0;
      n++;
    }
  if(cols.energy_ttl > run.power*run.inclength*24 && (run.power!=0 || run.ignore==0))cols.energy_xcd=1;
}



void row::calc_keepalive(){
  cols.keepalive_ttl=0;
  cols.keepalive_xcd=0;

  if(run.newsams)
    {
      cols.keepalive_ttl=run.ku_power+run.pl_power;
      if(run.needssams==1)
	{
	  cols.keepalive_ttl+=run.sams_power;
	}
    }

  for(int n=0;n!=stn->modules.size();n++)
    cols.keepalive_ttl+=stn->modules[n].calc_keepalive_ttl();
  if(cols.keepalive_ttl > run.keepalive-run.hskpg && (run.keepalive!=0 || run.ignore==0))cols.keepalive_xcd=1;
}



void row::calc_energy_crewtend(){
  int n=0;
  cols.energy_crewtend_ttl=0;
  cols.energy_crewtend_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.energy_crewtend_ttl_mod[n]=stn->modules[j].calc_energy_crewtend_ttl();
      cols.energy_crewtend_ttl+=cols.energy_crewtend_ttl_mod[n];
      if(cols.energy_crewtend_ttl_mod[n] > (stn->modules[j].power)*run.inclength*8 && (stn->modules[j].power!=0 || run.ignore==0))
	{
	  cols.energy_crewtend_xcd_mod[n]=1;
	  cols.energy_crewtend_xcd=1;
	}
      else
	cols.energy_crewtend_xcd_mod[n]=0;
      n++;
    }
}


void row::calc_MTL(){
  int n=0;
  cols.MTL_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.MTL_ttl_mod[n]=stn->modules[j].calc_MTL_ttl();
      if(cols.MTL_ttl_mod[n] > (stn->modules[j].MTL_total)*run.inclength*24 && (stn->modules[j].MTL_total!=0 || run.ignore==0))
	{
	  cols.MTL_xcd_mod[n]=1;
	  cols.MTL_xcd=1;
	}
      else
	cols.MTL_xcd_mod[n]=0;
      n++;
    }
  // Add JEMEF MTL to JEM MTL
  cols.MTL_ttl_mod[4]+=cols.MTL_ttl_mod[5];
  cols.MTL_ttl_mod[5]=0;
  cols.MTL_ttl_mod[4]+=cols.MTL_ttl_mod[6];
  cols.MTL_ttl_mod[6]=0;
}


void row::calc_LTL(){
  int n=0;
  cols.LTL_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.LTL_ttl_mod[n]=stn->modules[j].calc_LTL_ttl();
      if(cols.LTL_ttl_mod[n] > (stn->modules[j].LTL_total)*run.inclength*24 && (stn->modules[j].LTL_total || run.ignore==0))
	{
	  cols.LTL_xcd_mod[n]=1;
	  cols.LTL_xcd=1;
	}
      else
	cols.LTL_xcd_mod[n]=0;
      n++;
    }
}



void row::calc_MTL_simul(){
  int n=0;
  cols.MTL_simul_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.MTL_ttl_simul_mod[n]=stn->modules[j].calc_MTL_ttl_simul();
      if(cols.MTL_ttl_simul_mod[n] > (stn->modules[j].MTL_total) && (stn->modules[j].MTL_total!=0 || run.ignore==0))
	{
	  cols.MTL_xcd_simul_mod[n]=1;
	  cols.MTL_simul_xcd=1;
	}
      else
	cols.MTL_xcd_simul_mod[n]=0;
      n++;
    }
  // Add JEMEF MTL to JEM MTL
  cols.MTL_ttl_simul_mod[4]+=cols.MTL_ttl_simul_mod[5];
  cols.MTL_ttl_simul_mod[5]=0;
  cols.MTL_ttl_simul_mod[4]+=cols.MTL_ttl_simul_mod[6];
  cols.MTL_ttl_simul_mod[6]=0;
}



void row::calc_LTL_simul(){
  int n=0;
  cols.LTL_simul_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.LTL_ttl_simul_mod[n]=stn->modules[j].calc_LTL_ttl_simul();
      if(cols.LTL_ttl_simul_mod[n] > (stn->modules[j].LTL_total) && (stn->modules[j].LTL_total || run.ignore==0))
	{
	  cols.LTL_xcd_simul_mod[n]=1;
	  cols.LTL_simul_xcd=1;
	}
      else
	cols.LTL_xcd_simul_mod[n]=0;
      n++;
    }
}



void row::calc_MTL_crewtend(){
  int n=0;
  cols.MTL_crewtend_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.MTL_crewtend_ttl_mod[n]=stn->modules[j].calc_MTL_crewtend_ttl();
      if(cols.MTL_crewtend_ttl_mod[n] > (stn->modules[j].MTL_total)*run.inclength*8 && (stn->modules[j].MTL_total!=0 || run.ignore==0))
	{
	  cols.MTL_crewtend_xcd_mod[n]=1;
	  cols.MTL_crewtend_xcd=1;
	}
      else
	cols.MTL_crewtend_xcd_mod[n]=0;
      n++;
    }
  // Add JEMEF MTL to JEM MTL
  cols.MTL_crewtend_ttl_mod[4]+=cols.MTL_crewtend_ttl_mod[5];
  cols.MTL_crewtend_ttl_mod[5]=0;
  cols.MTL_crewtend_ttl_mod[4]+=cols.MTL_crewtend_ttl_mod[6];
  cols.MTL_crewtend_ttl_mod[6]=0;
}



void row::calc_LTL_crewtend(){
  int n=0;
  cols.LTL_crewtend_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.LTL_crewtend_ttl_mod[n]=stn->modules[j].calc_LTL_crewtend_ttl();
      if(cols.LTL_crewtend_ttl_mod[n] > (stn->modules[j].LTL_total)*run.inclength*8 && (stn->modules[j].LTL_total!=0 || run.ignore==0))
	{
	  cols.LTL_crewtend_xcd_mod[n]=1;
	  cols.LTL_crewtend_xcd=1;
	}
      else
	cols.LTL_crewtend_xcd_mod[n]=0;
      n++;
    }
}



void row::calc_acoustics(){
  int n=0;
  cols.acoustics_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.acoustics_ttl_mod[n]=stn->modules[j].calc_acoustics_ttl();
      if(cols.acoustics_ttl_mod[n] > (stn->modules[j].acoustics)*run.inclength*24 && (stn->modules[j].acoustics!=0 || run.ignore==0))
	{
	  cols.acoustics_xcd_mod[n]=1;
	  cols.acoustics_xcd=1;
	}
      else
	cols.acoustics_xcd_mod[n]=0;
      n++;
    }
}




void row::calc_acoustics_simul(){
  int n=0;
  cols.acoustics_xcd_simul=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.acoustics_ttl_simul_mod[n]=stn->modules[j].calc_acoustics_ttl_simul();
      if(cols.acoustics_ttl_simul_mod[n] > (stn->modules[j].acoustics) && (stn->modules[j].acoustics!=0 || run.ignore==0))
	{
	  cols.acoustics_xcd_simul_mod[n]=1;
	  cols.acoustics_xcd_simul=1;
	}
      else
	cols.acoustics_xcd_simul_mod[n]=0;
      n++;
    }
}




void row::calc_acoustics_crewtend(){
  int n=0;
  cols.acoustics_crewtend_xcd=0;
  for(int j=0;j!=stn->modules.size();j++)
    {
      cols.acoustics_crewtend_ttl_mod[n]=stn->modules[j].calc_acoustics_crewtend_ttl();
      if(cols.acoustics_crewtend_ttl_mod[n] > (stn->modules[j].acoustics)*run.inclength*8 && (stn->modules[j].acoustics!=0 || run.ignore==0))
	{
	  cols.acoustics_crewtend_xcd_mod[n]=1;
	  cols.acoustics_crewtend_xcd=1;
	}
      else
	cols.acoustics_crewtend_xcd_mod[n]=0;
      n++;
    }
}




void row::addcombo(vector<rack> combo){
  stn->addcombo(combo);
}

void row::addkeepalivecombo(vector<rack> combo){
  stn->addkeepalivecombo(combo);
}
