ImportGrid¶
- class acoular.grids.ImportGrid¶
Bases:
Grid
Load a 3D grid from an XML file.
This class is used to import a 3D grid defined in an XML file. The grid’s positions and subgrid names are parsed and stored for further processing.
- file = File(filter=['*.xml'], exists=True, desc='name of the xml file to import')¶
Name of the .xml-file from which to read the data.
- subgrids = CArray(desc='names of subgrids for each point')¶
Names of subgrids for each point. This is an optional property, typically used when grids are divided into named subregions.
- digest = Property(depends_on=['_gpos'])¶
A unique identifier for the grid, based on its properties. (read-only)
- import_gpos()¶
Import the grid point locations and subgrid names from an XML file.
This method is automatically called whenever the
file
attribute changes.Notes
The XML file should have elements with tag
<pos>
, where each<pos>
element contains attributes forx
,y
,z
, and optionallysubgrid
.Examples
Generate the positional data of two microphone grids:
>>> import numpy as np >>> >>> # Grid 1: ten points aranged in a circle in the x-y plane at z=0 >>> args = 2 * np.pi * np.arange(10) / 10 >>> x1 = np.cos(args) >>> y1 = np.sin(args) >>> z1 = np.zeros_like(x1) >>> grid1 = np.vstack([x1, y1, z1]).T >>> >>> # Grid 2: nine points aranged in a mesh grid the the x-y plane at z=1 >>> a = np.linspace(-1, 1, 3) >>> x2, y2 = np.meshgrid(a, a) >>> z2 = np.ones_like(x2) >>> grid2 = np.vstack([x2, y2, z2])
Save the generated data in an XML file:
>>> import xml.etree.cElementTree as ET >>> >>> # Create the root element for the XML document >>> root = ET.Element('root') >>> >>> # Loop over both grid 1 and grid 2, and create XML elements for each of their points >>> for num, grid in enumerate([grid1, grid2]): ... for x, y, z in grid: ... # For each point in the grid, create a 'pos' XML element ... ET.SubElement(root, 'pos', subgrid=str(num), x=str(x), y=str(y), z=str(z)) >>> >>> # Create the final XML tree >>> tree = ET.ElementTree(root) >>> >>> # Write the XML tree to a file named 'two_grids.xml' >>> tree.write('two_grids.xml')
The
two_grids.xml
file will look like this:<root> <pos subgrid="0" x="1.0" y="0.0" z="0.0" /> <pos subgrid="0" x="0.8090169943749475" y="0.5877852522924731" z="0.0" /> <pos subgrid="0" x="0.30901699437494745" y="0.9510565162951535" z="0.0" /> <pos subgrid="0" x="-0.30901699437494734" y="0.9510565162951536" z="0.0" /> <pos subgrid="0" x="-0.8090169943749473" y="0.5877852522924732" z="0.0" /> <pos subgrid="0" x="-1.0" y="1.2246467991473532e-16" z="0.0" /> <pos subgrid="0" x="-0.8090169943749475" y="-0.587785252292473" z="0.0" /> <pos subgrid="0" x="-0.30901699437494756" y="-0.9510565162951535" z="0.0" /> <pos subgrid="0" x="0.30901699437494723" y="-0.9510565162951536" z="0.0" /> <pos subgrid="0" x="0.8090169943749473" y="-0.5877852522924732" z="0.0" /> <pos subgrid="1" x="-1.0" y="0.0" z="1.0" /> <pos subgrid="1" x="-1.0" y="0.0" z="1.0" /> <pos subgrid="1" x="-1.0" y="0.0" z="1.0" /> <pos subgrid="1" x="-1.0" y="-1.0" z="-1.0" /> <pos subgrid="1" x="0.0" y="0.0" z="0.0" /> <pos subgrid="1" x="1.0" y="1.0" z="1.0" /> <pos subgrid="1" x="1.0" y="1.0" z="1.0" /> <pos subgrid="1" x="1.0" y="1.0" z="1.0" /> <pos subgrid="1" x="1.0" y="1.0" z="1.0" /> </root>
Importing the
two_grids.xml
file:>>> import acoular as ac >>> >>> grids = ac.ImportGrid(file='two_grids.xml') >>> grids.size 19 >>> grids.subgrids array(['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'], dtype='<U1') >>> grids.gpos.T array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 8.09016994e-01, 5.87785252e-01, 0.00000000e+00], [ 3.09016994e-01, 9.51056516e-01, 0.00000000e+00], [-3.09016994e-01, 9.51056516e-01, 0.00000000e+00], [-8.09016994e-01, 5.87785252e-01, 0.00000000e+00], [-1.00000000e+00, 1.22464680e-16, 0.00000000e+00], [-8.09016994e-01, -5.87785252e-01, 0.00000000e+00], [-3.09016994e-01, -9.51056516e-01, 0.00000000e+00], [ 3.09016994e-01, -9.51056516e-01, 0.00000000e+00], [ 8.09016994e-01, -5.87785252e-01, 0.00000000e+00], [-1.00000000e+00, 0.00000000e+00, 1.00000000e+00], [-1.00000000e+00, 0.00000000e+00, 1.00000000e+00], [-1.00000000e+00, 0.00000000e+00, 1.00000000e+00], [-1.00000000e+00, -1.00000000e+00, -1.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00], [ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00], [ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00], [ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00]])
Consider two XML files,
grid1.xml
andgrid2.xml
containing different grids.>>> import acoular as ac >>> >>> grid = ac.ImportGrid(file='grid1.xml') >>> grid.size 8 >>> grid.file = 'grid2.xml' >>> grid.size 12