Parse a SCG INK file.

    Parameters
    ----------
    filename : string
        The path to a SCG INK file.

    Returns
    -------
    HandwrittenData
        The recording as a HandwrittenData object.

def parse_scg_ink_file(filename):
    """Parse a SCG INK file.

    Parameters
    ----------
    filename : string
        The path to a SCG INK file.

    Returns
    -------
    HandwrittenData
        The recording as a HandwrittenData object.
    """
    stroke_count = 0
    stroke_point_count = -1
    recording = []
    current_stroke = []
    time = 0
    got_annotations = False
    annotations = []

    formula_in_latex = get_latex(filename)

    with open(filename) as f:
        contents = f.read().strip()
    lines = contents.split("\n")
    for i, line in enumerate(lines):
        line = line.strip()
        if i == 0 and line != 'SCG_INK':
            raise ValueError(("%s: SCG Ink files have to start with 'SCG_INK'."
                              " The file started with %s.") %
                             (filename, line))
        elif i == 1:
            try:
                stroke_count = int(line)
            except ValueError:
                raise ValueError(("%s: Second line has to be the number of "
                                  "strokeswhich has to be an integer, but "
                                  "was '%s'") % (filename, line))
            if stroke_count <= 0:
                # os.remove(filename)
                # return []
                raise ValueError(("%s: Stroke count was %i, but should be "
                                  "> 0.") % (filename, stroke_count))
        elif i == 2:
            try:
                stroke_point_count = int(line)
            except ValueError:
                raise ValueError("%s: Third line has to be the number of "
                                 "points which has to be an integer, but was "
                                 "'%s'" % (filename, line))
            if stroke_point_count <= 0:
                raise ValueError(("%s: Stroke point count was %i, but should "
                                  "be > 0.") % (filename, stroke_count))
        elif i > 2:
            if stroke_point_count > 0:
                x, y = [int(el) for el in line.strip().split(" ")]
                current_stroke.append({'x': x, 'y': y, 'time': time})
                time += 20
                stroke_point_count -= 1
            elif line == 'ANNOTATIONS' or got_annotations:
                got_annotations = True
                annotations.append(line)
            elif stroke_count > 0:
                try:
                    stroke_point_count = int(line)
                except ValueError:
                    raise ValueError(("%s: Line %i has to be the number of "
                                      "points which has to be an integer, "
                                      " but was '%s'") %
                                     (filename, i + 1, line))
                if stroke_point_count <= 0:
                    raise ValueError(("%s: Stroke point count was %i, but "
                                      "should be > 0.") %
                                     (filename, stroke_count))
            if stroke_point_count == 0 and len(current_stroke) > 0:
                time += 200
                recording.append(current_stroke)
                stroke_count -= 1
                current_stroke = []
    hw = handwritten_data.HandwrittenData(json.dumps(recording),
                                          formula_in_latex=formula_in_latex,
                                          formula_id=datasets.formula_to_dbid(mathbrush_formula_fix(formula_in_latex)))
    hw.internal_id = "/".join(filename.split("/")[-2:])
    hw.segmentation, hw.symbol_stream = get_segmentation(recording,
                                                         annotations,
                                                         hw.internal_id)
    hw.description = "\n".join(annotations)
    hw.username = "MathBrush::%s" % os.path.basename(os.path.dirname(filename))
    copyright_str = ("This dataset was contributed by MathBrush. You can "
                     "download their complete dataset by contacting them. See "
                     "[www.scg.uwaterloo.ca/mathbrush/]"
                     "(https://www.scg.uwaterloo.ca/mathbrush/publications/"
                     "corpus.pdf)")
    hw.user_id = datasets.getuserid(hw.username, copyright_str)
    return hw