<commit_before>"""Collection of routines used to read and parse the ovf file.

Part of ovf2vtk.

<NAME>, <EMAIL>

"""

import sys

import struct

try:
    import numpy as Numeric
except ImportError:
    print "This program needs Numpy. Please download and install. \
(http://sourceforge.net/projects/numpy)."
    print "If you are using Numeric, you can use the \
older version 0.1.17 of ovf2vtk."
    raise ImportError("Couldn't import Numpy -- cannot proceed.")


__version__ = "$Revision: 1.6 $"

# keyword list from ovf-file:
keywords = ["Title:",
            "Desc: Applied field (T):",
            "Desc: Iteration:",
            "meshtype:", "meshunit:",
            "xbase:", "ybase:", "zbase:",
            "xstepsize:", "ystepsize:", "zstepsize:",
            "xnodes:", "ynodes:", "znodes:",
            "xmin:", "ymin:", "zmin:", "xmax:", "ymax:", "zmax:",
            "boundary:", "valueunit:", "valuemultiplier:",
            "ValueRangeMinMag:", "ValueRangeMaxMag:",
            "End: Head:", "Begin: Data:", "OOMMF:",
            "Segment count:", "Begin: Segme"]


def parse_for_keywords(keywords, line, dic={}):
    for x in keywords:
        if line[0:len(x)+2] == "# "+x:
            dic[x] = line[len(x)+2:]
            # remove end of line character if it is there
            if dic[x][-1] == "\n":
                dic[x] = dic[x][0:-1]
            # remove any leading white space
            while dic[x][0] == " ":
                dic[x] = dic[x][1:]
    return dic


def analyze(filename, verbose=0):
    f = open(filename, "rb")
    lines = []
    dic = {}

    while 1:
        line = f.readline()
        if not line:               # until eof
            break
        if line[0] == "#":
            dic = parse_for_keywords(keywords, line, dic)
            lines.append(line)
    if verbose:
        print "#Analysing", filename, ": Found", len(dic.keys()), "keywords"
    f.close()
    return dic


def what_data(filename, verbose=0):
    DATAKEYWORD = "# Begin: Data"

    """returns a dictionary:
    startbyte = integer (bytes from beginning of file)
    startline = integer (lines from beginning of file)
    type      = 'binary4','binary8','ascii'
    """
    f = open(filename, "rb")
    lines = 0
    bytes = 0
    ans = {}
    while 1:
        line = f.readline()
        bytes = bytes + len(line)
        lines = lines + 1
        if not line:               # until eof
            print "***Reached end of file before encountering data"
            print "   Cowardly stopping here"
            print "   Some debug info:"
            print "   Have read", lines, "lines and"
            print "            ", bytes, "bytes."
            sys.exit(1)

        if line[0:len(DATAKEYWORD)] == DATAKEYWORD:
            bits = line.split()
            if bits[3] == "Binary":
                ans["type"] = "binary" + bits[4]
            else:
                if bits[3] == "Text":
                    ans["type"] = "ascii"
                else:
                    print "Data file", filename, \
                          "appears neither to be a text or"\
                          "a binary file."
                    print "Cowardly stopping here."
                    sys.exit(1)
            break
    ans["startbyte"] = bytes
    ans["startline"] = lines

    f.close()

    if verbose == 1:
        print "Data in", filename, "start in line", lines, "at byte",\
              bytes, "and is", ans["type"]

    return ans


def read_structured_ascii_oommf_data(fname, byte, dimensions, verbose=0):

    """fname is the filemane to read
    byte is the byte in that file at which the data starts
    dimensions = 3-tuple (nx, ny, nz) with the numbers of cells in the mesh

    This function will return one vector for each mesh cell combined in a list
    (in the fashion OOMMF stores data, i.e. (from OOMMF userguide)
    'these are ordered with the x index incremented first, then the y
    index, and the z index last.  This is nominally Fortran order, and
    is adopted here because commonly x will be the longest dimension,
    and z the shortest'

    The returned value is a list of 3-tuples. """

    data = open(fname).read()

    datalines = str.split(data[byte:], '\n')

    vectorfield = []

    for datum in datalines:
        if datum[0:16] == "# End: Data Text":
            break
        if datum[0] == "#":
            print "I found a # in the first column. Complete row is", datum
            print "I only expected '# End: Data Text'."
            print "cowardly stopping here"
            raise Exception("FileFormatError, See above for more details")

        vector_str = str.split(datum[0:-1])

        vector = map(lambda a: float(a), vector_str)
        if len(vector) != 3:
            print "vector_str=", vector_str
            print "vector    =", vector
            print "datum =", datum
            raise Exception("Oops, vector_str shold have 3 entries")

        vectorfield.append(vector)

    # expected number of data:
    data_exp = dimensions[0]*dimensions[1]*dimensions[2]

    if (data_exp - len(vectorfield)):
        print "Hmm, expected nx*ny*ny =", data_exp, "items, but got",\
              len(vectorfield), "."
        print "Cowardly stopping here."
        raise Exception("FileFormatError, read too many/too few data")

    print "Hint: Reading ASCII-OOMMF file is slow (that could be changed) \
and the files are large. Why not save data as binary?"

    return Numeric.array(vectorfield)


