This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
fitsviewer.coffee
224 lines (179 loc) · 6.04 KB
/
fitsviewer.coffee
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
# Troublesome
# #/examine/AGZ00013gv
class FITSViewer extends Spine.Controller
validDestination: "http://www.sdss.org.uk/"
viewportDimension: 424
bins: 10000
events:
"click .band": "selectBand"
"change #stretch": "selectStretch"
constructor: ->
super
# Default stretch
@stretch = 'linear'
# Listen for when WebFITS Api is ready
@bind('fitsviewer:ready', =>
# Setup WebFITS context
@wfits = new astro.WebFITS(@el.find('.subject')[0], @viewportDimension)
@wfits.setupControls()
# Request FITS images
@requestFITS(@survey, @survey_id)
@unbind('fitsviewer:ready')
)
# Request WebFITS
@getApi()
# Storage for data
@histograms = {}
@means = {}
@percentiles = {}
getApi: ->
# Determine if WebGL is supported, otherwise fall back to canvas
canvas = document.createElement('canvas')
context = canvas.getContext('webgl')
context = canvas.getContext('experimental-webgl') unless context?
# Load appropriate WebFITS library asynchronously
lib = if context? then 'gl' else 'canvas'
url = "/javascripts/webfits-#{lib}.js"
$.getScript(url, =>
@trigger 'fitsviewer:ready'
)
requestFITS: (survey, id) ->
window.addEventListener("message", @receiveFITS, false)
msg =
survey: survey
id: id
$("#dataonwire")[0].contentWindow.postMessage(msg, @validDestination)
receiveFITS: (e) =>
if e.origin is 'https://api.zooniverse.org'
return null
# TODO: Error handling needs work.
if e.data.error?
window.removeEventListener("message", @receiveFITS, false)
else
data = e.data
buffer = data.arraybuffer
band = data.band
f = new astro.FITS.File(buffer)
dataunit = f.getDataUnit()
arr = dataunit.getFrame()
[min, max] = dataunit.getExtent(arr)
@wfits.loadImage(band, arr, dataunit.width, dataunit.height)
[histogram, mean, lower, upper] = computeStatistics(min, max, @bins, arr)
@histograms[band] = histogram
@means[band] = mean
@percentiles[band] = [lower, upper]
if @wfits.nImages is @bands.length
# Stop listening after all images received
window.removeEventListener("message", @receiveFITS, false)
# Setup controls
@controls = $("#viewer-controls")
@createMetadata()
@createBandButtons()
@createStretchButtons()
# Get global extent
mins = []
maxs = []
for key, values of @percentiles
mins.push values[0]
maxs.push values[1]
gMin = Math.min.apply(Math, mins)
gMax = Math.max.apply(Math, maxs)
# Set parameters for visualization
@wfits.setExtent(gMin, gMax)
@wfits.setImage(@bands[0])
createMetadata: =>
@subjectInfo = $("#examine .subject-info")
@subjectInfo.append("""
<div class='row'>
<span class='key'>#{ I18n.t 'fits.x_y' }:</span>
<span class='xy value'></span>
</div>
""")
@subjectInfo.append("""
<div class='row'>
<span class='key'>#{ I18n.t 'fits.intensity' }:</span>
<span class='intensity value'></span>
</div>
""")
destroyMetadata: =>
@subjectInfo.empty() if @subjectInfo
@subjectInfo = null
createBandButtons: =>
for band in @bands
bandLower = band.toLowerCase()
@controls.append("<button id='band-#{band}' class='band' value='#{band}'>#{bandLower}</button>")
@controls.append("<button id='band-color' class='band' value='color'>#{ I18n.t('fits.color') }</button>")
destroyBandButtons: =>
@controls.empty() if @controls
@controls = null
createStretchButtons: =>
@controls.append("<select id='stretch'>
<option value='linear'>#{ I18n.t('fits.linear') }</option>
<option value='logarithm'>#{ I18n.t('fits.logarithm') }</option>
<option value='sqrt'>#{ I18n.t('fits.square_root') }</option>
<option value='arcsinh'>#{ I18n.t('fits.arcsinh') }</option>
<option value='power'>#{ I18n.t('fits.power') }</option>
</select>")
@stretch = $("#stretch")
destroyStretchButtons: =>
@controls.empty() if @controls
selectBand: (e) =>
band = e.currentTarget.value
if band is 'color'
@el.find('.subject img').show()
else
@el.find('.subject img').hide()
@wfits.setImage(band)
@wfits.draw()
selectStretch: (e) =>
@stretch = e.currentTarget.value
@wfits.setStretch(@stretch)
computeStatistics = (min, max, bins, data) ->
range = max - min
binSize = range / bins
numPixels = data.length
numNaNs = 0
sum = 0
# Initialize array
histogram = new Array(bins + 1)
for value, index in histogram
step = binSize * index
histogram[index] = [step, 0]
for value, index in data
if isNaN(value)
numNaNs += 1
continue
sum += value
index = Math.floor(((value - min) / range) * bins)
histogram[index][1] += 1
mean = sum / (numPixels - numNaNs)
# Recompute sum to be offset by min
sum = 0
sorted = [] # Push to JS array to use the default sort
for value in data
value -= min
sum += value
sorted.push value
sorted = sorted.sort()
# Compute percentiles
[lower, upper] = [0.0025, 0.9975]
running = 0
for value, index in sorted
running += value
percentile = running / sum
if percentile > lower
lower = sorted[index - 1] + min
break
running = 0
for value, index in sorted
running += value
percentile = running / sum
if percentile > upper
upper = sorted[index - 1] + min
break
return [histogram, mean, lower, upper]
teardown: =>
@destroyStretchButtons()
@destroyBandButtons()
@destroyMetadata()
module.exports = FITSViewer