示例#1
0
文件: WRFTools.py 项目: jcmt/WRFTools
def plot_domains(dlist):
  from mpl_toolkits.basemap import Basemap
  import matplotlib.pyplot as plt
  import ticks

  wrf = {}
  for i in range(1, len(dlist)+1):
    wrf['d%02d'%i] = get(dlist[i-1])

  if wrf['d01'].nc.MAP_PROJ == 1:
    proj = 'lcc'
  elif wrf['d01'].nc.MAP_PROJ == 3:
    proj = 'merc'
  else:
    return('Projection not suported')

  lat_1 = wrf['d01'].nc.TRUELAT1
  lat_2 = wrf['d01'].nc.TRUELAT2
  lon_0 = wrf['d01'].nc.CEN_LON
  lat_0 = wrf['d01'].nc.CEN_LAT
  llcrnrlat = wrf['d01'].lat().min() - 5
  urcrnrlat = wrf['d01'].lat().max() + 5
  llcrnrlon = wrf['d01'].lon().min() - 5
  urcrnrlon = wrf['d01'].lon().max() + 5

  plt.figure()
  ax = plt.axes()
  m = Basemap(projection=proj, llcrnrlat=llcrnrlat, urcrnrlat=urcrnrlat, \
  llcrnrlon=llcrnrlon, urcrnrlon=urcrnrlon, lat_1=lat_1, \
  lat_2=lat_2, lat_0=lat_0, lon_0=lon_0, resolution='i')

  #m.drawcoastlines(color='black', linewidth=2)
  #m.drawcountries(linewidth=1.5)
  m.bluemarble()

  parallels = ticks.loose_label(llcrnrlat, urcrnrlat)
  m.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=14)
  meridians = ticks.loose_label(llcrnrlon, urcrnrlon)
  m.drawmeridians(meridians, labels=[0, 0, 0, 1], fontsize=14)

  for i in range(1,len(dlist)+1):
    xb = var_border(wrf['d%02d'%i].lon())
    yb = var_border(wrf['d%02d'%i].lat())

    x, y = m(xb,yb)
    tx, ty = m(wrf['d%02d'%i].lon()[-1,0], wrf['d%02d'%i].lat()[-1,0]+0.5)
    colors = ['lightblue', 'pink', 'lightgreen', 'lightsalmon', 'silver', 'khaki']

    ax.plot(x,y, lw=2, c=colors[i-1])
    ax.annotate('d%02d'%i, xy=(tx, ty), fontsize=16, color=colors[i-1])
示例#2
0
文件: WRFTools.py 项目: jcmt/WRFTools
  def pcolor(self, VAR, tstep=None, colorbar=True, level=0, pcolor=False, norm=None, coastcolor='k', **kargs):
    '''
    lat-lon plot on a base map

    usage:
       pcolor(VAR, colormap, colorbar, tstep, level, shading, norm)

       VAR is a wrfout variable (string) or a 2D numpy array
       if VAR is tring a tstep and level must be given to acquire the
       variable. IF NOT the first level and time will be used
       shading can be one of: flat (default), interp (contourf) or None
       (pcolor)
    '''
    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import ticks

    if not tstep:
      return "A time step must be specified..."
    else:
      if isinstance(VAR, str):
        if len(self.nc.variables[VAR].dimensions) == 4:
          VAR = self.getvar(VAR, tstep=tstep, nlev=level, ny=':', nx=':')
        elif len(self.nc.variables[VAR].dimensions) == 3:
          VAR = self.getvar(VAR, tstep=tstep)
        elif len(self.nc.variables[VAR].dimensions) == 2:
          VAR = self.getvar(VAR)

      if self.nc.MAP_PROJ == 1:
        proj = 'lcc'
      elif self.nc.MAP_PROJ == 3:
        proj = 'merc'
      else:
        return('Projection not suported')

      lat_1 = self.nc.TRUELAT1
      lat_2 = self.nc.TRUELAT2
      lon_0 = self.nc.CEN_LON
      lat_0 = self.nc.CEN_LAT
      llcrnrlat = self.lat().min()
      urcrnrlat = self.lat().max()
      llcrnrlon = self.lon().min()
      urcrnrlon = self.lon().max()

      res = 'i'
      if self.nc.DX < 25000:
        res = 'h'

      plt.figure()
      ax = plt.axes()
      m = Basemap(projection=proj, llcrnrlat=llcrnrlat, urcrnrlat=urcrnrlat, \
                    llcrnrlon=llcrnrlon, urcrnrlon=urcrnrlon, lat_1=lat_1, \
                    lat_2=lat_2, lat_0=lat_0, lon_0=lon_0, resolution=res, area_thresh=10000)

      m.drawcoastlines(color=coastcolor, linewidth=2)
      m.drawcountries(color=coastcolor, linewidth=1.5)

      parallels = ticks.loose_label(self.lat().min(),self.lat().max())
      m.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=14)
      meridians = ticks.loose_label(self.lon().min(),self.lon().max())
      m.drawmeridians(meridians, labels=[0, 0, 0, 1], fontsize=14)

      x, y = m(self.lon(), self.lat())

      if not pcolor:
        if not norm:
          levels = np.linspace(VAR.min(), VAR.max(), 200)
        else:
          levels = np.linspace(norm.min(), norm.max(), 200)
        cs = ax.contourf(x, y, VAR, levels=levels, **kargs)
      else:
        cs = ax.pcolormesh(x, y, VAR, **kargs)

      ax.set_title(self.time(tstep=tstep))

      if colorbar:
        fmt = plt.matplotlib.ticker.FormatStrFormatter("%.1f")
        if not norm:
          clev = np.linspace(np.round(VAR.min()), np.round(VAR.max()), 10, endpoint=True)
        else:
          clev = np.linspace(np.round(norm.min()), np.round(norm.max()), 10, endpoint=True)
        cbar = m.colorbar(cs, location='right', ticks=clev, format=fmt, pad='5%')
        cbar.ax.tick_params(labelsize=12)
      return ax, m