Example #1
0
    def generate_radar_latlons(
            self):  #(dim0,dim1,ll_lon,ll_lat,ur_lon,ur_lat):
        '''
        Generate lats,lons for polar stereographically 
        projected radar from data characteristics 
        (predetermined cases: new Dutch radar, old Dutch radar, European composite)
        '''

        import pickle

        dim0, dim1 = self.values.shape

        if dim0 == 765:  ## high-res dutch radar
            raddomain = 'nlrad'
            (ll_lat, ll_lon) = (49.362, 0.)
            (ur_lat, ur_lon) = (55.389, 10.856)
        elif dim0 == 256:  ## low-res dutch radar
            raddomain = 'oldrad'
            (ll_lat, ll_lon) = (49.769, 0.)
            (ur_lat, ur_lon) = (54.818, 9.743)
        elif dim0 == 512:  ## european composite
            raddomain = 'eurrad'
            (ll_lat, ll_lon) = (41.937, -9.271)
            (ur_lat, ur_lon) = (58.089, 20.453)

        home = os.getenv('HOME')
        mapdir = os.path.join(home, 'python/tools')
        mappkl = os.path.join(mapdir, raddomain + '.pkl')
        if os.path.exists(mappkl):
            f = open(mappkl, 'r')
            polmap = pickle.load(f)
            f.close()

        elif 0:  # possibly faster (!?)
            sys.path.append(mapdir)  #'/usr/people/plas/python/tools'
            import bmap

            polmap = bmap.myMap(domain=raddomain, modelname='radar').bmap

        else:  # if this fails (?)
            # import basemap:
            from mpl_toolkits.basemap import Basemap  # map functionality

            polmap = Basemap(projection='stere',
                             lat_0=90.,
                             lon_0=0.,
                             lat_ts=60.,
                             llcrnrlon=ll_lon,
                             llcrnrlat=ll_lat,
                             urcrnrlon=ur_lon,
                             urcrnrlat=ur_lat)

        lons, lats = polmap.makegrid(dim1, dim0, returnxy=False)
        #print 'LON',lons,'\nLAT',lats
        self.latlons = (lats, lons)
Example #2
0
    def plotmap(self,domain = 'nl',colors=None,colormap='jet',ml = 60,lsmask_colour = 'black',outdir = './'):

        '''
        Create a map with one of the levels and the line along which the slice is taken indicated
        '''

        import matplotlib.pyplot as plt
        import matplotlib.cm as CM

        if not self.__dict__.has_key('leadtime'):
            self.leadtime = 0

        # create a figure
        fig  = plt.figure()
        ax   = fig.add_subplot(1,1,1)

        modelname = 'Harmonie'
        parametername = 'Parameter '+str(self.param)
        cmap = CM.get_cmap(colormap)

        # create the map
        plot_map = bmap.myMap(domain = domain, modelname = modelname)
        plot_map.xymap(latlons = (self.latlons[0],self.latlons[1]), domain = domain, modelname = modelname)
        plot_map.set_axes(ax)
        plot_map.dress_up_map(domain = domain,lsmask_colour = lsmask_colour)
        pcont = plot_map.bmap.contourf(plot_map.x,plot_map.y,self.data[ml],# here takes the last, maybe default to level?
                                       20,cmap=cmap,extend='both',ax=ax)

        # colorbar:
        cb = fig.colorbar(pcont,shrink=0.9, extend='both',format='%.2f') #format='%.1e') 
        cb.set_label(parametername,fontsize=9)
        for t in cb.ax.get_yticklabels():
            t.set_fontsize(9)

        ax.set_title('Parameter '+str(self.param)+' (level '+str(ml)+'), * start, p end', fontsize=9)
        #xa,ya   = plot_map.bmap(4.878867,52.375345) #NL148
        #print xa,ya

        # calculate where the points are on the map and draw them
        xs,ys = plot_map.bmap(self.line[1],self.line[0]) 
        plot_map.bmap.plot(xs,ys,'o',color='white')

        # start and endpoint
        plot_map.bmap.plot(xs[0],ys[0],'*',color='white',markersize=15)
        plot_map.bmap.plot(xs[-1],ys[-1],'p',color='white',markersize=15)
        
        # save to file
        plotfile = os.path.join(outdir,'slice_map_'+self.case+'_'+str(self.param)+'.png')
        plotfile = os.path.join(outdir,'slice_map_{case}_{par}_{date}+{lt}.png'.format(case=self.case,par=self.param,date=self.date,lt=str(self.leadtime).zfill(3)))
