Exemplo n.º 1
0
Arquivo: roms.py Projeto: bssrdf/okean
  def path_s_levels(self,time,x,y,rw='r',**kargs):
    inds=kargs.get('inds',None)

    xr,yr,h,m=self.grid.vars()
    zeta=self.use('zeta',SEARCHtime=time)

    if inds:
      i0,i1=inds['xi']
      j0,j1=inds['eta']

      xr=xr[j0:j1,i0:i1]
      yr=yr[j0:j1,i0:i1]
      h=h[j0:j1,i0:i1]
      m=m[j0:j1,i0:i1]
      zeta= zeta[j0:j1,i0:i1]

    if np.ma.isMA(zeta): h=np.ma.masked_where(zeta.mask,h)
    else:# probably a clm/ini file. Mask maskc point (pygridgen complex grid):
      if 'maskc' in netcdf.varnames(self.grid.nc):
        mc=self.grid.use('maskc')
        if inds: mc=mc[j0:j1,i0:i1]
        h=np.ma.masked_where(mc==0,h)
        zeta=np.ma.masked_where(mc==0,zeta)

    h    = calc.griddata(xr,yr,h,x,y,extrap=False)
    zeta = calc.griddata(xr,yr,zeta,x,y,extrap=False)

    z=rt.s_levels(h,zeta,self.s_params,rw=rw)
    z=np.squeeze(z)
    return np.ma.masked_where(np.isnan(z),z)
Exemplo n.º 2
0
    def s_levels(self,
                 sparams,
                 zeta=0,
                 h=False,
                 ruvpw='r',
                 i=False,
                 j=False,
                 k=False):
        ruvpw = ruvpw[0]
        isW = ruvpw == 'w'

        if h is False:
            h = self.h

        try:
            zeta.shape == h.shape
        except:
            zeta = np.tile(zeta, h.shape).astype(h.dtype)

        if h.ndim == 2:
            h = rt.rho2uvp(h, ruvpw)
            zeta = rt.rho2uvp(zeta, ruvpw)

        zr, zw = rt.s_levels(h, zeta, sparams)
        if isW: z = zw
        else: z = zr

        if k is False: k = slice(None)
        if j is False: j = slice(None)
        if i is False: i = slice(None)

        return np.squeeze(z[k, j, i])
Exemplo n.º 3
0
Arquivo: roms.py Projeto: bssrdf/okean
  def s_levels(self,sparams,zeta=0,h=False,loc='rr',i=False,j=False,k=False):
    try:
      hLoc,vLoc=loc
    except:
      hLoc,vLoc=loc,'r'

####    ruvpw=ruvpw[0]
####    isW=ruvpw=='w'

    if h is False:
      h=self.h

    try:
      zeta.shape==h.shape
    except:
      zeta=np.tile(zeta,h.shape).astype(h.dtype)

    if h.ndim==2:
      h=rt.rho2uvp(h,hLoc)
      zeta=rt.rho2uvp(zeta,hLoc)

    z=rt.s_levels(h,zeta,sparams,rw=vLoc)
###    zr,zw=rt.s_levels(h,zeta,sparams)
###    if isW:z=zw
###    if vLoc=='w': z=zw
###    else: z=zr

    if k is False: k=slice(None)
    if j is False: j=slice(None)
    if i is False: i=slice(None)

    return np.squeeze(z[k,j,i])
Exemplo n.º 4
0
    def s_levels(self,
                 time,
                 ruvpw='r',
                 i=False,
                 j=False,
                 k=False,
                 extrapZeta=False):
        ruvpw = ruvpw[0]

        h = self.grid.h
        zeta = self.use('zeta', SEARCHtime=time)

        if extrapZeta:
            if not calc.ismarray(zeta):
                zeta = np.ma.masked_where(self.grid.mask == 0, zeta)
            zeta = calc.mask_extrap(self.grid.lon, self.grid.lat, zeta)

        h = rt.rho2uvp(h, ruvpw)
        zeta = rt.rho2uvp(zeta, ruvpw)

        z = rt.s_levels(h, zeta, self.s_params, rw=ruvpw)

        if k is False: k = slice(None)
        if j is False: j = slice(None)
        if i is False: i = slice(None)

        return z[k, j, i]
Exemplo n.º 5
0
  def s_levels(self,time,loc='rr',i=False,j=False,k=False,extrapZeta=False):
##    ruvpw=ruvpw[0]
    try:
      hLoc,vLoc=loc
    except:
      hLoc,vLoc=loc,'r'
      

    h=self.grid.h
    zeta=self.use('zeta',SEARCHtime=time)

    if extrapZeta:
      if not calc.ismarray(zeta): zeta=np.ma.masked_where(self.grid.mask==0,zeta)
      zeta=calc.mask_extrap(self.grid.lon,self.grid.lat,zeta)

    h=rt.rho2uvp(h,hLoc) ##########ruvpw)
    zeta=rt.rho2uvp(zeta,hLoc) ###########ruvpw)

    z=rt.s_levels(h,zeta,self.s_params,rw=vLoc) ##########ruvpw)

    if k is False: k=slice(None)
    if j is False: j=slice(None)
    if i is False: i=slice(None)

    return z[k,j,i]
Exemplo n.º 6
0
  def path_s_levels(self,time,x,y,rw='r',**kargs):
    inds=kargs.get('inds',None)

    xr,yr,h,m=self.grid.vars()
    zeta=self.use('zeta',SEARCHtime=time)

    if inds:
      i0,i1=inds['xi']
      j0,j1=inds['eta']

      xr=xr[j0:j1,i0:i1]
      yr=yr[j0:j1,i0:i1]
      h=h[j0:j1,i0:i1]
      m=m[j0:j1,i0:i1]
      zeta= zeta[j0:j1,i0:i1]

    if np.ma.isMA(zeta): h=np.ma.masked_where(zeta.mask,h)
    else:# probably a clm/ini file. Mask maskc point (pygridgen complex grid):
      if 'maskc' in netcdf.varnames(self.grid.nc):
        mc=self.grid.use('maskc')
        if inds: mc=mc[j0:j1,i0:i1]
        h=np.ma.masked_where(mc==0,h)
        zeta=np.ma.masked_where(mc==0,zeta)

    h    = calc.griddata(xr,yr,h,x,y,extrap=False)
    zeta = calc.griddata(xr,yr,zeta,x,y,extrap=False)

    z=rt.s_levels(h,zeta,self.s_params,rw=rw)
    z=np.squeeze(z)
    return np.ma.masked_where(np.isnan(z),z)
Exemplo n.º 7
0
  def s_levels(self,sparams,zeta=0,h=False,loc='rr',i=False,j=False,k=False):
    try:
      hLoc,vLoc=loc
    except:
      hLoc,vLoc=loc,'r'

####    ruvpw=ruvpw[0]
####    isW=ruvpw=='w'

    if h is False:
      h=self.h

    try:
      zeta.shape==h.shape
    except:
      zeta=np.tile(zeta,h.shape).astype(h.dtype)

    if h.ndim==2:
      h=rt.rho2uvp(h,hLoc)
      zeta=rt.rho2uvp(zeta,hLoc)

    z=rt.s_levels(h,zeta,sparams,rw=vLoc)
###    zr,zw=rt.s_levels(h,zeta,sparams)
###    if isW:z=zw
###    if vLoc=='w': z=zw
###    else: z=zr

    if k is False: k=slice(None)
    if j is False: j=slice(None)
    if i is False: i=slice(None)

    return np.squeeze(z[k,j,i])
Exemplo n.º 8
0
  def s_levels(self,sparams,zeta=0,h=False,ruvpw='r',i=False,j=False,k=False):
    ruvpw=ruvpw[0]
    isW=ruvpw=='w'

    if h is False:
      h=self.h

    try:
      zeta.shape==h.shape
    except:
      zeta=np.tile(zeta,h.shape).astype(h.dtype)

    if h.ndim==2:
      h=rt.rho2uvp(h,ruvpw)
      zeta=rt.rho2uvp(zeta,ruvpw)

    zr,zw=rt.s_levels(h,zeta,sparams)
    if isW:z=zw
    else: z=zr

    if k is False: k=slice(None)
    if j is False: j=slice(None)
    if i is False: i=slice(None)

    return np.squeeze(z[k,j,i])
Exemplo n.º 9
0
  def path_s_levels(self,time,x,y,rw='r'):
    xr,yr,h,m=self.grid.vars()
    zeta=self.use('zeta',SEARCHtime=time)
    h    = calc.griddata(xr,yr,h,x,y,extrap=False)
    zeta = calc.griddata(xr,yr,zeta,x,y,extrap=False)

    z=rt.s_levels(h,zeta,self.s_params,rw=rw)
    z=np.squeeze(z)
    return np.ma.masked_where(np.isnan(z),z)
Exemplo n.º 10
0
    def s_levels(self, itime, isw=False):
        tts, ttb, hc, N = self.s_params
        zeta = self.use('zeta', ftime=itime)

        zr, zw = rt.s_levels(self.h, zeta, hc, tts, ttb, N)
        if isw: z = zw
        else: z = zr

        return squeeze(z)
Exemplo n.º 11
0
  def s_levels(self,itime,isw=False):
    tts,ttb,hc,N=self.s_params
    zeta=self.use('zeta',ftime=itime)

    zr,zw=rt.s_levels(self.h,zeta,hc,tts,ttb,N)
    if isw:z=zw
    else: z=zr

    return squeeze(z)
Exemplo n.º 12
0
    def path_s_levels(self, time, x, y, rw='r'):
        xr, yr, h, m = self.grid.vars()
        zeta = self.use('zeta', SEARCHtime=time)

        if np.ma.isMA(zeta): h = np.ma.masked_where(zeta.mask, h)
        else:  # probably a clm/ini file. Mask maskc point (pygridgen complex grid):
            if 'maskc' in netcdf.varnames(self.grid.name):
                mc = self.grid.use('maskc')
                h = np.ma.masked_where(mc == 0, h)
                zeta = np.ma.masked_where(mc == 0, zeta)

        h = calc.griddata(xr, yr, h, x, y, extrap=False)
        zeta = calc.griddata(xr, yr, zeta, x, y, extrap=False)

        z = rt.s_levels(h, zeta, self.s_params, rw=rw)
        z = np.squeeze(z)
        return np.ma.masked_where(np.isnan(z), z)
Exemplo n.º 13
0
  def path_s_levels(self,time,x,y,rw='r'):
    xr,yr,h,m=self.grid.vars()
    zeta=self.use('zeta',SEARCHtime=time)

    if np.ma.isMA(zeta): h=np.ma.masked_where(zeta.mask,h)
    else:# probably a clm/ini file. Mask maskc point (pygridgen complex grid):
      if 'maskc' in netcdf.varnames(self.grid.name):
        mc=self.grid.use('maskc')
        h=np.ma.masked_where(mc==0,h)
        zeta=np.ma.masked_where(mc==0,zeta)

    h    = calc.griddata(xr,yr,h,x,y,extrap=False)
    zeta = calc.griddata(xr,yr,zeta,x,y,extrap=False)

    z=rt.s_levels(h,zeta,self.s_params,rw=rw)
    z=np.squeeze(z)
    return np.ma.masked_where(np.isnan(z),z)
Exemplo n.º 14
0
    def s_levels(self, time, ruvpw="r", i=False, j=False, k=False, extrapZeta=False):
        ruvpw = ruvpw[0]

        h = self.grid.h
        zeta = self.use("zeta", SEARCHtime=time)

        if extrapZeta:
            if not calc.ismarray(zeta):
                zeta = np.ma.masked_where(self.grid.mask == 0, zeta)
            zeta = calc.mask_extrap(self.grid.lon, self.grid.lat, zeta)

        h = rt.rho2uvp(h, ruvpw)
        zeta = rt.rho2uvp(zeta, ruvpw)

        z = rt.s_levels(h, zeta, self.s_params, rw=ruvpw)

        if k is False:
            k = slice(None)
        if j is False:
            j = slice(None)
        if i is False:
            i = slice(None)

        return z[k, j, i]
Exemplo n.º 15
0
    def time_series(self, varname, x, y, times=False, depth=False, **opts):
        t, z, v = [[]] * 3

        RetVarOnly = False
        savename = False
        retMsg = False
        nearest = True  # UNIQUE case Currently
        plot = False

        if 'retvar' in opts.keys(): RetVarOnly = opts['retvar']
        if 'savename' in opts.keys(): savename = opts['savename']
        if 'msg' in opts.keys(): retMsg = opts['msg']
        if 'nearest' in opts.keys(): nearest = opts['msg']
        if 'plot' in opts.keys(): plot = opts['msg']

        if varname not in netcdf.varnames(self.name):
            msg = ':: variable %s not found' % varname
            if retMsg: return t, z, v, msg
            else:
                print(msg)
                return t, z, v

        isW = varname == 'w'
        hasZ = self.hasz(varname)

        if not depth is False and hasZ:
            if isW and depth >= self.S_W:
                msg = 'k = %d exceeds S_W dimension (%d)' % (depth, self.S_W)
                if retMsg: return t, z, v, msg
                else:
                    print(msg)
                    return t, z, v

            elif depth >= self.S_RHO:
                msg = 'k = %d exceeds S_RHO dimension (%d)' % (depth,
                                                               self.S_RHO)
                if retMsg: return t, z, v, msg
                else:
                    print(msg)
                    return t, z, v

        if times is False:
            times = 0, len(self.tdays)

        if self.hast(varname) and times[-1] > self.TIME:
            msg = 't = %d exceeds TIME dimension (%d)' % (times[-1], self.TIME)
            if retMsg: return t, z, v, msg
            else:
                print(msg)
                return t, z, v

        lon, lat, hr, mr = self.grid.vars(ruvp=self.var_at(varname))
        dist = (lon - x)**2 + (lat - y)**2
        i, j = np.where(dist == dist.min())
        i, j = i[0], j[0]
        v = self.use(varname,
                     xiSEARCH=j,
                     etaSEARCH=i,
                     SEARCHtime=range(times[0], times[-1])).T

        # time:
        t = self.tdays[range(times[0], times[-1])]
        t = t + 0. * v

        # depth:
        if hasZ:
            h = self.grid.h[i, j]
            zeta = self.use('zeta',
                            xiSEARCH=j,
                            etaSEARCH=i,
                            SEARCHtime=range(times[0], times[-1]))
            h = h + 0 * zeta
            z = rt.s_levels(h, zeta, self.s_params, rw=varname)
            z = np.squeeze(z)

            if not depth is False:
                if depth >= 0:
                    t = t[0, :]
                    z = z[depth, :]
                    v = v[depth, :]
                else:
                    t0, z0 = t, z
                    t = t[0, :]
                    z = depth + 0. * t
                    v = calc.griddata(t0,
                                      z0,
                                      v,
                                      t,
                                      z,
                                      extrap=False,
                                      norm_xy=True)

        else:  # not hasZ
            z = 0. * t

        if plot:
            pass  # TODO

        if RetVarOnly:
            return v
        else:
            if retMsg: return t, z, v, ''
            else: return t, z, v
Exemplo n.º 16
0
  def time_series(self,varname,x,y,times=None,depth=None,**opts):
########    plot= opts.get('plot',False)
########    ax  = opts.get('ax',None)
    coords=opts.get('coords',self._default_coords('sliceiso')).split(',')

    if times is None: times=range(0,self.time.size)

    out=Data()
    out.msg=self.check_slice(varname,t=np.max(times),k=depth) 
    if out.msg: return out###None,aux


