Exemplo n.º 1
0
 def draw_transect(self,outpath,fname,radar=True):
     B = BirdsEye(self.W)
     m,x,y = B.basemap_setup()
     m.drawgreatcircle(self.lonA,self.latA,self.lonB,self.latB)
     # tv = 'Q_pert'
     tv = 'cref';lv=False
     # lv = 800
     # clvs = N.arange(-0.005,0.0052,0.0002)
     # cmap='BrBG'
     S = Scales('cref',False)
     clvs = S.clvs
     cmap = S.cm
     m.contourf(x,y,self.W.get(tv,utc=self.tidx,level=lv)[0,0,:,:],levels=clvs,cmap=cmap)
     B.save(outpath,fname)
     plt.close(B.fig)
Exemplo n.º 2
0
    def plot_verif(self,
                   data,
                   grid,
                   lats=None,
                   lons=None,
                   subplotn=0,
                   save=False,
                   utc=None,
                   vrbl=None,
                   scales=None,
                   cb=True,
                   levels=None):
        """ Plot verification postage stamp.
        """
        ax = self.ax.flatten()[subplotn]
        cmap, clvs = self.get_scales(vrbl=vrbl, lvs=levels)

        # Set up panel
        BE = BirdsEye(fpath=None,
                      ax=ax,
                      fig=self.fig,
                      grid=grid,
                      proj=self.proj,
                      use_basemap=self.use_basemap)

        # mplkwargs = dict('cmap':cmap, 'levels':clvs)
        BE.plot2D(
            data,
            save=False,
            cb=cb,
            # mplkwargs=mplkwargs,
            # cmap=cmap,cen_lat=cen_lat,cen_lon=cen_lon,W=W,**ld,
            cmap=cmap,
            levels=clvs,
        )
        ax.set_title("Verif")
        if save:
            self.save()
        print("Plotted verification panel.")
        return
Exemplo n.º 3
0
def plot_thumbnails(plotutc,vrbl):
    """ Plot data, including verification if it exists.

    There are three plotting options.
    The d01 and d02 domains, as is, and a third
    interpolation to common (inner) domain.
    """
    if vrbl == 'REFL_comp':
        R_large = Radar(plotutc,radardir)
        R_small = copy.copy(R_large)
        R_large.get_subdomain(**d01_limdict,overwrite=True)
        R_small.get_subdomain(**d02_limdict,overwrite=True)
    for nloop,dom in enumerate((1,1,2)):
        if nloop == 0:
            # This go round, do not zoom in on outer domain
            R = R_large
            ld = {}
            opt = "large"
        else:
            R = R_small
            ld = d02_limdict
            opt = "small"
        fhstr = utils.pretty_fhr(fhr)
        W = E.arbitrary_pick(dom=dom,dataobj=True)
        fig,axes = plt.subplots(nrows=3,ncols=4,figsize=(10,8))
        # fname = "test_verif_d{:02d}_{}_{}".format(dom,opt)
        fname = "test_verif_d{:02d}_{}_{}".format(dom,opt,fhstr)
        lats,lons = E.get_latlons(dom=dom)
        nx,ny = E.get_nx_ny(dom=dom)
        cen_lat,cen_lon = E.get_cenlatlons(dom=dom)
        # xx,yy = E.get_xx_yy(dom=dom)
        for plot,ax in zip(plotlist,axes.flat):
            print("Plotting {}".format(plot))
            if plot == 'Verif':
                # first plot - observed radar
                R.plot_radar(fig=fig,ax=ax,drawcounties=True,cb=False)
                ax.set_title(plot)

            elif isinstance(plot,int):
                memname = 'm{:02d}'.format(plot)
                # members (plots 3 to 12)
                # if dom == 1:
                    # Plot only overlapping domain
                    # data = E.get(
                print("Getting data.")
                data = E.get('REFL_comp',utc=plotutc,dom=dom,members=memname)[0,0,0,:,:]
            
                print("Setting up axis.")
                # BE = BirdsEye(ax=ax,fig=fig,proj='lcc')
                BE = BirdsEye(ax=ax,fig=fig,proj='merc')
                print("Plotting on axis.")
                BE.plot2D(data,save=False,drawcounties=True,
                                # lats=lats,lons=lons,
                                clvs=S.clvs,cmap=S.cm,
                                # nx=nx,ny=ny,
                                W=W,cb=False,
                                # x=xx,y=yy,
                                cen_lat=cen_lat,cen_lon=cen_lon,**ld)

                print("Setting axis title.")
                title = 'Member {:02d}'.format(plot)
                ax.set_title(title)

            else: 
                ax.grid('off')
                ax.axis('off')

        fig.tight_layout()
        outfpath = os.path.join(outdir,fname)
        utils.trycreate(outfpath)
        fig.savefig(outfpath)
        print("Saving figure to",outfpath)
