示例#1
0
def set_image_color_percentiles(image, plo, phi):
	# hackery...
	I = image.copy().ravel()
	I.sort()
	N = len(I)
	mn = I[max(0, int(round(plo * N / 100.)))]
	mx = I[min(N-1, int(round(phi * N / 100.)))]
	gci().set_clim(mn, mx)
	return (mn,mx)
示例#2
0
    def draw_inset_colorbar(self,
                            format=None,
                            label=None,
                            ticks=None,
                            fontsize=11,
                            **kwargs):
        defaults = dict(width="25%",
                        height="5%",
                        loc=7,
                        bbox_to_anchor=(0., -0.04, 1, 1))
        setdefaults(kwargs, defaults)

        ax = plt.gca()
        im = plt.gci()
        cax = inset_axes(ax, bbox_transform=ax.transAxes, **kwargs)
        cmin, cmax = im.get_clim()

        if (ticks is None) and (cmin is not None) and (cmax is not None):
            cmed = (cmax + cmin) / 2.
            delta = (cmax - cmin) / 10.
            ticks = np.array([cmin + delta, cmed, cmax - delta])

        tmin = np.min(np.abs(ticks[0]))
        tmax = np.max(np.abs(ticks[1]))

        if format is None:
            if (tmin < 1e-2) or (tmax > 1e3):
                format = '$%.1e$'
            elif (tmin > 0.1) and (tmax < 100):
                format = '$%.1f$'
            elif (tmax > 100):
                format = '$%i$'
            else:
                format = '$%.2g$'
                #format = '%.2f'

        kwargs = dict(format=format, ticks=ticks, orientation='horizontal')

        if format == 'custom':
            ticks = np.array([cmin, 0.85 * cmax])
            kwargs.update(format='$%.0e$', ticks=ticks)

        cbar = plt.colorbar(cax=cax, **kwargs)
        cax.xaxis.set_ticks_position('top')
        cax.tick_params(axis='x', labelsize=fontsize)

        if format == 'custom':
            ticklabels = cax.get_xticklabels()
            for i, l in enumerate(ticklabels):
                val, exp = ticklabels[i].get_text().split('e')
                ticklabels[i].set_text(r'$%s \times 10^{%i}$' %
                                       (val, int(exp)))
            cax.set_xticklabels(ticklabels)

        if label is not None:
            cbar.set_label(label, size=fontsize)
            cax.xaxis.set_label_position('top')

        plt.sca(ax)
        return cbar, cax
示例#3
0
    def draw_inset_colorbar(self, format=None):
        ax = plt.gca()
        im = plt.gci()
        cax = inset_axes(ax, width="25%", height="5%", loc=7)
        cmin, cmax = im.get_clim()
        cmed = (cmax + cmin) / 2.
        delta = (cmax - cmin) / 10.

        ticks = np.array([cmin + delta, cmed, cmax - delta])
        tmin = np.min(np.abs(ticks[0]))
        tmax = np.max(np.abs(ticks[1]))

        if format is None:
            if (tmin < 1e-2) or (tmax > 1e3):
                format = '%.1e'
            elif (tmin > 0.1) and (tmax < 100):
                format = '%.1f'
            elif (tmax > 100):
                format = '%i'
            else:
                format = '%.2g'

        cbar = plt.colorbar(cax=cax,
                            orientation='horizontal',
                            ticks=ticks,
                            format=format)
        cax.xaxis.set_ticks_position('top')
        cax.tick_params(axis='x', labelsize=10)
        plt.sca(ax)
        return cbar
示例#4
0
def usecolormap(filename, name=None):
    if name is None:
        import os
        name = os.path.splitext(filename)[0]
        pass
    mymap = makecolormap(filename, name)
    import pylab
    #pylab.rc( 'image', cmap = name )
    gci = pylab.gci()
    gci.set_cmap(mymap)
    return
示例#5
0
def usecolormap( filename, name = None ):
    if name is None:
        import os
        name = os.path.splitext( filename )[0]
        pass
    mymap = makecolormap( filename, name )
    import pylab
    #pylab.rc( 'image', cmap = name )
    gci = pylab.gci()
    gci.set_cmap( mymap )
    return
示例#6
0
def X_axis_label_format(format):
    """
  Define the X-axis label format
  """
    this_axes = py.gca()
    this_figure = py.gcf()
    this_image = py.gci()
    # Set a tick on every integer that is multiple of base in the view interval
    # which is 1 in this case
    majorLocator = py.MultipleLocator(1)
    majorFormatter = py.FormatStrFormatter(format)
    this_axes.xaxis.set_major_locator(majorLocator)
    this_axes.xaxis.set_major_formatter(majorFormatter)
示例#7
0
    def __init__(self, im=None):
        """if figure.image is not passed as parameter,
        use current image"""
        from pylab import colorbar, gci
        if im == None:
            im = gci()

        ax = im.axes
        cb = colorbar(im, ax=ax)
        self.colorbar_instance = cb
        canvas = ax.figure.canvas
        if not hasattr(canvas, '_colorbars'):
            canvas._colorbars = {}
        canvas._colorbars[cb.ax] = cb
        canvas.mpl_connect('scroll_event', self.onWheel)
        return
示例#8
0
    def __init__(self, im=None):
        """if figure.image is not passed as parameter,
        use current image"""
        from pylab import colorbar, gci
        if im == None:
            im = gci()

        ax = im.axes
        cb = colorbar(im, ax=ax)
        self.colorbar_instance = cb
        canvas = ax.figure.canvas
        if not hasattr(canvas,'_colorbars'):
            canvas._colorbars = {}
        canvas._colorbars[cb.ax] = cb
        canvas.mpl_connect('scroll_event', self.onWheel)
        return
示例#9
0
文件: ROI.py 项目: shibun/misc
def new_ROI(im=None, shape='polygon'):
    """Set up an ROI picker and return it. This is the only way that the
    ROI class should be involked. Requires an input image (the thing
    returned by imshow), or can try the latest image in the current
    axes."""
    plt.ion()
    im = plt.gci() if im is None else im
    if im == None: raise ValueError('No image to pick on')
    ax = im.axes
    fig = ax.figure
    if shape=='polygon' or shape=='p':
        cursor = ROI(im, ax, fig)
    elif shape=='circle' or shape=='c':
        cursor = ROIcircle(im, ax, fig)
    elif shape=='ellipse' or shape=='e':
        cursor = ROIellipse(im, ax, fig)
    elif shape=='rectangle' or shape=='r':
        raise NotImplementedError("Rectangle ROI not yet created")
    else:
        raise ValueError("ROI shape must not understood")
    return cursor
