"""Scan a directory containing GULP NEB .gin files and verify these have correct format.
"""
import os
import sys
from PyLib.TinyParser import TinyParser

def VerifyNEBFileFormat(sNEBFilePath):
	"""Verify NEB file.
	Expected format:
		...
		cell
		fractional
		{word .}+
		{rcell
		rfractional
		{word .}+}+
		fcell
		ffractional
		{word .}+
		connect
		...
		species
		{word .}+
		library
	"""
	p= TinyParser(None,sNEBFilePath)
	dtAtomLabels= {}
	sPrefix=""
	bActive=True
	p.MoveTo(sPrefix+"cell",1)
	
	# log atom labels in all images
	while bActive:
		p.MoveTo(sPrefix+"fractional",1)
		p.EatLine()
		while True:
			sAtomLabel= p.EatWord()
			if sAtomLabel=="rcell":
				sPrefix="r"
				break
			elif sAtomLabel=="fcell":
				sPrefix="f"
				break
			elif sAtomLabel=="connect":
				bActive=False
				break
			if sAtomLabel not in dtAtomLabels:
				dtAtomLabels[sAtomLabel]=1
			p.EatLine()
	
	# now verify that species section rows exist for only the above atom labels
	p.MoveTo("species",1)
	p.Step()
	while True:
		sAtomLabel= p.EatWord()
		if sAtomLabel=="": break
		p.EatLine()
		if sAtomLabel not in dtAtomLabels:
			raise Exception(f"{sAtomLabel} not seen in atom labels for {sNEBFilePath}")
		del dtAtomLabels[sAtomLabel]
	if len(dtAtomLabels)!=0:
		raise Exception(f"{dtAtomLabels} remain for {sNEBFilePath}")
	else:
		print(sNEBFilePath)

def Main():
	"""Entry point.
	"""
	if len(sys.argv)==1:
		print("Syntax: python VerifyNEBFiles.py <dir>\n"
			" where <dir> is name of directory containing GULP NEB .gin files.")
		sys.exit(1)
	sDir= sys.argv[1]
	if sDir[-1]!="/": sDir+= "/"
	
	for sNebFile in os.listdir(sDir):
		try:
			VerifyNEBFileFormat(sDir+sNebFile)
		except:
			print(f"Error occured processing {sNebFile}")
			raise

if __name__=="__main__": Main()
"""Scan a directory containing GULP NEB .gin files and verify these have correct format.
"""
import os
import sys
from PyLib.TinyParser import TinyParser

def VerifyNEBFileFormat(sNEBFilePath):
	"""Verify NEB file.
	Expected format:
		...
		cell
		fractional
		{word .}+
		{rcell
		rfractional
		{word .}+}+
		fcell
		ffractional
		{word .}+
		connect
		...
		species
		{word .}+
		library
	"""
	p= TinyParser(None,sNEBFilePath)
	dtAtomLabels= {}
	sPrefix=""
	bActive=True
	p.MoveTo(sPrefix+"cell",1)
	
	# log atom labels in all images
	while bActive:
		p.MoveTo(sPrefix+"fractional",1)
		p.EatLine()
		while True:
			sAtomLabel= p.EatWord()
			if sAtomLabel=="rcell":
				sPrefix="r"
				break
			elif sAtomLabel=="fcell":
				sPrefix="f"
				break
			elif sAtomLabel=="connect":
				bActive=False
				break
			if sAtomLabel not in dtAtomLabels:
				dtAtomLabels[sAtomLabel]=1
			p.EatLine()
	
	# now verify that species section rows exist for only the above atom labels
	p.MoveTo("species",1)
	p.Step()
	while True:
		sAtomLabel= p.EatWord()
		if sAtomLabel=="": break
		p.EatLine()
		if sAtomLabel not in dtAtomLabels:
			raise Exception(f"{sAtomLabel} not seen in atom labels for {sNEBFilePath}")
		del dtAtomLabels[sAtomLabel]
	if len(dtAtomLabels)!=0:
		raise Exception(f"{dtAtomLabels} remain for {sNEBFilePath}")
	else:
		print(sNEBFilePath)

def Main():
	"""Entry point.
	"""
	if len(sys.argv)==1:
		print("Syntax: python VerifyNEBFiles.py <dir>\n"
			" where <dir> is name of directory containing GULP NEB .gin files.")
		sys.exit(1)
	sDir= sys.argv[1]
	if sDir[-1]!="/": sDir+= "/"
	
	for sNebFile in os.listdir(sDir):
		try:
			VerifyNEBFileFormat(sDir+sNebFile)
		except:
			print(f"Error occured processing {sNebFile}")
			raise

if __name__=="__main__": Main()
Scan a directory containing GULP NEB .gin files and verify these have correct format. Verify NEB file. 	Expected format: 		... 		cell 		fractional 		{word .}+ 		{rcell 		rfractional 		{word .}+}+ 		fcell 		ffractional 		{word .}+ 		connect 		... 		species 		{word .}+ 		library # log atom labels in all images # now verify that species section rows exist for only the above atom labels Entry point.