-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.go
289 lines (254 loc) · 8.34 KB
/
dataset.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
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
package main
import (
"context"
"fmt"
"path"
"sync"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/filepaths"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/utils"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo"
)
/* An example:
{
"dataset_id": "089063943",
"group": "PIXL-FM",
"drive_id": 0,
"site_id": 8,
"target_id": "?",
"site": "",
"target": "",
"title": "Dourbes",
"sol": "0257",
"rtt": "089063943",
"sclk": 689790062,
"context_image": "PCW_0257_0689790669_000RCM_N00800000890639430006075J01.png",
"location_count": 3341,
"data_file_size": 20767829,
"context_images": 37,
"tiff_context_images": 2,
"normal_spectra": 6666,
"dwell_spectra": 0,
"bulk_spectra": 2,
"max_spectra": 2,
"pseudo_intensities": 3333,
"detector_config": "PIXL",
"create_unixtime_sec": 1663283056
}
*/
type SrcSummaryFileData struct {
DatasetID string `json:"dataset_id"`
Group string `json:"group"`
DriveID int32 `json:"drive_id"`
SiteID int32 `json:"site_id"`
TargetID string `json:"target_id"`
Site string `json:"site"`
Target string `json:"target"`
Title string `json:"title"`
SOL string `json:"sol"`
RTT interface{} `json:"rtt,string"` // Unfortunately we stored it as int initially, so this has to accept files stored that way
SCLK int32 `json:"sclk"`
ContextImage string `json:"context_image"`
LocationCount int `json:"location_count"`
DataFileSize int `json:"data_file_size"`
ContextImages int `json:"context_images"`
TIFFContextImages int `json:"tiff_context_images"`
NormalSpectra int `json:"normal_spectra"`
DwellSpectra int `json:"dwell_spectra"`
BulkSpectra int `json:"bulk_spectra"`
MaxSpectra int `json:"max_spectra"`
PseudoIntensities int `json:"pseudo_intensities"`
DetectorConfig string `json:"detector_config"`
CreationUnixTimeSec int64 `json:"create_unixtime_sec"`
}
func (s SrcSummaryFileData) GetRTT() string {
result := ""
switch s.RTT.(type) {
case float64:
f, ok := s.RTT.(float64)
if ok {
result = fmt.Sprintf("%d", int(f))
}
case int:
i, ok := s.RTT.(int)
if ok {
result = fmt.Sprintf("%d", i)
}
default:
result = fmt.Sprintf("%v", s.RTT)
}
padding := 9 - len(result)
if padding > 0 {
for i := 0; i < padding; i++ {
result = "0" + result
}
}
return result
}
type SrcDatasetConfig struct {
Datasets []SrcSummaryFileData `json:"datasets"`
}
func SrcGetDatasetFilePath(datasetID string, fileName string) string {
return path.Join("Datasets", datasetID, fileName)
}
func migrateDatasets(
configBucket string,
srcBucket string,
destDataBucket string,
fs fileaccess.FileAccess,
dest *mongo.Database,
limitToDatasetIds []string,
userGroups map[string]string) error {
// Drop images collection
coll := dest.Collection(dbCollections.ImagesName)
err := coll.Drop(context.TODO())
if err != nil {
return err
}
// And beam locations
coll = dest.Collection(dbCollections.ImageBeamLocationsName)
err = coll.Drop(context.TODO())
if err != nil {
return err
}
// And default images
coll = dest.Collection(dbCollections.ScanDefaultImagesName)
err = coll.Drop(context.TODO())
if err != nil {
return err
}
// Also drop scan collection
coll = dest.Collection(dbCollections.ScansName)
err = coll.Drop(context.TODO())
if err != nil {
return err
}
// First, get the dataset summaries
summaries := SrcDatasetConfig{}
err = fs.ReadJSON(configBucket, "PixliseConfig/datasets.json", &summaries, false)
if err != nil {
return err
}
var wg sync.WaitGroup
insertCount := 0
for _, dataset := range summaries.Datasets {
if len(limitToDatasetIds) > 0 && !utils.ItemInSlice(dataset.DatasetID, limitToDatasetIds) {
fmt.Printf(" SKIPPING scan: %v...\n", dataset.DatasetID)
continue
}
wg.Add(1)
go func(dataset SrcSummaryFileData) {
defer wg.Done()
fmt.Printf("Importing scan: %v...\n", dataset.DatasetID)
instrument := protos.ScanInstrument_PIXL_FM
if dataset.Group == "PIXL_EM" {
instrument = protos.ScanInstrument_PIXL_EM
} else if dataset.Group == "Breadboard" {
instrument = protos.ScanInstrument_JPL_BREADBOARD
}
meta := map[string]string{
"RTT": dataset.GetRTT(),
"SCLK": fmt.Sprintf("%v", dataset.SCLK),
"Sol": dataset.SOL,
"DriveId": fmt.Sprintf("%v", dataset.DriveID),
//"Drive": dataset.Drive, <-- doesn't exist
"TargetId": dataset.TargetID,
"Target": dataset.Target,
"SiteId": fmt.Sprintf("%v", dataset.SiteID),
"Site": dataset.Site,
}
counts := map[string]int32{
"NormalSpectra": int32(dataset.NormalSpectra),
"DwellSpectra": int32(dataset.DwellSpectra),
"BulkSpectra": int32(dataset.BulkSpectra),
"MaxSpectra": int32(dataset.MaxSpectra),
"PseudoIntensities": int32(dataset.PseudoIntensities),
}
destItem := protos.ScanItem{
Id: dataset.DatasetID,
Title: dataset.Title,
Description: "",
TimestampUnixSec: uint32(dataset.CreationUnixTimeSec),
Instrument: instrument,
InstrumentConfig: dataset.DetectorConfig,
DataTypes: []*protos.ScanItem_ScanTypeCount{},
Meta: meta,
ContentCounts: counts,
}
if dataset.LocationCount > 0 {
destItem.DataTypes = append(destItem.DataTypes, &protos.ScanItem_ScanTypeCount{
DataType: protos.ScanDataType_SD_XRF,
Count: uint32(dataset.LocationCount),
})
}
if dataset.TIFFContextImages > 0 {
destItem.DataTypes = append(destItem.DataTypes, &protos.ScanItem_ScanTypeCount{
DataType: protos.ScanDataType_SD_RGBU,
Count: uint32(dataset.TIFFContextImages),
})
}
if dataset.ContextImages > 0 {
destItem.DataTypes = append(destItem.DataTypes, &protos.ScanItem_ScanTypeCount{
DataType: protos.ScanDataType_SD_IMAGE,
Count: uint32(dataset.ContextImages),
})
}
// Decide which group to link this scan to
memberGroup := userGroups["PIXL-FM"]
if instrument == protos.ScanInstrument_PIXL_EM {
memberGroup = userGroups["PIXL-EM"]
} else if instrument != protos.ScanInstrument_PIXL_FM {
memberGroup = userGroups["JPL Breadboard"]
}
_, err := coll.InsertOne(context.TODO(), &destItem)
if err != nil {
fatalError(err)
}
// Each scan needs an ownership item to define who can view/edit it
// Prefix the ID with "scan_" because the dataset IDs are likely not that long, and we also want them
// to differ from our random ones
err = saveOwnershipItem( /*"scan_"+*/ dataset.DatasetID, protos.ObjectType_OT_SCAN, "", memberGroup, "", uint32(dataset.CreationUnixTimeSec), dest)
if err != nil {
fatalError(err)
}
err = importImagesForDataset(dataset.DatasetID, instrument, srcBucket, destDataBucket, fs, dest)
if err != nil {
fatalError(err)
}
// Copy dataset bin file
s3SourcePath := SrcGetDatasetFilePath(dataset.DatasetID, filepaths.DatasetFileName)
s3DestPath := filepaths.GetScanFilePath(dataset.DatasetID, filepaths.DatasetFileName)
err = fs.CopyObject(srcBucket, s3SourcePath, destDataBucket, s3DestPath)
if err != nil {
fatalError(err)
}
// Copy diffraction db bin file
s3SourcePath = SrcGetDatasetFilePath(dataset.DatasetID, filepaths.DiffractionDBFileName)
s3DestPath = filepaths.GetScanFilePath(dataset.DatasetID, filepaths.DiffractionDBFileName)
err = fs.CopyObject(srcBucket, s3SourcePath, destDataBucket, s3DestPath)
if err != nil {
fatalError(err)
}
// Set the default image
err = setDefaultImage(dataset.DatasetID, dataset.ContextImage, dest)
if err != nil {
fatalError(err)
}
insertCount++
}(dataset)
}
// Wait for all
wg.Wait()
fmt.Printf("Scans inserted: %v\n", insertCount)
return err
}
func setDefaultImage(scanId string, image string, db *mongo.Database) error {
if len(image) <= 0 {
return nil // nothing to store
}
coll := db.Collection(dbCollections.ScanDefaultImagesName)
_, err := coll.InsertOne(context.TODO(), &protos.ScanImageDefaultDB{ScanId: scanId, DefaultImageFileName: path.Join(scanId, image)})
return err
}