B
    }d8&                 @   sr   d 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mZ dd
lmZ dZG dd deeZdS )zTransfer function plots
    )Time)OrderedDictN   )BodePlot)label_to_latex   )TransferFunctionProductFFTMixin)
GPS_SCALESz Evan Goetz <evan.goetz@ligo.org>c                   s   e Zd ZdZdZ fddZedd Ze fddZ	ed	d
 Z
dd Z f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  ZS )TransferFunctionzaPlot transfer function between a reference time series and one
    or more other time series
    Ztransferfunctionc                sd   t  j|| | jjp| jd | _d| jkr>| jdd | _| jj| _d | _| jd | _	g | _
d S )Nr   ,r   )super__init__argsrefZ	chan_listref_chansplitplot_dBsubplot	test_chantfs)selfr   kwargs)	__class__ f/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/cli/transferfunction.pyr   (   s    

zTransferFunction.__init__c             C   s   | j j| j S )zCThe current `~matplotlib.axes.Axes` of this product's plot
        )plotaxesr   )r   r   r   r   ax3   s    zTransferFunction.axc                s   t  |}|jddd |S )Nz--refz7Reference channel against which others will be compared)help)r   arg_channelsadd_argument)clsparsergroup)r   r   r   r    9   s    zTransferFunction.arg_channelsc             C   s4   | j d|dd}|jdddd | j d|d	d}|S )
Nymaglog)scalez	--plot-dB
store_truezPlot transfer function in dB)actionr   yphaselinear)Z	_arg_axisr!   )r"   r#   r$   r   r   r   	arg_yaxis@   s
    zTransferFunction.arg_yaxisc             C   sX   | j d }t|dddj}| d| d| j d}d| jj d	| jj }d
||gS )Nr   gpsutc)formatr'   z | z ()z
fftlength=z
, overlap=z, )
start_listr   Zisodurationr   secpfftoverlapjoin)r   r-   r.   ZtstrZfftstrr   r   r   	get_titleI   s
    
zTransferFunction.get_titlec                s(   |j d krd|_ |jrd|_ t |S )Nr&   r+   )Z	ymagscaler   r   _finalize_arguments)r   r   )r   r   r   r7   R   s
    
z$TransferFunction._finalize_argumentsc             C   s2   d}| j dkr d}| jr |d7 }| j dkr.d}|S )zText for y-axis label
         r   Z	Magnitudez [dB]r   zPhase [deg.])r   r   )r   Z	ylabelstrr   r   r   
get_ylabelZ   s    

zTransferFunction.get_ylabelc             C   s   d| j  d| j S )zFStart of default super title, first channel is appended to it
        zTransfer function: /)r   r   )r   r   r   r   get_suptitleg   s    zTransferFunction.get_suptitlec             C   s0   x*dD ]"}|| _ |   |   |   qW d S )N)r   r   )r   scale_axes_from_dataZset_xaxis_propertiesset_yaxis_properties)r   r   r   r   r   set_axes_propertiesl   s
    
z$TransferFunction.set_axes_propertiesc       	         s   fdd} fdd}t j  d}t j  d}t j  d}t j  d}|d	kr|d
k	r|d
k	r|dk r||| f}n||f}|d d
k	s|d d
k	r|d|  |r|d| |dtkr|d|jjd |d
kr  drt d }nt d }jdkr8 dkr8d
}|rXjrNt|}|d| |d}|d}|d}	d 
  d| d|d d|d  	d 
  d|  d
S )zTGeneric method to set properties for X/Y axis
        on a specific subplot
        c                s,   t jjj d d   |   }|S )Nget_r   )getattrr   r   r   lower)paramret)axisr   r   r   _getx   s    z3TransferFunction._set_axis_properties.<locals>._getc                sT      dr0tjjj d|  ||}n tjjj d|  ||}|S )NyZset_yZset_x)rA   
