Example #1
0
def _draw_time_series_plot(evaluation, plot_config):
    """"""
    time_range_info = plot_config['time_range']
    ref_ds = evaluation.ref_dataset
    target_ds = evaluation.target_datasets

    if time_range_info == 'monthly':
        ref_ds.values, ref_ds.times = utils.calc_climatology_monthly(ref_ds)

        for t in target_ds:
            t.values, t.times = utils.calc_climatology_monthly(t)
    else:
        logger.error('Invalid time range provided. Only monthly is supported '
                     'at the moment')
        return

    if evaluation.subregions:
        for bound_count, bound in enumerate(evaluation.subregions):
            results = []
            labels = []

            subset = dsp.subset(bound,
                                ref_ds,
                                subregion_name="R{}_{}".format(
                                    bound_count, ref_ds.name))

            results.append(utils.calc_time_series(subset))
            labels.append(subset.name)

            for t in target_ds:
                subset = dsp.subset(bound,
                                    t,
                                    subregion_name="R{}_{}".format(
                                        bound_count, t.name))
                results.append(utils.calc_time_series(subset))
                labels.append(subset.name)

            plots.draw_time_series(np.array(results), ref_ds.times, labels,
                                   'R{}'.format(bound_count),
                                   **plot_config.get('optional_args', {}))

    else:
        results = []
        labels = []

        results.append(utils.calc_time_series(ref_ds))
        labels.append(ref_ds.name)

        for t in target_ds:
            results.append(utils.calc_time_series(t))
            labels.append(t.name)

        plots.draw_time_series(np.array(results), ref_ds.times, labels,
                               'time_series',
                               **plot_config.get('optional_args', {}))
    Bounds(30.0, 40.0, -15.0,  0.0),
    Bounds(33.0, 40.0, 25.0, 35.0)]

region_list = [["R" + str(i + 1)] for i in xrange(13)]

for regions in region_list:
    firstTime = True
    subset_name = regions[0] + "_CRU31"
    # labels.append(subset_name) #for legend, uncomment this line
    subset = dsp.subset(CRU31, list_of_regions[region_counter], subset_name)
    tSeries = utils.calc_time_series(subset)
    results.append(tSeries)
    tSeries = []
    firstTime = False
    for member, each_target_dataset in enumerate(target_datasets):
        subset_name = regions[0] + "_" + target_datasets[member].name
        # labels.append(subset_name) #for legend, uncomment this line
        subset = dsp.subset(target_datasets[member],
                            list_of_regions[region_counter],
                            subset_name)
        tSeries = utils.calc_time_series(subset)
        results.append(tSeries)
        tSeries = []

    plotter.draw_time_series(np.array(results), CRU31.times, labels, regions[
                             0], ptitle=regions[0], fmt='png')
    results = []
    tSeries = []
    labels = []
    region_counter += 1
region_list = [["R" + str(i + 1)] for i in xrange(13)]

for regions in region_list:
    firstTime = True
    subset_name = regions[0] + "_CRU31"
    #labels.append(subset_name) #for legend, uncomment this line
    subset = dsp.subset(list_of_regions[region_counter], CRU31, subset_name)
    tSeries = utils.calc_time_series(subset)
    results.append(tSeries)
    tSeries = []
    firstTime = False
    for member, each_target_dataset in enumerate(target_datasets):
        subset_name = regions[0] + "_" + target_datasets[member].name
        #labels.append(subset_name) #for legend, uncomment this line
        subset = dsp.subset(list_of_regions[region_counter],
                            target_datasets[member], subset_name)
        tSeries = utils.calc_time_series(subset)
        results.append(tSeries)
        tSeries = []

    plotter.draw_time_series(np.array(results),
                             CRU31.times,
                             labels,
                             regions[0],
                             ptitle=regions[0],
                             fmt='png')
    results = []
    tSeries = []
    labels = []
    region_counter += 1
