Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def resolve_data_size(self, index):
# Resolves the size of a given data element based on the names in the
# existing rec_array. Assumes repeating data element names follow the
# format _X
if self.data_structure_type != DataStructureType.recarray:
message = 'Data structure type is {}. Data structure type must ' \
'be recarray.'.format(self.data_structure_type)
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(
self.data_dimensions.structure.get_model(),
self.data_dimensions.structure.get_package(),
self.data_dimensions.structure.path,
'resolving data size',
self.data_dimensions.structure.name,
inspect.stack()[0][3],
type_, value_, traceback_, message,
self._simulation_data.debug)
if len(self.layer_storage.first_item().internal_data[0]) <= index:
return 0
label = self.layer_storage.first_item().\
internal_data.dtype.names[index]
label_list = label.split('_')
if len(label_list) == 1:
return 1
data_expected = []
for data_type in self._recarray_type_list:
data_expected.append('<{}>'.format(
data_type[0]))
message = 'An error occurred when storing data ' \
'"{}" in a recarray. {} data is a one ' \
'or two dimensional list containing ' \
'the variables "{}" (some variables ' \
'may be optional, see MF6 ' \
'documentation), but data "{}" was ' \
'supplied.'.format(
self.data_dimensions.structure.name,
self.data_dimensions.structure.name,
' '.join(data_expected), data)
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(
self.data_dimensions.structure.get_model(),
self.data_dimensions.structure.get_package(),
self.data_dimensions.structure.path,
'setting array data',
self.data_dimensions.structure.name,
inspect.stack()[0][3], type_, value_,
traceback_, message,
self._simulation_data.debug)
self._verify_list(new_data)
return new_data
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(
self.structure.get_model(),
self.structure.get_package(),
self._path,
'storing one based integer',
self.structure.name,
inspect.stack()[0][3], type_,
value_, traceback_, message,
self._simulation_data.debug)
data = self._get_storage_obj().get_data() + 1
else:
data = self._get_storage_obj().get_data()
except Exception as ex:
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(self.structure.get_model(),
self.structure.get_package(),
self._path,
'getting data',
self.structure.name,
inspect.stack()[0][3], type_,
value_, traceback_, None,
self._simulation_data.debug)
try:
# data
values = to_string(data, self._data_type, self._simulation_data,
self._data_dimensions, data_item=data_item)
except Exception as ex:
message = 'Could not convert "{}" of type "{}" ' \
'to a string.'.format(data,
self._data_type)
type_, value_, traceback_ = sys.exc_info()
def make_layered(self):
if self.supports_layered():
try:
self._get_storage_obj().make_layered()
except Exception as ex:
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(self.structure.get_model(),
self.structure.get_package(),
self._path,
'making data layered',
self.structure.name,
inspect.stack()[0][3], type_,
value_, traceback_, None,
self._simulation_data.debug, ex)
else:
if self._data_dimensions.get_model_grid().grid_type() == \
DiscretizationType.DISU:
comment = 'Layered option not available for unstructured ' \
'grid. {}'.format(self._path)
else:
comment = 'Data "{}" does not support layered option. ' \
'{}'.format(self._data_name, self._path)
type_, value_, traceback_ = sys.exc_info()
layer_index = self.layer_storage.nth_index(layer_num)
success = success and self._set_array_layer(layer_data,
layer_index,
multiplier,
key)
if not success:
# try to store as a single layer
success = self._set_array_layer(data, layer, multiplier, key)
self.layered = bool(self.layer_storage.get_total_size() > 1)
if not success:
message = 'Unable to set data "{}" layer {}. Data is not ' \
'in a valid format' \
'.'.format(self.data_dimensions.structure.name,
layer_num)
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(
self.data_dimensions.structure.get_model(),
self.data_dimensions.structure.get_package(),
self.data_dimensions.structure.path, 'setting array data',
self.data_dimensions.structure.name, inspect.stack()[0][3],
type_, value_, traceback_, message,
self._simulation_data.debug)
data_line = org_data_line
return self._load_list_line(
storage, arr_line, line_num,
data_loaded, build_type_list,
current_key, data_index_start,
data_set, True, data_line=data_line)
else:
comment = 'Not enough data provided ' \
'for {}. Data for required ' \
'data item "{}" not ' \
'found'.format(struct.name,
data_item.
name)
type_, value_, \
traceback_ = sys.exc_info()
raise MFDataException(
struct.get_model(),
struct.get_package(),
struct.path,
'loading data list from '
'package file',
struct.name,
inspect.stack()[0][3], type_,
value_, traceback_, comment,
self._simulation_data.debug)
data = arr_line[data_index]
repeat_count += 1
if data_item.type == DatumType.keystring:
self.simple_line = False
if repeat_count <= 1: # only process the
# keyword on the first repeat find
def is_empty(self):
for key, dataset in self.datasets.items():
try:
has_data = dataset.has_data()
except MFDataException as mfde:
raise MFDataException(mfdata_except=mfde,
model=self._container_package.model_name,
package=self._container_package._get_pname(),
message='Error occurred while verifying'
' data of dataset "{}" in block '
'"{}"'.format(
dataset.structure.name,
self.structure.name))
if has_data is not None and has_data:
return False
return True
def get_data(self, apply_mult=False, **kwargs):
try:
if self._get_storage_obj() is None:
return None
return self._get_storage_obj().get_data()
except Exception as ex:
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(self.structure.get_model(),
self.structure.get_package(), self._path,
'getting data', self.structure.name,
inspect.stack()[0][3], type_, value_,
traceback_, None,
self._simulation_data.debug, ex)
def get_data(self, layer=None, apply_mult=False, **kwargs):
if self._get_storage_obj() is None:
self._data_storage = self._new_storage(False)
if isinstance(layer, int):
layer = (layer,)
storage = self._get_storage_obj()
if storage is not None:
try:
data = storage.get_data(layer, apply_mult)
if 'array' in kwargs and kwargs['array'] \
and isinstance(self, MFTransientArray):
data = np.expand_dims(data, 0)
return data
except Exception as ex:
type_, value_, traceback_ = sys.exc_info()
raise MFDataException(self.structure.get_model(),
self.structure.get_package(),
self._path,
'getting data',
self.structure.name,
inspect.stack()[0][3], type_,
value_, traceback_, None,
self._simulation_data.debug, ex)
return None
# each layer stores a different aux variable
layers = len(package_dim.get_aux_variables()[0]) - 1
layer_shape = (layers,)
storage.layered = True
while storage.layer_storage.list_shape[0] < layers:
storage.add_layer()
else:
storage.flatten()
try:
dimensions = storage.get_data_dimensions(
layer_shape)
except Exception as ex:
type_, value_, traceback_ = sys.exc_info()
comment = 'Could not get data shape for key "{}".'.format(
self._current_key)
raise MFDataException(self.structure.get_model(),
self.structure.get_package(),
self._path,
'getting data shape',
self.structure.name,
inspect.stack()[0][3], type_,
value_, traceback_, comment,
self._simulation_data.debug, ex)
layer_size = 1
for dimension in dimensions:
layer_size *= dimension
if aux_var_index is None:
# loop through the number of layers
for layer in storage.layer_storage.indexes():
self._load_layer(layer, layer_size, storage, arr_line,
file_handle, layer_shape)