-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectrum.go
211 lines (184 loc) · 6.28 KB
/
spectrum.go
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
package wsHandler
import (
"errors"
"fmt"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
"github.com/pixlise/core/v4/core/utils"
protos "github.com/pixlise/core/v4/generated-protos"
)
func HandleSpectrumReq(req *protos.SpectrumReq, hctx wsHelpers.HandlerContext) (*protos.SpectrumResp, error) {
exprPB, indexes, err := beginDatasetFileReqForRange(req.ScanId, req.Entries, hctx)
if err != nil {
return nil, err
}
detectorIdIdx := -1
readtypeIdx := -1
for c, label := range exprPB.MetaLabels {
if label == "DETECTOR_ID" {
detectorIdIdx = c
} else if label == "READTYPE" {
readtypeIdx = c
}
if readtypeIdx > -1 && detectorIdIdx > -1 {
break
}
}
spectra := []*protos.Spectra{}
for _, c := range indexes {
detectorSpectra := []*protos.Spectrum{}
for _, detector := range exprPB.Locations[c].Detectors {
spectrum, err := convertSpectrum(detector, exprPB.MetaLabels, exprPB.MetaTypes, detectorIdIdx, readtypeIdx)
if err != nil {
return nil, fmt.Errorf("%v for spectrum at idx: %v", err, c)
}
detectorSpectra = append(detectorSpectra, spectrum)
}
spectra = append(spectra, &protos.Spectra{Spectra: detectorSpectra})
}
// For now, all our spectra are 4096 but in time if we have other detectors, we should be able to send back
// the channel count based on the detector here...
channelCount := uint32(4096)
result := &protos.SpectrumResp{
SpectraPerLocation: spectra,
ChannelCount: channelCount,
NormalSpectraForScan: uint32(exprPB.NormalSpectra),
DwellSpectraForScan: uint32(exprPB.DwellSpectra),
}
for c, label := range exprPB.MetaLabels {
if label == "LIVETIME" {
result.LiveTimeMetaIndex = uint32(c)
break
}
}
// If bulk or max spectra are required... find & return those too
if req.BulkSum || req.MaxValue {
bulkSpectra, maxSpectra, err := getBulkMaxSpectra(req.BulkSum, req.MaxValue, exprPB, detectorIdIdx, readtypeIdx)
if err != nil {
return nil, err
}
result.BulkSpectra = bulkSpectra
result.MaxSpectra = maxSpectra
}
return result, nil
}
// Returns array of bulk, array of max, error
func getBulkMaxSpectra(getBulk bool, getMax bool,
exprPB *protos.Experiment,
detectorIdIdx int,
readtypeIdx int) ([]*protos.Spectrum, []*protos.Spectrum, error) {
bulkSpectra := []*protos.Spectrum{}
maxSpectra := []*protos.Spectrum{}
for c, loc := range exprPB.Locations {
for _, detector := range loc.Detectors {
for _, m := range detector.Meta {
if m.LabelIdx == int32(readtypeIdx) {
// Verify type
if t := exprPB.MetaTypes[m.LabelIdx]; t == protos.Experiment_MT_STRING {
// These are hard-coded string values
switch m.Svalue {
case "BulkSum":
if getBulk {
spectrum, err := convertSpectrum(detector, exprPB.MetaLabels, exprPB.MetaTypes, detectorIdIdx, readtypeIdx)
if err != nil {
return nil, nil, err
}
bulkSpectra = append(bulkSpectra, spectrum)
break
}
case "MaxValue":
if getMax {
spectrum, err := convertSpectrum(detector, exprPB.MetaLabels, exprPB.MetaTypes, detectorIdIdx, readtypeIdx)
if err != nil {
return nil, nil, err
}
maxSpectra = append(maxSpectra, spectrum)
break
}
}
} else {
return nil, nil, fmt.Errorf("Unexpected %v when reading spectrum type for spectrum at idx: %v", t, c)
}
}
}
}
// At this point, if we've what we're after, stop. We assume that ALL detectors bulk or max spectra will only be defined
// in a single location.
if (!getBulk || getBulk && len(bulkSpectra) > 0) &&
(!getMax || getMax && len(maxSpectra) > 0) {
break
}
}
return bulkSpectra, maxSpectra, nil
}
func convertSpectrum(
detector *protos.Experiment_Location_DetectorSpectrum,
metaLabels []string,
metaTypes []protos.Experiment_MetaDataType,
detectorIdIdx int,
readtypeIdx int) (*protos.Spectrum, error) {
detectorType := protos.SpectrumType_SPECTRUM_UNKNOWN
detectorId := ""
meta := map[int32]*protos.ScanMetaDataItem{}
for _, m := range detector.Meta {
if m.LabelIdx >= int32(len(metaLabels)) {
return nil, fmt.Errorf("LabelIdx %v out of range when reading meta", m.LabelIdx)
}
label := metaLabels[m.LabelIdx]
if m.LabelIdx == int32(detectorIdIdx) {
// Verify type
if t := metaTypes[m.LabelIdx]; t == protos.Experiment_MT_STRING {
detectorId = m.Svalue
} else {
return nil, fmt.Errorf("Unexpected %v when reading detector id", t)
}
} else if m.LabelIdx == int32(readtypeIdx) {
// Verify type
if t := metaTypes[m.LabelIdx]; t == protos.Experiment_MT_STRING {
// These are hard-coded string values
switch m.Svalue {
case "Normal":
detectorType = protos.SpectrumType_SPECTRUM_NORMAL
case "Dwell":
detectorType = protos.SpectrumType_SPECTRUM_DWELL
case "BulkSum":
detectorType = protos.SpectrumType_SPECTRUM_BULK
case "MaxValue":
detectorType = protos.SpectrumType_SPECTRUM_MAX
}
} else {
return nil, fmt.Errorf("Unexpected %v when reading spectrum type", t)
}
// It's just meta-data to save, however, we don't need to save PMC in here!
} else if label != "PMC" {
// Check that this slot isn't taken
if _, ok := meta[m.LabelIdx]; ok {
return nil, fmt.Errorf("Conflicting label index %v when reading spectrum meta", m.LabelIdx)
}
mSave := &protos.ScanMetaDataItem{}
if t := metaTypes[m.LabelIdx]; t == protos.Experiment_MT_STRING {
mSave.Value = &protos.ScanMetaDataItem_Svalue{Svalue: m.Svalue}
} else if t == protos.Experiment_MT_INT {
mSave.Value = &protos.ScanMetaDataItem_Ivalue{Ivalue: m.Ivalue}
} else if t == protos.Experiment_MT_FLOAT {
mSave.Value = &protos.ScanMetaDataItem_Fvalue{Fvalue: m.Fvalue}
} else {
return nil, fmt.Errorf("Unknown type %v for meta label: %v when reading spectrum type", t, label)
}
meta[m.LabelIdx] = mSave
}
}
// If we didn't get anything, complain
if len(detectorId) <= 0 {
return nil, errors.New("Failed to read detector id")
} else if detectorType == protos.SpectrumType_SPECTRUM_UNKNOWN {
return nil, errors.New("Failed to read spectrum type")
}
spectrum := &protos.Spectrum{
Detector: detectorId,
Type: detectorType,
Meta: meta,
MaxCount: uint32(detector.SpectrumMax),
Counts: utils.ConvertIntSlice[uint32](detector.Spectrum),
}
return spectrum, nil
}