Skip to content
Snippets Groups Projects
Commit 01dd0ad3 authored by Ask Hjorth Larsen's avatar Ask Hjorth Larsen
Browse files

fix the way in which simplematchers are invoked: Extract only the stream of...

fix the way in which simplematchers are invoked: Extract only the stream of json, not the surrounding headers and other decorations.
parent cfe6bdc5
Branches
No related tags found
No related merge requests found
from __future__ import print_function
from builtins import object from builtins import object
import logging import logging
import setup_paths import setup_paths
from nomadcore.simple_parser import mainFunction, SimpleMatcher as SM from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.unit_conversion.unit_conversion \ from nomadcore.unit_conversion.unit_conversion \
import register_userdefined_quantity import register_userdefined_quantity
import os, sys, json import os, sys, json
from util import OCT_ENERGY_UNIT_NAME, f_num, i_num, numpattern, integer from util import OCT_ENERGY_UNIT_NAME, f_num, i_num, numpattern, integer,\
parse_file_without_decorations
# Match lines like: " Total = -7.05183\n" # Match lines like: " Total = -7.05183\n"
def oct_energy_sm(octname, nomadname): def oct_energy_sm(octname, nomadname):
...@@ -78,14 +80,6 @@ class OctopusParserContext(object): ...@@ -78,14 +80,6 @@ class OctopusParserContext(object):
#logging.getLogger("nomadcore.parsing").info("closing section_scf_iteration bla gIndex %d %s", gIndex, section.simpleValues) #logging.getLogger("nomadcore.parsing").info("closing section_scf_iteration bla gIndex %d %s", gIndex, section.simpleValues)
#self.scfIterNr += 1 #self.scfIterNr += 1
def parse_infofile(meta_info_env, pew, fname):
def parse_infofile(metaInfoEnv, pew, fname): parse_file_without_decorations(pew, meta_info_env, infoFileDescription,
#with open('info-parser.log', 'w') as fd: parserInfo, OctopusParserContext(), fname)
mainFunction(infoFileDescription,
metaInfoEnv,
parserInfo,
#outF=fd,
cachingLevelForMetaName={},
superBackend=pew,
superContext=OctopusParserContext(),
mainFile=fname)
from nomadcore.simple_parser import mainFunction, SimpleMatcher as SM from nomadcore.simple_parser import mainFunction, SimpleMatcher as SM
from util import numpattern, i_num, f_num, e_num, word, integer from util import numpattern, i_num, f_num, e_num, word, integer, \
parse_file_without_decorations
parserInfo = { parserInfo = {
"name": "logfile_parser_octopus", "name": "logfile_parser_octopus",
...@@ -8,7 +10,6 @@ parserInfo = { ...@@ -8,7 +10,6 @@ parserInfo = {
} }
logFileDescription = SM( logFileDescription = SM(
name='root', name='root',
weak=True, weak=True,
...@@ -19,8 +20,6 @@ logFileDescription = SM( ...@@ -19,8 +20,6 @@ logFileDescription = SM(
subMatchers=[ subMatchers=[
SM(r'Version\s*:\s*%s' % word('program_version')), SM(r'Version\s*:\s*%s' % word('program_version')),
SM(r'Revision\s*:\s*%s' % integer('x_octopus_log_svn_revision')), SM(r'Revision\s*:\s*%s' % integer('x_octopus_log_svn_revision')),
#SM(r'Input: [SmearingFunction = %s]' % word(''))
# Grr. But we have to convert semi_conducting to some other word.
] ]
) )
...@@ -29,15 +28,7 @@ class OctopusLogFileParserContext(object): ...@@ -29,15 +28,7 @@ class OctopusLogFileParserContext(object):
pass pass
def parse_logfile(metaInfoEnv, pew, fname): def parse_logfile(meta_info_env, pew, fname):
#with open('logfile-parse.log', 'w') as fd: parse_file_without_decorations(pew, meta_info_env, logFileDescription,
mainFunction(logFileDescription, parserInfo, OctopusLogFileParserContext(),
metaInfoEnv, fname)
parserInfo,
#outF=fd,
cachingLevelForMetaName={},
superBackend=pew,
superContext=OctopusLogFileParserContext(),
mainFile=fname)
#for key in metaInfoEnv:
# print('key', key)
...@@ -262,16 +262,16 @@ def parse(fname, fd): ...@@ -262,16 +262,16 @@ def parse(fname, fd):
nspins = calc.get_number_of_spins() nspins = calc.get_number_of_spins()
nkpts = len(calc.get_k_point_weights()) nkpts = len(calc.get_k_point_weights())
#print('Parse info file using SimpleMatcher', file=fd) print('Parse info file using SimpleMatcher', file=fd)
#parse_infofile(metaInfoEnv, pew, fname) parse_infofile(metaInfoEnv, pew, fname)
#logfile = find_octopus_logfile(dirname) logfile = find_octopus_logfile(dirname)
#if logfile is None: if logfile is None:
# print('No stdout logfile found', file=fd) print('No stdout logfile found', file=fd)
#else: else:
# print('Found stdout logfile %s' % logfile, file=fd) print('Found stdout logfile %s' % logfile, file=fd)
# print('Parse logfile using SimpleMatcher', file=fd) print('Parse logfile using SimpleMatcher', file=fd)
# parse_logfile(metaInfoEnv, pew, logfile) parse_logfile(metaInfoEnv, pew, logfile)
print('Add parsed values', file=fd) print('Add parsed values', file=fd)
with open_section('section_system'): with open_section('section_system'):
...@@ -289,6 +289,7 @@ def parse(fname, fd): ...@@ -289,6 +289,7 @@ def parse(fname, fd):
convert_unit(atoms.get_positions(), 'angstrom')) convert_unit(atoms.get_positions(), 'angstrom'))
pew.addArrayValues('configuration_periodic_dimensions', pew.addArrayValues('configuration_periodic_dimensions',
np.array(atoms.pbc)) np.array(atoms.pbc))
with open_section('section_single_configuration_calculation'): with open_section('section_single_configuration_calculation'):
with open_section('section_method'): with open_section('section_method'):
pew.addValue('number_of_spin_channels', nspins) pew.addValue('number_of_spin_channels', nspins)
......
from nomadcore.simple_parser import mainFunction, runParser
OCT_ENERGY_UNIT_NAME = 'usrOctEnergyUnit' OCT_ENERGY_UNIT_NAME = 'usrOctEnergyUnit'
f_num = r'[-+]?(\d*\.\d+|\d+\.\d*)' # e.g.: 0.7 1. -.1 f_num = r'[-+]?(\d*\.\d+|\d+\.\d*)' # e.g.: 0.7 1. -.1
e_num = r'[-+]?\d*\.\d+[EeDd][-+]\d*' # e.g.: -7.642e-300 e_num = r'[-+]?\d*\.\d+[EeDd][-+]\d*' # e.g.: -7.642e-300
...@@ -18,3 +20,31 @@ def integer(meta): ...@@ -18,3 +20,31 @@ def integer(meta):
return pat(meta, regex=i_num) return pat(meta, regex=i_num)
def floating(meta): def floating(meta):
return pat(meta, regex='%s|%s' % (f_num, e_num)) return pat(meta, regex='%s|%s' % (f_num, e_num))
def parse_file_without_decorations(pew, meta_info_env, matchers,
parser_info, supercontext, fname):
"""Run SimpleMatcher on a file printing only sequence of json."""
# If we set outF, it only redirects some of the stuff.
# It writes "[" followed by json followed by "]", to
# different streams. By redirecting one of them we can get only
# the json. Strange but true!
class DevNull:
def write(self, txt):
pass
# This is to prevent another header (nomad_parse_events_1_0 etc.)
def parsefile(parser_builder, uri, path, backend, super_context):
with open(path) as fd:
runParser(parser_builder, backend, super_context, fd)
mainFunction(matchers,
meta_info_env,
parser_info,
outF=DevNull(), # Eats surrounding []
parseFile=parsefile,
cachingLevelForMetaName={},
superBackend=pew,
superContext=supercontext,
mainFile=fname)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment