B
    d             J   @   s	  d Z yddlmZ W n  ek
r4   ddlmZ Y nX ddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlZddlmZ d	d
lmZ d	dlmZ d	dlmZ d	dlmZ dZdej ZejZdddZdd ZG dd deZ e j!Z"e j#Z$G dd deZ%G dd de%Z&G dd deZ'e(ddZ)G dd dej*Z+G d d! d!ej*j,Z-e-e+_,e(d"d#Z.G d$d% d%ej*Z/G d&d' d'ej*j,Z0e0e/_,G d(d) d)ej*Z1G d*d+ d+ej*j,Z2e2e1_,G d,d- d-ej*Z3G d.d/ d/ej*j,Z4e4e3_,e(d0d1Z5G d2d3 d3ej*Z6G d4d5 d5ej*j,Z7e7e6_,e(d6d7Z8G d8d9 d9ej*Z9G d:d; d;ej*j,Z:e:e9_,e(d<d=Z;G d>d? d?ej*Z<G d@dA dAej*j,Z=e=e<_,G dBdC dCej*Z>G dDdE dEej*j,Z?e?e>_,e(dFdGZ@G dHdI dIej*ZAG dJdK dKej*j,ZBeBeA_,e(dLdGZCG dMdN dNej*ZDG dOdP dPej*j,ZEeEeD_,G dQdR dRej*ZFG dSdT dTej*j,ZGeGeF_,e(dUdGZHG dVdW dWej*ZIG dXdY dYej*j,ZJeJeI_,G dZd[ d[ej*ZKG d\d] d]ej*j,ZLeLeK_,e(d^dGZMG d_d` d`ej*ZNG dadb dbej*j,ZOeOeN_,G dcdd ddej*ZPG dedf dfej*j,ZQeQeP_,e(dgdGZRG dhdi diej*ZSG djdk dkej*j,ZTeTeS_,e(dldmZUG dndo doej*ZVG dpdq dqej*j,ZWeWeV_,e(drdmZXG dsdt dtej*ZYG dudv dvej*j,ZZeZeY_,e(dwdmZ[G dxdy dyej*Z\G dzd{ d{ej*j,Z]e]e\_,e(d|d}Z^G d~d dej*Z_G dd dej*j,Z`e`e__,e(ddmZaG dd dej*ZbG dd dej*j,Zceceb_,G dd dej*ZdG dd dej*j,Zeeeed_,G dd dej*ZfG dd dej*j,Zgegef_,G dd dej*ZhG dd dej*j,Zieieh_,e(ddZjG dd dej*ZkG dd dej*j,Zlelek_,e(ddZmG dd dej*ZneG dd dej*j,Zoeoen_,e(ddZpG dd dej*ZqG dd dej*j,Zrereq_,e(ddZsG dd dej*ZtG dd deoZueuet_,e(ddZvG dd dej*ZwG dd dej*j,Zxexew_,e(ddZyG dd dej*ZzG dd dej*j,Z{e{ez_,e(ddZ|G dd dej*Z}G dd dej*j,Z~e~e}_,G dd dej*ZG dd dej*j,Zee_,e(dd¡Ze(ddáZG ddń dej*ZG ddǄ dej*j,Zee_,e(ddGZG ddʄ dej*ZG dd̄ dej*j,Zee_,G dd΄ dej*ZG ddЄ dej*j,Zee_,e(ddҡZG ddԄ dej*ZG ddք dej*j,Zee_,G dd؄ dej*ZG ddڄ dej*j,Zee_,ezjezeKjeKejeePjePe}je}ejee>je>e<je<e9je9ehjehekjekeAjeAe/je/ejeeFjeFeSjeSe1je1e+je+e3je3e6je6eqjeqetjetenjeneYjeYeVjeVebjebe\je\eDjeDeIjeIeNjeNedjedefjefe_je_ejeejeewjewejei%Zdd܄ Zddބ ZdS )a  
LSC Table definitions.  These must be kept synchronized with the official
definitions in the LDAS CVS repository at
http://www.ldas-sw.ligo.caltech.edu/cgi-bin/cvsweb.cgi/ldas/dbms/db2/sql.
Maintenance of the table definitions is left to the conscience of
interested users.
    )total_orderingN)sax)segments)git_version)	iterutils)LIGOTimeGPS   )ligolw)table)types)ilwdz"Kipp Cannon <kipp.cannon@ligo.org>z	git id %sc          
   K   s
  | t jd| j| jif|}|jd }|dk	rxt|D ]J}||jkr`t	
d||jf |tt j|| |j| d q>W n>x<t|j D ]*\}}|tt j|| |d qW |  |tt j|dtjjjtjjjd |S )a  
	Construct a pre-defined LSC table.  The optional columns argument
	is a sequence of the names of the columns the table should be
	constructed with.  If columns = None, then the table is constructed
	with all valid columns (use columns = [] to create a table with no
	columns).

	Example:

	>>> import sys
	>>> tbl = New(ProcessTable, [u"process_id", u"start_time", u"end_time", u"comment"])
	>>> tbl.write(sys.stdout)	# doctest: +NORMALIZE_WHITESPACE
	<Table Name="process:table">
		<Column Name="process:comment" Type="lstring"/>
		<Column Name="process:end_time" Type="int_4s"/>
		<Column Name="process:process_id" Type="ilwd:char"/>
		<Column Name="process:start_time" Type="int_4s"/>
		<Stream Delimiter="," Name="process:table" Type="Local">
		</Stream>
	</Table>
	Namez:%sNz"invalid Column '%s' for Table '%s')r   Type)r   	Delimiterr   )r   Z	xmlreaderZAttributesImpl	TableNameenc	tableNamer   sortedvalidcolumnsr	   ElementErrorZappendChildr
   ColumnitemsZ_end_of_columnsZTableStreamZgetAttributer   defaultr   )clscolumnskwargsnewZ
colnamefmtkeyvalue r   b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/glue/ligolw/lsctables.pyNewH   s    "

.&2r!   c             C   s   t dd | tjjD S )za
	Return True if the document tree below elem contains non-LSC
	tables, otherwise return False.
	c             s   s   | ]}|j tkV  qd S )N)r   TableByName).0tr   r   r    	<genexpr>r   s    z"HasNonLSCTables.<locals>.<genexpr>)anyZgetElementsByTagNamer	   TableZtagName)elemr   r   r    HasNonLSCTablesm   s    r)   c               @   s>   e Zd Zdd Zedd Zedd Zddd	Zd
d ZdS )instrumentspropertyc             C   s
   || _ d S )N)name)selfr+   r   r   r    __init__v   s    zinstrumentsproperty.__init__c                s    dkrdS d kr:t dd  dD }|d |S d krht dd  dD }|d |S    t dkrt d st  fd	dtd
t dD S  rt  gS t  S )a  
		Parse the values stored in the "ifos" and "instruments"
		columns found in many tables.  This function is mostly for
		internal use by the .instruments properties of the
		corresponding row classes.  The mapping from input to
		output is as follows (rules are applied in order):

		input is None --> output is None

		input contains "," --> output is set of strings split on
		"," with leading and trailing whitespace stripped from each
		piece and empty strings removed from the set

		input contains "+" --> output is set of strings split on
		"+" with leading and trailing whitespace stripped from each
		piece and empty strings removed from the set

		else, after stripping input of leading and trailing
		whitespace,

		input has an even length greater than two --> output is set
		of two-character pieces

		input is a non-empty string --> output is a set containing
		input as single value

		else output is an empty set.

		NOTE:  the complexity of this algorithm is a consequence of
		there being several conventions in use for encoding a set
		of instruments into one of these columns;  it has been
		proposed that L.L.W.  documents standardize on the
		comma-delimited variant of the encodings recognized by this
		function, and for this reason the inverse function,
		instrumentsproperty.set(), implements that encoding only.

		NOTE:  to force a string containing an even number of
		characters to be interpreted as a single instrument name
		and not to be be split into two-character pieces, add a ","
		character to the end to force the comma-delimited decoding
		to be used.  instrumentsproperty.set() does this for you.

		Example:

		>>> print(instrumentsproperty.get(None))
		None
		>>> assert instrumentsproperty.get(u"") == set()
		>>> assert instrumentsproperty.get(u"  ,  ,,") == set()
		>>> assert instrumentsproperty.get(u"H1") == {'H1'}
		>>> assert instrumentsproperty.get(u"SWIFT") == {'SWIFT'}
		>>> assert instrumentsproperty.get(u"H1L1") == {'H1', 'L1'}
		>>> assert instrumentsproperty.get(u"H1L1,") == {'H1L1'}
		>>> assert instrumentsproperty.get(u"H1,L1") == {'H1', 'L1'}
		>>> assert instrumentsproperty.get(u"H1+L1") == {'H1', 'L1'}
		N,c             s   s   | ]}|  V  qd S )N)strip)r#   ifor   r   r    r%      s    z*instrumentsproperty.get.<locals>.<genexpr> +c             s   s   | ]}|  V  qd S )N)r/   )r#   r0   r   r   r    r%      s       c             3   s   | ]} ||d   V  qdS )r3   Nr   )r#   n)ifosr   r    r%      s    r   )setsplitdiscardr/   lenrange)r5   resultr   )r5   r    gety   s     9

"
zinstrumentsproperty.getc             C   s   | dkrdS t tdd | D }t|r<tdd |D rDt| t|dkr|t|d dkr|t|d d s|d|d  S d	|S )
a  
		Convert an iterable of instrument names into a value
		suitable for storage in the "ifos" column found in many
		tables.  This function is mostly for internal use by the
		.instruments properties of the corresponding row classes.
		The input can be None or an iterable of zero or more
		instrument names, none of which may be zero-length, consist
		exclusively of spaces, or contain "," or "+" characters.
		The output is a single string containing the unique
		instrument names concatenated using "," as a delimiter.
		instruments will only be iterated over once and so can be a
		generator expression.  Whitespace is allowed in instrument
		names but might not be preserved.  Repeated names will not
		be preserved.

		NOTE:  in the special case that there is 1 instrument name
		in the iterable and it has an even number of characters > 2
		in it, the output will have a "," appended in order to
		force instrumentsproperty.get() to parse the string back
		into a single instrument name.  This is a special case
		included temporarily to disambiguate the encoding until all
		codes have been ported to the comma-delimited encoding.
		This behaviour will be discontinued at that time.  DO NOT
		WRITE CODE THAT RELIES ON THIS!  You have been warned.

		Example:

		>>> print(instrumentsproperty.set(None))
		None
		>>> assert instrumentsproperty.set(()) == ''
		>>> assert instrumentsproperty.set((u"H1",)) == 'H1'
		>>> assert instrumentsproperty.set((u"H1",u"H1",u"H1")) == 'H1'
		>>> assert instrumentsproperty.set((u"H1",u"L1")) == 'H1,L1'
		>>> assert instrumentsproperty.set((u"SWIFT",)) == 'SWIFT'
		>>> assert instrumentsproperty.set((u"H1L1",)) == 'H1L1,'
		Nc             s   s   | ]}|  V  qd S )N)r/   )r#   
instrumentr   r   r    r%      s    z*instrumentsproperty.set.<locals>.<genexpr>c             s   s   | ]}d |kpd|kV  qdS )r.   r2   Nr   )r#   r=   r   r   r    r%      s    r   r   r3   z%s,r.   )r   r6   allr&   
ValueErrorr9   join)instrumentsZ_instrumentsr   r   r    r6      s    &,zinstrumentsproperty.setNc             C   s   |  t|| jS )N)r<   getattrr+   )r,   objr   r   r   r    __get__   s    zinstrumentsproperty.__get__c             C   s   t || j| | d S )N)setattrr+   r6   )r,   rC   rA   r   r   r    __set__   s    zinstrumentsproperty.__set__)N)	__name__
__module____qualname__r-   staticmethodr<   r6   rD   rF   r   r   r   r    r*   u   s
   O4
r*   c               @   s:   e Zd ZdZdd ZdZdZeefZdddZd	d
 Z	dS )gpspropertyzK
	Descriptor used internally to implement LIGOTimeGPS-valued
	properties.
	c             C   s(   || _ t|| _|| _t|| _d S )N)s_nameoperator
attrgetterget_sns_nameget_ns)r,   rL   rP   r   r   r    r-     s    zgpsproperty.__init__)il    )l    l    Nc             C   sT   |  |}| |}|d kr(|d kr(d S |dkrJ||f| jkrDtjS tjS t||S )Nl    )rO   rQ   posinfr   ZPosInfinityZNegInfinityr   )r,   rC   r   snsr   r   r    rD     s    

zgpsproperty.__get__c             C   s   |d krd  }}nt |tjs(t|rZ|dkr<| j\}}q|dk rP| j\}}qt|nFy|j}|j	}W n  t
k
r   | |t|S X t|dkrtdt|| j| t|| j| d S )Nr   iɚ;z$denormalized LIGOTimeGPS not allowed)
isinstancer   infinitymathisinfrR   neginfr?   Z
gpsSecondsZgpsNanoSecondsAttributeErrorrF   r   absrE   rL   rP   )r,   rC   gpsrS   rT   r   r   r    rF     s"    


zgpsproperty.__set__)N)
rG   rH   rI   __doc__r-   rR   rY   ZinfsrD   rF   r   r   r   r    rK     s   
	rK   c                   s(   e Zd Z fddZ fddZ  ZS )gpsproperty_with_gmstc                s   t t| || || _d S )N)superr^   r-   	gmst_name)r,   rL   rP   r`   )	__class__r   r    r-   7  s    zgpsproperty_with_gmst.__init__c                sL   t t| || |d kr*t|| jd  n| |}t|| jt| d S )N)r_   r^   rF   rE   r`   rD   lalZGreenwichMeanSiderealTime)r,   rC   r\   )ra   r   r    rF   ;  s
    
zgpsproperty_with_gmst.__set__)rG   rH   rI   r-   rF   __classcell__r   r   )ra   r    r^   6  s   r^   c               @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
segmentpropertyzf
	Descriptor used internally to expose pairs of GPS-valued properties
	as segment-valued properties.
	c             C   s   || _ || _d S )N)startstop)r,   Z
start_nameZ	stop_namer   r   r    r-   K  s    zsegmentproperty.__init__Nc             C   s8   t || j}t || j}|d kr,|d kr,d S t||S )N)rB   re   rf   r   segment)r,   rC   r   re   rf   r   r   r    rD   O  s
    zsegmentproperty.__get__c             C   s:   |d krd  }}n|\}}t || j| t || j| d S )N)rE   re   rf   )r,   rC   segre   rf   r   r   r    rF   V  s
    
zsegmentproperty.__set__)N)rG   rH   rI   r]   r-   rD   rF   r   r   r   r    rd   F  s   
rd   process
process_idc               @   sH   e Zd ZdZddddddddddddddddZdZedZdd	 Zd
S )ProcessTableri   lstringint_4sz	ilwd:char)programversionZcvs_repositoryZcvs_entry_timecommentZ	is_onlinenodeusernameZunix_procid
start_timeend_timeZjobiddomainr5   rj   zPRIMARY KEY (process_id)r   c                s   t  fdd| D S )zh
		Return a set containing the process IDs from rows whose
		program string equals the given program.
		c             3   s   | ]}|j  kr|jV  qd S )N)rn   rj   )r#   row)rn   r   r    r%     s    z2ProcessTable.get_ids_by_program.<locals>.<genexpr>)r6   )r,   rn   r   )rn   r    get_ids_by_program  s    zProcessTable.get_ids_by_programN)	rG   rH   rI   r   r   constraints	ProcessIDnext_idrw   r   r   r   r    rk   k  s&   rk   c               @   s6   e Zd ZdZeej Ze	dZ
dd Zdd ZdS )Processz
	Example:

	>>> x = Process()
	>>> x.instruments = (u"H1", u"L1")
	>>> assert x.ifos == 'H1,L1'
	>>> assert x.instruments == {'H1', 'L1'}
	r5   c             C   s   | j S )z3
		Return a set of the instruments for this row.
		)rA   )r,   r   r   r    get_ifos  s    zProcess.get_ifosc             C   s
   || _ dS )z
		Serialize a sequence of instruments into the ifos
		attribute.  The instrument names must not contain the ","
		character.
		N)rA   )r,   rA   r   r   r    set_ifos  s    zProcess.set_ifosN)rG   rH   rI   r]   tuplerk   r   keys	__slots__r*   rA   r|   r}   r   r   r   r    r{     s
   r{   lfnlfn_idc               @   s.   e Zd ZdZdddddddZdZedZdS )	LfnTabler   z	ilwd:charrl   rm   )rj   r   r+   rp   rs   rt   zPRIMARY KEY (lfn_id)r   N)rG   rH   rI   r   r   rx   LfnIDrz   r   r   r   r    r     s   r   c               @   s   e Zd Zeej ZdS )LfnN)rG   rH   rI   r~   r   r   r   r   r   r   r   r    r     s   r   c               @   s0   e Zd ZdZddddddZddiZdd Zd	S )
ProcessParamsTableZprocess_paramsrl   z	ilwd:char)rn   rj   paramtyper   Zpp_pip_index)rj   r   c             C   s8   |j d k	r&|j tjkr&td|j  tj| | d S )Nzunrecognized type '%s')r   ligolwtypesZTypesr	   r   r
   r'   append)r,   rv   r   r   r    r     s    zProcessParamsTable.appendN)rG   rH   rI   r   r   how_to_indexr   r   r   r   r    r     s   r   c               @   s8   e Zd ZdZeej Ze	dd Z
e
jdd Z
dS )ProcessParamsa  
	Example:

	>>> x = ProcessParams()
	>>> x.pyvalue = u"test"
	>>> assert x.type == 'lstring'
	>>> assert x.value == 'test'
	>>> assert x.pyvalue == 'test'
	>>> x.pyvalue = 6.
	>>> assert x.type == 'real_8'
	>>> assert x.value == '6'
	>>> x.pyvalue
	6.0
	>>> x.pyvalue = None
	>>> print(x.type)
	None
	>>> print(x.value)
	None
	>>> print(x.pyvalue)
	None
	>>> x.pyvalue = True
	>>> assert(x.type) == 'int_4s'
	>>> assert x.value == '1'
	>>> x.pyvalue
	1
	c             C   sL   | j d krd S ytj| j }W n" tk
r@   td| j Y nX || j S )Nzinvalid type '%s')r   r   ZToPyTyper   KeyErrorr?   )r,   Z	parsefuncr   r   r    pyvalue  s    
zProcessParams.pyvaluec             C   sz   |d krd  | _ | _n`ytjt | | _ W n( tk
rR   tdtt | Y nX | j tjkrd|ntj| j  || _d S )Nztype not supported: %s)	r   r   r   Z
FromPyTyper   r?   reprZStringTypesZ
FormatFunc)r,   r   r   r   r    r     s    N)rG   rH   rI   r]   r~   r   r   r   r   propertyr   setterr   r   r   r    r     s   
r   c               @   sb   e Zd ZdZdddddddddddddddddZddiZdd	 Zd
d ZdddZdddZ	dS )SearchSummaryTableZsearch_summaryz	ilwd:charrl   rm   )rj   Zshared_objectZlalwrapper_cvs_tagZlal_cvs_tagrp   r5   in_start_timein_start_time_nsin_end_timein_end_time_nsout_start_timeout_start_time_nsout_end_timeout_end_time_nsneventsZnnodesZss_pi_index)rj   c             C   s   t dd | D S )z
		Return a segmentlist object describing the times spanned by
		the input segments of all rows in the table.

		Note:  the result is not coalesced, the segmentlist
		contains the segments as they appear in the table.
		c             s   s   | ]}|j V  qd S )N)
