def create_group(self,name):
    '''
Create a new group with given name in a writable file.

f.create_group(name)
name -- a string specifying the group name.
    '''

    #print 'in create group'
    #import pdb; pdb.set_trace()
    g = self._obj.create_group(name)
    if not g is None:
        gp  = _proxy(g,'str')
        #gp.file = weakref.proxy(g)
        gp.file = g
        if self.parent is None:
            gp.parent = weakref.proxy(self.groups['/'])
        else:
            gp.parent = weakref.proxy(self)
        gp.ma_mode = self.ma_mode
        gp.groups = nd.nioDict()
        gp.groups.path = g.path
        gp.variables = nd.nioDict()
        gp.variables.path = g.path
        if self.groups.topdict is None:
            self.groups[name] = gp
            self.groups['/'].groups[name] = gp
            gp.groups.topdict = self.groups
            gp.variables.topdict = self.variables
        elif self.name is '/': 
            self.groups.topdict[name] = gp
            self.groups[name] = gp
            gp.groups.topdict = self.groups.topdict
            gp.variables.topdict = self.variables.topdict
        else:
            path = self.path + '/' + name
            self.groups.topdict[path] = gp
            self.groups[name] = gp
            gp.groups.topdict = self.groups.topdict
            gp.variables.topdict = self.variables.topdict
        return gp
    else:
        return None
示例#2
0
def create_group(self,name):
    '''
Create a new group with given name in a writable file.

f.create_group(name)
name -- a string specifying the group name.
    '''

    #print 'in create group'
    #import pdb; pdb.set_trace()
    g = self._obj.create_group(name)
    if not g is None:
        gp  = _proxy(g,'str')
        #gp.file = weakref.proxy(g)
        gp.file = g
        if self.parent is None:
            gp.parent = weakref.proxy(self.groups['/'])
        else:
            gp.parent = weakref.proxy(self)
        gp.ma_mode = self.ma_mode
        gp.groups = nd.nioDict()
        gp.groups.path = g.path
        gp.variables = nd.nioDict()
        gp.variables.path = g.path
        if self.groups.topdict is None:
            self.groups[name] = gp
            self.groups['/'].groups[name] = gp
            gp.groups.topdict = self.groups
            gp.variables.topdict = self.variables
        elif self.name is '/': 
            self.groups.topdict[name] = gp
            self.groups[name] = gp
            gp.groups.topdict = self.groups.topdict
            gp.variables.topdict = self.variables.topdict
        else:
            path = self.path + '/' + name
            self.groups.topdict[path] = gp
            self.groups[name] = gp
            gp.groups.topdict = self.groups.topdict
            gp.variables.topdict = self.variables.topdict
        return gp
    else:
        return None
def _create_coordinates_attriibute(file,var):
    coords = nd.nioDict()
    #print "creating coordinates attribute for ", var.name
    if len(var.dimensions) == 1 and var.dimensions[0] == var.name:
        coords[var.name] = var
        return coords

    if var.parent is None:
        parent = file
    else:
        parent = var.parent
    
    cnames = None
    cdict = {}
    if var.attributes.has_key('coordinates'):
        s = var.attributes['coordinates'].replace(',',' ')
        cnames = s.split()
        #print var.attributes['coordinates']

    dlist = []
    if not cnames is None:
        for c in cnames:
            if c in parent.variables.keys():
                cdict[c] = parent.variables[c]
                if dlist.count(cdict[c].dimensions) == 0:
                    dlist.append(cdict[c].dimensions)

    #if len(cdict) > 0: print cdict, dlist
    dims = []
    if len(dlist) == 1:  # means there is only one set of coordinates involved 
        cd = dlist[0] # cd is the dimension tuple
        dims = list(var.dimensions)
        for d in cd:
            if d in dims:
                dims.remove(d)
        coords[cd] = tuple(cdict.values())
                
    for d in dims:
        while not parent is None:
            if d in parent.dimensions.keys():
                for v in parent.variables.values():
                    if d == v.name and v.rank == 1:
                        coords[d] = v
                        break
            parent = parent.parent

    #print coords
    return coords