示例#10
0
def data2roms(data,grd,sparams,**kargs):
  '''
  Interpolates data to roms 3D grid.

  The dict data must contain the prognostic variables temp, salt, u,
  v (3d) and ssh (zeta, 2d), as well as lon, lat (2d), depth (1d) and
  time/date info: date (data date), date0 (reference date) and time
  (difference between date and date0). The input data can be provided
  by load_data.

  Parameters
  ----------
  data : dict with prognostic variables
  grd : ROMS netcdf grid file
  sparams : s-coordinates parameters, theta_s,theta_b, hc and NLevels

  **kargs:
  ij : axis for vertical interpolations (*i,j)
  ij_ind : list of i or j index for vertical interpolation, all by
           default (ij_ind=False)
  horizAux : if True, the original data horizontally interpolated is
             returned and can be used for next data2roms call with
             this same karg
  quiet : output messages flag (false by default)
  '''

  ij='j'
  ij_ind=False
  horizAux=False
  quiet=False
  if 'ij'       in kargs.keys(): ij       = kargs['ij']
  if 'ij_ind'   in kargs.keys(): ij_ind   = kargs['ij_ind']
  if 'horizAux' in kargs.keys(): horizAux = kargs['horizAux']
  if 'quiet'    in kargs.keys(): quiet    = kargs['quiet']

  if not quiet: print 'using grid %s' % grd
  g=roms.Grid(grd)
  xr,yr,hr,mr=g.vars('r')
  xu,yu,hu,mu=g.vars('u')
  xv,yv,hv,mv=g.vars('v')
  ny,nx=hr.shape
  nz=sparams[3]

  NX=data['NX']
  NY=data['NY']
  NZ=data['NZ']

  if not quiet: print 'calc s levels...'
  sshr=calc.griddata(data['lon'],data['lat'],data['ssh'],xr,yr,extrap=True)

  Zr = g.s_levels(sparams,sshr,hr,'r')
  Zu = g.s_levels(sparams,sshr,hr,'u')
  Zv = g.s_levels(sparams,sshr,hr,'v')

  # interp horiz:
  retHorizAux=horizAux is True
  if horizAux in (True,False):
    TEMP = np.ma.masked_all((NZ,ny,nx),data['temp'].dtype)
    SALT = np.ma.masked_all((NZ,ny,nx),data['salt'].dtype)
    U    = np.ma.masked_all((NZ,ny,nx),data['u'].dtype)
    V    = np.ma.masked_all((NZ,ny,nx),data['v'].dtype)

    if not quiet: print 'horizontal interpolation:'
    for i in range(NZ):
      if not quiet and i%10==0: print '   lev %d of %d' % (i,NZ)
      #import pylab
      #pylab.figure()
      #pylab.pcolormesh(data['lon'],data['lat'],data['temp'][i,...])

      try: TEMP[i,...] = calc.griddata(data['lon'],data['lat'],data['temp'][i,...],xr,yr,extrap=True)
      except: pass

      try: SALT[i,...] = calc.griddata(data['lon'],data['lat'],data['salt'][i,...],xr,yr,extrap=True)
      except: pass

      try: U[i,...] = calc.griddata(data['lon'],data['lat'],data['u'][i,...],xr,yr,extrap=True)
      except: pass

      try: V[i,...] = calc.griddata(data['lon'],data['lat'],data['v'][i,...],xr,yr,extrap=True)
      except: pass

    # rotate U,V:
    if not quiet: print 'rotating U,V to grid angle'
    angle=g.use('angle')  # rad
    U,V=calc.rot2d(U,V,angle)
    U=rt.rho2uvp3d(U,'u')
    V=rt.rho2uvp3d(V,'v')

    horizAux={}
    horizAux['TEMP'] = TEMP
    horizAux['SALT'] = SALT
    horizAux['U']    = U
    horizAux['V']    = V

  else:
    TEMP = horizAux['TEMP']
    SALT = horizAux['SALT']
    U    = horizAux['U']
    V    = horizAux['V']

  # interp vert:
  nxu=nx-1
  nyv=ny-1
  #> -----------------------------------------------------------------
  useInd=not ij_ind is False
  if ij_ind is False:
    if   ij=='j': ij_ind=range(ny)
    elif ij=='i': ij_ind=range(nx)
  else:
    try: iter(ij_ind)
    except: ij_ind=[ij_ind]

    if   ij=='j': ny=nyv=len(ij_ind)
    elif ij=='i': nx=nxu=len(ij_ind)
  # -----------------------------------------------------------------<

  Temp = np.zeros((nz,ny ,nx ),data['temp'].dtype)
  Salt = np.zeros((nz,ny ,nx ),data['salt'].dtype)
  Uvel = np.zeros((nz,ny ,nxu),data['u'].dtype)
  Vvel = np.zeros((nz,nyv,nx ),data['v'].dtype)


  jslice=lambda x,ind: x[:,ind,:]
  islice=lambda x,ind: x[:,:,ind]

  ZZr = np.tile(data['depth'],(nx,ny,1)).T
  ZZu = np.tile(data['depth'],(nxu,ny,1)).T
  ZZv = np.tile(data['depth'],(nx,nyv,1)).T

  if not useInd is False: #>------------------------------------------
    if   ij=='j':
      slice=jslice
      sshr=sshr[ij_ind,:]
      hr  =hr[ij_ind,:]
    elif ij=='i':
      slice=islice
      sshr=sshr[:,ij_ind]
      hr  =hr[:,ij_ind]

    Zr,Zu,Zv,TEMP,SALT,U,V=[slice(k,ij_ind) for k in [Zr,Zu,Zv,TEMP,SALT,U,V]]
  # -----------------------------------------------------------------<

  if useInd: # then store distances for a possible bry file
    dtype=Temp.dtype
    distr=np.zeros((nz,ny, nx ),dtype)
    distu=np.zeros((nz,ny, nxu),dtype)
    distv=np.zeros((nz,nyv,nx ),dtype)

  if not quiet: print 'vertical interpolation:'
  if ij=='j':
    for j in range(ny):
      if not quiet and (ny<10 or (ny>=10 and j%10==0)): print '  j=%3d of %3d' % (j,ny)
      ind=ij_ind[j]
      dr=np.tile(calc.distance(xr[ind,:],yr[ind,:]),(nz,1))
      du=np.tile(calc.distance(xu[ind,:],yu[ind,:]),(nz,1))
      Dr=np.tile(calc.distance(xr[ind,:],yr[ind,:]),(NZ,1))
      Du=np.tile(calc.distance(xu[ind,:],yu[ind,:]),(NZ,1))

      if useInd:
        distr[:,j,:]=dr;
        distu[:,j,:]=du;

      Temp[:,j,:]   = calc.griddata(Dr,ZZr[:,j,:],TEMP[:,j,:],dr,Zr[:,j,:],extrap=True)
      Salt[:,j,:]   = calc.griddata(Dr,ZZr[:,j,:],SALT[:,j,:],dr,Zr[:,j,:],extrap=True)
      if 0 and j%10==0:
        print Dr.shape, ZZr[:,j,:].shape
        import pylab as pl
        pl.figure(1)
        pl.clf()
        pl.pcolormesh(Dr,ZZr[:,j,:],SALT[:,j,:])
        pl.colorbar()
        clim=pl.gci().get_clim()
      
        pl.figure(2)
        pl.clf()
        pl.pcolormesh(dr,Zr[:,j,:],Salt[:,j,:])
        pl.clim(clim)
        pl.colorbar()
        raw_input()
      
      Uvel[:,j,:]   = calc.griddata(Du,ZZu[:,j,:],U[:,j,:],   du,Zu[:,j,:],extrap=True)
      if j<Vvel.shape[1]:
        dv=np.tile(calc.distance(xv[ind,:],yv[ind,:]),(nz,1))
        Dv=np.tile(calc.distance(xv[ind,:],yv[ind,:]),(NZ,1))
        Vvel[:,j,:] = calc.griddata(Dv,ZZv[:,j,:],V[:,j,:],   dv,Zv[:,j,:],extrap=True)
        if useInd:
          distv[:,j,:]=dv

      if np.any(np.isnan(Temp[:,j,:])): print 'found nan in temp',j
      if np.any(np.isnan(Salt[:,j,:])): print 'found nan in salt',j
      if np.any(np.isnan(Uvel[:,j,:])): print 'found nan in u',j
      if j<Vvel.shape[1] and np.any(np.isnan(Vvel[:,j,:])): print 'found nan in v',j


  elif ij=='i':
    for i in range(nx):
      if not quiet and (nx<10 or (nx>=10 and i%10==0)): print '  i=%3d of %3d' % (i,nx)
      ind=ij_ind[i]
      dr=np.tile(calc.distance(xr[:,ind],yr[:,ind]),(nz,1))
      dv=np.tile(calc.distance(xv[:,ind],yv[:,ind]),(nz,1))
      Dr=np.tile(calc.distance(xr[:,ind],yr[:,ind]),(NZ,1))
      Dv=np.tile(calc.distance(xv[:,ind],yv[:,ind]),(NZ,1))

      if useInd:
        distr[:,:,i]=dr;
        distv[:,:,i]=dv;

      Temp[:,:,i]   = calc.griddata(Dr,ZZr[:,:,i],TEMP[:,:,i],dr,Zr[:,:,i],extrap=True)
      Salt[:,:,i]   = calc.griddata(Dr,ZZr[:,:,i],SALT[:,:,i],dr,Zr[:,:,i],extrap=True)
      Vvel[:,:,i]   = calc.griddata(Dv,ZZv[:,:,i],V[:,:,i],   dv,Zv[:,:,i],extrap=True)
      if i<Uvel.shape[2]:
        du=np.tile(calc.distance(xu[:,ind],yu[:,ind]),(nz,1))
        Du=np.tile(calc.distance(xu[:,ind],yu[:,ind]),(NZ,1))
        Uvel[:,:,i] = calc.griddata(Du,ZZu[:,:,i],U[:,:,i],   du,Zu[:,:,i],extrap=True)
        if useInd:
          distu[:,:,i]=du


  # uv bar:
  if not quiet: print 'calc uvbar'
  if useInd is False:
    ubar,vbar=rt.uvbar(Uvel,Vvel,sshr,hr,sparams)
  else: #>------------------------------------------------------------
    sshu=calc.griddata(data['lon'],data['lat'],data['ssh'],xu,yu,extrap=True)
    sshv=calc.griddata(data['lon'],data['lat'],data['ssh'],xv,yv,extrap=True)

    if ij=='j':
      sshu=sshu[ij_ind,:]
      sshv=sshv[ij_ind,:]
      hu  =hu[ij_ind,:]
      hv  =hv[ij_ind,:]
    elif ij=='i':
      sshu=sshu[:,ij_ind]
      sshv=sshv[:,ij_ind]
      hu  =hu[:,ij_ind]
      hv  =hv[:,ij_ind]

    ubar=rt.barotropic(Uvel,sshu,hu,sparams)
    vbar=rt.barotropic(Vvel,sshv,hv,sparams)
  # -----------------------------------------------------------------<


  Vars=cb.odict()
  Vars['temp'] = Temp
  Vars['salt'] = Salt
  Vars['u']    = Uvel
  Vars['v']    = Vvel
  Vars['zeta'] = sshr
  Vars['ubar'] = ubar
  Vars['vbar'] = vbar

  Vars['date']   = data['date']

  if not useInd is False: #>------------------------------------------
    Vars['depth']  = Zr
    Vars['depthu'] = Zu
    Vars['depthv'] = Zv

    Vars['dist']  = distr
    Vars['distu'] = distu
    Vars['distv'] = distv
  # -----------------------------------------------------------------<


  if retHorizAux: return Vars, horizAux
  else: return Vars
