Esempio n. 1
0
def spatiofreq_noplt(chs, lat, lon, yrs, per='year'):
    '''Get grid-cell frequencies for no. of times a grid-cell falls within a
       contour describing a feature from metblobs.
       spatiofreq_nolt is new version by RJ designed to read in subsets of events
       designed for plotting within a multipanel plot
       AND to just output the values of "allmask" rather than plotting
       so that you can do plotting in the wrapper
       & calculate biases

       Need to first run SubSet_Events.evset_info to get chs

       per is used to determine if it's plotting per year or per CBs in this model

       If a subset of month or season is required, input only those cbs'''

    allmask = np.zeros((lat.shape[0], lon.shape[0]), dtype=np.float32)

    nblobs = len(chs)
    print 'Running spatiofreq on ' + str(nblobs) + ' CBs '

    for bl in range(nblobs):
        this_ch = chs[bl]
        mask = my.poly2mask(lon, lat, this_ch)
        allmask = allmask + np.float32(mask)

    if per == 'year':
        std_mask = allmask / len(yrs)
    elif per == 'cbs':
        std_mask = allmask / nblobs * 100
        print 'Dividing by number of blobs'
        print nblobs

    return std_mask
Esempio n. 2
0
                                            print 'Selecting rain under TTTs'
                                            masked_rain = np.ma.zeros((nttt4rain, nlat, nlon), dtype=np.float32)
                                            rcnt=0
                                            for rdt in range(nttt):
                                                this_dt=dates_ln[rdt]
                                                ix = my.ixdtimes(ttt_rain_dates, [this_dt[0]], \
                                                                 [this_dt[1]], [this_dt[2]], [0])
                                                if len(ix) >= 1:
                                                    ind=ix[0]
                                                    this_dt_rain=ttt_rain_dates[ind]
                                                    print 'Checking dates correspond'
                                                    print this_dt
                                                    print this_dt_rain
                                                    print 'Running poly to mask'
                                                    chmask = my.poly2mask(rlon, rlat, chs_ln[rdt])
                                                    print 'Finished running poly to mask'
                                                    r = np.ma.MaskedArray(rainsel[ind, :, :], mask=~chmask)
                                                    masked_rain[rcnt, :, :] = r
                                                    rcnt+=1

                                        elif under_of=='dayof':
                                            masked_rain=rainsel[:]

                                        # Get a timeseries of mean TTT rain from each event
                                        print 'Getting a rain value for each TTT event'
                                        reg_ttt_sum = np.zeros((nttt4rain), dtype=np.float32)
                                        reg_ttt_mean = np.zeros((nttt4rain), dtype=np.float32)

                                        if weightlats:
                                            latr = np.deg2rad(rlat)
                        indices = indices
                        if under_of == 'under':
                            chs_4rain = np.squeeze(np.asarray(chs_4rain))
                    nttt = len(indices)

                    print 'Selecting rain on TTT days'
                    rainsel = rain[indices, :, :]
                    ttt_rain_dates = rdtime[indices]
                    ndt = nttt

                    if under_of == 'under':

                        print 'Selecting rain under TTTs'
                        masked_rain = np.ma.zeros((ndt, nlat, nlon), dtype=np.float32)
                        for rdt in range(ndt):
                            chmask = my.poly2mask(rlon, rlat, chs_4rain[rdt])
                            r = np.ma.MaskedArray(rainsel[rdt, :, :], mask=~chmask)
                            masked_rain[rdt, :, :] = r

                    elif under_of=='dayof':
                        masked_rain=rainsel[:]

                    # Get a timeseries of mean TTT rain from each event
                    print 'Getting a rain value for each TTT event'
                    reg_ttt_sum = np.zeros((len(ttt_rain_dates)), dtype=np.float32)
                    reg_ttt_mean = np.zeros((len(ttt_rain_dates)), dtype=np.float32)
                    for st in range(len(ttt_rain_dates)):
                        reg_ttt_mean[st] = np.ma.mean(masked_rain[st,:,:])

                    # Getting a long term sum or mean
                    tottttrain=np.nansum(reg_ttt_mean)
