diff --git a/toolboxes/Utilities/inputfile_old2new.py b/toolboxes/Utilities/inputfile_old2new.py deleted file mode 100644 index 761a49d3..00000000 --- a/toolboxes/Utilities/inputfile_old2new.py +++ /dev/null @@ -1,429 +0,0 @@ -# Copyright (C) 2015-2022: The University of Edinburgh, United Kingdom -# Authors: Craig Warren, Antonis Giannopoulos, and John Hartley -# -# This file is part of gprMax. -# -# gprMax is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# gprMax is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with gprMax. If not, see . - -import argparse -import logging - -logger = logging.getLogger(__name__) - - -"""Converts old to new style input files.""" - -# Parse command line arguments -parser = argparse.ArgumentParser(description='Converts old style input file to new style input file.', usage='cd gprMax; python -m tools.inputfile_new2old inputfile') -parser.add_argument('inputfile', help='name of input file including path') -args = parser.parse_args() - -inputfile = args.inputfile - -with open(inputfile, 'r') as f: - # Strip out any newline characters and comments that must begin with double hashes - inputlines = [line.rstrip() for line in f] - -# New file name base -try: - newfile = inputfile.split('.')[0] -except: - newfile = inputfile -newfile += '_v3syntax' - -logger.info("Attempting to convert inputfile '{}' to use new syntax...\n".format(inputfile)) - -model2D = False -txs = [] -badwaveforms = ['gaussiandot', 'gaussiandotdot'] -linesources = [] -voltagesources = [] -hertziandipoles = [] -transmissionlines = [] - -lindex = 0 -while(lindex < len(inputlines)): - - if inputlines[lindex].startswith('#') and not inputlines[lindex].startswith('##'): - cmd = inputlines[lindex].split(':') - cmdname = cmd[0].lower() - params = cmd[1].split() - - if cmdname == '#dx_dy': - model2D = True - # Syntax of old command: #dx_dy: x y - replacement = '#dx_dy_dz: {:g} {:g} {:g}'.format(float(params[0]), float(params[1]), float(params[1])) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - dx_dy_dz = (float(params[0]), float(params[1]), float(params[1])) - - elif cmdname == '#dx_dy_dz': - dx_dy_dz = (float(params[0]), float(params[1]), float(params[2])) - lindex += 1 - - else: - lindex += 1 - - else: - lindex += 1 - -lindex = 0 -while(lindex < len(inputlines)): - - if inputlines[lindex].startswith('#') and not inputlines[lindex].startswith('##'): - cmd = inputlines[lindex].split(':') - cmdname = cmd[0].lower() - params = cmd[1].split() - - if cmdname == '#domain': - if model2D: - # Syntax of old command: #domain: x y - replacement = '#domain: {:g} {:g} {:g}'.format(float(params[0]), float(params[1]), dx_dy_dz[2]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - domain = (float(params[0]), float(params[1]), dx_dy_dz[2]) - else: - domain = (float(params[0]), float(params[1]), float(params[2])) - - lindex += 1 - - elif cmdname == '#time_window': - params = params[0].lower() - if '.' in params or 'e' in params: - timewindow = float(params) - else: - timewindow = int(params) - lindex += 1 - - elif cmdname == '#num_of_procs': - # Syntax of old command: #num_of_procs: nthreads - replacement = '#num_threads: {}'.format(params[0]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#tx': - txs.append(inputlines[lindex]) - lindex += 1 - - elif cmdname == '#line_source': - linesources.append(inputlines[lindex]) - lindex += 1 - - elif cmdname == '#voltage_source': - voltagesources.append(inputlines[lindex]) - lindex += 1 - - elif cmdname == '#hertzian_dipole': - hertziandipoles.append(inputlines[lindex]) - lindex += 1 - - elif cmdname == '#transmission_line': - transmissionlines.append(inputlines[lindex]) - lindex += 1 - - elif cmdname == '#rx': - if model2D: - # Syntax of old command: #rx: x1 y1 - replacement = '#rx: {} {} {}'.format(params[0], params[1], 0) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#rx_box': - if model2D: - # Syntax of old command: #rx_box: x1 y1 x2 y2 dx dy - replacement = '#rx_array: {} {} {} {} {} {} {} {} {}'.format(params[0], params[1], 0, params[2], params[3], dx_dy_dz[2], params[4], params[5], dx_dy_dz[2]) - else: - # Syntax of old command: #rx_box: x1 y1 z1 x2 y2 z2 dx dy dz - replacement = '#rx_array: {} {} {} {} {} {} {} {} {}'.format(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#tx_steps': - if model2D: - # Syntax of old command: #tx_steps: dx dy - replacement = '#src_steps: {} {} {}'.format(params[0], params[1], 0) - else: - # Syntax of old command: #tx_steps: dx dy dz - replacement = '#src_steps: {} {} {}'.format(params[0], params[1], params[2]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#rx_steps': - if model2D: - # Syntax of old command: #rx_steps: dx dy - replacement = '#rx_steps: {} {} {}'.format(params[0], params[1], 0) - else: - # Syntax of old command: #rx_steps: dx dy dz - replacement = '#rx_steps: {} {} {}'.format(params[0], params[1], params[2]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#medium': - # Syntax of old command: #medium: e_rs e_inf tau sig_e mu_r sig_m ID - replacement = '#material: {} {} {} {} {}'.format(params[0], params[3], params[4], params[5], params[6]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - if float(params[1]) > 0: - replacement = '#add_dispersion_debye: 1 {} {} {}'.format(float(params[0]) - float(params[1]), params[2], params[6]) - logger.info("Command '{}' added.".format(replacement)) - inputlines.insert(lindex + 1, replacement) - lindex += 1 - - elif cmdname == '#box': - if model2D: - # Syntax of old command: #box: x1 y1 x2 y2 ID - replacement = '#box: {} {} {} {} {} {} {}'.format(params[0], params[1], 0, params[2], params[3], dx_dy_dz[2], params[4]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#triangle': - if model2D: - # Syntax of old command: #triangle: x1 y1 x2 y2 x3 y3 ID - replacement = '#triangle: {} {} {} {} {} {} {} {} {} {} {}'.format(params[0], params[1], 0, params[2], params[3], 0, params[4], params[5], 0, dx_dy_dz[2], params[6]) - else: - # Syntax of old command: #triangle: x1 y1 z1 x2 y2 z2 x3 y3 z3 ID - replacement = '#triangle: {} {} {} {} {} {} {} {} {} {} {}'.format(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], 0, params[9]) - - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#wedge': - # Syntax of old command: #wedge: x1 y1 z1 x2 y2 z2 x3 y3 z3 thickness ID - replacement = '#triangle: {} {} {} {} {} {} {} {} {} {} {}'.format(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#bowtie': - logger.info("Command '{}', is no longer supported. You can create the bowtie shape using two triangle commands.".format(inputlines[lindex])) - inputlines.pop(lindex) - - elif cmdname == '#cylinder': - if model2D: - # Syntax of old command: #cylinder: x y radius ID - replacement = '#cylinder: {} {} {} {} {} {} {} {}'.format(params[0], params[1], 0, params[0], params[1], dx_dy_dz[2], params[2], params[3]) - else: - # Syntax of old command: #cylinder: axis axis_start axis_stop f1 f2 radius ID - if params[0] == 'x': - replacement = '#cylinder: {} {} {} {} {} {} {} {}'.format(params[1], params[3], params[4], params[2], params[3], params[4], params[5], params[6]) - elif params[0] == 'y': - replacement = '#cylinder: {} {} {} {} {} {} {} {}'.format(params[3], params[1], params[4], params[3], params[2], params[4], params[5], params[6]) - elif params[0] == 'z': - replacement = '#cylinder: {} {} {} {} {} {} {} {}'.format(params[3], params[4], params[1], params[3], params[4], params[2], params[5], params[6]) - - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#cylinder_new': - # Syntax of old command: #cylinder_new: x1 y1 z1 x2 y2 z2 radius ID - replacement = '#cylinder: {} {} {} {} {} {} {} {}'.format(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#cylindrical_segment': - logger.info("Command '{}' has been removed as it is no longer supported. You can create a cylindrical segment by using a #box to cut through a #cylinder.".format(inputlines[lindex])) - inputlines.pop(lindex) - - elif cmdname in ['#x_segment', '#y_segment']: - logger.info("Command '{}' has been removed. A circular segment can be created by using the #cylinder command and cutting it with a #box. Alternatively the #cylindrical_sector command maybe useful.".format(inputlines[lindex])) - inputlines.pop(lindex) - - elif cmdname == '#media_file': - logger.info("Command '{}' has is no longer supported. Please include your materials using the #material command directly in the input file.".format(inputlines[lindex])) - inputlines.pop(lindex) - - elif cmdname == '#pml_layers': - # Syntax of old command: #pml_layers: num_layers - replacement = '#pml_cells: {}'.format(params[0]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname in ['#abc_order', '#abc_type', 'abc_optimisation_angles', '#abc_mixing_parameters', '#abc_stability_factors']: - logger.info("Command '{}' has been removed as Higdon Absorbing Boundary Conditions (ABC) are no longer supported. The default ABC is the (better performing) Perfectly Matched Layer (PML).".format(inputlines[lindex])) - inputlines.pop(lindex) - - elif cmdname == '#analysis': - # Syntax of old command: #analysis: num_model_runs outputfile outputfiletype - if int(params[0]) > 1: - extra = " To run a model multiple times use the command line option -n, e.g. gprMax {} -n {}".format(inputfile, int(params[0])) - else: - extra = '' - logger.info("Command '{}' has been removed as it is no longer required.{}".format(inputlines[lindex], extra)) - inputlines.pop(lindex) - - elif cmdname in ['#end_analysis', '#number_of_media', '#nips_number']: - logger.info("Command '{}' has been removed as it is no longer required.".format(inputlines[lindex])) - inputlines.pop(lindex) - - elif cmdname == '#snapshot': - if model2D: - # Syntax of old command: #snapshot: i1 x1 y1 x2 y2 dx dy time filename type - replacement = '#snapshot: {} {} {} {} {} {} {} {} {} {} {}'.format(params[1], params[2], 0, params[3], params[4], dx_dy_dz[2], params[5], params[6], dx_dy_dz[2], params[7], params[8]) - else: - # Syntax of old command: #snapshot: i1 x1 y1 z1 x2 y2 z2 dx dy dz time filename type - replacement = '#snapshot: {} {} {} {} {} {} {} {} {} {} {}'.format(params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10], params[11]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#geometry_file': - # Syntax of old command: #geometry_file: filename - if params[0].endswith('.geo'): - params = params[0].split('.') - replacement = '#geometry_view: 0 0 0 {} {} {} {} {} {} {} n'.format(domain[0], domain[1], domain[2], dx_dy_dz[0], dx_dy_dz[1], dx_dy_dz[2], params[0]) - logger.info("Command '{}', replaced with '{}'. This is a geometry view of the entire domain, sampled at the spatial resolution of the model, using the per Yee cell option (n). You may want to consider taking a smaller geometry view or using a coarser sampling. You may also want to use the per Yee cell edge option (f) to view finer details.".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname == '#geometry_vtk': - # Syntax of old command: #geometry_vtk: x1 y1 z1 x2 y2 z2 dx dy dz filename type - replacement = '#geometry_view: {} {} {} {} {} {} {} {} {} {} {}'.format(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]) - logger.info("Command '{}', replaced with '{}'".format(inputlines[lindex], replacement)) - inputlines.pop(lindex) - inputlines.insert(lindex, replacement) - lindex += 1 - - elif cmdname in ['#plane_wave', '#thin_wire', '#huygens_surface']: - logger.exception("Command '{}' has not yet implemented in the new version of gprMax. For now please continue to use the old version.".format(inputlines[lindex])) - raise ValueError - - else: - lindex += 1 - - else: - lindex += 1 - -# Convert separate #line_source and associated #tx to #waveform and #hertzian_dipole -for source in linesources: - params = source.split() - if params[3] is badwaveforms: - logger.exception("Waveform types {} are not compatible between new and old versions of gprMax.".format(''.join(badwaveforms))) - raise ValueError - elif params[3] == 'ricker': - params[3] = 'gaussiandotnorm' - waveform = '#waveform: {} {} {} {}'.format(params[3], params[1], params[2], params[4]) - tx = next(tx for tx in txs if tx.split()[3] == params[4]) - hertziantx = tx.split() - if float(hertziantx[4]) > 0 or float(hertziantx[5]) != timewindow: - hertzian = '#hertzian_dipole: z {} {} {} {} {} {}'.format(hertziantx[1], hertziantx[2], 0, hertziantx[3], hertziantx[4], hertziantx[5]) - else: - hertzian = '#hertzian_dipole: z {} {} {} {}'.format(hertziantx[1], hertziantx[2], 0, hertziantx[3]) - - logger.info("Commands '{}' and '{}', replaced with '{}' and '{}'".format(source, tx, waveform, hertzian)) - inputlines.remove(source) - inputlines.remove(tx) - inputlines.append(waveform) - inputlines.append(hertzian) - -# Convert separate #hertzian_dipole and associated #tx to #waveform and #hertzian_dipole -for source in hertziandipoles: - params = source.split() - if params[3] is badwaveforms: - logger.exception("Waveform types {} are not compatible between new and old versions of gprMax.".format(''.join(badwaveforms))) - raise ValueError - elif params[3] == 'ricker': - params[3] = 'gaussiandotnorm' - waveform = '#waveform: {} {} {} {}'.format(params[3], params[1], params[2], params[4]) - tx = next(tx for tx in txs if tx.split()[5] == params[4]) - hertziantx = tx.split() - if float(hertziantx[6]) > 0 or float(hertziantx[7]) != timewindow: - hertzian = '#hertzian_dipole: {} {} {} {} {} {} {}'.format(hertziantx[1], hertziantx[2], hertziantx[3], hertziantx[4], hertziantx[5], hertziantx[6], hertziantx[7]) - else: - hertzian = '#hertzian_dipole: {} {} {} {} {}'.format(hertziantx[1], hertziantx[2], hertziantx[3], hertziantx[4], hertziantx[5]) - - logger.info("Commands '{}' and '{}', replaced with '{}' and '{}'".format(source, tx, waveform, hertzian)) - inputlines.remove(source) - inputlines.remove(tx) - inputlines.append(waveform) - inputlines.append(hertzian) - -# Convert separate #voltage_source and associated #tx to #waveform and #voltage_source -for source in voltagesources: - params = source.split() - if params[3] is badwaveforms: - logger.exception("Waveform types {} are not compatible between new and old versions of gprMax.".format(''.join(badwaveforms))) - raise ValueError - elif params[3] == 'ricker': - params[3] = 'gaussiandotnorm' - waveform = '#waveform: {} {} {} {}'.format(params[3], params[1], params[2], params[5]) - tx = next(tx for tx in txs if tx.split()[5] == params[5]) - voltagesourcetx = tx.split() - if float(voltagesourcetx[6]) > 0 or float(voltagesourcetx[7]) != timewindow: - voltagesource = '#voltage_source: {} {} {} {} {} {} {} {}'.format(voltagesourcetx[1], voltagesourcetx[2], voltagesourcetx[3], voltagesourcetx[4], params[4], voltagesourcetx[5], voltagesourcetx[6], voltagesourcetx[7]) - else: - voltagesource = '#voltage_source: {} {} {} {} {} {}'.format(voltagesourcetx[1], voltagesourcetx[2], voltagesourcetx[3], voltagesourcetx[4], params[4], voltagesourcetx[5]) - - logger.info("Commands '{}' and '{}', replaced with '{}' and '{}'".format(source, tx, waveform, voltagesource)) - inputlines.remove(source) - inputlines.remove(tx) - inputlines.append(waveform) - inputlines.append(voltagesource) - -# Convert separate #transmission_line and associated #tx to #waveform and #transmission_line -for source in transmissionlines: - params = source.split() - if params[3] is badwaveforms: - logger.exception("Waveform types {} are not compatible between new and old versions of gprMax.".format(''.join(badwaveforms))) - raise ValueError - elif params[3] == 'ricker': - params[3] = 'gaussiandotnorm' - waveform = '#waveform: {} {} {} {}'.format(params[3], params[1], params[2], params[6]) - tx = next(tx for tx in txs if tx.split()[5] == params[6]) - transmissionlinetx = tx.split() - if float(transmissionlinetx[6]) != 0 or float(transmissionlinetx[7]) < timewindow: - transmissionline = '#transmission_line: {} {} {} {} {} {} {} {}'.format(transmissionlinetx[1], transmissionlinetx[2], transmissionlinetx[3], transmissionlinetx[4], params[5], transmissionlinetx[5], transmissionlinetx[6], transmissionlinetx[7]) - else: - transmissionline = '#transmission_line: {} {} {} {} {} {}'.format(transmissionlinetx[1], transmissionlinetx[2], transmissionlinetx[3], transmissionlinetx[4], params[5], transmissionlinetx[5]) - - logger.info("Commands '{}' and '{}', replaced with '{}' and '{}'".format(source, tx, waveform, transmissionline)) - inputlines.remove(source) - inputlines.remove(tx) - inputlines.append(waveform) - inputlines.append(transmissionline) - -# Write new input file -newinputfile = newfile + '.in' - -with open(newinputfile, 'w') as f: - for line in inputlines: - f.write('{}\n'.format(line)) - -logger.info("\nWritten new input file: '{}'".format(newinputfile)) diff --git a/toolboxes/Utilities/outputfiles_merge.py b/toolboxes/Utilities/outputfiles_merge.py index 276cc338..25034b2a 100644 --- a/toolboxes/Utilities/outputfiles_merge.py +++ b/toolboxes/Utilities/outputfiles_merge.py @@ -34,13 +34,13 @@ 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. + filename: string of tilename (including path) of output file. + rxnumber: int of receiver output number. + rxcomponent: string of receiver output field/current component. Returns: - outputdata (array): Array of A-scans, i.e. B-scan data. - dt (float): Temporal resolution of the model. + outputdata: array of A-scans, i.e. B-scan data. + dt: float of temporal resolution of the model. """ # Open output file and read some attributes @@ -58,7 +58,9 @@ def get_output_data(filename, rxnumber, rxcomponent): # Check if requested output is in file if rxcomponent not in availableoutputs: - logger.exception(f"{rxcomponent} output requested to plot, but the available output for receiver 1 is {', '.join(availableoutputs)}") + logger.exception(f"{rxcomponent} output requested to plot, but the " + + f"available output for receiver 1 is " + + f"{', '.join(availableoutputs)}") raise ValueError outputdata = f[path + '/' + rxcomponent] @@ -72,8 +74,8 @@ def merge_files(outputfiles, removefiles=False): then optionally removes the series of output files. Args: - outputfiles (list): List of output files to be merged. - removefiles (boolean): Flag to remove individual output files after merge. + outputfiles: list of output files to be merged. + removefiles: boolean flag to remove individual output files after merge. """ merged_outputfile = os.path.commonprefix(outputfiles) + '_merged.h5' @@ -124,9 +126,13 @@ def merge_files(outputfiles, removefiles=False): 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 = 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') + parser.add_argument('--remove-files', action='store_true', default=False, + help='flag to remove individual output files after merge') args = parser.parse_args() files = glob.glob(args.basefilename + '*.h5')