Loading [MathJax]/extensions/tex2jax.js
LSST Applications g04a91732dc+7fec47d7bc,g07dc498a13+5ab4d22ec3,g0fba68d861+565de8e5d5,g1409bbee79+5ab4d22ec3,g1a7e361dbc+5ab4d22ec3,g1fd858c14a+11200c7927,g20f46db602+25d63fd678,g35bb328faa+fcb1d3bbc8,g4d2262a081+61302e889d,g4d39ba7253+d05e267ece,g4e0f332c67+5d362be553,g53246c7159+fcb1d3bbc8,g60b5630c4e+d05e267ece,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8048e755c2+a1301e4c20,g8852436030+163ceb82d8,g89139ef638+5ab4d22ec3,g89e1512fd8+fbb808ebce,g8d6b6b353c+d05e267ece,g9125e01d80+fcb1d3bbc8,g989de1cb63+5ab4d22ec3,g9f33ca652e+8abe617c77,ga9baa6287d+d05e267ece,gaaedd4e678+5ab4d22ec3,gabe3b4be73+1e0a283bba,gb1101e3267+fefe9ce5b1,gb58c049af0+f03b321e39,gb90eeb9370+824c420ec4,gc741bbaa4f+77ddc33078,gcf25f946ba+163ceb82d8,gd315a588df+0f88d5218e,gd6cbbdb0b4+c8606af20c,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+e6bd566e97,ge278dab8ac+932305ba37,ge82c20c137+76d20ab76d,w.2025.10
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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

◆ 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.