Skip to content

Commit 1e75e49

Browse files
authored
Merge pull request #193 from iraf-community/issue192
Fix the use of Pset arguments when calling a task in PyRAF CL
2 parents bbc2061 + 2595ffd commit 1e75e49

File tree

12 files changed

+264
-3
lines changed

12 files changed

+264
-3
lines changed

.github/workflows/citest.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
- name: Ubuntu 24.04 (Python-3.12.3), native
1919
os: ubuntu-24.04
2020
method: native
21+
iraf: /usr/lib/iraf/
2122
upload_coverage: yes
2223

2324
- name: Ubuntu 24.04 (Python-3.12.3), native, without IRAF
@@ -26,6 +27,7 @@ jobs:
2627

2728
- name: Ubuntu 22.04 (Python-3.10), pip
2829
os: ubuntu-22.04
30+
iraf: /usr/lib/iraf/
2931
method: pip
3032

3133
- name: macOS 26, pip
@@ -37,11 +39,11 @@ jobs:
3739
if: matrix.method == 'native'
3840
uses: actions/checkout@v6
3941

40-
- name: Setup dependencies
42+
- name: Setup dependencies, Ubuntu/native
4143
if: startsWith(matrix.os, 'ubuntu') && matrix.method == 'native'
4244
run: |
4345
sudo apt-get update
44-
if [ "$iraf" ]; then
46+
if [ "${{ matrix.iraf }}" ]; then
4547
sudo apt-get install --no-install-recommends iraf iraf-noao iraf-dev
4648
else
4749
echo "PYRAF_NO_IRAF=yes" >> $GITHUB_ENV
@@ -50,7 +52,7 @@ jobs:
5052
sudo apt-get install --no-install-recommends python3-dev python3-pip python3-astropy python3-numpy-dev python3-setuptools python3-setuptools-scm python3-tk python3-pytest python3-pytest-cov ipython3
5153
pip3 install "stsci.tools>=4.0.1" coveragepy
5254
53-
- name: Setup dependencies
55+
- name: Setup dependencies, Ubuntu/pip
5456
if: startsWith(matrix.os, 'ubuntu') && matrix.method == 'pip'
5557
run: |
5658
sudo apt-get update

pyraf/irafpar.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,18 @@ def setParList(self, *args, **kw):
10381038
p = p.get().getParObject(tail)
10391039
# what if *this* p is a IrafParPset ? skip for now,
10401040
# since we think no one is doubly nesting PSETs
1041+
1042+
# The original parameter in the pardict is not linked
1043+
# to the child Pset, so we need to retrieve the
1044+
# pardict one.
1045+
p.set(value)
1046+
p.setFlags(_cmdlineFlag)
1047+
try:
1048+
p = self.getParObject(p.name)
1049+
except KeyError:
1050+
p = copy.deepcopy(p)
1051+
self.addParam(p)
1052+
10411053
p.set(value)
10421054
p.setFlags(_cmdlineFlag)
10431055
if p.mode != "h":

