From 2f3cc13928475d247d28c6e7c4924b1c5def7b98 Mon Sep 17 00:00:00 2001 From: Daria Tomecka <zarvok79@yahoo.com> Date: Wed, 27 Jul 2016 17:39:41 +0200 Subject: [PATCH] sub parser for struct file --- parser/parser-wien2k/wien2k_parser.py | 27 +++++++-- parser/parser-wien2k/wien2k_parser_struct.py | 56 +++++++++++++++++++ .../eu/nomad_lab/parsers/Wien2kParser.scala | 1 + 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 parser/parser-wien2k/wien2k_parser_struct.py diff --git a/parser/parser-wien2k/wien2k_parser.py b/parser/parser-wien2k/wien2k_parser.py index d3e82e0..7554864 100644 --- a/parser/parser-wien2k/wien2k_parser.py +++ b/parser/parser-wien2k/wien2k_parser.py @@ -1,9 +1,10 @@ from builtins import object import setup_paths -from nomadcore.simple_parser import mainFunction +from nomadcore.simple_parser import mainFunction, AncillaryParser, CachingLevel from nomadcore.simple_parser import SimpleMatcher as SM from nomadcore.local_meta_info import loadJsonFile, InfoKindEl import os, sys, json +import wien2k_parser_struct class Wien2kContext(object): """context for wien2k parser""" @@ -13,7 +14,7 @@ class Wien2kContext(object): def initialize_values(self): """allows to reset values if the same superContext is used to parse different files""" - pass + self.metaInfoEnv = self.parser.parserBuilder.metaInfoEnv def startedParsing(self, path, parser): """called when parsing starts""" @@ -26,6 +27,21 @@ class Wien2kContext(object): section["x_wien2k_version"][0] + " " + section["x_wien2k_release_date"][0]) + def onOpen_section_system(self, backend, gIndex, section): + + structSuperContext = wien2k_parser_struct.Wien2kStructContext() + structParser = AncillaryParser( + fileDescription = wien2k_parser_struct.buildStructureMatchers(), + parser = self.parser, + cachingLevelForMetaName = wien2k_parser_struct.get_cachingLevelForMetaName(self.metaInfoEnv, CachingLevel.PreOpenedIgnore), + superContext = structSuperContext) + + mainFile = self.parser.fIn.fIn.name + fName = mainFile[:-4] + ".struct" + if os.path.exists(fName): + with open(fName) as fIn: + structParser.parseFile(fIn) + # description of the input mainFileDescription = SM( name = 'root', @@ -37,7 +53,7 @@ mainFileDescription = SM( repeats = True, required = True, forwardMatch = True, - sections = ['section_run', 'section_single_configuration_calculation'], + sections = ['section_run', 'section_method', 'section_system', 'section_single_configuration_calculation'], subMatchers = [ SM( name = 'header', @@ -48,9 +64,12 @@ mainFileDescription = SM( name = "scf iteration", startReStr = r"\s*:ITE(?P<x_wien2k_iteration_number>[0-9]+):\s*[0-9]*. ITERATION", sections=["section_scf_iteration"], + repeats = True, subMatchers=[ SM(r":NATO :\s*(?P<x_wien2k_number_of_independent_atoms>[0-9]+)INDEPENDENT AND\s*(?P<x_wien2k_total_atoms>[0-9]+)\s*TOTAL ATOMS IN UNITCELL"), - SM("\s*SUBSTANCE: (?P<x_wien2k_system_name>.*)") + SM(r"\s*SUBSTANCE: (?P<x_wien2k_system_name>.*)"), + SM(r":POT\s*:\s*POTENTIAL OPTION\s*(?P<x_wien2k_potential_option>[0-9]+)"), + SM(r":VOL\s*:\s*UNIT CELL VOLUME\s*=\s*(?P<x_wien2k_unit_cell_volume__angstrom3>[0-9.]+)") ] ) ] diff --git a/parser/parser-wien2k/wien2k_parser_struct.py b/parser/parser-wien2k/wien2k_parser_struct.py new file mode 100644 index 0000000..8657792 --- /dev/null +++ b/parser/parser-wien2k/wien2k_parser_struct.py @@ -0,0 +1,56 @@ +from builtins import object +import setup_paths +from nomadcore.simple_parser import mainFunction, CachingLevel +from nomadcore.simple_parser import SimpleMatcher as SM +from nomadcore.local_meta_info import loadJsonFile, InfoKindEl +import os, sys, json + +class Wien2kStructContext(object): + """context for wien2k struct parser""" + + def __init__(self): + self.parser = None + + def initialize_values(self): + """allows to reset values if the same superContext is used to parse different files""" + pass + + def startedParsing(self, path, parser): + """called when parsing starts""" + self.parser = parser + # allows to reset values if the same superContext is used to parse different files + self.initialize_values() + + +# description of the input +def buildStructureMatchers(): + return SM( + name = 'root', + weak = True, + startReStr = "", + sections = ["section_run", "section_system"], + subMatchers = [ + SM(name = 'systemName', + startReStr = r"(?P<x_wien2k_system_nameIn>.*)") + ]) + +def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl): + """Sets the caching level for the metadata. + + Args: + metaInfoEnv: metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py. + CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation. + This allows to run the parser without opening new sections. + + Returns: + Dictionary with metaname as key and caching level as value. + """ + # manually adjust caching of metadata + cachingLevelForMetaName = { + 'section_run': CachingLvl, + 'section_system': CachingLvl + } + cachingLevelForMetaName["x_wien2k_system_nameIn"] = CachingLevel.ForwardAndCache + return cachingLevelForMetaName + +# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/fhi_aims.nomadmetainfo.json diff --git a/src/main/scala/eu/nomad_lab/parsers/Wien2kParser.scala b/src/main/scala/eu/nomad_lab/parsers/Wien2kParser.scala index ba10957..ad314e2 100644 --- a/src/main/scala/eu/nomad_lab/parsers/Wien2kParser.scala +++ b/src/main/scala/eu/nomad_lab/parsers/Wien2kParser.scala @@ -27,6 +27,7 @@ object Wien2kParser extends SimpleExternalParserGenerator( "--uri", "${mainFileUri}", "${mainFilePath}"), resList = Seq( "parser-wien2k/wien2k_parser.py", + "parser-wien2k/wien2k_parser_struct.py", "parser-wien2k/setup_paths.py", "nomad_meta_info/public.nomadmetainfo.json", "nomad_meta_info/common.nomadmetainfo.json", -- GitLab