def buffer_2_beam(buf, mode='pickle'):
	
	if buf[0]<0:
		beam=None
	else:
		i_buf = 0
		
		macroparticlenumber = int(buf[i_buf])
		i_buf += 1
		
		particlenumber_per_mp = buf[i_buf]
		i_buf += 1
		
		charge = buf[i_buf]
		i_buf += 1
		
		mass = buf[i_buf]
		i_buf += 1
		
		circumference = buf[i_buf]
		i_buf += 1
		
		gamma = buf[i_buf]
		i_buf += 1
		
		id_ = buf[i_buf:i_buf+macroparticlenumber].astype(np.int32)
		i_buf += macroparticlenumber

		x =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		xp =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		y =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		yp =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		z =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber	
		
		dp = buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		slice_info_buf_size = int(buf[i_buf])
		i_buf += 1
		
		slice_info_buf = buf[i_buf:i_buf+slice_info_buf_size]
		i_buf += slice_info_buf_size
		
		beam = Particles(macroparticlenumber=macroparticlenumber,
						particlenumber_per_mp=particlenumber_per_mp, charge=charge,
						mass=mass, circumference=circumference, gamma=gamma, 
						coords_n_momenta_dict={\
								'x': np.atleast_1d(x),
								'xp':np.atleast_1d(xp),
								'y':np.atleast_1d(y),
								'yp':np.atleast_1d(yp),	
								'z':np.atleast_1d(z),
								'dp':np.atleast_1d(dp)})
		
		beam.id = np.atleast_1d(id_)

		
		if mode=='json':
			si_int = slice_info_buf.astype(np.int)
			si_str = ''.join(map(unichr, list(si_int)))
			beam.slice_info = json.loads(si_str)
		elif mode=='pickle':
			# Get length in bytes
			llrec = int(slice_info_buf[0])
			s1back_padded = np.frombuffer(slice_info_buf[1:].tobytes(), dtype='S1') 
			s1back = s1back_padded[:llrec]
			pss_rec = s1back.tobytes()
			beam.slice_info = pickle.loads(pss_rec)
		else:
			raise ValueError('Unknown mode!')


		
		# if slice_info_buf[0] < 0.:
		# 	beam.slice_info = None
		# elif slice_info_buf[0] == 0.:
		# 	beam.slice_info = 'unsliced'
		# else:
		# 	beam.slice_info = {\
  #                   'z_bin_center':slice_info_buf[1] ,
  #                   'z_bin_right':slice_info_buf[2],
  #                   'z_bin_left':slice_info_buf[3]}




	return beam
def buffer_2_beam(buf):

    if buf[0] < 0:
        beam = None
    else:
        i_buf = 0

        macroparticlenumber = int(buf[i_buf])
        i_buf += 1

        particlenumber_per_mp = buf[i_buf]
        i_buf += 1

        charge = buf[i_buf]
        i_buf += 1

        mass = buf[i_buf]
        i_buf += 1

        circumference = buf[i_buf]
        i_buf += 1

        gamma = buf[i_buf]
        i_buf += 1

        id_ = np.int_(buf[i_buf:i_buf + macroparticlenumber])
        i_buf += macroparticlenumber

        x = buf[i_buf:i_buf + macroparticlenumber]
        i_buf += macroparticlenumber

        xp = buf[i_buf:i_buf + macroparticlenumber]
        i_buf += macroparticlenumber

        y = buf[i_buf:i_buf + macroparticlenumber]
        i_buf += macroparticlenumber

        yp = buf[i_buf:i_buf + macroparticlenumber]
        i_buf += macroparticlenumber

        z = buf[i_buf:i_buf + macroparticlenumber]
        i_buf += macroparticlenumber

        dp = buf[i_buf:i_buf + macroparticlenumber]
        i_buf += macroparticlenumber

        slice_info_buf = buf[i_buf:i_buf + 4]
        i_buf += 4

        beam = Particles(macroparticlenumber=macroparticlenumber,
            particlenumber_per_mp=particlenumber_per_mp, charge=charge,
            mass=mass, circumference=circumference, gamma=gamma,
            coords_n_momenta_dict={\
              'x': np.atleast_1d(x),
              'xp':np.atleast_1d(xp),
              'y':np.atleast_1d(y),
              'yp':np.atleast_1d(yp),
              'z':np.atleast_1d(z),
              'dp':np.atleast_1d(dp)})

        beam.id = np.atleast_1d(id_)

        if slice_info_buf[0] < 0.:
            beam.slice_info = None
        elif slice_info_buf[0] == 0.:
            beam.slice_info = 'unsliced'
        else:
            beam.slice_info = {\
                             'z_bin_center':slice_info_buf[1] ,
                             'z_bin_right':slice_info_buf[2],
                             'z_bin_left':slice_info_buf[3]}

    return beam
