def _qc(self, file_name, out_file_name=''): """ _qc() [private] Purpose: Carries out a quality control operation on a single file. Parameters: file_name [type=str] The name of the file to quality control. out_file_name [type=str] Optional file name specifying where to write the output of the quality control procedure. If not specified, the file given by file_name will be overwritten with quality-controlled data. Returns: [nothing] """ file = DataIO(file_name=file_name, mode="rw") file_data_good = {} file_data_dims = () for variables, function in self.rules.iteritems(): # Loop through all the rules that we've defined and find the indexes of all the good data. if variables.find(",") > -1: variables = re.split(", *", variables) else: variables = [ variables ] for var in variables: if var not in file_data_good: file_data_dims = file.get_variable_attribute(var, 'dimensions') file_data_good[var] = np.array(np.ones(tuple([ file.get_dimension(d) for d in file_data_dims ]), dtype=bool)) # Get the variable data from the file (returns as a list of numpy arrays) file_data = file.get_variable(variables) # Plug the list straight into the quality control function. Do the element-wise "and" procedure between the file_data_good array and the returned array immediately. good = function(*file_data) for var in variables: file_data_good[var] &= good # file_data_good &= file_data_good[:,5].data.reshape((file.get_dimension(file_data_dims[0]), 1)) for var in file_data_good.keys(): # Do the splicing for every variable with the same dimensions as the variables we've qc'ed. qc_data = np.where(file_data_good[var], file.get_variable(var), np.nan) file.set_variable(var, qc_data) # new_dims = [] # keep_indexes = [] # for idx, var_dim in enumerate(var_dims): # if var_dim in file_data_dims: # new_dims.append("%s_qc" % var_dim) # keep_indexes.append(np.unique(np.where(file_data_good)[idx])) # else: # new_dims.append(var_dim) # keep_indexes.append(np.arange(file.get_dimension(var_dim), dtype=np.int32)) # file.splice_variable(variable, tuple(new_dims), tuple(keep_indexes)) # Sew 'er up file.close(out_file_name=out_file_name) return
#!/usr/local/epd/bin/python import sys sys.path.append("../") from util.dataIO import DataIO import numpy as np df = DataIO("wrfout_d01_PLEV.nc", mode="rw") missing_value = df.get_variable_attribute('T', 'missing_value') df.set_variable_attribute('P', 'missing_value', missing_value) df.close()