示例#11
0
def data2roms(data,grd,sparams,**kargs):
  '''
  Interpolates data to roms 3D grid.

  The dict data must contain the prognostic variables temp, salt, u,
  v (3d) and ssh (zeta, 2d), as well as lon, lat (2d), depth (1d) and
  time/date info: date (data date), date0 (reference date) and time
  (difference between date and date0). The input data can be provided
  by load_data.

  Parameters
  ----------
  data : dict with prognostic variables
  grd : ROMS netcdf grid file
  sparams : s-coordinates parameters, theta_s,theta_b, hc and NLevels

  **kargs:
  ij : axis for vertical interpolations (*i,j)
  ij_ind : list of i or j index for vertical interpolation, all by
           default (ij_ind=False)
  horizAux : if True, the original data horizontally interpolated is
             returned and can be used for next data2roms call with
             this same karg
  quiet : output messages flag (false by default)
  proj : projection - False, *auto* (use default grid projection) or basemap proj
         if False, horizontal interpolations will use lonxlat instead of distances
  interp_opts: options for griddata
  rep_surf: repeat surface level (new upper level)
            - If surface variables are present rep_surf must be True
              and for each variable (ex temp) will be used the surface
              variable (if present, ex ss_temp) instead of the upper layer.
  '''

  ij          = kargs.get('ij','j')
  ij_ind      = kargs.get('ij_ind',False)
  horizAux    = kargs.get('horizAux',False)
  quiet       = kargs.get('quiet',False)
  proj        = kargs.get('proj','auto') # lonxlat to distance before
                                         # horizontal interpolation
  interp_opts = kargs.get('interp_opts',{})
  rep_surf    = kargs.get('rep_surf',True) # create a surface upper level
                                           # before interpolation

  if not quiet: print('using grid %s' % grd)
  g=roms.Grid(grd)
  xr,yr,hr,mr=g.vars('r')
  xu,yu,hu,mu=g.vars('u')
  xv,yv,hv,mv=g.vars('v')
  ny,nx=hr.shape
  ny0,nx0=hr.shape
  nz=sparams[3]

  if proj=='auto': proj=g.get_projection()
  if proj:
    print('projecting coordinates...')
    xr,yr=proj(xr,yr)
    xu,yu=proj(xu,yu)
    xv,yv=proj(xv,yv)
    dlon,dlat=proj(data['lon'],data['lat'])
    Rdz=1/100. # distance to depth ratio (300km vs 3000m)
    distance=lambda x,y: np.append(0.,np.sqrt(np.diff(x)**2+np.diff(y)**2).cumsum())
  else:
    dlon,dlat=data['lon'],data['lat']
    Rdz=1.
    distance=calc.distance

  # needed for s_levels and for rep_surf!
  sshr=calc.griddata(dlon,dlat,data['ssh'],xr,yr,extrap=True,**interp_opts)

  # repeat surface:
  any_ssvar=False
  if rep_surf:
    # copy data cos dont want to change the original dataset:
    import copy
    data=copy.deepcopy(data)
    for vname in ['temp','salt','u','v','depth']:
      if data[vname].ndim==1: # depth !
        if np.ma.isMA(data[vname]): vstack=np.ma.hstack
        else: vstack=np.hstack
      else:
        if np.ma.isMA(data[vname]): vstack=np.ma.vstack
        else: vstack=np.vstack

      ss_vname='ss_'+vname
      if ss_vname in data and not quiet:
        any_ssvar=True
        print('- using surface variable %s'%ss_vname)

      if data['depth'][0]>data['depth'][1]: # surf at ind 0
        if ss_vname in data: data[vname]=vstack((data[ss_vname][np.newaxis],data[vname]))
        else:                data[vname]=vstack((data[vname][0][np.newaxis],data[vname]))
        if vname=='depth': data[vname][0]=sshr.max()
        surf_ind=0
      else:
        if ss_vname in data: data[vname]=vstack((data[vname],data[ss_vname][np.newaxis]))
        else:                data[vname]=vstack((data[vname],data[vname][-1][np.newaxis]))
        if vname=='depth': data[vname][-1]=sshr.max()
        surf_ind=-1

    data['NZ']=data['NZ']+1


  NX=data['NX']
  NY=data['NY']
  NZ=data['NZ']

  if not quiet: print('calc s levels...')
  Zr = g.s_levels(sparams,sshr,hr,'rr')
  Zu = g.s_levels(sparams,sshr,hr,'ur')
  Zv = g.s_levels(sparams,sshr,hr,'vr')

  # interp horiz:
  retHorizAux=horizAux is True
  if horizAux in (True,False):
    TEMP = np.ma.masked_all((NZ,ny,nx),data['temp'].dtype)
    SALT = np.ma.masked_all((NZ,ny,nx),data['salt'].dtype)
    U    = np.ma.masked_all((NZ,ny,nx),data['u'].dtype)
    V    = np.ma.masked_all((NZ,ny,nx),data['v'].dtype)

    if not quiet: print('horizontal interpolation:')
    for i in range(NZ):
      if not quiet and i%10==0: print('   lev %d of %d' % (i,NZ))
      #import pylab
      #pylab.figure()
      #pylab.pcolormesh(data['lon'],data['lat'],data['temp'][i,...])

      try: TEMP[i,...] = calc.griddata(dlon,dlat,data['temp'][i,...],xr,yr,extrap=True,**interp_opts)
      except: pass

      try: SALT[i,...] = calc.griddata(dlon,dlat,data['salt'][i,...],xr,yr,extrap=True,**interp_opts)
      except: pass

      try: U[i,...] = calc.griddata(dlon,dlat,data['u'][i,...],xr,yr,extrap=True,**interp_opts)
      except: pass

      try: V[i,...] = calc.griddata(dlon,dlat,data['v'][i,...],xr,yr,extrap=True,**interp_opts)
      except: pass

    # rotate U,V:
    if not quiet: print('rotating U,V to grid angle')
    U,V=calc.rot2d(U,V,g.angle) # g.angle in rad
    U=rt.rho2uvp3d(U,'u')
    V=rt.rho2uvp3d(V,'v')

    horizAux={}
    horizAux['TEMP'] = TEMP
    horizAux['SALT'] = SALT
    horizAux['U']    = U
    horizAux['V']    = V

  else:
    TEMP = horizAux['TEMP']
    SALT = horizAux['SALT']
    U    = horizAux['U']
    V    = horizAux['V']

  # interp vert:
  nxu=nx-1
  nyv=ny-1
  #> -----------------------------------------------------------------
  useInd=not ij_ind is False
  if ij_ind is False:
    if   ij=='j': ij_ind=range(ny)
    elif ij=='i': ij_ind=range(nx)
  else:
    try: iter(ij_ind)
    except: ij_ind=[ij_ind]

    if   ij=='j': ny=nyv=len(ij_ind)
    elif ij=='i': nx=nxu=len(ij_ind)
  # -----------------------------------------------------------------<

  Temp = np.zeros((nz,ny ,nx ),data['temp'].dtype)
  Salt = np.zeros((nz,ny ,nx ),data['salt'].dtype)
  Uvel = np.zeros((nz,ny ,nxu),data['u'].dtype)
  Vvel = np.zeros((nz,nyv,nx ),data['v'].dtype)


  jslice=lambda x,ind: x[:,ind,:]
  islice=lambda x,ind: x[:,:,ind]

  if any_ssvar:
    ZZr = np.tile(data['depth'],(nx0,ny0,1)).T
    ZZu = np.tile(data['depth'],(nx0-1,ny0,1)).T
    ZZv = np.tile(data['depth'],(nx0,ny0-1,1)).T
    # replace 1st level by sea surface height!
    # instead of sshr.max(). This is at least slightly more correct,
    # and should be no problem for variables without surface counterpart.
    ZZr[surf_ind]=sshr
    ZZu[surf_ind]=(sshr[:,1:]+sshr[:,:-1])/2.
    ZZv[surf_ind]=(sshr[1:]+sshr[:-1])/2.
  else:
    ZZr = np.tile(data['depth'],(nx,ny,1)).T
    ZZu = np.tile(data['depth'],(nxu,ny,1)).T
    ZZv = np.tile(data['depth'],(nx,nyv,1)).T

  if not useInd is False: #>------------------------------------------
    if   ij=='j':
      slice=jslice
      sshr=sshr[ij_ind,:]
      hr  =hr[ij_ind,:]
    elif ij=='i':
      slice=islice
      sshr=sshr[:,ij_ind]
      hr  =hr[:,ij_ind]

    Zr,Zu,Zv,TEMP,SALT,U,V=[slice(k,ij_ind) for k in [Zr,Zu,Zv,TEMP,SALT,U,V]]
    if any_ssvar:
      ZZr,ZZu,ZZv=[slice(k,ij_ind) for k in [ZZr,ZZu,ZZv]]
  # -----------------------------------------------------------------<

  if useInd: # then store distances for a possible bry file
    dtype=Temp.dtype
    distr=np.zeros((nz,ny, nx ),dtype)
    distu=np.zeros((nz,ny, nxu),dtype)
    distv=np.zeros((nz,nyv,nx ),dtype)

  if not quiet: print('vertical interpolation:')
  if ij=='j':
    for j in range(ny):
      if not quiet and (ny<20 or (ny>=20 and j%20==0)): print('  j=%3d of %3d' % (j,ny))
      ind=ij_ind[j]
      dr=np.tile(distance(xr[ind,:],yr[ind,:]),(nz,1))
      du=np.tile(distance(xu[ind,:],yu[ind,:]),(nz,1))
      Dr=np.tile(distance(xr[ind,:],yr[ind,:]),(NZ,1))
      Du=np.tile(distance(xu[ind,:],yu[ind,:]),(NZ,1))

      if useInd:
        distr[:,j,:]=dr;
        distu[:,j,:]=du;

      Temp[:,j,:]   = calc.griddata(Rdz*Dr,ZZr[:,j,:],TEMP[:,j,:],Rdz*dr,Zr[:,j,:],extrap=True,**interp_opts)
      Salt[:,j,:]   = calc.griddata(Rdz*Dr,ZZr[:,j,:],SALT[:,j,:],Rdz*dr,Zr[:,j,:],extrap=True,**interp_opts)
      if 0 and j%10==0:
        print(Dr.shape, ZZr[:,j,:].shape)
        import pylab as pl
        pl.figure(1)
        pl.clf()
        pl.pcolormesh(Dr,ZZr[:,j,:],TEMP[:,j,:])
        pl.colorbar()
        clim=pl.gci().get_clim()

        pl.figure(2)
        pl.clf()
        pl.pcolormesh(dr,Zr[:,j,:],Temp[:,j,:])
        pl.clim(clim)
        pl.colorbar()
        try: raw_input()
        except: input() # python 3

      Uvel[:,j,:]   = calc.griddata(Rdz*Du,ZZu[:,j,:],U[:,j,:],   Rdz*du,Zu[:,j,:],extrap=True,**interp_opts)
      if j<Vvel.shape[1]:
        dv=np.tile(distance(xv[ind,:],yv[ind,:]),(nz,1))
        Dv=np.tile(distance(xv[ind,:],yv[ind,:]),(NZ,1))
        Vvel[:,j,:] = calc.griddata(Rdz*Dv,ZZv[:,j,:],V[:,j,:],   Rdz*dv,Zv[:,j,:],extrap=True,**interp_opts)
        if useInd:
          distv[:,j,:]=dv

      if np.any(np.isnan(Temp[:,j,:])): print('found nan in temp',j)
      if np.any(np.isnan(Salt[:,j,:])): print('found nan in salt',j)
      if np.any(np.isnan(Uvel[:,j,:])): print('found nan in u',j)
      if j<Vvel.shape[1] and np.any(np.isnan(Vvel[:,j,:])): print('found nan in v',j)


  elif ij=='i':
    for i in range(nx):
      if not quiet and (nx<20 or (nx>=20 and i%20==0)): print('  i=%3d of %3d' % (i,nx))
      ind=ij_ind[i]
      dr=np.tile(distance(xr[:,ind],yr[:,ind]),(nz,1))
      dv=np.tile(distance(xv[:,ind],yv[:,ind]),(nz,1))
      Dr=np.tile(distance(xr[:,ind],yr[:,ind]),(NZ,1))
      Dv=np.tile(distance(xv[:,ind],yv[:,ind]),(NZ,1))

      if useInd:
        distr[:,:,i]=dr;
        distv[:,:,i]=dv;

      Temp[:,:,i]   = calc.griddata(Rdz*Dr,ZZr[:,:,i],TEMP[:,:,i],Rdz*dr,Zr[:,:,i],extrap=True,**interp_opts)
      Salt[:,:,i]   = calc.griddata(Rdz*Dr,ZZr[:,:,i],SALT[:,:,i],Rdz*dr,Zr[:,:,i],extrap=True,**interp_opts)
      Vvel[:,:,i]   = calc.griddata(Rdz*Dv,ZZv[:,:,i],V[:,:,i],   Rdz*dv,Zv[:,:,i],extrap=True,**interp_opts)
      if i<Uvel.shape[2]:
        du=np.tile(distance(xu[:,ind],yu[:,ind]),(nz,1))
        Du=np.tile(distance(xu[:,ind],yu[:,ind]),(NZ,1))
        Uvel[:,:,i] = calc.griddata(Rdz*Du,ZZu[:,:,i],U[:,:,i],   Rdz*du,Zu[:,:,i],extrap=True,**interp_opts)
        if useInd:
          distu[:,:,i]=du


  # uv bar:
  if not quiet: print('calc uvbar')
  if useInd is False:
    ubar,vbar=rt.uvbar(Uvel,Vvel,sshr,hr,sparams)
  else: #>------------------------------------------------------------
    sshu=calc.griddata(dlon,dlat,data['ssh'],xu,yu,extrap=True,**interp_opts)
    sshv=calc.griddata(dlon,dlat,data['ssh'],xv,yv,extrap=True,**interp_opts)

    if ij=='j':
      sshu=sshu[ij_ind,:]
      sshv=sshv[ij_ind,:]
      hu  =hu[ij_ind,:]
      hv  =hv[ij_ind,:]
    elif ij=='i':
      sshu=sshu[:,ij_ind]
      sshv=sshv[:,ij_ind]
      hu  =hu[:,ij_ind]
      hv  =hv[:,ij_ind]

    ubar=rt.barotropic(Uvel,sshu,hu,sparams)
    vbar=rt.barotropic(Vvel,sshv,hv,sparams)
  # -----------------------------------------------------------------<


  #Vars=cb.odict()
  Vars=OrderedDict()
  Vars['temp'] = Temp
  Vars['salt'] = Salt
  Vars['u']    = Uvel
  Vars['v']    = Vvel
  Vars['zeta'] = sshr
  Vars['ubar'] = ubar
  Vars['vbar'] = vbar

  Vars['date']   = data['date']

  if not useInd is False: #>------------------------------------------
    Vars['depth']  = Zr
    Vars['depthu'] = Zu
    Vars['depthv'] = Zv

    Vars['dist']  = distr
    Vars['distu'] = distu
    Vars['distv'] = distv
  # -----------------------------------------------------------------<


  if retHorizAux: return Vars, horizAux
  else: return Vars