pyraf/tests/test_cl_param.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import os
2+
from pathlib import Path
3+
from io import StringIO
4+
import shutil
5+
import pytest
6+
7+
from .utils import HAS_IRAF
8+
9+
from pyraf import iraf
10+
11+
try:
12+
from iraf import softools
13+
except ImportError:
14+
pytest.skip("IRAF softools not available", allow_module_level=True)
15+
16+
17+
@pytest.fixture(scope="session", autouse=True)
18+
def setup_testpkg(tmp_path_factory):
19+
"""Copy the test package to a temporary dir, compile the SPP tasks and
20+
load the package
21+
22+
"""
23+
base = tmp_path_factory.mktemp("iraf")
24+
for fname in (Path(__file__).parent / "testpkg").iterdir():
25+
shutil.copy(fname, base)
26+
iraf.set(testpkg=str(base) + "/")
27+
cwd = Path.cwd()
28+
os.chdir(base)
29+
try:
30+
iraf.xc("spppars.x")
31+
finally:
32+
os.chdir(cwd)
33+
iraf.task(testpkgDOTpkg="testpkg$testpkg.cl")
34+
from iraf import testpkg
35+
36+
yield
37+
38+
39+
def showpars(taskname, **kwargs):
40+
"""Run the task with given parameters
41+
42+
Return the output as a dictionary of key-value pairs with proper
43+
type for the value
44+
45+
"""
46+
task = iraf.module.getTask(taskname)
47+
params = task.getParDict()
48+
stdout = StringIO()
49+
task(StdoutAppend=stdout, **kwargs)
50+
stdout.seek(0)
51+
res = {}
52+
for l in stdout.read().splitlines():
53+
key, val = l.split("=", 1)
54+
res[key] = params[key].checkValue(val)
55+
return res
56+
57+
58+
@pytest.mark.parametrize(
59+
"name,value",
60+
[
61+
("strpar", "foo.fits"),
62+
("intpar", 2),
63+
("intpar", -2),
64+
("fltpar", 2.0),
65+
("fltpar", 2.0e1),
66+
("fltpar", 2),
67+
("boolpar", True),
68+
("boolpar", 1),
69+
],
70+
)
71+
@pytest.mark.parametrize("task", ["clpars", "simplepars"])
72+
def test_simplepars(task, name, value):
73+
"""Check that simple parameters work for CL and SPP tasks"""
74+
ref = showpars(task)
75+
shortname = name.split(".", 1)[-1]
76+
ref[shortname] = value
77+
assert ref == showpars(task, **{name: value})
78+
79+
80+
@pytest.mark.parametrize(
81+
"name,value",
82+
[
83+
("psetpar.psintpar", 3),
84+
("psintpar", 3),
85+
("psetpar.psstrpar", "bar"),
86+
("psstrpar", "bar"),
87+
],
88+
)
89+
@pytest.mark.parametrize("task", ["clpars", "psetpars0", "psetpars1"])
90+
def test_pset(task, name, value):
91+
"""Check that parameter sets for for CL and SPP tasks"""
92+
ref = showpars(task)
93+
shortname = name.split(".", 1)[-1]
94+
ref[shortname] = value
95+
assert ref == showpars(task, **{name: value})
96+
97+
98+
@pytest.mark.xfail(reason="Not implemented in PyRAF")
99+
@pytest.mark.parametrize("task", ["clpars", "psetpars0", "psetpars1"])
100+
def test_pars_other_pset(task):
101+
"""Check that setting another parameter set works in CL and SPP tasks"""
102+
ref = showpars(task)
103+
ref["psstrpar"] = "foobar"
104+
ref["psintpar"] = -1
105+
assert ref == showpars(task, psetpar="psetpar1")
106+
107+
108+
@pytest.mark.parametrize(
109+
"name,value",
110+
[
111+
("intpar", "-20"),
112+
("intpar", "20"),
113+
("intpar", "foo"),
114+
("fltpar", "-100.5"),
115+
("fltpar", "1e3"),
116+
("fltpar", "foo"),
117+
("boolpar", "foo"),
118+
("boolpar", -99),
119+
],
120+
)
121+
@pytest.mark.parametrize("task", ["clpars", "simplepars"])
122+
def test_pars_invalid_value(task, name, value):
123+
"""Check that invalid values raise a ValueError"""
124+
with pytest.raises(ValueError):
125+
showpars(task, **{name: value})
126+
127+
128+
@pytest.mark.parametrize(
129+
"name,value",
130+
[
131+
("invalidpar", "foo"),
132+
("psetpar", "foo"),
133+
],
134+
)
135+
@pytest.mark.parametrize("task", ["clpars", "simplepars"])
136+
def test_pars_invalid_name(task, name, value):
137+
"""Check that unknown parameters raise a KeyError"""
138+
with pytest.raises(KeyError):
139+
showpars(task, **{name: value})

pyraf/tests/testpkg/clpars.cl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Procedure clpars: CL script to just print the parameters
2+
procedure clpars(strpar, intpar, fltpar, boolpar, psetpar)
3+
begin
4+
print ("strpar=", strpar)
5+
print ("intpar=", intpar)
6+
print ("fltpar=", fltpar)
7+
print ("boolpar=", boolpar)
8+
print ("psstrpar=", psetpar.psstrpar)
9+
print ("psintpar=", psetpar.psintpar)
10+
end

pyraf/tests/testpkg/clpars.par

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
strpar,s,h,"unset",,,"String parameter"
2+
intpar,i,h,1,-10,10,"Integer parameter"
3+
fltpar,r,h,1.0,-100,100,"Floating point parameter"
4+
boolpar,b,h,no,,,"Boolean parameter"
5+
psetpar,pset,h,,,,"Parameter set (pset)"

pyraf/tests/testpkg/psetpar.par

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
psstrpar,s,h,"",,,"String par in pset"
2+
psintpar,i,h,0,,,"Integer par in pset"

pyraf/tests/testpkg/psetpar1.par

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
psstrpar,s,h,"foobar",,,"String par in pset"
2+
psintpar,i,h,-1,,,"Integer par in pset"

pyraf/tests/testpkg/psetpars0.par

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
simple,b,h,no,,,"Use simplified parameter set access"
2+
psetpar,pset,h,,,,"Parameter set (pset)"

pyraf/tests/testpkg/psetpars1.par

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
psetpars0.par

pyraf/tests/testpkg/simplepars.par

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
strpar,s,h,"foo",,,"String parameter"
2+
intpar,i,h,1,-10,10,"Integer parameter"
3+
fltpar,r,h,1.0,-100,100,"Floating point parameter"
4+
boolpar,b,h,no,,,"Boolean parameter"
5+

0 commit comments

Comments
 (0)