in_segment)r#   rv   r   r   r    r%   H  s    z0SearchSummaryTable.get_inlist.<locals>.<genexpr>)r   segmentlist)r,   r   r   r    
get_inlist@  s    zSearchSummaryTable.get_inlistc             C   s   t dd | D S )z
		Return a segmentlist object describing the times spanned by
		the output segments of all rows in the table.

		Note:  the result is not coalesced, the segmentlist
		contains the segments as they appear in the table.
		c             s   s   | ]}|j V  qd S )N)out_segment)r#   rv   r   r   r    r%   R  s    z1SearchSummaryTable.get_outlist.<locals>.<genexpr>)r   r   )r,   r   r   r    get_outlistJ  s    zSearchSummaryTable.get_outlistNc                sR   t  }xD| D ]<  jpd}|dks. j|kr|t fdd|D  qW |S )aT  
		Return a segmentlistdict mapping instrument to in segment
		list.  If process_ids is a sequence of process IDs, then
		only rows with matching IDs are included otherwise all rows
		are included.

		Note:  the result is not coalesced, each segmentlist
		contains the segments listed for that instrument as they
		appeared in the table.
		)NNc             3   s    | ]}|t  jgfV  qd S )N)r   r   r   )r#   r0   )rv   r   r    r%   c  s    z<SearchSummaryTable.get_in_segmentlistdict.<locals>.<genexpr>)r   segmentlistdictrA   rj   extenddict)r,   process_idsseglistsr5   r   )rv   r    get_in_segmentlistdictT  s    

 z)SearchSummaryTable.get_in_segmentlistdictc                sR   t  }xD| D ]<  jpd}|dks. j|kr|t fdd|D  qW |S )aU  
		Return a segmentlistdict mapping instrument to out segment
		list.  If process_ids is a sequence of process IDs, then
		only rows with matching IDs are included otherwise all rows
		are included.

		Note:  the result is not coalesced, each segmentlist
		contains the segments listed for that instrument as they
		appeared in the table.
		)NNc             3   s    | ]}|t  jgfV  qd S )N)r   r   r   )r#   r0   )rv   r   r    r%   u  s    z=SearchSummaryTable.get_out_segmentlistdict.<locals>.<genexpr>)r   r   rA   rj   r   r   )r,   r   r   r5   r   )rv   r    get_out_segmentlistdictf  s    

 z*SearchSummaryTable.get_out_segmentlistdict)N)N)
rG   rH   rI   r   r   r   r   r   r   r   r   r   r   r    r   (  s,   


r   c               @   s   e Zd ZdZeej Ze	dZ
eddZeddZeddZed	d
ZeddZeddZdd Zdd Zdd Zdd Zdd Zdd ZdS )SearchSummarya  
	Example:

	>>> x = SearchSummary()
	>>> x.instruments = (u"H1", u"L1")
	>>> assert x.ifos == 'H1,L1'
	>>> assert x.instruments == {'H1', 'L1'}
	>>> x.in_start = x.out_start = LIGOTimeGPS(0)
	>>> x.in_end = x.out_end = LIGOTimeGPS(10)
	>>> x.in_segment
	segment(LIGOTimeGPS(0, 0), LIGOTimeGPS(10, 0))
	>>> x.out_segment
	segment(LIGOTimeGPS(0, 0), LIGOTimeGPS(10, 0))
	>>> x.in_segment = x.out_segment = None
	>>> print(x.in_segment)
	None
	>>> print(x.out_segment)
	None
	r5   r   r   r   r   r   r   r   r   in_startin_end	out_startout_endc             C   s   | j S )z3
		Return a set of the instruments for this row.
		)rA   )r,   r   r   r    r|     s    zSearchSummary.get_ifosc             C   s
   || _ dS )z
		Serialize a sequence of instruments into the ifos
		attribute.  The instrument names must not contain the ","
		character.
		N)rA   )r,   rA   r   r   r    r}     s    zSearchSummary.set_ifosc             C   s   | j S )z
		Get the input segment.
		)r   )r,   r   r   r    get_in  s    zSearchSummary.get_inc             C   s
   || _ dS )z
		Set the input segment.
		N)r   )r,   rh   r   r   r    set_in  s    zSearchSummary.set_inc             C   s   | j S )z
		Get the output segment.
		)r   )r,   r   r   r    get_out  s    zSearchSummary.get_outc             C   s
   || _ dS )z
		Set the output segment.
		N)r   )r,   rh   r   r   r    set_out  s    zSearchSummary.set_outN)rG   rH   rI   r]   r~   r   r   r   r   r*   rA   rK   r   r   r   r   rd   r   r   r|   r}   r   r   r   r   r   r   r   r    r   y  s   





r   search_summvarssearch_summvar_idc               @   s,   e Zd ZdZddddddZdZedZdS )	SearchSummVarsTabler   z	ilwd:charrl   real_8)rj   r   r+   stringr   zPRIMARY KEY (search_summvar_id)r   N)rG   rH   rI   r   r   rx   SearchSummVarsIDrz   r   r   r   r    r     s   r   c               @   s   e Zd Zeej ZdS )SearchSummVarsN)rG   rH   rI   r~   r   r   r   r   r   r   r   r    r     s   r   
experimentexperiment_idc            	   @   sN   e Zd ZdZdddddddddZdZedZdd	d
ZdddZ	dd Z
dS )ExperimentTabler   z	ilwd:charrl   rm   )r   search_groupsearchlars_idrA   gps_start_timegps_end_timecommentszPRIMARY KEY (experiment_id)r   Nc       	   	   C   sR   t |}xD| D ]<}|j|j|j|j|j|j|jf|||||||fkr|jS qW dS )a   
		Return the expr_def_id for the row in the table whose
		values match the givens.
		If a matching row is not found, returns None.

		@search_group: string representing the search group (e.g., cbc)
		@serach: string representing search (e.g., inspiral)
		@lars_id: string representing lars_id
		@instruments: the instruments; must be a python set
		@gps_start_time: string or int representing the gps_start_time of the experiment
		@gps_end_time: string or int representing the gps_end_time of the experiment
		N)	ifos_from_instrument_setr   r   r   rA   r   r   r   r   )	r,   r   r   r   rA   r   r   r   rv   r   r   r    get_expr_id  s
    
2
zExperimentTable.get_expr_idc       
   	   C   sp   | j |||||||d}|r |S |  }	|  |	_||	_||	_||	_t||	_||	_	||	_
||	_| |	 |	jS )a   
		Creates a new def_id for the given arguments and returns it. 
		If an entry already exists with these, will just return that id.

		@search_group: string representing the search group (e.g., cbc)
		@serach: string representing search (e.g., inspiral)
		@lars_id: string representing lars_id
		@instruments: the instruments; must be a python set
		@gps_start_time: string or int representing the gps_start_time of the experiment
		@gps_end_time: string or int representing the gps_end_time of the experiment
		)r   )r   RowTypeget_next_idr   r   r   r   r   rA   r   r   r   r   )
r,   r   r   r   rA   r   r   r   check_idrv   r   r   r    write_new_expr_id  s    


z!ExperimentTable.write_new_expr_idc                sF    fdd| D }t |dkr&tdt |dkr>td  |d S )z6
		Returns row in matching the given experiment_id.
		c                s   g | ]}|j  kr|qS r   )r   )r#   rv   )r   r   r    
<listcomp><  s    z3ExperimentTable.get_row_from_id.<locals>.<listcomp>r   z!duplicate ids in experiment tabler   zid '%s' not found in table)r9   r?   )r,   r   rv   r   )r   r    get_row_from_id8  s    zExperimentTable.get_row_from_id)N)N)rG   rH   rI   r   r   rx   ExpDefIDrz   r   r   r   r   r   r   r    r     s   

!r   c               @   s*   e Zd Zeej Zdd Zdd Z	dS )
Experimentc             C   s
   t | jS )z3
		Return a set of the instruments for this row.
		)instrument_set_from_ifosrA   )r,   r   r   r    get_instrumentsH  s    zExperiment.get_instrumentsc             C   s   t || _dS )z
		Serialize a sequence of instruments into the ifos
		attribute.  The instrument names must not contain the ","
		character.
		N)r   rA   )r,   rA   r   r   r    set_instrumentsN  s    zExperiment.set_instrumentsN)
rG   rH   rI   r~   r   r   r   r   r   r   r   r   r   r    r   E  s   r   experiment_summaryexperiment_summ_idc            	   @   sz   e Zd ZdZdddddddddZdZdd	d
ZedZdddddgZ	dd Z
dddZdddZd ddZd!ddZdS )"ExperimentSummaryTabler   z	ilwd:charrl   rm   int_4u)r   r   time_slide_idveto_def_namedatatypesim_proc_iddurationr   z PRIMARY KEY (experiment_summ_id))r   )r   )Zes_ei_indexZes_dt_indexr   slideall_data
playgroundexclude_playZ
simulationc             C   sp   i }xf| D ]^}|j |kr"i ||j < |j|j|j|jf||j  krHtd|j||j  |j|j|j|jf< q
W |S )z
		Return table as a dictionary mapping experiment_id, time_slide_id,
		veto_def_name, and sim_proc_id (if it exists) to the expr_summ_id.
		z-duplicate entries in experiment_summary table)r   r   r   r   r   r   r   )r,   drv   r   r   r    
as_id_dict|  s    


$z!ExperimentSummaryTable.as_id_dictNc             C   s>   x8| D ]0}|j |j|j|j|jf|||||fkr|jS qW dS )a  
		Return the expr_summ_id for the row in the table whose experiment_id, 
		time_slide_id, veto_def_name, and datatype match the given. If sim_proc_id,
		will retrieve the injection run matching that sim_proc_id.
		If a matching row is not found, returns None.
		N)r   r   r   r   r   r   )r,   r   r   r   r   r   rv   r   r   r    get_expr_summ_id  s    	
&
z'ExperimentSummaryTable.get_expr_summ_idc             C   sh   | j |||||d}|r|S |  }|  |_||_||_||_||_||_d|_	d|_
| | |jS )a_  
		Writes a single entry to the experiment_summ table. This can be used
		for either injections or non-injection experiments. However, it is
		recommended that this only be used for injection experiments; for
		non-injection experiments write_experiment_summ_set should be used to
		ensure that an entry gets written for every time-slide performed.
		)r   N)r   r   r   r   r   r   r   r   r   r   r   r   )r,   r   r   r   r   r   r   rv   r   r   r    write_experiment_summ  s    	

