LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+bd2ed33bd6,g1470d8bcf6+de7501a2e0,g14a832a312+ff425fae3c,g2079a07aa2+86d27d4dc4,g2305ad1205+91a32aca49,g295015adf3+762506a1ad,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+c34e8be1fa,g487adcacf7+5fae3daba8,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+ea1711114f,g5a732f18d5+53520f316c,g64a986408d+bd2ed33bd6,g858d7b2824+bd2ed33bd6,g8a8a8dda67+585e252eca,g99cad8db69+016a06b37a,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+ef4e3a5875,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+09e12c87ab,gc120e1dc64+bc2e06c061,gc28159a63d+0e5473021a,gcf0d15dbbd+c34e8be1fa,gdaeeff99f8+f9a426f77a,ge6526c86ff+508d0e0a30,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf18bd8381d+8d59551888,gf1cff7945b+bd2ed33bd6,w.2024.16
LSST Data Management Base Package
Loading...
Searching...
No Matches
Classes | Functions | Variables
lsst.display.ds9.ds9 Namespace Reference

Classes

class  Buffer
 
class  DisplayImpl
 
class  Ds9Error
 
class  Ds9Event
 

Functions

 getXpaAccessPoint ()
 
 ds9Version ()
 
 selectFrame (frame)
 
 ds9Cmd (cmd=None, trap=True, flush=False, silent=True, frame=None, get=False)
 
 initDS9 (execDs9=True)
 
 _i_mtv (data, wcs, title, isMask)
 

Variables

 file
 
bool needShow = True
 
 _maskTransparency = None
 
int XPA_SZ_LINE = 4096 - 100
 
 cmdBuffer
 
 haveGzip = not os.system("gzip < /dev/null > /dev/null 2>&1")
 

Function Documentation

◆ _i_mtv()

lsst.display.ds9.ds9._i_mtv ( data,
wcs,
title,
isMask )
protected
Internal routine to display an image or a mask on a DS9 display.

Parameters
----------
data : Subclass of `lsst.afw.image.Image` or `lsst.afw.image.Mask`
    Data to display.
wcs : `lsst.afw.geom.SkyWcs`
    WCS of data.
title : `str`
    Title of display.
isMask : `bool`
    Is ``data`` a mask?

Definition at line 641 of file ds9.py.

641def _i_mtv(data, wcs, title, isMask):
642 """Internal routine to display an image or a mask on a DS9 display.
643
644 Parameters
645 ----------
646 data : Subclass of `lsst.afw.image.Image` or `lsst.afw.image.Mask`
647 Data to display.
648 wcs : `lsst.afw.geom.SkyWcs`
649 WCS of data.
650 title : `str`
651 Title of display.
652 isMask : `bool`
653 Is ``data`` a mask?
654 """
655 title = str(title) if title else ""
656
657 if isMask:
658 xpa_cmd = f"xpaset {getXpaAccessPoint()} fits mask"
659 # ds9 mis-handles BZERO/BSCALE in uint16 data.
660 # The following hack works around this.
661 # This is a copy we're modifying
662 if data.getArray().dtype == np.uint16:
663 data |= 0x8000
664 else:
665 xpa_cmd = f"xpaset {getXpaAccessPoint()} fits"
666
667 if haveGzip:
668 xpa_cmd = "gzip | " + xpa_cmd
669
670 pfd = os.popen(xpa_cmd, "w")
671
672 ds9Cmd(flush=True, silent=True)
673
674 try:
675 afwDisplay.writeFitsImage(pfd.fileno(), data, wcs, title)
676 except Exception as e:
677 try:
678 pfd.close()
679 except Exception:
680 pass
681
682 raise e
683
684 try:
685 pfd.close()
686 except Exception:
687 pass

◆ ds9Cmd()

lsst.display.ds9.ds9.ds9Cmd ( cmd = None,
trap = True,
flush = False,
silent = True,
frame = None,
get = False )
Issue a DS9 command, raising errors as appropriate.

Parameters
----------
cmd : `str`, optional
    Command to execute.
trap : `bool`, optional
    Trap errors.
flush : `bool`, optional
    Flush the output.
silent : `bool`, optional
    Do not print trapped error messages.
frame : `int`, optional
    Frame number on which to execute command.
get : `bool`, optional
    Return xpa response.

Definition at line 226 of file ds9.py.

226def ds9Cmd(cmd=None, trap=True, flush=False, silent=True, frame=None, get=False):
227 """Issue a DS9 command, raising errors as appropriate.
228
229 Parameters
230 ----------
231 cmd : `str`, optional
232 Command to execute.
233 trap : `bool`, optional
234 Trap errors.
235 flush : `bool`, optional
236 Flush the output.
237 silent : `bool`, optional
238 Do not print trapped error messages.
239 frame : `int`, optional
240 Frame number on which to execute command.
241 get : `bool`, optional
242 Return xpa response.
243 """
244
245 global cmdBuffer
246 if cmd:
247 if frame is not None:
248 cmd = f"{selectFrame(frame)};{cmd}"
249
250 if get:
251 return xpa.get(None, getXpaAccessPoint(), cmd, "").strip()
252
253 # Work around xpa's habit of silently truncating long lines; the value
254 # ``5`` provides some margin to handle new lines and the like.
255 if cmdBuffer._lenCommands + len(cmd) > XPA_SZ_LINE - 5:
256 ds9Cmd(flush=True, silent=silent)
257
258 cmdBuffer._commands += ";" + cmd
259 cmdBuffer._lenCommands += 1 + len(cmd)
260
261 if flush or cmdBuffer._lenCommands >= cmdBuffer._getSize():
262 cmd = (cmdBuffer._commands + "\n")
263 cmdBuffer._commands = ""
264 cmdBuffer._lenCommands = 0
265 else:
266 return
267
268 cmd = cmd.rstrip()
269 if not cmd:
270 return
271
272 try:
273 ret = xpa.set(None, getXpaAccessPoint(), cmd, "", "", 0)
274 if ret:
275 raise OSError(ret)
276 except OSError as e:
277 if not trap:
278 raise Ds9Error(f"XPA: {e}, ({cmd})")
279 elif not silent:
280 print(f"Caught ds9 exception processing command \"{cmd}\": {e}", file=sys.stderr)
281
282
bool strip
Definition fits.cc:930

