1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
Regenerate reference JSON files for testing the VAMAS comment extraction.
"""

import os
import json
import numpy as np
from pynxtools_xps.vms.vamas_comment_handler import handle_comments


def read_comment_file(filepath: str):
    """Read comments from one vms comment test file."""

    no_of_comments = 0
    comment_lines = []

    with open(filepath, "rb") as vms_file:
        for i, line in enumerate(vms_file):
            if i == 0:
                no_of_comments = int(line)
            else:
                comment_lines += [line.decode("utf-8", errors="ignore").strip()]

    return no_of_comments, comment_lines


def generate_ref_comment_jsons():
    """This generates reference json files for the vms comment handler."""
    files = [
        ("kratos.vms", "block"),
        ("phi.vms", "block"),
        ("casa_header.vms", "header"),
        ("casa_process.vms", "block"),
        ("specs_header.vms", "header"),
        ("specs_block.vms", "block"),
    ]

    for file, comment_type in files:
        filepath = os.path.join(
            os.path.dirname(__file__), "..", "tests", "data", "vms_comments", file
        )
        ref_json_filepath = filepath.replace(".vms", "_ref.json")

        _, comment_lines = read_comment_file(filepath)

        comments = handle_comments(comment_lines, comment_type=comment_type)

        if file == "casa_process.vms":
            casa_process = comments["casa"]
            comments = casa_process.flatten_metadata()

        for key, val in comments.items():
            if isinstance(val, np.ndarray):
                comments[key] = val.tolist()

        with open(ref_json_filepath, "w") as json_file:
            json.dump(comments, json_file, indent=4)


if __name__ == "__main__":
    generate_ref_comment_jsons()