title: Converting angles outside the natural range (for theta: 0 to 180 degrees) to an equivalent angle, inside that range
body: <p>How can I shorten the following code? It is a simple code that looks at the angle value and makes sure that they do not go out of range. The problem is that although this does the job, I really want something that is more pythonic, easier to work with and less cumbersome to write. </p>
  
  <pre><code>theta_min_lim = 0.0
  theta_max_lim = 180.0
  if theta_min&lt;theta_min_lim:
      new_theta_min = theta_max_lim-abs(theta_min)
      theta_row_index_along_new = np.array(np.where(sph_pos_count[:,1]&gt;=new_theta_min)).flatten()
      theta_row_index_along = np.concatenate((theta_row_index_along_new, theta_row_index_along))
  
  if theta_max&gt;theta_max_lim:
      new_theta_max = theta_max-theta_max_lim
      theta_row_index_along_new = np.array(np.where(sph_pos_count[:,1]&lt;=new_theta_max)).flatten()
      theta_row_index_along = np.concatenate((theta_row_index_along_new, theta_row_index_along))
  
  if phi_min&lt;phi_min_lim:
      new_phi_min = phi_max_lim-abs(phi_min)
      phi_row_index_along_new = np.array(np.where(sph_pos_count[:,2]&gt;=new_phi_min)).flatten()
      phi_row_index_along = np.concatenate((phi_row_index_along_new, phi_row_index_along))
  
  if phi_max&gt;phi_max_lim:
      new_phi_max = phi_max-phi_max_lim
      phi_row_index_along_new = np.array(np.where(sph_pos_count[:,2]&lt;=new_phi_max)).flatten()
      phi_row_index_along = np.concatenate((phi_row_index_along_new, phi_row_index_along))
  
  #theta_row_index_along = np.concatenate(theta_row_index_along_new, theta_row_index_along)
  row_index_along = np.intersect1d(theta_row_index_along,phi_row_index_along)
  
  sph_pos_count_along = sph_pos_count[row_index_along]
  
  #GIving range for theta and phi in the direction along the velocity
   theta_min_opp = vtheta_opp - 2.0*max(sph_cord[:,1])/dtheta
   theta_max_opp = vtheta_opp + 2.0*max(sph_cord[:,1])/dtheta
   phi_min_opp = vphi_opp - 2.0*max(sph_cord[:,2])/dphi
   phi_max_opp = vphi_opp + 2.0*max(sph_cord[:,2])/dphi
  
   #Finding index of spheres opposite to the direction of motion
  
   theta_row_index_opp = np.array(np.where(np.logical_and(sph_pos_count[:,1]&gt;=theta_min_opp, sph_pos_count[:,1]&lt;=theta_max_opp))).flatten()
  phi_row_index_opp = np.array(np.where(np.logical_and(sph_pos_count[:,2]&gt;=phi_min_opp,  sph_pos_count[:,2]&lt;=phi_max_opp))).flatten()
  
  if theta_min_opp&lt;theta_min_lim:
      new_theta_min_opp = theta_max_lim-abs(theta_min_opp)
      theta_row_index_opp_new = np.array(np.where(sph_pos_count[:,1]&gt;=new_theta_min_opp)).flatten()
      theta_row_index_opp = np.concatenate((theta_row_index_opp_new, theta_row_index_opp))
  
  if theta_max_opp&gt;theta_max_lim:
      new_theta_max_opp = theta_max_opp-theta_max_lim
      theta_row_index_opp_new = np.array(np.where(sph_pos_count[:,1]&lt;=new_theta_max_opp)).flatten()
      theta_row_index_opp = np.concatenate((theta_row_index_opp_new, theta_row_index_opp))
  
  if phi_min_opp&lt;phi_min_lim:
      new_phi_min_opp = phi_max_lim-abs(phi_min_opp)
      phi_row_index_opp_new = np.array(np.where(sph_pos_count[:,2]&gt;=new_phi_min_opp)).flatten()
      phi_row_index_opp = np.concatenate((phi_row_index_opp_new, phi_row_index_opp))
  
  if phi_max_opp&gt;phi_max_lim:
      new_phi_max_opp = phi_max_opp-phi_max_lim
      phi_row_index_opp_new = np.transpose(np.where(sph_pos_count[:,2]&lt;=new_phi_max_opp)).flatten()
      phi_row_index_opp = np.concatenate((phi_row_index_opp_new, phi_row_index_opp))
  </code></pre>