Beispiel #1
0
  def set_types(self):

    # find nctype and dtype:
    if self.atttype:
      # nctype:
      self.nctype = nctypes.type_2nc(self.atttype,ncver=self.ncversion)

      # dtype:
      isstr=isinstance(self.value,basestring)
      isbool=isinstance(self.value,bool)
      if isstr: strlen=len(str)
      else: strlen=None
      self.dtype  = nctypes.type_2numpy(self.atttype,isstr=isstr,strlen=strlen,
                               isbool=isbool)
    else:
      if not self.nctype: self.nctype = nctypes.type_var2nc(self.value,self.ncversion)
      if not self.dtype: self.dtype = nctypes.type_var2numpy(self.value)
Beispiel #2
0
    def set_types(self):

        # find nctype and dtype:
        if self.atttype:
            # nctype:
            self.nctype = nctypes.type_2nc(self.atttype, ncver=self.ncversion)

            # dtype:
            isstr = isinstance(self.value, basestring)
            isbool = isinstance(self.value, bool)
            if isstr: strlen = len(str)
            else: strlen = None
            self.dtype = nctypes.type_2numpy(self.atttype,
                                             isstr=isstr,
                                             strlen=strlen,
                                             isbool=isbool)
        else:
            if not self.nctype:
                self.nctype = nctypes.type_var2nc(self.value, self.ncversion)
            if not self.dtype: self.dtype = nctypes.type_var2numpy(self.value)
Beispiel #3
0
    def add_var(self, varname, vartype, dimnames, **kargs):
        '''
    Creates netcdf variable

    Ex: add_var('temp','float32',('lon','lat','z'))


    About compression:

    Compression kargs options (available for netcdf4 interface)
      zlib, default True, turn on compression
      lsd (least_significant_digit), default is None: lossy
        compression with lsd precision
      complevel, 1..9, copression level, default 4


    About vartype:

       i) netcdf type names:
         for netcdf3:
           byte,char,short,int,float,double

         for netcdf4:
           + int64, ushort, uint,uint64, string

       ii) Numeric type code: fd1silbwuc

       iii) Numpy dtype or type name

    kargs are required when vartype is a netcdf type name
    and the interface is scientific, see type_2numeric
    and when interface is netcdf4, see type_2dtype

    No need for kargs when:
     1. interface = pycdf
     2. interface is scientific and vartype is numpy type name or
        dtype, or vartipe is numeric typecode
     3. interface is netcdf4 and vartype is numpy type name or dtype
    '''

        if self._interface == 'pycdf':
            vartype = nctypes.type_2pycdf(vartype)
            self._nc.def_var(varname, vartype, dimnames)
        elif self._interface == 'scientific':
            vartype = type_2numeric(vartype, **kargs)
            self._nc.createVariable(varname, vartype, dimnames)
        elif self._interface == 'netcdf4':
            vartype = nctypes.type_2numpy(vartype, **kargs)

            # NOTA: ocorre erro ao criar ex int64 em ncver 3. Pode fazer sentido
            # converter para int? ie, qd o tipo nao e suportado pela ncver!?

            zlib = kargs.get('zlib', True)
            lsd = kargs.get('lsd', None)
            complevel = kargs.get('complevel', 4)
            fill_value = kargs.get('fill_value', None)

            self._nc.createVariable(varname,
                                    vartype,
                                    dimnames,
                                    zlib=zlib,
                                    complevel=complevel,
                                    least_significant_digit=lsd,
                                    fill_value=fill_value)

            newvar = Pyncvar(self, varname)

            # update self.vars and self.varnames:
            self.vars[varname] = newvar

            # update ncdump_info:
            if not self._ncdump_info is False:
                self._ncdump_info['variables'][varname] = cbt.odict()

        return newvar
Beispiel #4
0
  def add_var(self,varname,vartype,dimnames,**kargs):
    '''
    Creates netcdf variable

    Ex: add_var('temp','float32',('lon','lat','z'))


    About compression:

    Compression kargs options (available for netcdf4 interface)
      gzip, default True: gzip compression
      lsd (least_significant_digit), default is None: lossy
        compression with lsd precision


    About vartype:

       i) netcdf type names:
         for netcdf3:
           byte,char,short,int,float,double

         for netcdf4:
           + int64, ushort, uint,uint64, string

       ii) Numeric type code: fd1silbwuc

       iii) Numpy dtype or type name

    kargs are required when vartype is a netcdf type name
    and the interface is scientific, see type_2numeric
    and when interface is netcdf4, see type_2dtype

    No need for kargs when:
     1. interface = pycdf
     2. interface is scientific and vartype is numpy type name or
        dtype, or vartipe is numeric typecode
     3. interface is netcdf4 and vartype is numpy type name or dtype
    '''

    if self._interface=='pycdf':
      vartype=nctypes.type_2pycdf(vartype)
      self._nc.def_var(varname,vartype,dimnames)
    elif self._interface=='scientific':
      vartype=type_2numeric(vartype,**kargs)
      self._nc.createVariable(varname,vartype,dimnames)
    elif self._interface=='netcdf4':
      vartype=nctypes.type_2numpy(vartype,**kargs)

      # NOTA: ocorre erro ao criar ex int64 em ncver 3. Pode fazer sentido
      # converter para int? ie, qd o tipo nao e suportado pela ncver!?

      zlib=True
      lsd=None
      fill_value=None
      if 'gzip' in kargs: zlib = kargs['gzip']
      if 'lsd'  in kargs: lsd  = kargs['lsd']
      if 'fill_value'  in kargs: fill_value  = kargs['fill_value']

      self._nc.createVariable(varname,vartype,dimnames,zlib=zlib,
                              least_significant_digit=lsd,fill_value=fill_value)

      newvar=Pyncvar(self,varname)

      # update self.vars and self.varnames:
      self.vars[varname]=newvar

      # update ncdump_info:
      if not self._ncdump_info is False:
        self._ncdump_info['variables'][varname]=cbt.odict()

    return newvar