__init__.py 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright 2018 Markus Scheidgen
#
# 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.

"""
16
The *parsing* module is an interface for the existing NOMAD-coe parsers.
17
18
This module redefines some of the old NOMAD-coe python-common functionality to create a
more coherent interface to the parsers.
19
20
21
22
23
24
25
26

Assumption about parsers
------------------------
For now, we make a few assumption about parsers
- they always work on the same *meta-info* version
- they have no conflicting python requirments
- they can be loaded at the same time and can be used within the same python process
- they are uniquely identified by a GIT URL and publicly accessible
27
- their version is uniquely identified by a GIT commit SHA
28
29
30
31
32
33

Each parser is defined via an instance of :class:`Parser`.

.. autoclass:: nomad.parsing.Parser
    :members:

Markus Scheidgen's avatar
Markus Scheidgen committed
34
35
36
37
The implementation :class:`LegacyParser` is used for most NOMAD-coe parsers.

.. autoclass:: nomad.parsing.LegacyParser

38
39
40
41
42
The parser definitions are available via the following two variables.

.. autodata:: nomad.parsing.parsers
.. autodata:: nomad.parsing.parser_dict

Markus Scheidgen's avatar
Markus Scheidgen committed
43
44
Parsers are reused for multiple caclulations.

45
46
47
48
Parsers and calculation files are matched via regular expressions.

.. autofunc:: nomad.parsing.match_parser

Markus Scheidgen's avatar
Markus Scheidgen committed
49
Parsers in NOMAD-coe use a *backend* to create output. There are different NOMAD-coe
Markus Scheidgen's avatar
Markus Scheidgen committed
50
basends. In nomad@FAIRDI, we only currently only use a single backed. A version of
Markus Scheidgen's avatar
Markus Scheidgen committed
51
52
53
NOMAD-coe's *LocalBackend*. It stores all parser results in memory. The following
classes provide a interface definition for *backends* as an ABC and a concrete implementation
based on NOMAD-coe's *python-common* module.
54
55
56
57
58

.. autoclass:: nomad.parsing.AbstractParserBackend
    :members:
.. autoclass:: nomad.parsing.LocalBackend
    :members:
Markus Scheidgen's avatar
Markus Scheidgen committed
59

60
"""
61
62
from typing import Callable, IO
import magic
63
64
65
66
import gzip
import bz2

from nomad import files
67
68

from nomad.parsing.backend import AbstractParserBackend, LocalBackend, LegacyLocalBackend, JSONStreamWriter, BadContextURI, WrongContextState
69
from nomad.parsing.parser import Parser, LegacyParser, VaspOutcarParser
70
from nomad.parsing.artificial import TemplateParser, GenerateRandomParser, ChaosParser
71

72

73
74
75
76
77
78
79
_compressions = {
    b'\x1f\x8b\x08': ('gz', gzip.open),
    b'\x42\x5a\x68': ('bz2', bz2.open)
}


def match_parser(mainfile: str, upload_files: files.StagingUploadFiles) -> 'Parser':
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    """
    Performs parser matching. This means it take the given mainfile and potentially
    opens it with the given callback and tries to identify a parser that can parse
    the file.

    This is determined by filename (e.g. *.out), mime type (e.g. text/*, application/xml),
    and beginning file contents.

    Arguments:
        mainfile: The upload relative path to the mainfile
        open: A function that allows to open a stream to the file

    Returns: The parser, or None if no parser could be matched.
    """
94
95
96
    with upload_files.raw_file(mainfile, 'rb') as f:
        compression, open_compressed = _compressions.get(f.read(3), (None, open))

97
98
    mainfile_path = upload_files.raw_file_object(mainfile).os_path
    with open_compressed(mainfile_path, 'rb') as f:
99
100
101
102
103
104
105
        buffer = f.read(2048)

    mime_type = magic.from_buffer(buffer, mime=True)
    if mime_type.startswith('application') and not mime_type.endswith('xml'):
        return None

    for parser in parsers:
