forked from diffpy/diffpy.structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranstru.py
More file actions
executable file
·129 lines (107 loc) · 3.62 KB
/
transtru.py
File metadata and controls
executable file
·129 lines (107 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
##############################################################################
#
# diffpy.structure by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2006 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Translate structure file to different format.
Usage: ``transtru INFMT..OUTFMT strufile``
Translates structure file strufile from `INFMT` to `OUTFMT` format and prints it
to the screen. Use "-" as `strufile` to read from standard input. To save the
translated file, use
``transtru INFMT..OUTFMT strufile > strufile.out``
Supported input and output structure formats are
* `INFMT`: ``inputFormats``
* `OUTFMT`: ``outputFormats``
Options:
-h, --help
Display this message.
-V, --version
Show script version.
"""
from __future__ import print_function
import sys
from diffpy.structure import Structure
from diffpy.structure.structureerrors import StructureFormatError
def usage(style=None):
"""Show usage info, for ``style=="brief"`` show only first 2
lines."""
import os.path
myname = os.path.basename(sys.argv[0])
msg = __doc__.replace("transtru", myname)
if style == "brief":
msg = msg.split("\n")[1] + "\n" + "Try `%s --help' for more information." % myname
else:
from diffpy.structure.parsers import inputFormats, outputFormats
msg = msg.replace("inputFormats", " ".join(inputFormats()))
msg = msg.replace("outputFormats", " ".join(outputFormats()))
print(msg)
return
def version():
from diffpy.structure import __version__
print("diffpy.structure", __version__)
return
def main():
import getopt
# default parameters
try:
opts, args = getopt.getopt(sys.argv[1:], "hV", ["help", "version"])
except getopt.GetoptError as errmsg:
print(errmsg, file=sys.stderr)
sys.exit(2)
# process options
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-V", "--version"):
version()
sys.exit()
if len(args) < 1:
usage("brief")
sys.exit()
# process arguments
from diffpy.structure.parsers import inputFormats, outputFormats
try:
infmt, outfmt = args[0].split("..", 1)
if infmt not in inputFormats():
print("'%s' is not valid input format" % infmt, file=sys.stderr)
sys.exit(2)
if outfmt not in outputFormats():
print("'%s' is not valid output format" % outfmt, file=sys.stderr)
sys.exit(2)
except ValueError:
print(
"invalid format specification '%s' does not contain .." % args[0],
file=sys.stderr,
)
sys.exit(2)
# ready to do some real work
try:
strufile = args[1]
stru = Structure()
if args[1] == "-":
stru.read_structure(sys.stdin.read(), infmt)
else:
stru.read(strufile, infmt)
sys.stdout.write(stru.write_structure(outfmt))
except IndexError:
print("strufile not specified", file=sys.stderr)
sys.exit(2)
except IOError as e:
print("%s: %s" % (strufile, e.strerror), file=sys.stderr)
sys.exit(1)
except StructureFormatError as e:
print("%s: %s" % (strufile, e), file=sys.stderr)
sys.exit(1)
return
if __name__ == "__main__":
main()