"""
CIFファイルを読み込み、結晶構造情報を表示するスクリプト。
このスクリプトは、CIF (Crystallographic Information File) 形式のファイルを読み込み、
その中の結晶構造データを解析し、主要な情報を標準出力に表示します。
tkCIFクラスを利用してファイルのパースを行い、得られた結晶オブジェクトから
単位胞情報、原子座標、密度などを計算し、出力します。
:doc:`cif_read_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
from tklib.tkfile import tkFile
from tklib.tkutils import terminate, pint, pfloat, getarg, getintarg, getfloatarg
from tklib.tkapplication import tkApplication
from tklib.tkcrystal.tkcif import tkCIF
from tklib.tkcrystal.tkcrystal import tkCrystal
from tklib.tkcrystal.tkatomtype import tkAtomType
debug = 0
infile = 'test.cif'
single = 1
findvalidstructure = 1
[ドキュメント]
def usage():
"""
スクリプトの利用方法を表示します。
この関数は、コマンドラインからのスクリプト実行方法とその例を標準出力に示します。
"""
print("")
print("Usage:")
print(" python {} infile ".format(sys.argv[0]))
print(" ex: python {} {}"
.format(sys.argv[0], infile))
[ドキュメント]
def terminate(message = None):
"""
メッセージを表示し、スクリプトを終了します。
オプションで指定されたメッセージを表示した後、usage()関数を呼び出して利用方法を示し、
ユーザーの入力を待ってからプログラムを終了します。
:param message: str, optional 表示する終了メッセージ。デフォルトはNone。
:returns: None (プログラムを終了するため、実際の戻り値はありません)
"""
if message is not None:
print("")
print(message)
print("")
usage()
print("")
input("\nPress ENTER to exit>>")
exit()
infile = getarg(1, infile)
debug = getintarg(2, debug)
[ドキュメント]
def main():
"""
スクリプトのメイン処理を実行します。
この関数は、アプリケーションの初期設定を行い、ログファイルへの出力リダイレクトを設定します。
指定されたCIFファイルを読み込み、その内容を解析し、結晶構造に関する主要な情報を
標準出力およびログファイルに出力します。密度計算なども行います。
"""
app = tkApplication()
logfile = app.replace_path(infile, template = ["{dirname}", "{filebody}-out.txt"])
print(f"Open logfile [{logfile}]")
app.redirect(targets = ["stdout", logfile], mode = 'w')
print("")
print("=============== CIF file read ============")
print("infile: {}".format(infile))
print("single: {}".format(single))
print("findvalidstructure: {}".format(findvalidstructure))
print("debug: {}".format(debug))
print("")
print("Read [{}]".format(infile))
# if not tkutils.IsFile(infile):
# terminate("Error: Invalid infile [{}]".format(infile))
cif = tkCIF()
cif.debug = debug
cifdata = cif.ReadCIF(infile, find_valid_structure = findvalidstructure)
cif.Close()
if not cifdata:
terminate("Error: Could not get cifdat from infile [{}]".format(infile))
cifdata.Print()
cry = cifdata.GetCrystal()
print("")
print("==============================================")
cry.PrintInf()
print("")
print("==============================================")
d = cry.Density()
ad = cry.AtomDensity()
terminate()
if __name__ == "__main__":
main()