Example #1
0
  def border(self,type='r',margin=0,di=1,dj=1):
    if   type == 'r': lon,lat=self.lon, self.lat
    elif type == 'u': lon,lat=self.lonu,self.latu
    elif type == 'v': lon,lat=self.lonv,self.latv
    elif type == 'p': lon,lat=self.lonp,self.latp

    if margin>0:
      m,n=lon.shape
      lon=lon[margin:m-margin,margin:n-margin]
      lat=lat[margin:m-margin,margin:n-margin]
    elif margin<0: # expand grid in multiples of last cell
      n=-margin
      x=lon.copy()
      y=lat.copy()

      x[0,:]=(n+1)*x[0,:]-n*x[1,:]
      y[0,:]=(n+1)*y[0,:]-n*y[1,:]

      x[-1,:]=(n+1)*x[-1,:]-n*x[-2,:]
      y[-1,:]=(n+1)*y[-1,:]-n*y[-2,:]

      x[:,0]=(n+1)*x[:,0]-n*x[:,1]
      y[:,0]=(n+1)*y[:,0]-n*y[:,1]

      x[:,-1]=(n+1)*x[:,-1]-n*x[:,-2]
      y[:,-1]=(n+1)*y[:,-1]-n*y[:,-2]

      lon=x
      lat=y

    xb=calc.var_border(lon,di,dj)
    yb=calc.var_border(lat,di,dj)
    return xb,yb
Example #2
0
  def border(self,type='r',margin=0,di=1,dj=1):
    if   type == 'r': lon,lat=self.lon, self.lat
    elif type == 'u': lon,lat=self.lonu,self.latu
    elif type == 'v': lon,lat=self.lonv,self.latv
    elif type == 'p': lon,lat=self.lonp,self.latp

    if margin>0:
      m,n=lon.shape
      lon=lon[margin:m-margin,margin:n-margin]
      lat=lat[margin:m-margin,margin:n-margin]
    elif margin<0: # expand grid in multiples of last cell
      n=-margin
      x=lon.copy()
      y=lat.copy()

      x[0,:]=(n+1)*x[0,:]-n*x[1,:]
      y[0,:]=(n+1)*y[0,:]-n*y[1,:]

      x[-1,:]=(n+1)*x[-1,:]-n*x[-2,:]
      y[-1,:]=(n+1)*y[-1,:]-n*y[-2,:]

      x[:,0]=(n+1)*x[:,0]-n*x[:,1]
      y[:,0]=(n+1)*y[:,0]-n*y[:,1]

      x[:,-1]=(n+1)*x[:,-1]-n*x[:,-2]
      y[:,-1]=(n+1)*y[:,-1]-n*y[:,-2]

      lon=x
      lat=y

    xb=calc.var_border(lon,di,dj)
    yb=calc.var_border(lat,di,dj)
    return xb,yb
Example #3
0
def ww3_specpoints(romsgrd,nseg=(5,5),addxy=(.001,.001)):
  gx=netcdf.use(romsgrd,'lon_rho')
  gy=netcdf.use(romsgrd,'lat_rho')
  mask=netcdf.use(romsgrd,'mask_rho')
  eta,xi=gx.shape

  if 0: # use range (step=nseg)
    spec_res=nseg
    dxi,deta=spec_res

    ix=range(0,xi,dxi)+[xi-1]
    iy=range(0,eta,deta)+[eta-1]
  else: # use linspace (nseg=n segments)
    nNS,nEW=nseg
    ix=np.round(np.linspace(0,xi-1,nNS)).astype('i')
    iy=np.round(np.linspace(0,eta-1,nEW)).astype('i')


  ix,iy=np.meshgrid(ix,iy)
  ix=calc.var_border(ix)
  iy=calc.var_border(iy)

  # unsorted unique:
  _,i=np.unique(ix+iy*1j,return_index=True)
  i=np.sort(i)

  ix=ix[i]
  iy=iy[i]

  # add 1st:
  ix=np.append(ix,ix[0])
  iy=np.append(iy,iy[0])

  # create segments:
  segx=[]
  segy=[]
  for i in range(len(ix)-1):
    I=ix[i],ix[i+1]
    J=iy[i],iy[i+1]
    i0,i1=np.min(I),np.max(I)
    j0,j1=np.min(J),np.max(J)

    if i0==i1: mseg=mask[j0:j1,i0]
    else:      mseg=mask[j0,i0:i1]


    if 1:
      # not use fully masked segments:
      if np.all(mseg==0):
        print 'masked segment %d %d %d %d'%(i0,j0,i1,j1)
        continue
    else:
      # not use if segment starts with mask:
      if mseg.size and mseg[0]==0:
        print 'masked 1st point of segment %d %d %d %d'%(i0,j0,i1,j1)
        continue

   
    segx+=[[i0,i1]]
    segy+=[[j0,j1]]

  # XY and IJ:
  XY=[]
  IJ=[]
  for i in range(len(segx)):
    i0,i1=segx[i][0],segx[i][1]
    j0,j1=segy[i][0],segy[i][1]

    IJ+=[[i0,j0,i1,j1]]
    XY+=[[gx[j0,i0], gy[j0,i0], gx[j1,i1], gy[j1,i1]]]

  IJ=np.asarray(IJ)
  XY=np.asarray(XY)

  # got to be sure the point is inside the comp grid avoiding the message:
  # "** Error            : Boundary point outside comp. grid"
  if not addxy is False:
    XY=in_comp_grid(XY,IJ,addxy)

  return XY,IJ
Example #4
0
def ww3_specpoints(romsgrd, nseg=(5, 5), addxy=(.001, .001)):
    gx = netcdf.use(romsgrd, 'lon_rho')
    gy = netcdf.use(romsgrd, 'lat_rho')
    mask = netcdf.use(romsgrd, 'mask_rho')
    eta, xi = gx.shape

    if 0:  # use range (step=nseg)
        spec_res = nseg
        dxi, deta = spec_res

        ix = range(0, xi, dxi) + [xi - 1]
        iy = range(0, eta, deta) + [eta - 1]
    else:  # use linspace (nseg=n segments)
        nNS, nEW = nseg
        ix = np.round(np.linspace(0, xi - 1, nNS)).astype('i')
        iy = np.round(np.linspace(0, eta - 1, nEW)).astype('i')

    ix, iy = np.meshgrid(ix, iy)
    ix = calc.var_border(ix)
    iy = calc.var_border(iy)

    # unsorted unique:
    _, i = np.unique(ix + iy * 1j, return_index=True)
    i = np.sort(i)

    ix = ix[i]
    iy = iy[i]

    # add 1st:
    ix = np.append(ix, ix[0])
    iy = np.append(iy, iy[0])

    # create segments:
    segx = []
    segy = []
    for i in range(len(ix) - 1):
        I = ix[i], ix[i + 1]
        J = iy[i], iy[i + 1]
        i0, i1 = np.min(I), np.max(I)
        j0, j1 = np.min(J), np.max(J)

        if i0 == i1: mseg = mask[j0:j1, i0]
        else: mseg = mask[j0, i0:i1]

        if 1:
            # not use fully masked segments:
            if np.all(mseg == 0):
                print 'masked segment %d %d %d %d' % (i0, j0, i1, j1)
                continue
        else:
            # not use if segment starts with mask:
            if mseg.size and mseg[0] == 0:
                print 'masked 1st point of segment %d %d %d %d' % (i0, j0, i1,
                                                                   j1)
                continue

        segx += [[i0, i1]]
        segy += [[j0, j1]]

    # XY and IJ:
    XY = []
    IJ = []
    for i in range(len(segx)):
        i0, i1 = segx[i][0], segx[i][1]
        j0, j1 = segy[i][0], segy[i][1]

        IJ += [[i0, j0, i1, j1]]
        XY += [[gx[j0, i0], gy[j0, i0], gx[j1, i1], gy[j1, i1]]]

    IJ = np.asarray(IJ)
    XY = np.asarray(XY)

    # got to be sure the point is inside the comp grid avoiding the message:
    # "** Error            : Boundary point outside comp. grid"
    if not addxy is False:
        XY = in_comp_grid(XY, IJ, addxy)

    return XY, IJ