示例#12
0
def data2roms(data, grd, sparams, **kargs):
    '''
  Interpolates data to roms 3D grid.

  The dict data must contain the prognostic variables temp, salt, u,
  v (3d) and ssh (zeta, 2d), as well as lon, lat (2d), depth (1d) and
  time/date info: date (data date), date0 (reference date) and time
  (difference between date and date0). The input data can be provided
  by load_data.

  Parameters
  ----------
  data : dict with prognostic variables
  grd : ROMS netcdf grid file
  sparams : s-coordinates parameters, theta_s,theta_b, hc and NLevels

  **kargs:
  ij : axis for vertical interpolations (*i,j)
  ij_ind : list of i or j index for vertical interpolation, all by
           default (ij_ind=False)
  horizAux : if True, the original data horizontally interpolated is
             returned and can be used for next data2roms call with
             this same karg
  quiet : output messages flag (false by default)
  proj : projection - False, name or basemap proj - lcc by default
         if False, horizontal interpolations will use lonxlat instead of distances
  interp_opts: options for griddata
  rep_surf: repeat surface level (new upper level)
  '''

    ij = kargs.get('ij', 'j')
    ij_ind = kargs.get('ij_ind', False)
    horizAux = kargs.get('horizAux', False)
    quiet = kargs.get('quiet', False)
    proj = kargs.get('proj', 'lcc')  # lonxlat to distance before
    # horizontal interpolation
    interp_opts = kargs.get('interp_opts', {})
    rep_surf = kargs.get('rep_surf', True)  # create a surface upper level
    # before interpolation

    if not quiet: print 'using grid %s' % grd
    g = roms.Grid(grd)
    xr, yr, hr, mr = g.vars('r')
    xu, yu, hu, mu = g.vars('u')
    xv, yv, hv, mv = g.vars('v')
    ny, nx = hr.shape
    nz = sparams[3]

    if proj:
        print 'projecting coordinates...'
        if isinstance(proj, basestring):
            lonc = (xr.max() + xr.min()) / 2.
            latc = (yr.max() + yr.min()) / 2.
            from mpl_toolkits.basemap import Basemap
            proj = Basemap(projection=proj,
                           width=1,
                           height=1,
                           resolution=None,
                           lon_0=lonc,
                           lat_0=latc,
                           lat_1=latc)

        xr, yr = proj(xr, yr)
        xu, yu = proj(xu, yu)
        xv, yv = proj(xv, yv)
        dlon, dlat = proj(data['lon'], data['lat'])
        Rdz = 1 / 100.  # distance to depth ratio (300km vs 3000m)
        distance = lambda x, y: np.append(
            0.,
            np.sqrt(np.diff(x)**2 + np.diff(y)**2).cumsum())
    else:
        dlon, dlat = data['lon'], data['lat']
        distance = calc.distance

    # needed for s_levels and for rep_surf!
    sshr = calc.griddata(dlon,
                         dlat,
                         data['ssh'],
                         xr,
                         yr,
                         extrap=True,
                         **interp_opts)

    # repeat surface:
    if rep_surf:
        # copy data cos dont want to change the original dataset:
        import copy
        data = copy.deepcopy(data)
        for vname in ['temp', 'salt', 'u', 'v', 'depth']:
            if data[vname].ndim == 1:  # depth !
                if np.ma.isMA(data[vname]): vstack = np.ma.hstack
                else: vstack = np.hstack
            else:
                if np.ma.isMA(data[vname]): vstack = np.ma.vstack
                else: vstack = np.vstack

            if data['depth'][0] > data['depth'][1]:  # surf at ind 0
                data[vname] = vstack((data[vname][0][np.newaxis], data[vname]))
                if vname == 'depth': data[vname][0] = sshr.max()
            else:
                data[vname] = vstack(
                    (data[vname], data[vname][-1][np.newaxis]))
                if vname == 'depth': data[vname][-1] = sshr.max()

        data['NZ'] = data['NZ'] + 1

    NX = data['NX']
    NY = data['NY']
    NZ = data['NZ']

    if not quiet: print 'calc s levels...'
    Zr = g.s_levels(sparams, sshr, hr, 'r')
    Zu = g.s_levels(sparams, sshr, hr, 'u')
    Zv = g.s_levels(sparams, sshr, hr, 'v')

    # interp horiz:
    retHorizAux = horizAux is True
    if horizAux in (True, False):
        TEMP = np.ma.masked_all((NZ, ny, nx), data['temp'].dtype)
        SALT = np.ma.masked_all((NZ, ny, nx), data['salt'].dtype)
        U = np.ma.masked_all((NZ, ny, nx), data['u'].dtype)
        V = np.ma.masked_all((NZ, ny, nx), data['v'].dtype)

        if not quiet: print 'horizontal interpolation:'
        for i in range(NZ):
            if not quiet and i % 10 == 0: print '   lev %d of %d' % (i, NZ)
            #import pylab
            #pylab.figure()
            #pylab.pcolormesh(data['lon'],data['lat'],data['temp'][i,...])

            try:
                TEMP[i, ...] = calc.griddata(dlon,
                                             dlat,
                                             data['temp'][i, ...],
                                             xr,
                                             yr,
                                             extrap=True,
                                             **interp_opts)
            except:
                pass

            try:
                SALT[i, ...] = calc.griddata(dlon,
                                             dlat,
                                             data['salt'][i, ...],
                                             xr,
                                             yr,
                                             extrap=True,
                                             **interp_opts)
            except:
                pass

            try:
                U[i, ...] = calc.griddata(dlon,
                                          dlat,
                                          data['u'][i, ...],
                                          xr,
                                          yr,
                                          extrap=True,
                                          **interp_opts)
            except:
                pass

            try:
                V[i, ...] = calc.griddata(dlon,
                                          dlat,
                                          data['v'][i, ...],
                                          xr,
                                          yr,
                                          extrap=True,
                                          **interp_opts)
            except:
                pass

        # rotate U,V:
        if not quiet: print 'rotating U,V to grid angle'
        angle = g.use('angle')  # rad
        U, V = calc.rot2d(U, V, angle)
        U = rt.rho2uvp3d(U, 'u')
        V = rt.rho2uvp3d(V, 'v')

        horizAux = {}
        horizAux['TEMP'] = TEMP
        horizAux['SALT'] = SALT
        horizAux['U'] = U
        horizAux['V'] = V

    else:
        TEMP = horizAux['TEMP']
        SALT = horizAux['SALT']
        U = horizAux['U']
        V = horizAux['V']

    # interp vert:
    nxu = nx - 1
    nyv = ny - 1
    #> -----------------------------------------------------------------
    useInd = not ij_ind is False
    if ij_ind is False:
        if ij == 'j': ij_ind = range(ny)
        elif ij == 'i': ij_ind = range(nx)
    else:
        try:
            iter(ij_ind)
        except:
            ij_ind = [ij_ind]

        if ij == 'j': ny = nyv = len(ij_ind)
        elif ij == 'i': nx = nxu = len(ij_ind)
    # -----------------------------------------------------------------<

    Temp = np.zeros((nz, ny, nx), data['temp'].dtype)
    Salt = np.zeros((nz, ny, nx), data['salt'].dtype)
    Uvel = np.zeros((nz, ny, nxu), data['u'].dtype)
    Vvel = np.zeros((nz, nyv, nx), data['v'].dtype)

    jslice = lambda x, ind: x[:, ind, :]
    islice = lambda x, ind: x[:, :, ind]

    ZZr = np.tile(data['depth'], (nx, ny, 1)).T
    ZZu = np.tile(data['depth'], (nxu, ny, 1)).T
    ZZv = np.tile(data['depth'], (nx, nyv, 1)).T

    if not useInd is False:  #>------------------------------------------
        if ij == 'j':
            slice = jslice
            sshr = sshr[ij_ind, :]
            hr = hr[ij_ind, :]
        elif ij == 'i':
            slice = islice
            sshr = sshr[:, ij_ind]
            hr = hr[:, ij_ind]

        Zr, Zu, Zv, TEMP, SALT, U, V = [
            slice(k, ij_ind) for k in [Zr, Zu, Zv, TEMP, SALT, U, V]
        ]
    # -----------------------------------------------------------------<

    if useInd:  # then store distances for a possible bry file
        dtype = Temp.dtype
        distr = np.zeros((nz, ny, nx), dtype)
        distu = np.zeros((nz, ny, nxu), dtype)
        distv = np.zeros((nz, nyv, nx), dtype)

    if not quiet: print 'vertical interpolation:'
    if ij == 'j':
        for j in range(ny):
            if not quiet and (ny < 10 or (ny >= 10 and j % 10 == 0)):
                print '  j=%3d of %3d' % (j, ny)
            ind = ij_ind[j]
            dr = np.tile(distance(xr[ind, :], yr[ind, :]), (nz, 1))
            du = np.tile(distance(xu[ind, :], yu[ind, :]), (nz, 1))
            Dr = np.tile(distance(xr[ind, :], yr[ind, :]), (NZ, 1))
            Du = np.tile(distance(xu[ind, :], yu[ind, :]), (NZ, 1))

            if useInd:
                distr[:, j, :] = dr
                distu[:, j, :] = du

            Temp[:, j, :] = calc.griddata(Rdz * Dr,
                                          ZZr[:, j, :],
                                          TEMP[:, j, :],
                                          Rdz * dr,
                                          Zr[:, j, :],
                                          extrap=True,
                                          **interp_opts)
            Salt[:, j, :] = calc.griddata(Rdz * Dr,
                                          ZZr[:, j, :],
                                          SALT[:, j, :],
                                          Rdz * dr,
                                          Zr[:, j, :],
                                          extrap=True,
                                          **interp_opts)
            if 0 and j % 10 == 0:
                print Dr.shape, ZZr[:, j, :].shape
                import pylab as pl
                pl.figure(1)
                pl.clf()
                pl.pcolormesh(Dr, ZZr[:, j, :], SALT[:, j, :])
                pl.colorbar()
                clim = pl.gci().get_clim()

                pl.figure(2)
                pl.clf()
                pl.pcolormesh(dr, Zr[:, j, :], Salt[:, j, :])
                pl.clim(clim)
                pl.colorbar()
                raw_input()

            Uvel[:, j, :] = calc.griddata(Rdz * Du,
                                          ZZu[:, j, :],
                                          U[:, j, :],
                                          Rdz * du,
                                          Zu[:, j, :],
                                          extrap=True,
                                          **interp_opts)
            if j < Vvel.shape[1]:
                dv = np.tile(distance(xv[ind, :], yv[ind, :]), (nz, 1))
                Dv = np.tile(distance(xv[ind, :], yv[ind, :]), (NZ, 1))
                Vvel[:, j, :] = calc.griddata(Rdz * Dv,
                                              ZZv[:, j, :],
                                              V[:, j, :],
                                              Rdz * dv,
                                              Zv[:, j, :],
                                              extrap=True,
                                              **interp_opts)
                if useInd:
                    distv[:, j, :] = dv

            if np.any(np.isnan(Temp[:, j, :])): print 'found nan in temp', j
            if np.any(np.isnan(Salt[:, j, :])): print 'found nan in salt', j
            if np.any(np.isnan(Uvel[:, j, :])): print 'found nan in u', j
            if j < Vvel.shape[1] and np.any(np.isnan(Vvel[:, j, :])):
                print 'found nan in v', j

    elif ij == 'i':
        for i in range(nx):
            if not quiet and (nx < 10 or (nx >= 10 and i % 10 == 0)):
                print '  i=%3d of %3d' % (i, nx)
            ind = ij_ind[i]
            dr = np.tile(distance(xr[:, ind], yr[:, ind]), (nz, 1))
            dv = np.tile(distance(xv[:, ind], yv[:, ind]), (nz, 1))
            Dr = np.tile(distance(xr[:, ind], yr[:, ind]), (NZ, 1))
            Dv = np.tile(distance(xv[:, ind], yv[:, ind]), (NZ, 1))

            if useInd:
                distr[:, :, i] = dr
                distv[:, :, i] = dv

            Temp[:, :, i] = calc.griddata(Rdz * Dr,
                                          ZZr[:, :, i],
                                          TEMP[:, :, i],
                                          Rdz * dr,
                                          Zr[:, :, i],
                                          extrap=True,
                                          **interp_opts)
            Salt[:, :, i] = calc.griddata(Rdz * Dr,
                                          ZZr[:, :, i],
                                          SALT[:, :, i],
                                          Rdz * dr,
                                          Zr[:, :, i],
                                          extrap=True,
                                          **interp_opts)
            Vvel[:, :, i] = calc.griddata(Rdz * Dv,
                                          ZZv[:, :, i],
                                          V[:, :, i],
                                          Rdz * dv,
                                          Zv[:, :, i],
                                          extrap=True,
                                          **interp_opts)
            if i < Uvel.shape[2]:
                du = np.tile(distance(xu[:, ind], yu[:, ind]), (nz, 1))
                Du = np.tile(distance(xu[:, ind], yu[:, ind]), (NZ, 1))
                Uvel[:, :, i] = calc.griddata(Rdz * Du,
                                              ZZu[:, :, i],
                                              U[:, :, i],
                                              Rdz * du,
                                              Zu[:, :, i],
                                              extrap=True,
                                              **interp_opts)
                if useInd:
                    distu[:, :, i] = du

    # uv bar:
    if not quiet: print 'calc uvbar'
    if useInd is False:
        ubar, vbar = rt.uvbar(Uvel, Vvel, sshr, hr, sparams)
    else:  #>------------------------------------------------------------
        sshu = calc.griddata(dlon,
                             dlat,
                             data['ssh'],
                             xu,
                             yu,
                             extrap=True,
                             **interp_opts)
        sshv = calc.griddata(dlon,
                             dlat,
                             data['ssh'],
                             xv,
                             yv,
                             extrap=True,
                             **interp_opts)

        if ij == 'j':
            sshu = sshu[ij_ind, :]
            sshv = sshv[ij_ind, :]
            hu = hu[ij_ind, :]
            hv = hv[ij_ind, :]
        elif ij == 'i':
            sshu = sshu[:, ij_ind]
            sshv = sshv[:, ij_ind]
            hu = hu[:, ij_ind]
            hv = hv[:, ij_ind]

        ubar = rt.barotropic(Uvel, sshu, hu, sparams)
        vbar = rt.barotropic(Vvel, sshv, hv, sparams)
    # -----------------------------------------------------------------<

    Vars = cb.odict()
    Vars['temp'] = Temp
    Vars['salt'] = Salt
    Vars['u'] = Uvel
    Vars['v'] = Vvel
    Vars['zeta'] = sshr
    Vars['ubar'] = ubar
    Vars['vbar'] = vbar

    Vars['date'] = data['date']

    if not useInd is False:  #>------------------------------------------
        Vars['depth'] = Zr
        Vars['depthu'] = Zu
        Vars['depthv'] = Zv

        Vars['dist'] = distr
        Vars['distu'] = distu
        Vars['distv'] = distv
    # -----------------------------------------------------------------<

    if retHorizAux: return Vars, horizAux
    else: return Vars
