cleanup for exodus
@@ -8,37 +8,13 @@ I/O for Exodus etc.
import numpy
-def read(filename):
- import netCDF4
-
- nc = netCDF4.Dataset(filename)
-
- points = numpy.array([
- nc.variables['coordx'][:],
- nc.variables['coordy'][:],
- nc.variables['coordz'][:],
- ]).T
-
- elem_types = {
- 'quad': 'quad',
- 'HEX': 'hexahedron',
- 'TETRA': 'tetra',
- 'TRIANGLE': 'triangle',
- }
-
- point_data = {}
- if 'name_nod_var' in nc.variables:
- variable_names = [
- b''.join(c).decode('UTF-8')
- for c in nc.variables['name_nod_var'][:]
- ]
-
- n = len(variable_names)
- is_accounted_for = n * [False]
-
+def merge_variables(variable_names):
# Exodus stores variables as "<name>_Z", "<name>_R" to form a
# two-dimensional value and "<name>X", "<name>Y", "<name>Z" to form a
# three-dimensional value. Separate those out first
+ n = len(variable_names)
+ is_accounted_for = n * [False]
+
variable_indices = {}
for k, var in enumerate(variable_names):
@@ -81,6 +57,37 @@ def read(filename):
assert all(is_accounted_for)
+ return variable_indices
+
+
+def read(filename):
+ import netCDF4
+
+ nc = netCDF4.Dataset(filename)
+
+ if 'coordx' in nc.variables:
+ points = numpy.array([
+ nc.variables['coordx'][:],
+ nc.variables['coordy'][:],
+ nc.variables['coordz'][:],
+ ]).T
+ else:
+ assert 'coord' in nc.variables
+ points = nc.variables['coord'][:].T
+ if points.shape[1] == 2:
+ points = numpy.column_stack([
+ points,
+ numpy.zeros(len(points))
+ ])
+
+ # handle point data
+ point_data = {}
+ if 'name_nod_var' in nc.variables:
+ variable_names = [
+ b''.join(c).decode('UTF-8')
+ for c in nc.variables['name_nod_var'][:]
+ ]
+ variable_indices = merge_variables(variable_names)
for name, indices in variable_indices.items():
if len(indices) == 1:
point_data[name] = \
@@ -91,6 +98,15 @@ def read(filename):
for index in indices
]).T
+ elem_types = {
+ 'HEX': 'hexahedron',
+ 'quad': 'quad',
+ 'SHELL4': 'quad',
+ 'TETRA': 'tetra',
+ 'TRI3': 'triangle',
+ 'TRIANGLE': 'triangle',
+ }
+
cells = {}
for key in nc.variables:
var = nc.variables[key]
