
dRc           @   sI  d  d l  m Z d  d l Z d Z d Z d Z d Z d Z d Z d	 Z	 d	 Z
 d
 Z e	 e
 Z e e Z e e	 Z d Z d Z d Z d Z d Z e Z d e f d     YZ d   Z d   Z d   Z d   Z d   Z d d  Z e d d  Z d   Z d   Z  e d  Z! e d  Z" e d  Z# e d  Z$ e d   Z% e d!  Z& e d"  Z' e d#  Z( e d$  Z) e d%  Z* e d&  Z+ e d'  Z, e d(  Z- e d)  Z. e d*  Z/ d+   Z0 d,   Z1 d-   Z2 e3 e e e d.  Z4 e3 e e e d/  Z5 e e e d0  Z6 e3 e d1  Z7 e3 e d2  Z8 e d3  Z9 e d4  Z: d S(5   i(   t   floorNim  in  g     v@gk8v@g@i*  i<   i   i	   i   i    iY  iYh% t   EonErrorc           B   s   e  Z d    Z d   Z RS(   c         C   s   | |  _  d  S(   N(   t   message(   t   selfR   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   __init__   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   __str__   s    (   t   __name__t
   __module__R   R   (    (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyR      s   	c          C   s   d d  l  }  |  j d  j j S(   Nii   (   t   syst	   _getframet   f_codet   co_name(   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   whoami%   s    c         C   s   t  |  t  p t  |  t  S(   N(   t
   isinstancet   intt   long(   t   var(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt
   is_integer)   s    c         C   s   t  |  t  S(   N(   R   t   float(   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   is_float,   s    c         C   s   t  |   p t |   S(   N(   R   R   (   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt	   is_number/   s    c         C   s   t  |  t  S(   N(   R   t   str(   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt	   is_string2   s    i
   c         C   s   t  |   r y( d |  k r' t |   n	 t |   } Wq t j d | |   } x7 | r | d k r | d 8} t j d | |   } qS W| s t t   d |    n  | j d  }  d |  k r t |   n	 t |   } q Xn |  } | S(   sN  
	# NAME:
	#	best_guess_for_number
	# PURPOSE:
	#	Tries to extract a valid float or integer number from
	#	a string. If a number (float or integer) is specified
	#	then the input is returned unmodified.
	# CALLING SEQUENCE:
	#	value = best_guess_for_number( var, n=10 )
	# INPUTS:
	#	var         string containing single number
	#				representing time.
	# OPTIONAL INPUTS:
	#	n=10        maximum number of digits before the decimal
	#	            point
	# OUTPUTS:
	#	value       number extracted from 'var' as float or
	#	            integer number.
	# PROCEDURE:
	#	- If the input is already a float or integer than the
	#	  input value is returned.
	#	- For a string the following steps are done:
	#	  1. Try to convert to number using 'float' (if the input
	#	     contains a dot, or 'long' if not.
	#	  2. If this fails, then extract the number with the longest
	#	     number of leading digits (but no more then 'n')
	#	  3. If no number is found, raise error
	#	  4. If number is found convert to float number.
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	t   .s   \d{%s,}(\.\d+)?i   s&   , no good number found in string, '%s'i    (   R   R   R   t   ret   searchR   R   t   group(   R   t   nt   valuet   m(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   best_guess_for_number5   s    !(
+c         C   s  t  |   ruy( d |  k r' t |   n	 t |   } Wn t j d | |   } x7 | r | d k r | d 8} t j d | |   } qS W| s t t   d |    n  | j d  }  d |  k r t |   n	 t |   } n X|  d d k r|  d d !}  n  d |  k r`|  j d  } t |  d | ! t |  | d  d | t	 |   | d f } qt |   d f } n |  d f } | S(   NR   s   \d{%s,}(\.\d+)?i   s&   , no good number found in string, '%s'i    ii
   (
   R   R   R   R   R   R   R   R   t   indext   len(   R   t   exponentR   R   R   t   p(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   disassemble_numberm   s(    (
(Dc         C   s5   d |  d } | d d d d d d d	 d
 g | f S(   s  
	# NAME:
	#	weekday
	# PURPOSE:
	#	Gives the weekday for the time specified as the number
	#	of days since 2000/01/01 00:00:00 (Gregorian)
	# CALLING SEQUENCE:
	#	day = weekday( days )
	# INPUTS:
	#	days        integer scalar
	#	                days since 2000/01/01 00:00:00
	# OUTPUTS:
	#	(day,name)  tuple of integer and char
	#	                day is day of week (Monday=1, Sunday=7)
	#	                name is full name of weekday
	# PROCEDURE:
	#	2000/01/01 in the Gregorian calender was a Saturday
	#	day of week 6).
	#	Note that 2000/01/01 in the Julian calendar corresponds
	#	to 2000/01/14 in the Gregorian calendar, i.e. a Friday
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i   i   i   t   Mondayt   Tuesdayt	   Wednesdayt   Thursdayt   Fridayt   Saturdayt   Sunday(    (   t   dayst   w(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   weekday   s    c         C   s>  d d d d d d d d d	 d
 d d g } t  |  r | j   st t |  d k r | d d k r | d j   r t |  } n  t |  r t | d d  \ } } | |  7} nt g  | D] } | d d !j   ^ q } | d d !j   } | | k rt t   d |   n  |  } | j	 |  } | | d | | f S(   st  
	# NAME:
	#	convert_month
	# PURPOSE:
	#	Converts integer month to character month and v.v.
	# CATEGORY:
	#	general/python
	# CALLING SEQUENCE:
	#	(yr, imonth, cmonth) = convert_month( yr, month )
	# INPUTS:
	#	yr      scalar; integer
	#	            specifies the year
	#	month   scalar; integer or string
	#	            if string then the first 3 characters must
	#	            match one of JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,
	#	            SEP,OCT,NOV,DE (case insensitive).
	# OUTPUTS:
	#	(yr, imonth, cmonth)
	#	        tuple with:
	#	        yr      integer year
	#	        imonth  integer month in [1..12]
	# 	        cmonth  string with full name of month
	# SIDE EFFECT:
	#	If an invalid string is specified for the month,
	#	then an error exception is raised.
	# PROCEDURE:
	#	For numeric input of the month values outside the range 1-12
	#	are mapped back into this range, and 'yr' is updated
	#	accordingly. e.g. if month=14 and yr=2003 on input, then
	#	imonth=2 and yr=2004 is returned.
	#
	#	For character input of the month the first three characters
	#	are used to identify the integer month (case insensitive).
	#	If no match is found an exception is raised
	#	(see SIDE EFFECTS)
	#
	#	The character representation is the full name of the month
	#	with the first char in uppercase (e.g. January)
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	t   Januaryt   Februaryt   Marcht   Aprilt   Mayt   Junet   Julyt   Augustt	   Septembert   Octobert   Novembert   Decemberi   i    t   -t   +i   i   s)   , invalid character input for month, '%s'(   R:   R;   (
   R   t   isdigitR    R   R   t   divmodt   upperR   R   R   (   t   yrt   montht   cmonthst   iyt   imt   cmt   months(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   convert_month   s    **J&c         C   s4   |  d d k o3 | p3 |  d d k p3 |  d d k S(   s  
	# NAME:
	#	isleapyear
	# PURPOSE:
	#	Check whether specified year is a leap year
	# CALLING SEQUENCE:
	#	leap = isleapyear(yr [, [julian=]True/False])
	# INPUTS:
	#	yr		scalar, integer
	#				year
	# OPTIONAL INPUT PARAMETERS:
	#	julian	boolean, default: False
	#				by default the Gregorian calendar is assumed.
	#				if julian=True the Julian calendar is assumed
	# PROCEDURE:
	#	In the Julian calendar every year that is a multiple of four is a
	#	leap year. In the Gregorian calendar centurion years (divisible
	#	by 100) are not leap years, unless they are also divisible by 400.
	#	So 1700, 1800, 1900 were not leap yeass, but 1600 and 2000 were.
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu
	i   i    id   i  (    (   R?   t   julian(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   is_leap_year   s    c         C   sU   t  |  |  \ }  } } d d t |  |  d d d d d d d d d d g | d S(   s  
	# NAME:
	#	days_in_month
	# PURPOSE:
	#	Gives the number of days in a given month
	# CALLING SEQUENCE:
	#	days = days_in_month(yr, month [, [julian=]Trus/False])
	# INPUTS:
	#	yr          integer scalar
	#	                year
	#	month       integer or character scalar
	#	                month
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	days        integer scalar
	#	                number of days in month
	# CALLS:
	#	convert_month, is_leap_year
	# PROCEDURE:
	#	Trivial
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i   i   i   i   (   RF   RH   (   R?   R@   RG   RC   RD   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   days_in_month  s    c         C   sq   t  |  |  \ }  } } | d 8} | d t | d d g  t | d d d g  | d k d t |  |  S(   s  
	# NAME:
	#	days_in_past_months
	# PURPOSE:
	#	Gives the number of days from the start of the year
	#	upto, but excluding, the specified month
	# CALLING SEQUENCE:
	#	days = days_in_past_months(yr, month [, [julian=]Trus/False])
	# INPUTS:
	#	yr          integer scalar
	#	                year
	#	month       integer or character scalar
	#	                month
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	days        integer scalar
	#	                number of days upto and excluding (yr, month)
	# CALLS:
	#	convert_month, is_leap_year
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i   i   i   i   i   i    (   RF   t   mint   maxRH   (   R?   R@   RG   RC   RD   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   days_in_past_months%  s    
c         C   s   t  |  |  r t St S(   sj  
	# NAME:
	#	days_in_year
	# PURPOSE:
	#	Gives the number of days in the specified year
	# CALLING SEQUENCE:
	#	days = days_in_year(yr [, [julian=]Trus/False])
	# INPUTS:
	#	yr          integer scalar
	#	                year
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	days        integer scalar
	#	                number of days in year
	# CALLS:
	#	is_leap_year
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	(   RH   t   g_DAYS_PER_LEAP_YEARt   g_DAYS_PER_NORMAL_YEAR(   R?   RG   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   days_in_yearD  s    c         C   s^   |  } | d k } | d k  } | | 8} d | | } t  |  | d | | | d | d S(   s^  
	# NAME:
	#	days_in_past_years
	# PURPOSE:
	#	Number of days from 2000/01/01 to 2000+yr/01/01
	# INPUTS:
	#	yr          integer scalar
	#	                years since 2000/01/01
	#	                (i.e. the full year, minus 2000)
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	days        integer scalar
	#	                number of days since 2000/01/01
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i    i   i   id   i  (   RN   (   R?   RG   t   At   Ct   Dt   B(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   days_in_past_years]  s    
c         C   s   t  |  |  \ }  } } xB | d k  r\ t  |  | d  \ }  } } | t |  | |  7} q Wt |  | |  } xH | | k r | | 8} t  |  | d  \ }  } } t |  | |  } qr W|  | | | f S(   s  
	# NAME:
	#	fix_yr_month_day
	# PURPOSE:
	#	Checks for out of range values for month and/or day,
	#	and maps them back to valid values
	# CaLLING SEQUENCE:
	#	(yr,month,day) = fix_yr_month_day(yr,month,day)
	# INPUTS:
	#	yr          integer scalar
	#	month       integer or char month
	#	day         day of month
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	(yr, month, cmonth, day)
	#	            tuple
	#	                year, month and day of month, mapped back to
	#	                valid values.
	#	                Output month is of same type as input month
	#	                (integer or char)
	# CALLS:
	#	convert_month, days_in_month, is_integer
	# PROCEDURE:
	#	Adjustments are made, subtracting/adding one month
	#	at a time until values for month and day of month
	#	are valid
	# MODIFICATION PROCEDURE:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i   (   RF   RI   (   R?   R@   t   dayRG   RC   RD   R+   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   fix_yr_month_day|  s    #
c         C   s   x- | d k  r/ |  d 8}  | t  |  |  7} q Wt  |  |  } x3 | | k rt | | 8} |  d 7}  t  |  |  } qB W|  | f S(   s  
	# NAME:
	#	fix_yr_doy
	# PURPOSE:
	#	Checks for out of range values for day of year,
	#	and maps it back to valid values
	# INPUTS:
	#	yr          integer scalar; year
	#	doy         integer scalar; day of year
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	(yr, doy)   tuple
	#	                year, doy, mapped back to valid values.
	# CALLS:
	#	convert_month, days_in_year
	# PROCEDURE:
	#	Adjustments are made, subtracting/adding one year
	#	at a time until value for day of year is valid
	# MODIFICATION PROCEDURE:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i   (   RO   (   R?   t   doyRG   R+   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt
   fix_yr_doy  s    


c   
      C   s   d | } | t  } t |  d  \ } }	 t |  }  | |	 t 7} | | t | t | | 7} t | |  \ } }	 |  t |  7}  t |	  r |	 n! |	 t |	  k r t |	  n |	 } |  | f S(   s)  
	# NAME:
	#	fix_ds
	# PURPOSE:
	#	Reduces time-of-day quantifiers day, hour, minute second
	#	and chipsec to day and chipsec
	# INPUTS:
	#	day, hour, minute, second, chipsec
	#	            scalar; integer or float
	# OPTIONAL INPUT PARAMETERS:
	#	exponent    scalar; integer; default: g_EXPONENT
	# OUTPUTS:
	#	day, chipsec
	#	            scalars; day will be integer, chipsec is integer or
	#	            float in range [0..10^exponent-1]
	# PROCEDURE:
	#	Input hour, minute, second and chipsec can be "out of range",
	#	i.e. hour can be negative or >= 24, minute and second can
	#	be negative or >= 60, chipsec can be negative or
	#	>= 10^exponent.
	#	Adjustments are made, bringing chipsec into its
	#	customary range.
	# MODIFICATION PROCEDURE:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i
   i   (   t   g_SECONDS_PER_DAYR=   R   t   g_HOURS_PER_DAYt   g_MINUTES_PER_HOURt   g_SECONDS_PER_MINUTER   (
   RU   t   hourt   minutet   secondt   chipsecR!   t   tictacs_per_secondt   tictacs_per_dayt   it   f(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   fix_ds  s    

6c   	      C   s  d | } t  |  d  \ } } t |  }  | | t 7} t  | d  \ } } t |  } | | t 7} t  | d  \ } } t |  } | | t 7} t  | d  \ } } t |  } | | | 7} t  | |  \ } } | t |  7} t |  r | n! | t |  k rt |  n | } t  | t  \ } } | | 7} t  | t  \ } } | | 7} t  | t  \ } } |  | 7}  |  | | | | f S(   s  
	# NAME:
	#	fix_dhms
	# PURPOSE:
	#	Normalizes time-of-day quantifiers day, hour, minute
	#	second and chipsec
	# INPUTS:
	#	day, hour, minute, second, chipsec
	#	            scalars; integer or float
	# OPTIONAL INPUT PARAMETERS:
	#	exponent    scalar; integer; default: g_EXPONENT
	# OUTPUTS:
	#	day, hour, minute, second, chipsec
	#	            scalar; day, hour, minute, second are integer
	#	            in the customary range for each unit, i.e.
	#	            hour in [0:23], minute and second in [0..59].
	#	            chipsec will be integer or float in the range
	#	            [0..10^exponent-1]
	# PROCEDURE:
	#	Input hour, minute, second and chipsec can be "out of range",
	#	i.e. hour can be negative or >= 24, minute and second can
	#	be negative or >= 60, chipsec can be negative or
	#	>= 10^exponent.
	#	Adjustments are made, subtracting/adding as appropriate
	#	to bring each unit in is customary range.
	# MODIFICATION PROCEDURE:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i
   i   (   R=   R   RZ   R[   R\   R   (	   RU   R]   R^   R_   R`   R!   Ra   Rc   Rd   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   fix_dhms  s.    
6


c         C   s+   |  | t  7}  | t |  |  } |  | f S(   s  
	# NAME:
	#	underestimate_years
	# PURPOSE:
	#	Underestimate the number of whole years in time
	#	specified as days since 2000/01/01 00:00:00
	# CALLING SEQUENCE:
	#	(yr,doy) = underestimate_years( yr, doy, days [, julian=True/False )
	# INPUTS:
	#	yr      integer scalar;
	#	            years since 2000/01/01
	#	doy	integer scalar;
	#	            remaining part of days; may still contain whole year
	#	days    integer scalar
	#	            days since 2000/01/01
	# OPTIONAL INPUT PARAMETERS:
	#	julian  boolean, default: False
	#	            by default the Gregorian calendar is assumed.
	#	            if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	(yr,doy)
	#	        tuple of two integers
	#	            improved estimates for yr and doy
	#	            yr: years since 2000/01/01
	#	            doy: remaining part of days
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	(   RM   RT   (   R?   RW   R+   RG   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   underestimate_years:  s     c         C   s6   | | d d 7} | t  |  | d |  } | | f S(   sf  
	# NAME:
	#	underestimate_months
	# PURPOSE:
	#	Underestimate number of whole months in specified
	#	day of year
	# CALLING SEQUENCE:
	#	(month,day) = underestimate_months( yr, month, day, doy, julian )
	# INPUTS:
	#	yr      integer scalar;
	#	            year
	#	month   integer scalar
	#	            month
	#	day     integer scalar
	#	            day of month; may still contain whole month
	#	doy     integer doy
	#	            day of year
	# OPTIONAL INPUT PARAMETERS:
	#	julian  boolean, default: False
	#	            by default the Gregorian calendar is assumed.
	#	            if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	(month, day)
	#	        tuple of two integers
	#	            improved estimates for month and day of month
	# CALLS:
	#	days_in_past_months
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i   i   (   RL   (   R?   R@   RU   RW   RG   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   underestimate_monthsc  s    !c         C   s   t  |   r d } n! t |  d  \ }  } t |   }  d } |  } t | | |  |  \ } } x1 t |  t k r t | | |  |  \ } } q` W| d 7} | d 7} t | | |  \ } } | d k r | | 7} n  | | f S(   s  
	# NAME:
	#	year_doy
	# PURPOSE:
	#	Get year and day of year from days elapsed since
	#	2000/01/01 00:00:00 (Greg)
	# CALLING SEQUENCE:
	#	(yr,doy) = year_doy( days [, julian=True/False] )
	# INPUTS:
	#	days    scalar; any numerical type
	#	            days since 2000/01/01 00:00:00 (Greg)
	#	            (can include time of day as fraction)
	# OPTIONAL INPUT PARAMETERS:
	#	julian  boolean, default: False
	#	            by default the Gregorian calendar is assumed.
	#	            if julian=True the Julian calendar is assumed
	# CALLS:
	#	underestimate_years, fix_yr_doy
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i    i   i  (   R   R=   R   Rg   t   absRN   RX   (   R+   RG   t   fractionR?   RW   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   year_doy  s    	

c         C   s4   t  |  d |  } | d k r0 | | d 7} n  | S(   s*  
	# NAME:
	#	days_since_2000
	# PURPOSE:
	#	Get days since 2000/01/01 00:00:00 from year and day of year
	# CALLING SEQUENCE:
	#	days = days_since_2000( yr, doy [, julian=True/False] )
	# INPUTS:
	#	yr      integer scalar
	#	            year
	#	doy     scalar of any numerical type
	#	            day of year (incl. fraction for time of day)
	# OPTIONAL INPUT PARAMETERS:
	#	julian  boolean, default: False
	#	            by default the Gregorian calendar is assumed.
	#	            if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	days    scalar; same numerical type as doy
	#	            days since 2000/01/01 00:00:00
	#	            (incl. fraction for time of day)
	# CALLS:
	#	underestimate_years, fix_yr_doy
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i  i   (   RT   (   R?   RW   RG   R+   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   days_since_2000  s    c         C   s   t  |  r d } n! t | d  \ } } t |  } t |  | |  \ }  } d } | } t |  | | | |  \ } } x. | d k r t |  | | | |  \ } } q{ W| d 7} t |  | | |  \ }  } } } | d k r | | 7} n  |  | | | f S(   s_  
	# NAME:
	#	doy2month_day
	# PURPOSE:
	#	Get yr, month and day of month from yr and day of year
	# CALLING SEQUENCE:
	#	(yr,month,day) = doy2month_day( yr, doy [, julian=True/False] )
	# INPUTS:
	#	yr      integer scalar
	#	            year
	#	doy     scalar of any numerical type
	#	            day of year (incl. fraction for time of day)
	# OPTIONAL INPUT PARAMETERS:
	#	julian  boolean, default: False
	#	            by default the Gregorian calendar is assumed.
	#	            if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	(yr,month,cmonth,day)
	#	        tuple with year, month and day of month
	#	            yr and month are integers, day is of same
	#	            numerical type as input doy
	# CALLS:
	#	fix_yr_doy, under_estimate_months, days_in_month
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	i    i   i   (   R   R=   R   RX   Rh   RV   (   R?   RW   RG   Rj   RC   RU   RD   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   doy2month_day  s    !	"
!c         C   s   t  |  | | d | \ }  } } } t |  r9 d } n! t | d  \ } } t |  } t |  | |  | } t |  | |  \ }  } | d k r | | 7} n  |  | f S(   s  
	# NAME:
	#	month_day2doy
	# PURPOSE:
	#	Get yr and day of year from yr, month and day of month
	# CALLING SEQUENCE:
	#	(yr,doy) = month_day2doy( yr, month, day [, julian=True/False] )
	# INPUTS:
	#	yr          integer scalar
	#	                year
	#	month       scalar of type integer or char
	#	day         scalar of any numerical type
	#	                day of month (incl. fraction for time of day)
	# OPTIONAL INPUT PARAMETERS:
	#	julian      boolean, default: False
	#	                by default the Gregorian calendar is assumed.
	#	                if julian=True the Julian calendar is assumed
	# OUTPUTS:
	#	(yr,doy)    tuple with year and day of year
	#	                yr is integers, doy is of same
	#	                numerical type as input day
	# CALLS:
	#	convert_month, days_in_past_months, fix_yr_doy
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	RG   i    i   (   RV   R   R=   R   RL   RX   (   R?   R@   RU   RG   RC   RD   Rj   RW   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   month_day2doy%  s    $	c         C   s#   t  t |  | d t t d t S(   s  
	# NAME:
	#	gregorian_to_julian
	# PURPOSE:
	#	Converts (yr,doy) in Gregorian calendary to (yr,doy) in
	#	Julian calendar
	# CALLING SEQUENCE:
	#	(yr,doy) = gregorian_to_julian( yr, doy )
	# INPUTS:
	#	yr          integer scalar
	#	                year
	#	doy         scalar of any numerical type
	#	                day of year (incl. fraction for time of day)
	# OUTPUTS:
	#	(yr,doy)    tuple with year and day of year in Julian calendar
	#	                yr is integer, doy is of same
	#	                numerical type as input day
	# CALLS:
	#	year_doy, days_since_2000
	# PROCEDURE:
	#	2000/01/01 (Julian) = 2000/01/14 (Gregorian) (13 day difference)
	#	1582/10/05 (Julian) = 1582/10/15 (Gregorian) (10 day difference)
	#	3-day difference due to 1700, 1800, 1900 which are Julian leap years
	#	but non-leap Gregrorian years
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	RG   (   Rk   Rl   t   Falset   g_GREG2JULIANt   True(   R?   RW   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   gregorian_to_julianR  s    c         C   s#   t  t |  | d t t d t S(   s  
	# NAME:
	#	julian_to_gregorian
	# PURPOSE:
	#	Converts (yr,doy) in Julian calendary to (yr,doy) in
	#	Gregorian calendar
	# CALLING SEQUENCE:
	#	(yr,doy) = julian_to_gregorian( yr, doy )
	# INPUTS:
	#	yr          integer scalar
	#	                year
	#	doy         scalar of any numerical type
	#	                day of year (incl. fraction for time of day)
	# OUTPUTS:
	#	(yr,doy)    tuple with year and day of year in Gregorian calendar
	#	                yr is integer, doy is of same
	#	                numerical type as input day
	# CALLS:
	#	year_doy, days_since_2000
	# PROCEDURE:
	#	2000/01/01 (Julian) = 2000/01/14 (Gregorian) (13 day difference)
	#	1582/10/05 (Julian) = 1582/10/15 (Gregorian) (10 day difference)
	#	3-day difference due to 1700, 1800, 1900 which are Julian leap years
	#	but non-leap Gregrorian years
	# MODIFICATION HISTORY:
	#	JUL-2009, Paul Hick (UCSD/CAIDA/CASS; pphick@ucsd.edu)
	RG   (   Rk   Rl   Rq   Rp   Ro   (   R?   RW   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   julian_to_gregorianp  s    c         C   sH  i9 d] d 6d^ d 6d_ d 6d` d 6da d
 6db d 6dc d 6dd d 6de d 6df d 6dg d 6dh d 6di d 6dj d 6dk d 6dl d  6dm d" 6dn d$ 6do d& 6dp d( 6dq d* 6dr d, 6ds d. 6dt d0 6du d2 6dv d3 6dw d4 6dx d5 6dy d6 6dz d7 6d{ d8 6d| d9 6d} d: 6d~ d; 6d d< 6d d> 6d d? 6d d@ 6d dB 6d dC 6d dD 6d dE 6d dF 6d dG 6d dH 6d dI 6d dJ 6d dK 6d dL 6d dM 6d dN 6d dO 6d dP 6d dQ 6d dR 6d dS 6d dT 6} |  d  k st |   d k rd S|  | k r| |  S|  d d !d4 k rd n d } |  | dU k r| d 7} d } n) |  | dV k r0| d 7} d } n d } t |  |  d	 k  rjt t   dW |  dX   n  |  | j   st t   dY |  dX   n  t |  | | d ! } t |  | d | d	 ! } | d k  s| dA k r t t   dZ |  dX   n  | d k  s| d[ k r6t t   d\ |  dX   n  | | | | f S(   Ni    t   Zi   RP   i   RS   i   RQ   i   RR   i   t   Ei   t   Fi   t   Gi   t   Hi	   t   Ii
   t   Ki   t   Li   t   Mit   Nit   Oit   Pit   Qit   Rit   Sit   Tit   Uit   Vit   Wit   Xit   Yt   UTt   UTCt   GMTt   WETt   WTt   CETt   CESTt   EETt   EESTt   EATi   t   AFTt   JSTt   NZSTi   t   NZDTt   AKSTt   AKDTt   PSTt   PDTt   MSTt   MDTt   CSTt   CDTt   ESTt   EDTt   ASTt   ADTt   PMSTt   PMDTt   AZOTt   AZOSTt   EGTt   EGSTR;   R:   s-   , incomplete timezone (expected [+/-]hhmm), 't   's   , unrecognized timezone, 's   , invalid hours in timezone, 'i;   s    , invalid minutes in timezone, '(   i    i    (   i   i    (   i   i    (   i   i    (   i   i    (   i   i    (   i   i    (   i   i    (   i   i    (   i	   i    (   i
   i    (   i   i    (   i   i    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   i    i    (   i    i    (   i    i    (   i    i    (   i    i    (   i   i    (   i   i    (   i   i    (   i   i    (   i   i    (   i   i   (   i	   i    (   i   i    (   i   i    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   ii    (   i    i    (   ii    (   i    i    (   i    i    (   t   NoneR    R   R   R<   R   (   t   tz_fieldt   tzR"   t   signt   tz_hourt	   tz_minute(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   disassemble_timezone  s    

	
	c         C   s  d d l  m } | r |   n d4 } | d  k r| } d  } d  }	 d  }
 d  } d  } d  } d  } d  } d | k r | j d  } d } x) | | | | !d	 | k r | d 7} q W| d 8} t |  | | | ! } | d | !d
 | | | | } n  d | k rq| j d  } t |  | | d ! } d | | d k d | | d k } | d | !d | | d } n  | d  k r| d } t } n t } t } t } d | k r| j d  } t |  | | d ! }	 | d | !d | | d } t | |	 |  \ } }
 } } t } t } n d | j   k r| j   j d  } |  | | d !}
 | d | !d | | d } t } t } t	 | |
  \ } }
 } nv d | k r| j d  } t |  | | d ! }
 | d | !d | | d } t } t } t	 | |
  \ } }
 } n  | sd | k r| r5| d }
 n  t } | j d  } t |  | | d ! } | d | !d | | d } n | rd } n  t } t
 | |
 |  | }	 n  | r| d }	 t | |	 |  \ } }
 } } n  d | k r8| j d  } t |  | | d ! } | d | !d | | d } t } n | rK| d } n  d | k r| j d  } t |  | | d ! } | d | !d | | d } t } n | r| d } n  d | k r| j d  } t |  | | d ! } t |   } d } xf | d | | k  rbd |  | d | k rb|  | d | d k rbd d | | k rb| d 7} qW| d 8} | d k rt t t |  | d | d | ! d | |   } n  | d | !d
 d | | | d | } t } n | r| d } d } n  | sp|	 d  k rd }	 n  | d  k r.d } n  | d  k rCd } n  | d  k rXd } n  | d  k rpd } qpn  d | k r| j d  } |  | j d
  d } | d | !d
 t |  | | d } t |  \ } } | | 8} | | 8} n  | | k rJt t   d  | d!   qJn+t j d" |   rtd# |  k rIt |   n	 t |   } d$ | d  d  d  d  d  d  d  f	 Sd% d& d' d( d) d* d+ d, d- d. d/ d0 g } xq t t |   D]] } | | |  j   k r|  j   j | |  } |  d | !d1 | d |  | d }  qqWd2 j g  t |   D]( } d
 | g | d k oI| d k ^ q% j   j   }  t |   } d } | | k r| d } | d }	 | d } | d } | d } d } nm t |  |  } t |  |  d k r
	d | | d k d | | d k } n  d }	 d } d } d } d } | d 7} | | k r	t |  |  d k r	t |  |  }	 t | |	 |  \ } }
 } } q	t |  |  }
 | d 7} | | k r	t |  |  n d } t	 | |
  \ } }
 } t
 | |
 |  | }	 n  | d 7} | | k r
t |  |  } n  | d 7} | | k rD
t |  |  } n  | d 7} | | k rm
t |  |  } n  | d 7} | | k rJ|  | d  d  d  |  | <x@ t |  |  d k r
|  | d d k r
|  | d |  | <q
W|  | d2 k r
d } qJ|  | d  d  d  |  | <t t t |  |  d | t |  |    } n  t |	 | | | | d3 | \ }	 } } } } t | |	 |  \ } }	 t | |	 |  \ } }
 } } | |	 |
 | | | | | | f	 S(5   Ni(   t   gmtimei  i   i    i   t   YYYYi   R   t    t   YYi   il  i2   s     t   DOYi   s      t   MONt   MMt   DDi   t   hht   mmt   sst   0t   9s   ss.t   di
   t   TZs   , unrecognized format, 'R   s   ^\d{9,10}.*?$R   t   posixt   JANt   FEBt   MARt   APRt   MAYt   JUNt   JULt   AUGt   SEPt   OCTt   NOVt   DECs   %02dt    R!   (	   i  i   i   i    i    i    i   i   i    (   t   timeR   R   t   findR   Rq   Ro   Rm   R>   RF   RL   R    R   t   roundt   splitR   R   R   R   t   matchR   t   ranget   joint   listt   stripRf   RX   (   t   ctimet   formatR!   RG   t   nowR   t   ttt   original_formatt   yearRW   R@   RU   R]   R^   R_   R`   R"   R   t   fill_nowt	   have_ydoyt   have_ymdt   cmontht   lRc   R   R   R   R   RE   t   x(    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   split_date_format  s\   
$& 
		 	
!		[
:)	
	 	 	 	 	 '
!$*,P




	)
!
"



-	5-c   #   
   C   sO  d d  l  } | j d  } d } g  } t   }	 x | j |  D]r }
 | d 7} |
 j d  } d | k r@ | j |
 j d  | j d  f  | j d  \ } } | |	 | <q@ q@ W| j	   x7 | D]/ } | d | d !| | d | d d } q W| j
 | |   }
 |
 d  k r>t t   d |  d | d   n  d  } d  } d  } d  } d  } d  } d  } d  } d  } d  } d  } xK|	 j   D]=} |
 j |	 |  } | d	 k rd
 | k rt |  n	 t |  } q| d k r2d
 | k rt |  n	 t |  } d | | d k d | | d k } q| d k red
 | k rVt |  n	 t |  } q| d k rz| } q| d k rt |  } q| d k rd
 | k rt |  n	 t |  } q| d k rd
 | k rt |  n	 t |  } q| d k r.d
 | k rt |  n	 t |  } q| d k rC| } q| d k rX| } q| d k rd
 | k r|t |  n	 t |  } d | d  d  d  d  d  d  d  f	 St t   d | d | d   qW| d  k rn  d d l m } | r|   n d  } | d  k r!| d } t } n t } t } t } | d  k rlt | | |  \ } } } } t } t } n3 | d  k rt | |  \ } } } t } t } n  | r| d  k r| r| d } n  t | | |  | } t } t } n  | r| d } n  | d  k rt } n | r%| d } n  | d  k r:t } n | rM| d } n  | d  k rd
 | k r| } | j d
  } | d k rt | d | ! n d } | | d } t |  }  |  d k rt t t |  d | |    n d } n t |  } d } t } n | r&| d } d } n  | s| d  k rAd } n  | d  k rVd } n  | d  k rkd } n  | d  k rd } n  | d  k rd } qn  | d  k rt |  \ }! }" | |! 8} | |" 8} n  t | | | | | d | \ } } } } } t | | |  \ } } t | | |  \ } } } } | | | | | | | | | f	 S(!   Nis	   \((.*?)\)i    i   t   =s   , 's   ' does not match 'R   R   R   R   il  i2   i  R   R   R   R   R   R   R   R   R   s(   ' contains unrecognized time specifier '(   R   i   i   i   i   i
   R!   (	   i  i   i   i    i    i    i   i   i    (   R   t   compilet   dictt   finditerR   t   appendt   startR   R   t   reverseR   R   R   R   t   keysR   R   R   R   Rq   Ro   Rm   RF   RL   R    R   R   R   Rf   RX   (#   R   t   regexR!   RG   R   R   t   rRc   t   to_be_removedt   matchesR   t   gt   keyR   R   RW   R@   R   RU   R]   R^   R_   R`   R   t   timezoneR   R   R   R   R   t   csecR"   R   R   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   split_date_regex  s    	
%
-&'$)'	'''		$'
						%9	
	 	 	 	 	 
-c         C   sn  d d l  m } | r |   n d } t |  } |  d  k rP | d }  t } n0 t } |  d d k r t t   d |    n  t } t } | d  k r t |  | |
  \ }  } } } t } t } n3 | d  k r t } t } t	 |  |  \ }  } } n  | r| d  k r| r| d } n" | d  k r@t t   d   n  t
 |  rw| d d k rwt t   d	 |   n  t	 |  |  \ }  } } t } t } t |  | |
  | } n  | r| d
 } n  | d  k rt } n | r| d } n  | d  k rt } n | r| d } n  | d  k r,t } n | rE| d } d } n  | s| d  k r`d } n  | d  k rud } n  | d  k rd } n  | d  k rd } n  | d  k rd } qn  | d  k rt |  \ } } | | 8} | | 8} n  t | | | | | d |	 \ } } } } } t |  | |
  \ }  } t |  | |
  \ }  } } } |  | | | | | | | | f	 S(   Ni(   R   i  i   i    i   s$   , 'year' must be an integer; year=%ss,   , must specify 'month' with 'year' and 'day's&   , 'month' must be an integer; month=%si   i   i   R!   (	   i  i   i   i    i    i    i   i   i    (   R   R   R   R   Rq   Ro   R   R   Rm   RF   R   RL   R   Rf   RX   (   R   RW   R@   RU   R]   R^   R_   R`   R   R!   RG   R   R   R   R   R   R   R   R   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   fill_date_fieldsV  s    
					
	 	 	 	 	 
-c         C   s  d } d } d } d } d } | d  k rw| } d }	 x" d |	 d | k rZ |	 d 7}	 q9 W|	 d k r | j d |	  }
 | t |  |
 |
 |	 ! t 7} | d |
 !d |	 | |
 |	 } n  d }	 x" d |	 d | k r |	 d 7}	 q W|	 d k rt| j d |	  }
 | d |  |
 |
 |	 !k r8t |  |
 |
 |	 ! n t |  |
 |
 |	 ! 7} | d |
 !d |	 | |
 |	 } n  d }	 x" d |	 d | k r|	 d 7}	 q}W|	 d k r'| j d |	  }
 d |  |
 |
 |	 !k rt |  |
 |
 |	 ! n t |  |
 |
 |	 ! } | d |
 !d |	 | |
 |	 } n  d }	 x" d |	 d | k rQ|	 d 7}	 q0W|	 d k r| j d |	  }
 d |  |
 |
 |	 !k rt |  |
 |
 |	 ! n t |  |
 |
 |	 ! } | d |
 !d |	 | |
 |	 } n  d }	 x" d	 |	 d | k r|	 d 7}	 qW|	 d k rJ| j d	 |	  }
 t |  |
 |
 |	 ! } t |   } d } xn |
 |	 | | k  rd
 |  |
 |	 | k r|  |
 |	 | d k rd	 |	 d d | | k r| d 7} qPW| d 8} | d k rt t t |  |
 |	 d |
 |	 d | ! d | |   } n  | d |
 !d |	 d | | |
 |	 d | } n  | | k rPt t   d | d   qPnd j	 g  t
 |   D]( } d | g | d
 k o| d k ^ q j   j   }  t |   }	 d } |	 | k rt |  |  } n  | d 7} |	 | k r!t |  |  } n  | d 7} |	 | k rJt |  |  } n  | d 7} |	 | k rst |  |  } n  | d 7} |	 | k rP|  | d  d  d  |  | <x@ t |  |  d k r|  | d d
 k r|  | d |  | <qW|  | d k r d } qP|  | d  d  d  |  | <t t t |  |  d | t |  |    } n  t | | | | | d | \ } } } } } | | | | | f S(   Ni    R   i   R   RR   R   t   hR   t   sR   R   R   i
   s   , unrecognized format, 'R   R   iR!   (   R   R   R   t   g_JULIAN_YEARR   R    R   R   R   R   R   R   R   Rf   (   R   R   R!   RU   R]   R^   R_   R`   R   R   R"   R   Rc   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   split_delta_format  s    $I$E$E$c
B4!P



-	5-c         C   s  d d  l  } | j d  } d } g  } t   } x | j |  D]r } | d 7} | j d  }	 d |	 k r@ | j | j d  |	 j d  f  |	 j d  \ }
 } | | |
 <q@ q@ W| j	   x7 | D]/ } | d | d !| | d | d d } q W| j
 | |   } d } d } d } d } d } x| j   D]}
 | j | |
  } d |
 k rs| t |  t 7} n  d |
 k r| d | k rt |  n	 t |  7} n  d	 |
 k rd | k rt |  n	 t |  } n  d
 |
 k rd | k rt |  n	 t |  } n  d |
 k r7| } d | k r| } | j d  } | d k rbt | d | ! n d } | | d } t |  } | d k rt t t |  d | |   n d } qt |  } d } q7q7Wt | | | | | d | \ } } } } } | | | | | f S(   Nis	   \((.*?)\)i    i   R   R   RR   R   R   R   R   i
   R!   (   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    R   R   Rf   (   R   R   R!   R   R   Rc   R   R   R   R   R   R   RU   R]   R^   R_   R`   R   R"   R   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   split_delta_regex   sX    	
%
-+''%9-c         C   s  |  d  k r d n	 t |   }  | d  k r0 d n	 t |  } | d  k rN d n	 t |  } | d  k rl d n	 t |  } | d  k r d n	 t |  } | d  k r d n	 t |  } |  d  k r | |  t 7} d }  n  t | | | | | d | \ } } } } } | | | | | f S(   Ni    R!   (   R   R   R   Rf   (   R   RU   R]   R^   R_   R`   R!   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   fill_delta_fields]  s    	-c   
      C   s6  t  |  } |  } | j |  } | d | k rV | j | d | d d |  } n  d } x& | d d | | k r | d 7} q_ W| d 8} | d k r | d | !| | | | } nw | d | | }	 | | k  r t |	  }	 n  d | d | !| | | | d t |  d |	 | | | d | f } | S(	   s\  
	#+
	# NAME:
	#	fill_fraction
	# PURPOSE:
	#	eon helper function
	# CALLING SEQUENCE:
	#	str = fill_fraction(format, spec, integer_spec, fractional_spec , exponent])
	# INPUTS:
	#	format             string containing time format
	#	spec
	#	integer_spec
	#	fractional_spec
	#	exponent
	# OUTPUTS:
	#	str
	# PROCEDURE:
	# MODIFICATION HISTORY:
	#-
	s   .d#R   R   i   i    i
   s
   %s%s%s%s%ss   %0(   R    R   t   replaceR   R   (
   R   t   spect   integer_spect   fractional_specR!   t   slent   resultR"   Rc   R`   (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   fill_fractionn  s*    %
 
(;   t   mathR    R   RN   RM   R   t   g_BESSEL_YEARt   g_JD2000_B1900t   g_JD2000_POSIXR\   R[   RZ   t   g_SECONDS_PER_HOURRY   t   g_MINUTES_PER_DAYt
   g_EXPONENTRp   t	   g_NJD_NJDt	   g_NJD_MJDt   g_NJD_JDRo   t   g_EON_DEBUGt	   ExceptionR   R   R   R   R   R   R   R#   R-   RF   RH   RI   RL   RO   RT   RV   RX   Re   Rf   Rg   Rh   Rk   Rl   Rm   Rn   Rr   Rs   R   R   R   R   R   R   R   R   R  (    (    (    s-   /home/soft/smei/ucsd/gen/python/eon_helper.pyt   <module>   sn   


						8"		B1%*>)&>"<-			 %^l=