◆ ds9Version()

lsst.display.ds9.ds9.ds9Version ( )
Get the version of DS9 in use.

Returns
-------
version : `str`
    Version of DS9 in use.

Definition at line 90 of file ds9.py.

90def ds9Version():
91 """Get the version of DS9 in use.
92
93 Returns
94 -------
95 version : `str`
96 Version of DS9 in use.
97 """
98 try:
99 v = ds9Cmd("about", get=True)
100 return v.splitlines()[1].split()[1]
101 except Exception as e:
102 print(f"Error reading version: {e}", file=sys.stderr)
103 return "0.0.0"
104
105

◆ getXpaAccessPoint()

lsst.display.ds9.ds9.getXpaAccessPoint ( )
Parse XPA_PORT if set and return an identifier to send DS9 commands.

Returns
-------

xpaAccessPoint : `str`
    Either a reference to the local host with the configured port, or the
    string ``"ds9"``.

Notes
-----
If you don't have XPA_PORT set, the usual xpans tricks will be played
when we return ``"ds9"``.

Definition at line 62 of file ds9.py.

62def getXpaAccessPoint():
63 """Parse XPA_PORT if set and return an identifier to send DS9 commands.
64
65 Returns
66 -------
67
68 xpaAccessPoint : `str`
69 Either a reference to the local host with the configured port, or the
70 string ``"ds9"``.
71
72 Notes
73 -----
74 If you don't have XPA_PORT set, the usual xpans tricks will be played
75 when we return ``"ds9"``.
76 """
77 xpa_port = os.environ.get("XPA_PORT")
78 if xpa_port:
79 mat = re.search(r"^DS9:ds9\s+(\d+)\s+(\d+)", xpa_port)
80 if mat:
81 port1, port2 = mat.groups()
82
83 return f"127.0.0.1:{port1}"
84 else:
85 print(f"Failed to parse XPA_PORT={xpa_port}", file=sys.stderr)
86
87 return "ds9"
88
89

◆ initDS9()

lsst.display.ds9.ds9.initDS9 ( execDs9 = True)
Initialize DS9.

Parameters
----------
execDs9 : `bool`, optional
    If DS9 is not running, attempt to execute it.

Definition at line 283 of file ds9.py.

283def initDS9(execDs9=True):
284 """Initialize DS9.
285
286 Parameters
287 ----------
288 execDs9 : `bool`, optional
289 If DS9 is not running, attempt to execute it.
290 """
291 try:
292 xpa.reset()
293 ds9Cmd("iconify no; raise", False)
294 ds9Cmd("wcs wcsa", False) # include the pixel coordinates WCS (WCSA)
295
296 v0, v1 = ds9Version().split('.')[0:2]
297 global needShow
298 needShow = False
299 try:
300 if int(v0) == 5:
301 needShow = (int(v1) <= 4)
302 except Exception:
303 pass
304 except Ds9Error as e:
305 if not re.search('xpa', os.environ['PATH']):
306 raise Ds9Error('You need the xpa binaries in your path to use ds9 with python')
307
308 if not execDs9:
309 raise Ds9Error
310
311 if not shutil.which("ds9"):
312 raise NameError("ds9 doesn't appear to be on your path")
313 if "DISPLAY" not in os.environ:
314 raise RuntimeError("$DISPLAY isn't set, so I won't be able to start ds9 for you")
315
316 print(f"ds9 doesn't appear to be running ({e}), I'll try to exec it for you")
317
318 os.system('ds9 &')
319 for i in range(10):
320 try:
321 ds9Cmd(selectFrame(1), False)
322 break
323 except Ds9Error:
324 print("waiting for ds9...\r", end="")
325 sys.stdout.flush()
326 time.sleep(0.5)
327 else:
328 print(" \r", end="")
329 break
330
331 sys.stdout.flush()
332
333 raise Ds9Error
334
335

◆ selectFrame()

lsst.display.ds9.ds9.selectFrame ( frame)
Convert integer frame number to DS9 command syntax.

Parameters
----------
frame : `int`
    Frame number

Returns
-------
frameString : `str`

Definition at line 211 of file ds9.py.

211def selectFrame(frame):
212 """Convert integer frame number to DS9 command syntax.
213
214 Parameters
215 ----------
216 frame : `int`
217 Frame number
218
219 Returns
220 -------
221 frameString : `str`
222 """
223 return f"frame {frame}"
224
225

Variable Documentation

◆ _maskTransparency

lsst.display.ds9.ds9._maskTransparency = None
protected

Definition at line 59 of file ds9.py.

◆ cmdBuffer

lsst.display.ds9.ds9.cmdBuffer

Definition at line 208 of file ds9.py.

◆ file

lsst.display.ds9.ds9.file

Definition at line 40 of file ds9.py.

◆ haveGzip

lsst.display.ds9.ds9.haveGzip = not os.system("gzip < /dev/null > /dev/null 2>&1")

Definition at line 638 of file ds9.py.

◆ needShow

bool lsst.display.ds9.ds9.needShow = True

Definition at line 48 of file ds9.py.

◆ XPA_SZ_LINE

int lsst.display.ds9.ds9.XPA_SZ_LINE = 4096 - 100

Definition at line 110 of file ds9.py.