z,ExperimentSummaryTable.write_experiment_summTFc       	      C   s   xz|D ]r}t ||  sd|r2| j|||ddd |rJ| j|||ddd |rx| j|||ddd q| j|||ddd qW |r|  S dS )a&  
		Method for writing a new set of non-injection experiments to the experiment
		summary table. This ensures that for every entry in the 
		experiment table, an entry for every slide is added to
		the experiment_summ table, rather than just an entry for slides that
		have events in them. Default is to write a 3 rows for zero-lag: one for
		all_data, playground, and exclude_play. (If all of these are set to false,
		will only slide rows.)
		
		Note: sim_proc_id is hard-coded to None because time-slides
		are not performed with injections.

		@experiment_id: the experiment_id for this experiment_summary set
		@time_slide_dict: the time_slide table as a dictionary; used to figure out
			what is zero-lag and what is slide
		@veto_def_name: the name of the vetoes applied
		@write_all_data: if set to True, writes a zero-lag row who's datatype column
			is set to 'all_data'
		@write_playground: same, but datatype is 'playground'
		@write_exclude_play: same, but datatype is 'exclude_play'
		@return_dict: if set to true, returns an id_dict of the table
		r   N)r   r   r   r   )r&   valuesr   r   )	r,   r   Ztime_slide_dictr   Zwrite_all_dataZwrite_playgroundZwrite_exclude_playZreturn_dictZslide_idr   r   r    write_non_injection_summary  s    
z2ExperimentSummaryTable.write_non_injection_summaryc             C   sb   xL| D ]D}|j |krq|jdkr&d|_|r>| j|7  _|jS ||_|jS qW tdt| dS )aA  
		Add num_events to the nevents column in a specific entry in the table. If
		add_to_current is set to False, will overwrite the current nevents entry in
		the row with num_events. Otherwise, default is to add num_events to
		the current value.

		Note: Can subtract events by passing a negative number to num_events.
		Nr   z$'%s' could not be found in the table)r   r   r?   str)r,   r   Z
num_eventsZadd_to_currentrv   r   r   r    add_nevents  s    	



z"ExperimentSummaryTable.add_nevents)N)N)TTTF)T)rG   rH   rI   r   r   rx   r   	ExpSummIDrz   Z	datatypesr   r   r   r   r   r   r   r   r    r   f  s&   


'r   c               @   s   e Zd Zeej ZdS )ExperimentSummaryN)rG   rH   rI   r~   r   r   r   r   r   r   r   r    r     s   r   c               @   s,   e Zd ZdZdddZdddZdd Zd	S )
ExperimentMapTableZexperiment_mapz	ilwd:char)r   coinc_event_id)r   )r   )Zem_esi_indexZem_cei_indexc             C   sD   g }x"| D ]}|j |kr
||j q
W t|dkr@td| |S )zJ
		Gets all the experiment_summ_ids that map to a given coinc_event_id.
		r   z3'%s' could not be found in the experiment_map table)r   r   r   r9   r?   )r,   r   Zexperiment_summ_idsrv   r   r   r    get_experiment_summ_ids  s    

z*ExperimentMapTable.get_experiment_summ_idsN)rG   rH   rI   r   r   r   r   r   r   r   r    r     s   r   c               @   s   e Zd Zeej ZdS )ExperimentMapN)rG   rH   rI   r~   r   r   r   r   r   r   r   r    r     s   r   gds_triggerevent_idc               @   s^   e Zd ZdZdddddddddddddddddddddddddddddZdZed	Zd
ZdS )GDSTriggerTabler   rm   zilwd:char_uz	ilwd:charrl   real_4)
creator_dbrj   	filter_idr+   subtyper0   rs   start_time_nsr   priorityZdispositionsizeZsignificance	frequency	bandwidth	time_peakZtime_averageZ
time_sigmaZ	freq_peakZfreq_averageZ
freq_sigmaZnoise_powerZsignal_powerZpixel_count
confidenceZ
binarydataZbinarydata_lengthr   zPRIMARY KEY (event_id)r   )rj   r0   r   N)	rG   rH   rI   r   r   rx   GDSTriggerIDrz   interncolumnsr   r   r   r    r   1  s@   r   c               @   sb   e Zd Zeej Zdd Zdd Z	dd Z
dd Zd	d
 Zdd Zdd Zdd Zdd ZdS )
GDSTriggerc             C   s   t | j| jS )N)r   rs   r   )r,   r   r   r    	get_start]  s    zGDSTrigger.get_startc             C   s   |j |j | _| _d S )N)secondsnanosecondsrs   r   )r,   r\   r   r   r    	set_start`  s    zGDSTrigger.set_startc             C   s   t | j| j| j S )N)r   rs   r   r   )r,   r   r   r    get_stopc  s    zGDSTrigger.get_stopc             C   s   t | j| jS )N)r   r   )r,   r   r   r    get_peakf  s    zGDSTrigger.get_peakc             C   s   |j |j | _| _d S )N)r   r   r   peak_time_ns)r,   r\   r   r   r    set_peaki  s    zGDSTrigger.set_peakc             C   s    t | j| j}t||| j S )N)r   rs   r   r   rg   r   )r,   re   r   r   r    
get_periodl  s    zGDSTrigger.get_periodc             C   s,   |d j |d j | _| _tt|| _d S )Nr   )r   r   rs   r   floatr[   r   )r,   periodr   r   r    
set_periodp  s    zGDSTrigger.set_periodc             C   s   | j }t||| j S )N)r   r   rg   r   )r,   lowr   r   r    get_bandt  s    zGDSTrigger.get_bandc             C   s   |d | _ t|| _d S )Nr   )r   r[   r   )r,   bandr   r   r    set_bandx  s    
zGDSTrigger.set_bandN)rG   rH   rI   r~   r   r   r   r   r   r   r   r   r  r  r  r  r	  r   r   r   r    r   V  s   r   
sngl_burstc            1   @   s  e Zd ZdZddddddddddddddddddddddddddddddddddddddddddddddddd0ZdZed	Zd
Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+S ),SnglBurstTabler
  rm   z	ilwd:charrl   r   r   )0r   rj   r   r0   r   channelrs   r   	stop_timestop_time_nsr   flowfhighcentral_freqr   	amplitudesnrr   chisq	chisq_dofZtfvolumehrssZtime_lag	peak_timer   Zpeak_frequencyZpeak_strainZpeak_time_errorZpeak_frequency_errorZpeak_strain_errorms_start_timems_start_time_nsms_stop_timems_stop_time_nsms_durationms_flowms_fhighms_bandwidthZms_hrssZms_snrZms_confidenceZparam_one_nameZparam_one_valueZparam_two_nameZparam_two_valueZparam_three_nameZparam_three_valuer   zPRIMARY KEY (event_id)r   )rj   r0   r   r  c             C   s$   |  dkr| jS | | S dS )z@returns: an array of column values for each row in the table

		@param column:
			name of column to return
		@returntype:
			numpy.ndarray
		qN)lowerget_qgetColumnByNameasarray)r,   columnr   r   r    
get_column  s    zSnglBurstTable.get_columnc             C   s   t dd | D S )zP@returns: the peak time of each row in the table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )r   )r#   rv   r   r   r    r     s    z+SnglBurstTable.get_peak.<locals>.<listcomp>)numpyr$  )r,   r   r   r    r     s    zSnglBurstTable.get_peakc             C   s   t dd | D S )zQ@returns: the start time of each row in the table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )r   )r#   rv   r   r   r    r     s    z,SnglBurstTable.get_start.<locals>.<listcomp>)r'  r$  )r,   r   r   r    r     s    zSnglBurstTable.get_startc             C   s   t dd | D S )zq@returns: the start time of the most significant tile for
		each row in the table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )get_ms_start)r#   rv   r   r   r    r     s    z/SnglBurstTable.get_ms_start.<locals>.<listcomp>)r'  r$  )r,   r   r   r    r(    s    zSnglBurstTable.get_ms_startc             C   s   t dd | D S )zP@returns: the stop time of each row in the table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )r   )r#   rv   r   r   r    r     s    z+SnglBurstTable.get_stop.<locals>.<listcomp>)r'  r$  )r,   r   r   r    r     s    zSnglBurstTable.get_stopc             C   s   t dd | D S )zp@returns: the stop time of the most significant tile for
		each row in the table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )get_ms_stop)r#   rv   r   r   r    r     s    z.SnglBurstTable.get_ms_stop.<locals>.<listcomp>)r'  r$  )r,   r   r   r    r)    s    zSnglBurstTable.get_ms_stopc             C   s   t dd | D S )zH@returns: the Q of each row in the table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )r"  )r#   rv   r   r   r    r     s    z(SnglBurstTable.get_q.<locals>.<listcomp>)r'  r$  )r,   r   r   r    r"    s    zSnglBurstTable.get_qc             C   s   t dd | D S )zb@returns: the Z (Omega-Pipeline energy) of each row in the
		table
		@returntype: numpy.ndarray
		c             S   s   g | ]}|  qS r   )get_z)r#   rv   r   r   r    r     s    z(SnglBurstTable.get_z.<locals>.<listcomp>)r'  r$  )r,   r   r   r    r*    s    zSnglBurstTable.get_zc             C   s   t dd | D S )za@returns: the period segment of each row in the table
		@returntype: ligo.segments.segmentlist
		c             S   s   g | ]}|  qS r   )r  )r#   rv   r   r   r    r     s    z-SnglBurstTable.get_period.<locals>.<listcomp>)r   r   )r,   r   r   r    r    s    zSnglBurstTable.get_periodc             C   s   t dd | D S )z@returns: the period segment for the most significant tile
		of each row in the table
		@returntype: ligo.segments.segmentlist
		c             S   s   g | ]}|  qS r   )get_ms_period)r#   rv   r   r   r    r     s    z0SnglBurstTable.get_ms_period.<locals>.<listcomp>)r   r   )r,   r   r   r    r+    s    zSnglBurstTable.get_ms_periodc             C   s   t dd | D S )za@returns: the frequency band of each row in the table
		@returntype: ligo.segments.segmentlist
		c             S   s   g | ]}|  qS r   )r  )r#   rv   r   r   r    r     s    z+SnglBurstTable.get_band.<locals>.<listcomp>)r   r   )r,   r   r   r    r  
  s    zSnglBurstTable.get_bandc             C   s   t dd | D S )zX@returns: the frequency band of the most significant tile
		for each row in the table
		c             S   s   g | ]}|  qS r   )get_ms_band)r#   rv   r   r   r    r     s    z.SnglBurstTable.get_ms_band.<locals>.<listcomp>)r   r   )r,   r   r   r    r,    s    zSnglBurstTable.get_ms_bandc             C   s4   |   }x&| D ]}| }||kr|| qW |S )zL@returns: those rows of the table that don't lie within a
		given seglist
		)copyr   r   )r,   seglistkeeprv   timer   r   r    veto  s    
zSnglBurstTable.vetoc             C   s4   |   }x&| D ]}| }||kr|| qW |S )zF@returns: those rows of the table that lie within a given
		seglist
		)r-  r   r   )r,   r.  vetoedrv   r0  r   r   r    r2  !  s    
zSnglBurstTable.vetoedc             C   s:   |   }x,| D ]$}| }|||j kr|| qW |S )N)r-  r   r0   r   )r,   seglistdictr/  rv   r0  r   r   r    veto_seglistdict,  s    
zSnglBurstTable.veto_seglistdictc             C   s:   |   }x,| D ]$}| }|||j kr|| qW |S )N)r-  r   r0   r   )r,   r3  r2  rv   r0  r   r   r    vetoed_seglistdict4  s    
z!SnglBurstTable.vetoed_seglistdictN)rG   rH   rI   r   r   rx   SnglBurstIDrz   r   r&  r   r   r(  r   r)  r"  r*  r  r+  r  r,  r1  r2  r4  r5  r   r   r   r    r    s   r  c               @   s^  e Zd Zeej ZeddZ	eddZ
eddZedd Zejd	d Zed
d Zejdd ZeddZeddZeddZedd Zejdd Zedd Zejdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Z d1d2 Z!d3d4 Z"d5d6 Z#d7d8 Z$d9d: Z%d;d< Z&d=d> Z'd?d@ Z(dAS )B	SnglBurstrs   r   r  r  r  r   c             C   sn   | j }y
| j}W n tk
r(   d }Y nX |d k	rN|d krN| jd k	rN|| j }|d krb|d krbd S t||S )N)re   rf   rZ   r   r   rg   )r,   re   rf   r   r   r    r  H  s    


zSnglBurst.periodc             C   s:   |d krd  | _  | _| _n|\| _ | _tt|| _d S )N)re   rf   r   r  r[   )r,   rh   r   r   r    r  V  s    c             C   s<   | j d kr| jd krd S t| j | jd  | j | jd  S )Ng       @)r  r   r   rg   )r,   r   r   r    r  ^  s    zSnglBurst.bandc             C   s~   |d kr<yd  | _ | _W n tk
r,   Y nX d  | _| _n>y|\| _ | _W n tk
r`   Y nX t|d | _t|| _d S )Ng       @)r  r  rZ   r  r   sumr[   )r,   rh   r   r   r    r  d  s    r  r  r  r  Zms_peak_timeZms_peak_time_nsc             C   sT   | j }| j}|d k	r4|d kr4| jt d k	r4|| j }|d krH|d krHd S t||S )N)ms_startms_stopmsr   r  r   rg   )r,   re   rf   r   r   r    	ms_period~  s    
zSnglBurst.ms_periodc             C   s:   |d krd  | _  | _| _n|\| _ | _tt|| _d S )N)r9  r:  r  r  r[   )r,   rh   r   r   r    r<    s    c             C   s.   | j d kr| jd krd S t| j | j | j S )N)r  r  r   rg   )r,   r   r   r    ms_band  s    zSnglBurst.ms_bandc             C   s6   |d krd  | _  | _| _n|\| _| _t|| _ d S )N)r  r  r  r[   )r,   rh   r   r   r    r=    s    c             C   s   | j S )N)re   )r,   r   r   r    r     s    zSnglBurst.get_startc             C   s
   || _ d S )N)re   )r,   r\   r   r   r    r     s    zSnglBurst.set_startc             C   s   | j S )N)rf   )r,   r   r   r    r     s    zSnglBurst.get_stopc             C   s
   || _ d S )N)rf   )r,   r\   r   r   r    set_stop  s    zSnglBurst.set_stopc             C   s   | j S )N)peak)r,   r   r   r    r     s    zSnglBurst.get_peakc             C   s
   || _ d S )N)r?  )r,   r\   r   r   r    r    s    zSnglBurst.set_peakc             C   s   | j S )N)r  )r,   r   r   r    r    s    zSnglBurst.get_periodc             C   s
   || _ d S )N)r  )r,   r  r   r   r    r    s    zSnglBurst.set_periodc             C   s   | j S )N)r  )r,   r   r   r    r    s    zSnglBurst.get_bandc             C   s
   || _ d S )N)r  )r,   r  r   r   r    r	    s    zSnglBurst.set_bandc             C   s   | j S )N)r9  )r,   r   r   r    r(    s    zSnglBurst.get_ms_startc             C   s
   || _ d S )N)r9  )r,   r\   r   r   r    set_ms_start  s    zSnglBurst.set_ms_startc             C   s   | j S )N)r:  )r,   r   r   r    r)    s    zSnglBurst.get_ms_stopc             C   s
   || _ d S )N)r:  )r,   r\   r   r   r    set_ms_stop  s    zSnglBurst.set_ms_stopc             C   s   | j S )N)r<  )r,   r   r   r    r+    s    zSnglBurst.get_ms_periodc             C   s
   || _ d S )N)r<  )r,   r  r   r   r    set_ms_period  s    zSnglBurst.set_ms_periodc             C   s   | j S )N)r=  )r,   r   r   r    r,    s    zSnglBurst.get_ms_bandc             C   s
   || _ d S )N)r=  )r,   r  r   r   r    set_ms_band  s    zSnglBurst.set_ms_bandc             C   s   | j d tjd  | j S )Nr3   g      ?)r   r'  pir  )r,   r   r   r    r"    s    zSnglBurst.get_qc             C   s   | j d d S )Nr3   g       @)r  )r,   r   r   r    r*    s    zSnglBurst.get_zN))rG   rH   rI   r~   r  r   r   r   rK   re   rf   r?  r   r  r   r  r9  r:  Zms_peakr<  r=  r   r   r   r>  r   r  r  r  r  r	  r(  r@  r)  rA  r+  rB  r,  rC  r"  r*  r   r   r   r    r7  =  sF   