def read_structured_binary_oommf_data(fname, byte, dimensions, datatype,
                                      verbose=0):
    """fname is the filename to read
    byte is the byte in that file at which the (binary) data starts
    datatype = 'binary4','binary8','ascii'
    dimensions = 3-tuple (nx, ny, nz) with the numbers of cells in the mesh

    This function will return one tuple for each mesh cell combined in a list
    (in the fashion OOMMF stores data, i.e. (from OOMMF userguide)
    'these are ordered with the x index incremented first, then the y
    index, and the z index last.  This is nominally Fortran order, and
    is adopted here because commonly x will be the longest dimension,
    and z the shortest'

    The returned value is a list of 3-tuples. """

    if datatype == "binary4":
        floatsize = 4
    elif datatype == "binary8":
        floatsize = 8
    elif datatype == "ascii":
        print "ascii -oommf data not supported here"
        raise "Not Implemented Error", "ascii-oommf data not supported here"
    else:
        print "unknow datatype (expected  'binary4','binary8' [or 'ascii'],\
 but got ", datatype
        raise "Error"<commit_msg>changed method of exception raising in new_omfread.<commit_after>"""Collection of routines used to read and parse the ovf file.

Part of ovf2vtk.

<NAME>, <EMAIL>

"""

import sys

import struct

try:
    import numpy as Numeric
except ImportError:
    print "This program needs Numpy. Please download and install. \
(http://sourceforge.net/projects/numpy)."
    print "If you are using Numeric, you can use the \
older version 0.1.17 of ovf2vtk."
    raise ImportError("Couldn't import Numpy -- cannot proceed.")


__version__ = "$Revision: 1.6 $"

# keyword list from ovf-file:
keywords = ["Title:",
            "Desc: Applied field (T):",
            "Desc: Iteration:",
            "meshtype:", "meshunit:",
            "xbase:", "ybase:", "zbase:",
            "xstepsize:", "ystepsize:", "zstepsize:",
            "xnodes:", "ynodes:", "znodes:",
            "xmin:", "ymin:", "zmin:", "xmax:", "ymax:", "zmax:",
            "boundary:", "valueunit:", "valuemultiplier:",
            "ValueRangeMinMag:", "ValueRangeMaxMag:",
            "End: Head:", "Begin: Data:", "OOMMF:",
            "Segment count:", "Begin: Segme"]


def parse_for_keywords(keywords, line, dic={}):
    for x in keywords:
        if line[0:len(x)+2] == "# "+x:
            dic[x] = line[len(x)+2:]
            # remove end of line character if it is there
            if dic[x][-1] == "\n":
                dic[x] = dic[x][0:-1]
            # remove any leading white space
            while dic[x][0] == " ":
                dic[x] = dic[x][1:]
    return dic


def analyze(filename, verbose=0):
    f = open(filename, "rb")
    lines = []
    dic = {}

    while 1:
        line = f.readline()
        if not line:               # until eof
            break
        if line[0] == "#":
            dic = parse_for_keywords(keywords, line, dic)
            lines.append(line)
    if verbose:
        print "#Analysing", filename, ": Found", len(dic.keys()), "keywords"
    f.close()
    return dic