示例#4
0
def _create_coordinates_attriibute(file,var):
    coords = nd.nioDict()
    #print "creating coordinates attribute for ", var.name
    if len(var.dimensions) == 1 and var.dimensions[0] == var.name:
        coords[var.name] = var
        return coords

    if var.parent is None:
        parent = file
    else:
        parent = var.parent
    
    cnames = None
    cdict = {}
    if var.attributes.has_key('coordinates'):
        s = var.attributes['coordinates'].replace(',',' ')
        cnames = s.split()
        #print var.attributes['coordinates']

    dlist = []
    if not cnames is None:
        for c in cnames:
            if c in parent.variables.keys():
                cdict[c] = parent.variables[c]
                if dlist.count(cdict[c].dimensions) == 0:
                    dlist.append(cdict[c].dimensions)

    #if len(cdict) > 0: print cdict, dlist
    dims = []
    if len(dlist) == 1:  # means there is only one set of coordinates involved 
        cd = dlist[0] # cd is the dimension tuple
        dims = list(var.dimensions)
        for d in cd:
            if d in dims:
                dims.remove(d)
        coords[cd] = tuple(cdict.values())
                
    for d in dims:
        while not parent is None:
            if d in parent.dimensions.keys():
                for v in parent.variables.values():
                    if d == v.name and v.rank == 1:
                        coords[d] = v
                        break
            parent = parent.parent

    #print coords
    return coords
def open_file(filename, mode = 'r', options=None, history='', format=''):
    '''
Open a file containing data in a supported format for reading and/or writing.

f = Nio.open_file(filepath, mode='r',options=None, history='', format='')
filepath -- path of file with data in a supported format. Either the path must
end with an extension indicating the expected format of the file (it need not
be part of the actual file name), or it must be specified using the optional 
'format' argument. Valid extensions include:
    .nc, .cdf, .netcdf, .nc3, .nc4,  -- NetCDF
    .gr, .grb, .grib, .gr1, .grb1, .grib1, .gr2, .grb2, .grib2, -- GRIB
    .hd, .hdf -- HDF
    .h5, .hdf5 -- HDF5
    .he2, .he4, .hdfeos -- HDFEOS
    .he5, .hdfeos5 -- HDFEOS5
    .shp, .mif, .gmt, .rt1 -- shapefiles, other formats supported by GDAL OGR
    .ccm -- CCM history files
Extensions are handled case-insensitvely, i.e.: .grib, .GRIB, and .Grib all
indicate a GRIB file.
mode -- access mode (optional):
     'r' -- open an existing file for reading
     'w','r+','rw','a' -- open an existing file for modification
     'c' -- create a new file open for writing
options -- instance of NioOptions class used to specify generic or 
format-specific options.
history -- a string specifying text to be appended to the file's global
attribute. The attribute is created if it does not exist. Only valid
if the file is opened for writing.
format -- a string specifying the expected format. Valid strings are the
same as the extensions listed above without the initial period (.). 

Returns an NioFile object.
    '''

    ma_mode  = _get_masked_array_mode(options,_Nio.option_defaults)
    use_axis_att = _get_axis_att(options,_Nio.option_defaults)
    explicit_fill_values = _get_option_value(options,_Nio.option_defaults,'ExplicitFillValues')
    mask_below_value = _get_option_value(options,_Nio.option_defaults,'MaskBelowValue')
    mask_above_value = _get_option_value(options,_Nio.option_defaults,'MaskAboveValue')
    
    file = _Nio.open_file(filename,mode,options,history,format)

    file_proxy = _proxy(file, 'str', __del__=__del__,create_variable=create_variable,create_group=create_group,close=close)
    setattr(file_proxy.__class__,'set_option',set_option)
    file_proxy.file = file
    file_proxy.parent = None
    file_proxy.open = 1
    if not (explicit_fill_values is None and mask_below_value is None and mask_above_value is None):
        file_proxy.ma_mode = 'maskedexplicit'
    else:
        file_proxy.ma_mode = ma_mode
    file_proxy.explicit_fill_values = explicit_fill_values
    file_proxy.mask_below_value = mask_below_value
    file_proxy.mask_above_value = mask_above_value

    if use_axis_att:
        cf_dims = _get_cf_dims(file_proxy)
        newdims = {}
        cf2dims = {}
        dimensions = file_proxy.dimensions
        for dim in dimensions:
            try:
                newdim = cf_dims[dim]
            except KeyError:
                newdim = dim
            newdims[newdim] = dimensions[dim]
            cf2dims[newdim] = dim
    else:
        cf2dims = None
        newdims = file_proxy.dimensions
    file_proxy.cf_dimensions = newdims
    file_proxy.cf2dims = cf2dims

    variable_proxies = nd.nioDict()
    variable_proxies.path = '/'
    variable_proxies.topdict = None
    for var in file.variables.keys():
        # print var, ": ", sys.getrefcount(file.variables[var])
        vp  = _proxy(file.variables[var],'str','len',
                     __setitem__=__setitem__,__getitem__=__getitem__,get_value=get_value,assign_value=assign_value)
        vp.file = weakref.proxy(file_proxy)