r7  c               @   sF   e Zd ZdZdddddddddddddddddddddZddiZd	S )
MultiBurstTableZmulti_burstrm   z	ilwd:charrl   r   )r   rj   r   r5   rs   r   r   r  r   r  r   r  r  r   false_alarm_rateZligo_axis_raZligo_axis_dec
ligo_angleligo_angle_sigr   Zmb_cei_index)r   N)rG   rH   rI   r   r   r   r   r   r   r    rE    s,   rE  c               @   s   e Zd Zeej ZedZ	e
ddZe
ddZedd Zejdd Zed	d
 Zejdd
 Zdd Zdd Zdd Zdd ZdS )
MultiBurstr5   rs   r   r  r   c             C   s.   | j }|d kr| jd krd S t||| j S )N)re   r   r   rg   )r,   re   r   r   r    r    s    zMultiBurst.periodc             C   s2   |d krd  | _ | _n|d | _ tt|| _d S )Nr   )re   r   r  r[   )r,   rh   r   r   r    r  %  s    
c             C   s<   | j d kr| jd krd S t| j | jd  | j | jd  S )Ng       @)r  r   r   rg   )r,   r   r   r    r  -  s    zMultiBurst.bandc             C   s2   |d krd  | _ | _nt|d | _ t|| _d S )Ng       @)r  r   r8  r[   )r,   rh   r   r   r    r  3  s    c             C   s   | j S )N)rA   )r,   r   r   r    r|   =  s    zMultiBurst.get_ifosc             C   s
   || _ d S )N)rA   )r,   rA   r   r   r    r}   @  s    zMultiBurst.set_ifosc             C   s   | j S )N)r?  )r,   r   r   r    r   C  s    zMultiBurst.get_peakc             C   s
   || _ d S )N)r?  )r,   r\   r   r   r    r  F  s    zMultiBurst.set_peakN)rG   rH   rI   r~   rE  r   r   r   r*   rA   rK   re   r?  r   r  r   r  r|   r}   r   r  r   r   r   r    rI    s   


rI  sngl_inspiralc            A   @   sX  e Zd ZdZddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd@ZdZed	Zd
Zd8ddZ	dd Z
dd Zdd Zdd Zd9ddZd:ddZd;ddZd<ddZd=dd Zd>d!d"Zd?d$d%Zd&d' Zd(d) Zd@d+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7S )ASnglInspiralTablerJ  z	ilwd:charrl   rm   r   r   )@rj   r0   r   r  rt   end_time_nsend_time_gmstimpulse_timeimpulse_time_nsZtemplate_durationZevent_durationr  eff_distance	coa_phasemass1mass2mchirpmtotaletakappachitau0tau2tau3tau4tau5ttotalpsi0psi3alphaalpha1alpha2alpha3alpha4alpha5alpha6betaf_finalr  r  r  
bank_chisqbank_chisq_dof
cont_chisqcont_chisq_dofZsigmasqZrsqveto_durationGamma0ZGamma1ZGamma2ZGamma3ZGamma4ZGamma5ZGamma6ZGamma7ZGamma8ZGamma9spin1xspin1yspin1zspin2xspin2yspin2zr   zPRIMARY KEY (event_id)r   )rj   r0   r   r       @o@      @c             C   s   |dkr|   S |dkr |  S |dkr0|  S |dkr@|  S |dkrT| j|dS |dkrh| j|dS |d	krx|  S |d
kr|  S | |	 S d S )Nreduced_chisqreduced_bank_chisqreduced_cont_chisqZsnr_over_chiZeffective_snr)facnew_snr)indexZlvS5statZchirp_eff_distance)
get_reduced_chisqget_reduced_bank_chisqget_reduced_cont_chisqget_snr_over_chiget_effective_snrget_new_snrget_lvS5statget_chirp_eff_distr#  r$  )r,   r%  rz  r|  r   r   r    r&    s"    zSnglInspiralTable.get_columnc             C   s   dd | D S )Nc             S   s   g | ]}|  qS r   )get_end)r#   rv   r   r   r    r     s    z-SnglInspiralTable.get_end.<locals>.<listcomp>r   )r,   r   r   r    r    s    zSnglInspiralTable.get_endc             C   s   |  dd|  d d  S )Nr  r3   r  )r&  )r,   r   r   r    r}    s    z#SnglInspiralTable.get_reduced_chisqc             C   s   |  d|  d S )Nrj  rk  )r&  )r,   r   r   r    r~    s    z(SnglInspiralTable.get_reduced_bank_chisqc             C   s   |  d|  d S )Nrl  rm  )r&  )r,   r   r   r    r    s    z(SnglInspiralTable.get_reduced_cont_chisqc             C   s4   |  d}|  d}|d|d |  d  |d  S )Nr  rw  r   r3   g      ?)r&  )r,   rz  r  rchisqr   r   r    r    s    

z#SnglInspiralTable.get_effective_snrc             C   s4   |  d}|  d}|d|d |  d  |d  S )Nr  rx  r   r3   g      ?)r&  )r,   rz  r  r  r   r   r    get_bank_effective_snr  s    

z(SnglInspiralTable.get_bank_effective_snrc             C   s4   |  d}|  d}|d|d |  d  |d  S )Nr  ry  r   r3   g      ?)r&  )r,   rz  r  r  r   r   r    get_cont_effective_snr  s    

z(SnglInspiralTable.get_cont_effective_snrc             C   sN   |  d}|  d}d}|dd|||    d|   }t||dk | |S )Nr  rw  g       @g      ?r   g      ?)r&  r'  putmask)r,   r|  r  r  nhighnewsnrr   r   r    r    s    

 zSnglInspiralTable.get_new_snrc             C   sN   |  d}|  d}d}|dd|||    d|   }t||dk | |S )Nr  rx  g       @g      ?r   g      ?)r&  r'  r  )r,   r|  r  r  r  Z
banknewsnrr   r   r    get_bank_new_snr  s    

 z"SnglInspiralTable.get_bank_new_snrc             C   sN   |  d}|  d}d}|dd|||    d|   }t||dk | |S )Nr  ry  g       @g      ?r   g      ?)r&  r'  r  )r,   r|  r  r  r  Z
contnewsnrr   r   r    get_cont_new_snr  s    

 z"SnglInspiralTable.get_cont_new_snrffffff?c             C   s"   |  d}|  d}t|||S )NrT  rP  )r&  SnglInspiralchirp_distance)r,   ref_massrT  eff_distr   r   r    r    s    

z$SnglInspiralTable.get_chirp_eff_distc             C   s   |  d|  dd  S )Nr  r  g      ?)r&  )r,   r   r   r    r    s    z"SnglInspiralTable.get_snr_over_chic             C   s
   |  dS )Nrh  )r&  )r,   r   r   r    r    s    zSnglInspiralTable.get_lvS5statFc                sD   |rt  fdd|  | S |  }| fdd| D  |S dS )z
		Return a SnglInspiralTable with rows from self having IFO equal
		to the given ifo. If inplace, modify self directly, else create
		a new table and fill it.
		c                s
   | j  kS )N)r0   )rv   )r0   r   r    <lambda>       z*SnglInspiralTable.ifocut.<locals>.<lambda>c                s   g | ]}|j  kr|qS r   )r0   )r#   rv   )r0   r   r    r     s    z,SnglInspiralTable.ifocut.<locals>.<listcomp>N)r   Zinplace_filterr-  r   )r,   r0   ZinplaceZifoTrigsr   )r0   r    ifocut  s    zSnglInspiralTable.ifocutc             C   sH   |   }|   }x2| D ]*}| }||kr6|| q|| qW |S )N)r-  r  r   )r,   r.  r2  r/  rv   r0  r   r   r    r1    s    
zSnglInspiralTable.vetoc             C   sH   |   }|   }x2| D ]*}| }||kr6|| q|| qW |S )zk
		Return the inverse of what veto returns, i.e., return the triggers
		that lie within a given seglist.
		)r-  r  r   )r,   r.  r2  r/  rv   r0  r   r   r    r2    s    
zSnglInspiralTable.vetoedc             C   sN   |   }|   }x8| D ]0}| }|||j kr<|| q|| qW |S )N)r-  r  r0   r   )r,   r3  r2  r/  rv   r0  r   r   r    r4  !  s    
z"SnglInspiralTable.veto_seglistdictc             C   sN   |   }|   }x8| D ]0}| }|||j kr<|| q|| qW |S )N)r-  r  r0   r   )r,   r3  r2  r/  rv   r0  r   r   r    r5  ,  s    
z$SnglInspiralTable.vetoed_seglistdictc                s$   |   }| fdd| D  |S )z
		Return the triggers with a specific slide number.
		@param slide_num: the slide number to recover (contained in the event_id)
		c             3   s   | ]}|   kr|V  qd S )N)get_slide_number)r#   rv   )	slide_numr   r    r%   =  s    z-SnglInspiralTable.getslide.<locals>.<genexpr>)r-  r   )r,   r  
slideTrigsr   )r  r    getslide7  s    zSnglInspiralTable.getslideN)ru  rv  )ru  )ru  )ru  )rv  )rv  )rv  )r  )F)rG   rH   rI   r   r   rx   SnglInspiralIDrz   r   r&  r  r}  r~  r  r  r  r  r  r  r  r  r  r  r  r1  r2  r4  r5  r  r   r   r   r    rK  Y  s   








rK  c               @   s  e Zd Zeej Zed0ddZ	e
ddZedd Zejdd Zed	d
 Zejdd
 Zedd Zejdd Zdd Zdd Zdd Zd1ddZd2ddZd3ddZd4ddZd5dd Zd6d!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/S )7r  ffffff?c             C   s   | d| | d  S )Ng ڌ?g?r   )distrT  r  r   r   r    r  D  s    zSnglInspiral.chirp_distancert   rL  c             C   s<   | j d kr"| jd kr"| jd kr"d S tj| j | j| jfddS )Ndouble)dtype)ro  rp  rq  r'  array)r,   r   r   r    spin1N  s    zSnglInspiral.spin1c             C   s.   |d krd\| _ | _| _n|\| _ | _| _d S )N)NNN)ro  rp  rq  )r,   spinr   r   r    r  T  s    c             C   s<   | j d kr"| jd kr"| jd kr"d S tj| j | j| jfddS )Nr  )r  )rr  rs  rt  r'  r  )r,   r   r   r    spin2[  s    zSnglInspiral.spin2c             C   s.   |d krd\| _ | _| _n|\| _ | _| _d S )N)NNN)rr  rs  rt  )r,   r  r   r   r    r  a  s    c             C   s
   t | jS )N)intrn  )r,   r   r   r    template_idm  s    zSnglInspiral.template_idc             C   s   t || _d S )N)r  rn  )r,   r  r   r   r    r  q  s    c             C   s   t | jd| j d  S )Nr3   )r  r  r  )r,   r   r   r    r}  y  s    zSnglInspiral.get_reduced_chisqc             C   s   t | j| j S )N)r  rj  rk  )r,   r   r   r    r~  |  s    z#SnglInspiral.get_reduced_bank_chisqc             C   s   t | j| j S )N)r  rl  rm  )r,   r   r   r    r    s    z#SnglInspiral.get_reduced_cont_chisq     @o@c             C   s(   | j d| j d |  d  |  d  S )Nr   r3   g      ?)r  r}  )r,   rz  r   r   r    r    s    zSnglInspiral.get_effective_snrc             C   s(   | j d| j d |  d  |  d  S )Nr   r3   g      ?)r  r~  )r,   rz  r   r   r    r    s    z#SnglInspiral.get_bank_effective_snrc             C   s(   | j d| j d |  d  |  d  S )Nr   r3   g      ?)r  r  )r,   rz  r   r   r    r    s    z#SnglInspiral.get_cont_effective_snr      @c             C   s@   |   }d}|dkr6| jd|||   d d|   S | jS d S )Ng       @g      ?r   r3   )r}  r  )r,   r|  r  r  r   r   r    r    s
    "zSnglInspiral.get_new_snrc             C   s@   |   }d}|dkr6| jd|||   d d|   S | jS d S )Ng       @g      ?r   r3   )r~  r  )r,   r|  r  r  r   r   r    r    s
    "zSnglInspiral.get_bank_new_snrc             C   s@   |   }d}|dkr6| jd|||   d d|   S | jS d S )Ng       @g      ?r   r3   )r  r  )r,   r|  r  r  r   r   r    r    s
    "zSnglInspiral.get_cont_new_snrc             C   s   | j S )N)ra  )r,   r   r   r    get_far  s    zSnglInspiral.get_farc             C   s   | j dk rd| _ d| j  S )Ng&.>g      ?)ra  )r,   r   r   r    get_ifar  s    
zSnglInspiral.get_ifarc             C   s   | j S )N)rh  )r,   r   r   r    r    s    zSnglInspiral.get_lvS5statc             C   sd   t | j|jp`t | j|jp`t | j|jp`t | j|jp`t | j|jp`t | j|jp`t | j|j S )N)cmpr0   endrR  rS  r  r  r   )r,   otherr   r   r    __eq__  s    zSnglInspiral.__eq__c             C   s   | j S )N)r  )r,   r   r   r    r    s    zSnglInspiral.get_endc             C   s
   || _ d S )N)r  )r,   r\   r   r   r    set_end  s    zSnglInspiral.set_endN)r  )r  )r  )r  )r  )r  )r  ) rG   rH   rI   r~   rK  r   r   r   rJ   r  rK   r  r   r  r   r  r  r}  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r    r  A  s2   






