def open_file(self, file_name, mode=None, format='nc'): """ open_file() [public] Purpose: Opens a file with the given mode. Parameters: file_name [type=string] Name of the file to open. mode [type=string] How to open the file (e.g. 'r' for reading, 'rw' for reading and writing, etc.) Passing None defaults to opening for reading only. Returns: [nothing] """ # Set the mode default if mode is None: mode = 'r' # If the file is already open, close it. if self._df is not None: self.close() self._file_name = file_name self._format = format # Figure out whether we're reading, writing, or both. self._read = (mode.find('r') > -1) self._write = (mode.find('w') > -1 or mode == 'r+') # if self._write: # warning("DataIO: Writing is currently not supported. Opening as read-only.") # self._write = False if reader.__name__ == "Nio": # If the reader is PyNIO open it with PyNIO's open command self._df = reader.open_file(file_name, mode=mode, format=format) elif reader.__name__ == "scipy.io.netcdf": # If the reader is scipy, open it with scipy's open command if self._read: self._df = reader.netcdf_file(file_name, 'r') elif self._write: self._df = reader.netcdf_file(file_name, 'w') elif reader.__name__ == "Dataset": # If the reader is netCDF4, open it with that open command if self._read: self._df = reader(file_name, mode='r') elif self._write: self._df = reader(file_name, mode='a') return
def open_file(self, file_name, mode=None): """ open_file() [public] Purpose: Opens a file with the given mode. Parameters: file_name [type=string] Name of the file to open. mode [type=string] How to open the file (e.g. 'r' for reading, 'rw' for reading and writing, etc.) Passing None defaults to opening for reading only. Returns: [nothing] """ # Set the mode default if mode is None: mode = 'r' # If the file is already open, close it. if self._df is not None: self.close() self._file_name = file_name # Figure out whether we're reading, writing, or both. self._read = (mode.find('r') > -1) self._write = (mode.find('w') > -1 or mode == 'r+') # if self._write: # warning("DataIO: Writing is currently not supported. Opening as read-only.") # self._write = False if reader.__name__ == "Nio": # If the reader is PyNIO open it with PyNIO's open command self._df = reader.open_file(file_name, mode=mode) elif reader.__name__ == "scipy.io.netcdf": # If the reader is scipy, open it with scipy's open command if self._read: self._df = reader.netcdf_file(file_name, 'r') elif self._write: self._df = reader.netcdf_file(file_name, 'w') elif reader.__name__ == "Dataset": # If the reader is netCDF4, open it with that open command if self._read: self._df = reader(file_name, mode='r') elif self._write: self._df = reader(file_name, mode='a') return
def close(self,out_file_name=''): """ close() [public] Purpose: Closes the file and resets the class to its initial state. The file is set for writing, and any changes have occurred, create a new file and copy all the variables over, without anything that's been "removed." Parameters: [none] Returns: [nothing] """ self._attempted_close = True if self._write: # Copy the data to another file, excluding the removed variables ... tmp_file_name = "%s.tmp" % self._file_name tmp_file = None if reader.__name__ == "Nio": tmp_file = reader.open_file(tmp_file_name, format='nc', mode='w') elif reader.__name__ == "scipy.io.netcdf": tmp_file = reader.netcdf_file(tmp_file_name, mode='w') elif reader.__name__ == "Dataset": tmp_file = reader(tmp_file_name,'w') # Copy dimensions over for dimension, length in self._df.dimensions.iteritems(): if reader.__name__ == "Nio": tmp_file.create_dimension(dimension, length) elif reader.__name__ in ["scipy.io.netcdf", "Dataset"]: tmp_file.createDimension(dimension, len(length)) # Create new dimensions added by splicing for variable, splice_info in self._sp_variables.iteritems(): dim_names, index_lists = splice_info for dim_name, indexes in zip(dim_names, index_lists): if dim_name not in tmp_file.dimensions.keys(): if reader.__name__ == "Nio": tmp_file.create_dimension(dim_name, len(np.unique(indexes))) elif reader.__name__ in ["scipy.io.netcdf", "Dataset"]: tmp_file.createDimension(dim_name, len(np.unique(indexes))) # Copy variable definitions over for variable, data in self._df.variables.iteritems(): if variable not in self._rm_variables: # If this variable is going to be spliced, find its new dimensions if variable not in self._sp_variables: dim_names = data.dimensions else: dim_names, index_lists = self._sp_variables[variable] if reader.__name__ == "Nio": tmp_file.create_variable(variable, data.typecode(), dim_names) elif reader.__name__ == "scipy.io.netcdf": tmp_file.createVariable(variable, data.dtype, dim_names) elif reader.__name__ == "Dataset": if hasattr(data, "_FillValue"): tmp_file.createVariable(variable, data.dtype, dim_names, fill_value=data._FillValue) else: tmp_file.createVariable(variable, data.dtype, dim_names) # Copy file attributes over for attribute in dir(self._df): if not hasattr(tmp_file, attribute) and attribute not in self._rm_attributes['__file__']: setattr(tmp_file, attribute, getattr(self._df, attribute)) # Copy variable attributes over for variable, data in tmp_file.variables.iteritems(): try: for attribute in dir(self._df.variables[variable]): if not hasattr(data, attribute) and attribute not in self._rm_attributes[variable]: setattr(data, attribute, getattr(self._df.variables[variable], attribute)) except KeyError: for attribute in dir(self._df.variables[variable]): if not hasattr(data, attribute): setattr(data, attribute, getattr(self._df.variables[variable], attribute)) # Copy variable data over (NetCDF4 Python appears to require this be done after setting all the variable attributes) for variable, data in self._df.variables.iteritems(): if variable not in self._rm_variables: tmp_variable = np.copy(self._df.variables[variable][:]) # Overwrite the data if we need to ... if variable in self._ov_variables: tmp_variable = np.copy(self._ov_variables[variable]) # Do splicing ... if variable in self._sp_variables: dim_names, index_lists = self._sp_variables[variable] tmp_variable = np.copy(self._df.variables[variable][np.ix_(*index_lists)]) # Put it in the file ... tmp_file.variables[variable][:] = tmp_variable # for dimension, length in tmp_file.dimensions.iteritems(): # print dimension, length self._df.close() if self._write: # Move temporary file to original file's location import shutil if len(out_file_name) > 0: shutil.move(tmp_file_name,out_file_name) else: shutil.move(tmp_file_name, self._file_name) self._df = None self._file_name = "" self._read = False self._write = False self._rm_variables = [] self._sp_variables = {} self._rm_attributes = { '__file__':[] } return
def close(self, out_file_name=''): """ close() [public] Purpose: Closes the file and resets the class to its initial state. The file is set for writing, and any changes have occurred, create a new file and copy all the variables over, without anything that's been "removed." Parameters: [none] Returns: [nothing] """ self._attempted_close = True if self._write: # Copy the data to another file, excluding the removed variables ... tmp_file_name = "%s.tmp" % self._file_name tmp_file = None if reader.__name__ == "Nio": tmp_file = reader.open_file(tmp_file_name, mode='w', format=self._format) elif reader.__name__ == "scipy.io.netcdf": tmp_file = reader.netcdf_file(tmp_file_name, mode='w') elif reader.__name__ == "Dataset": tmp_file = reader(tmp_file_name, 'w') # Copy dimensions over for dimension, length in self._df.dimensions.iteritems(): if reader.__name__ == "Nio": tmp_file.create_dimension(dimension, length) elif reader.__name__ in ["scipy.io.netcdf", "Dataset"]: tmp_file.createDimension(dimension, len(length)) # Create new dimensions added by splicing for variable, splice_info in self._sp_variables.iteritems(): dim_names, index_lists = splice_info for dim_name, indexes in zip(dim_names, index_lists): if dim_name not in tmp_file.dimensions.keys(): if reader.__name__ == "Nio": tmp_file.create_dimension(dim_name, len(np.unique(indexes))) elif reader.__name__ in ["scipy.io.netcdf", "Dataset"]: tmp_file.createDimension(dim_name, len(np.unique(indexes))) # Copy variable definitions over for variable, data in self._df.variables.iteritems(): if variable not in self._rm_variables: # If this variable is going to be spliced, find its new dimensions if variable not in self._sp_variables: dim_names = data.dimensions else: dim_names, index_lists = self._sp_variables[variable] if reader.__name__ == "Nio": tmp_file.create_variable(variable, data.typecode(), dim_names) elif reader.__name__ in ["scipy.io.netcdf", "Dataset"]: tmp_file.createVariable(variable, data.dtype, dim_names) # Copy file attributes over for attribute in dir(self._df): if attribute[:2] != "__" and not hasattr( tmp_file, attribute ) and attribute not in self._rm_attributes['__file__']: setattr(tmp_file, attribute, getattr(self._df, attribute)) # Copy variable attributes over for variable, data in tmp_file.variables.iteritems(): try: for attribute in dir(self._df.variables[variable]): if not hasattr( data, attribute ) and attribute not in self._rm_attributes[variable]: setattr( data, attribute, getattr(self._df.variables[variable], attribute)) except KeyError: for attribute in dir(self._df.variables[variable]): if not hasattr(data, attribute): setattr( data, attribute, getattr(self._df.variables[variable], attribute)) # Copy variable data over (NetCDF4 Python appears to require this be done after setting all the variable attributes) for variable, data in self._df.variables.iteritems(): if variable not in self._rm_variables: tmp_variable = np.copy(self._df.variables[variable][:]) # Overwrite the data if we need to ... if variable in self._ov_variables: tmp_variable = np.copy(self._ov_variables[variable]) # Do splicing ... if variable in self._sp_variables: dim_names, index_lists = self._sp_variables[variable] tmp_variable = np.copy( self._df.variables[variable][np.ix_(*index_lists)]) # Put it in the file ... tmp_file.variables[variable][:] = tmp_variable # for dimension, length in tmp_file.dimensions.iteritems(): # print dimension, length self._df.close() if self._write: # Move temporary file to original file's location import shutil if len(out_file_name) > 0: shutil.move(tmp_file_name, out_file_name) else: shutil.move(tmp_file_name, self._file_name) self._df = None self._file_name = "" self._format = "" self._read = False self._write = False self._rm_variables = [] self._sp_variables = {} self._rm_attributes = {'__file__': []} return