Example #3
0
    domain = 'eur'
    modelname = 'Harmonie'
    parametername = 'Precipitation (liquid)'
    lsmask_colour = 'black'  # no need to set this

    ## Instantiate a figure (create a canvas to draw on)
    global fig
    fig = matplotlib.pyplot.figure()
    ax = fig.add_subplot(1, 1, 1)

    # set title
    ax.set_title('Example of {model} on domain {dom}'.format(model=modelname,
                                                             dom=domain))

    ## make a basemap instance, set location, size, draw meridians etc
    plot_map = bmap.myMap(domain=domain, modelname=modelname)
    plot_map.xymap(latlons=(lats, lons), domain=domain, modelname=modelname)
    plot_map.set_axes(ax)
    plot_map.dress_up_map(domain=domain, lsmask_colour=lsmask_colour)

    pcplevels = (0.1, 0.3, 1, 3, 10, 30, 100)

    # Use your own colours...
    pcpcolors = ('white', 'lightgrey', 'grey', 'LightCoral', 'red', 'black')
    cmap = None

    # ... or use something like this:
    #pcpcolors = None
    #cmap      = CM.get_cmap('jet')
    #cmap      = CM.get_cmap('hot_r')
Example #4
0
def vectorplot(grib_info_x, grib_info_y, domain = 'nl', outdir = './', grib_info = {}):
                
    '''A quiver plot based on two fields, perhaps add an optional underlying field '''

    global fig
    
    # fig  = matplotlib.pyplot.figure()
    ax   = fig.add_subplot(1,1,1)

    plot_map = bmap.myMap(domain = domain, modelname = grib_info_x['model'])
    plot_map.xymap(latlons = grib_info_x['latlons'], domain = domain, modelname = grib_info_x['model'])
    plot_map.set_axes(ax)
    plot_map.dress_up_map(domain = domain)
     
    if grib_info.has_key('parameter'):
        print 'huh?',grib_info
        if grib_info['colormap'] == None and  grib_info['plotcolors'] == None: 
            cmap = CM.get_cmap('jet')
        elif grib_info['colormap'] is not None:
            cmap = grib_info['colormap']
            cmap.set_over(color='k',alpha = None)
            grib_info['plotcolors'] = None
        else:
            pass
        
    #title
    ax.set_title( grib_info_x['model']+': '+grib_info_x['name']+' (parameter '+str(grib_info_x['param'])+\
                      ', level '+str(grib_info_x['level'])+\
                      ') \n at '+str(grib_info_x['date'])+suf3(grib_info_x['time'],suf=2)+'+'+suf3(grib_info_x['leadtime']) 
                  )

    #thinning factor:
    if domain == 'nl' and 'Harmonie' in grib_info_x['model']:
        thf = 8
    elif domain == 'eur':
        thf = 20
    elif domain in ('rot','ijs') and 'Harmonie' in grib_info_x['model'] :
        thf = 1
    else:
        thf = 8
        

    # actual plots
    arrows = plot_map.bmap.quiver(plot_map.x[::thf,::thf],plot_map.y[::thf,::thf],
                                  grib_info_x['values'][::thf,::thf],grib_info_y['values'][::thf,::thf],
                                  scale=400,
                                  color = 'red',
                                  ax=ax)

 
    if 0: # maybe to plot second
        # colorbar:
        cb = fig.colorbar(pcont,shrink=0.9, extend='both',format='%.1e') 
        cb.set_label(grib_info['name'],fontsize=9)

    # naming and saving
    mkdir_p(outdir)

    plotfile = os.path.join(outdir,
                            'test_vec_'+grib_info_x['source_short']+'_'+str(domain)+'_'\
                                +str(grib_info_x['param'])+'_'+str(grib_info_x['level'])+'_'\
                                +str(grib_info_x['date'])+suf3(grib_info_x['time'],suf=2)+'+'+suf3(grib_info_x['leadtime'])+'.png')

    sf = fig.savefig(plotfile)
    print 'created plot: '+plotfile

    textfile = os.path.join(outdir,'README.txt')
    if os.path.isfile(textfile):
        pass
    else:
        f = open(textfile,'wt')
        f.write('Plot generated by fabriek.py (plottypes.vectorplot()). \n\nContact [email protected]')
        f.close()

        
    #print dir(arrows)
    #sys.exit(1)
    #clean up
    #for coll in arrows.collections:
    #    coll.remove()
    
    try:
        for coll in pcont.collections:
            coll.remove()

        for coll in plcont.collections:
            coll.remove()

        # delete colorbar
        cbcoll = fig.get_axes()[1]
        cbcoll.collections = []
        fig.delaxes(cbcoll)
        fig.subplots_adjust(right=0.90)

    except:
        pass