Exemplo n.º 4
0
    def plot(
            self,
            fpath,
            fmt='default',
            W=None,
            vrbl='REFL_comp',
            # Nlim=None,Elim=None,Slim=None,Wlim=None):
            ld=None,
            lats=None,
            lons=None,
            fig=None,
            ax=None):
        """ Plot basic quicklook images.

        Setting fmt to 'default' will plot raw data,
        plus objects identified.
        """
        if ld is None:
            ld = dict()
        nobjs = len(self.objects)

        if fmt == 'default':
            # if fig is None:
            F = Figure(ncols=2, nrows=1, figsize=(8, 4), fpath=fpath)
            # F.W = W
            with F:
                ax = F.ax[0]
                # Plot raw array
                BE = BirdsEye(ax=ax, fig=F.fig)

                # Discrete colormap
                import matplotlib as M
                cmap_og = M.cm.get_cmap('tab20')
                # cmap_colors = [cmap_og(i) for i in range(cmap_og.N)]
                color_list = cmap_og(N.linspace(0, 1, nobjs))
                # cmap = M.colors.ListedColormap(M.cm.tab20,N=len(self.objects))
                cmap = M.colors.LinearSegmentedColormap.from_list(
                    'discrete_objects', color_list, nobjs)
                # bounds = N.linspace(0,nobjs,nobjs+1)
                # norm = M.colors.BoundaryNorm(bounds,cmap_og.N)
                masked_objs = N.ma.masked_less(self.obj_array, 1)

                BE.plot2D(
                    plottype='pcolormesh',
                    data=masked_objs,
                    save=False,
                    cb='horizontal',
                    #clvs=N.arange(1,nobjs),
                    W=W,
                    cmap=cmap,
                    mplkwargs={'vmin': 1},
                    **ld,
                    lats=lats,
                    lons=lons)

                ax = F.ax[1]
                S = Scales(vrbl)
                BE = BirdsEye(ax=ax, fig=F.fig)
                BE.plot2D(data=self.raw_data,
                          save=False,
                          W=W,
                          cb='horizontal',
                          lats=lats,
                          lons=lons,
                          cmap=S.cm,
                          clvs=S.clvs,
                          **ld)
        return
Exemplo n.º 5
0
    def plot_radar(self,
                   outdir=False,
                   fig=False,
                   ax=False,
                   fname=False,
                   Nlim=False,
                   Elim=False,
                   Slim=False,
                   Wlim=False,
                   cb=True,
                   drawcounties=False,
                   save='auto'):
        """
        Plot radar data.

        Args:

        save        :   (str,bool) - If 'auto', saving will only occur if
                        fig and ax are not specified.
                        If True, the figure is saved; if False, not.
        """
        # if not fig:
        # fig, ax = plt.subplots()
        # self.generate_basemap(fig,ax,Nlim,Elim,Slim,Wlim)
        #lons, lats = self.m.makegrid(self.xlen,self.ylen)
        if isinstance(Nlim, float):
            data, lats, lons = self.get_subdomain(Nlim, Elim, Slim, Wlim)
        else:
            data = self.data
            lats = self.lats  #flip lats upside down?
            lons = self.lons
            # x,y = self.m(*N.meshgrid(lons,lats))

        # xx,uy = self.m(lons,lats)
        # x,y = self.m(*N.meshgrid(lons,lats))
        # x,y = self.m(*N.meshgrid(lons,lats[::-1]))

        # Custom colorbar
        radarcmap = ct.reflect_ncdc(self.clvs)
        # radarcmap = ct.ncdc_modified_ISU(self.clvs)

        # Convert pixel levels to dBZ
        dBZ = self.get_dBZ(data)

        # dBZ[dBZ<0] = 0

        # def plot2D(self,data,fname,outdir,plottype='contourf',
        # save=1,smooth=1,lats=False,lons=False,
        # clvs=False,cmap=False,title=False,colorbar=True,
        # locations=False):
        if not fname:
            tstr = utils.string_from_time('output', self.utc)
            fname = 'verif_radar_{0}.png'.format(tstr)
        F = BirdsEye(fig=fig, ax=ax, proj=self.proj)
        if cb:
            cb = 'horizontal'
        if save is 'auto':
            if (fig is not False) and (ax is not False):
                save = False
            else:
                save = True
        F.plot2D(
            dBZ,
            fname,
            outdir=outdir,
            lats=lats,
            lons=lons,
            cmap=radarcmap,
            clvs=N.arange(5, 90, 5),
            cb=cb,
            cblabel='Composite reflectivity (dBZ)',
            drawcounties=drawcounties,
            save=save,
            lat_ts=50.0,
        )