r  c               @   s6   e Zd ZdZddddddddddd
ZddiZd	Zd
S )CoincInspiralTableZcoinc_inspiralz	ilwd:charrl   rm   r   )
r   r5   rt   rL  massrT  Zminimum_durationr  rF  combined_farZci_cei_index)r   )r   r5   N)rG   rH   rI   r   r   r   r   r   r   r   r    r    s   r  c               @   sP   e Zd ZdZeej Ze	dZ
eddZdd Zdd Zd	d
 Zdd ZdS )CoincInspiralz
	Example:

	>>> x = CoincInspiral()
	>>> x.instruments = (u"H1", u"L1")
	>>> assert x.ifos == 'H1,L1'
	>>> assert x.instruments == {'H1', 'L1'}
	>>> x.end = LIGOTimeGPS(10)
	>>> x.end
	LIGOTimeGPS(10, 0)
	>>> x.end = None
	>>> print(x.end)
	None
	r5   rt   rL  c             C   s   | j S )N)r  )r,   r   r   r    r    s    zCoincInspiral.get_endc             C   s
   || _ d S )N)r  )r,   r\   r   r   r    r    s    zCoincInspiral.set_endc             C   s   | j S )N)rA   )r,   r   r   r    r|     s    zCoincInspiral.get_ifosc             C   s
   || _ d S )N)rA   )r,   r5   r   r   r    r}     s    zCoincInspiral.set_ifosN)rG   rH   rI   r]   r~   r  r   r   r   r*   rA   rK   r  r  r  r|   r}   r   r   r   r    r    s   
r  sngl_ringdownc               @   s^   e Zd ZdZdddddddddddddddddddddddddZdZed	Zd
Zdd Z	dS )SnglRingdownTabler  z	ilwd:charrl   rm   r   r   )rj   r0   r  rs   r   start_time_gmstr   qualityphaser  r  epsilonZnum_clust_trigsZds2_H1H2Zds2_H1L1Zds2_H1V1Zds2_H2L1Zds2_H2V1Zds2_L1V1r  r  r  Zsigma_sqr   zPRIMARY KEY (event_id)r   )rj   r0   r  c             C   s   dd | D S )Nc             S   s   g | ]}|  qS r   )r   )r#   rv   r   r   r    r   <  s    z/SnglRingdownTable.get_start.<locals>.<listcomp>r   )r,   r   r   r    r   ;  s    zSnglRingdownTable.get_startN)
rG   rH   rI   r   r   rx   SnglRingdownIDrz   r   r   r   r   r   r    r    s:   r  c               @   s2   e Zd Zeej Zdd Zdd Z	dd Z
dS )SnglRingdownc             C   s   t | j| jS )N)r   rs   r   )r,   r   r   r    r   B  s    zSnglRingdown.get_startc             C   s   |j |j | _| _d S )N)r   r   rs   r   )r,   r\   r   r   r    r   E  s    zSnglRingdown.set_startc             C   s<   t | j}|d }|d d }|d }t |t |t |fS )z;
		Return the three pieces of the int_8s-style event_id.
		i ʚ;i )r  r   )r,   int_event_idaslidenumbr   r   r    get_id_partsH  s
    
zSnglRingdown.get_id_partsN)rG   rH   rI   r~   r  r   r   r   r   r   r  r   r   r   r    r  ?  s   r  c               @   sD   e Zd ZdZddddddddddddddddddZddiZd	Zd
S )CoincRingdownTableZcoinc_ringdownz	ilwd:charrl   rm   r   )r   r5   rs   r   r   r  r  r  r  Zchoppedl_snrZsnr_sqZeff_coh_snrZ	null_statrW  Z	snr_ratiorF  r  Zcr_cei_index)r   )r   r5   N)rG   rH   rI   r   r   r   r   r   r   r   r    r  _  s(   r  c               @   s:   e Zd Zeej Zdd Zdd Z	dd Z
dd Zd	S )
CoincRingdownc             C   s   t | j| jS )N)r   rs   r   )r,   r   r   r    r   ~  s    zCoincRingdown.get_startc             C   s   |j |j | _| _d S )N)r   r   rs   r   )r,   r\   r   r   r    r     s    zCoincRingdown.set_startc             C   s   t || _d S )N)r   r5   )r,   r5   r   r   r    r}     s    zCoincRingdown.set_ifosc             C   s
   t | jS )N)r   r5   )r,   r   r   r    r|     s    zCoincRingdown.get_ifosN)rG   rH   rI   r~   r  r   r   r   r   r   r}   r|   r   r   r   r    r  {  s
   r  multi_inspiralc            r   @   s
  e Zd ZdZddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddqZdZed	Zd
ZdddddddZ	dd Z
dd Zdd ZdQddZdd Zdd  Zd!d" Zd#d$ Zd%d& ZdRd(d)Zd*d+ ZdSd,d-Zd.d/ ZdTd0d1Zd2d3 ZdUd4d5Zd6d7 ZdVd8d9ZdWd=d>Zd?d@ ZdAdB ZdCdD ZdEdF Z dGdH Z!dIdJ Z"dKdL Z#dMdN Z$dXdOdPZ%d'S )YMultiInspiralTabler  z	ilwd:charrl   rm   r   r   )qrj   r5   r   rt   rL  rM  rN  rO  r  distanceZeff_dist_h1Zeff_dist_h2
eff_dist_l
eff_dist_g
eff_dist_t
eff_dist_vZeff_dist_h1h2rQ  rR  rS  rT  rV  rX  rW  rY  rZ  r[  r\  r]  r^  r  Zsnr_dofr  r  rj  rk  rl  rm  Z	trace_snrZsnr_h1Zsnr_h2Zsnr_lZsnr_gZsnr_tZsnr_vZ
amp_term_1Z
amp_term_2Z
amp_term_3Z
amp_term_4Z
amp_term_5Z
amp_term_6Z
amp_term_7Z
amp_term_8Z
amp_term_9Zamp_term_10Z
sigmasq_h1Z
sigmasq_h2Z	sigmasq_lZ	sigmasq_gZ	sigmasq_tZ	sigmasq_vZchisq_h1Zchisq_h2Zchisq_lZchisq_gZchisq_tZchisq_vsngl_chisq_dofZbank_chisq_h1Zbank_chisq_h2Zbank_chisq_lZbank_chisq_gZbank_chisq_tZbank_chisq_vsngl_bank_chisq_dofZcont_chisq_h1Zcont_chisq_h2Zcont_chisq_lZcont_chisq_gZcont_chisq_tZcont_chisq_vZsngl_cont_chisq_dofradecrG  rH  inclinationpolarizationZnull_statisticZnull_stat_h1h2Znull_stat_degenr   Z	h1quad_reZ	h1quad_imZ	h2quad_reZ	h2quad_imZ	l1quad_reZ	l1quad_imZ	g1quad_reZ	g1quad_imZ	t1quad_reZ	t1quad_imZ	v1quad_reZ	v1quad_imZcoh_snr_h1h2ZcohSnrSqLocalZautoCorrCohSqZcrossCorrCohSqZautoCorrNullSqZcrossCorrNullSqZampMetricEigenVal1ZampMetricEigenVal2r   zPRIMARY KEY (event_id)r   )rj   r5   r   gZh1h2lr$   v)ZG1ZH1ZH2ZL1ZT1ZV1c             C   s   |dkr|   S |dkr |  S |dkr0|  S |dkr@|  S |dkrP|  S |dkr`|  S |dkrp|  S |dkr| jd	d
S |dkr| jdd
S |dkr| jdd
S |dkr| jdd
S | |	 S d S )Nbestnrr{  Znull_snrZ	coinc_snrrw  rx  ry  Zreduced_chisq_h1Zhl)r0   Zreduced_chisq_h2r  Zreduced_chisq_lr  Zreduced_chisq_vr  )

get_bestnrr  get_null_snrget_coinc_snrr}  r~  r  get_reduced_sngl_chisqr#  r$  )r,   r%  r   r   r    r&  	  s.    zMultiInspiralTable.get_columnc             C   s<   t | r.tt|   d jddd S tg S d S )Nr3   r   )axisg      ?)r9   r'  r$  listget_sngl_snrsr   r8  r  )r,   r   r   r    r  /	  s    z MultiInspiralTable.get_coinc_snrc             C   s   dd | D S )Nc             S   s   g | ]}|  qS r   )r  )r#   rv   r   r   r    r   7	  s    z.MultiInspiralTable.get_end.<locals>.<listcomp>r   )r,   r   r   r    r  6	  s    zMultiInspiralTable.get_end      @      @r  c             C   sN   |  d}|  d| }|dd|||    d|   }t||dk | |S )Nr  z
reduced_%sg      ?r   g      ?)r&  r'  r  )r,   r|  r  r%  r  r  r  r   r   r    r  9	  s
    
 zMultiInspiralTable.get_new_snrc             C   s.   |   d | dd  }d||dk < |d S )z:
		Get the coherent Null SNR for each row in the table.
		r3   r  g        r   g      ?)r  r&  )r,   null_snr_sqr   r   r    r  D	  s    zMultiInspiralTable.get_null_snrc             C   s<   t | r.tt|   d jddd S tg S dS )z:@returns the coincident chisq for each row in the table
		r3   r   )r  g      ?N)r9   r'  r$  r  get_sngl_chisqsr   r8  r  )r,   r   r   r    get_coinc_chisqL	  s    z"MultiInspiralTable.get_coinc_chisqc             C   s&   t t| r| d jpd}|  | S )zR@returns the coincident chisq per degree of freedom for each
		row in the table
		r   r   )r  r9   r  r  )r,   Zdofr   r   r    get_reduced_coinc_chisqU	  s    z*MultiInspiralTable.get_reduced_coinc_chisqc             C   s    |   |   }d||dk < |S )z=@returns the coherent null chisq for each row in the table
		g        r   )r  r}  )r,   null_chisq_sqr   r   r    get_null_chisq\	  s    
z!MultiInspiralTable.get_null_chisqc             C   s   |  d| j|   S )zV
		Get the single-detector SNR of the given instrument for each
		row in the table.
		z
sigmasq_%s)r&  instrument_idupper)r,   r=   r   r   r    get_sigmasqd	  s    zMultiInspiralTable.get_sigmasqNc                sD   t  r:|s$tttt d j}t fdd|D S t S dS )zN
		Return dictionary of single-detector sigmas for each row in the
		table.
		r   c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   u	  s   z2MultiInspiralTable.get_sigmasqs.<locals>.<genexpr>N)r9   r  mapr   r   r5   r   )r,   rA   r   )r,   r    get_sigmasqsl	  s    
zMultiInspiralTable.get_sigmasqsc             C   s   |  d| j|   S )zV
		Get the single-detector SNR of the given instrument for each
		row in the table.
		zsnr_%s)r&  r  r  )r,   r=   r   r   r    get_sngl_snr{	  s    zMultiInspiralTable.get_sngl_snrc                sL   t  r*|dkr*tttt d j}n|dkr6g }t fdd|D S )z=
		Get the single-detector SNRs for each row in the table.
		Nr   c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   	  s   z3MultiInspiralTable.get_sngl_snrs.<locals>.<genexpr>)r9   r  r  r   r   r5   r   )r,   rA   r   )r,   r    r  	  s    z MultiInspiralTable.get_sngl_snrsc             C   s   |  d| j|   S )zY
		Get the single-detector \chi^2 of the given instrument for each
		row in the table.
		zchisq_%s)r&  r  r  )r,   r=   r   r   r    get_sngl_chisq	  s    z!MultiInspiralTable.get_sngl_chisqc                sL   t  r*|dkr*tttt d j}n|dkr6g }t fdd|D S )z?
		Get the single-detector \chi^2 for each row in the table.
		Nr   c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   	  s   z5MultiInspiralTable.get_sngl_chisqs.<locals>.<genexpr>)r9   r  r  r   r   r5   r   )r,   rA   r   )r,   r    r  	  s    z"MultiInspiralTable.get_sngl_chisqsc             C   s   |  d| j|   S )zY
		Get the single-detector \chi^2 of the given instrument for each
		row in the table.
		zbank_chisq_%s)r&  r  r  )r,   r=   r   r   r    get_sngl_bank_chisq	  s    z&MultiInspiralTable.get_sngl_bank_chisqc                sL   t  r*|dkr*tttt d j}n|dkr6g }t fdd|D S )z?
		Get the single-detector \chi^2 for each row in the table.
		Nr   c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   	  s   z:MultiInspiralTable.get_sngl_bank_chisqs.<locals>.<genexpr>)r9   r  r  r   r   r5   r   )r,   rA   r   )r,   r    get_sngl_bank_chisqs	  s    z'MultiInspiralTable.get_sngl_bank_chisqsc             C   s   |  d| j|   S )zY
		Get the single-detector \chi^2 of the given instrument for each
		row in the table.
		zcont_chisq_%s)r&  r  r  )r,   r=   r   r   r    get_sngl_cont_chisq	  s    z&MultiInspiralTable.get_sngl_cont_chisqc                sL   t  r*|dkr*tttt d j}n|dkr6g }t fdd|D S )z?
		Get the single-detector \chi^2 for each row in the table.
		Nr   c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   	  s   z:MultiInspiralTable.get_sngl_cont_chisqs.<locals>.<genexpr>)r9   r  r  r   r   r5   r   )r,   rA   r   )r,   r    get_sngl_cont_chisqs	  s    z'MultiInspiralTable.get_sngl_cont_chisqs      @      4@皙?c                s    fdd| D S )z8
		Get the BestNR statistic for each row in the table
		c          	      s    g | ]}|j  d qS ))r|  r  null_snr_thresholdnull_grad_threshnull_grad_val)r  )r#   rv   )r|  r  r  r  r  r   r    r   	  s   z1MultiInspiralTable.get_bestnr.<locals>.<listcomp>r   )r,   r|  r  r  r  r  r   )r|  r  r  r  r  r    r  	  s    zMultiInspiralTable.get_bestnrc             C   s
   |  dS )Nr  )r&  )r,   r   r   r    getstat	  s    zMultiInspiralTable.getstatc             C   sH   |   }|   }x2| D ]*}| }||kr6|| q|| qW |S )N)r-  r  r   )r,   r.  r2  r/  rv   r0  r   r   r    r1  	  s    
zMultiInspiralTable.vetoc             C   sH   |   }|   }x2| D ]*}| }||kr6|| q|| qW |S )zk
		Return the inverse of what veto returns, i.e., return the triggers
		that lie within a given seglist.
		)r-  r  r   )r,   r.  r2  r/  rv   r0  r   r   r    r2  	  s    
zMultiInspiralTable.vetoedc                s$   |   }| fdd| D  |S )z
		Return the triggers with a specific slide number.
		@param slide_num: the slide number to recover (contained in the event_id)
		c             3   s   | ]}|   kr|V  qd S )N)r  )r#   rv   )r  r   r    r%   	  s    z.MultiInspiralTable.getslide.<locals>.<genexpr>)r-  r   )r,   r  r  r   )r  r    r  	  s    zMultiInspiralTable.getslidec             C   s   |  d|  d S )zH@returns the chisq per degree of freedom for each row in
		this table
		r  r  )r&  )r,   r   r   r    r}  	  s    z$MultiInspiralTable.get_reduced_chisqc             C   s   |  d|  d S )zM@returns the bank chisq per degree of freedom for each row in
		this table
		rj  rk  )r&  )r,   r   r   r    r~  
  s    z)MultiInspiralTable.get_reduced_bank_chisqc             C   s   |  d|  d S )zZ@returns the auto (continuous) chisq per degree of freedom
		for each row in this table
		rl  rm  )r&  )r,   r   r   r    r  
  s    z)MultiInspiralTable.get_reduced_cont_chisqc             C   s"   |  d| j|   |  d S )zX@returns the single-detector chisq per degree of freedom for
		each row in this table
		zchisq_%sr  )r&  r  r  )r,   r=   r   r   r    r  
  s    z)MultiInspiralTable.get_reduced_sngl_chisqc             C   s   |  }|dkr| |}n>|dkr0| |}n*|dkrB|  }nt| |t| d|  }| d}|dd|||    d|   }t||dk | |S )	Nr  rj  rl  z%s_dofr  g      ?r   g      ?)r!  r  get_reduced_sngl_bank_chisqr  rB   r&  r'  r  )r,   r0   r%  r|  r  r  r  r  r   r   r    get_sngl_new_snr
  s    

 z#MultiInspiralTable.get_sngl_new_snr)r  r  r  )N)N)N)N)N)r  r  r  r  r  )r  r  r  )&rG   rH   rI   r   r   rx   MultiInspiralIDrz   r   r  r&  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r1  r2  r  r}  r~  r  r  r  r   r   r   r    r    s(  

	




 

	r  c               @   s   e Zd Zeej ZejZdd Z	dd Z
dd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zd8ddZd9ddZd:ddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd;d5d6Z d7S )<MultiInspiralc             C   s   | j | j S )N)r  r  )r,   r   r   r    r}  *
  s    zMultiInspiral.get_reduced_chisqc             C   s   | j | j S )N)rj  rk  )r,   r   r   r    r~  -
  s    z$MultiInspiral.get_reduced_bank_chisqc             C   s   | j | j S )N)rl  rm  )r,   r   r   r    r  0
  s    z$MultiInspiral.get_reduced_cont_chisqc             C   s"   t | d| j|   t | d S )Nzchisq_%sr  )rB   r  r  )r,   r=   r   r   r    r  3
  s    z$MultiInspiral.get_reduced_sngl_chisqc             C   s"   t | d| j|   t | d S )Nzbank_chisq_%sr  )rB   r  r  )r,   r=   r   r   r    r  8
  s    z)MultiInspiral.get_reduced_sngl_bank_chisqc             C   s"   t | d| j|   t | d S )Nzcont_chisq_%sr  )rB   r  r  )r,   r=   r   r   r    get_reduced_sngl_cont_chisq=
  s    z)MultiInspiral.get_reduced_sngl_cont_chisqc             C   s   t | j| jS )N)r   rt   rL  )r,   r   r   r    r  C
  s    zMultiInspiral.get_endc             C   s
   t | jS )z3
		Return a set of the instruments for this row.
		)r   r5   )r,   r   r   r    r|   F
  s    zMultiInspiral.get_ifosc             C   s"   t t|   d  d S )z*
		Get the coincident SNR for this row.
		r3   g      ?)r'  r$  r  r  r   r8  )r,   r   r   r    r  L
  s    zMultiInspiral.get_coinc_snrc             C   s"   t t|   d  d S )z-@returns the coincident chisq for this row
		r3   g      ?)r'  r$  r  r  r   r8  )r,   r   r   r    r  S
  s    zMultiInspiral.get_coinc_chisqc             C   s   |   | j S )zC@returns the coincident chisq per degree of freedom for this row
		)r  r  )r,   r   r   r    r  Y
  s    z%MultiInspiral.get_reduced_coinc_chisq      @      @r  c             C   s   |  }|dkr|  }n<|dkr,|  }n*|dkr>|  }nt| |t| d|  }|dkr| jd|||   d d|   S | jS d S )Nr  rj  rl  z%s_dofg      ?r   r3   )r!  r}  r~  r  rB   r  )r,   r|  r  r%  r  r   r   r    r  ^
  s    


zMultiInspiral.get_new_snrc             C   sz   |  }|dkr| |}n&|dkr0| |}n|dkrB| |}|dkrp| |d|||   d d|   S | jS d S )Nr  rj  rl  g      ?r   r3   )r!  r  r  r  r  r  )r,   r0   r%  r|  r  r  r   r   r    r  n
  s    
zMultiInspiral.get_sngl_new_snrc                s"   t  fddtjD S )zA@returns a dictionary of single-detector newSNRs for this row.
		c             3   s$   | ]}|j | d fV  qdS ))r%  r|  r  N)r  )r#   r0   )r%  r|  r  r,   r   r    r%   
  s   z2MultiInspiral.get_sngl_new_snrs.<locals>.<genexpr>)r   r   r5   )r,   r%  r|  r  r   )r%  r|  r  r,   r    get_sngl_new_snrs}
  s    zMultiInspiral.get_sngl_new_snrsc             C   s@   t t|   d  | jd  }|dk r4dS |d S dS )z-
		Get the coherent Null SNR for this row.
		r3   r   g      ?N)r'  r$  r  r  r   r8  r  )r,   r  r   r   r    r  
  s
    zMultiInspiral.get_null_snrc             C   s$   |   |   }|dk rdS |S dS )z0@returns the coherent null chisq for this row
		r   N)r  r}  )r,   r  r   r   r    r  
  s
    
zMultiInspiral.get_null_chisqc             C   s   t | d| j|   S )zI
		Get the single-detector SNR for the given instrument for this
		row
		zsnr_%s)rB   r  r  )r,   r=   r   r   r    r  
  s    zMultiInspiral.get_sngl_snrc                s   t  fddt jD S )z?
		Return a dictionary of single-detector SNRs for this row.
		c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   
  s    z.MultiInspiral.get_sngl_snrs.<locals>.<genexpr>)r   r   r5   )r,   r   )r,   r    r  
  s    zMultiInspiral.get_sngl_snrsc             C   s   t | d| j|   S )zM@returns the single-detector chisq for the given instrument
		for this row
		zchisq_%s)rB   r  r  )r,   r=   r   r   r    r  
  s    zMultiInspiral.get_sngl_chisqc                s   t  fddt jD S )z@@returns a dictionary of single-detector chisqs for this row.
		c             3   s   | ]}|  |fV  qd S )N)r  )r#   r0   )r,   r   r    r%   
  s    z0MultiInspiral.get_sngl_chisqs.<locals>.<genexpr>)r   r   r5   )r,   r   )r,   r    r  
  s    zMultiInspiral.get_sngl_chisqsc             C   s   t || _dS )z
		Serialize a sequence of instruments into the ifos
		attribute.  The instrument names must not contain the ","
		character.
		N)r   r5   )r,   rA   r   r   r    r}   
  s    zMultiInspiral.set_ifosc             C   s<   t | j}|d }|d d }|d }t |t |t |fS )z;
		Return the three pieces of the int_8s-style event_id.
		i ʚ;i )r  r   )r,   r  r  r  r  r   r   r    r  
  s
    
zMultiInspiral.get_id_partsc             C   s"   |   \}}}|dkrd| }|S )z.
		Return the slide-number for this trigger
		i  )r  )r,   r  Zslide_numberr  r   r   r    r  
  s    zMultiInspiral.get_slide_number      @      4@皙?c             C   sd   | j ||dd}t|  dk r$|S | j|kr@|| j| | 7 }|  |kr`|d|   |  }|S )z/
		Return the BestNR statistic for this row.
		r  )r|  r  r%     r   )r  r9   r|   r  r  )r,   r|  r  r  r  r  r  r   r   r    r  
  s    
