"""
CIFファイルの読み込み、処理、および関連する結晶学データ操作を行うスクリプト。
このスクリプトは、指定されたCIFファイル(単一または複数)を読み込み、
結晶構造データを解析し、密度や原子密度などの物理量を計算します。
また、データの妥当性チェックを行い、結果を新しいCIFファイルとして出力したり、
処理済みのファイルを指定されたディレクトリに移動したりする機能を提供します。
:doc:`cif_usage`
"""
import os
import sys
import shutil
import glob
import numpy as np
from numpy import sin, cos, tan, pi
import csv
from pprint import pprint
sys.path.append("d:/git/tkProg/tklib/python")
from tklib.tkfile import tkFile
from tklib.tkcrystal.tkcif import tkCIF
from tklib.tkcrystal.tkcrystal import tkCrystal
from tklib.tkcrystal.tkatomtype import tkAtomType
infile = 'COD/*.cif'
#infile = 'cif/*.cif'
#infile = 'test1.cif'
#infile = 'test.cif'
debug = 0
single = 1
findvalidstructure = 1
checklist = 'cifcheck.txt'
okdir = 'cif/ok'
#okdir = ''
print("infile: {}".format(infile))
print("single: {}".format(single))
print("findvalidstructure: {}".format(findvalidstructure))
print("checklist: {}".format(checklist))
print("debug: {}".format(debug))
[ドキュメント]
def main():
"""
スクリプトの主要な処理フローを定義し、CIFファイルの読み込みと処理を実行します。
この関数は、コマンドライン引数で指定されたパターンに基づいてCIFファイルを検索し、
それぞれのファイルを `tkCIF` クラスを使用して読み込み、処理します。
`single` フラグが有効な場合、各CIFファイルから単一の結晶構造を抽出し、
その構造の密度と原子密度を計算して妥当性をチェックします。
妥当な構造であれば、簡易CIFファイルと結晶データから生成したCIFファイルを
それぞれ `a.cif` および `b.cif` として出力します。
`okdir` が設定されている場合、正常に処理されたCIFファイルは指定されたディレクトリに移動されます。
`single` フラグが無効な場合、ファイル内のすべてのCIF構造が処理されます。
処理の進行状況は標準出力に表示され、チェックリストファイルに処理されたファイル名が記録されます。
:returns: None
:rtype: None
"""
cout = tkFile(checklist, 'w')
files = glob.glob(infile)
for f in files:
print("\nRead from [{}]".format(f))
cout.Write("{}\n".format(f))
cif = tkCIF()
cif.debug = debug
# atom = tkAtomType()
# inf = atom.GetAtomInformation('Au')
# for key in inf:
# print("{}: {}".format(key, inf[key]))
# print("")
# exit()
if single:
cifdata = cif.ReadCIF(f, find_valid_structure = findvalidstructure)
cifdata.Print()
cry = cifdata.GetCrystal()
print("")
print("==============================================")
cry.PrintInf()
print("")
print("==============================================")
d = cry.Density()
ad = cry.AtomDensity()
if not (0.8 < d < 20.0):
print("Error in [{}]: Too low or large density {} g/cm3".format(f, d))
exit()
if not (1.0e22 < ad < 10.0e23):
print("Error in [{}]: Too low or large atom density {} /cm3".format(f, ad))
exit()
if okdir != '':
# print("fp=", cif.fp)
# cif.fp.close()
print("move {} to {}".format(f, okdir))
shutil.move(f, okdir)
# shutil.copy2(f, okdir)
# os.unlink(f)
outfile = 'a.cif'
cifdata.WriteSimpleCIFFile(outfile)
outfile = 'b.cif'
cifdata.CreateCIFFileFromCCrystal(cry, outfile)
# pos0 = [0.3, 0.2, 0.1]
# isym = 1
# pos1 = cry.DoSymmetryOperation(pos0, isym)
# print("pos0 ", pos0, " => sym %d" % (isym), pos1)
else:
cifdatas = cif.ReadCIFs(f)
for cifdata in cifdatas:
cifdata.Print()
cif.Close()
cout.Close()
if __name__ == "__main__":
narg = len(sys.argv)
if narg >= 2:
infile = sys.argv[1]
if narg >= 3:
debug = int(sys.argv[2])
main()