Updating of outputfiles_merge to allow it to be imported as a function.

这个提交包含在:
Craig Warren
2018-01-17 17:17:57 +01:00
父节点 afd30b27ab
当前提交 c137bef5c3
共有 4 个文件被更改,包括 93 次插入91 次删除

查看文件

@@ -155,7 +155,7 @@ You should have produced 60 output files, one for each A-scan, with names ``cyli
python -m tools.outputfiles_merge user_models/cylinder_Bscan_2D python -m tools.outputfiles_merge user_models/cylinder_Bscan_2D
You should see a combined output file ``cylinder_Bscan_2D_merged.out``. The tool will ask you if you want to delete the original single A-scan output files or keep them. You should see a combined output file ``cylinder_Bscan_2D_merged.out``. You can add the optional argument ``--remove-files`` if you want to automatically delete the original single A-scan output files.
You can now view an image of the B-scan using the command: You can now view an image of the B-scan using the command:
@@ -171,9 +171,3 @@ You can now view an image of the B-scan using the command:
:width: 600px :width: 600px
B-scan of model of a metal cylinder buried in a dielectric half-space. B-scan of model of a metal cylinder buried in a dielectric half-space.

查看文件

@@ -21,15 +21,16 @@ where ``inputfile`` is the name of input file including the path.
outputfiles_merge.py outputfiles_merge.py
-------------------- --------------------
gprMax produces a separate output file for each trace (A-scan) in a B-scan. This module combines the separate output files into a single file, and offers to remove the separate output files afterwards. Usage (from the top-level gprMax directory) is: gprMax produces a separate output file for each trace (A-scan) in a B-scan. This module combines the separate output files into a single file, and can remove the separate output files afterwards. Usage (from the top-level gprMax directory) is:
.. code-block:: none .. code-block:: none
python -m tools.outputfiles_merge basefilename python -m tools.outputfiles_merge basefilename --remove-files
where: where:
* ``basefilename`` is the base name file of the output file series, e.g. for ``myoutput1.out``, ``myoutput2.out`` the base file name would be ``myoutput`` * ``basefilename`` is the base name file of the output file series, e.g. for ``myoutput1.out``, ``myoutput2.out`` the base file name would be ``myoutput``
* ``remove-files`` is an optional argument (flag) that when given will remove the separate output files after the merge.
convert_png2h5.py convert_png2h5.py

查看文件

@@ -25,25 +25,61 @@ import numpy as np
from gprMax._version import __version__ from gprMax._version import __version__
"""Merges traces (A-scans) from multiple output files into one new file, then removes the series of output files."""
# Parse command line arguments def get_output_data(filename, rxnumber, rxcomponent):
parser = argparse.ArgumentParser(description='Merges traces (A-scans) from multiple output files into one new file, then removes the series of output files.', usage='cd gprMax; python -m tools.outputfiles_merge basefilename') """Gets B-scan output data from a model.
parser.add_argument('basefilename', help='base name of output file series including path')
args = parser.parse_args()
basefilename = args.basefilename Args:
outputfile = basefilename + '_merged.out' filename (string): Filename (including path) of output file.
files = glob.glob(basefilename + '*.out') rxnumber (int): Receiver output number.
outputfiles = [filename for filename in files if '_merged' not in filename] rxcomponent (str): Receiver output field/current component.
modelruns = len(outputfiles)
print('Found {} files to merge'.format(modelruns))
# Combined output file Returns:
fout = h5py.File(outputfile, 'w') outputdata (array): Array of A-scans, i.e. B-scan data.
dt (float): Temporal resolution of the model.
"""
# Add positional data for rxs # Open output file and read some attributes
for model in range(modelruns): f = h5py.File(filename, 'r')
nrx = f.attrs['nrx']
dt = f.attrs['dt']
# Check there are any receivers
if nrx == 0:
raise CmdInputError('No receivers found in {}'.format(filename))
path = '/rxs/rx' + str(rxnumber) + '/'
availableoutputs = list(f[path].keys())
# Check if requested output is in file
if rxcomponent not in availableoutputs:
raise CmdInputError('{} output requested to plot, but the available output for receiver 1 is {}'.format(rxcomponent, ', '.join(availableoutputs)))
outputdata = f[path + '/' + rxcomponent]
outputdata = np.array(outputdata)
return outputdata, dt
def merge_files(basefilename, removefiles=False):
"""Merges traces (A-scans) from multiple output files into one new file,
then optionally removes the series of output files.
Args:
basefilename (string): Base name of output file series including path.
outputs (boolean): Flag to remove individual output files after merge.
"""
outputfile = basefilename + '_merged.out'
files = glob.glob(basefilename + '*.out')
outputfiles = [filename for filename in files if '_merged' not in filename]
modelruns = len(outputfiles)
# Combined output file
fout = h5py.File(outputfile, 'w')
# Add positional data for rxs
for model in range(modelruns):
fin = h5py.File(basefilename + str(model + 1) + '.out', 'r') fin = h5py.File(basefilename + str(model + 1) + '.out', 'r')
nrx = fin.attrs['nrx'] nrx = fin.attrs['nrx']
@@ -71,10 +107,19 @@ for model in range(modelruns):
fin.close() fin.close()
fout.close() fout.close()
check = input('Do you want to remove the multiple individual output files? [y] or n:') if removefiles:
if not check or check == 'y':
for model in range(modelruns): for model in range(modelruns):
file = basefilename + str(model + 1) + '.out' file = basefilename + str(model + 1) + '.out'
os.remove(file) os.remove(file)
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(description='Merges traces (A-scans) from multiple output files into one new file, then optionally removes the series of output files.', usage='cd gprMax; python -m tools.outputfiles_merge basefilename')
parser.add_argument('basefilename', help='base name of output file series including path')
parser.add_argument('--remove-files', action='store_true', default=False, help='flag to remove individual output files after merge')
args = parser.parse_args()
merge_files(args.basefilename, removefiles=args.remove_files)

查看文件

@@ -25,45 +25,7 @@ import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from gprMax.exceptions import CmdInputError from gprMax.exceptions import CmdInputError
from .outputdata import get_output_data
def get_output_data(filename, rxnumber, rxcomponent):
"""Gets B-scan output data from a model.
Args:
filename (string): Filename (including path) of output file.
rxnumber (int): Receiver output number.
rxcomponent (str): Receiver output field/current component.
Returns:
outputdata (array): Array of A-scans, i.e. B-scan data.
dt (float): Temporal resolution of the model.
"""
# Open output file and read some attributes
f = h5py.File(filename, 'r')
nrx = f.attrs['nrx']
dt = f.attrs['dt']
# Check there are any receivers
if nrx == 0:
raise CmdInputError('No receivers found in {}'.format(filename))
path = '/rxs/rx' + str(rxnumber) + '/'
availableoutputs = list(f[path].keys())
# Check if requested output is in file
if rxcomponent not in availableoutputs:
raise CmdInputError('{} output requested to plot, but the available output for receiver 1 is {}'.format(rxcomponent, ', '.join(availableoutputs)))
outputdata = f[path + '/' + rxcomponent]
outputdata = np.array(outputdata)
# Check that there is more than one A-scan present
if outputdata.shape[1] == 1:
raise CmdInputError('{} contains only a single A-scan.'.format(filename))
return outputdata, dt
def mpl_plot(filename, outputdata, dt, rxnumber, rxcomponent): def mpl_plot(filename, outputdata, dt, rxnumber, rxcomponent):