zMultiInspiral.get_bestnrN)r  r  r  )r  r  r  )r  r  r  )r  r  r  r  r  )!rG   rH   rI   r~   r  r   r   r   r  r}  r~  r  r  r  r  r  r|   r  r  r  r  r  r  r  r  r  r  r  r  r}   r  r  r  r   r   r   r    r  &
  s6   




	 r  sim_inspiralsimulation_idc            ;   @   s   e Zd ZdZddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd:ZdZed	Zd
Zdd Z	dddZ
dddZdd ZdddZdd Zdd ZdddZdS ) SimInspiralTabler   z	ilwd:charrl   rm   r   r   ):rj   waveformgeocent_end_timegeocent_end_time_nsZ
h_end_timeZh_end_time_nsZ
l_end_timeZl_end_time_nsZ
g_end_timeZg_end_time_nsZ
t_end_timeZt_end_time_nsZ
v_end_timeZv_end_time_nsrM  sourcerR  rS  rT  rV  r  	longitudelatituder  rQ  r  r_  r`  ra  rb  rc  rd  re  rf  rg  rh  ro  rp  rq  rr  rs  rt  Ztheta0Zphi0Zf_lowerri  
eff_dist_hr  r  r  r  Znumrel_mode_minZnumrel_mode_maxZnumrel_dataZ	amp_orderZtaperZbandpassr  zPRIMARY KEY (simulation_id)r   )rj   r  r  c             C   s   |dks|dkr|   S |dd dkrV|dd dkrVt|dkrV|d }| |S |d	krh| d
S |dkrz| dS |dks|dkr| d }| d }|| S | | S d S )NZ
chirp_distr  r      Zchirp_eff_dist   )_hZ_lZ_gZ_t_vr  r   r  r3   Z
total_massrU  rR  rS  )get_chirp_distr9   r  get_spin_magr#  r$  )r,   r%  sitem1m2r   r   r    r&  6  s    ,


zSimInspiralTable.get_columnffffff?c             C   s"   |  d}|  d}t|||S )NrT  r  )r&  r  r  )r,   r  rT  r  r   r   r    r  H  s    

zSimInspiralTable.get_chirp_distc             C   s&   |  d}|  d| }t|||S )NrT  Z	eff_dist_)r&  r  r  )r,   r  r  rT  r  r   r   r    r  M  s    
z#SimInspiralTable.get_chirp_eff_distc             C   s^   |  dt| d }|  dt| d }|  dt| d }|d |d  |d  d S )Nr  xyzr3   g      ?)r&  r   )r,   objectnumbersxZsyszr   r   r    r  R  s    zSimInspiralTable.get_spin_magNc                s&   |   }| fdd| D  |S )Nc             3   s    | ]}|  kr|V  qd S )N)r  )r#   rv   )r.  r  r   r    r%   Z  s    z(SimInspiralTable.veto.<locals>.<genexpr>)r-  r   )r,   r.  r  r/  r   )r.  r  r    r1  X  s    zSimInspiralTable.vetoc             C   sH   |   }|   }x2| D ]*}| }||kr6|| q|| qW |S )N)r-  r  r   )r,   r.  r2  r/  rv   r0  r   r   r    r1  ]  s    
c             C   sH   |   }|   }x2| D ]*}| }||kr6|| q|| qW |S )zk
		Return the inverse of what veto returns, i.e., return the triggers
		that lie within a given seglist.
		)r-  r  r   )r,   r.  r2  r/  rv   r0  r   r   r    r2  h  s    
zSimInspiralTable.vetoedc                s   t  fdd| D S )Nc                s   g | ]}|j  d qS ))r  )r  )r#   rv   )r  r   r    r   x  s    z,SimInspiralTable.get_end.<locals>.<listcomp>)r'  r$  )r,   r  r   )r  r    r  w  s    zSimInspiralTable.get_end)r  )r  )N)N)rG   rH   rI   r   r   rx   SimInspiralIDrz   r   r&  r  r  r  r1  r2  r  r   r   r   r    r  
  s   


r  c               @   s   e Zd ZdZeej Ze	dddZ
edd Zejdd Zedd	 Zejd
d	 Zedd Zejdd Zdd Zdd Zdd Zdd Zd ddZdd Zd!ddZdd ZdS )"SimInspiralam  
	Example:

	>>> x = SimInspiral()
	>>> x.ra_dec = 0., 0.
	>>> x.ra_dec
	(0.0, 0.0)
	>>> x.ra_dec = None
	>>> print(x.ra_dec)
	None
	>>> x.time_geocent = None
	>>> print(x.time_geocent)
	None
	>>> print(x.end_time_gmst)
	None
	>>> x.time_geocent = LIGOTimeGPS(6e8)
	>>> print(x.time_geocent)
	600000000
	>>> print(numpy.around(x.end_time_gmst, 8))
	-2238.39417156
	r  r  rM  c             C   s$   | j d kr| jd krd S | j | jfS )N)r  r  )r,   r   r   r    ra_dec  s    zSimInspiral.ra_decc             C   s&   |d krd  | _ | _n|\| _ | _d S )N)r  r  )r,   radecr   r   r    r    s    c             C   s<   | j d kr"| jd kr"| jd kr"d S tj| j | j| jfddS )Nr  )r  )ro  rp  rq  r'  r  )r,   r   r   r    r    s    zSimInspiral.spin1c             C   s.   |d krd\| _ | _| _n|\| _ | _| _d S )N)NNN)ro  rp  rq  )r,   r  r   r   r    r    s    c             C   s<   | j d kr"| jd kr"| jd kr"d S tj| j | j| jfddS )Nr  )r  )rr  rs  rt  r'  r  )r,   r   r   r    r    s    zSimInspiral.spin2c             C   s.   |d krd\| _ | _| _n|\| _ | _| _d S )N)NNN)rr  rs  rt  )r,   r  r   r   r    r    s    c             C   s4   | j ||  }| j\}}|ttj| j||| S )a  
		Return the "time" of the injection, delay corrected for the
		displacement from the geocentre to the given instrument.

		NOTE:  this method does not account for the rotation of the
		Earth that occurs during the transit of the plane wave from
		the detector to the geocentre.  That is, it is assumed the
		Earth is in the same orientation with respect to the
		celestial sphere when the wave passes through the detector
		as when it passes through the geocentre.  The Earth rotates
		by about 1.5 urad during the 21 ms it takes light to travel
		the radius of the Earth, which corresponds to 10 m of
		displacement at the equator, or 33 light-ns.  Therefore,
		the failure to do a proper retarded time calculation here
		results in errors as large as 33 ns.  This is insignificant
		for burst searches, but be aware that this approximation is
		being made if the return value is used in other contexts.
		)time_geocentr  rb   TimeDelayFromEarthCentercached_detector_by_prefixlocation)r,   r=   
offsetvect	t_geocentr  r  r   r   r    time_at_instrument  s    
zSimInspiral.time_at_instrumentc             C   s   t dt | jS )NzSSimInspiral.get_time_geocent() is deprecated.  use SimInspiral.time_geocent instead)warningswarnDeprecationWarningr  )r,   r   r   r    get_time_geocent  s    zSimInspiral.get_time_geocentc             C   s   t dt || _d S )NzSSimInspiral.set_time_geocent() is deprecated.  use SimInspiral.time_geocent instead)r&  r'  r(  r  )r,   r\   r   r   r    set_time_geocent  s    zSimInspiral.set_time_geocentc             C   s   t dt | jS )NzGSimInspiral.get_ra_dec() is deprecated.  use SimInspiral.ra_dec instead)r&  r'  r(  r  )r,   r   r   r    
get_ra_dec  s    zSimInspiral.get_ra_decNc             C   sD   t dt |d kr| jS tt| d|  t| d|  S d S )NznSimInspiral.get_end() is deprecated.  use SimInspiral.time_geocent or SimInspiral.time_at_instrument() insteadz%s_end_timez%s_end_time_ns)r&  r'  r(  r  r   rB   r!  )r,   r  r   r   r    r    s    zSimInspiral.get_endc             C   s   t | d|d   S )Nzeff_dist_%sr   )rB   r!  )r,   r=   r   r   r    get_eff_dist  s    zSimInspiral.get_eff_distffffff?c             C   s   t | || j|S )N)r  r  r,  rT  )r,   r=   r  r   r   r    r    s    zSimInspiral.get_chirp_eff_distc             C   s    t | d| }tt||S )Nzspin%d)rB   rW   sqrtr'  dot)r,   r  rS   r   r   r    r    s    zSimInspiral.get_spin_mag)N)r-  )rG   rH   rI   r]   r~   r  r   r   r   r^   r  r   r  r   r  r  r%  r)  r*  r+  r  r,  r  r  r   r   r   r    r  {  s"   

r  	sim_burstc               @   sN   e Zd ZdZdddddddddddddddddddddZdZed	Zd
ZdS )SimBurstTabler0  z	ilwd:charrl   r   rm   Zint_8u)rj   r  r  r  psitime_geocent_gpstime_geocent_gps_nstime_geocent_gmstr   r   r   r   Zpol_ellipse_angleZpol_ellipse_er  r  Zegw_over_rsquaredZwaveform_numberr   r  zPRIMARY KEY (simulation_id)r   )rj   r  N)	rG   rH   rI   r   r   rx   
SimBurstIDrz   r   r   r   r   r    r1    s0   r1  c               @   sn   e Zd ZdZeej Ze	dddZ
edd Zejdd Zdd	 Zd
d Zdd Zdd ZdddZdS )SimBurstar  
	Example:

	>>> x = SimBurst()
	>>> x.ra_dec = 0., 0.
	>>> x.ra_dec
	(0.0, 0.0)
	>>> x.ra_dec = None
	>>> print(x.ra_dec)
	None
	>>> x.time_geocent = None
	>>> print(x.time_geocent)
	None
	>>> print(x.time_geocent_gmst)
	None
	>>> x.time_geocent = LIGOTimeGPS(6e8)
	>>> print(x.time_geocent)
	600000000
	>>> print(numpy.around(x.time_geocent_gmst, 8))
	-2238.39417156
	r3  r4  r5  c             C   s$   | j d kr| jd krd S | j | jfS )N)r  r  )r,   r   r   r    r  ?  s    zSimBurst.ra_decc             C   s&   |d krd  | _ | _n|\| _ | _d S )N)r  r  )r,   r  r   r   r    r  E  s    c             C   s4   | j ||  }| j\}}|ttj| j||| S )a  
		Return the "time" of the injection, delay corrected for the
		displacement from the geocentre to the given instrument.

		NOTE:  this method does not account for the rotation of the
		Earth that occurs during the transit of the plane wave from
		the detector to the geocentre.  That is, it is assumed the
		Earth is in the same orientation with respect to the
		celestial sphere when the wave passes through the detector
		as when it passes through the geocentre.  The Earth rotates
		by about 1.5 urad during the 21 ms it takes light to travel
		the radius of the Earth, which corresponds to 10 m of
		displacement at the equator, or 33 light-ns.  Therefore,
		the failure to do a proper retarded time calculation here
		results in errors as large as 33 ns.  This is insignificant
		for burst searches, but be aware that this approximation is
		being made if the return value is used in other contexts.
		)r  r  rb   r   r!  r"  )r,   r=   r#  r$  r  r  r   r   r    r%  L  s    
zSimBurst.time_at_instrumentc             C   s   | j S )N)r  )r,   r   r   r    r)  g  s    zSimBurst.get_time_geocentc             C   s
   || _ d S )N)r  )r,   r\   r   r   r    r*  j  s    zSimBurst.set_time_geocentc             C   s   | j S )N)r  )r,   r   r   r    r+  m  s    zSimBurst.get_ra_decNc             C   s&   |dkr| j S |d }| ||diS )z
		Do not use this method:  use .time_at_instrument() if that's what you want, or use .time_geocent if that's what you want.

		Also ... this doesn't return the *end time*, it returns the *PEAK TIME*.  You've been warned.
		N1g        )r  r%  )r,   r  r=   r   r   r    r  p  s    zSimBurst.get_end)N)rG   rH   rI   r]   r~   r1  r   r   r   r^   r  r   r  r   r%  r)  r*  r+  r  r   r   r   r    r7  %  s   r7  sim_ringdownc            !   @   sf   e Zd ZdZddddddddddddddddddddddddddddddddd ZdZed	Zd
ZdS )SimRingdownTabler9  z	ilwd:charrl   rm   r   r   ) rj   r  coordinatesgeocent_start_timegeocent_start_time_nsZh_start_timeZh_start_time_nsZl_start_timeZl_start_time_nsZv_start_timeZv_start_time_nsr  r  r  r  r  r  r   r  r  r  r  r  r  r	  r  r  r  Zhrss_hZhrss_lZhrss_vr  zPRIMARY KEY (simulation_id)r   )rj   r  r;  N)	rG   rH   rI   r   r   rx   SimRingdownIDrz   r   r   r   r   r    r:    sH   r:  c               @   s$   e Zd Zeej ZdddZdS )SimRingdownNc             C   s@   |st | j| jS |d  }t t| |d t| |d S d S )Nr   Z_start_timeZ_start_time_ns)r   r<  r=  r!  rB   )r,   r  r   r   r    r     s    zSimRingdown.get_start)N)	rG   rH   rI   r~   r:  r   r   r   r   r   r   r   r    r?    s   r?  
summ_valuesumm_value_idc               @   sD   e Zd ZdZddddddddddddddddZdZedZd	Zd
S )SummValueTabler@  z	ilwd:charrl   rm   r   )rA  rn   rj   frameset_groupsegment_def_idrs   r   rt   rL  r0   r+   r   errorZintvaluerp   zPRIMARY KEY (summ_value_id)r   )rn   rj   r0   r+   rp   N)	rG   rH   rI   r   r   rx   SummValueIDrz   r   r   r   r   r    rB    s&   rB  c               @   sD   e Zd ZdZeej Ze	dZ
eddZeddZeddZd	S )
	SummValuea9  
	Example:

	>>> x = SummValue()
	>>> x.instruments = (u"H1", u"L1")
	>>> assert x.ifo == 'H1,L1'
	>>> assert x.instruments == {'H1', 'L1'}
	>>> x.start = LIGOTimeGPS(0)
	>>> x.end = LIGOTimeGPS(10)
	>>> x.segment
	segment(LIGOTimeGPS(0, 0), LIGOTimeGPS(10, 0))
	>>> x.segment = None
	>>> print(x.segment)
	None
	r0   rs   r   rt   rL  re   r  N)rG   rH   rI   r]   r~   rB  r   r   r   r*   rA   rK   re   r  rd   rg   r   r   r   r    rG    s   

rG  sim_inst_paramsc               @   s&   e Zd ZdZdddddZedZdS )SimInstParamsTablerH  z	ilwd:charrl   r   )r  r+   rp   r   r   N)rG   rH   rI   r   r   SimInstParamsIDrz   r   r   r   r    rI    s   rI  c               @   s   e Zd Zeej ZdS )SimInstParamsN)rG   rH   rI   r~   rI  r   r   r   r   r   r   r    rK    s   rK  c               @   s0   e Zd ZdZddddddddddddddZdS )StochasticTableZ
stochasticz	ilwd:charrl   rm   r   )rj   ifo_oneifo_twochannel_onechannel_twors   r   r   Zduration_nsf_minf_maxZcc_statZcc_sigmaN)rG   rH   rI   r   r   r   r   r   r    rL  (  s   rL  c               @   s   e Zd Zeej ZdS )
StochasticN)rG   rH   rI   r~   rL  r   r   r   r   r   r   r    rS  ;  s   rS  c               @   s0   e Zd ZdZddddddddddddddZdS )StochSummTableZ	stochsummz	ilwd:charrl   rm   r   )rj   rM  rN  rO  rP  rs   r   rt   rL  rQ  rR  Zy_optrE  N)rG   rH   rI   r   r   r   r   r   r    rT  K  s   rT  c               @   s   e Zd Zeej ZdS )	StochSummN)rG   rH   rI   r~   rT  r   r   r   r   r   r   r    rU  ^  s   rU  c            )   @   sf   e Zd ZdZddddddddddddddddddddddddddddddddddddddddd(ZdS )ExtTriggersTableZexternal_triggerz	ilwd:charrl   rm   r   )(rj   Zdet_altsZdet_bandZdet_fluenceZdet_fluence_intZdet_nameZdet_peakZdet_peak_intZdet_snrZ
email_timeZ	event_decZevent_dec_errZevent_epochZevent_err_typeZevent_raZevent_ra_errrs   r   Z
event_typeZevent_zZevent_z_errZnotice_commentsZ	notice_idZnotice_sequenceZnotice_timeZnotice_typeZ
notice_urlZobs_fov_decZobs_fov_dec_widthZ
obs_fov_raZobs_fov_ra_widthZobs_loc_eleZobs_loc_latZobs_loc_longZligo_fave_lhoZligo_fave_lloZ
ligo_delayZevent_number_gcnZevent_number_grbZevent_statusN)rG   rH   rI   r   r   r   r   r   r    rV  r  sR   rV  c               @   s   e Zd Zeej ZdS )ExtTriggersN)rG   rH   rI   r~   rV  r   r   r   r   r   r   r    rW    s   rW  filterr   c               @   s0   e Zd ZdZddddddddZdZedZdS )	FilterTablerX  z	ilwd:charrl   rm   )rj   rn   rs   Zfilter_namer   Z	param_setrp   zPRIMARY KEY (filter_id)r   N)rG   rH   rI   r   r   rx   FilterIDrz   r   r   r   r    rY    s   rY  c               @   s   e Zd Zeej ZdS )FilterN)rG   rH   rI   r~   rY  r   r   r   r   r   r   r    r[    s   r[  rg   
segment_idc            
   @   s8   e Zd ZdZdddddddddd	ZdZedZdZdS )	SegmentTablerg   rm   z	ilwd:char)	r   rj   r\  rs   r   rt   rL  rD  segment_def_cdbzPRIMARY KEY (segment_id)r   )rj   N)	rG   rH   rI   r   r   rx   	SegmentIDrz   r   r   r   r   r    r]    s   r]  c                   s   e Zd ZdZeej Ze	ddZ
e	ddZeddZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Z fddZdd Zdd Z  ZS )Segmenta  
	Example:

	>>> x = Segment()
	>>> x.start = LIGOTimeGPS(0)
	>>> x.end = LIGOTimeGPS(10)
	>>> x.segment
	segment(LIGOTimeGPS(0, 0), LIGOTimeGPS(10, 0))
	>>> x.segment = None
	>>> print(x.segment)
	None
	>>> print(x.start)
	None
	>>> # non-LIGOTimeGPS times are converted to LIGOTimeGPS
	>>> x.segment = (20, 30.125)
	>>> x.end
	LIGOTimeGPS(30, 125000000)
	>>> # initialization from a tuple or with arguments
	>>> Segment((20, 30)).segment
	segment(LIGOTimeGPS(20, 0), LIGOTimeGPS(30, 0))
	>>> Segment(20, 30).segment
	segment(LIGOTimeGPS(20, 0), LIGOTimeGPS(30, 0))
	>>> # use as a segment object in segmentlist operations
	>>> from ligo import segments
	>>> x = segments.segmentlist([Segment(0, 10), Segment(20, 30)])
	>>> abs(x)
	LIGOTimeGPS(20, 0)
	>>> y = segments.segmentlist([Segment(5, 15), Segment(25, 35)])
	>>> abs(x & y)
	LIGOTimeGPS(10, 0)
	>>> abs(x | y)
	LIGOTimeGPS(30, 0)
	>>> 8.0 in x
	True
	>>> 12 in x
	False
	>>> Segment(2, 3) in x
	True
	>>> Segment(2, 12) in x
	False
	>>> segments.segment(2, 3) in x
	True
	>>> segments.segment(2, 12) in x
	False
	>>> # make sure results are segment table row objects
	>>> segments.segmentlist(map(Segment, x & y))	# doctest: +ELLIPSIS
	[<glue.ligolw.lsctables.Segment object at 0x...>, <glue.ligolw.lsctables.Segment object at 0x...>]

	This implementation uses a non-standard extension to encode
	infinite values for boundaries:  the second and nanosecond
	components are both set to 0x7FFFFFFF or 0xFFFFFFFF to indicate
	positive resp. negative infinity.  For this reason, "denormalized"
	LIGOTimeGPS objects (objects whose nanoseconds fields contain
	values exceeding +/-999999999) are disallowed for use with this
	class.

	Example:

	>>> x = Segment()
	>>> # OK
	>>> x.start = -segments.infinity()
	>>> # also OK
	>>> x.start = float("-inf")
	>>> # infinite boundaries always returned as segments.infinity
	>>> # instances
	>>> x.start
	-infinity
	>>> x.end = float("+inf")
	>>> x.segment
	segment(-infinity, infinity)
	rs   r   rt   rL  re   r  c             C   s   | j S )z/
		Return the segment described by this row.
		)rg   )r,   r   r   r    r<   6  s    zSegment.getc             C   s
   || _ dS )z,
		Set the segment described by this row.
		N)rg   )r,   rg   r   r   r    r6   <  s    zSegment.setc             C   s
   t | jS )N)r[   rg   )r,   r   r   r    __abs__D  s    zSegment.__abs__c             C   s
   | j |k S )N)rg   )r,   r  r   r   r    __lt__G  s    zSegment.__lt__c             C   s
   | j |kS )N)rg   )r,   r  r   r   r    r  J  s    zSegment.__eq__c             C   s
   || j kS )N)rg   )r,   r  r   r   r    __contains__M  s    zSegment.__contains__c             C   s
   | j | S )N)rg   )r,   ir   r   r    __getitem__P  s    zSegment.__getitem__c                sF   |rt t| jf | |rBy
|| _W n tk
r@   |\| _Y nX d S )N)r_   r`  r-   rg   r?   )r,   argsr   )ra   r   r    r-   S  s    
zSegment.__init__c             C   s
   t | jS )N)r9   rg   )r,   r   r   r    __len__^  s    zSegment.__len__c             C   s
   t | jS )N)boolrg   )r,   r   r   r    __nonzero__a  s    zSegment.__nonzero__)rG   rH   rI   r]   r~   r]  r   r   r   rK   re   r  rd   rg   r<   r6   ra  rb  r  rc  re  r-   rg  ri  rc   r   r   )ra   r    r`    s   H


r`  segment_definerrD  c            	   @   s6   e Zd ZdZdddddddddZdZedZdZd	S )
SegmentDefTablerj  rm   z	ilwd:charrl   )r   rj   rD  r5   r+   ro   rp   insertion_timezPRIMARY KEY (segment_def_id)r   )rj   N)	rG   rH   rI   r   r   rx   SegmentDefIDrz   r   r   r   r   r    rk  t  s   rk  c               @   s6   e Zd ZdZeej Ze	dZ
dd Zdd ZdS )
SegmentDefz
	Example:

	>>> x = SegmentDef()
	>>> x.instruments = (u"H1", u"L1")
	>>> assert x.ifos == 'H1,L1'
	>>> assert x.instruments =={'H1', 'L1'}
	r5   c             C   s   | j S )z3
		Return a set of the instruments for this row.
		)rA   )r,   r   r   r    r|     s    zSegmentDef.get_ifosc             C   s
   || _ dS )z
		Serialize a sequence of instruments into the ifos
		attribute.  The instrument names must not contain the ","
		character.
		N)rA   )r,   rA   r   r   r    r}     s    zSegmentDef.set_ifosN)rG   rH   rI   r]   r~   rk  r   r   r   r*   rA   r|   r}   r   r   r   r    rn    s
   rn  segment_summarysegment_sum_idc               @   sD   e Zd ZdZddddddddddd
