Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010, 2015 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

## 

## \file 

## \brief Definitions to talk to firefly from python 

 

from __future__ import absolute_import, division, print_function 

from past.builtins import long 

 

from socket import gaierror 

import tempfile 

 

import lsst.afw.display.interface as interface 

import lsst.afw.display.virtualDevice as virtualDevice 

import lsst.afw.display.ds9Regions as ds9Regions 

import lsst.afw.display.displayLib as displayLib 

import lsst.afw.math as afwMath 

import lsst.log 

 

try: 

import firefly_client 

_fireflyClient = None 

except ImportError as e: 

raise RuntimeError("Cannot import firefly_client: %s" % (e)) 

from ws4py.client import HandshakeError 

 

class FireflyError(Exception): 

 

def __init__(self, str): 

Exception.__init__(self, str) 

 

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 

 

 

def firefly_version(): 

"""Return the version of firefly_client in use, as a string""" 

return(firefly_client.__version__) 

 

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 

 

 

class DisplayImpl(virtualDevice.DisplayImpl): 

"""Device to talk to a firefly display""" 

 

@staticmethod 

def __handleCallbacks(event): 

if 'type' in event['data']: 

if event['data']['type'] == 'AREA_SELECT': 

lsst.log.debug('*************area select') 

pParams = {'URL': 'http://web.ipac.caltech.edu/staff/roby/demo/wise-m51-band2.fits', 

'ColorTable': '9'} 

plot_id = 3 

global _fireflyClient 

_fireflyClient.show_fits(fileOnServer=None, plot_id=plot_id, additionalParams=pParams) 

 

lsst.log.debug("Callback event info: {}".format(event)) 

return 

data = dict((_.split('=') for _ in event.get('data', {}).split('&'))) 

if data.get('type') == "POINT": 

lsst.log.debug("Event Received: %s" % data.get('id')) 

 

def __init__(self, display, verbose=False, url=None, 

name=None, *args, **kwargs): 

virtualDevice.DisplayImpl.__init__(self, display, verbose) 

 

86 ↛ 87line 86 didn't jump to line 87, because the condition on line 86 was never true if self.verbose: 

print("Opening firefly device %s" % (self.display.frame if self.display else "[None]")) 

 

global _fireflyClient 

90 ↛ 113line 90 didn't jump to line 113, because the condition on line 90 was never false if not _fireflyClient: 

try: 

92 ↛ 93line 92 didn't jump to line 93, because the condition on line 92 was never true if url is None: 

_fireflyClient = firefly_client.FireflyClient(channel=name, **kwargs) 

else: 

_fireflyClient = firefly_client.FireflyClient(url, 

channel=name, **kwargs) 

except (HandshakeError, gaierror) as e: 

raise RuntimeError("Unable to connect to %s: %s" % (url or '', e)) 

 

global localbrowser 

localbrowser, browser_url = _fireflyClient.launch_browser(verbose=self.verbose) 

if not localbrowser and not self.verbose: 

_fireflyClient.display_url() 

if self.verbose: 

print('localbrowser: ', localbrowser, ' browser_url: ', browser_url) 

try: 

_fireflyClient.add_listener(self.__handleCallbacks) 

except Exception as e: 

raise RuntimeError("Cannot add listener. Browser must be connected" + 

"to %s: %s" % 

(_fireflyClient.get_firefly_url(), e)) 

 

self._isBuffered = False 

self._regions = [] 

self._regionLayerId = None 

self._fireflyFitsID = None 

self._fireflyMaskOnServer = None 

self._client = _fireflyClient 

self._channel = _fireflyClient.channel 

self._url = _fireflyClient.get_firefly_url() 

self._localbrowser = localbrowser 

self._maskIds = [] 

self._maskDict = {} 

 

def _getRegionLayerId(self): 

return "lsstRegions%s" % self.display.frame if self.display else "None" 

 

def _mtv(self, image, mask=None, wcs=None, title=""): 

"""Display an Image and/or Mask on a Firefly display 

""" 

if title == "": 

title = str(self.display.frame) 

if image: 

self._erase() 

 

with tempfile.NamedTemporaryFile() as fd: 

displayLib.writeFitsImage(fd.name, image, wcs, title) 

fd.flush() 

fd.seek(0,0) 

self._fireflyFitsID = _fireflyClient.upload_data(fd, 'FITS') 

 

ret = _fireflyClient.show_fits(self._fireflyFitsID, plot_id=str(self.display.frame), 

Title=title, MultiImageIdx=0) 

