File: scale.py

package info (click to toggle)
avogadrolibs 1.93.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,388 kB
  • sloc: cpp: 98,107; ansic: 2,215; python: 954; perl: 321; sh: 154; makefile: 37
file content (86 lines) | stat: -rwxr-xr-x 2,436 bytes parent folder | download
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
#!/usr/bin/python3
"""
 This source file is part of the Avogadro project.
 This source code is released under the New BSD License, (the "License").
"""

import argparse
import json
import sys

# Some globals:
debug = True


def getOptions():
    userOptions = {}

    userOptions['X Scale'] = {}
    userOptions['X Scale']['type'] = 'float'
    userOptions['X Scale']['default'] = 1.0
    userOptions['X Scale']['precision'] = 3
    userOptions['X Scale']['toolTip'] = 'Multiplier for X coordinates'

    userOptions['Y Scale'] = {}
    userOptions['Y Scale']['type'] = 'float'
    userOptions['Y Scale']['default'] = 1.0
    userOptions['Y Scale']['precision'] = 3
    userOptions['Y Scale']['toolTip'] = 'Multiplier for Y coordinates'

    userOptions['Z Scale'] = {}
    userOptions['Z Scale']['type'] = 'float'
    userOptions['Z Scale']['default'] = 1.0
    userOptions['Z Scale']['precision'] = 3
    userOptions['Z Scale']['toolTip'] = 'Multiplier for Z coordinates'

    opts = {'userOptions': userOptions}

    return opts


def scale(opts, mol):
    xScale = float(opts['X Scale'])
    yScale = float(opts['Y Scale'])
    zScale = float(opts['Z Scale'])

    coords = mol['atoms']['coords']['3d']
    for i in range(0, len(coords), 3):
        coords[i] = coords[i] * xScale
        coords[i + 1] = coords[i + 1] * yScale
        coords[i + 2] = coords[i + 2] * zScale

    return mol


def runCommand():
    # Read options from stdin
    stdinStr = sys.stdin.read()

    # Parse the JSON strings
    opts = json.loads(stdinStr)

    # Prepare the result
    result = {}
    result['cjson'] = scale(opts, opts['cjson'])
    return result

if __name__ == "__main__":
    parser = argparse.ArgumentParser('Scale molecular coordinates.')
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--print-options', action='store_true')
    parser.add_argument('--run-command', action='store_true')
    parser.add_argument('--display-name', action='store_true')
    parser.add_argument('--menu-path', action='store_true')
    parser.add_argument('--lang', nargs='?', default='en')
    args = vars(parser.parse_args())

    debug = args['debug']

    if args['display_name']:
        print("Scale Coordinates...")
    if args['menu_path']:
        print("&Extensions")
    if args['print_options']:
        print(json.dumps(getOptions()))
    elif args['run_command']:
        print(json.dumps(runCommand()))