ZdZedZdZdd
dZ	d	S )SegmentSumTablero  rm   z	ilwd:charrl   )
r   rj   rp  rs   r   rt   rL  rp   rD  r^  zPRIMARY KEY (segment_sum_id)r   )rj   rD  Nc                s4    dkrt dd | D S t  fdd| D S )a  
		Return a segmentlist object describing the times spanned by
		the segments carrying the given segment_def_id.  If
		segment_def_id is None then all segments are returned.

		Note:  the result is not coalesced, the segmentlist
		contains the segments as they appear in the table.
		Nc             s   s   | ]}|j V  qd S )N)rg   )r#   rv   r   r   r    r%     s    z&SegmentSumTable.get.<locals>.<genexpr>c             3   s   | ]}|j  kr|jV  qd S )N)rD  rg   )r#   rv   )rD  r   r    r%     s    )r   r   )r,   rD  r   )rD  r    r<     s    	zSegmentSumTable.get)N)
rG   rH   rI   r   r   rx   SegmentSumIDrz   r   r<   r   r   r   r    rq    s   rq  c               @   s   e Zd Zeej ZdS )
SegmentSumN)rG   rH   rI   r~   rq  r   r   r   r   r   r   r    rs    s   rs  
time_slider   c               @   sH   e Zd ZdZdddddZdZedZdZd	d
 Z	dd Z
