<commit_before>from openquake.nrmllib.hazard.writers import SourceModelXMLWriter
from converters.shapefileconverter import ShapeFileConverter
import os


def convert(sourceFileName, targetFileName, sourceType,
            nameMappings={'A': 'aGRval', 'B': 'bGRval', 'ID': 'EMME_IDAS', 'NAME': 'CODE'}):
    deserializedShps = convertShapeFileToNrml(sourceFileName, targetFileName, nameMappings, sourceType)

    fileName, ext = os.path.splitext(targetFileName)
    sourceModels = list()
    for i in xrange(len(deserializedShps)):
        deserializedShp = deserializedShps[i][0]
        shapeFileName = fileName + '_' + str(i) + ext
        writer = SourceModelXMLWriter(shapeFileName)
        writer.serialize(deserializedShp)
        sourceModels.append((os.path.basename(shapeFileName), deserializedShps[i][1]))

    writeSourceModelTree(os.path.join(os.path.dirname(targetFileName), 'source_model_logic_tree.xml'), sourceModels)


def convertShapeFileToNrml(sourceFileName, targetFileName, nameMappings, sourceType):
    ind = sourceFileName.rindex('.')
    sourceFileName = sourceFileName[:ind]

    s = ShapeFileConverter(sourceFileName, targetFileName, nameMappings)
    return s.parse(sourceType)


def writeSourceModelTree(targetFilePath, sourceModels):
    template = '''<?xml version="1.0" encoding="UTF-8"?>
<nrml xmlns:gml="http://www.opengis.net/gml"
      xmlns="http://openquake.org/xmlns/nrml/0.4">
    <logicTree logicTreeID="lt1">
        <logicTreeBranchingLevel branchingLevelID="bl1">
            <logicTreeBranchSet uncertaintyType="sourceModel" branchSetID="bs1">
                [[branches]]
            </logicTreeBranchSet>
        </logicTreeBranchingLevel>
    </logicTree>
</nrml>
        '''

    branches = ''
    branchTemplate = '''
                <logicTreeBranch branchID="b[[index]]">
                    <uncertaintyModel>../../SourceModels/[[sourceModelName]]</uncertaintyModel>
                    <uncertaintyWeight>[[weight]]</uncertaintyWeight>
                </logicTreeBranch>
        '''

    for i in xrange(len(sourceModels)):
        branchTemplateStr = branchTemplate.replace('[[index]]', str(i+1))
        branchTemplateStr = branchTemplateStr.replace('[[sourceModelName]]', sourceModels[i][0])
        branchTemplateStr = branchTemplateStr.replace('[[weight]]', str(sourceModels[i][1]))
        branches = "\n".join([branches, branchTemplateStr])

    template = template.replace('[[branches]]', branches)
    f = open(targetFilePath, 'w')
    f.write(template)
    f.write('\n')

    f.flush()
    f.close()<commit_msg>using ElementTree in writeSourceModelTree from now on.<commit_after>from openquake.nrmllib.hazard.writers import SourceModelXMLWriter
from converters.shapefileconverter import ShapeFileConverter
from xml.etree import ElementTree
from common.xml_utils import write_xml

import os


def convert(sourceFileName, targetFileName, sourceType,
            nameMappings={'A': 'aGRval', 'B': 'bGRval', 'ID': 'EMME_IDAS', 'NAME': 'CODE'}):
    deserializedShps = convertShapeFileToNrml(sourceFileName, targetFileName, nameMappings, sourceType)

    fileName, ext = os.path.splitext(targetFileName)
    sourceModels = list()
    for i in xrange(len(deserializedShps)):
        deserializedShp = deserializedShps[i][0]
        shapeFileName = fileName + '_' + str(i) + ext
        writer = SourceModelXMLWriter(shapeFileName)
        writer.serialize(deserializedShp)
        sourceModels.append((os.path.basename(shapeFileName), deserializedShps[i][1]))

    writeSourceModelTree(os.path.join(os.path.dirname(targetFileName), 'source_model_logic_tree.xml'), sourceModels)


def convertShapeFileToNrml(sourceFileName, targetFileName, nameMappings, sourceType):
    ind = sourceFileName.rindex('.')
    sourceFileName = sourceFileName[:ind]

    s = ShapeFileConverter(sourceFileName, targetFileName, nameMappings)
    return s.parse(sourceType)


def writeSourceModelTree(targetFilePath, sourceModels):
    nrml_node = ElementTree.Element('nrml', {'xmlns': 'http://openquake.org/xmlns/nrml/0.4'})
    logic_tree_node = ElementTree.SubElement(nrml_node, 'logicTree', {'logicTreeID': 'lt1'})

    logic_tree_branching_level_node = ElementTree.SubElement(logic_tree_node, 'logicTreeBranchingLevel',
                                                    {'branchingLevelID': 'bl1'})
    logic_tree_branch_set_node = ElementTree.SubElement(logic_tree_branching_level_node, 'logicTreeBranchSet',
                                               {'uncertaintyType': 'sourceModel', 'branchSetID': 'bs1'})

    for i in xrange(len(sourceModels)):
        logic_tree_branch_node = ElementTree.SubElement(logic_tree_branch_set_node, 'logicTreeBranch',
                                                        {'branchID': 'b'+str(i+1)})

        uncertainty_mode_node = ElementTree.SubElement(logic_tree_branch_node, 'uncertaintyModel')
        uncertainty_mode_node.text = '../../SourceModels/' + sourceModels[i][0]
        uncertainty_weight_node = ElementTree.SubElement(logic_tree_branch_node, 'uncertaintyWeight')
        uncertainty_weight_node.text = str(sourceModels[i][1])

    tree = ElementTree.ElementTree(element=nrml_node)

    write_xml(tree, targetFilePath)
{'fraction_non_alphanumeric': 0.12145593869731801, 'fraction_numerical': 0.004406130268199234, 'mean_word_length': 3.9488151658767774, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 1, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '953561', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1545, 'n_words': 246}