Example #5
0
def contourplot(grib_info,domain = 'nl',outdir = './',
                lsmask_colour = 'black',exarg = 'both',
                v=0):
    
    global fig

    if grib_info == None:
        print 'No plot info'
        return None


    #fig  = matplotlib.pyplot.figure()
    ax   = fig.add_subplot(1,1,1)

    t1 = time.clock()
    if v: print 'contourplot: dressing up',time.clock() - t1
    plot_map = bmap.myMap(domain = domain, modelname = grib_info['model'])
    plot_map.xymap(latlons = grib_info['latlons'], domain = domain, modelname = grib_info['model'])
    plot_map.set_axes(ax)
    plot_map.dress_up_map(domain = domain,lsmask_colour = lsmask_colour)
    if v: print 'done dressing up',time.clock() - t1
    
    diy = True # switch to use private plotting (True) or testmap in myMap instance (False)
    if diy:
        if grib_info['plotlevels'] == None:
            gmax,gmin = grib_info['values'].max(),grib_info['values'].min()
            grib_info['plotlevels'] = numpy.arange(gmin,gmax,(gmax-gmin)/10.)
            print gmin,gmax,grib_info['plotlevels']
        if grib_info['colormap'] == None and grib_info['plotcolors'] == None: 
            cmap = CM.get_cmap('jet')
        elif grib_info['colormap'] is not None:
            cmap = grib_info['colormap']
            cmap.set_over(color='k',alpha = None)
            grib_info['plotcolors'] = None

            # for gray-scale pictures, do not extend the colorbar beyond [0,1]
            #for item in grib_info.keys(): print item, grib_info[item]
            #if grib_info['grib_indicator'] == 71: # does not always work?
            if grib_info['param'] == 71: 
                #if cmap in (CM.get_cmap('gray'),CM.get_cmap('gray_r'),CM.get_cmap('binary_r')):
                exarg = 'neither'

        elif grib_info['plotcolors'] is not None:
            cmap = None
        else:
            pass

        # validtime:
        # for observations, make sure leadtime is 0 (were inserted into arbitrary grib message)
        if grib_info['model'] in ('Radar NL','Radar EUR','MSG'):
            grib_info['leadtime'] = 0 

        if int(grib_info['time']) > 100: # could be refined to insert minutes for example
            grib_info['time'] = int(grib_info['time'])/100

        validtime = dt.datetime.strptime(str(grib_info['date'])[0:8],'%Y%m%d') + \
            dt.timedelta(hours = int(grib_info['time']) ) + \
            dt.timedelta(hours = int(grib_info['leadtime']) )
        validdtg = validtime.strftime('%Y-%m-%d, %H')

        #print str(grib_info['date']),str(grib_info['time']),dt.datetime.strptime(str(grib_info['date'])[0:8],'%Y%m%d'),validtime, validdtg; #sys.exit(1)

        #title
        ax.set_title( grib_info['model']+': '+grib_info['name']+' \n(parameter '+str(grib_info['param'])+\
                          ', level '+str(grib_info['level'])+\
                          ') \n at '+str(grib_info['date'])+suf3(grib_info['time'],suf=2)+\
                          '+'+suf3(grib_info['leadtime'])+': '+str(validtime)+' UTC') 


        # actual plots
        if v: print 'actual plot',time.clock() - t1
        if v: print 'x,y,data shape',plot_map.x.shape,plot_map.y.shape,grib_info['values'].shape
        pcont = plot_map.bmap.contourf(plot_map.x,plot_map.y,grib_info['values'],
                                       grib_info['plotlevels'],
                                       cmap=cmap,colors = grib_info['plotcolors'],extend=exarg,ax=ax)

        if grib_info['draw_line']: 
            if v: print 'drawing contour lines: ',
            if grib_info.has_key('lines'): 
                lines = grib_info['lines']
                print lines
            else:
                print 'contourplot draw_line: is this an option?'
                lines = grib_info['plotlevels'] # probably redundant
            plcont = plot_map.bmap.contour(plot_map.x,plot_map.y,\
                                               grib_info['values'],lines,colors='black',ax=ax)

        # colorbar:
        cb = fig.colorbar(pcont,shrink=0.9, extend='both',format='%.2f') #format='%.1e') 
        cb.set_label(grib_info['name'],fontsize=9)
        for t in cb.ax.get_yticklabels():
            t.set_fontsize(9)
        if grib_info['draw_line']: 
            cb.add_lines(plcont)

        # naming and saving
        # include source,domain, and parameter/level, and starting date + leadtime
        if v: print 'getting plot title',time.clock() - t1        
        mkdir_p(outdir)

        plotfile = os.path.join(outdir,
                                'test_'+grib_info['source_short']+'_'+str(domain)+'_'\
                                +str(grib_info['param'])+'_'+str(grib_info['level'])+'_'\
                                +str(grib_info['date'])+suf3(grib_info['time'],suf=2)+'+'+\
                                suf3(grib_info['leadtime'])+'.png')
        if v: print 'saving plot',time.clock() - t1        
        fig.savefig(plotfile, dpi = 100) #dpi = 300)

        cmd = 'convert -trim '+plotfile+' '+plotfile
        #os.system(cmd)
        print 'created plot: '+plotfile,time.clock() - t1


        textfile = os.path.join(outdir,'README.txt')
        if os.path.isfile(textfile):
            pass
        else:
            f = open(textfile,'wt')
            f.write('Plot generated by fabriek.py (plottypes.contourplot()). \n\nContact [email protected]')
            f.close()

        #del(cbcoll)
        del(ax)

        fig.clf()
        gc.collect() # force garbage collection

        return plotfile
