-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfieldplot.py
More file actions
executable file
·597 lines (433 loc) · 17.5 KB
/
fieldplot.py
File metadata and controls
executable file
·597 lines (433 loc) · 17.5 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/usr/bin/env python
'''
Toy program to demo a query of the Data Lab TAP service and visualize
the object density and associated CMD. The user moves the cursor in the
left-hand plot, the smaller plots track to show the catalog points zoomed
in around the cursor, and the g-r vs g CMD for the points around the cursor,
and a histogram of the differential values.
A reticle in the zoomed position plot shows the size of the sampling cursor
being used. A mouseover in the CMD plot will show the corresponding point
in the density plot with a highlight marker. Holding the Shift key down
will suspend tracking to allow you to move the cursor to another window
without changing the zoom/CMD display.
A box may be dragged out in the CMD plot to select points corresponding to
specific color/magnitude region, the density plot will automatically be
updated. Similarly, the contrast of the density plot can be adjusted by
selecting a horizontal region of the histogram plot, e.g. to show only the
values 3-sigma above the mean drag the cursor between zero (the difference
from the mean) and 3 (3-sigma above the mean).
Command-line options:
-h, --help # Help
-d, --debug # Debug
-v, --verbose # Verbose
-c, --catalog # Catalog to query
-l, --load # Load the FITS bintable
-p, --ra # Set RA position
-r, --dec # Set DEC position
-s, --sz # Set field size
-S, --small_k # Set small kernel size
-B, --big_k # Set big kernel size
-F, --floor # Set clipping floor (sigma)
-C, --ceiling # Set clipping ceiling (sigma)
Additional work to pre-fetch/cache data, or use of different density tools
will help speed and precision but is TBD.
MJF (2/15/16)
'''
import matplotlib.pyplot as plt
import numpy as np
from gavo import votable
from lxml import etree
import psycopg2 # for database connectivity
import psycopg2.extras
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
#from matplotlib.widgets import Button, RadioButtons, CheckButtons
#from matplotlib.widgets import Slider
#from matplotlib.widgets import Cursor
#from matplotlib.widgets import RectangleSelector
#from matplotlib.widgets import SpanSelector
from astropy.io import fits
from astropy import convolution
from astropy.table import Table
#import matplotlib.patches as patches
#import mpld3
import sys, time, math, getopt
################################################
# Some interesting initial positions
################################################
xpos = 0
ypos = 0
xsize = 2.5 # initial field size in degrees
ysize = 2.5
################################################
# Globals
################################################
ra0 = None # Entire dataset
dec0 = None
g0 = None
gr0 = None
ra_idx = None
dec_idx = None
fname = None # file name to load
field = 169 # Hydra II
small_k = 1.0 # Differential kernel parameters
big_k = 6.0
floor = 2.0
ceiling = 999.0
use_tap = 0
dmap = None # resultant density map
cursz = 5.0 # mouseover cursor size (degrees)
# Data Lab TAP Service endpoint
accessURL = "http://zeus1.sdm.noao.edu:8080/ivoa-dal/tap"
# Direct database access connection
#conn_str = "host='localhost' dbname='tapdb' user='dlquery' password=''"
conn_str = "host='zeus1.sdm.noao.edu' dbname='tapdb' user='dlquery' password=''"
# Default catalog
catalog = 'smash.blue_stars'
# GETDATA -- Retrieve the data via TAP and put into a numpy array
def getData (field, catalog='smash.blue_stars'):
global xpos, ypos, xsize, ysize, use_tap
# Template query string based on (xpos,ypos) position.
query = ('select ra_j2000,dec_j2000,gmag,rmag from '+ catalog + \
' where (fid = \'%d\' AND' \
' (gmag is not null) and ' + \
' (gmag between 10 and 23) and ((gmag-rmag) between -1.0 and 1.0))') % \
field
print "Getting data ....",
sys.stdout.flush()
start_time = time.time()
if (use_tap == 1):
raw = votable.ADQLSyncJob (accessURL, query).run().openResult()
# We now need to parse the VOTable XML
xml = etree.parse (raw)
data = []
for row in xml.findall('//TR'):
data.append ([td.text for td in row.findall('TD')])
data = np.array (data).T
ra0 = data[0].astype('float64')
dec0 = data[1].astype('float64')
g0 = data[2].astype('float')
r0 = data[3].astype('float')
gr0 = np.subtract (g0,r0)
print 'Got', data.shape[1], 'objects in %s seconds' % \
(time.time()-start_time)
else:
try:
conn = psycopg2.connect (conn_str)
cursor = conn.cursor ()
except psycopg2.Error as e:
print 'Error: Cannot connect to database'
sys.exit(2)
cursor.execute (query)
conn.commit ()
ra0 = []
dec0 = []
g0 = []
r0 = []
gr0 = []
records = cursor.fetchall ()
for rec in records:
ra_, dec_, g_, r_ = rec
ra0.append(ra_)
dec0.append(dec_)
g0.append(g_)
r0.append(r_)
gr0 = np.subtract (g0,r0)
xpos, ypos = np.mean (ra0), np.mean (dec0)
xsize = (np.max(ra0) - np.min(ra0)) / 2.0
ysize = (np.max(dec0) - np.min(dec0)) / 2.0
return ra0, dec0, g0, gr0
# READDATA -- Read the data from a static FITS BINTABLE (testing/demo mode)
def readData (fname):
global xpos, ypos, xsize, ysize
global ra0, dec0, g0, gr0
start_time = time.time()
print "Getting data in file '%s' ...." % fname,
sys.stdout.flush()
data = fits.getdata (fname)
t = Table (data)
ra0 = t['ra_j2000']
dec0 = t['dec_j2000']
#g0 = t['g'] # FIXME -- Need to account for null values
#gr0 = t['g_r']
g0 = t['gmag'] # FIXME -- Need to account for null values
gr0 = t['gmag'] - t['rmag']
print ra0
print 'c/m (%g,%g) (%g,%g)' % (g0.min(),g0.max(),gr0.min(),gr0.max())
xpos, ypos = np.mean (ra0), np.mean (dec0)
xsize = (ra0.max() - ra0.min()) / 2.0
ysize = (dec0.max() - dec0.min()) / 2.0
print 'Got', len(ra0), 'objects in %s seconds' % (time.time()-start_time)
return ra0, dec0, g0, gr0
# DWARF_FILTER -- Ken Mighell's differential convolution filter.
def dwarf_filter (ra, dec, ra_index, dec_index, fwhm_small=2.0, fwhm_big=20):
if (ra is None or dec is None):
return
x = (ra if ra_index is None else ra[ra_index])
y = (dec if dec_index is None else dec[dec_index])
start_time = time.time()
print "Computing differential convolution .... ",
sys.stdout.flush()
# Information about declination (y) [degrees]
ymean = (y.min() + y.max()) / 2.0
ydiff_arcmin = (y.max() - y.min()) * 60.0 # convert from degrees to arcmin
# Information about right ascension (x) [degrees in time]:
xdiff = x.max() - x.min() # angular separation [degrees (time)]
xmean = (x.min() + x.max())/2.0
# convert from degrees in time to separation in angular degrees:
xdiff_angular = (x.max() - x.min()) * np.cos (ymean*(np.pi/180.0))
# convert from degress to arcmin
xdiff_angular_arcmin = xdiff_angular * 60.0
# Get the number of one-arcmin pixels in the X and Y directions:
nx = np.rint (xdiff_angular_arcmin).astype('int')
ny = np.rint (ydiff_arcmin).astype('int')
# Create a two-dimensional histogram of the raw counts:
Counts, xedges, yedges = np.histogram2d (x, y, (nx,ny) )
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
raw_hist = np.rot90 (Counts).copy() # hack around Pythonic weirdness
# Make the small and big Gaussian kernels with a standard deviation
# of the given FWHM in arcmin^2 pixels.
stddev = (fwhm_small / 2.35)
kernel_small = convolution.Gaussian2DKernel ( stddev, factor=1)
stddev = (fwhm_big / 2.35)
kernel_big = convolution.Gaussian2DKernel ( stddev, factor=1)
# Compute the differential convolution kernels.
conv_big = convolution.convolve (raw_hist, kernel_big)
conv_small = convolution.convolve (raw_hist, kernel_small)
conv_delta = conv_small - conv_big
delta = conv_delta.copy()
# Compute statistics and the floor
mean = np.mean (delta, dtype='float64')
sigma = np.std (delta, dtype='float64')
median = np.median (delta) # not used
floor = mean
#print 'dwarf_filter: mean = %g sigma = %g' % (mean, sigma)
# Clip to specified limits.
clipped = delta.copy()
if dmap is not None:
if dmap.lower == None:
dmap.lower = mean + (2 * sigma)
else:
floor = dmap.lower
clipped[ delta < dmap.lower ] = dmap.lower
if dmap.upper != None:
clipped[ delta > dmap.upper ] = dmap.upper
else:
dmap.upper = 999.0
#print 'clip limits = (%g,%g)' % (dmap.lower, dmap.upper)
else:
clipped[ delta < floor ] = floor
print " done in %s seconds\n" % (time.time() - start_time)
# Return the computed fields.
return raw_hist, extent, delta, clipped
pass
# DENSMAP -- A Class to handle the density convolution map..
class densMap (object):
"""
DENSPLOT -- A class to ...
"""
def __init__ (self, ra, dec, small_k, big_k):
self.ra = ra
self.dec = dec
self.small_k = small_k
self.big_k = big_k
self.raw = None
self.extent = None
self.delta = None
self.clipped = None
self.lower = None
self.upper = None
def setData (self, ra, dec):
self.ra = ra
self.dec = dec
def clipRefresh (self, lower, upper):
self.clipped = dmap.delta.copy()
sigma = np.std (self.clipped, dtype='float64')
floor = (self.clipped.mean() if lower is None else lower)
self.lower = lower * sigma
self.clipped[ self.delta < self.lower ] = self.lower
if upper is not None:
self.upper = upper * sigma
self.clipped[ self.delta > self.upper ] = self.upper
print 'clipRefresh: sigma = %g floor = %g upper = %g' % \
(sigma,self.lower,self.upper)
densWindow.redraw() # Redraw the sub-plots
def setHist (self, raw, extent, delta, clipped):
self.raw = raw # raw counts histogram
self.extent = extent # edges of the field
self.delta = delta # difference in convolved field
self.clipped = clipped # clipped map of difference
# DENSPLOT -- A Class to handle the density window.
class densPlot (object):
"""
DENSPLOT -- A class to ...
"""
def __init__ (self, ax):
self.ax = ax
self.canvas = ax.figure.canvas
self.xdata = None
self.ydata = None
self.showContours = True
self.showImage = True
self.use_raw = True
# Create a text box to display the density stats.
at = AnchoredText ("Max=%8.5g Min=%8.5g" % (0.0, 0.0),
prop=dict(size=8), frameon=True, loc=2)
self.ax.add_artist (at)
def setData (self, xdata, ydata):
self.xdata = xdata
self.ydata = ydata
def setLabels (self, xlab, ylab):
self.ax.set_xlabel (xlab)
self.ax.set_ylabel (ylab)
def drawContour (self, hist, extent):
x = np.linspace (extent[0], extent[1], num=len(hist[0]))
y = np.linspace (extent[3], extent[2], num=len(hist))
CS = self.ax.contour (x, y, hist, 8, alpha=0.25)
self.ax.clabel (CS, inline=1, fontsize=10)
self.contours = CS
self.canvas.draw() # redraw the plot
def update (self):
#global raw, extent, delta, clipped
# Create the main density display.
raw, extent, delta, clipped = dwarf_filter (ra0, dec0,
ra_idx, dec_idx, fwhm_small=small_k, fwhm_big=big_k)
dmap.setHist (raw, extent, delta, clipped)
self.redraw()
def redraw (self):
self.ax.clear()
self.ax.set_xlim (xpos + xsize, xpos - xsize)
self.ax.set_ylim (ypos - ysize, ypos + ysize)
at = AnchoredText ("Max=%8.5g Min=%8.5g Npts=%d" % \
(dmap.clipped.max(), dmap.clipped.min(),len(ra0[ra_idx])),
prop=dict(size=8), frameon=True, loc=2)
self.ax.add_artist (at)
if self.showImage:
self.ax.imshow (dmap.clipped, extent=dmap.extent,
interpolation='nearest', cmap='Greys')
if self.showContours:
if self.use_raw:
self.drawContour (dmap.raw, dmap.extent)
else:
self.drawContour (dmap.clipped, dmap.extent)
self.canvas.draw () # Redraw the sub-plots
# CMDPLOT -- A Class to handle the CMD window.
class cmdPlot (object):
"""
CMDPLOT -- A class to ...
"""
def __init__ (self, ax):
global gr0, g0
self.ax = ax
self.canvas = ax.figure.canvas
self.color = None
self.mag = None
self.ax.set_xlim (-0.5, 1.0)
#self.ax.set_ylim (g0.max()+0.25, g0.min()-0.25)
self.ax.set_ylim (min(25.0,g0.max()+0.25), 14.0)
def setData (self, color, mag):
self.color = color
self.mag = mag
self.ax.set_xlim (-0.5, 1.0)
self.ax.set_ylim (min(25.0,mag.max()+0.25), 14.0)
def setLabels (self, xlabel, ylabel):
self.ax.set_xlabel (xlabel)
self.ax.set_ylabel (ylabel)
def update (self, x, y):
global ra, dec, cursz
try:
# Find all points within some specified radius
dist = np.hypot (x - ra0, y - dec0)
idx = dist < cursz
gr, g = gr0[idx], g0[idx]
# Redraw the CMD plot
self.ax.clear()
self.ax.scatter (gr, g, s=1, marker='.', cmap='grey', alpha=0.09)
at = AnchoredText ("Npts = %d" % len(gr), prop=dict(size=8),
frameon=True, loc=2)
self.ax.add_artist (at)
self.ax.set_xlim (-0.5, 1.0)
self.ax.set_ylim (min(25.0,self.mag.max()+0.25), 14.0)
self.canvas.draw () # Redraw the sub-plots
except:
# Just ignore any exceptions for now
pass
############################
# Task main()
############################
if __name__ == '__main__':
# Process commandline args.
try:
opts, args = getopt.getopt(sys.argv[1:],"hdvtl:c:f:r:d:s:S:B:F:C:",
["help","debug","verbose","load","catalog=", "field=",
"ra=","dec=","sz=", "small_k=", "big_k=", "floor=", "ceiling="])
except getopt.GetoptError:
print 'Error processing arguments:'
Usage ()
sys.exit (1)
for opt, arg in opts:
if opt in ("-h", "--help"): # Help
Usage ()
sys.exit ()
elif opt in ("-d", "--debug"): # Debug
debug = 1
elif opt in ("-v", "--verbose"): # Verbose
verbose = 1
elif opt in ("-t", "--tap"): # TAP interface
use_tap = 1
elif opt in ("-c", "--catalog"): # Catalog to query
catalog = arg
elif opt in ("-f", "--field"): # SMASH Field Number
field = int (arg)
elif opt in ("-l", "--load"): # Load the FITS bintable
fname = arg
elif opt in ("-p", "--ra"): # Set RA position
xpos = float (arg)
elif opt in ("-r", "--dec"): # Set DEC position
ypos = float (arg)
elif opt in ("-s", "--sz"): # Set field size
xsize = ysize = float (arg)
elif opt in ("-S", "--small_k"): # Set small kernel size
small_k = float (arg)
elif opt in ("-B", "--big_k"): # Set big kernel size
big_k = float (arg)
elif opt in ("-F", "--floor"): # Set clipping floor (sigma)
floor = float (arg)
elif opt in ("-C", "--ceiling"): # Set clipping ceiling (sigma)
ceiling = float (arg)
# Get the data and compute the differential convolution density
# histograms.
if fname is not None:
ra0, dec0, g0, gr0 = readData ( fname )
elif xpos is not None and ypos is not None:
ra0, dec0, g0, gr0 = getData ( field, catalog )
ra_idx = ra0 < 361 # Initialize for all values
dec_idx = dec0 < 91
# Initialize the density map. [FIXME: need to turn this into object]
dmap = densMap (ra0, dec0, small_k, big_k)
raw, extent, delta, clipped = dwarf_filter (dmap.ra, dmap.dec,
ra_idx, dec_idx, fwhm_small=dmap.small_k, fwhm_big=dmap.big_k)
dmap.setData (ra0, dec0)
dmap.setHist (raw, extent, delta, clipped)
# Create the subplot windows.
fig, (axcmd, axdens) = plt.subplots (1, 2, figsize=(12,5))
# Create the density convolution subplot window.
densWindow = densPlot (axdens)
densWindow.setLabels ('RA (deg)', 'Dec (deg)')
densWindow.setData (ra0, dec0)
densWindow.redraw ()
axcmd.set_aspect (1.0)
# Create a plot of the CMD centered on the initial position.
cmdWindow = cmdPlot (axcmd)
cmdWindow.setLabels ("(g-r)", "g")
cmdWindow.setData (gr0, g0)
cmdWindow.update (np.mean(ra0), np.mean(dec0))
ratio_default=(axdens.get_xlim()[1]-axdens.get_xlim()[0]) / (axdens.get_ylim()[1]-axdens.get_ylim()[0])
axdens.set_aspect (abs(ratio_default))
ratio_default=(axcmd.get_xlim()[1]-axcmd.get_xlim()[0]) / (axcmd.get_ylim()[1]-axcmd.get_ylim()[0])
axcmd.set_aspect (abs(ratio_default))
fig.canvas.draw ()
# Finalize the plot and display
#plt.tight_layout (pad=2.0)
plt.suptitle ("Field %d" % field, fontsize=20)
plt.show ()
#plt.savefig ("Field%d.jpeg" % field)