comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java @ 426:c6b7ca0febcb

Fix step to fix issue69: Do the right calculation flys-artifacts/trunk@1924 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 15 May 2011 10:59:08 +0000
parents aae8f327425e
children 909196be11a0
comparison
equal deleted inserted replaced
425:1827050bb967 426:c6b7ca0febcb
67 public void setName(String name) { 67 public void setName(String name) {
68 this.name = name; 68 this.name = name;
69 } 69 }
70 } 70 }
71 // class Column 71 // class Column
72
73 public static class QPosition {
74
75 protected double q;
76 protected int index;
77 protected double weight;
78
79 public QPosition() {
80 }
81
82 public QPosition(double q, int index, double weight) {
83 this.index = index;
84 this.weight = weight;
85 }
86
87 } // class Position
72 88
73 public static class Row 89 public static class Row
74 implements Serializable, Comparable<Row> 90 implements Serializable, Comparable<Row>
75 { 91 {
76 double km; 92 double km;
331 outQs[i] = weight(factor, qs[i], other.qs[i]); 347 outQs[i] = weight(factor, qs[i], other.qs[i]);
332 } 348 }
333 349
334 return new double [][] { outWs, outQs }; 350 return new double [][] { outWs, outQs };
335 } 351 }
352
353 public QPosition getQPosition(double q) {
354 int qIndex = getQIndex(q);
355
356 if (qIndex >= 0) {
357 // direct column match
358 return new QPosition(q, qIndex, 1.0);
359 }
360
361 qIndex = -qIndex -1;
362
363 if (qIndex < 0 || qIndex >= qs.length-1) {
364 // do not extrapolate
365 return null;
366 }
367
368 return new QPosition(
369 q, qIndex, factor(q, qs[qIndex-1], qs[qIndex]));
370 }
371
372 public QPosition getQPosition(Row other, double km, double q) {
373
374 QPosition qpt = getQPosition(q);
375 QPosition qpo = other.getQPosition(q);
376
377 if (qpt == null || qpo == null) {
378 return null;
379 }
380
381 double kmWeight = factor(km, this.km, other.km);
382
383 // XXX: Index interpolation is a bit sticky here!
384
385 int index = (int)Math.round(
386 weight(kmWeight, qpt.index, qpo.index));
387
388 double weight = weight(kmWeight, qpt.weight, qpo.weight);
389
390 return new QPosition(q, index, weight);
391 }
392
393 public void storeWQ(
394 QPosition qPosition,
395 double [] ows,
396 double [] oqs,
397 int index
398 ) {
399 int qIdx = qPosition.index;
400 double qWeight = qPosition.weight;
401
402 if (qWeight == 1.0) {
403 oqs[index] = qs[qIdx];
404 ows[index] = ws[qIdx];
405 }
406 else {
407 oqs[index] = weight(qWeight, qs[qIdx-1], qs[qIdx]);
408 ows[index] = weight(qWeight, ws[qIdx-1], ws[qIdx]);
409 }
410 }
411
412 public void storeWQ(
413 Row other,
414 double km,
415 QPosition qPosition,
416 double [] ows,
417 double [] oqs,
418 int index
419 ) {
420 double kmWeight = factor(km, this.km, other.km);
421
422 double tq, tw;
423 double oq, ow;
424
425 int qIdx = qPosition.index;
426 double qWeight = qPosition.weight;
427
428 if (qWeight == 1.0) {
429 tq = qs[qIdx];
430 tw = ws[qIdx];
431 oq = other.qs[qIdx];
432 ow = other.ws[qIdx];
433 }
434 else {
435 tq = weight(qWeight, qs[qIdx-1], qs[qIdx]);
436 tw = weight(qWeight, ws[qIdx-1], ws[qIdx]);
437 oq = weight(qWeight, other.qs[qIdx-1], other.qs[qIdx]);
438 ow = weight(qWeight, other.ws[qIdx-1], other.ws[qIdx]);
439 }
440
441 oqs[index] = weight(kmWeight, oq, tq);
442 ows[index] = weight(kmWeight, ow, tw);
443 }
336 } 444 }
337 // class Row 445 // class Row
338 446
339 protected List<Row> rows; 447 protected List<Row> rows;
340 448
403 511
404 Row r1 = rows.get(rowIndex-1); 512 Row r1 = rows.get(rowIndex-1);
405 Row r2 = rows.get(rowIndex); 513 Row r2 = rows.get(rowIndex);
406 514
407 return r1.interpolateWQ(r2, km, steps); 515 return r1.interpolateWQ(r2, km, steps);
516 }
517
518 public boolean interpolate(
519 double q,
520 int referenceIndex,
521 double [] kms,
522 double [] ws,
523 double [] qs
524 ) {
525 Row kmKey = new Row(kms[referenceIndex]);
526
527 int rowIndex = Collections.binarySearch(rows, kmKey);
528
529 int R1 = rows.size()-1;
530
531 QPosition qPosition = null;
532
533 if (rowIndex >= 0) { // direct row match
534 qPosition = rows.get(rowIndex).getQPosition(q);
535 }
536 else {
537 rowIndex = -rowIndex -1;
538
539 if (rowIndex < 1 || rowIndex >= R1) {
540 // do not extrapolate
541 return false;
542 }
543
544 // interpolation case
545 Row r1 = rows.get(rowIndex-1);
546 Row r2 = rows.get(rowIndex);
547
548 qPosition = r1.getQPosition(r2, kms[referenceIndex], q);
549 }
550
551 if (qPosition == null) {
552 // we cannot locate q in reference row
553 return false;
554 }
555
556 for (int i = 0; i < kms.length; ++i) {
557 kmKey.km = kms[i];
558 rowIndex = Collections.binarySearch(rows, kmKey);
559
560 if (rowIndex >= 0) {
561 // direct row match
562 rows.get(rowIndex).storeWQ(qPosition, ws, qs, i);
563 continue;
564 }
565
566 rowIndex = -rowIndex -1;
567
568 if (rowIndex < 1 || rowIndex >= R1) {
569 // do not extrapolate
570 ws[i] = qs[i] = Double.NaN;
571 continue;
572 }
573 Row r1 = rows.get(rowIndex-1);
574 Row r2 = rows.get(rowIndex);
575 r1.storeWQ(r2, kms[i], qPosition, ws, qs, i);
576 }
577
578 return true;
408 } 579 }
409 580
410 public static WstValueTable getTable(River river) { 581 public static WstValueTable getTable(River river) {
411 return getTable(river, 0); 582 return getTable(river, 0);
412 } 583 }

http://dive4elements.wald.intevation.org