Exemplo n.º 6
0
    def plot_fcst(
            self,
            data,
            grid,
            titles=None,
            scales=None,
            # W=None,cen_lon=None,cen_lat=None,
            vrbl=None,
            ld=None,
            save=True,
            cb=False,
            levels=None):
        """
        Args:
            data: 3D numpy array, (members,lat,lon).
        """
        self.nmems = data.shape[0]

        cmap, clvs = self.get_scales(scales=scales, vrbl=vrbl, lvs=levels)

        if ld == None:
            ld = {}

        # if W == None:
        # raise Exception("WRFOut object needed until refactoring complete.")

        # if cen_lat == None:
        # raise Exception("Need cen_lat and cen_lon until refactoring complete.")

        if titles is not None:
            title_itr = iter(titles)

        assert data.ndim == 3

        memlist = N.arange(self.naxes) - (self.naxes - self.nmems)

        for naxis, nmem, ax in zip(range(self.naxes), memlist, self.ax.flat):
            if naxis == 0:
                # This is for verification
                continue
            elif naxis == 1:
                # This is just for space
                # TODO: logic that skips this when there are 19 or whatever
                ax.grid("off")
                ax.axis("off")
                # continue
            else:
                assert nmem > -1
                BE = BirdsEye(fpath=None,
                              ax=ax,
                              fig=self.fig,
                              grid=grid,
                              proj=self.proj,
                              use_basemap=self.use_basemap)
                if titles is not None:
                    title = ax.set_title(next(title_itr))
                print("Plotting forecast member #{} on subplot #{}".format(
                    nmem, naxis))
                BE.plot2D(
                    data[nmem, :, :],
                    save=False,
                    cb=cb,
                    # clvs=clvs,cmap=cmap,cb=False,
                    # cen_lat=cen_lat,cen_lon=cen_lon,W=W,
                    # **ld,
                    levels=clvs,
                    cmap=cmap)
        if save:
            self.save()
Exemplo n.º 7
0
    def plot(
        self,
        data=None,
        outdir=False,
        fig=False,
        ax=False,
        fname=False,
        # Nlim=False, Elim=False, Slim=False,Wlim=False,
        cb=True,
        drawcounties=False,
        save='auto',
        lats=None,
        lons=None,
        proj='merc',
    ):
        """ Plot data.

        Todo:
            * This doesn't feel right. Plotting should be done elsewhere.

        Args:

        save        :   (str,bool) - If 'auto', saving will only occur if
                        fig and ax are not specified.
                        If True, the figure is saved; if False, not.
        """
        # if isinstance(Nlim,float):
        # data, lats, lons = self.get_subdomain(Nlim,Elim,Slim,Wlim)
        # else:
        # if not given, try to use self's

        if data == None:
            data = self.get()
        # data = getattr(self,'data',data)
        if lats == None:
            lats = self.lats
        # lats = getattr(self,'lats',lats)
        if lons == None:
            lons = self.lons
        # lons = getattr(self,'lons',lons)

        # Need to implement options for this
        cmap = None
        clvs = None

        if not fname:
            tstr = utils.string_from_time('output', self.utc)
            fname = 'verif_ST4_{0}.png'.format(tstr)
        F = BirdsEye(fig=fig, ax=ax, proj=proj)
        if cb:
            cb = 'horizontal'
        if save == 'auto':
            if (fig is not False) and (ax is not False):
                save = False
            else:
                save = True
        F.plot2D(
            data,
            fname,
            outdir=outdir,
            lats=lats,
            lons=lons,
            cmap=cmap,
            clvs=clvs,
            cb=cb,
            cblabel='Accumulated precipitation (mm)',
            drawcounties=drawcounties,
            save=save,
            lat_ts=50.0,
        )
Exemplo n.º 8
0
                # first plot - observed radar
                R.plot_radar(fig=fig,ax=ax,drawcounties=True,cb=False)
                ax.set_title(plot)

            elif isinstance(plot,int):
                memname = 'm{:02d}'.format(plot)
                # members (plots 3 to 12)
                # if dom == 1:
                    # Plot only overlapping domain
                    # data = E.get(
                print("Getting data.")
                data = E.get('REFL_comp',utc=plotutc,dom=dom,members=memname)[0,0,0,:,:]
            
                print("Setting up axis.")
                # BE = BirdsEye(ax=ax,fig=fig,proj='lcc')
                BE = BirdsEye(ax=ax,fig=fig,proj='merc')
                print("Plotting on axis.")
                BE.plot2D(data,save=False,drawcounties=True,
                                # lats=lats,lons=lons,
                                clvs=S.clvs,cmap=S.cm,
                                # nx=nx,ny=ny,
                                W=W,cb=False,
                                # x=xx,y=yy,
                                cen_lat=cen_lat,cen_lon=cen_lon,**ld)

                print("Setting axis title.")
                title = 'Member {:02d}'.format(plot)
                ax.set_title(title)

            else: 
                ax.grid('off')