-
Notifications
You must be signed in to change notification settings - Fork 1
/
summary.go
156 lines (141 loc) · 5.48 KB
/
summary.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
// Licensed to NASA JPL under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. NASA JPL licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package output
import (
"fmt"
"path"
"reflect"
"strings"
"github.com/pixlise/core/v3/api/filepaths"
datasetModel "github.com/pixlise/core/v3/core/dataset"
"github.com/pixlise/core/v3/core/fileaccess"
"github.com/pixlise/core/v3/data-import/internal/dataConvertModels"
protos "github.com/pixlise/core/v3/generated-protos"
)
func makeSummaryFileContent(exp *protos.Experiment, datasetID string, group string, meta dataConvertModels.FileMetaData, fileSize int, creationUnixTimeSec int64) datasetModel.SummaryFileData {
contextImgCount := len(exp.AlignedContextImages) + len(exp.UnalignedContextImages) + len(exp.MatchedAlignedContextImages)
tiffContextImgCount := 0
// Count the number of TIFF context images so that we can quickly determine if the dataset is RGBU
for _, img := range exp.AlignedContextImages {
if strings.HasSuffix(img.Image, ".tif") {
tiffContextImgCount++
}
}
for _, img := range exp.UnalignedContextImages {
if strings.HasSuffix(img, ".tif") {
tiffContextImgCount++
}
}
for _, img := range exp.MatchedAlignedContextImages {
if strings.HasSuffix(img.Image, ".tif") {
tiffContextImgCount++
}
}
s := datasetModel.SummaryFileData{
DatasetID: datasetID,
Group: group,
ContextImage: exp.MainContextImage,
TargetID: exp.TargetId,
SiteID: meta.SiteID,
DriveID: meta.DriveID,
Target: meta.Target,
Site: meta.Site,
Title: meta.Title,
SOL: meta.SOL,
RTT: meta.RTT,
SCLK: meta.SCLK,
LocationCount: len(exp.Locations),
DataFileSize: fileSize,
ContextImages: contextImgCount,
TIFFContextImages: tiffContextImgCount,
NormalSpectra: int(exp.NormalSpectra),
DwellSpectra: int(exp.DwellSpectra),
BulkSpectra: int(exp.BulkSpectra),
MaxSpectra: int(exp.MaxSpectra),
PseudoIntensities: int(exp.PseudoIntensities),
DetectorConfig: exp.DetectorConfig,
CreationUnixTimeSec: creationUnixTimeSec,
}
return s
}
func SummaryDiff(currentSummary datasetModel.SummaryFileData, previousSummary datasetModel.SummaryFileData) (datasetModel.SummaryFileData, error) {
// Create a new Summary struct for collecting the diff
summaryDiffData := datasetModel.SummaryFileData{}
valuesDiff := reflect.ValueOf(&summaryDiffData)
valuesOld := reflect.ValueOf(&previousSummary)
// Prep fields and values of the new Summary struct for iteration
fields := reflect.TypeOf(currentSummary)
values := reflect.ValueOf(currentSummary)
num := fields.NumField()
for i := 0; i < num; i++ {
// Grab the value for new, old and diff structs based on the current field
field := fields.Field(i)
value := values.Field(i)
valueOld := reflect.Indirect(valuesOld).FieldByName(field.Name)
valueDiff := reflect.Indirect(valuesDiff).FieldByName(field.Name)
//fmt.Print("Type:", field.Type, ",", field.Name, "=", value, "\n")
// NOTE: RTT has become an interface{} because it has to be able to read old saved files which had int
// and now saves as string. This breaks here, because we can't compare interface values in general but
// we have a hard-coded work-around here for it
if field.Name == "RTT" {
oldRTT := fmt.Sprintf("%v", valueOld)
newRTT := fmt.Sprintf("%v", value)
if oldRTT != newRTT {
summaryDiffData.RTT = newRTT
}
} else {
// If field has changed, indicate the new value in diff, otherwise leave diff field nil
switch value.Kind() {
case reflect.String:
if value.String() != valueOld.String() {
valueDiff.SetString(value.String())
}
case reflect.Int, reflect.Int32, reflect.Int64:
if value.Int() != valueOld.Int() {
valueDiff.SetInt(value.Int())
}
case reflect.Slice:
_, ok := value.Interface().([]string)
if !ok {
fmt.Println("Couldn't parse slice")
}
//fmt.Printf("%v", len(slice))
case reflect.Struct:
reflect.DeepEqual(value, valueOld)
default:
return summaryDiffData, fmt.Errorf("unable to compare field of type: %s", value.Kind())
}
}
}
return summaryDiffData, nil
}
func readPreviousSummary(datasetID string, bucket string, fs fileaccess.FileAccess) (datasetModel.SummaryFileData, error) {
summaryData := datasetModel.SummaryFileData{}
// Listing files in artifact bucket/<dataset-id>/
files, err := fs.ListObjects(bucket, datasetID)
if err != nil {
return summaryData, err
}
if len(files) > 0 {
err = fs.ReadJSON(bucket, path.Join(datasetID, filepaths.DatasetSummaryFileName), &summaryData, false)
summaryData.RTT = summaryData.GetRTT() // For backwards compatibility we can read it as an int, but here we convert to string
if err != nil {
return summaryData, err
}
}
return summaryData, nil
}