示例#13
0
def getCorr(order=5,i0=None,Imat=None,i0_wp=1e6,fraclims_dc=[.9,1.1]):
  """ Getting nonlinear correction factors form a calibration dataset consiting of:
  i0     	array of intensities the calibration has been made for
  Imat   	2D array of the corresponding reference patterns, in each row 
  		there is one ravelled array of each intensity bin in i0.
  i0_wp		a working point around which a correction polynomial will be
  		developed for each pixel.
  order		the polynomial order up to which will be deveoped.
  fraclims_dc	relative factor for the i0,Imat data limits which are used to
  		determine the working point location.
  
  Returns

  """

  #i0,Imat = getData()
  msk = tools.filtvec(i0,i0_wp*np.asarray(fraclims_dc))
  p0 = tools.polyFit(i0[msk],Imat[msk,:],2)
  dc = tools.polyVal(p0,i0_wp)
  comps = tools.polyFit(i0-i0_wp,Imat-dc,order,removeOrders=[0])
  compsder = tools.polyDer(comps)
  c = lambda(i): tools.polyVal(comps,i-np.asarray(tools.iterfy(i0_wp)))+dc
  c_prime = lambda(i): tools.polyVal(compsder,i-np.asarray(tools.iterfy(i0_wp)))
  t = lambda(i): (c_prime(i0_wp).T * (i-i0_wp)).T + dc
  
  cprimeic = c_prime(i0_wp)
  dcorr_const = -cprimeic*i0_wp + c(i0_wp) - t(0) 
  def dcorr(i,D):
    return (i*cprimeic.T + dcorr_const.T + ((D-c(i))*cprimeic/c_prime(i)).T).T
    #return (i*cprimeic.T + dcorr_const.T ).T
  return dcorr,comps,t





  tools.nfigure('testplot')
  plt.clf()
  plt.subplot(1,2,1)
  Imean = (Imat.T/i0).T
  tools.imagesc(np.asarray([ti / np.mean(Imean[-10:,:],0) for ti in Imean]))
  tools.clim_std(6)
  cl = plt.gci().get_clim()
  plt.colorbar()
  plt.set_cmap(plt.cm.RdBu_r)
  
  
  plt.subplot(1,2,2)

  cmps = copy.copy(comps)
  cmps[-2,:] = 0
  cc = lambda(i): tools.polyVal(cmps,i-np.asarray(tools.iterfy(i0_wp)))
  Ir = Imat-c(i0)+t(i0)-t(0)
  Ir = dcorr(i0,Imat)
  #Ir = ((Imat-cc(i0)).T/i0).T
  #tools.imagesc(Ir) 
  Ir = (Ir.T/i0).T
  tools.imagesc(np.asarray([ti / np.mean(Ir[-10:,:],0) for ti in Ir]))
  plt.clim(cl)
  plt.colorbar()
  plt.set_cmap(plt.cm.RdBu_r)
  plt.draw()

  tools.nfigure('testplot_components')
  plt.clf()
  ah = None
  for n,comp in enumerate(comps):
    if ah is None:
      ah = plt.subplot(len(comps),1,n+1)
    else:
      plt.subplot(len(comps),1,n+1,sharex=ah)
    plt.plot(comp)
    lims = np.percentile(comp,[1,99])

    plt.ylim(lims)


  return c,c_prime 
