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 .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', create_variable=create_variable, close=close) setattr(file_proxy.__class__, 'set_option', set_option) file_proxy.file = file 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 = {} for var in file.variables.keys(): vp = _proxy(file.variables[var], 'str', 'len', __setitem__=__setitem__, __getitem__=__getitem__, get_value=get_value, assign_value=assign_value) vp.file = file_proxy vp.varname = var 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 file_proxy.variables = variable_proxies #print file_proxy, file_proxy.variables return file_proxy
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
''' opt = _Nio.options() return opt ''' Since the NioVariable and NioFile types do not actually get created until a file is opened, do a minimal file open here to create the types and make them public ''' filepath = os.getenv("NCARG_NCARG") fname = 'netcdf/pop.nc' if filepath: filepath = os.path.join(filepath,"data",fname) if os.access(filepath,os.R_OK): file = _Nio.open_file(filepath) file_proxy = _proxy(file, 'str', __del__=__del__,create_variable=create_variable,create_group=create_group,close=close) file_proxy.parent = None var_proxy = _proxy(file.variables['t'],'str','len', __setitem__=__setitem__,__getitem__=__getitem__,get_value=get_value,assign_value=assign_value) for cls in _known_proxy_classes.values(): if isinstance(file_proxy,cls): NioFile = cls elif isinstance(var_proxy,cls): NioVariable = cls __all__.append(NioFile) __all__.append(NioVariable) file.close() del(file_proxy)
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 .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', create_variable=create_variable,close=close) setattr(file_proxy.__class__,'set_option',set_option) file_proxy.file = file 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 = {} for var in file.variables.keys(): vp = _proxy(file.variables[var],'str','len', __setitem__=__setitem__,__getitem__=__getitem__,get_value=get_value,assign_value=assign_value) vp.file = file_proxy vp.varname = var 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 file_proxy.variables = variable_proxies #print file_proxy, file_proxy.variables return file_proxy