106
        if parser.is_mainfile(mainfile_path, mime_type, buffer.decode('utf-8'), compression):
107
108
109
110
111
            return parser

    return None


112
parsers = [
113
    GenerateRandomParser(),
114
    TemplateParser(),
115
    ChaosParser(),
116
    LegacyParser(
117
        name='parsers/vasp',
118
        parser_class_name='vaspparser.VASPRunParserInterface',
119
120
        mainfile_mime_re=r'(application/xml)|(text/.*)',
        mainfile_contents_re=(
121
122
123
124
            r'^\s*<\?xml version="1\.0" encoding="ISO-8859-1"\?>\s*'
            r'?\s*<modeling>'
            r'?\s*<generator>'
            r'?\s*<i name="program" type="string">\s*vasp\s*</i>'
125
126
            r'?'),
        supported_compressions=['gz', 'bz2']
127
    ),
128
    VaspOutcarParser(
129
        name='parsers/vasp',
130
        parser_class_name='vaspparser.VaspOutcarParser',
131
132
        mainfile_name_re=r'(.*/)?OUTCAR(\.[^\.]*)?',
        mainfile_contents_re=(r'^\svasp\.')
133
    ),
134
    LegacyParser(
135
        name='parsers/exciting',
136
        parser_class_name='excitingparser.ExcitingParser',
137
138
        mainfile_name_re=r'^.*/INFO\.OUT?',
        mainfile_contents_re=(
139
140
141
142
143
            r'^\s*=================================================+\s*'
            r'\s*\|\s*EXCITING\s+\S+\s+started\s*='
            r'\s*\|\s*version hash id:\s*\S*\s*=')
    ),
    LegacyParser(
144
        name='parsers/fhi-aims',
145
        parser_class_name='fhiaimsparser.FHIaimsParser',
146
        mainfile_contents_re=(
147
148
149
            r'^(.*\n)*'
            r'?\s*Invoking FHI-aims \.\.\.'
            r'?\s*Version')
150
151
    ),
    LegacyParser(
152
        name='parsers/cp2k',
153
        parser_class_name='cp2kparser.CP2KParser',
154
        mainfile_contents_re=(
155
156
157
158
            r'\*\*\*\* \*\*\*\* \*\*\*\*\*\*  \*\*  PROGRAM STARTED AT\s.*\n'
            r' \*\*\*\*\* \*\* \*\*\*  \*\*\* \*\*   PROGRAM STARTED ON\s*.*\n'
            r' \*\*    \*\*\*\*   \*\*\*\*\*\*    PROGRAM STARTED BY .*\n'
            r' \*\*\*\*\* \*\*    \*\* \*\* \*\*   PROGRAM PROCESS ID .*\n'
speckhard's avatar
speckhard committed
159
160
            r'  \*\*\*\* \*\*  \*\*\*\*\*\*\*  \*\*  PROGRAM STARTED IN .*\n'
        )
161
    ),
162
    LegacyParser(
163
        name='parsers/crystal',
164
        parser_class_name='crystalparser.CrystalParser',
165
        mainfile_contents_re=(
speckhard's avatar
speckhard committed
166
167
168
169
170
            r'\s*[\*]{22,}'  # Looks for '*' 22 times or more in a row.
            r'\s*\*\s{20,}\*'  # Looks for a '*' sandwhiched by whitespace.
            r'\s*\*\s{10,}CRYSTAL(?P<majorVersion>[\d]+)\s{10,}\*'
            r'\s*\*\s{10,}public \: (?P<minorVersion>[\d\.]+) \- .*\*'
        )
171
    ),
172
173
174
    # The main contents regex of CPMD was causing a catostrophic backtracking issue
    # when searching through the first 500 bytes of main files. We decided
    # to use only a portion of the regex to avoid that issue.
175
    LegacyParser(
176
        name='parsers/cpmd',
177
        parser_class_name='cpmdparser.CPMDParser',
178
        mainfile_contents_re=(
179
180
181
182
183
184
185
186
            # r'\s+\*\*\*\*\*\*  \*\*\*\*\*\*    \*\*\*\*  \*\*\*\*  \*\*\*\*\*\*\s*'
            # r'\s+\*\*\*\*\*\*\*  \*\*\*\*\*\*\*   \*\*\*\*\*\*\*\*\*\*  \*\*\*\*\*\*\*\s+'
            r'\*\*\*       \*\*   \*\*\*  \*\* \*\*\*\* \*\*  \*\*   \*\*\*'
            # r'\s+\*\*        \*\*   \*\*\*  \*\*  \*\*  \*\*  \*\*    \*\*\s+'
            # r'\s+\*\*        \*\*\*\*\*\*\*   \*\*      \*\*  \*\*    \*\*\s+'
            # r'\s+\*\*\*       \*\*\*\*\*\*    \*\*      \*\*  \*\*   \*\*\*\s+'
            # r'\s+\*\*\*\*\*\*\*  \*\*        \*\*      \*\*  \*\*\*\*\*\*\*\s+'
            # r'\s+\*\*\*\*\*\*  \*\*        \*\*      \*\*  \*\*\*\*\*\*\s+'
speckhard's avatar
speckhard committed
187
        )
188
    ),
speckhard's avatar
speckhard committed
189
    LegacyParser(
190
        name='parsers/nwchem',
speckhard's avatar
speckhard committed
191
        parser_class_name='nwchemparser.NWChemParser',
192
        mainfile_contents_re=(
193
194
195
196
197
            r'\s+Northwest Computational Chemistry Package \(NWChem\) \d+\.\d+'
            r'\s+------------------------------------------------------'
            r'\s+Environmental Molecular Sciences Laboratory'
            r'\s+Pacific Northwest National Laboratory'
            r'\s+Richland, WA 99352'
speckhard's avatar
speckhard committed
198
199
200
        )
    ),
    LegacyParser(
201
        name='parsers/bigdft',
speckhard's avatar
speckhard committed
202
        parser_class_name='bigdftparser.BigDFTParser',
203
        mainfile_contents_re=(
Markus Scheidgen's avatar
Markus Scheidgen committed
204
205
206
207
208
209
            # r'__________________________________ A fast and precise DFT wavelet code\s*'
            # r'\|     \|     \|     \|     \|     \|\s*'
            # r'\|     \|     \|     \|     \|     \|      BBBB         i       gggggg\s*'
            # r'\|_____\|_____\|_____\|_____\|_____\|     B    B               g\s*'
            # r'\|     \|  :  \|  :  \|     \|     \|    B     B        i     g\s*'
            # r'\|     \|-0\+--\|-0\+--\|     \|     \|    B    B         i     g        g\s*'
speckhard's avatar
speckhard committed
210
            r'\|_____\|__:__\|__:__\|_____\|_____\|___ BBBBB          i     g         g\s*'
Markus Scheidgen's avatar
Markus Scheidgen committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
            # r'\|  :  \|     \|     \|  :  \|     \|    B    B         i     g         g\s*'
            # r'\|--\+0-\|     \|     \|-0\+--\|     \|    B     B     iiii     g         g\s*'
            # r'\|__:__\|_____\|_____\|__:__\|_____\|    B     B        i      g        g\s*'
            # r'\|     \|  :  \|  :  \|     \|     \|    B BBBB        i        g      g\s*'
            # r'\|     \|-0\+--\|-0\+--\|     \|     \|    B        iiiii          gggggg\s*'
            # r'\|_____\|__:__\|__:__\|_____\|_____\|__BBBBB\s*'
            # r'\|     \|     \|     \|  :  \|     \|                           TTTTTTTTT\s*'
            # r'\|     \|     \|     \|--\+0-\|     \|  DDDDDD          FFFFF        T\s*'
            # r'\|_____\|_____\|_____\|__:__\|_____\| D      D        F        TTTT T\s*'
            # r'\|     \|     \|     \|  :  \|     \|D        D      F        T     T\s*'
            # r'\|     \|     \|     \|--\+0-\|     \|D         D     FFFF     T     T\s*'
            # r'\|_____\|_____\|_____\|__:__\|_____\|D___      D     F         T    T\s*'
            # r'\|     \|     \|  :  \|     \|     \|D         D     F          TTTTT\s*'
            # r'\|     \|     \|--\+0-\|     \|     \| D        D     F         T    T\s*'
            # r'\|_____\|_____\|__:__\|_____\|_____\|          D     F        T     T\s*'
            # r'\|     \|     \|     \|     \|     \|         D               T    T\s*'
            # r'\|     \|     \|     \|     \|     \|   DDDDDD       F         TTTT\s*'
            # r'\|_____\|_____\|_____\|_____\|_____\|______                    www\.bigdft\.org'
speckhard's avatar
speckhard committed
229
        )
230
231
    ),
    LegacyParser(
232
        name='parsers/wien2k',
233
        parser_class_name='wien2kparser.Wien2kParser',
234
        mainfile_contents_re=r':LABEL\d+: using WIEN2k_\d+\.\d+'
Markus Scheidgen's avatar
Markus Scheidgen committed
235
236
237
    ),
    LegacyParser(
        name='parsers/band',
238
        parser_class_name='bandparser.BANDParser',
Markus Scheidgen's avatar
Markus Scheidgen committed
239
        mainfile_contents_re=r' +\* +Amsterdam Density Functional +\(ADF\)'),
Daniel Speckhard's avatar
Daniel Speckhard committed
240
241
242
    LegacyParser(
        name='parsers/gaussian',
        parser_class_name='gaussianparser.GaussianParser',
243
        mainfile_contents_re=(
Daniel Speckhard's avatar
Daniel Speckhard committed
244
245
246
            r'\s*Cite this work as:'
            r'\s*Gaussian [0-9]+, Revision [A-Za-z0-9.]*,'
            r'\s\*\*\*\*\*\*\*\*\*\*\*\**'
Markus Scheidgen's avatar
Markus Scheidgen committed
247
248
            r'\s*Gaussian\s*([0-9]+):\s*([A-Za-z0-9-.]+)\s*([0-9][0-9]?\-[A-Z][a-z][a-z]\-[0-9]+)'
            r'\s*([0-9][0-9]?\-[A-Z][a-z][a-z]\-[0-9]+)')
249
250
251
    ),
    LegacyParser(
        name='parsers/quantumespresso',
252
        parser_class_name='quantumespressoparser.QuantumEspressoParserPWSCF',
253
        mainfile_contents_re=(
Markus Scheidgen's avatar
Markus Scheidgen committed
254
255
            r'^\s*Program (\S+)\s+v\.(\S+)(?:\s+\(svn\s+rev\.\s+'
            r'(\d+)\s*\))?\s+starts[^\n]+'
256
            r'(?:\s*\n?)*This program is part of the open-source Quantum')
Daniel Speckhard's avatar
Daniel Speckhard committed
257
258
259
260
261
    ),
    LegacyParser(
        name='parsers/abinit',
        parser_class_name='abinitparser.AbinitParser',
        mainfile_contents_re=(r'^\n\.Version\s*[0-9.]*\s*of ABINIT\s*')
speckhard's avatar
speckhard committed
262
    )
263
]
264

Daniel Speckhard's avatar
Daniel Speckhard committed
265

266
267
""" Instanciation and constructor based config of all parsers. """

Markus Scheidgen's avatar
Markus Scheidgen committed
268
parser_dict = {parser.name: parser for parser in parsers}  # type: ignore
269
""" A dict to access parsers by name. Usually 'parsers/<...>', e.g. 'parsers/vasp'. """