Example #4
0
def calculate_metrics_and_make_plots(varName, workdir, lons, lats, obsData, mdlData, obsRgn, mdlRgn, obsList, mdlList, subRegions, \
                                     subRgnLon0, subRgnLon1, subRgnLat0, subRgnLat1):
    '''
    Purpose:: 
        Calculate all the metrics used in Kim et al. [2013] paper and plot them 

    Input::
        varName - evaluating variable
        workdir -
        lons -
        lats -
        obsData -
        mdlData -
        obsRgn -
        mdlRgn -
        obsList -
        mdlList -
        subRegions - 
        subRgnLon0, subRgnLat0 - southwest boundary of sub-regions [numSubRgn]
        subRgnLon1, subRgnLat1 - northeast boundary of sub-regions [numSubRgn]
    Output:: 
        png files
        
     '''
   
   
    nobs, nt, ny, nx = obsData.shape
    nmodel = mdlData.shape[0]
    ### TODO: unit conversion (K to C)
    if varName == 'temp':
        obsData[0, :, :, :] = obsData[0, :, :, :] - 273.15
        if subRegions:
            obsRgn[0, :, :] = obsRgn[0, :, :] - 273.15
    if varName == 'prec' and obsData.max() > mdlData.max()*1000.:
        mdlData[:, :, :, :] = mdlData[:, :, :, :]*86400.
        if subRegions:
            mdlRgn[:, :, :] = mdlRgn[:, :, :]*86400.
        
    oTser, oClim = calcClimYear( obsData[0, :, :, :])
    bias_of_overall_average = ma.zeros([nmodel, ny, nx])
    spatial_stdev_ratio = np.zeros([nmodel])
    spatial_corr = np.zeros([nmodel])
    mdlList.append('ENS')
    
    for imodel in np.arange(nmodel):
        mTser, mClim = calcClimYear( mdlData[imodel,:,:,:])
        bias_of_overall_average[imodel,:,:] = calcBias(mClim, oClim)
        spatial_corr[imodel], sigLev = calcPatternCorrelation(oClim, mClim)
        spatial_stdev_ratio[imodel] = calcSpatialStdevRatio(mClim, oClim)   
    fig_return = plotter.draw_contour_map(oClim, lats, lons, workdir+'/observed_climatology_'+varName, fmt='png', gridshape=(1, 1),
                   clabel='', ptitle='', subtitles=obsList, cmap=None, 
                   clevs=None, nlevs=10, parallels=None, meridians=None,
                   extend='neither')    
    # TODO:
    # Be sure to update "gridshape" argument to be the number of sub plots (rows,columns). This should be improved so that the 
    # gridshape is optimally determined for a given number of models. For example:
    # For 3 models, a gridshape of (2,2) would be sensible:
    # X X 
    # X
    #
    fig_return = plotter.draw_contour_map(bias_of_overall_average, lats, lons, workdir+'/bias_of_climatology_'+varName, fmt='png', gridshape=(6, 2),
                   clabel='', ptitle='', subtitles=mdlList, cmap=None, 
                   clevs=None, nlevs=10, parallels=None, meridians=None,
                   extend='neither')
    Taylor_data = np.array([spatial_stdev_ratio, spatial_corr]).transpose()
    
    fig_return = plotter.draw_taylor_diagram(Taylor_data, mdlList, refname='CRU', fname = workdir+'/Taylor_'+varName, fmt='png',frameon=False)

    if subRegions:
        nseason = 2      # (0: summer and 1: winter)
        nregion = len(subRgnLon0)
        season_name = ['summer','winter']
        rowlabels = ['PNw','PNe','CAn','CAs','SWw','SWe','COL','GPn','GPc','GC','GL','NE','SE','FL']
        collabels = ['M1','M2','M3','M4','M5','M6','ENS']
        collabels[nmodel-1] = 'ENS'
        
        for iseason in [0,1]:
            portrait_subregion = np.zeros([4, nregion, nmodel])
            portrait_titles = ['(a) Normalized Bias', '(b) Normalized STDV', '(c) Normalized RMSE', '(d) Correlation']
            if iseason == 0:
                monthBegin=6
                monthEnd=8
            if iseason == 1:
                monthBegin=12
                monthEnd=2
                      
            obsTser,obsClim = calcClimSeasonSubRegion(6,8,obsRgn[0,:,:])
            for imodel in np.arange(nmodel):
                mTser, mClim =  calcClimSeasonSubRegion(6,8,mdlRgn[imodel,:,:])
                for iregion in np.arange(nregion):
                      portrait_subregion[0,iregion,imodel] = calcBias(mClim[iregion],obsClim[iregion])/calcTemporalStdev(obsTser[iregion,:])   
                      portrait_subregion[1,iregion,imodel] = calcTemporalStdev(mTser[iregion,:])/ calcTemporalStdev(obsTser[iregion,:]) 
                      portrait_subregion[2,iregion,imodel] = calcRootMeanSquaredDifferenceAveragedOverTime(mTser[iregion,:], obsTser[iregion,:])/calcTemporalStdev(obsTser[iregion,:])
                      portrait_subregion[3,iregion, imodel] = calcTemporalCorrelationSubRegion(mTser[iregion,:],obsTser[iregion,:])
            portrait_return = plotter.draw_portrait_diagram(portrait_subregion, rowlabels, collabels[0:nmodel], workdir+'/portrait_diagram_'+season_name[iseason]+'_'+varName, fmt='png', 
                             gridshape=(2, 2), xlabel='', ylabel='', clabel='', 
                             ptitle='', subtitles=portrait_titles, cmap=None, clevs=None, 
                             nlevs=10, extend='neither')  
            # annual cycle
            nmonth = 12
            times = np.arange(nmonth)
            data_names = [obsList[0]] + list(mdlList)
            annual_cycle = np.zeros([nregion, nmonth, nmodel+1])
            obsTser, annual_cycle[:, :, 0] = calcAnnualCycleMeansSubRegion(obsRgn[0,:,:])
            obsStd = calcAnnualCycleStdevSubRegion(obsRgn[0,:,:])
            for imodel in np.arange(nmodel):
                mdlTser, annual_cycle[:, :, imodel+1] = calcAnnualCycleMeansSubRegion(mdlRgn[imodel, :, :])
            # Make annual_cycle shape compatible with draw_time_series
            annual_cycle = annual_cycle.swapaxes(1, 2)
            tseries_return = plotter.draw_time_series(annual_cycle, times, data_names, workdir+'/time_series_'+varName, gridshape=(7, 2), 
                  subtitles=rowlabels, label_month=True)
