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
from __future__ import print_function
from builtins import object
import logging
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.unit_conversion.unit_conversion \
import register_userdefined_quantity
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"
def oct_energy_sm(octname, nomadname):
......@@ -78,14 +80,6 @@ class OctopusParserContext(object):
#logging.getLogger("nomadcore.parsing").info("closing section_scf_iteration bla gIndex %d %s", gIndex, section.simpleValues)
#self.scfIterNr += 1
def parse_infofile(metaInfoEnv, pew, fname):
#with open('info-parser.log', 'w') as fd:
mainFunction(infoFileDescription,
metaInfoEnv,
parserInfo,
#outF=fd,
cachingLevelForMetaName={},
superBackend=pew,
superContext=OctopusParserContext(),
mainFile=fname)
def parse_infofile(meta_info_env, pew, fname):
parse_file_without_decorations(pew, meta_info_env, infoFileDescription,
parserInfo, OctopusParserContext(), fname)
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 = {
"name": "logfile_parser_octopus",
......@@ -8,7 +10,6 @@ parserInfo = {
}
logFileDescription = SM(
name='root',
weak=True,
......@@ -19,8 +20,6 @@ logFileDescription = SM(
subMatchers=[
SM(r'Version\s*:\s*%s' % word('program_version')),
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):
pass
def parse_logfile(metaInfoEnv, pew, fname):
#with open('logfile-parse.log', 'w') as fd:
mainFunction(logFileDescription,
metaInfoEnv,
parserInfo,
#outF=fd,
cachingLevelForMetaName={},
superBackend=pew,
superContext=OctopusLogFileParserContext(),
mainFile=fname)
#for key in metaInfoEnv:
# print('key', key)
def parse_logfile(meta_info_env, pew, fname):
parse_file_without_decorations(pew, meta_info_env, logFileDescription,
parserInfo, OctopusLogFileParserContext(),
fname)
......@@ -262,16 +262,16 @@ def parse(fname, fd):
nspins = calc.get_number_of_spins()
nkpts = len(calc.get_k_point_weights())
#print('Parse info file using SimpleMatcher', file=fd)
#parse_infofile(metaInfoEnv, pew, fname)
print('Parse info file using SimpleMatcher', file=fd)
parse_infofile(metaInfoEnv, pew, fname)
#logfile = find_octopus_logfile(dirname)
#if logfile is None:
# print('No stdout logfile found', file=fd)
#else:
# print('Found stdout logfile %s' % logfile, file=fd)
# print('Parse logfile using SimpleMatcher', file=fd)
# parse_logfile(metaInfoEnv, pew, logfile)
logfile = find_octopus_logfile(dirname)
if logfile is None:
print('No stdout logfile found', file=fd)
else:
print('Found stdout logfile %s' % logfile, file=fd)
print('Parse logfile using SimpleMatcher', file=fd)
parse_logfile(metaInfoEnv, pew, logfile)
print('Add parsed values', file=fd)
with open_section('section_system'):
......@@ -289,6 +289,7 @@ def parse(fname, fd):
convert_unit(atoms.get_positions(), 'angstrom'))
pew.addArrayValues('configuration_periodic_dimensions',
np.array(atoms.pbc))
with open_section('section_single_configuration_calculation'):
with open_section('section_method'):
pew.addValue('number_of_spin_channels', nspins)
......
from nomadcore.simple_parser import mainFunction, runParser
OCT_ENERGY_UNIT_NAME = 'usrOctEnergyUnit'
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
......@@ -18,3 +20,31 @@ def integer(meta):
return pat(meta, regex=i_num)
def floating(meta):
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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment