Пример #1
0
  def __init__(self,name):
    if cb.isstr(name):
      import glob
      f=os.path.splitext(name)[0][:-1]+'*'+os.path.splitext(name)[1]
      files=glob.glob(f)
      files.sort()
    else:
      files=name

    if cb.isstr(files[0]): # list of filenames
      self._grids=[Grid(i) for i in files]
    else: self._grids=name # list of Grid, used by MHis only
Пример #2
0
  def __init__(self,name):
    if cb.isstr(name):
      import glob
      f=os.path.splitext(name)[0][:-1]+'*'+os.path.splitext(name)[1]
      files=glob.glob(f)
      files.sort()
    else:
      files=name

    if cb.isstr(files[0]): # list of filenames
      self._grids=[Grid(i) for i in files]
    else: self._grids=name # list of Grid, used by MHis only
Пример #3
0
  def __init__(self,name,grd=False):
    import glob
    if cb.isstr(name):
      f=os.path.splitext(name)[0][:-1]+'*'+os.path.splitext(name)[1]
      files=glob.glob(f)
      files.sort()
    else:
      files=name

    if cb.isstr(grd):
      f=os.path.splitext(grd)[0][:-1]+'*'+os.path.splitext(grd)[1]
      gfiles=glob.glob(f)
      gfiles.sort()

    if grd:
      self._his=[]
      for i in range(len(files)):
        self._his+=[His(files[i],gfiles[i])]
    else:
      self._his=[His(i) for i in files]

    ###self.grid=MGrid([i.grid.name for i in self._his])
    self.grid=MGrid([i.grid for i in self._his])
Пример #4
0
  def __init__(self,name,grd=False):
    import glob
    if cb.isstr(name):
      f=os.path.splitext(name)[0][:-1]+'*'+os.path.splitext(name)[1]
      files=glob.glob(f)
      files.sort()
    else:
      files=name

    if cb.isstr(grd):
      f=os.path.splitext(grd)[0][:-1]+'*'+os.path.splitext(grd)[1]
      gfiles=glob.glob(f)
      gfiles.sort()

    if grd:
      self._his=[]
      for i in range(len(files)):
        self._his+=[His(files[i],gfiles[i])]
    else:
      self._his=[His(i) for i in files]

    ###self.grid=MGrid([i.grid.name for i in self._his])
    self.grid=MGrid([i.grid for i in self._his])
Пример #5
0
  def __init__(self,his,grd=False,quiet=True):
    self.name=his
    self.isremote=False
    if cb.isstr(his):
      if his.startswith('http:'): self.isremote=True
      else: self.name=os.path.realpath(his)

    self.type='his'
    self.quiet=quiet

    self.nc=netcdf.ncopen(self.name)
    self.load_grid(grd)
    self.load()
    self.load_dims()
    self.load_atts()
Пример #6
0
  def __init__(self,his,grd=False,quiet=True):
    self.name=his
    self.isremote=False
    if cb.isstr(his):
      if his.startswith('http:'): self.isremote=True
      else: self.name=os.path.realpath(his)

    self.type='his'
    self.quiet=quiet

    self.nc=netcdf.ncopen(self.name)
    self.load_grid(grd)
    self.load()
    self.load_dims()
    self.load_atts()
Пример #7
0
  def load_vars(self,vars):
    var=netcdf.var(self.nc)
    for i in vars:
      if not self.quiet: print(':: loading var %s'%i)

      if isinstance(i,dict):
        iname, iopts = list(i.items())[0]
      elif cb.isstr(i):
        iname=i
        if iname.startswith('@'): iopts=iname[1:]
        else: iopts=iname

      if not isinstance(iopts,tuple):
        iopts=(iopts,)

      found=0
      if not hasattr(self,'var_as'): self.var_as={}
      for j in iopts:
        if j in var:
          if iname.startswith('@'): # variable will be loaded only when needed!
            iname=iname[1:]

            # can't use self._get inside lambda function... j is always the last element!
            # so, instead of:
            # setattr(self.__class__, iname, property(fget=lambda self: self._get(j)))
            # a propVar variable is used, storing the riht j value:
            ob=propVar(j)
            setattr(self.__class__,iname,property(fget=ob.get))##lambda self: self.getx(vv)))
          else:
            setattr(self,iname,var[j][:])

          found=1
          if not self.quiet: print('  - found %s as %s'%(iname,j))

          # store original name and other info, like units
          try: units=var[j].atts['units'].value
          except: units=''
          self.var_as[iname]={'name':j,'units':units}
          break
      if not found and not self.quiet: print(':: %s not found in %s'%(str(iopts),self.name))