dddZdS )TimeSlideTablert  z	ilwd:charrl   r   )rj   r   r=   offsetz'PRIMARY KEY (time_slide_id, instrument)r   )rj   r   r=   c             C   sn   ddl m} i }xX| D ]P}|j|kr0| ||j< |j||j krTtd|j|jf |j||j |j< qW |S )zI
		Return a ditionary mapping time slide IDs to offset
		dictionaries.
		r   )offsetvectorz'%s': duplicate instrument '%s')Zlalburst.offsetvectorrw  r   r=   r   rv  )r,   rw  r   rv   r   r   r    as_dict  s    

zTimeSlideTable.as_dictc             C   sN   |   }x@| D ]4\}}|  }|j|_||_||_||_| | qW |S )ad  
		Append rows describing an instrument --> offset mapping to
		this table.  offsetvect is a dictionary mapping instrument
		to offset.  process should be the row in the process table
		on which the new time_slide table rows will be blamed (or
		any object with a process_id attribute).  The return value
		is the time_slide_id assigned to the new rows.
		)r   r   r   rj   r   r=   rv  r   )r,   r#  ri   r   r=   rv  rv   r   r   r    append_offsetvector   s    	z"TimeSlideTable.append_offsetvectorNFc                s   |r  fdd|    D }n fdd|    D }t|dkrb|rR|d S tdt  t|dkrv|d S |dkrtdt  |  |S )	af  
		Return the time_slide_id corresponding to the offset vector
		described by offsetvect, a dictionary of instrument/offset
		pairs.

		If the optional create_new argument is None (the default),
		then the table must contain a matching offset vector.  The
		return value is the ID of that vector.  If the table does
		not contain a matching offset vector then KeyError is
		raised.

		If the optional create_new argument is set to a Process
		object (or any other object with a process_id attribute),
		then if the table does not contain a matching offset vector
		a new one will be added to the table and marked as having
		been created by the given process.  The return value is the
		ID of the (possibly newly created) matching offset vector.

		If the optional superset_ok argument is False (the default)
		then an offset vector in the table is considered to "match"
		the requested offset vector only if they contain the exact
		same set of instruments.  If the superset_ok argument is
		True, then an offset vector in the table is considered to
		match the requested offset vector as long as it provides
		the same offsets for the same instruments as the requested
		vector, even if it provides offsets for other instruments
		as well.

		More than one offset vector in the table might match the
		requested vector.  If the optional nonunique_ok argument is
		False (the default), then KeyError will be raised if more
		than one offset vector in the table is found to match the
		requested vector.  If the optional nonunique_ok is True
		then the return value is the ID of one of the matching
		offset vectors selected at random.
		c                s2   g | ]*\}} t  fd d| D kr|qS )c             3   s"   | ]\}}| kr||fV  qd S )Nr   )r#   r=   rv  )r#  r   r    r%   :  s    z>TimeSlideTable.get_time_slide_id.<locals>.<listcomp>.<genexpr>)r   r   )r#   idr   )r#  r   r    r   :  s    z4TimeSlideTable.get_time_slide_id.<locals>.<listcomp>c                s   g | ]\}} |kr|qS r   r   )r#   rz  r   )r#  r   r    r   <  s    r   r   z%s not uniqueNz%s not found)rx  r   r9   r   r   ry  )r,   r#  
create_newZsuperset_okZnonunique_okZidsr   )r#  r    get_time_slide_id  s    &z TimeSlideTable.get_time_slide_id)NFF)rG   rH   rI   r   r   rx   TimeSlideIDrz   r   rx  ry  r|  r   r   r   r    ru    s   ru  c               @   s   e Zd Zeej ZdS )	TimeSlideN)rG   rH   rI   r~   ru  r   r   r   r   r   r   r    r~  O  s   r~  coinc_definercoinc_def_idc               @   s<   e Zd ZdZdddddZdZedZdd	iZdddZ	dS )CoincDefTabler  z	ilwd:charrl   r   )r  r   search_coinc_typedescriptionzPRIMARY KEY (coinc_def_id)r   Zcd_ssct_index)r   r  TNc                s    fdd| D }t |dkr0td f t |dkrF|d jS |sVt f|  }|  |_ |_|_||_| 	| |jS )a  
		Return the coinc_def_id for the row in the table whose
		search string and search_coinc_type integer have the values
		given.  If a matching row is not found, the default
		behaviour is to create a new row and return the ID assigned
		to the new row.  If, instead, create_new is False then
		KeyError is raised when a matching row is not found.  The
		optional description parameter can be used to set the
		description string assigned to the new row if one is
		created, otherwise the new row is left with no description.
		c                s$   g | ]}|j |jf fkr|qS r   )r   r  )r#   rv   )r   r  r   r    r   }  s    z2CoincDefTable.get_coinc_def_id.<locals>.<listcomp>r   z<(search, search coincidence type) = ('%s', %d) is not uniquer   )
r9   r?   r  r   r   r   r   r  r  r   )r,   r   r  r{  r  rowsrv   r   )r   r  r    get_coinc_def_idp  s    


zCoincDefTable.get_coinc_def_id)TN)
rG   rH   rI   r   r   rx   
CoincDefIDrz   r   r  r   r   r   r    r  b  s   r  c               @   s   e Zd Zeej ZdS )CoincDefN)rG   rH   rI   r~   r  r   r   r   r   r   r   r    r    s   r  coinc_eventr   c               @   s>   e Zd ZdZddddddddZdZedZd	Zd
ddZ	dS )
CoincTabler  z	ilwd:charrl   r   r   )rj   r  r   r   rA   r   Z
likelihoodzPRIMARY KEY (coinc_event_id)r   )rj   r  r   rA   )r  )r   )Zce_cdi_indexZce_tsi_indexN)
rG   rH   rI   r   r   rx   CoincIDrz   r   r   r   r   r   r    r    s   r  c               @   s2   e Zd Zeej ZedZ	dd Z
dd ZdS )CoincrA   c             C   s   | j S )N)insts)r,   r   r   r    r     s    zCoinc.get_instrumentsc             C   s
   || _ d S )N)r  )r,   rA   r   r   r    r     s    zCoinc.set_instrumentsN)rG   rH   rI   r~   r  r   r   r   r*   r  r   r   r   r   r   r    r    s   r  c               @   s*   e Zd ZdZddddZdZdddZd	S )
CoincMapTableZcoinc_event_mapz	ilwd:charZchar_v)r   
table_namer   )r  )r  r   )r   )Zcem_tn_ei_indexZcem_cei_indexN)rG   rH   rI   r   r   r   r   r   r   r   r    r    s   r  c               @   s   e Zd Zeej ZdS )CoincMapN)rG   rH   rI   r~   r  r   r   r   r   r   r   r    r    s   r  dq_list
dq_list_iddq_list_row_idc               @   s.   e Zd ZdZdddddddZdZedZdS )	DQSpecListTabler  z	ilwd:charrl   r   )r  r  r=   flag
low_windowhigh_windowz(PRIMARY KEY (dq_list_id, dq_list_row_id)r   N)rG   rH   rI   r   r   rx   DQSpecListIDrz   r   r   r   r    r    s   r  c               @   s"   e Zd Zeej Zdd ZdS )DQSpecc             C   s>   x8t |D ],\}}||d | j |d | j ||< q
W dS )zH
		Apply our low and high windows to the segments in a
		segmentlist.
		r   r   N)	enumeratera   r  r  )r,   r.  rd  rh   r   r   r    apply_to_segmentlist  s    zDQSpec.apply_to_segmentlistN)	rG   rH   rI   r~   r  r   r   r   r  r   r   r   r    r     s   r  
ligolw_monc            
   @   s4   e Zd ZdZdddddddddd	ZdZedZdS )	LIGOLWMonTabler  rm   z	ilwd:charr   )	r   rj   r0  time_nsr  r   r   r   rl  zPRIMARY KEY (event_id)r   N)rG   rH   rI   r   r   rx   LIGOLWMonIDrz   r   r   r   r    r    s   r  c               @   s*   e Zd Zeej Zdd Zdd Z	dS )	LIGOLWMonc             C   s   t | j| jS )N)r   r0  r  )r,   r   r   r    get_time/  s    zLIGOLWMon.get_timec             C   s   |j |j | _| _d S )N)r   r   r0  r  )r,   r\   r   r   r    set_time2  s    zLIGOLWMon.set_timeN)
rG   rH   rI   r~   r  r   r   r   r  r  r   r   r   r    r  ,  s   r  c               @   s.   e Zd ZdZddddddddddd
ZdZdS )VetoDefTableZveto_definerz	ilwd:charrl   rm   )
rj   r0   r+   ro   categoryrs   rt   Z	start_padZend_padrp   )rj   r0   N)rG   rH   rI   r   r   r   r   r   r   r    r  B  s   r  c               @   s   e Zd Zeej ZdS )VetoDefN)rG   rH   rI   r~   r  r   r   r   r   r   r   r    r  S  s   r  	summ_mimesumm_mime_idc               @   sD   e Zd ZdZddddddddddddddddddZdZedZd	S )
SummMimeTabler  rl   z	ilwd:charrm   Zblob)originrj   filenameZ	submitterrC  rD  rs   r   rt   rL  r  ZdescripZmimedataZmimedata_lengthmimetyperp   r  zPRIMARY KEY (summ_mime_id)r   N)rG   rH   rI   r   r   rx   
SummMimeIDrz   r   r   r   r    r  f  s(   r  c               @   s:   e Zd Zeej Zdd Zdd Z	dd Z
dd Zd	S )
SummMimec             C   s   t | j| jS )N)r   rs   r   )r,   r   r   r    r     s    zSummMime.get_startc             C   s   |j |j | _| _d S )N)r   r   rs   r   )r,   r\   r   r   r    r     s    zSummMime.set_startc             C   s   t | j| jS )N)r   rt   rL  )r,   r   r   r    r    s    zSummMime.get_endc             C   s   |j |j | _| _d S )N)r   r   rt   rL  )r,   r\   r   r   r    r    s    zSummMime.set_endN)rG   rH   rI   r~   r  r   r   r   r   r   r  r  r   r   r   r    r    s
   r  c               @   s   e Zd ZdZdddZdS )TimeSlideSegmentMapTableZtime_slide_segment_mapz	ilwd:char)rD  r   N)rG   rH   rI   r   r   r   r   r   r    r    s   r  c               @   s   e Zd Zeej ZdS )TimeSlideSegmentMapN)rG   rH   rI   r~   r  r   r   r   r   r   r   r    r    s   r  c             C   s   x| D ]}|   qW dS )a  
	For each class in the list, if the .next_id attribute is not None
	(meaning the table has an ID generator associated with it), set
	.next_id to 0.  This has the effect of reseting the ID generators,
	and is useful in applications that process multiple documents and
	add new rows to tables in those documents.  Calling this function
	between documents prevents new row IDs from growing continuously
	from document to document.  There is no need to do this, it's
	purpose is merely aesthetic, but it can be confusing to open a
	document and find process ID 300 in the process table and wonder
	what happened to the other 299 processes.

	Example:

	>>> reset_next_ids(TableByName.values())
	N)Zreset_next_id)classesr   r   r   r    reset_next_ids  s    
r  c             C   s"   t | } | jfdd}|| _| S )an  
	Modify ContentHandler, a sub-class of
	glue.ligolw.LIGOLWContentHandler, to cause it to use the Table
	classes defined in this module when parsing XML documents.

	Example:

	>>> from glue.ligolw import ligolw
	>>> class MyContentHandler(ligolw.LIGOLWContentHandler):
	...	pass
	...
	>>> use_in(MyContentHandler)
	<class 'glue.ligolw.lsctables.MyContentHandler'>
	c             S   s0   t j|d }|tkr$t| |S || ||S )Nr   )r
   r'   r   r"   )r,   parentattrsZ__orig_startTabler+   r   r   r    
startTable  s    zuse_in.<locals>.startTable)r
   use_inr  )ZContentHandlerr  r   r   r    r    s    
r  )N)r]   	functoolsr   ImportErrorZfunctools32rW   r'  rM   r&  xmlr   Zligor   Zgluer   r   rb   r   r1   r	   r
   r   r   r   
__author__rz  __version__date__date__r!   r)   objectr*   r<   r   r6   r   rK   r^   rd   Zget_ilwdchar_classry   r'   rk   r   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6  r  r7  rE  rI  r  rK  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r6  r1  r7  r>  r:  r?  rF  rB  rG  rJ  rI  rK  rL  rS  rT  rU  rV  rW  rZ  rY  r[  r_  r]  r`  rm  rk  rn  rr  rq  rs  r}  ru  r~  r  r  r  r  r  r  r  r  r  ZDQSpecListRowIDr  r  r  r  r  r  r  r  r  r  r  r  r   r"   r  r  r   r   r   r    <module>!   s  

% /"3QGV %& 3 &"4 i "$    @ ~V).} k/

#