
    R1i                          d Z ddlmZmZ ddlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ dez  ZdZedfd	Zd
 Zdez  dfdZdez  dfdZd Zd Zd Zd Zy)z1Routines to search for maxima and zero crossings.    )print_functiondivision)addappendargsortbool_concatenatediffflatnonzeroint8
issubdtypelinspacemultiplyreshapesign   )DAY_SgMbP?N   c                 x   | j                   }| j                  }|j                  }||k\  rt        dj                  | |            t	        |dd      }|8t	        |dd      }	|	t        d      ||z
  |	z  }
|
dk  rd}
t        |
|z        }nt        ||z
  |z        dz   }t        |||      }t        |||||      S )zFind the times at which a discrete function of time changes value.

    This routine is used to find instantaneous events like sunrise,
    transits, and the seasons.  See :doc:`searches` for how to use it
    yourself.

    z3your start_time {0} is later than your end_time {1}	step_daysNrough_periodz?the function you have passed is missing a "step_days" attribute      ?   )	tstt
ValueErrorformatgetattrAttributeErrorintr   _find_discrete)
start_timeend_timefepsilonnumr   jd0jd1r   r   periodssample_countjds                V/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/skyfield/searchlib.pyfind_discreter-      s     
B
--C
++C
czN &X68 	8 ;-Iq.$7  "G H H9,S=G7S=) C#I23a7	#sL	)B"b!Wc22    c                 x   t        dd|      }|ddd   }t        j                  }	 | j                  |      } ||      }	t	        t        |	            }
t        |
      s#|j                  |
      }|	j                  |
      }	n|j                  |
      }|j                  |
dz         }|d   |d   z
  |k  r;|	j                  |
dz         }	t        t        |      d|z  kD  df      }||   }|	|   }	n0 |||      j                          |||      j                         z   }| j                  |      t        |	      fS )	z<Algorithm core, for callers that already have a `jd` vector.        r   Nr   r   g      @T)r   r   outertt_jdr   r
   lentaker	   flatten_fix_numpy_deprecation)r   r+   r$   r%   r&   end_mask
start_maskotyindicesendsstartsmasks                 r,   r!   r!   0   s8   S#&H$B$JA
HHRLaDd1g&7|777#DwA!www{#
 7VAY')w{#A dcGm!;gFGD:D$Avz"**,qx/@/H/H/JJ5 8 88D>1!444r.   r   c                     fd}t        dd      |_        t        dd      |_        t        | ||||      \  }}|t	        |       fS )zFind the local minima in the values returned by a function of time.

    This routine is used to find events like minimum elongation.  See
    :doc:`searches` for how to use it yourself.

    c                      |        S N )r<   r$   s    r,   gzfind_minima.<locals>.g[   s    adUlr.   r   Nr   )r   r   r   find_maximar8   )r"   r#   r$   r%   r&   rF   r<   r=   s     `     r,   find_minimarH   T   sQ     Q5AN![$/AKz8Q=DAq$aR(((r.   c                    | j                   }| j                  }|j                  }||k\  rt        dj                  | |            t	        |dd      }|7|j
                  |z  }	t        ||z
  |	z        dz   }
t        ||	z
  ||	z   |
      }n2t        ||z
  |z        dz   }||z
  |z  }t        ||z
  ||z   |dz         }t        dd|      }|ddd   }t        j                  }	 |j                  |      } ||      }|d	   |d
   z
  |k  rSt        ||      \  }}||k\  ||k  z  }||   }||   }t        |      r#t        dt        |      |kD  f      }||   }||   }nt        |      \  }}t         t!        ||||f       t        |      s|d
d
 x}}n^|j#                  |      }|j#                  |      } |||      j%                          |||      j%                         z   }t'        |      }|j                  |      t)        |      fS )zFind the local maxima in the values returned by a function of time.

    This routine is used to find events like highest altitude and
    maximum elongation.  See :doc:`searches` for how to use it yourself.

    z/start_time {0} is not earlier than end_time {1}r   N   r   r0   r   r1   r   r   r2   )r   r   r   r   r   r   r    r   r   r3   r4   _identify_maximar5   r	   r
   _choose_brackets_tracer6   r7   _remove_adjacent_duplicatesr8   )r"   r#   r$   r%   r&   r   r'   r(   r   bumpbumpsr+   steps	real_step	end_alphastart_alphar;   r<   r=   keepersrA   leftrightr@   r?   s                            r,   rG   rG   a   sC    
B
--C
++C
czJ &X68 	8 ;-I~~#S3Y$&'!+cDj#*e4 S3Y)+,q03Y%'	cIosY	Bc3'IDbD/KA
HHRLaD
 Q4!A$;'!$R+EB SyR3Y/GGB'
A 2w"GT"X-?#@AXdG&q)eAq$&'4yqVOBwwu~v{#++-$	0B0J0J0LL(,M P 88B</222r.   c                     t        t        t        |                   }t        |dk        }t        t	        j
                  |ddg      d      }t        |      }|dz   }||fS )zDReturn the indices between which we should search for maxima of `y`.r   r   r1   )r
   r   r   r   r   r3   rN   )r=   dsdr>   rV   rW   s        r,   rL   rL      sZ    
tDG}
C#'"G399Wq!f-r2D&t,D1HE;r.   c                 2   t        t        t        |                  }t        |dk(        dz   }| j                  |      }|j                  |      }t        |      }|j                  |      }|dk(  }t        |dd |dd z        }|j                  |      }	|j                  |dz         dz   }
| j                  |	      | j                  |
      z   }|dz  }|j                  |	dz         }t	        ||f      } t	        ||f      }t        |       }| |   ||   fS )z>Return the maxima we can see in the series y as simple points.r   r1   Nr   g       @)r
   r   r   r6   r	   r   )xr=   rY   r>   peak_xpeak_ydsd2
minus_onesplateau_indicesplateau_left_indicesplateau_right_indices	plateau_x	plateau_ys                r,   rK   rK      s   
tDG}
C #)$q(GVVG_FVVG_F
 #G88GDJ!*Sb/JqrN"BCO"<<8#LL1)<=A+,qvv6K/LLII+a/0IVY'(AVY'(AajGW:qz!!r.   c                 \    t        |       s| S t        |       dk7  }t        |dg      }| |   S )Nr   T)r5   r
   r   )arA   s     r,   rN   rN      s2    q67a<D$DT7Nr.   c                 P    t        | j                  t              rt        | _        | S rD   )r   dtyper   r   )r=   s    r,   r8   r8      s     !''5!Hr.   )__doc__
__future__r   r   numpyr   r   r   r   r	   r
   r   r   r   r   r   r   r   	constantsr   EPSILONrM   r-   r!   rH   rG   rL   rK   rN   r8   rE   r.   r,   <module>ro      s{    7 /H H H H 
%-	3:  3H"5H 25u" ) 25u" V3p"6r.   