def buffer_2_beam(buf, mode='pickle'):
	
	if buf[0]<0:
		beam=None
	else:
		i_buf = 0
		
		macroparticlenumber = int(buf[i_buf])
		i_buf += 1
		
		particlenumber_per_mp = buf[i_buf]
		i_buf += 1
		
		charge = buf[i_buf]
		i_buf += 1
		
		mass = buf[i_buf]
		i_buf += 1
		
		circumference = buf[i_buf]
		i_buf += 1
		
		gamma = buf[i_buf]
		i_buf += 1
		
		id_ = buf[i_buf:i_buf+macroparticlenumber].astype(np.int32)
		i_buf += macroparticlenumber

		x =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		xp =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		y =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		yp =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		z =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber	
		
		dp = buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		slice_info_buf_size = int(buf[i_buf])
		i_buf += 1
		
		slice_info_buf = buf[i_buf:i_buf+slice_info_buf_size]
		i_buf += slice_info_buf_size
		
		beam = Particles(macroparticlenumber=macroparticlenumber,
						particlenumber_per_mp=particlenumber_per_mp, charge=charge,
						mass=mass, circumference=circumference, gamma=gamma, 
						coords_n_momenta_dict={\
								'x': np.atleast_1d(x),
								'xp':np.atleast_1d(xp),
								'y':np.atleast_1d(y),
								'yp':np.atleast_1d(yp),	
								'z':np.atleast_1d(z),
								'dp':np.atleast_1d(dp)})
		
		beam.id = np.atleast_1d(id_)

		
		if mode=='json':
			si_int = slice_info_buf.astype(np.int)
			si_str = ''.join(map(chr, list(si_int)))
			beam.slice_info = json.loads(si_str)
		elif mode=='pickle':
			# Get length in bytes
			llrec = int(slice_info_buf[0])
			s1back_padded = np.frombuffer(slice_info_buf[1:].tobytes(), dtype='S1') 
			s1back = s1back_padded[:llrec]
			pss_rec = s1back.tobytes()
			beam.slice_info = pickle.loads(pss_rec)
		else:
			raise ValueError('Unknown mode!')


		
		# if slice_info_buf[0] < 0.:
		# 	beam.slice_info = None
		# elif slice_info_buf[0] == 0.:
		# 	beam.slice_info = 'unsliced'
		# else:
		# 	beam.slice_info = {\
  #                   'z_bin_center':slice_info_buf[1] ,
  #                   'z_bin_right':slice_info_buf[2],
  #                   'z_bin_left':slice_info_buf[3]}




	return beam
def buffer_2_beam(buf):
	
	if buf[0]<0:
		beam=None
	else:
		i_buf = 0
		
		macroparticlenumber = int(buf[i_buf])
		i_buf += 1
		
		particlenumber_per_mp = buf[i_buf]
		i_buf += 1
		
		charge = buf[i_buf]
		i_buf += 1
		
		mass = buf[i_buf]
		i_buf += 1
		
		circumference = buf[i_buf]
		i_buf += 1
		
		gamma = buf[i_buf]
		i_buf += 1
		
		id_ = np.int_(buf[i_buf:i_buf+macroparticlenumber])
		i_buf += macroparticlenumber

		x =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		xp =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		y =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		yp =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		z =  buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber	
		
		dp = buf[i_buf:i_buf+macroparticlenumber]
		i_buf += macroparticlenumber
		
		slice_info_buf = buf[i_buf:i_buf+4]
		i_buf += 4
		
		beam = Particles(macroparticlenumber=macroparticlenumber,
						particlenumber_per_mp=particlenumber_per_mp, charge=charge,
						mass=mass, circumference=circumference, gamma=gamma, 
						coords_n_momenta_dict={\
								'x': np.atleast_1d(x),
								'xp':np.atleast_1d(xp),
								'y':np.atleast_1d(y),
								'yp':np.atleast_1d(yp),	
								'z':np.atleast_1d(z),
								'dp':np.atleast_1d(dp)})
		
		beam.id = np.atleast_1d(id_)
		
		if slice_info_buf[0] < 0.:
			beam.slice_info = None
		elif slice_info_buf[0] == 0.:
			beam.slice_info = 'unsliced'
		else:
			beam.slice_info = {\
                    'z_bin_center':slice_info_buf[1] ,
                    'z_bin_right':slice_info_buf[2],
                    'z_bin_left':slice_info_buf[3]}
	
	return beam