parser.py 3 KB
Newer Older
1
2
from builtins import next
from builtins import range
3
4
5
import os
import re
import logging
6
from nomadcore.baseclasses import ParserInterface
7
from cp2kparser.versions.versionsetup import get_main_parser
8
logger = logging.getLogger("nomad")
9
10
11
12
13
14


#===============================================================================
class CP2KParser(ParserInterface):
    """This class handles the initial setup before any parsing can happen. It
    determines which version of CP2K was used to generate the output and then
15
    sets up a correct main parser.
16
17
18
19

    After the implementation has been setup, you can parse the files with
    parse().
    """
20
21
    def __init__(self, main_file, metainfo_to_keep=None, backend=None, default_units=None, metainfo_units=None, debug=True, store=True):
        super(CP2KParser, self).__init__(main_file, metainfo_to_keep, backend, default_units, metainfo_units, debug, store)
22
23
24
25
26

    def setup_version(self):
        """Setups the version by looking at the output file and the version
        specified in it.
        """
27
28
29
30
31
32
33
34
        # Search for the CP2K version specification and the RUN_TYPE for the
        # calculation. The correct and optimized parser is initialized based on
        # this information.
        regex_version = re.compile(r" CP2K\| version string:\s+CP2K version ([\d\.]+)")
        regex_run_type = re.compile(r"\s+GLOBAL\| Run type\s+(.+)")
        n_lines = 50
        version_id = None
        run_type = None
35
        with open(self.parser_context.main_file, 'r') as outputfile:
36
            for i_line in range(n_lines):
37
                line = next(outputfile)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
                result_version = regex_version.match(line)
                result_run_type = regex_run_type.match(line)
                if result_version:
                    version_id = result_version.group(1).replace('.', '')
                if result_run_type:
                    run_type = result_run_type.group(1)
        if version_id is None:
            msg = "Could not find a version specification from the given main file."
            logger.exception(msg)
            raise RuntimeError(msg)
        if run_type is None:
            msg = "Could not find a version specification from the given main file."
            logger.exception(msg)
            raise RuntimeError(msg)
52
53
54

        # Setup the root folder to the fileservice that is used to access files
        dirpath, filename = os.path.split(self.parser_context.main_file)
55
        dirpath = os.path.abspath(dirpath)
56
57
58
59
60
        self.parser_context.file_service.setup_root_folder(dirpath)
        self.parser_context.file_service.set_file_id(filename, "output")

        # Setup the correct main parser based on the version id. If no match
        # for the version is found, use the main parser for CP2K 2.6.2
61
        self.main_parser = get_main_parser(version_id, run_type)(self.parser_context.main_file, self.parser_context)
62
63

    def get_metainfo_filename(self):
Lauri Himanen's avatar
Lauri Himanen committed
64
        return "cp2k.nomadmetainfo.json"
65
66
67

    def get_parser_info(self):
        return {'name': 'cp2k-parser', 'version': '1.0'}