o

����n�gqP����������������������@���s����d�Z�ddlZddlZddlZddlZddlZddlZddlZej	�
���ddlZddl
Z
e
�d��ddlmZ�ddlmZ�ddlZddlmZ�G�dd��d�ZdS�)z�
Name: subseasonal_plots_performance_diagram.py
Contact(s): Shannon Shields
Abstract: This script is run by subseasonal_plots.py in ush/subseasonal.
          This script generates a performance diagram plot.
�����N�agg)�	PlotSpecsc�������������������@���s ���e�Zd�ZdZdd��Zdd��ZdS�)�PerformanceDiagramz.
    Create a performance diagram graphic
    c	�����������	������C���s4���||�_�||�_||�_||�_||�_||�_||�_||�_dS�)a��! Initialize PerformanceDiagram class

             Args:
                 logger          - logger object
                 input_dir       - path to input directory (string)
                 output_dir      - path to output directory (string)
                 model_info_dict - model information dictionary (strings)
                 plot_info_dict  - plot information dictionary (strings)
                 date_info_dict  - date information dictionary (strings)
                 met_info_dict   - MET information dictionary (strings)
                 logo_dir        - directory with logo images (string)
 
             Returns:
        N)�logger�	input_dir�
output_dir�model_info_dict�date_info_dict�plot_info_dict�
met_info_dict�logo_dir)	�selfr���r���r���r���r	���r
���r���r�����r����^/lfs/h1/ops/prod/packages/evs.v1.0.19/ush/subseasonal/subseasonal_plots_performance_diagram.py�__init__���s���
zPerformanceDiagram.__init__c�����������\���������sL��|�j��d��|�j��d|�j�����|�j��d|�j�����|�j��d|�j�����|�j��d|�j�����|�j��d|�j�����|�jd�dkrP|�j��d	|�jd������t	�
d
��tj�
|�jd�}tj�|�sct�|��|�j��d|�����g�d
�}|�j��d��t�|�j�|�jd�|�jd�|�jd�|�jd�|�jd�|�jd�|�jd�|�jd�|�jd�|�jd��\���fdd�tt���D��}�fdd�tt���D��}|�jd�dkr�|�j��dd�
|��d�d �|�jd���d��d!�d�
|�����}n(|�jd�d"k�r|�j��d#d�
|��d�d �|�jd���d��d$�d�
|�����}|�j��d%|�j�����|�j��d&��g���|�jd'�D��]D}|�j��d(|�����|�jd'��|�}|�jd)�|�}t�|�j�|�j|�j|�j|�j|�jd*�|�jd+�||�jd,�|�jd-�||�jd.�|�jd/�|�jd0�|�jd1�|�jd2�|�jd�|||�jd��}	���|	d3�j�d4������|	j�d
�������}
||�jd'�d
�k�r�tjtj tj!j"|
|gd5dgd6�|�jd'�d7�}|D�]�}|�j��d8|��d9�|�jd.������t�#|�j�|	|�jd.�|�\}
}|
j�d
�������}
|�jd:�d;k�r+|�j��d<��tj$�%|�}tj$�&|�}|j'tj d=�}|
D�]$}|
�|�}||d>d>�f�|
j(|<�|	j(|��)|
j(|��*���j|	j(|<��q|
D�];}|
�|�}|�jd.�d?v��rDd@}|
j(|�}ndA}|	j(|�}t�+|�j�||�jd.�||�}t�,|��sg||j(||f|f<��q-�q��q'|�j��dB��t-|�j�dC�}|�.���g�dD�}t/j0�1|�}t�2dEdFdE�}t�3||�\}}||�}dGdG|�dG|��dG��}g�dH�}|�4|�jd��}|�4dI�}|�4dJ�} |�4dK�}!t������t�5��t�)��dLk����t���dMk�r�|�j��6dN��t	�
dM��nt���d
k�r�|�j��dO��dPg��|�7|�j|�j��d
��}"dQ}#tj�
|�j8dR�}$tj�|$��r,dS}#t/j9�:|$�}%|�;dT|j<d
�|j<dM�t=j>dU��\}&}'}(dQ})tj�
|�j8dV�}*tj�|*��rYdS})t/j9�:|*�}+|�;dW|j<d
�|j<dM�t=j>dU��\},}-}.|�?||�j|�j�}/|�j��dX��t=j@dMdM|j<d
�|j<dM�fdY�\}0}1|0�A|"��|1�BdQ��|1�C| ��|1�Dd
dMg��|1�Et�2d
dZd[���|1�F|��|1�Gd
dMg��|1�Ht�2d
dZd[���|#�r�|0jI|%|&|'dM|.d\�}2|2�JdS��|)�r�|0jI|+|,|-dM|.d\�}3t=jK||||d]d^d_�}4d`}5g�}6|D�]*}7t�Lt�M|5da�t�M|7da�dM���}8t�Lt�M|5da�t�M|8da���}9|6�N|8|9f���q�t=jO|4db|6dc��t=jP|||t�2dddZd[�|dedf�}:|1�Q��jRdg�};|1�Q��jS}<dh}=|1�Q��jT|1�Q��jS�}>|0�U|;|<|=|>g�}?t=jV|:di|?|:jWdj�}@|@�X|!��dkdl��}A|�Y��}Bt|�jd'��ttZ|B�[����k�r�|�j��6dmt|�jd'����d��d�dn�
|�jd'���do�dp�ttZ|B�[���������t	�
dM��g�}Ci�}D|�jd'�D�]7}|�jd'��|�dM�}E|tZ|B�[���v��r�|B|�}Fn|Bdqt\|E���}F|C�N|A|Fdq�drdsd
|Fdt�du���|F|D|<��q���fdvd�|�jd'�D��}G|1j]|C|Gdwdx|j^|j_dy�}Ht=�`���|1�a|H��g�}Ig�}J|�b��}K|
D�]�}|�cdz�d
�}L|�cdz�dM�}M|�cdz�da�}N|�j|L�d{�}O|j(|�}P|MtZ|K�[���v��r5|K|M�}Qn|K|L�}Q|Pj(dJ�}Rtj$�%|R�}St|S�tj$�d|S��}T|Pj(dI�}Utj$�%|U�}Vt|V�tj$�d|V��}W|Td
k�r�|Wd
k�r�|�j��d||L��d}|M��d�d~|N������|1je|S|V|Qd�|Qd��da|Qd���d�d
ttZ|�j�[����|
�|��d��d���|J�N|N��|I�N|AdP|Qd�|Qd��d�d
dr���|�jd'�D�] }|1jf|R|�|U|�|Qd�dadr|D|�dq�|D|�dt�da�d�d����q��q|1jg�h��}X|H�i���j��}Y|X�k|Yjl|YjSf|YjR|YjTfg�}Z|1j]|I|Jd�|Zd
�dM�dZ�fdx|j^|j_dy�}[|�j��d�|/���t=�m|/��t=�n���t=�od���d>S�)�z\! Create the performance diagram graphic

             Args:

             Returns:
        z Creating performance diagrams...zInput directory: zOutput directory: zModel information dictionary: zDate information dictionary: zPlot information dictionary: �stat�PERF_DIAz)Cannot make performance diagram for stat r����imageszPlots will be in: )�SRATIO�POD�CSIz#Creating valid and init date arrays�	date_type�
start_date�end_date�valid_hr_start�valid_hr_end�valid_hr_inc�
init_hr_start�init_hr_end�init_hr_inc�
forecast_hourc������������������������g�|�]	}��|���d���qS��z
%Y%m%d_%H%M%S��strftime��.0�d)�valid_datesr���r����
<listcomp>b���������z?PerformanceDiagram.make_performance_diagram.<locals>.<listcomp>c��������������������r!���r"���r#���r%���)�
init_datesr���r���r)���d���r*����VALIDz9Based on date information, plot will display valid dates z, � zfor forecast hour zwith initialization dates �INITzBBased on date information, plot will display initialization dates zwith valid dates z!Reading in model stat files from z%Building dataframe for all thresholds�fcst_var_threshsz%Building data for forecast threshold �obs_var_threshs�
fcst_var_name�fcst_var_level�obs_var_name�
obs_var_level�	line_type�grid�vx_mask�
interp_method�
interp_points�
FCST_UNITS�str�model)�names)�columnszCalculating statistic z from line type �event_equalizationZYESzDoing event equalization)�
fill_valueN)�CNT�GRAD�CTS�NBRCTS�NBRCNT�VCNT�mean�aggregationzDoing plot set up�performance_diagram)
z#ffffffz#f5f5f5z#ecececz#dfdfdfz#cbcbcbz#b2b2b2z#8e8e8ez#6f6f6fz#545454z#3f3f3fg����MbP?gj�t��?��������?)皙�����?g�������?g�������?g333333�?g�������?rJ���g333333�?g�������?��������@g������@g������@g������$@r���r���r����nan����zFATAL ERROR, DIFFERING UNITSzEmpty dataframe��Fznoaa.pngT�leftz
figure.dpiznws.png�rightzCreating performance diagram)�figsizeg�������?rK���)�zorder�alpha�gray�dashed)�colors�
linestylesg�������?����z%1.1f)�fmt�manualg���������neither)�cmap�extendg�������?g{�G�z�?�vertical)�orientation�cax�ticksc�����������������S���s ���t�jg�g�|�|d||||d�	d�S�)NrL���)�marker�mec�mew�c�ls�lw�msr���)�plt�plot)�mrf���rg���rh���ri���rd���r���r���r����<lambda>6��s����
��z=PerformanceDiagram.make_performance_diagram.<locals>.<lambda>z-FATAL ERROR, REQUESTED NUMBER OF THRESHOLDS (�,z/) EXCEEDS PRESET MARKER SETTING, REDUCE NUMBER zOF THRESHOLDS TO <= rc����white�solid�
markersize�blackc��������������������s���g�|�]}|��d���d�����qS�)r-���r���r���)r&����t)�
fcst_unitsr���r���r)���]��s������)��������?g333333��zupper center)�bbox_to_anchor�loc�ncol�fontsize�/�obs_namez	Plotting z - z- �color�	linestyle�	linewidth�None����)r|���r}���r~���rc���rq���rS��������(���)rf���r~����
edgecolorsrc����srS���ru���zSaving image as �all)pr����info�debugr���r���r���r	���r
����warning�sys�exit�os�path�join�exists�makedirs�sub_util�get_plot_dates�range�len�index�build_dfr���r^����values�astype�tolist�get_level_values�unique�pd�	DataFrame�nprM����
MultiIndex�from_product�calculate_stat�ma�masked_invalid�	mask_cols�filledrw����where�notna�calculate_average�isnanr����set_up_plot�
matplotlibrW����ListedColormap�arange�meshgrid�get_stat_plot_name�delete�error�get_plot_titler����image�imread�get_logo_location�fig_sizerj����rcParams�get_savefig_name�subplots�suptitler6����
set_xlabel�set_xlim�
set_xticks�
set_ylabel�set_ylim�
set_yticks�figimage�set_visible�contour�sqrt�power�append�clabel�contourf�get_position�x1�y0�y1�add_axes�colorbar�levels�	set_label�get_marker_plot_settings�list�keysr;����legend�legend_ncol�legend_font_size�draw�
add_artist�get_model_plot_settings�split�count_maskedrk����scatter�	transData�inverted�	get_frame�get_bbox�	transform�x0�savefig�clf�close)\r
���Zoutput_image_dirZperf_dia_stat_list�format_valid_datesZformat_init_dates�
plot_dates�fcst_var_threshZfcst_var_thresh_idx�obs_var_thresh�all_model_dfZmodel_idx_listZperf_dia_stat_avg_dfr����stat_df�
stat_arrayZmasked_stat_array�	model_idxZ
model_idx_numZ
avg_methodZcalc_avg_dfZmodel_idx_fcst_var_thresh_avgZ
plot_specs_pdZ
csi_colorsZcmap_csiZpd_ticksZpd_srZpd_podZpd_biasZpd_csiZ
pd_bias_clevs�stat_plot_nameZ
POD_plot_nameZSRATIO_plot_nameZ
CSI_plot_name�
plot_titleZplot_left_logoZplot_left_logo_pathZleft_logo_img_arrayZleft_logo_xpixel_locZleft_logo_ypixel_locZleft_logo_alphaZplot_right_logoZplot_right_logo_pathZright_logo_img_arrayZright_logo_xpixel_locZright_logo_ypixel_locZright_logo_alpha�
image_name�fig�axZ
left_logo_imgZright_logo_imgZCBIAS�radiusZCBIAS_label_locZbias_val�x�yZCFCSIZ	cbar_leftZcbar_bottomZ
cbar_widthZcbar_heightZcbar_ax�cbar�fZ thresh_marker_plot_settings_dictZthresh_legend_handlesZthresh_mark_dictZfcst_var_thresh_numZfcst_var_thresh_marker_dictZthresh_legend_labelsZ
thresh_legendZmodel_legend_handlesZmodel_legend_labels�model_plot_settings_dict�	model_num�model_num_nameZmodel_num_plot_nameZmodel_num_obs_nameZmodel_num_dataZmodel_num_plot_settings_dictZmodel_num_SRATIOZmasked_model_num_SRATIOZmodel_num_npts_SRATIOZ
model_num_PODZmasked_model_num_PODZmodel_num_npts_POD�invZ
legend_boxZlegend_box_invZmodel_legendr���)rt���r+���r(���r����make_performance_diagram7���s�����
�

�

�

�������������������
���	
�
��

��

�
�����*�



��
���
��
��



�
��"
�

�
�
��������
���
����
��

��

��

���
���

��

��


��
z+PerformanceDiagram.make_performance_diagramN)�__name__�
__module__�__qualname__�__doc__r���r����r���r���r���r���r������s����r���)r����r����r�����logging�datetime�glob�
subprocess�pandasr�����plotting� deregister_matplotlib_converters�numpyr����r�����use�matplotlib.pyplot�pyplotrj����matplotlib.dates�dates�md�subseasonal_utilr�����subseasonal_plots_specsr���r���r���r���r���r����<module>���s"���