Esempio n. 4
0
def spatiofreq6(m,chs,modname,lat,lon,yrs,per='year',\
                clim=(4,36,4),savefig=False,\
                col='col',cbar='ind',title=''):
    '''Get grid-cell frequencies for no. of times a grid-cell falls within a
       contour describing a feature from metblobs.
       spatiofreq6 is new version by RJ designed to read in subsets of events
       designed for plotting within a multipanel plot

       Need to first run SubSet_Events.evset_info to get chs

       per is used to determine if it's plotting per year or per CBs in this model

       If a subset of month or season is required, input only those cbs

    USAGE: if cbar='ind' will make it for just this plot
           if cbar='set' will make it at right assuming multipanel
           title can be string or variable e.g. modname '''

    allmask = np.zeros((lat.shape[0], lon.shape[0]), dtype=np.float32)

    nblobs = len(chs)
    print 'Running spatiofreq on ' + str(nblobs) + ' CBs '

    for bl in range(nblobs):
        this_ch = chs[bl]
        mask = my.poly2mask(lon, lat, this_ch)
        allmask = allmask + np.float32(mask)

    if col == 'col':
        cm = plt.cm.magma
    elif col == 'bw':
        cm = plt.cm.gist_gray_r

    if per == 'year':
        std_mask = allmask / len(yrs)
    elif per == 'cbs':
        std_mask = allmask / nblobs * 100
        print 'Dividing by number of blobs'
        print nblobs

    ## NEED TO DO THIS SINCE PCOLOR IS NOT SHADING VALUES OUTSIDE OF THE CLIMS
    cstd_mask = np.where(std_mask > clim[1], clim[1], std_mask)
    cstd_mask = np.where(cstd_mask < clim[0], clim[0], cstd_mask)
    # Plot pcolor
    pcolmap = m.pcolormesh(lon, lat, cstd_mask, cmap=cm, zorder=1)
    img = plt.gci()  # gets a reference for the image

    plt.clim(clim[0], clim[1])  # sets color limits of current image
    bounds = np.arange(clim[0], clim[1] + clim[2], clim[2])
    if savefig:
        f, ax = plt.gcf(), plt.gca()
        axcol = f.add_axes([0.93, 0.2, 0.02, 0.6])
        plt.colorbar(mappable=img, cax=axcol, boundaries=bounds)
        my.ytickfonts()
        plt.ylabel('grid-point count / year', fontdict=fd)
        plt.axes(ax)
        fname = '/FootprintFreqencygray-' + modname + '.png'
        plt.savefig(fname, dpi=150)
    else:
        f, ax = plt.gcf(), plt.gca()  # get reference and set axes
        if cbar == 'set':
            axcol = f.add_axes([0.91, 0.15, 0.01, 0.6])
            plt.colorbar(cax=axcol, boundaries=bounds)
        elif cbar == 'ind':
            plt.colorbar()
        my.ytickfonts(fontsize=10, fontweight='demibold')
        if per == 'year':
            if cbar == 'set':
                plt.ylabel('grid-point count / year', fontsize=10)
        elif per == 'cbs':
            if cbar == 'set':
                plt.ylabel('% of cbs covering gridbox', fontsize=10)
        plt.axes(ax)
        plt.title(title, fontsize=8, fontweight='demibold')

    return std_mask, img
Esempio n. 5
0
                                smp_edts = smp_edts[keyinds]
                                smp_degs = smp_degs[keyinds]
                                smp_mons = smp_mons[keyinds]
                                smp_chs = smp_chs[keyinds]
                                smp_ekeys = smp_ekeys[keyinds]
                                smp_olrmns = smp_olrmns[keyinds]

                            print 'Printing data for OLR means stored in mbs files'
                            print smp_olrmns


                            #   then use the outline to cut out data from sample
                            masked_var=np.ma.zeros((nsamp,this_nlat,this_nlon),dtype=np.float32)
                            cbmeans=np.zeros(nsamp,dtype=np.float32)
                            for rdt in range(nsamp):
                                chmask = my.poly2mask(lon, lat, smp_chs[rdt])
                                r = np.ma.MaskedArray(chosedata[rdt, :, :], mask=~chmask)
                                masked_var[rdt, :, :] = r
                                cbmeans[rdt]= np.ma.mean(r)

                            print 'Printing data for OLR means calculated here'
                            print cbmeans

                            if which_olr=='calc':
                                new_cbmeans=cbmeans
                            elif which_olr=='store':
                                new_cbmeans=smp_olrmns

                            # Adjust if thresh adjustment
                            if th_offset:
                                new_cbmeans=new_cbmeans+thdiff
                            for edt in range(len(edts)):
                                ix = my.ixdtimes(smpdtime, [edts[edt][0]], \
                                                 [edts[edt][1]], [edts[edt][2]], [0])
                                if len(ix) >= 1:
                                    date_check.append(edts[edt])
                                    chs_smpl.append(chs[edt])
                            date_check = np.squeeze(np.asarray(date_check))
                            chs_smpl = np.squeeze(np.asarray(chs_smpl))

                            #   then use the outline to cut out data from sample
                            masked_var = np.ma.zeros(
                                (nsamp, this_nlat, this_nlon),
                                dtype=np.float32)
                            cbmeans = np.zeros(nsamp, dtype=np.float32)
                            for rdt in range(nsamp):
                                chmask = my.poly2mask(lon, lat, chs_smpl[rdt])
                                r = np.ma.MaskedArray(chosedata[rdt, :, :],
                                                      mask=~chmask)
                                masked_var[rdt, :, :] = r
                                cbmeans[rdt] = np.ma.mean(r)

                            # Collect this model's data for anova
                            collect[cnt - 1] = cbmeans
                            print collect[cnt - 1]

                            # Make boxplot
                            plt.boxplot(cbmeans,
                                        positions=[cnt],
                                        notch=0,
                                        sym='+',
                                        vert=1,