cif_read.py ダウンロード/コピー

cif_read.py をダウンロード

cif_read.py
cif_read.py
  1"""
  2CIFファイルを読み込み、結晶構造情報を表示するスクリプト。
  3
  4このスクリプトは、CIF (Crystallographic Information File) 形式のファイルを読み込み、
  5その中の結晶構造データを解析し、主要な情報を標準出力に表示します。
  6tkCIFクラスを利用してファイルのパースを行い、得られた結晶オブジェクトから
  7単位胞情報、原子座標、密度などを計算し、出力します。
  8
  9:doc:`cif_read_usage`
 10"""
 11import os
 12import sys
 13import shutil
 14import glob
 15import numpy as np
 16from numpy import sin, cos, tan, pi
 17import csv
 18from pprint import pprint
 19
 20
 21from tklib.tkfile import tkFile
 22from tklib.tkutils import terminate, pint, pfloat, getarg, getintarg, getfloatarg
 23from tklib.tkapplication import tkApplication
 24
 25from tklib.tkcrystal.tkcif import tkCIF
 26from tklib.tkcrystal.tkcrystal import tkCrystal
 27from tklib.tkcrystal.tkatomtype import tkAtomType
 28
 29
 30debug = 0
 31
 32infile = 'test.cif'
 33single = 1
 34findvalidstructure = 1
 35
 36
 37def usage():
 38    """
 39    スクリプトの利用方法を表示します。
 40
 41    この関数は、コマンドラインからのスクリプト実行方法とその例を標準出力に示します。
 42    """
 43    print("")
 44    print("Usage:")
 45    print(" python {} infile ".format(sys.argv[0]))
 46    print("     ex: python {} {}"
 47                .format(sys.argv[0], infile))
 48
 49def terminate(message = None):
 50    """
 51    メッセージを表示し、スクリプトを終了します。
 52
 53    オプションで指定されたメッセージを表示した後、usage()関数を呼び出して利用方法を示し、
 54    ユーザーの入力を待ってからプログラムを終了します。
 55
 56    :param message: str, optional 表示する終了メッセージ。デフォルトはNone。
 57    :returns: None (プログラムを終了するため、実際の戻り値はありません)
 58    """
 59    if message is not None:
 60        print("")
 61        print(message)
 62
 63    print("")
 64    usage()
 65    print("")
 66    
 67    input("\nPress ENTER to exit>>")
 68    exit()
 69
 70
 71infile = getarg(1, infile)
 72debug  = getintarg(2, debug)
 73
 74
 75def main():
 76    """
 77    スクリプトのメイン処理を実行します。
 78
 79    この関数は、アプリケーションの初期設定を行い、ログファイルへの出力リダイレクトを設定します。
 80    指定されたCIFファイルを読み込み、その内容を解析し、結晶構造に関する主要な情報を
 81    標準出力およびログファイルに出力します。密度計算なども行います。
 82    """
 83    app = tkApplication()
 84    logfile = app.replace_path(infile, template = ["{dirname}", "{filebody}-out.txt"])
 85    print(f"Open logfile [{logfile}]")
 86    app.redirect(targets = ["stdout", logfile], mode = 'w')
 87
 88    print("")
 89    print("=============== CIF file read ============")
 90    print("infile: {}".format(infile))
 91    print("single: {}".format(single))
 92    print("findvalidstructure: {}".format(findvalidstructure))
 93    print("debug: {}".format(debug))
 94
 95    print("")
 96    print("Read [{}]".format(infile))
 97
 98#    if not tkutils.IsFile(infile):
 99#        terminate("Error: Invalid infile [{}]".format(infile))
100    cif = tkCIF()
101    cif.debug = debug
102    
103    cifdata = cif.ReadCIF(infile, find_valid_structure = findvalidstructure)
104    cif.Close()
105
106    if not cifdata:
107        terminate("Error: Could not get cifdat from infile [{}]".format(infile))
108
109    cifdata.Print()
110    cry = cifdata.GetCrystal()
111    print("")
112    print("==============================================")
113    cry.PrintInf()
114    print("")
115    print("==============================================")
116            
117    d = cry.Density()
118    ad = cry.AtomDensity()
119    
120
121    terminate()
122
123
124if __name__ == "__main__":
125    main()