示例#14
0
def plot2D(array,
           marker='',
           colour='',
           vmin=0,
           vmax=200,
           cbar=3,
           fig=1,
           figsize=None,
           aspect="equal",
           name="",
           fname="Cmax",
           dir=this_dir,
           axes=['on', 'off']):
    """ Plots a surface represented explicitly by a 2D array XY or implicitly by a set of 3D points XYZ """
    cmap = "Greys_r"
    norm = plt.Normalize(vmin=vmin, vmax=vmax)
    if dir != "":
        fname = '%s/%s' % (dir, fname)
        ext = ".png"
    if fig != 0:
        plt.figure(fig, figsize=figsize)
        plt.title("%s gamut" % name)
        plt.xlabel("H")
        plt.ylabel("L", rotation='horizontal')
        plt.xlim([0, 360])
        plt.ylim([0, 100])
        if array.shape[1] == 3:
            # array is the 3D surface of a 2D function
            L = array[:, 0]
            C = array[:, 1]
            H = array[:, 2]
            plt.tricontourf(H, L, C, cmap=cmap, norm=norm)
            if marker != '':
                ax = plt.gca()
                if len(colour) > 0:
                    ax.plot(H, L, marker, c=colour)
                else:
                    #ax.plot(H,L,marker,color=convert.clip3(convert.LCH2RGB(L,C,H)).tolist())
                    for h, l, c in zip(H, L, array):
                        ax.plot(h,
                                l,
                                marker,
                                color=convert.clip3(
                                    convert.LCH2RGB(c[0], c[1], c[2])))
            plt.gca().set_aspect(aspect)
        else:
            # array is a 2D map
            plt.imshow(array,
                       origin='lower',
                       extent=[H_min, H_max, L_min, L_max],
                       aspect=aspect,
                       interpolation='nearest',
                       cmap=cmap,
                       norm=norm)
        locator = ticker.MultipleLocator(60)
        plt.gca().xaxis.set_major_locator(locator)
        if cbar > 0:
            cax = make_axes_locatable(plt.gca()).append_axes("right",
                                                             size="%.f%%" %
                                                             cbar,
                                                             pad=0.10)
            cb = plt.colorbar(plt.gci(), cax=cax)
            cb.locator = ticker.MultipleLocator(50)
            cb.update_ticks()
            cb.set_label("Cmax")
        if dir != "" and 'on' in axes:
            print("writing %s" % (fname + "_axon" + ext))
            plt.savefig(fname + "_axon" + ext, dpi=None, bbox_inches='tight')
    if dir != "" and 'off' in axes and array.shape[1] > 3:
        print("writing %s" % (fname + "_axoff" + ext))
        plt.imsave(arr=array,
                   origin='lower',
                   cmap=cmap,
                   vmin=vmin,
                   vmax=vmax,
                   fname=fname + "_axoff" + ext)
def ridge_detection(y, sampling_frequency, framesz, hop,sigma,prct_thr=98, cf_cutoff=5000, prune=True, plot=True):

    BWallangles = [[0 for x in range(8)] for x1 in range(len(sigma))]
    nsamples = len(y)    
    T = 1.0*nsamples/sampling_frequency
    
    gabor_orig_allsigma = []    
    for sigma_i in range(len(sigma)):    
        gabor_orig, gabor_orig_dw, gabor_orig_dt, freq = stft(y, sampling_frequency, framesz, hop, sigma=sigma[sigma_i])
        gabor_orig_allsigma.append(gabor_orig)
        
        if plot:
            pl.figure()
            pl.imshow(db(np.square(scipy.absolute(gabor_orig))).T,origin='lower',aspect='auto', 
                      extent=[0, T, 0, sampling_frequency], interpolation='nearest')
            pl.ylim((0,sampling_frequency/2))
            pl.xlabel('Time')
            pl.ylabel('Frequency')
            vmin, vmax = pl.gci().get_clim()
            
            pl.figure()
            pl.imshow(db(np.square(scipy.absolute(gabor_orig_dw))).T,origin='lower',aspect='auto', 
                      extent=[0, T, 0, sampling_frequency], interpolation='nearest')
            pl.ylim((0,sampling_frequency/2))
            pl.xlabel('Time')
            pl.ylabel('Frequency')
            
        
        
        lowpass_gabor_orig_dw = gabor_orig_dw[:,freq<=cf_cutoff]                
        
        # pruning using mixed partial derivative dtdw
        if prune:
            lowfreq = freq[freq<=cf_cutoff]
            freq_orig = np.repeat(lowfreq[:,None], lowpass_gabor_orig_dw.shape[0], axis=1).T
            freq_shift = np.imag(lowpass_gabor_orig_dw)
            freq_new = freq_orig - freq_shift
            [gx,gy] = np.gradient(freq_new)
            
            prune_mask = np.abs(gy/freq[1]) < 0.6
        else:
            prune_mask = np.ones(lowpass_gabor_orig_dw.shape).astype(bool)
        
        n_angle = 1
        
        if plot:
            fig = pl.figure()    
        for angle_i in range(n_angle):
            theta = np.pi/8*angle_i
            s = (-1)*(np.imag(lowpass_gabor_orig_dw*np.exp(1j*theta))<0)+(np.imag(lowpass_gabor_orig_dw*np.exp(1j*theta))>0)
            [gx,gy]=np.gradient(s)
            BW=((-gx*np.cos(theta+np.pi/2)+gy*np.sin(theta+np.pi/2))>.1)
            
            BW = BW & prune_mask
            
            CC = measure.regionprops(measure.label(BW))
            weightv = np.zeros((len(CC),))
            powerv = np.zeros((len(CC),))
            for i in range(len(CC)):
                weightv[i] = CC[i].area
                inds = CC[i].coords
                powerv[i] = np.mean(np.abs(gabor_orig[inds[:,0], inds[:,1]]))
            
            a = np.logical_and(weightv>=np.percentile(weightv, prct_thr),powerv>=np.percentile(powerv, 10))
            tempv = np.zeros_like(BW)
            
            for index in np.nonzero(a)[0]:
                ind = CC[index].coords
                tempv[ind[:,0], ind[:,1]] = 1
            BWallangles[sigma_i][angle_i]=tempv.astype(int)
            
            if plot:
                ax = fig.add_subplot(2,4,angle_i+1)
                ax.imshow(tempv.T,origin='lower',aspect='auto', extent=[0, T, 0, cf_cutoff], cmap='binary',interpolation='nearest')
                ax.set_xlabel('Time')
                ax.set_ylabel('Frequency')
    
    consensus_allsigma = []
    for sigma_i in range(len(sigma)):
        consensus = np.zeros(BW.shape)
        if sigma_i == len(sigma)-1:
            neighboor_sigma = sigma_i-1
        else:
            neighboor_sigma = sigma_i+1                    
        for angle_i in range(n_angle):
#            if angle_i==0:
#                cv= BWallangles[sigma_i][0]+BWallangles[sigma_i][1]+BWallangles[sigma_i][7] \
#                    + BWallangles[neighboor_sigma][0]
#                consensus=consensus+(cv>1).astype(int)
#            elif angle_i==7:
#                cv= BWallangles[sigma_i][0]+BWallangles[sigma_i][6]+BWallangles[sigma_i][7] \
#                    + BWallangles[neighboor_sigma][7]
#                consensus=consensus+(cv>1).astype(int)
#            else:
#            cv= BWallangles[sigma_i][angle_i] + BWallangles[sigma_i][angle_i-1]+BWallangles[sigma_i][angle_i+1] \
#                + BWallangles[neighboor_sigma][angle_i]
            cv= BWallangles[sigma_i][angle_i] 
            consensus=consensus+(cv>0).astype(int)
        consensus_allsigma.append(consensus>0)
        
    if plot:    
        pl.figure()
        for i in range(len(sigma)):
            pl.subplot(2,2,i+1)
            pl.imshow(consensus_allsigma[i].T,origin='lower',aspect='auto', extent=[0, T, 0, cf_cutoff], cmap='binary',interpolation='nearest')
        pl.xlabel('Time')
        pl.ylabel('Frequency')
        pl.clim((0,1))
    
    return gabor_orig_allsigma, consensus_allsigma
