Skip to content

Commit 7591e43

Browse files
committed
Add table input option
1 parent 35f81d4 commit 7591e43

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

Diff for: src/diffpy/morph/morphapp.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,30 @@ def custom_error(self, msg):
406406
def single_morph(parser, opts, pargs, stdout_flag=True, python_wrap=False):
407407
if len(pargs) < 2:
408408
parser.error("You must supply FILE1 and FILE2.")
409-
elif len(pargs) > 2:
409+
elif len(pargs) > 2 and not python_wrap:
410410
parser.error(
411411
"Too many arguments. Make sure you only supply FILE1 and FILE2."
412412
)
413+
elif len(pargs) != 6 and not python_wrap:
414+
parser.error(
415+
"Python wrapper error."
416+
)
413417

414418
# Get the PDFs
415-
x_morph, y_morph = getPDFFromFile(pargs[0])
416-
x_target, y_target = getPDFFromFile(pargs[1])
419+
# If we get from python, we may wrap, which has input size 4
420+
if len(pargs) == 6 and python_wrap:
421+
x_morph = pargs[1]
422+
y_morph = pargs[2]
423+
x_target = pargs[3]
424+
y_target = pargs[4]
425+
else:
426+
x_morph, y_morph = getPDFFromFile(pargs[0])
427+
x_target, y_target = getPDFFromFile(pargs[1])
417428

418429
if y_morph is None:
419-
parser.error(f"No data table found in file: {pargs[0]}.")
430+
parser.error(f"No data table found in: {pargs[0]}.")
420431
if y_target is None:
421-
parser.error(f"No data table found in file: {pargs[1]}.")
432+
parser.error(f"No data table found in: {pargs[1]}.")
422433

423434
# Get configuration values
424435
scale_in = "None"

Diff for: src/diffpy/morph/morphpy.py

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
import numpy as np
34
from diffpy.morph.morphapp import (
45
create_option_parser,
56
multiple_morphs,
@@ -8,14 +9,14 @@
89
)
910

1011

11-
def morph(file1, file2, **kwargs):
12+
def morph(morph_file, target_file, **kwargs):
1213
"""Run diffpy.morph at Python level.
1314
1415
Parameters
1516
----------
16-
file1: str
17+
morph_file: str
1718
Path-like object to the file to be morphed.
18-
file2: str
19+
target_file: str
1920
Path-like object to the target file.
2021
kwargs: dict
2122
See the diffpy.morph manual for options.
@@ -33,7 +34,53 @@ def morph(file1, file2, **kwargs):
3334
inputs.append(f"--{key}")
3435
inputs.append(f"{value}")
3536
(opts, pargs) = parser.parse_args(inputs)
36-
pargs = [file1, file2]
37+
pargs = [morph_file, target_file]
38+
39+
return single_morph(
40+
parser, opts, pargs, stdout_flag=False, python_wrap=True
41+
)
42+
43+
44+
def morphpy(morph_table, target_table, morph_header, target_header, **kwargs):
45+
"""Run diffpy.morph at Python level.
46+
47+
Parameters
48+
----------
49+
morph_table: numpy.array
50+
Two-column array of (r, gr) for morphed function.
51+
target_table: numpy.array
52+
Two-column array of (r, gr) for target function.
53+
morph_header: dict
54+
Any relevant parameters (e.g. wavelength, composition, temperature)
55+
for the morphed function.
56+
target_header: dict
57+
Any relevant parameters for the target ction.
58+
kwargs: dict
59+
See the diffpy.morph manual for options.
60+
61+
Returns
62+
-------
63+
dict:
64+
Summary of morphs.
65+
"""
66+
67+
parser = create_option_parser()
68+
69+
inputs = []
70+
for key, value in kwargs.items():
71+
inputs.append(f"--{key}")
72+
inputs.append(f"{value}")
73+
(opts, pargs) = parser.parse_args(inputs)
74+
75+
morph_table = np.array(morph_table)
76+
target_table = np.array(target_table)
77+
78+
x_morph = morph_table[:, 0]
79+
y_morph = morph_table[:, 1]
80+
x_target = target_table[:, 0]
81+
y_target = target_table[:, 1]
82+
83+
pargs = ["Morph", "Target", x_morph, y_morph, x_target, y_target]
3784

3885
return single_morph(
3986
parser, opts, pargs, stdout_flag=False, python_wrap=True

0 commit comments

Comments
 (0)