#        vp.file = file_proxy
        vp.varname = var
        vp.parent = None
        variable_proxies[var] = vp
        if use_axis_att:
            newdims = []
            dimensions = vp.dimensions
            for dim in dimensions:
                try:
                    newdim = cf_dims[dim]
                except KeyError:
                    newdim = dim
                newdims.append(newdim)
            vp.cf_dimensions = tuple(newdims)
        else:
            vp.cf_dimensions = vp.dimensions
        #print var, ": ", sys.getrefcount(file.variables[var])
    file_proxy.variables = variable_proxies

    group_proxies = nd.nioDict()
    group_proxies.path = '/'
    group_proxies.topdict = None
    for group in file.groups.keys():
        # print group, ": ", sys.getrefcount(file.groups[group])
        gp = _proxy(file.groups[group], 'str')
        gp.file = weakref.proxy(file.groups[group])
        gp.ma_mode = file_proxy.ma_mode
        #gp.file = file_proxy
        group_proxies[group] = gp

        v_proxy_refs = nd.nioDict()
        v_proxy_refs.path = file.groups[group].path
        v_proxy_refs.topdict = weakref.proxy(file_proxy.variables)
        #v_proxy_refs.topdict = file_proxy.variables
        for var in file.groups[group].variables.keys():
            #print file.groups[group].variables[var].path
            vp = file_proxy.variables[file.groups[group].variables[var].path]
            #print vp is file_proxy.variables[file.groups[group].variables[var].path]
            vp.parent = weakref.proxy(gp)
            #vp.parent = gp
            v_proxy_refs[var] = weakref.proxy(vp)
            #v_proxy_refs[var] = vp
        gp.variables = v_proxy_refs
        #print group, ": ", sys.getrefcount(file.groups[group])

    file_proxy.groups = group_proxies
    # all the groups need proxies before we can do this

    for group in file.groups.keys():
        g = file.groups[group]
        gp = file_proxy.groups[group]
        sl = group.rfind('/')
        if sl == -1:
            gp.parent = weakref.proxy(file_proxy.groups['/'])
            #gp.parent = file_proxy.groups['/']
        elif sl == 0:
            gp.parent =  weakref.proxy(file_proxy)
        else:
            gp.parent = weakref.proxy(file_proxy.groups[group[0:sl]])
            #gp.parent = file_proxy.groups[group[0:sl]]
        g_proxy_refs = nd.nioDict()
        g_proxy_refs.path = g.path
        g_proxy_refs.topdict = weakref.proxy(file_proxy.groups)
        #g_proxy_refs.topdict = file_proxy.groups
        for grp in g.groups.keys():
            grp_obj = g.groups[grp]
            #print grp_obj.path
            gproxy = file_proxy.groups[grp_obj.path]
            g_proxy_refs[grp] = gproxy
        gp.groups = g_proxy_refs



#    for var in file.variables.keys():
#        file_proxy.variables[var].coordinates = _create_coordinates_attriibute(file_proxy,file_proxy.variables[var])

    

    #print file_proxy, file_proxy.variables
    return file_proxy
示例#6
0
def open_file(filename, mode = 'r', options=None, history='', format=''):
    '''
Open a file containing data in a supported format for reading and/or writing.

f = Nio.open_file(filepath, mode='r',options=None, history='', format='')
filepath -- path of file with data in a supported format. Either the path must
end with an extension indicating the expected format of the file (it need not
be part of the actual file name), or it must be specified using the optional 
'format' argument. Valid extensions include:
    .nc, .cdf, .netcdf, .nc3, .nc4,  -- NetCDF
    .gr, .grb, .grib, .gr1, .grb1, .grib1, .gr2, .grb2, .grib2, -- GRIB
    .hd, .hdf -- HDF
    .h5, .hdf5 -- HDF5
    .he2, .he4, .hdfeos -- HDFEOS
    .he5, .hdfeos5 -- HDFEOS5
    .shp, .mif, .gmt, .rt1 -- shapefiles, other formats supported by GDAL OGR
    .ccm -- CCM history files
Extensions are handled case-insensitvely, i.e.: .grib, .GRIB, and .Grib all
indicate a GRIB file.
mode -- access mode (optional):
     'r' -- open an existing file for reading
     'w','r+','rw','a' -- open an existing file for modification
     'c' -- create a new file open for writing
options -- instance of NioOptions class used to specify generic or 
format-specific options.
history -- a string specifying text to be appended to the file's global
attribute. The attribute is created if it does not exist. Only valid
if the file is opened for writing.
format -- a string specifying the expected format. Valid strings are the
same as the extensions listed above without the initial period (.). 

Returns an NioFile object.
    '''

    ma_mode  = _get_masked_array_mode(options,_Nio.option_defaults)
    use_axis_att = _get_axis_att(options,_Nio.option_defaults)
    explicit_fill_values = _get_option_value(options,_Nio.option_defaults,'ExplicitFillValues')
    mask_below_value = _get_option_value(options,_Nio.option_defaults,'MaskBelowValue')
    mask_above_value = _get_option_value(options,_Nio.option_defaults,'MaskAboveValue')
    
    file = _Nio.open_file(filename,mode,options,history,format)

    file_proxy = _proxy(file, 'str', __del__=__del__,create_variable=create_variable,create_group=create_group,close=close)
    setattr(file_proxy.__class__,'set_option',set_option)
    file_proxy.file = file
    file_proxy.parent = None
    file_proxy.open = 1
    if not (explicit_fill_values is None and mask_below_value is None and mask_above_value is None):
        file_proxy.ma_mode = 'maskedexplicit'
    else:
        file_proxy.ma_mode = ma_mode
    file_proxy.explicit_fill_values = explicit_fill_values
    file_proxy.mask_below_value = mask_below_value
    file_proxy.mask_above_value = mask_above_value

    if use_axis_att:
        cf_dims = _get_cf_dims(file_proxy)
        newdims = {}
        cf2dims = {}
        dimensions = file_proxy.dimensions
        for dim in dimensions:
            try:
                newdim = cf_dims[dim]
            except KeyError:
                newdim = dim
            newdims[newdim] = dimensions[dim]
            cf2dims[newdim] = dim
    else:
        cf2dims = None
        newdims = file_proxy.dimensions
    file_proxy.cf_dimensions = newdims
    file_proxy.cf2dims = cf2dims

    variable_proxies = nd.nioDict()
    variable_proxies.path = '/'
    variable_proxies.topdict = None
    for var in file.variables.keys():
        # print var, ": ", sys.getrefcount(file.variables[var])
        vp  = _proxy(file.variables[var],'str','len',
                     __setitem__=__setitem__,__getitem__=__getitem__,get_value=get_value,assign_value=assign_value)
        vp.file = weakref.proxy(file_proxy)