Пример #8
0
  def load_vars(self,vars):
    var=netcdf.var(self.nc)
    for i in vars:
      if not self.quiet: print(':: loading var %s'%i)

      if isinstance(i,dict):
        iname, iopts = list(i.items())[0]
      elif cb.isstr(i):
        iname=i
        if iname.startswith('@'): iopts=iname[1:]
        else: iopts=iname

      if not isinstance(iopts,tuple):
        iopts=(iopts,)

      found=0
      if not hasattr(self,'var_as'): self.var_as={}
      for j in iopts:
        if j in var:
          if iname.startswith('@'): # variable will be loaded only when needed!
            iname=iname[1:]

            # can't use self._get inside lambda function... j is always the last element!
            # so, instead of:
            # setattr(self.__class__, iname, property(fget=lambda self: self._get(j)))
            # a propVar variable is used, storing the riht j value:
            ob=propVar(j)
            setattr(self.__class__,iname,property(fget=ob.get))##lambda self: self.getx(vv)))
          else:
            setattr(self,iname,var[j][:])

          found=1
          if not self.quiet: print('  - found %s as %s'%(iname,j))

          # store original name and other info, like units
          try: units=var[j].atts['units'].value
          except: units=''
          self.var_as[iname]={'name':j,'units':units}
          break
      if not found and not self.quiet: print(':: %s not found in %s'%(str(iopts),self.name))
Пример #9
0
def data2romsblk(data, grd, **kargs):
    quiet = kargs.get('quiet', True)
    Margin = kargs.get('margin',
                       4)  # for griddata and for original data to keep
    # if margin is 'no calc', no extraction of
    # a domain portion is done

    # about projection:
    # - if auto will use grid default projection
    # - if False, no projection is used
    # - any Basemap projection can be used
    proj = kargs.get('proj', 'auto')

    # about origina data to keep:
    # - if 2, will keep data only inside rt.grid_vicinity rectange
    # - if 1, all original data is kept
    keepOriginal = 2
    for k in kargs:
        if k.lower().startswith('keepor'): keepOriginal = kargs[k]

    if cb.isstr(grd): g = roms.Grid(grd)
    else: g = grd

    if proj == 'auto': proj = g.get_projection()

    if proj:
        data_x, data_y = proj(data['x'], data['y'])
        grd_x, grd_y = proj(g.lon, g.lat)
    else:
        data_x, data_y = data['x'], data['y']
        grd_x, grd_y = g.lon, g.lat

    cond = False
    out = {}

    for vname in data:
        if vname.startswith('INFO') or vname in 'xy': continue
        # vnames starting with INFO are an info key, without data

        if not quiet: print('  interp %s' % vname)

        if cond is False:
            if Margin == 'no calc':
                cond = np.ones_like(data['x'], 'bool')
                i1, i1 = 0, 0
                j2, i2 = data['x'].shape
            else:
                cond, inds = rt.grid_vicinity(grd,
                                              data['x'],
                                              data['y'],
                                              margin=Margin,
                                              rect=True,
                                              retinds=True)
                i1, i2, j1, j2 = inds

        out[vname] = calc.griddata(data_x[cond],
                                   data_y[cond],
                                   data[vname][cond],
                                   grd_x,
                                   grd_y,
                                   extrap=True)

        if keepOriginal:
            # keep original data:
            if keepOriginal == 1:
                out[vname +
                    '_original'] = data[vname]  # keep all original data
            elif keepOriginal == 2:
                out[vname + '_original'] = data[vname][
                    j1:j2, i1:i2]  # keep data only inside vicinity rectangle

            if 'x_original' not in out:
                if keepOriginal == 1:
                    out['x_original'] = data['x']
                    out['y_original'] = data['y']
                elif keepOriginal == 2:
                    out['x_original'] = data['x'][j1:j2, i1:i2]
                    out['y_original'] = data['y'][j1:j2, i1:i2]

    # about wind:
    if not quiet: print(' --> rot U,V wind and U,V wind stress')
    out['uwnd'], out['vwnd'] = calc.rot2d(out['uwnd'], out['vwnd'], g.angle)
    out['sustr'], out['svstr'] = calc.rot2d(out['sustr'], out['svstr'],
                                            g.angle)

    out['sustr'] = rt.rho2uvp(out['sustr'], 'u')
    out['svstr'] = rt.rho2uvp(out['svstr'], 'v')

    return out
