old_contents: """
  Author: Marusa Zerjal, 2019 - 07 - 15
  
  Take Sco-Cen components fitted to 6D data and make overlaps
  (using covariance matrix) with stars missing radial velocities
  in order to find more Sco-Cen candidates.
  
  MZ: It fails in python2 (cannot import emcee).
  
  """
  
  import numpy as np
  import sys
  sys.path.insert(0, '..')
  from chronostar.component import SphereComponent
  from chronostar import tabletool
  from chronostar import expectmax
  from astropy.table import Table, vstack, join
  
  
  
  c = np.load('all_nonbg_scocen_comps.npy') # including LCC
  print('components', c.shape)
  print('Are there duplicate components?')
  
  datafile = 'data_table_cartesian_with_bg_ols.fits'
  
  # Read Gaia data including both stars with known and missing radial velocities
  datafile = 'data_table_cartesian_100k.fits'
  data_table = tabletool.read(datafile)
  
  print('DATA READ', len(data_table))
  historical = 'c_XU' in data_table.colnames
  
  """
  # Read backgrounf overlaps
  ln_bg_ols = np.loadtxt('../scripts/bgols_multiprocessing.dat')
  print('len bg_ols', len(ln_bg_ols), 'len data_table', len(data_table))
  
  bg_lnol_colname = 'background_log_overlap'
  print('Background overlaps: insert column')
  tabletool.insert_column(data_table, ln_bg_ols, bg_lnol_colname, filename=datafile)
  
  print('Print bg ols to cartesian table')
  data_table.write('data_table_cartesian_with_bg_ols.fits', overwrite=True, format='fits')
  """
  
  ############################################################################
  ############ COMPONENT OVERLAPS ############################################
  ############################################################################
  
  print('Create data dict')
  # Create data dict for real
  data_dict = tabletool.build_data_dict_from_table(
          data_table,
          get_background_overlaps=True,
          historical=historical,
  )
  
  print 'KEYS', data_dict.keys()
  
  # Create components
  comps = [SphereComponent(pars=x) for x in c]
  
  # What about background?
  
  # COMPONENT OVERLAPS
  overlaps = expectmax.get_all_lnoverlaps(data_dict, comps)
  #print(overlaps)
  #print('overlaps.shape', overlaps.shape)
  
  membership_probabilities = np.array([expectmax.calc_membership_probs(ol) for ol in overlaps])
  #print(membership_probabilities)
  #print(membership_probabilities.shape)
  
  for i in range(membership_probabilities.shape[1]):
      data_table['comp_overlap_%d' % (i + 1)] = membership_probabilities[:, i]
  #for i in range(4):
  #    data_table['comp_overlap_usco%d'%(i+1)]=membership_probabilities[:,i]
  #for i in range(4):
  #    data_table['comp_overlap_ucl%d'%(i+1)]=membership_probabilities[:,i+4]
  
  print(data_table)
  #data_table.write('data_table_cartesian_with_bg_ols_and_component_overlaps.fits', format='fits')
  
new_contents: """
  Author: Marusa Zerjal, 2019 - 07 - 15
  
  Take Sco-Cen components fitted to 6D data and make overlaps
  (using covariance matrix) with stars missing radial velocities
  in order to find more Sco-Cen candidates.
  
  MZ: It fails in python2 (cannot import emcee).
  
  """
  
  import numpy as np
  import sys
  sys.path.insert(0, '..')
  from chronostar.component import SphereComponent
  from chronostar import tabletool
  from chronostar import expectmax
  from astropy.table import Table, vstack, join
  
  
  
  c = np.load('all_nonbg_scocen_comps.npy') # including LCC
  print('components', c.shape)
  print('Are there duplicate components?')
  
  datafile = 'data_table_cartesian_with_bg_ols.fits'
  
  # Read Gaia data including both stars with known and missing radial velocities
  datafile = 'data_table_cartesian_100k.fits'
  data_table = tabletool.read(datafile)
  
  print('DATA READ', len(data_table))
  historical = 'c_XU' in data_table.colnames
  
  """
  # Read backgrounf overlaps
  ln_bg_ols = np.loadtxt('../scripts/bgols_multiprocessing.dat')
  print('len bg_ols', len(ln_bg_ols), 'len data_table', len(data_table))
  
  bg_lnol_colname = 'background_log_overlap'
  print('Background overlaps: insert column')
  tabletool.insert_column(data_table, ln_bg_ols, bg_lnol_colname, filename=datafile)
  
  print('Print bg ols to cartesian table')
  data_table.write('data_table_cartesian_with_bg_ols.fits', overwrite=True, format='fits')
  """
  
  ############################################################################
  ############ COMPONENT OVERLAPS ############################################
  ############################################################################
  
  print('Create data dict')
  # Create data dict for real
  data_dict = tabletool.build_data_dict_from_table(
          data_table,
          get_background_overlaps=True,
          historical=historical,
  )
  
  print 'KEYS', data_dict.keys()
  
  # Create components
  comps = [SphereComponent(pars=x) for x in c]
  
  # What about background?
  
  # COMPONENT OVERLAPS
  overlaps = expectmax.get_all_lnoverlaps(data_dict, comps)
  #print(overlaps)
  print('overlaps.shape', overlaps.shape, len(comps))
  
  membership_probabilities = np.array([expectmax.calc_membership_probs(ol) for ol in overlaps])
  #print(membership_probabilities)
  #print(membership_probabilities.shape)
  
  for i in range(membership_probabilities.shape[1]):
      data_table['comp_overlap_%d' % (i + 1)] = membership_probabilities[:, i]
  #for i in range(4):
  #    data_table['comp_overlap_usco%d'%(i+1)]=membership_probabilities[:,i]
  #for i in range(4):
  #    data_table['comp_overlap_ucl%d'%(i+1)]=membership_probabilities[:,i+4]
  
  print(data_table)
  #data_table.write('data_table_cartesian_with_bg_ols_and_component_overlaps.fits', format='fits')
  
subject: print keys