o nÇgqPã@sždZddlZddlZddlZddlZddlZddlZddlZej   ¡ddl Z ddl Z e  d¡ddlmZddlmZddlZddlmZGdd„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 eZdZdZdd„Zdd„ZdS)ÚPerformanceDiagramz. Create a performance diagram graphic c Cs4||_||_||_||_||_||_||_||_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) Úselfrrrrr 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›¡|jddkrP|j d |jd›¡t   d ¡t j   |jd ¡}t j  |¡sct  |¡|j d |›¡gd ¢}|j d¡t |j|jd|jd|jd|jd|jd|jd|jd|jd|jd|jd¡ \‰‰‡fdd„ttˆƒƒDƒ}‡fdd„ttˆƒƒDƒ}|jddkrè|j dd  |¡dd |jd›dd!d  |¡¡ˆ}n(|jdd"kr|j d#d  |¡dd |jd›dd$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¡} ˆ | d3j d4¡ ¡¡| j d ¡ ¡ ¡} ||jd'd kr·tjtj tj!j"| |gd5dgd6|jd'd7} |D]±} |j d8| ›d9|jd.›¡t #|j| |jd.| ¡\} }| j d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d‚d ttZ|j [¡ƒƒ|  |¡dƒd„|J N|N¡|I N|AdP|Qd|Qd€d…d drƒ¡|jd'D] }|1jf|R||U||Qddadr|D|dq|D|dtdad†d‡q¼q|1jg h¡}X|H i¡ j¡}Y|X k|Yjl|YjSf|YjR|YjTfg¡}Z|1j]|I|Jdˆ|Zd dMdZfdx|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_datesrrÚ bóÿz?PerformanceDiagram.make_performance_diagram..cr!r"r#r%)Ú init_datesrrr)dr*Ú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š™™™™™é?rJg333333ó?gø?ç@g@g@g$@rrrÚ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 Ss tjgg||d||||d dS)NrL)ÚmarkerÚmecÚmewÚcÚlsÚlwÚmsr)ÚpltÚplot)ÚmrfrgrhrirdrrrÚ6s  þþz=PerformanceDiagram.make_performance_diagram..z-FATAL ERROR, REQUESTED NUMBER OF THRESHOLDS (ú,z/) EXCEEDS PRESET MARKER SETTING, REDUCE NUMBER zOF THRESHOLDS TO <= rcÚwhiteÚsolidÚ markersizeÚblackcsg|] }|›dˆd›‘qS)r-rr)r&Út)Ú fcst_unitsrrr)]sÿÿ)çà?g333333³¿z upper center)Úbbox_to_anchorÚlocÚncolÚfontsizeú/Úobs_namez Plotting z - z- ÚcolorÚ linestyleÚ linewidthÚNoneé)r|r}r~rcrqrSéé()rfr~Ú edgecolorsrcÚsrSruzSaving image as Úall)prÚinfoÚdebugrrrr 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_methodZ calc_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_leftZ cbar_bottomZ cbar_widthZ cbar_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)rtr+r(rÚmake_performance_diagram7s ÿÿÿ ÿ    õ ÿ  ÿÿÿþýüûþþýüûú ÿÿÿ ðÿÿ ÿýú  ÿ þÿ      ÿ ý    þ  þÿ€ñê *  ÿ       þÿ þÿÿ þÿ ÿ  ÿ    þ þ ÿ" þ  ÿ ÿ ÿ ÿÿþýüûú ÿÿÿ ÿÿýÿ  þû  ÿÿ   ÿÿ   ÿÿÿ ÿÿù ýÿ  ø€  ÿû z+PerformanceDiagram.make_performance_diagramN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrûrrrrrs r)rÿr‰r‹ÚloggingÚdatetimeÚglobÚ subprocessÚpandasr›ÚplottingÚ deregister_matplotlib_convertersÚnumpyrrªÚuseÚmatplotlib.pyplotÚpyplotrjÚmatplotlib.datesÚdatesÚmdÚsubseasonal_utilrÚsubseasonal_plots_specsrrrrrrÚs"