##    t,z,v=[[]]*3
#
#    RetVarOnly=False
#    savename=False
#    retMsg=False
#    nearest=True  # UNIQUE case Currently
#    plot=False
#
#    if 'retvar'   in opts.keys(): RetVarOnly = opts['retvar']
#    if 'savename' in opts.keys(): savename   = opts['savename']
#    if 'msg'      in opts.keys(): retMsg     = opts['msg']
#    if 'nearest'  in opts.keys(): nearest    = opts['msg']
#    if 'plot'     in opts.keys(): plot       = opts['msg']
#
#
#    if varname not in netcdf.varnames(self.name):
#      msg=':: variable %s not found' % varname
#      if retMsg: return t,z,v,msg
#      else:
#        print msg
#        return t,z,v
#
#    isW=varname=='w'
#    hasZ=self.hasz(varname)
#
#    if not depth is False and hasZ :
#      if isW and depth >= self.S_W:
#        msg='k = %d exceeds S_W dimension (%d)' % (depth,self.S_W)
#        if retMsg: return t,z,v,msg
#        else:
#          print msg
#          return t,z,v
#
#      elif depth >= self.S_RHO:
#        msg='k = %d exceeds S_RHO dimension (%d)' % (depth,self.S_RHO)
#        if retMsg: return t,z,v,msg
#        else:
#          print msg
#          return t,z,v
#
#    if times is False:
#      times=0,len(self.tdays)
#
#    if self.hast(varname) and times[-1]>self.TIME:
#      msg='t = %d exceeds TIME dimension (%d)' % (times[-1],self.TIME)
#      if retMsg: return t,z,v,msg
#      else:
#        print msg
#        return t,z,v
#

    # find nearest point:
    lon,lat,hr,mr=self.grid.vars(ruvp=self.var_at(varname))
    dist=(lon-x)**2+(lat-y)**2
    i,j=np.where(dist==dist.min())
    i,j=i[0],j[0]

    if depth>=0: arg={'s_SEARCH':depth}
    else: arg={}
    v=self.use(varname,xiSEARCH=j,etaSEARCH=i,SEARCHtime=times,**arg).T

    # calculate depths:
    if self.hasz(varname):
      h=self.grid.h[i,j]
      zeta=self.use('zeta',xiSEARCH=j,etaSEARCH=i,SEARCHtime=times)
      h=h+0*zeta
      z=rt.s_levels(h,zeta,self.s_params,rw=varname)
      z=np.squeeze(z)

    # depth slice:
    if not depth is None and self.hasz(varname) and depth<0:
      if v.ndim==2:
        from matplotlib.dates import date2num
        t=np.tile(date2num(self.time[times]),(v.shape[0],1))
        v=calc.griddata(t,z,v,t[0],depth+np.zeros(t[0].shape),
          extrap=opts.get('extrap',False),norm_xy=opts.get('norm_xy',False))
          # norm_xy True may be needed!
          # extrap also may be needed cos the 1st and last value may be masked!

      else: # one time only
        v=np.interp(depth,z,v,left=np.nan,right=np.nan)
        v=np.ma.masked_where(np.isnan(v),v) 
     
    # coords
    if 't' in coords:
      if v.ndim==2:
        out.t=np.tile(self.time[times],(v.shape[0],1))
        from matplotlib.dates import date2num
        out.tnum=np.tile(date2num(self.time[times]),(v.shape[0],1))
      else: out.t=self.time[times]

    if 'z' in coords and self.hasz(varname):
      if not depth is None:
        if depth>=0: out.z=z[depth,...]
        else: out.z=depth+0*v
      else: out.z=z

    if 'x' in coords: out.x=lon[i,j]
    if 'y' in coords: out.y=lat[i,j]
   
##    if plot and v.ndim:
##      if not ax:
##        ax=pl.gca()
##        opts['ax']=ax
##
##      if v.ndim==2:
##        p=ax.pcolormesh(out.tnum,out.z,v)
##        #ax.xaxis_date()
##        pl.colorbar(p,shrink=.7)
##      elif v.size>1:
##        if out.t.size>1: # time series:
##          ax.plot(out.t,v)
##        else: # vertical profile:
##          ax.plot(v,out.z)

    out.v=v
    out.coordsReq=','.join(sorted(coords))
    return out##v,aux
