def Make_AxisFieldRF_Gaps_and_Find_Neihbor_Nodes(rf_length_tolerance,accLattice,accSeq,dir_location,cavs):
	"""
	It returns (af_rf_gap_dict,rf_gap_ind_up_down_arr).
	This function analyzes the nodes in the accSeq and creates a dictionary and 
	an array:
	af_rf_gap_dict[rf_gap] = AxisFieldRF_Gap(rf_gap)
	and
	rf_gap_ind_up_down_arr[[rf_gap,gap_ind,drift_down_ind,drift_up_ind],...]
	where rf_gap is a BaseRF_Gap instance, and indexes drift_down_ind and 
	drift_up_ind are the indexes covering the edges of the axis filed of the
	particular AxisFieldRF_Gap.
	"""
	rank = orbit_mpi.MPI_Comm_rank(orbit_mpi.mpi_comm.MPI_COMM_WORLD)
	nodes = accSeq.getNodes()
	node_pos_dict = accLattice.getNodePositionsDict()
	#--------------------------------------------------
	#---- let's create the new AxisFieldRF_Gap instances
	af_rf_gap_dict = {}
	for cav in cavs:
		rf_gaps = cav.getRF_GapNodes()
		for rf_gap in rf_gaps:
			af_rf_gap = AxisFieldRF_Gap(rf_gap)
			af_rf_gap.readAxisFieldFile(dir_location)
			af_rf_gap_dict[rf_gap] = af_rf_gap
	#--------------------------------------------------
	#---- Let's fix the length of the axis field of the first gap if it is goes 
	#---- beyond of the beginning of the sequence
	if(len(cavs) > 0):
		accSeq_pos = accSeq.getPosition()	
		cav = cavs[0]
		rf_gap = cav.getRF_GapNodes()[0]
		(gap_pos_start,gap_pos_end) = node_pos_dict[rf_gap]
		(z_min,z_max) = af_rf_gap_dict[rf_gap].getZ_Min_Max()
		field_start = gap_pos_start - accSeq_pos + z_min
		if(field_start < rf_length_tolerance):
			z_min_new =  z_min + abs(field_start) + rf_length_tolerance
			func = af_rf_gap_dict[rf_gap].getAxisFieldFunction()
			func_new = RenormalizeFunction(func,z_min_new,z_max)
			af_rf_gap_dict[rf_gap].setAxisFieldFunction(func_new)
			(z_min_new,z_max_new) = (func_new.getMinX(),func_new.getMaxX())
			af_rf_gap_dict[rf_gap].setZ_Min_Max(z_min_new,z_max_new)
			msg  = "debug =============== WARNING  START ================ RF Gap="+rf_gap.getName()	
			msg += os.linesep
			msg += "Inside the Replace_BaseRF_Gap_to_AxisField_Nodes Python function. "
			msg += os.linesep
			msg += "The RF gap field  goes outside the start of the  AccSequence = " + accSeq.getName()
			msg += os.linesep
			msg += "The RF gap = " + rf_gap.getName()
			msg += os.linesep		
			msg += "That is wrong! The field will be cut shorter and re-normalized!"
			msg += os.linesep
			msg += "rf_gap (pos_start,pos_end)   = " + 	str((gap_pos_start- accSeq_pos,gap_pos_end - accSeq_pos))
			msg += os.linesep
			msg += "old rf gap (z_min,z_max) = " + str((z_min,z_max))
			msg += os.linesep
			msg += "new rf gap (z_min,z_max) = " + str((z_min_new,z_max_new))
			msg += os.linesep
			msg += "debug =============== WARNING  END ================"
			if(rank == 0): print msg
	#--------------------------------------------------
	#---- Let's fix the length of the axis fields to avoid the fields overlaping
	for cav in cavs:
		rf_gaps = cav.getRF_GapNodes()
		for gap_ind in range(len(rf_gaps) - 1):
			rf_gap0 = rf_gaps[gap_ind]
			rf_gap1 = rf_gaps[gap_ind+1]
			(gap0_pos_start,gap0_pos_end) = node_pos_dict[rf_gap0]
			(gap1_pos_start,gap1_pos_end) = node_pos_dict[rf_gap1]
			gap0_pos_end += af_rf_gap_dict[rf_gap0].getZ_Min_Max()[1]
			gap1_pos_start += af_rf_gap_dict[rf_gap1].getZ_Min_Max()[0]
			delta_z = gap0_pos_end - gap1_pos_start
			if(math.fabs(delta_z) < rf_length_tolerance):
				(z_min,z_max) = af_rf_gap_dict[rf_gap0].getZ_Min_Max()
				z_max -= delta_z
				af_rf_gap_dict[rf_gap0].setZ_Min_Max(z_min,z_max)
				#print "debug gap0=",rf_gap0.getName()," gap1=",rf_gap1.getName()," delta=",delta_z
			else:
				if(delta_z > 0.):
					msg  = "The Replace_BaseRF_Gap_to_AxisField_Nodes Python function. "
					msg += os.linesep
					msg += "The RF gap field overlaps more than rf_length_tolerance[mm]= "+str(1000.*rf_length_tolerance)
					msg += os.linesep
					msg = msg + "RF gap 0 = " + rf_gap0.getName()				
					msg = msg + os.linesep
					msg = msg + "RF gap 1 = " + rf_gap1.getName()				
					msg = msg + os.linesep
					(z_min,z_max) = af_rf_gap_dict[rf_gap0].getZ_Min_Max()
					(pos_start,pos_stop) = (gap0_pos_start+z_min,gap0_pos_start+z_max) 
					msg = msg + "Gap 0 (pos_start,pos_stop)= " + str((pos_start,pos_stop))
					msg = msg + os.linesep
					(z_min,z_max) = af_rf_gap_dict[rf_gap1].getZ_Min_Max()
					(pos_start,pos_stop) = (gap1_pos_end+z_min,gap1_pos_end+z_max) 
					msg = msg + "Gap 1 (pos_start,pos_stop)= " + str((pos_start,pos_stop))
					msg = msg + os.linesep	
					msg = msg + "Gap 0 limits (z_min,z_max)= " + str(af_rf_gap_dict[rf_gap0].getZ_Min_Max())
					msg = msg + os.linesep	
					msg = msg + "Gap 1 limits (z_min,z_max)= " + str(af_rf_gap_dict[rf_gap1].getZ_Min_Max())
					msg = msg + os.linesep
					msg = msg + "Overlapping delta= " + str(delta_z)
					msg = msg + os.linesep
					orbitFinalize(msg)
	#--------------------------------------------------------------------------------------
	#---- array with [rf_gap, drift indexes for the gap and drifts before and after the gap]
	#---- Here we will go through all rf gaps and find indexes of the drifts before (down) 
	#---- and after (up). These drifts should be replaced with the shorter drifts.
	#---- The drifts that covered by the RF gap field completely should be removed.
	#---------------------------------------------------------------------------------------
	#---- to speed up indexing let's build rf gaps vs. index dictionary
	rf_gap_ind_dict = {}
	for node_ind in range(len(nodes)):
		node = nodes[node_ind]
		if(isinstance(node,BaseRF_Gap)):
			rf_gap_ind_dict[node] = node_ind
	#-------------------------------------
	rf_gap_ind_up_down_arr = []
	for cav in cavs:
		rf_gaps = cav.getRF_GapNodes()
		for rf_gap in rf_gaps:
			gap_ind = rf_gap_ind_dict[rf_gap]
			(gap_pos_start,gap_pos_end) = node_pos_dict[rf_gap]
			drift_down_ind = gap_ind
			drift_up_ind = gap_ind
			(z_min,z_max) = af_rf_gap_dict[rf_gap].getZ_Min_Max()
			#---- let's find the next upstream node covering the edge of the field
			drift_down_ind = gap_ind - 1
			node =  nodes[drift_down_ind]
			(node_pos_start,node_pos_end) = node_pos_dict[node]
			while(node_pos_end > gap_pos_start + z_min):
				drift_down_ind = drift_down_ind - 1
				#--- if it is the beginning of sequence - we are done!
				if(drift_down_ind < 0):
					if(gap_pos_start + z_min < node_pos_start):
						node = nodes[drift_down_ind+1]
						#---- by default gap_pos_start=gap_pos_end for rf gap with length=0
						(gap_pos_start,gap_pos_end) = node_pos_dict[rf_gap]
						(z_min,z_max) = af_rf_gap_dict[rf_gap].getZ_Min_Max()
						(gap_pos_start,gap_pos_end) = (gap_pos_start+z_min,gap_pos_end+z_max) 
						(pos_start,pos_end) = node_pos_dict[node]
						func = af_rf_gap_dict[rf_gap].getAxisFieldFunction()
						delta_cut = pos_start - gap_pos_start
						func_new = RenormalizeFunction(func,z_min+delta_cut,z_max)
						af_rf_gap_dict[rf_gap].setAxisFieldFunction(func_new)
						(z_min_new,z_max_new) = (func_new.getMinX(),func_new.getMaxX())
						af_rf_gap_dict[rf_gap].setZ_Min_Max(z_min_new,z_max_new)
						msg  = "debug =============== WARNING  START ================ RF Gap="+rf_gap.getName()	
						msg += os.linesep
						msg += "Inside the Replace_BaseRF_Gap_to_AxisField_Nodes Python function. "
						msg += os.linesep
						msg += "The RF gap field overlaps the first element in AccSequence."
						msg += os.linesep
						msg += "It means that the field goes outside the AccSequence."
						msg += os.linesep
						msg += "That is wrong! The field will be cut shorter and re-normalized!"
						msg += os.linesep
						msg += "node    = " + node.getName()			
						msg += os.linesep
						msg += "node (pos_start,pos_end)   = " + 	str((pos_start,pos_end))
						msg += os.linesep
						msg += "rf_gap (pos_start,pos_end)   = " + 	str((gap_pos_start,gap_pos_end))
						msg += os.linesep
						msg += "old rf gap (z_min,z_max) = " + str((z_min,z_max))
						msg += os.linesep
						msg += "new rf gap (z_min,z_max) = " + str((z_min_new,z_max_new))
						msg += os.linesep
						msg += "debug =============== WARNING  END ================"
						orbitFinalize(msg)
					else:
						break
				#---------------------------------------------------------------------
				node =  nodes[drift_down_ind]
				(node_pos_start,node_pos_end) = node_pos_dict[node]
			drift_down_ind = drift_down_ind + 1
			#---------------------------------
			drift_up_ind = gap_ind + 1
			node =  nodes[drift_up_ind]
			(node_pos_start,node_pos_end) = node_pos_dict[node]
			while(node_pos_start < gap_pos_start + z_max):	
				drift_up_ind = drift_up_ind + 1
				#--- if it is the end of sequence - we are done!
				if(drift_up_ind > len(nodes) -1):
					if(gap_pos_start + z_max > node_pos_end):
						node = nodes[drift_up_ind-1]
						msg  = "The Replace_BaseRF_Gap_to_AxisField_Nodes Python function. "
						msg += os.linesep
						msg += "The RF gap field overlaps the last element in AccSequence."
						msg += os.linesep
						msg += "It means that the field goes outside the AccSequence."
						msg += os.linesep
						msg += "That is wrong! Stop! Please check the lattice!"
						msg += os.linesep
						msg += "RF gap  = " + rf_gap.getName()				
						msg += os.linesep
						msg += "node    = " + node.getName()			
						msg += os.linesep
						(gap_pos_start,gap_pos_end) = node_pos_dict[rf_gap]
						(z_min,z_max) = af_rf_gap_dict[rf_gap].getZ_Min_Max()
						(gap_pos_start,gap_pos_end) = (gap_pos_end+z_min,gap_pos_end+z_max) 
						(pos_start,pos_end) = node_pos_dict[node]	
						msg += "node (pos_start,pos_end)   = " + 	str((pos_start,pos_end))
						msg += os.linesep
						msg += "rf_gap (pos_start,pos_end)   = " + 	str((gap_pos_start,gap_pos_end))
						msg += os.linesep
						orbitFinalize(msg)
					else:
						break
				node =  nodes[drift_up_ind]
				(node_pos_start,node_pos_end) = node_pos_dict[node]
			drift_up_ind = drift_up_ind - 1
			rf_gap_ind_up_down_arr.append([rf_gap,gap_ind,drift_down_ind,drift_up_ind])
	#----------------------------------------------------------------------
	"""
	#---- Debug printing part of the code ---------------START-------------
	for node_ind in range(len(nodes)):
		node = nodes[node_ind]
		if(af_rf_gap_dict.has_key(node)):
			(pos_start,pos_stop) = node_pos_dict[node]
			pos_start += af_rf_gap_dict[node].getZ_Min_Max()[0]
			pos_stop += af_rf_gap_dict[node].getZ_Min_Max()[1]
			print "debug node_ind=",node_ind," node=",node.getName()," (pos_start,pos_end)=",(pos_start,pos_stop)
		else:
			print "debug node_ind=",node_ind," node=",node.getName()," (pos_start,pos_end)=",node_pos_dict[node]
	for [rf_gap,gap_ind,drift_down_ind,drift_up_ind] in rf_gap_ind_up_down_arr:
		print "debug gap=",rf_gap.getName()," gap_ind=",gap_ind," drift_down_ind=",drift_down_ind," drift_up_ind=",drift_up_ind
	#---- Debug printing part of the code ---------------STOP--------------
	"""
	#----------------------------------------------------------------------
	return (af_rf_gap_dict,rf_gap_ind_up_down_arr)