LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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.