Exemplo n.º 17
0
Arquivo: roms.py Projeto: bssrdf/okean
  def time_series(self,varname,x,y,times=None,depth=None,**opts):
    coords=opts.get('coords',self._default_coords('time_series')).split(',')

    if times is None: times=range(0,self.time.size)

    # depth or s_level: check is is float or if is negative!
    isDepth=False
    if not depth is None:
       if calc.isiterable(depth): depth=np.asarray(depth)
       if calc.isarray(depth):
         isDepth=np.any(depth<0) or depth.kind!='i' 
       else: isDepth=depth<0 or np.asarray(depth).dtype.kind!='i'

    out=Data()
    if not depth is None and not isDepth:
      out.msg=self.check_slice(varname,t=np.max(times),k=depth) 
    else:
      out.msg=self.check_slice(varname,t=np.max(times)) 

    if out.msg: return out

    # find nearest point:
    lon,lat,hr,mr=self.grid.vars(ruvp=self.var_at(varname))
    dist=(lon-x)**2+(lat-y)**2
    i,j=np.where(dist==dist.min())
    i,j=i[0],j[0]

    if not depth is None and not isDepth: arg={'s_SEARCH':depth}
    else: arg={}
    v=self.use(varname,xiSEARCH=j,etaSEARCH=i,SEARCHtime=times,**arg).T

    # calculate depths:
    if self.hasz(varname):
      h=self.grid.h[i,j]
      zeta=self.use('zeta',xiSEARCH=j,etaSEARCH=i,SEARCHtime=times)
      h=h+0*zeta
####      z=rt.s_levels(h,zeta,self.s_params,rw=varname)
      z=rt.s_levels(h,zeta,self.s_params,rw=self.var_at(varname)[1])
      z=np.squeeze(z)

    # depth slice:
    if isDepth and self.hasz(varname):
      if v.ndim==2:
        # could use calc.griddata, but better use slicez cos data at
        # different times may be independent!
        if 0:
          from matplotlib.dates import date2num
          t=np.tile(date2num(self.time[times]),(v.shape[0],1))
          v=calc.griddata(t,z,v,t[0],depth+np.zeros(t[0].shape),
            extrap=opts.get('extrap',False),norm_xy=opts.get('norm_xy',False))
            # norm_xy True may be needed!
            # extrap also may be needed cos the 1st and last value may be masked!

        else:
          nt=len(times)
          land_mask=np.ones((nt,1),dtype=v.dtype) # needed for slicez... not used here!        

          v,vm=rt.slicez(v[...,np.newaxis],land_mask,
               self.grid.h[i,j]*np.ones((nt,1),dtype=v.dtype), # bottom depth
               zeta[:,np.newaxis],self.s_params,depth,spline=opts.get('spline',True))

          v=np.ma.masked_where(vm,v)
          v=v[...,0]

      else: # one time only
        v=np.interp(depth,z,v,left=np.nan,right=np.nan)
        v=np.ma.masked_where(np.isnan(v),v) 


    out.v=v
    out.info['v']['name']=varname
    out.info['v']['slice']='time series'
    try: out.info['v']['units']=netcdf.vatt(self.nc,varname,'units')
    except: pass
     
    # coords
    if 't' in coords and self.hast(varname):
      if v.ndim==2:
        out.t=np.tile(self.time[times],(v.shape[0],1))
        from matplotlib.dates import date2num
        out.tnum=np.tile(date2num(self.time[times]),(v.shape[0],1))
      else: out.t=self.time[times]
      out.info['t']['name']='Time'
      out.info['tnum']=dict(name='Time',units=self.var_as['time']['units'])

    if 'z' in coords and self.hasz(varname):
      if not depth is None:
        if not isDepth: out.z=z[depth,...]
        else: out.z=depth+0*v
      else: out.z=z
      out.info['z']=dict(name='Depth',units='m')

    if 'x' in coords:
      out.x=lon[i,j]
      if self.grid.is_spherical:
         out.info['x']=dict(name='Longitude',units=r'$\^o$E')
      else:
        out.x=x/1000.
        out.info['x']=dict(name='X-position',units='km')

    if 'y' in coords:
      out.y=lat[i,j]
      if self.grid.is_spherical:
        out.info['y']=dict(name='Latitude',units=r'$\^o$N')
      else:
        out.y=y/1000.
        out.info['y']=dict(name='Y-position',units='km')


    out.coordsReq=','.join(sorted(coords))
    return out