示例#16
0
def data2roms(data, grd, sparams, **kargs):
    '''
  Interpolates data to roms 3D grid.

  The dict data must contain the prognostic variables temp, salt, u,
  v (3d) and ssh (zeta, 2d), as well as lon, lat (2d), depth (1d) and
  time/date info: date (data date), date0 (reference date) and time
  (difference between date and date0). The input data can be provided
  by load_data.

  Parameters
  ----------
  data : dict with prognostic variables
  grd : ROMS netcdf grid file
  sparams : s-coordinates parameters, theta_s,theta_b, hc and NLevels

  **kargs:
  ij : axis for vertical interpolations (*i,j)
  ij_ind : list of i or j index for vertical interpolation, all by
           default (ij_ind=False)
  horizAux : if True, the original data horizontally interpolated is
             returned and can be used for next data2roms call with
             this same karg
  quiet : output messages flag (false by default)
  '''

    ij = 'j'
    ij_ind = False
    horizAux = False
    quiet = False
    if 'ij' in kargs.keys(): ij = kargs['ij']
    if 'ij_ind' in kargs.keys(): ij_ind = kargs['ij_ind']
    if 'horizAux' in kargs.keys(): horizAux = kargs['horizAux']
    if 'quiet' in kargs.keys(): quiet = kargs['quiet']

    if not quiet: print 'using grid %s' % grd
    g = roms.Grid(grd)
    xr, yr, hr, mr = g.vars('r')
    xu, yu, hu, mu = g.vars('u')
    xv, yv, hv, mv = g.vars('v')
    ny, nx = hr.shape
    nz = sparams[3]

    NX = data['NX']
    NY = data['NY']
    NZ = data['NZ']

    if not quiet: print 'calc s levels...'
    sshr = calc.griddata(data['lon'],
                         data['lat'],
                         data['ssh'],
                         xr,
                         yr,
                         extrap=True)

    Zr = g.s_levels(sparams, sshr, hr, 'r')
    Zu = g.s_levels(sparams, sshr, hr, 'u')
    Zv = g.s_levels(sparams, sshr, hr, 'v')

    # interp horiz:
    retHorizAux = horizAux is True
    if horizAux in (True, False):
        TEMP = np.ma.masked_all((NZ, ny, nx), data['temp'].dtype)
        SALT = np.ma.masked_all((NZ, ny, nx), data['salt'].dtype)
        U = np.ma.masked_all((NZ, ny, nx), data['u'].dtype)
        V = np.ma.masked_all((NZ, ny, nx), data['v'].dtype)

        if not quiet: print 'horizontal interpolation:'
        for i in range(NZ):
            if not quiet and i % 10 == 0: print '   lev %d of %d' % (i, NZ)
            #import pylab
            #pylab.figure()
            #pylab.pcolormesh(data['lon'],data['lat'],data['temp'][i,...])

            try:
                TEMP[i, ...] = calc.griddata(data['lon'],
                                             data['lat'],
                                             data['temp'][i, ...],
                                             xr,
                                             yr,
                                             extrap=True)
            except:
                pass

            try:
                SALT[i, ...] = calc.griddata(data['lon'],
                                             data['lat'],
                                             data['salt'][i, ...],
                                             xr,
                                             yr,
                                             extrap=True)
            except:
                pass

            try:
                U[i, ...] = calc.griddata(data['lon'],
                                          data['lat'],
                                          data['u'][i, ...],
                                          xr,
                                          yr,
                                          extrap=True)
            except:
                pass

            try:
                V[i, ...] = calc.griddata(data['lon'],
                                          data['lat'],
                                          data['v'][i, ...],
                                          xr,
                                          yr,
                                          extrap=True)
            except:
                pass

        # rotate U,V:
        if not quiet: print 'rotating U,V to grid angle'
        angle = g.use('angle')  # rad
        U, V = calc.rot2d(U, V, angle)
        U = rt.rho2uvp3d(U, 'u')
        V = rt.rho2uvp3d(V, 'v')

        horizAux = {}
        horizAux['TEMP'] = TEMP
        horizAux['SALT'] = SALT
        horizAux['U'] = U
        horizAux['V'] = V

    else:
        TEMP = horizAux['TEMP']
        SALT = horizAux['SALT']
        U = horizAux['U']
        V = horizAux['V']

    # interp vert:
    nxu = nx - 1
    nyv = ny - 1
    #> -----------------------------------------------------------------
    useInd = not ij_ind is False
    if ij_ind is False:
        if ij == 'j': ij_ind = range(ny)
        elif ij == 'i': ij_ind = range(nx)
    else:
        try:
            iter(ij_ind)
        except:
            ij_ind = [ij_ind]

        if ij == 'j': ny = nyv = len(ij_ind)
        elif ij == 'i': nx = nxu = len(ij_ind)
    # -----------------------------------------------------------------<

    Temp = np.zeros((nz, ny, nx), data['temp'].dtype)
    Salt = np.zeros((nz, ny, nx), data['salt'].dtype)
    Uvel = np.zeros((nz, ny, nxu), data['u'].dtype)
    Vvel = np.zeros((nz, nyv, nx), data['v'].dtype)

    jslice = lambda x, ind: x[:, ind, :]
    islice = lambda x, ind: x[:, :, ind]

    ZZr = np.tile(data['depth'], (nx, ny, 1)).T
    ZZu = np.tile(data['depth'], (nxu, ny, 1)).T
    ZZv = np.tile(data['depth'], (nx, nyv, 1)).T

    if not useInd is False:  #>------------------------------------------
        if ij == 'j':
            slice = jslice
            sshr = sshr[ij_ind, :]
            hr = hr[ij_ind, :]
        elif ij == 'i':
            slice = islice
            sshr = sshr[:, ij_ind]
            hr = hr[:, ij_ind]

        Zr, Zu, Zv, TEMP, SALT, U, V = [
            slice(k, ij_ind) for k in [Zr, Zu, Zv, TEMP, SALT, U, V]
        ]
    # -----------------------------------------------------------------<

    if useInd:  # then store distances for a possible bry file
        dtype = Temp.dtype
        distr = np.zeros((nz, ny, nx), dtype)
        distu = np.zeros((nz, ny, nxu), dtype)
        distv = np.zeros((nz, nyv, nx), dtype)

    if not quiet: print 'vertical interpolation:'
    if ij == 'j':
        for j in range(ny):
            if not quiet and (ny < 10 or (ny >= 10 and j % 10 == 0)):
                print '  j=%3d of %3d' % (j, ny)
            ind = ij_ind[j]
            dr = np.tile(calc.distance(xr[ind, :], yr[ind, :]), (nz, 1))
            du = np.tile(calc.distance(xu[ind, :], yu[ind, :]), (nz, 1))
            Dr = np.tile(calc.distance(xr[ind, :], yr[ind, :]), (NZ, 1))
            Du = np.tile(calc.distance(xu[ind, :], yu[ind, :]), (NZ, 1))

            if useInd:
                distr[:, j, :] = dr
                distu[:, j, :] = du

            Temp[:, j, :] = calc.griddata(Dr,
                                          ZZr[:, j, :],
                                          TEMP[:, j, :],
                                          dr,
                                          Zr[:, j, :],
                                          extrap=True)
            Salt[:, j, :] = calc.griddata(Dr,
                                          ZZr[:, j, :],
                                          SALT[:, j, :],
                                          dr,
                                          Zr[:, j, :],
                                          extrap=True)
            if 0 and j % 10 == 0:
                print Dr.shape, ZZr[:, j, :].shape
                import pylab as pl
                pl.figure(1)
                pl.clf()
                pl.pcolormesh(Dr, ZZr[:, j, :], SALT[:, j, :])
                pl.colorbar()
                clim = pl.gci().get_clim()

                pl.figure(2)
                pl.clf()
                pl.pcolormesh(dr, Zr[:, j, :], Salt[:, j, :])
                pl.clim(clim)
                pl.colorbar()
                raw_input()

            Uvel[:, j, :] = calc.griddata(Du,
                                          ZZu[:, j, :],
                                          U[:, j, :],
                                          du,
                                          Zu[:, j, :],
                                          extrap=True)
            if j < Vvel.shape[1]:
                dv = np.tile(calc.distance(xv[ind, :], yv[ind, :]), (nz, 1))
                Dv = np.tile(calc.distance(xv[ind, :], yv[ind, :]), (NZ, 1))
                Vvel[:, j, :] = calc.griddata(Dv,
                                              ZZv[:, j, :],
                                              V[:, j, :],
                                              dv,
                                              Zv[:, j, :],
                                              extrap=True)
                if useInd:
                    distv[:, j, :] = dv

            if np.any(np.isnan(Temp[:, j, :])): print 'found nan in temp', j
            if np.any(np.isnan(Salt[:, j, :])): print 'found nan in salt', j
            if np.any(np.isnan(Uvel[:, j, :])): print 'found nan in u', j
            if j < Vvel.shape[1] and np.any(np.isnan(Vvel[:, j, :])):
                print 'found nan in v', j

    elif ij == 'i':
        for i in range(nx):
            if not quiet and (nx < 10 or (nx >= 10 and i % 10 == 0)):
                print '  i=%3d of %3d' % (i, nx)
            ind = ij_ind[i]
            dr = np.tile(calc.distance(xr[:, ind], yr[:, ind]), (nz, 1))
            dv = np.tile(calc.distance(xv[:, ind], yv[:, ind]), (nz, 1))
            Dr = np.tile(calc.distance(xr[:, ind], yr[:, ind]), (NZ, 1))
            Dv = np.tile(calc.distance(xv[:, ind], yv[:, ind]), (NZ, 1))

            if useInd:
                distr[:, :, i] = dr
                distv[:, :, i] = dv

            Temp[:, :, i] = calc.griddata(Dr,
                                          ZZr[:, :, i],
                                          TEMP[:, :, i],
                                          dr,
                                          Zr[:, :, i],
                                          extrap=True)
            Salt[:, :, i] = calc.griddata(Dr,
                                          ZZr[:, :, i],
                                          SALT[:, :, i],
                                          dr,
                                          Zr[:, :, i],
                                          extrap=True)
            Vvel[:, :, i] = calc.griddata(Dv,
                                          ZZv[:, :, i],
                                          V[:, :, i],
                                          dv,
                                          Zv[:, :, i],
                                          extrap=True)
            if i < Uvel.shape[2]:
                du = np.tile(calc.distance(xu[:, ind], yu[:, ind]), (nz, 1))
                Du = np.tile(calc.distance(xu[:, ind], yu[:, ind]), (NZ, 1))
                Uvel[:, :, i] = calc.griddata(Du,
                                              ZZu[:, :, i],
                                              U[:, :, i],
                                              du,
                                              Zu[:, :, i],
                                              extrap=True)
                if useInd:
                    distu[:, :, i] = du

    # uv bar:
    if not quiet: print 'calc uvbar'
    if useInd is False:
        ubar, vbar = rt.uvbar(Uvel, Vvel, sshr, hr, sparams)
    else:  #>------------------------------------------------------------
        sshu = calc.griddata(data['lon'],
                             data['lat'],
                             data['ssh'],
                             xu,
                             yu,
                             extrap=True)
        sshv = calc.griddata(data['lon'],
                             data['lat'],
                             data['ssh'],
                             xv,
                             yv,
                             extrap=True)

        if ij == 'j':
            sshu = sshu[ij_ind, :]
            sshv = sshv[ij_ind, :]
            hu = hu[ij_ind, :]
            hv = hv[ij_ind, :]
        elif ij == 'i':
            sshu = sshu[:, ij_ind]
            sshv = sshv[:, ij_ind]
            hu = hu[:, ij_ind]
            hv = hv[:, ij_ind]

        ubar = rt.barotropic(Uvel, sshu, hu, sparams)
        vbar = rt.barotropic(Vvel, sshv, hv, sparams)
    # -----------------------------------------------------------------<

    Vars = cb.odict()
    Vars['temp'] = Temp
    Vars['salt'] = Salt
    Vars['u'] = Uvel
    Vars['v'] = Vvel
    Vars['zeta'] = sshr
    Vars['ubar'] = ubar
    Vars['vbar'] = vbar

    Vars['date'] = data['date']

    if not useInd is False:  #>------------------------------------------
        Vars['depth'] = Zr
        Vars['depthu'] = Zu
        Vars['depthv'] = Zv

        Vars['dist'] = distr
        Vars['distu'] = distu
        Vars['distv'] = distv
    # -----------------------------------------------------------------<

    if retHorizAux: return Vars, horizAux
    else: return Vars