Пример #10
0
def data2romsblk(data,grd,**kargs):
  quiet=kargs.get('quiet',True)
  Margin=kargs.get('margin',4) # for griddata and for original data to keep
                               # if margin is 'no calc', no extraction of
                               # a domain portion is done

  # about projection:
  # - if auto will use grid default projection
  # - if False, no projection is used
  # - any Basemap projection can be used
  proj=kargs.get('proj','auto')

  # about origina data to keep:
  # - if 2, will keep data only inside rt.grid_vicinity rectange
  # - if 1, all original data is kept
  keepOriginal = 2
  for k in kargs:
    if k.lower().startswith('keepor'): keepOriginal=kargs[k]

  if cb.isstr(grd): g=roms.Grid(grd)
  else: g=grd

  if proj=='auto': proj=g.get_projection()

  if proj:
    data_x,data_y=proj(data['x'],data['y'])
    grd_x,grd_y=proj(g.lon,g.lat)
  else:
    data_x,data_y=data['x'],data['y']
    grd_x,grd_y=g.lon,g.lat

  cond=False
  out={}

  for vname in data:
    if vname.startswith('INFO') or vname in 'xy': continue
    # vnames starting with INFO are an info key, without data

    if not quiet: print('  interp %s' % vname)

    if cond is False:
      if Margin=='no calc':
        cond=np.ones_like(data['x'],'bool')
        i1,i1=0,0
        j2,i2=data['x'].shape
      else:
        cond,inds=rt.grid_vicinity(grd,data['x'],data['y'],
                                   margin=Margin,rect=True,retinds=True)
        i1,i2,j1,j2=inds

    out[vname]=calc.griddata(data_x[cond],data_y[cond],
                           data[vname][cond],grd_x,grd_y,extrap=True)

    if keepOriginal:
      # keep original data:
      if keepOriginal==1:
        out[vname+'_original']=data[vname] # keep all original data
      elif keepOriginal==2:
        out[vname+'_original']=data[vname][j1:j2,i1:i2] # keep data only inside vicinity rectangle

      if 'x_original' not in out:
        if keepOriginal==1:
          out['x_original']=data['x']
          out['y_original']=data['y']
        elif keepOriginal==2:
          out['x_original']=data['x'][j1:j2,i1:i2]
          out['y_original']=data['y'][j1:j2,i1:i2]

  # about wind:
  if not quiet: print(' --> rot U,V wind and U,V wind stress')
  out['uwnd'],out['vwnd']=calc.rot2d(out['uwnd'],out['vwnd'],g.angle)
  out['sustr'],out['svstr']=calc.rot2d(out['sustr'],out['svstr'],g.angle)

  out['sustr']=rt.rho2uvp(out['sustr'],'u')
  out['svstr']=rt.rho2uvp(out['svstr'],'v')

  return out