Exemplo n.º 18
0
  def time_series(self,varname,x,y,times=False,depth=False,**opts):
    t,z,v=[[]]*3

    RetVarOnly=False
    savename=False
    retMsg=False
    nearest=True  # UNIQUE case Currently
    plot=False

    if 'retvar'   in opts.keys(): RetVarOnly = opts['retvar']
    if 'savename' in opts.keys(): savename   = opts['savename']
    if 'msg'      in opts.keys(): retMsg     = opts['msg']
    if 'nearest'  in opts.keys(): nearest    = opts['msg']
    if 'plot'     in opts.keys(): plot       = opts['msg']


    if varname not in netcdf.varnames(self.name):
      msg=':: variable %s not found' % varname
      if retMsg: return t,z,v,msg
      else:
        print(msg)
        return t,z,v

    isW=varname=='w'
    hasZ=self.hasz(varname)

    if not depth is False and hasZ :
      if isW and depth >= self.S_W:
        msg='k = %d exceeds S_W dimension (%d)' % (depth,self.S_W)
        if retMsg: return t,z,v,msg
        else:
          print(msg)
          return t,z,v

      elif depth >= self.S_RHO:
        msg='k = %d exceeds S_RHO dimension (%d)' % (depth,self.S_RHO)
        if retMsg: return t,z,v,msg
        else:
          print(msg)
          return t,z,v

    if times is False:
      times=0,len(self.tdays)

    if self.hast(varname) and times[-1]>self.TIME:
      msg='t = %d exceeds TIME dimension (%d)' % (times[-1],self.TIME)
      if retMsg: return t,z,v,msg
      else:
        print(msg)
        return t,z,v

    lon,lat,hr,mr=self.grid.vars(ruvp=self.var_at(varname))
    dist=(lon-x)**2+(lat-y)**2
    i,j=np.where(dist==dist.min())
    i,j=i[0],j[0]
    v=self.use(varname,xiSEARCH=j,etaSEARCH=i,SEARCHtime=range(times[0],times[-1])).T

    # time:
    t=self.tdays[range(times[0],times[-1])]
    t=t+0.*v

    # depth:
    if hasZ:
      h=self.grid.h[i,j]
      zeta=self.use('zeta',xiSEARCH=j,etaSEARCH=i,SEARCHtime=range(times[0],times[-1]))
      h=h+0*zeta
      z=rt.s_levels(h,zeta,self.s_params,rw=varname)
      z=np.squeeze(z)

      if not depth is False:
        if depth>=0:
          t=t[0,:]
          z=z[depth,:]
          v=v[depth,:]
        else:
          t0,z0=t,z
          t=t[0,:]
          z=depth+0.*t
          v=calc.griddata(t0,z0,v,t,z,extrap=False,norm_xy=True)

    else: # not hasZ
      z=0.*t


    if plot:
      pass # TODO


    if RetVarOnly:
      return v
    else:
      if retMsg: return t,z,v,''
      else: return t,z,v