startswithr@   r   r   r   )rB   r   r   rC   )rD   r   r   r   _set}   s    
z3TransferFunction._set_axis_properties.<locals>._setr'   labelminmaxzauto-gpsNg    חAr   r   lim)epochrF   r9   Z
get_xlabelxr   z-axis parameters | scale: z | limits: z -    z-axis label: )rL   )r@   r   r
   rM   rA   rG   r   usetexr   r&   upper)	r   rD   rE   rH   r'   rI   Zmin_Zmax_Zlimitsr   )rD   r   r   _set_axis_propertiest   sF    	


(z%TransferFunction._set_axis_propertiesc             C   s$   | j dkr| d n
| d dS )z"Set properties for Y-axis
        r   r%   r*   N)r   rR   )r   r   r   r   r=      s    
z%TransferFunction.set_yaxis_propertiesc                sT   j jdkr`tdd  jD  j _ j jdkr` j jdksH j jdkr`tdd  jD  j _ j jdkrtdd  jD  j _ fdd	 jD }d}d}x|D ]} jdkr܈ jrd
t	
t|j }qt|j}nt	j|jdd}t|}t|}|dks||k r|}|dks&||kr|}qW  jjj||dd  jjdd dS )zBRestrict data limits for Y-axis based on what you can see
        Nc             s   s   | ]}|j d  V  qdS )r   N)xspan).0tfr   r   r   	<genexpr>   s    z8TransferFunction.scale_axes_from_data.<locals>.<genexpr>r   r&   c             s   s   | ]}|j jV  qd S )N)dfvalue)rT   rU   r   r   r   rV      s    c             s   s   | ]}|j d  V  qdS )r   N)rS   )rT   rU   r   r   r   rV      s    c                s    g | ]}|  jj jjqS r   )cropr   xminxmax)rT   rU   )r   r   r   
<listcomp>   s   z9TransferFunction.scale_axes_from_data.<locals>.<listcomp>   T)deg)ignoreF)Zscalex)r   rZ   rJ   r   xscaler[   rK   r   r   nplog10absrX   Zangler   ZyaxisZset_data_intervalZautoscale_view)r   ZcroppedZyminZymaxrU   valsminvalmaxvalr   )r   r   r<      s4    



z%TransferFunction.scale_axes_from_datac             C   sT   |    d| _| | jj | | jj | | jj  d| _| | jj  dS )z4Finalize figure object and show() or save()
        r   r   N)	r>   r   	set_titler   titleZset_suptitleZsuptitleZset_gridZnogrid)r   r   r   r   set_plot_properties   s    z$TransferFunction.set_plot_propertiesc          	   C   s  | j }t|j}|j}| dd| d|  |dk	r>||9 }| dd| j  t }xX| jD ]N}|j}y||| |j	j
< W q` tk
r   t ||< ||| |j	j
< Y q`X q`W t| j| j| jd}g | _x|D ]}|| | j}x|| D ]~}	|| |	 }|	| _|j||||jd}
|	}t| jd	kr@|d
|jj 7 }| jrPt|}|j|
| j|d | j|
 qW qW |jdkr|jsd	| |_|S )zAGenerate the transfer function plot from the time series
        r   z'Calculating transfer function secpfft: z, overlap: NrO   zReference channel: )figsizedpidB)	fftlengthr4   windowr   z, )rl   rI   r&   )r   floatr3   r4   r&   r   r   Z
timeseriesspanZchannelnameKeyErrorr   rj   rk   r   r   popr   Ztransfer_functionrn   lenr1   rM   r-   rP   r   Zadd_frequencyseriesappendr`   rZ   )r   r   rm   r4   groupsZseriessegr   Zreftsrq   rU   rI   r   r   r   	make_plot   sH    






zTransferFunction.make_plot)__name__
__module____qualname____doc__r)   r   propertyr   classmethodr    r,   r6   r7   r9   r;   r>   rR   r=   r<   ri   rx   __classcell__r   r   )r   r   r   "   s    		G%r   )r|   Zastropy.timer   collectionsr   numpyra   Z	plot.boder   Zplot.texr   Z
cliproductr   r	   Zplot.gpsr
   
__author__r   r   r   r   r   <module>   s   