<commit_before>#! /usr/bin/env python
"""
Lauches ParaView visualization.

User provides ParaView state file and output directory.

<NAME> 2015-12-02
"""
import argparse
import glob
import os
import subprocess
import tempfile

TMP_DIR = tempfile.gettempdir()


def generate_pvd_file(outdir, fieldname, timesteps, usepvtu=False):
    """
    Generates ParaView PVD XML file fieldName.pvd that contains vtu or ptvu files for
    the given time steps range.
    """
    template_header = """<?xml version="1.0"?>\n"""
    template_openblock = """<VTKFile byte_order="LittleEndian" version="0.1" type="Collection">\n<Collection>\n"""
    template_closeblock = """</Collection>\n</VTKFile>\n"""
    template_entry = """<DataSet timestep="{i}" part="0" group="" file="{name}_{i}.{ext}"/>\n"""
    extension = 'pvtu' if usepvtu else 'vtu'

    content = template_header
    content += template_openblock
    for i in timesteps:
        content += template_entry.format(i=i, name=fieldname, ext=extension)
    content += template_closeblock

    filename = os.path.join(outdir, fieldname+'.pvd')
    print 'generating', filename
    f = open(filename, 'w')
    f.write(content)
    f.close()


def process_args(outputdir, state_file, regenerate_pvd=True, timesteps=None, parallel_vtu=True):
    default_out_dir = 'outputs'
    temp_state_file = os.path.join(TMP_DIR, 'tmp.pvsm')
    paraview_bin = 'paraview'
    pv_log_file = os.path.join(TMP_DIR, 'log_pvoutput.txt')
    static_pvd_files = ['bath']  # outputs that are not time dependent
    # regenerate all existing PVD files
    if regenerate_pvd:
        pvd_files = glob.glob(os.path.join(outputdir, '*.pvd'))
        for f in pvd_files:
            path, fname = os.path.split(f)
            fieldName, extension = os.path.splitext(fname)
            if fieldName not in static_pvd_files:
                generate_pvd_file(outputdir, fieldName, timesteps, usepvtu=parallel_vtu)
    # read state file, replace directory with new one
    new_content = ''
    with open(state_file, 'r') as f:
        content = f.read()
        new_content = content.replace(default_out_dir, outputdir)
    with open(temp_state_file, 'w') as f:
        f.write(new_content)
    # lauch paraview with new independent thread
    log_file = open(pv_log_file, 'w')
    cmd = ' '.join([paraview_bin, '--state={:}'.format(temp_state_file), '>', pv_log_file])
    subprocess.Popen(cmd, shell=True, stdout=log_file, stderr=subprocess.STDOUT)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Launch ParaView visualization',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('outputdir', type=str,
                        help='Directory where .pvd files are stored')
    parser.add_argument('statefile', type=str,
                        help='ParaView *.pvsm state file')
    parser.add_argument('-r', action='store_true', dest='regenerate_pvd',
                        help='regenerate PVD files')
    parser.add_argument('-p', action='store_true', dest='parallel_vtu',
                        help='regenerate PVD files for parallel outputs')
    parser.add_argument('-f', '--first-time-step', type=int, default=0,
                        help='last time step for regenerated PVD file')<commit_msg>smarter path replacement in visualize-output with xml parser<commit_after>#! /usr/bin/env python
"""
Lauches ParaView visualization.

User provides ParaView state file and output directory.

<NAME> 2015-12-02
"""
import argparse
import glob
import os
import subprocess
import tempfile

TMP_DIR = tempfile.gettempdir()


def generate_pvd_file(outdir, fieldname, timesteps, usepvtu=False):
    """
    Generates ParaView PVD XML file fieldName.pvd that contains vtu or ptvu files for
    the given time steps range.
    """
    template_header = """<?xml version="1.0"?>\n"""
    template_openblock = """<VTKFile byte_order="LittleEndian" version="0.1" type="Collection">\n<Collection>\n"""
    template_closeblock = """</Collection>\n</VTKFile>\n"""
    template_entry = """<DataSet timestep="{i}" part="0" group="" file="{name}_{i}.{ext}"/>\n"""
    extension = 'pvtu' if usepvtu else 'vtu'

    content = template_header
    content += template_openblock
    for i in timesteps:
        content += template_entry.format(i=i, name=fieldname, ext=extension)
    content += template_closeblock

    filename = os.path.join(outdir, fieldname+'.pvd')
    print 'generating', filename
    f = open(filename, 'w')
    f.write(content)
    f.close()


def replace_path_in_xml(filename, outputfile, new_path):
    """
    Replaces all paths in paraview xml file PVDReader entries.

    All PVDReader entries of the form
    <Proxy group="sources" type="PVDReader" ...>
      <Property name="FileName" ...>
        <Element value="some/path/to/a_file.pvd" .../>
        ...
      </Property>
      ...
    </Proxy>

    will be reaplaced by
    <Proxy group="sources" type="PVDReader" ...>
      <Property name="FileName" ...>
        <Element value="new_path/a_file.pvd" .../>
        ...
      </Property>
      ...
    </Proxy>

    """
    import xml.etree.ElementTree as ET
    tree = ET.parse(filename)
    root = tree.getroot()
    readers = root[0].findall("Proxy[@type='PVDReader']")
    for reader in readers:
        fnameprop = reader.findall("Property[@name='FileName']/Element")[0]
        old_fname = fnameprop.attrib['value']
        path, file = os.path.split(old_fname)
        new_fname = os.path.join(new_path, file)
        fnameprop.attrib['value'] = new_fname
    tree.write(outputfile)


def process_args(outputdir, state_file, regenerate_pvd=True, timesteps=None, parallel_vtu=True):
    temp_state_file = os.path.join(TMP_DIR, 'tmp.pvsm')
    paraview_bin = 'paraview'
    pv_log_file = os.path.join(TMP_DIR, 'log_pvoutput.txt')
    static_pvd_files = ['bath']  # outputs that are not time dependent
    # regenerate all existing PVD files
    if regenerate_pvd:
        pvd_files = glob.glob(os.path.join(outputdir, '*.pvd'))
        for f in pvd_files:
            path, fname = os.path.split(f)
            fieldName, extension = os.path.splitext(fname)
            if fieldName not in static_pvd_files:
                generate_pvd_file(outputdir, fieldName, timesteps, usepvtu=parallel_vtu)
    # read state file, replace directory with new one
    replace_path_in_xml(state_file, temp_state_file, outputdir)
    # lauch paraview with new independent thread
    log_file = open(pv_log_file, 'w')
    cmd = ' '.join([paraview_bin, '--state={:}'.format(temp_state_file), '>', pv_log_file])
    subprocess.Popen(cmd, shell=True, stdout=log_file, stderr=subprocess.STDOUT)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Launch ParaView visualization',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('outputdir', type=str,
                        help='Directory where .pvd files are stored')
    parser.add_argument('statefile', type=str,
                        help='ParaView *.pvsm state file')
    parser.add_argument('-r', action='store_true', dest='regenerate_pvd',
                        help='regenerate PVD files')
    parser.add_argument('-p', action='store_true', dest='parallel_vtu',
                        help='regenerate PVD files for parallel outputs')
    parser.add_argument('-f', '--first-time-step', type=int, default=0,
                        help='last time step for regenerated PVD file')
{'fraction_non_alphanumeric': 0.12816099563087516, 'fraction_numerical': 0.003971931682775056, 'mean_word_length': 3.180409518539015, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 2, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7431990', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2383, 'n_words': 540}