if not ret["success"]: 

raise RuntimeError("Display of image failed") 

 

if mask: 

with tempfile.NamedTemporaryFile() as fdm: 

displayLib.writeFitsImage(fdm.name, mask, wcs, title) 

fdm.flush() 

fdm.seek(0,0) 

self._fireflyMaskOnServer = _fireflyClient.upload_data(fdm, 'FITS') 

 

self._maskDict = mask.getMaskPlaneDict() 

usedPlanes = long(afwMath.makeStatistics(mask, afwMath.SUM).getValue()) 

for k in self._maskDict: 

if ((1 << self._maskDict[k]) & usedPlanes): 

_fireflyClient.add_mask(bit_number=self._maskDict[k], 

image_number=0, 

plot_id=str(self.display.frame), 

mask_id=k, 

title=k + ' - bit %d'%self._maskDict[k], 

color=self.display.getMaskPlaneColor(k), 

file_on_server=self._fireflyMaskOnServer) 

self._maskIds.append(k) 

 

def _remove_masks(self): 

"""Remove mask layers""" 

for k in self._maskIds: 

_fireflyClient.remove_mask(plot_id=str(self.display.frame), mask_id=k) 

self._maskIds = [] 

 

def _buffer(self, enable=True): 

"""!Enable or disable buffering of writes to the display 

\param enable True or False, as appropriate 

""" 

self._isBuffered = enable 

 

def _flush(self): 

"""!Flush any I/O buffers 

""" 

if not self._regions: 

return 

 

if self.verbose: 

print("Flushing %d regions" % len(self._regions)) 

 

self._regionLayerId = self._getRegionLayerId() 

_fireflyClient.add_region_data(region_data=self._regions, plot_id=str(self.display.frame), 

region_layer_id=self._regionLayerId) 

self._regions = [] 

 

def _uploadTextData(self, regions): 

self._regions += regions 

 

if not self._isBuffered: 

self._flush() 

 

def _close(self): 

"""Called when the device is closed""" 

201 ↛ 202line 201 didn't jump to line 202, because the condition on line 201 was never true if self.verbose: 

print("Closing firefly device %s" % (self.display.frame if self.display else "[None]")) 

203 ↛ 204line 203 didn't jump to line 204, because the condition on line 203 was never true if _fireflyClient is not None: 

_fireflyClient.disconnect() 

_fireflyClient.session.close() 

 

def _dot(self, symb, c, r, size, ctype, fontFamily="helvetica", textAngle=None): 

"""Draw a symbol onto the specified DS9 frame at (col,row) = (c,r) [0-based coordinates] 

Possible values are: 

+ Draw a + 

x Draw an x 

* Draw a * 

o Draw a circle 

@:Mxx,Mxy,Myy Draw an ellipse with moments (Mxx, Mxy, Myy) (argument size is ignored) 

An object derived from afwGeom.ellipses.BaseCore Draw the ellipse (argument size is ignored) 

Any other value is interpreted as a string to be drawn. Strings obey the fontFamily (which may be extended 

with other characteristics, e.g. "times bold italic". Text will be drawn rotated by textAngle (textAngle 

is ignored otherwise). 

 

N.b. objects derived from BaseCore include Axes and Quadrupole. 

""" 

self._uploadTextData(ds9Regions.dot(symb, c, r, size, ctype, fontFamily, textAngle)) 

 

def _drawLines(self, points, ctype): 

"""Connect the points, a list of (col,row) 

Ctype is the name of a colour (e.g. 'red')""" 

 

self._uploadTextData(ds9Regions.drawLines(points, ctype)) 

 

def _erase(self): 

"""Erase all overlays on the image""" 

if self._regionLayerId: 

_fireflyClient.delete_region_layer(self._regionLayerId, plot_id=str(self.display.frame)) 

self._regionLayerId = None 

 

def _setCallback(self, what, func): 

if func != interface.noop_callback: 

try: 

status = _fireflyClient.add_extension('POINT' if False else 'AREA_SELECT', title=what, 

plot_id=str(self.display.frame), 

extension_id=what) 

if not status['success']: 

pass 

except Exception as e: 

raise RuntimeError("Cannot set callback. Browser must be (re)opened " + 

"to %s%s : %s" % 

(_fireflyClient.url_bw, 

_fireflyClient.channel, e)) 

 

def _getEvent(self): 

"""Return an event generated by a keypress or mouse click 

""" 

ev = interface.Event("q") 

 

