예제 #1
0
    def add_att(self, attname, attvalue, atttype=False):
        '''
    Creates file or variable attribute
    atttype can be a netcdf type name, numpy type name, dtype or
    numeric typecode
    '''
        if self._interface == 'pycdf':
            if atttype: Type = nctypes.type_2pycdf(atttype)
            else: Type = nctypes.type_var2pycdf(attvalue)
            a = self._nc.attr(attname)
            a.put(Type, attvalue)

        elif self._interface in ('scientific', 'netcdf4'):
            setattr(self._nc, attname, attvalue)

        newatt = Pyncatt(self._nc,
                         attname,
                         attvalue,
                         atttype=atttype,
                         ncversion=self.ncversion,
                         interface=self._interface)

        # update self.atts and self.attnames:
        self.atts[attname] = newatt

        # update ncdump_info:
        if not isinstance(self, Pyncvar):
            if not self._ncdump_info is False:
                self._ncdump_info['attributes'][attname] = {}

        return newatt
예제 #2
0
파일: pync4.py 프로젝트: jsh1012/okean
  def add_att(self,attname,attvalue,atttype=False):
    '''
    Creates file or variable attribute
    atttype can be a netcdf type name, numpy type name, dtype or
    numeric typecode
    '''
    if self._interface=='pycdf':
      if atttype: Type=nctypes.type_2pycdf(atttype)
      else:       Type=nctypes.type_var2pycdf(attvalue)
      a=self._nc.attr(attname)
      a.put(Type,attvalue)

    elif self._interface in ('scientific','netcdf4'):
      setattr(self._nc,attname,attvalue)

    newatt=Pyncatt(self._nc,attname,attvalue,
                   atttype=atttype,ncversion=self.ncversion,
                   interface=self._interface)

    # update self.atts and self.attnames:
    self.atts[attname]=newatt

    # update ncdump_info:
    if not isinstance(self,Pyncvar):
      if not self._ncdump_info is False:
        self._ncdump_info['attributes'][attname]={}

    return newatt
예제 #3
0
    def update(self, value, atttype=False):
        self.value = value
        self.atttype = atttype
        self.set_types()

        if self._interface == 'pycdf':
            if self.atttype: Type = nctypes.type_2pycdf(self.atttype)
            else: Type = nctypes.type_var2pycdf(self.value)
            a = self._nc.attr(self.name)
            a.put(Type, self.value)

        elif self._interface in ('scientific', 'netcdf4'):
            setattr(self._nc, self.name, value)
예제 #4
0
파일: pync4.py 프로젝트: jsh1012/okean
  def update(self,value,atttype=False):
      self.value=value
      self.atttype=atttype
      self.set_types()

      if self._interface=='pycdf':
        if self.atttype: Type=nctypes.type_2pycdf(self.atttype)
        else:            Type=nctypes.type_var2pycdf(self.value)
        a=self._nc.attr(self.name)
        a.put(Type,self.value)

      elif self._interface in ('scientific','netcdf4'):
        setattr(self._nc,self.name,value)
예제 #5
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
예제 #6
0
파일: pync4.py 프로젝트: jsh1012/okean
  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