def _convert_to_valid_data_type(self, array, format): # Convert the format to a type we understand if isinstance(array, Delayed): return array elif array is None: return array else: if 'A' in format and 'P' not in format: if array.dtype.char in 'SU': fsize = int(_convert_format(format)[1:]) return chararray.array(array, itemsize=fsize) else: numpy_format = _convert_format(format) return _convert_array(array, np.dtype(numpy_format)) elif 'L' in format: # boolean needs to be scaled back to storage values ('T', 'F') if array.dtype == np.dtype('bool'): return np.where(array == False, ord('F'), ord('T')) else: return np.where(array == 0, ord('F'), ord('T')) elif 'X' not in format and 'P' not in format: (repeat, fmt, option) = _parse_tformat(format) # Preserve byte order of the original array for now; see #77 numpy_format = array.dtype.byteorder + _convert_format(fmt) return _convert_array(array, np.dtype(numpy_format)) elif 'X' in format: return _convert_array(array, np.dtype('uint8')) else: return array
def _convert_to_valid_data_type(self, array): # Convert the format to a type we understand if isinstance(array, Delayed): return array elif array is None: return array else: format = self.format dims = self._dims if 'A' in format and 'P' not in format: if array.dtype.char in 'SU': if dims: # The 'last' dimension (first in the order given # in the TDIMn keyword itself) is the number of # characters in each string fsize = dims[-1] else: fsize = int(_convert_format(format)[1:]) return chararray.array(array, itemsize=fsize) else: numpy_format = _convert_format(format) return _convert_array(array, np.dtype(numpy_format)) elif 'L' in format: # boolean needs to be scaled back to storage values ('T', 'F') if array.dtype == np.dtype('bool'): return np.where(array == False, ord('F'), ord('T')) else: return np.where(array == 0, ord('F'), ord('T')) elif 'X' not in format and 'P' not in format: (repeat, fmt, option) = _parse_tformat(format) # Preserve byte order of the original array for now; see #77 numpy_format = array.dtype.byteorder + _convert_format(fmt) return _convert_array(array, np.dtype(numpy_format)) elif 'X' in format: return _convert_array(array, np.dtype('uint8')) else: return array