Example #5
0
def _draw_time_series_plot(evaluation, plot_config):
    """"""
    time_range_info = plot_config['time_range']
    ref_ds = evaluation.ref_dataset
    target_ds = evaluation.target_datasets

    if time_range_info == 'monthly':
        ref_ds.values, ref_ds.times = utils.calc_climatology_monthly(ref_ds)

        for t in target_ds:
            t.values, t.times = utils.calc_climatology_monthly(t)
    else:
        logger.error(
            'Invalid time range provided. Only monthly is supported '
            'at the moment'
        )
        return

    if evaluation.subregions:
        for bound_count, bound in enumerate(evaluation.subregions):
            results = []
            labels = []

            subset = dsp.subset(
                bound,
                ref_ds,
                subregion_name="R{}_{}".format(bound_count, ref_ds.name)
            )

            results.append(utils.calc_time_series(subset))
            labels.append(subset.name)

            for t in target_ds:
                subset = dsp.subset(
                    bound,
                    t,
                    subregion_name="R{}_{}".format(bound_count, t.name)
                )
                results.append(utils.calc_time_series(subset))
                labels.append(subset.name)

            plots.draw_time_series(np.array(results),
                                   ref_ds.times,
                                   labels,
                                   'R{}'.format(bound_count),
                                   **plot_config.get('optional_args', {}))

    else:
        results = []
        labels = []

        results.append(utils.calc_time_series(ref_ds))
        labels.append(ref_ds.name)

        for t in target_ds:
            results.append(utils.calc_time_series(t))
            labels.append(t.name)

        plots.draw_time_series(np.array(results),
                               ref_ds.times,
                               labels,
                               'time_series',
                               **plot_config.get('optional_args', {}))