示例#17
0
    def plot(self, z, cmap=meg_cmap, zrange="zero", label=False, showsens=False):
        """The array z of values to be plotted must be in the same
	order as the array returned by get_names(). zrange can be
	'auto', 'zero' which is auto but symmetric around 0, or
	a pair of min, max."""

        make_spline(self.x, self.y, array(z, "d"))
        r = range(self.M)
        for i in r:
            xx = self.X[i]
            for j in r:
                yy = self.Y[j]
                if not self.mask[j, i]:
                    self.Z[j, i] = interpolate(xx, yy, self.x, self.y)

        Z = ma.array(self.Z, mask=self.mask)

        if zrange == "auto" or zrange == "zero":
            zmin = ma.minimum.reduce(Z)
            zmax = ma.maximum.reduce(Z)
            if zrange == "zero":
                # If it crosses zero make it symmetrical.
                if (zmin < 0) and (0 < zmax):
                    maxmax = max(-zmin, zmax)
                    zmin = -maxmax
                    zmax = maxmax
        else:
            zmin = zrange[0]
            zmax = zrange[1]

        # First make nice tics for this range.
        # Then switch to the new limits given by the tics
        # so the contour plot will use the whole range.
        ticks, mticks = scale1(zmin, zmax)
        zmin, zmax = ticks[0], ticks[-1]

        nlevels = 100
        levels = linspace(zmin, zmax, nlevels)

        # plot the boundary and maybe the sensors
        plot(self.boundx, self.boundy, color="black", zorder=1, linewidth=3)
        if showsens:
            scatter(self.x, self.y, s=15, c=(0, 1, 0), zorder=2, marker="o", linewidth=0.5)

        #        contourf(self.X, self.Y, Z, levels, cmap = cmap)
        #        contourf(self.X, self.Y, Z, levels, cmap = matplotlib.cm.hot)
        contourf(self.X, self.Y, Z, levels)
        im = gci()  # save the ContourSet to return
        contour(self.X, self.Y, Z, 10, colors="black")

        if label:
            for name in self.sensors:
                s = self.topo[name]
                text(s.x, s.y, name)

        l = 1.02
        x = -0.01
        y = -0.04
        axis([x, x + l, y, y + l])
        axis("off")
        axis("scaled")
        return im, ticks
def make_panels(fig,datafiles, width=4, height = 3,
                prefix='tmprot_',**kwargs):
    """
    Makes png pictures of the given plot, with different angles.
    Args:
        ax (3D axis): te ax
        angles (list): the list of angles (in degree) under which to
                       take the picture.
        width,height (float): size, in inches, of the output images.
        prefix (str): prefix for the files created. 
     
    Returns: the list of files created (for later removal)
    """
     
    files = []
    
    for i,df in enumerate(datafiles):
        pl.clf()
        fig.set_size_inches(width,height)
        ticks = pub_plots(xmaj = 1000, xmin = 50, xstr = '%d', ymaj = 1000, ymin = 50, ystr = '%d')
        MatPlotParams = {'xtick.major.size': 4, 'ytick.major.size' : 4, 
                         'xtick.minor.size': 4, 'ytick.minor.size': 4,
                         'axes.labelsize': 6, 'xtick.labelsize': 10, 
                         'ytick.labelsize': 8}
        rcParams.update(MatPlotParams)

        pl.subplots_adjust(wspace = 0.35,hspace = 0.35)
        pl.figtext(.5,.95, 'Iteration:%d' %i, ha='center')
        
        indat = pf.open(df)
        data = indat[1].data
        model = indat[2].data
        residual = indat[3].data
        header = indat[2].header
        indat.close()
        
        zoom_min = data.shape[0]/2 - 100
        zoom_max = data.shape[0]/2 + 100
        
        fig.add_subplot(2,2,1)
        pl.title('data')
        pl.imshow(-2.5*np.log10(data[zoom_min:zoom_max,zoom_min:zoom_max])+25.256, cmap = cm.jet_r)
        pl.colorbar()
        ticks.set_plot(pl.gca())
        pl.gca().axes.get_xaxis().set_ticks([])
        pl.gca().axes.get_yaxis().set_ticks([])
        vmin, vmax = pl.gci().get_clim()

        fig.add_subplot(2,2,2)
        pl.title('model')
        pl.imshow(-2.5*np.log10(model[zoom_min:zoom_max,zoom_min:zoom_max])+25.256, cmap = cm.jet_r, vmin=vmin, vmax=vmax)
        ticks.set_plot(pl.gca())
        pl.text(0.1, 0.95,'$m_{total}$: %4.2f, $r_e$: %4.1f' %(float(header['1_MAG'].split()[0]),float(header['1_RE'].split()[0])),
             horizontalalignment='left',
             verticalalignment='center',
             fontsize=6, color = 'white',
             transform = pl.gca().transAxes)
        if i==17:
            pl.text(0.1, 0.05,'$n$: %3.2f, $\chi^2_{DOF}$: %.2f' %(float(header['1_N'].split()[0]),header['CHI2NU']),
             horizontalalignment='left',
             verticalalignment='center',
             fontsize=6, color = 'white',
             transform = pl.gca().transAxes)
        else:
            pl.text(0.1, 0.05,'$n$: %3.2f' %(float(header['1_N'].split()[0])),
             horizontalalignment='left',
             verticalalignment='center',
             fontsize=6, color = 'white',
             transform = pl.gca().transAxes)
        pl.gca().axes.get_xaxis().set_ticks([])
        pl.gca().axes.get_yaxis().set_ticks([])
        
        pl.colorbar()

        zoom_resid = -2.5*np.log10(data[zoom_min:zoom_max,zoom_min:zoom_max])+2.5*np.log10(model[zoom_min:zoom_max,zoom_min:zoom_max])
        
        fig.add_subplot(2,2,4)
        pl.title('residual')
        pl.imshow(zoom_resid, cmap = cm.jet)
        #pl.text(0.5, 0.9,'$\chi^2_{DOF}$: %.2f' %header['CHI2NU'],
        #     horizontalalignment='center',
        #     verticalalignment='center',
        #     fontsize=10, color = 'black',
        #     transform = pl.gca().transAxes)
        pl.colorbar()
        vmin, vmax = pl.gci().get_clim()
        ticks.set_plot(pl.gca())
        pl.gca().axes.get_xaxis().set_ticks([])
        pl.gca().axes.get_yaxis().set_ticks([])
        
        fig.add_subplot(2,2,3)
        pl.title('full frame')
        pl.imshow(-2.5*np.log10(data)+2.5*np.log10(model), cmap = cm.jet, vmin = vmin, vmax = vmax)
        
        cbar = pl.colorbar()
        ticks.set_plot(pl.gca())
        pl.gca().axes.get_xaxis().set_ticks([])
        pl.gca().axes.get_yaxis().set_ticks([])

        fname = '%s%03d.png'%(prefix,i)
        pl.savefig(fname)
        pl.savefig('%s_%d.eps'%('my_epsfiles',i))
        files.append(fname)
     
    return files