def all_obs(bins512 = False, date_lim=False):
  if bins512: template = '/data1/Daniele/B2217+47/ephemeris/160128_profile_template_512.std'
  else: template = '/data1/Daniele/B2217+47/ephemeris/151109_profile_template.std'
  template = psrchive.Archive_load(template).get_data().flatten()

  dates,obs_list = cum_profile.plot_lists(template=template, bin_reduc=bins512, date_lim=date_lim)
  observations = np.array(obs_list)
  date_list = np.array(dates)

  #Remove bad observation
  #if bins512:
  #  idx = np.where(date_list == datetime.date(2011,10,30))[0][0]
  #  date_list = np.hstack((date_list[:idx],date_list[idx+1:]))
  #  observations = np.vstack((observations[:idx],observations[idx+1:]))

  #Add earliest LOFAR observations
  old_date, old_obs = cum_profile.load_early_obs(template=template,bin_reduc=bins512)
  old_date = np.array(old_date)
  old_obs = np.array(old_obs)
  #if bins512:
  #  old_obs = np.mean(np.reshape(old_obs,(old_obs.shape[0],old_obs.shape[1]/2,2)),axis=2)

  observations = np.vstack((old_obs,observations))
  date_list = np.hstack((old_date,date_list))
  idx = np.argsort(date_list)
  date_list = date_list[idx]
  observations = observations[idx]

  return date_list,observations
if bins512: template = '/data1/Daniele/B2217+47/ephemeris/160128_profile_template_512.std'
else: template = '/data1/Daniele/B2217+47/ephemeris/151109_profile_template.std'
template = psrchive.Archive_load(template).get_data().flatten()

dates,obs_list = cum_profile.plot_lists(template=template,bin_reduc=bins512)
observations = np.array(obs_list)
date_list = np.array(dates)

#Remove bad observation
if bins512:
  idx = np.where(date_list == datetime.date(2011,10,30))[0][0]
  date_list = np.hstack((date_list[:idx],date_list[idx+1:]))
  observations = np.vstack((observations[:idx],observations[idx+1:]))

#Add earliest LOFAR observations
old_date, old_obs = cum_profile.load_early_obs(template=template,bin_reduc=bins512)
old_date = np.array(old_date)
old_obs = np.array(old_obs)

observations = np.vstack((old_obs,observations))
date_list = np.hstack((old_date,date_list))
idx = np.argsort(date_list)
date_list = date_list[idx]
observations = observations[idx]

#Average Observations on the same day
date_uniq, idx_uniq, repeated = np.unique(date_list,return_index=True,return_counts=True)
obs_uniq = observations[idx_uniq]
idx = np.where(repeated>1)[0]
for i in idx:
  date_rep = np.where(date_list == date_uniq[i])[0]