<commit_before>from xml.etree import ElementTree
from math import log, exp

def convertHazardMapToAscii(sourceFile, targetFile):
    tree = ElementTree.parse(sourceFile)

    with open(targetFile, 'w') as f:
        nodes = tree.findall('./oq:hazardMap/oq:node', {'oq': 'http://openquake.org/xmlns/nrml/0.4'})

        f.write('Lon\tLat\tIML\n')

        for node in nodes:
            f.write(node.attrib['lon'] + '\t' + node.attrib['lat'] + '\t' + node.attrib['iml'] + '\n')

        f.flush()


def convertHazardCurveToAscii(sourceFile, targetFile, returnPeriods=[72, 101, 475, 975, 2475, 4975, 10000]):
    tree = ElementTree.parse(sourceFile)

    with open(targetFile, 'w') as f:
        xmlNamespaces = {'oq': 'http://openquake.org/xmlns/nrml/0.4', 'gml': 'http://www.opengis.net/gml'}

        imlNode = tree.find('./oq:hazardCurves/oq:IMLs', xmlNamespaces)
        imlVals = [float(iml) for iml in imlNode.text.split(' ')]

        nodes = tree.findall('./oq:hazardCurves/oq:hazardCurve', xmlNamespaces)

        header = 'Lon\tLat'
        for returnPeriod in returnPeriods:
            header += '\t'
            header += str(returnPeriod)

        header += '\n'

        f.write(header)

        for node in nodes:
            coordinates = node.find('gml:Point/gml:pos', xmlNamespaces).text.split(' ')
            row = coordinates[0] + '\t' + coordinates[1]

            poes = [float(poe) for poe in node.find('oq:poEs', xmlNamespaces).text.split(' ')]

            for returnPeriod in returnPeriods:
                rrp = 1.0 / returnPeriod
                res = 0

                if imlVals[0] >= rrp:
                    res = imlVals[0]
                elif imlVals[len(imlVals)-1] <= rrp:
                    res = imlVals[len(imlVals)-1]
                else:
                    imlInd = 0
                    for i in xrange(1, len(poes)):
                        if poes[i] > rrp > poes[i-1] or poes[i-1] > rrp > poes[i]:
                            imlInd = i
                            break
                    x1 = c_log(imlVals[imlInd-1])
                    x2 = c_log(imlVals[imlInd])
                    y1 = c_log(poes[imlInd-1])
                    y2 = c_log(poes[imlInd])
                    y = log(rrp)

                    res = exp(((y - y1) * (x2 - x1)) / (y2 - y1) + x1)

                row += '\t' + str(res)

            f.write(row + '\n')

        f.flush()

def c_log(val):
    if val == 0:
        return 0.0

    return log(val)

def convertUhSpectraToAscii(sourceFile, targetFile):
    tree = ElementTree.parse(sourceFile)
<commit_msg>interpolation function fixed and refactored. hazard curve function parameter changed from return period to poe.<commit_after>from xml.etree import ElementTree
from math import log, exp
from common.math_utils import find_interpolated_val


def convertHazardMapToAscii(sourceFile, targetFile):
    tree = ElementTree.parse(sourceFile)

    with open(targetFile, 'w') as f:
        nodes = tree.findall('./oq:hazardMap/oq:node', {'oq': 'http://openquake.org/xmlns/nrml/0.4'})

        f.write('Lon\tLat\tIML\n')

        for node in nodes:
            f.write(node.attrib['lon'] + '\t' + node.attrib['lat'] + '\t' + node.attrib['iml'] + '\n')

        f.flush()


def convertHazardCurveToAscii(sourceFile, targetFile, poes=[0.39, 0.1, 0.049, 0.02, 0.005]):
    tree = ElementTree.parse(sourceFile)

    with open(targetFile, 'w') as f:
        xmlNamespaces = {'oq': 'http://openquake.org/xmlns/nrml/0.4', 'gml': 'http://www.opengis.net/gml'}

        imlNode = tree.find('./oq:hazardCurves/oq:IMLs', xmlNamespaces)
        imlVals = [float(iml) for iml in imlNode.text.split(' ')]

        nodes = tree.findall('./oq:hazardCurves/oq:hazardCurve', xmlNamespaces)

        #TODO: must remove calculation from here!
        returnPeriods = [round((-50.0/log(1-poe))) for poe in poes]

        header = 'Lon\tLat'
        for returnPeriod in returnPeriods:
            header += '\t'
            header += str(returnPeriod)

        header += '\n'

        f.write(header)

        for node in nodes:
            coordinates = node.find('gml:Point/gml:pos', xmlNamespaces).text.split(' ')
            row = coordinates[0] + '\t' + coordinates[1]

            node_poes = [float(poe) for poe in node.find('oq:poEs', xmlNamespaces).text.split(' ')]

            for poe in poes:
                res = find_interpolated_val(imlVals, node_poes, poe)
                row += '\t' + str(res)

            f.write(row + '\n')

        f.flush()


def convertUhSpectraToAscii(sourceFile, targetFile):
    tree = ElementTree.parse(sourceFile)

{'fraction_non_alphanumeric': 0.14511587611002816, 'fraction_numerical': 0.017760450508988522, 'mean_word_length': 2.0684385382059802, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3505327', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1580, 'n_words': 323}