Example #6
0
    else:
        gr12.read_from_grib(gfile, 61, level=457, TR=0, case='penalty')

        gr11 = getinfo.plotinfo(model='model')
        gr11.read_from_grib(gfileprev, 61, level=457, TR=0, case='penalty')

        gr12.values = gr12.values - gr11.values

    rad12.resample(gr12)
    gr12.get_settings(preset='PCP')
    #gr12.plot(domain = 'nl')

#sys.exit(0)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
mplot = bmap.myMap(domain='nl', dbg=True)
mplot.xymap(latlons=gr12.latlons, domain='nl')
mplot.set_axes(ax)
mplot.dress_up_map(domain='nl', lsmask_colour='k')
gcont = mplot.bmap.contourf(mplot.x,
                            mplot.y,
                            gr12.values, [2., 100],
                            colors='red',
                            ax=ax)
rcont = mplot.bmap.contourf(mplot.x,
                            mplot.y,
                            rad12.values, [2., 100],
                            colors='darkgreen',
                            ax=ax)

ax.set_title(
Example #7
0
    def plot(self, out=None, domain='nl', lsmask_colour='black'):

        if self.values == []: return None
        if self.latlons == None: return None

        sampledir = '/usr/people/plas/python/tools'

        if 1:  # to do: test if domain is not some other custom definition
            if domain == 'nl':
                grbs = pygrib.open(os.path.join(sampledir,
                                                'samplegrib_SA.grb'))
                nlgrid = grbs[1].latlons()
                #print nlgrid; sys.exit(0)
                self.resample(nlgrid)
                grbs.close()
            elif domain == 'eur':
                grbs = pygrib.open(os.path.join(sampledir,
                                                'samplegrib_LA.grb'))
                eurgrid = grbs[1].latlons()
                self.resample(eurgrid)
                grbs.close()
        else:  #
            print "resample failed, try original grid: "

        import matplotlib.pyplot as plt
        from matplotlib import cm as CM
        from matplotlib import rcParams
        rcParams[
            'contour.negative_linestyle'] = 'solid'  # solid lines for negative contours

        home = os.getenv('HOME')
        sys.path.append(os.path.join(home, 'python/tools'))
        import bmap
        #print bmap.__file__; sys.exit(0)

        # create figure canvas, axes
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)

        # create mapping, draw land-sea mask, meridians etc
        mplot = bmap.myMap(domain=domain, dbg=True)
        mplot.xymap(latlons=self.latlons,
                    domain=domain)  #, modelname = self.model)
        #print 'x,y map:',domain,mplot.x.shape, mplot.y.shape
        mplot.set_axes(ax)
        mplot.dress_up_map(domain=domain, lsmask_colour=lsmask_colour)

        #mplot.bmap.fillcontinents(color='Wheat',lake_color='#99ffff')
        #mplot.bmap.drawmapboundary(fill_color='aqua')

        exarg = 'max'  #'both' # extending colors beyond the given levels, could be 'both, 'neither', see matplotlib doc

        #### where to put this kind of intelligence?
        if self.plot_levels == None:
            if self.plot_range:  # is not None: make range of plot levels
                r = self.plot_range  # for brevity:
                try:
                    self.plot_levels = np.arange(r[0], r[1], r[2])
                except:
                    print 'Not a valid range definition: ', r  #,'\nUsing default (0,10,1)'
            else:
                gmax, gmin = self.values.max(), self.values.min()
                self.plot_levels = np.arange(gmin, gmax, (gmax - gmin) / 10.)
                print gmin, gmax, self.plot_levels

        if self.colormap == None and self.colors == None:
            self.colormap = CM.get_cmap('jet')
        elif self.colors is not None:
            self.colormap = None
        elif self.colormap is not None:
            cmap = self.colormap
            try:
                self.colormap = CM.get_cmap(cmap)
            except:
                print 'Is not available? See matplotlib doc ', self.colormap
                sys.exit(0)
            #cmap.set_over(color='k',alpha = None) # what, why?
            self.colors = None
        else:
            pass

        if self.__dict__.has_key('lines'):
            lines = self.lines
            print lines
        elif self.__dict__.has_key('line_range'):
            if self.line_range == None: pass
            r = self.line_range
            print r
            try:
                lines = np.arange(r[0], r[1], r[2])
            except:
                print 'Not a valid range definition: ', r
        else:
            print 'contourplot draw_line: using levels ', self.plot_levels
            lines = self.plot_levels  # probably redundant

        # check if name is well-behaved:
        # characters not allowed in strings for file names:
        import re
        re_ill = re.compile(r"^[^<>/{}[\]~`\s\*\\\?\|]*$")

        if re_ill.match(str(self.case)):
            #print("RE2: All chars are valid.",self.case)
            pass