def what_data(filename, verbose=0):
    DATAKEYWORD = "# Begin: Data"

    """returns a dictionary:
    startbyte = integer (bytes from beginning of file)
    startline = integer (lines from beginning of file)
    type      = 'binary4','binary8','ascii'
    """
    f = open(filename, "rb")
    lines = 0
    bytes = 0
    ans = {}
    while 1:
        line = f.readline()
        bytes = bytes + len(line)
        lines = lines + 1
        if not line:               # until eof
            print "***Reached end of file before encountering data"
            print "   Cowardly stopping here"
            print "   Some debug info:"
            print "   Have read", lines, "lines and"
            print "            ", bytes, "bytes."
            sys.exit(1)

        if line[0:len(DATAKEYWORD)] == DATAKEYWORD:
            bits = line.split()
            if bits[3] == "Binary":
                ans["type"] = "binary" + bits[4]
            else:
                if bits[3] == "Text":
                    ans["type"] = "ascii"
                else:
                    print "Data file", filename, \
                          "appears neither to be a text or"\
                          "a binary file."
                    print "Cowardly stopping here."
                    sys.exit(1)
            break
    ans["startbyte"] = bytes
    ans["startline"] = lines

    f.close()

    if verbose == 1:
        print "Data in", filename, "start in line", lines, "at byte",\
              bytes, "and is", ans["type"]

    return ans


def read_structured_ascii_oommf_data(fname, byte, dimensions, verbose=0):

    """fname is the filemane to read
    byte is the byte in that file at which the data starts
    dimensions = 3-tuple (nx, ny, nz) with the numbers of cells in the mesh

    This function will return one vector for each mesh cell combined in a list
    (in the fashion OOMMF stores data, i.e. (from OOMMF userguide)
    'these are ordered with the x index incremented first, then the y
    index, and the z index last.  This is nominally Fortran order, and
    is adopted here because commonly x will be the longest dimension,
    and z the shortest'

    The returned value is a list of 3-tuples. """

    data = open(fname).read()

    datalines = str.split(data[byte:], '\n')

    vectorfield = []

    for datum in datalines:
        if datum[0:16] == "# End: Data Text":
            break
        if datum[0] == "#":
            print "I found a # in the first column. Complete row is", datum
            print "I only expected '# End: Data Text'."
            print "cowardly stopping here"
            raise Exception("FileFormatError, See above for more details")

        vector_str = str.split(datum[0:-1])

        vector = map(lambda a: float(a), vector_str)
        if len(vector) != 3:
            print "vector_str=", vector_str
            print "vector    =", vector
            print "datum =", datum
            raise Exception("Oops, vector_str shold have 3 entries")

        vectorfield.append(vector)

    # expected number of data:
    data_exp = dimensions[0]*dimensions[1]*dimensions[2]

    if (data_exp - len(vectorfield)):
        print "Hmm, expected nx*ny*ny =", data_exp, "items, but got",\
              len(vectorfield), "."
        print "Cowardly stopping here."
        raise Exception("FileFormatError, read too many/too few data")

    print "Hint: Reading ASCII-OOMMF file is slow (that could be changed) \
and the files are large. Why not save data as binary?"

    return Numeric.array(vectorfield)


def read_structured_binary_oommf_data(fname, byte, dimensions, datatype,
                                      verbose=0):
    """fname is the filename to read
    byte is the byte in that file at which the (binary) data starts
    datatype = 'binary4','binary8','ascii'
    dimensions = 3-tuple (nx, ny, nz) with the numbers of cells in the mesh

    This function will return one tuple for each mesh cell combined in a list
    (in the fashion OOMMF stores data, i.e. (from OOMMF userguide)
    'these are ordered with the x index incremented first, then the y
    index, and the z index last.  This is nominally Fortran order, and
    is adopted here because commonly x will be the longest dimension,
    and z the shortest'

    The returned value is a list of 3-tuples. """

    if datatype == "binary4":
        floatsize = 4
    elif datatype == "binary8":
        floatsize = 8
    elif datatype == "ascii":
        print "ascii -oommf data not supported here"
        raise Exception("Not Implemented Error, \
ascii-oommf data not supported here")
    else:
        print "unknow datatype (expected  'binary4','binary8' [or 'ascii'],\
 but got ", datatype
        raise "Error"
{'fraction_non_alphanumeric': 0.11582953951787447, 'fraction_numerical': 0.00865736248973804, 'mean_word_length': 2.0961182994454712, 'pattern_counts': {'":': 22, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2457609', 'n_tokens_mistral': 4027, 'n_tokens_neox': 3862, 'n_words': 1411}