#        vp.file = file_proxy
        vp.varname = var
        vp.parent = None
        variable_proxies[var] = vp
        if use_axis_att:
            newdims = []
            dimensions = vp.dimensions
            for dim in dimensions:
                try:
                    newdim = cf_dims[dim]
                except KeyError:
                    newdim = dim
                newdims.append(newdim)
            vp.cf_dimensions = tuple(newdims)
        else:
            vp.cf_dimensions = vp.dimensions
        #print var, ": ", sys.getrefcount(file.variables[var])
    file_proxy.variables = variable_proxies

    group_proxies = nd.nioDict()
    group_proxies.path = '/'
    group_proxies.topdict = None
    for group in file.groups.keys():
        # print group, ": ", sys.getrefcount(file.groups[group])
        gp = _proxy(file.groups[group], 'str')
        gp.file = weakref.proxy(file.groups[group])
        gp.ma_mode = file_proxy.ma_mode
        #gp.file = file_proxy
        group_proxies[group] = gp

        v_proxy_refs = nd.nioDict()
        v_proxy_refs.path = file.groups[group].path
        v_proxy_refs.topdict = weakref.proxy(file_proxy.variables)
        #v_proxy_refs.topdict = file_proxy.variables
        for var in file.groups[group].variables.keys():
            #print file.groups[group].variables[var].path
            vp = file_proxy.variables[file.groups[group].variables[var].path]
            #print vp is file_proxy.variables[file.groups[group].variables[var].path]
            vp.parent = weakref.proxy(gp)
            #vp.parent = gp
            v_proxy_refs[var] = weakref.proxy(vp)
            #v_proxy_refs[var] = vp
        gp.variables = v_proxy_refs
        #print group, ": ", sys.getrefcount(file.groups[group])

    file_proxy.groups = group_proxies
    # all the groups need proxies before we can do this

    for group in file.groups.keys():
        g = file.groups[group]
        gp = file_proxy.groups[group]
        sl = group.rfind('/')
        if sl == -1:
            gp.parent = weakref.proxy(file_proxy.groups['/'])
            #gp.parent = file_proxy.groups['/']
        elif sl == 0:
            gp.parent =  weakref.proxy(file_proxy)
        else:
            gp.parent = weakref.proxy(file_proxy.groups[group[0:sl]])
            #gp.parent = file_proxy.groups[group[0:sl]]
        g_proxy_refs = nd.nioDict()
        g_proxy_refs.path = g.path
        g_proxy_refs.topdict = weakref.proxy(file_proxy.groups)
        #g_proxy_refs.topdict = file_proxy.groups
        for grp in g.groups.keys():
            grp_obj = g.groups[grp]
            #print grp_obj.path
            gproxy = file_proxy.groups[grp_obj.path]
            g_proxy_refs[grp] = gproxy
        gp.groups = g_proxy_refs



#    for var in file.variables.keys():
#        file_proxy.variables[var].coordinates = _create_coordinates_attriibute(file_proxy,file_proxy.variables[var])

    

    #print file_proxy, file_proxy.variables
    return file_proxy