Exemplo n.º 19
0
  def time_series(self,varname,x,y,times=None,depth=None,**opts):
    coords=opts.get('coords',self._default_coords('time_series')).split(',')

    if times is None: times=range(0,self.time.size)

    # depth or s_level: check is is float or if is negative!
    isDepth=False
    if not depth is None:
       if calc.isiterable(depth): depth=np.asarray(depth)
       if calc.isarray(depth):
         isDepth=np.any(depth<0) or depth.kind!='i' 
       else: isDepth=depth<0 or np.asarray(depth).dtype.kind!='i'

    out=Data()
    if not depth is None and not isDepth:
      out.msg=self.check_slice(varname,t=np.max(times),k=depth) 
    else:
      out.msg=self.check_slice(varname,t=np.max(times)) 

    if out.msg: return out

    # find nearest point:
    lon,lat,hr,mr=self.grid.vars(ruvp=self.var_at(varname))
    dist=(lon-x)**2+(lat-y)**2
    i,j=np.where(dist==dist.min())
    i,j=i[0],j[0]

    if not depth is None and not isDepth: arg={'s_SEARCH':depth}
    else: arg={}
    v=self.use(varname,xiSEARCH=j,etaSEARCH=i,SEARCHtime=times,**arg).T

    # calculate depths:
    if self.hasz(varname):
      h=self.grid.h[i,j]
      zeta=self.use('zeta',xiSEARCH=j,etaSEARCH=i,SEARCHtime=times)
      h=h+0*zeta
####      z=rt.s_levels(h,zeta,self.s_params,rw=varname)
      z=rt.s_levels(h,zeta,self.s_params,rw=self.var_at(varname)[1])
      z=np.squeeze(z)

    # depth slice:
    if isDepth and self.hasz(varname):
      if v.ndim==2:
        # could use calc.griddata, but better use slicez cos data at
        # different times may be independent!
        if 0:
          from matplotlib.dates import date2num
          t=np.tile(date2num(self.time[times]),(v.shape[0],1))
          v=calc.griddata(t,z,v,t[0],depth+np.zeros(t[0].shape),
            extrap=opts.get('extrap',False),norm_xy=opts.get('norm_xy',False))
            # norm_xy True may be needed!
            # extrap also may be needed cos the 1st and last value may be masked!

        else:
          nt=len(times)
          land_mask=np.ones((nt,1),dtype=v.dtype) # needed for slicez... not used here!        

          v,vm=rt.slicez(v[...,np.newaxis],land_mask,
               self.grid.h[i,j]*np.ones((nt,1),dtype=v.dtype), # bottom depth
               zeta[:,np.newaxis],self.s_params,depth,spline=opts.get('spline',True))

          v=np.ma.masked_where(vm,v)
          v=v[...,0]

      else: # one time only
        v=np.interp(depth,z,v,left=np.nan,right=np.nan)
        v=np.ma.masked_where(np.isnan(v),v) 


    out.v=v
    out.info['v']['name']=varname
    out.info['v']['slice']='time series'
    try: out.info['v']['units']=netcdf.vatt(self.nc,varname,'units')
    except: pass
     
    # coords
    if 't' in coords and self.hast(varname):
      if v.ndim==2:
        out.t=np.tile(self.time[times],(v.shape[0],1))
        from matplotlib.dates import date2num
        out.tnum=np.tile(date2num(self.time[times]),(v.shape[0],1))
      else: out.t=self.time[times]
      out.info['t']['name']='Time'
      out.info['tnum']=dict(name='Time',units=self.var_as['time']['units'])

    if 'z' in coords and self.hasz(varname):
      if not depth is None:
        if not isDepth: out.z=z[depth,...]
        else: out.z=depth+0*v
      else: out.z=z
      out.info['z']=dict(name='Depth',units='m')

    if 'x' in coords:
      out.x=lon[i,j]
      if self.grid.is_spherical:
         out.info['x']=dict(name='Longitude',units=r'$\^o$E')
      else:
        out.x=x#/1000.
        out.info['x']=dict(name='X-position',units='m')

    if 'y' in coords:
      out.y=lat[i,j]
      if self.grid.is_spherical:
        out.info['y']=dict(name='Latitude',units=r'$\^o$N')
      else:
        out.y=y#/1000.
        out.info['y']=dict(name='Y-position',units='m')


    out.coordsReq=','.join(sorted(coords))
    return out