if self.verbose: 

print("virtual[%s]._getEvent() -> %s" % (self.display.frame, ev)) 

 

return ev 

# 

# Set gray scale 

# 

 

def _scale(self, algorithm, min, max, unit=None, *args, **kwargs): 

stretch_algorithms = ('linear', 'log', 'loglog', 'equal', 'squared', 'sqrt', 

'asinh', 'powerlaw_gamma') 

interval_methods = ('percent', 'maxmin', 'absolute', 'zscale', 'sigma') 

# 

# 

# Normalise algorithm's case 

# 

if algorithm: 

algorithm = dict((a.lower(), a) for a in stretch_algorithms).get(algorithm.lower(), algorithm) 

 

if algorithm not in stretch_algorithms: 

raise FireflyError('Algorithm %s is invalid; please choose one of "%s"' % 

(algorithm, '", "'.join(stretch_algorithms))) 

self._stretchAlgorithm = algorithm 

else: 

algorithm = 'linear' 

 

if min == 'minmax': 

interval_type = 'percent' 

unit = 'percent' 

min, max = 0, 100 

elif min == 'zscale': 

interval_type = 'zscale' 

else: 

interval_type = None 

 

if not unit: 

unit = 'absolute' 

 

units = ('percent', 'absolute', 'sigma') 

if unit not in units: 

raise FireflyError('Unit %s is invalid; please choose one of "%s"' % (unit, '", "'.join(units))) 

 

if unit == 'sigma': 

interval_type = 'sigma' 

elif unit == 'absolute' and interval_type is None: 

interval_type = 'absolute' 

elif unit == 'percent': 

interval_type = 'percent' 

 

self._stretchMin = min 

self._stretchMax = max 

self._stretchUnit = unit 

 

if interval_type not in interval_methods: 

raise FireflyError('Interval method %s is invalid' % interval_type) 

 

if interval_type is not 'zscale': 

_fireflyClient.set_stretch(str(self.display.frame), stype=interval_type, algorithm=algorithm, 

lower_value=min, upper_value=max) 

else: 

if 'zscale_constrast' not in kwargs: 

kwargs['zscale_contrast'] = 25 

if 'zscale_samples' not in kwargs: 

kwargs['zscale_samples'] = 600 

if 'zscale_samples_perline' not in kwargs: 

kwargs['zscale_samples_perline'] = 120 

_fireflyClient.set_stretch(str(self.display.frame), stype='zscale', algorithm=algorithm, 

zscale_contrast=kwargs['zscale_contrast'], 

zscale_samples=kwargs['zscale_samples'], 

zscale_samples_perline=kwargs['zscale_samples_perline']) 

 

def _setMaskTransparency(self, transparency, maskplane): 

"""Specify mask transparency (percent); or None to not set it when loading masks""" 

if maskplane is not None: 

masklist = [maskplane] 

else: 

masklist = self._maskIds 

for k in masklist: 

_fireflyClient.dispatch_remote_action(channel=_fireflyClient.channel, 

action_type='ImagePlotCntlr.overlayPlotChangeAttributes', 

payload={'plotId': str(self.display.frame), 

'imageOverlayId': k, 

'attributes': {'opacity': transparency/100.}, 

'doReplot': False}) 

 

def _getMaskTransparency(self, maskplane): 

"""Return the current mask's transparency""" 

 

pass 

 

def _setMaskPlaneColor(self, maskplane, color): 

"""Specify mask color """ 

_fireflyClient.remove_mask(plot_id=str(self.display.frame), 

mask_id=maskplane) 

if (color != 'ignore'): 

_fireflyClient.add_mask(bit_number=self._maskDict[maskplane], 

image_number=1, 

plot_id=str(self.display.frame), 

mask_id=maskplane, 

color=self.display.getMaskPlaneColor(maskplane), 

file_on_server=self._fireflyFitsID) 

 

def _show(self): 

"""Show the requested window""" 

localbrowser, url = _fireflyClient.launch_browser(verbose=self.verbose) 

if not localbrowser and not self.verbose: 

_fireflyClient.display_url() 

# 

# Zoom and Pan 

# 

 

def _zoom(self, zoomfac): 

"""Zoom frame by specified amount""" 

 

_fireflyClient.set_zoom(plot_id=str(self.display.frame), factor=zoomfac) 

 

def _pan(self, colc, rowc): 

_fireflyClient.set_pan(plot_id=str(self.display.frame), x=colc, y=rowc)