comparison pkg/imports/gm.go @ 3264:9ae43313b463

Handle some possibly missing elements in NtS response The NtS XSD does not guarantee that value, value_min and value_max are present in a response. This led to circumventing the NOT NULL constraint for the waterlevel value by silently persisting missing values as zero and filling up missing confidence interval values especially for measurements with zeros.
author Tom Gottfried <tom@intevation.de>
date Wed, 15 May 2019 12:31:57 +0200
parents 4acbee65275d
children 3dee5cf16a58
comparison
equal deleted inserted replaced
3263:d23532a4d0c3 3264:9ae43313b463
186 } 186 }
187 return &summary, err 187 return &summary, err
188 } 188 }
189 189
190 // rescale returns a scaling function to bring the unit all to cm. 190 // rescale returns a scaling function to bring the unit all to cm.
191 func rescale(unit string) (func(float32) float32, error) { 191 func rescale(unit string) (func(*float32), error) {
192 192
193 var scale float32 193 var scale float32
194 194
195 switch strings.ToLower(unit) { 195 switch strings.ToLower(unit) {
196 case "mm": 196 case "mm":
207 scale = 100000.0 207 scale = 100000.0
208 default: 208 default:
209 return nil, fmt.Errorf("unknown unit '%s'", unit) 209 return nil, fmt.Errorf("unknown unit '%s'", unit)
210 } 210 }
211 211
212 fn := func(x float32) float32 { return scale * x } 212 fn := func(x *float32) {
213 if x != nil {
214 *x = scale * *x
215 }
216 }
213 return fn, nil 217 return fn, nil
214 } 218 }
215 219
216 func doForGM( 220 func doForGM(
217 ctx context.Context, 221 ctx context.Context,
262 } 266 }
263 convert, err := rescale(unit) 267 convert, err := rescale(unit)
264 if err != nil { 268 if err != nil {
265 return nil, err 269 return nil, err
266 } 270 }
271 convert(measure.Value)
272 convert(measure.Value_min)
273 convert(measure.Value_max)
274
267 isWaterlevel := *measure.Measure_code == nts.Measure_code_enumWAL 275 isWaterlevel := *measure.Measure_code == nts.Measure_code_enumWAL
268 err = insertStmt.QueryRowContext( 276 err = insertStmt.QueryRowContext(
269 ctx, 277 ctx,
270 currIsrs.CountryCode, 278 currIsrs.CountryCode,
271 currIsrs.LoCode, 279 currIsrs.LoCode,
276 msg.Identification.From, 284 msg.Identification.From,
277 msg.Identification.Language_code, 285 msg.Identification.Language_code,
278 msg.Identification.Country_code, 286 msg.Identification.Country_code,
279 msg.Identification.Date_issue, 287 msg.Identification.Date_issue,
280 referenceCode, 288 referenceCode,
281 convert(measure.Value), 289 measure.Value,
282 measure.Predicted, 290 measure.Predicted,
283 isWaterlevel, 291 isWaterlevel,
284 convert(measure.Value_min), 292 measure.Value_min,
285 convert(measure.Value_max), 293 measure.Value_max,
286 msg.Identification.Date_issue, 294 msg.Identification.Date_issue,
287 msg.Identification.Originator, 295 msg.Identification.Originator,
288 true, // staging_done 296 true, // staging_done
289 ).Scan(&gid) 297 ).Scan(&gid)
290 switch { 298 switch {