comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 709:3b7e9ddf6bb1

New model to transport data and error reports of calculations. flys-artifacts/trunk@2165 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Jun 2011 12:32:32 +0000
parents 757ff56b43b3
children cded0924193d
comparison
equal deleted inserted replaced
708:757ff56b43b3 709:3b7e9ddf6bb1
35 import de.intevation.flys.artifacts.model.MainValuesFactory; 35 import de.intevation.flys.artifacts.model.MainValuesFactory;
36 import de.intevation.flys.artifacts.model.WQDay; 36 import de.intevation.flys.artifacts.model.WQDay;
37 import de.intevation.flys.artifacts.model.WQKms; 37 import de.intevation.flys.artifacts.model.WQKms;
38 import de.intevation.flys.artifacts.model.WstValueTable; 38 import de.intevation.flys.artifacts.model.WstValueTable;
39 import de.intevation.flys.artifacts.model.WstValueTableFactory; 39 import de.intevation.flys.artifacts.model.WstValueTableFactory;
40 import de.intevation.flys.artifacts.model.Calculation;
40 import de.intevation.flys.artifacts.model.Calculation1; 41 import de.intevation.flys.artifacts.model.Calculation1;
41 import de.intevation.flys.artifacts.model.Calculation2; 42 import de.intevation.flys.artifacts.model.Calculation2;
42 import de.intevation.flys.artifacts.model.Calculation3; 43 import de.intevation.flys.artifacts.model.Calculation3;
43 import de.intevation.flys.artifacts.model.Calculation4; 44 import de.intevation.flys.artifacts.model.Calculation4;
45 import de.intevation.flys.artifacts.model.CalculationResult;
44 import de.intevation.flys.artifacts.model.Segment; 46 import de.intevation.flys.artifacts.model.Segment;
45 47
46 48
47 /** 49 /**
48 * The default WINFO artifact. 50 * The default WINFO artifact.
295 /** 297 /**
296 * Returns the data that is computed by a waterlevel computation. 298 * Returns the data that is computed by a waterlevel computation.
297 * 299 *
298 * @return an array of data triples that consist of W, Q and Kms. 300 * @return an array of data triples that consist of W, Q and Kms.
299 */ 301 */
300 public WQKms[] getWaterlevelData() 302 public CalculationResult getWaterlevelData()
301 throws NullPointerException
302 { 303 {
303 logger.debug("WINFOArtifact.getWaterlevelData"); 304 logger.debug("WINFOArtifact.getWaterlevelData");
304 305
305 River river = getRiver(); 306 River river = getRiver();
306 if (river == null) { 307 if (river == null) {
307 throw new NullPointerException("No river selected."); 308 return error(new WQKms[0], "No river selected.");
308 } 309 }
309 310
310 double[] kms = getKms(); 311 double[] kms = getKms();
311 if (kms == null) { 312 if (kms == null) {
312 throw new NullPointerException("No Kms selected."); 313 return error(new WQKms[0], "No Kms selected.");
313 } 314 }
314 315
315 double[] qs = getQs(); 316 double[] qs = getQs();
316 double[] ws = null; 317 double[] ws = null;
317 boolean qSel = true; 318 boolean qSel = true;
323 qs = getQsForWs(ws); 324 qs = getQsForWs(ws);
324 } 325 }
325 326
326 WstValueTable wst = WstValueTableFactory.getTable(river); 327 WstValueTable wst = WstValueTableFactory.getTable(river);
327 if (wst == null) { 328 if (wst == null) {
328 throw new NullPointerException("No Wst found for selected river."); 329 return error(new WQKms[0], "No Wst found for selected river.");
329 } 330 }
330 331
331 double refKm = Double.NaN; 332 double refKm = Double.NaN;
332 333
333 if (!isFreeQ()) { 334 if (!isFreeQ()) {
339 else { 340 else {
340 refKm = gauge.getStation().doubleValue(); 341 refKm = gauge.getStation().doubleValue();
341 } 342 }
342 } 343 }
343 344
344 WQKms[] results = computeWaterlevelData( 345 return computeWaterlevelData(
345 kms, qs, ws, wst, refKm, river.getKmUp()); 346 kms, qs, ws, wst, refKm, river.getKmUp());
346
347 return results;
348 } 347 }
349 348
350 /** 349 /**
351 * Computes the data of a waterlevel computation based on the interpolation 350 * Computes the data of a waterlevel computation based on the interpolation
352 * in WstValueTable. 351 * in WstValueTable.
355 * @param qa The discharge values. 354 * @param qa The discharge values.
356 * @param wst The WstValueTable used for the interpolation. 355 * @param wst The WstValueTable used for the interpolation.
357 * 356 *
358 * @return an array of data triples that consist of W, Q and Kms. 357 * @return an array of data triples that consist of W, Q and Kms.
359 */ 358 */
360 public static WQKms[] computeWaterlevelData( 359 public static CalculationResult computeWaterlevelData(
361 double [] kms, 360 double [] kms,
362 double [] qs, 361 double [] qs,
363 double [] ws, 362 double [] ws,
364 WstValueTable wst, 363 WstValueTable wst,
365 double refKm, 364 double refKm,
366 boolean up 365 boolean up
367 ) { 366 ) {
368 logger.info("WINFOArtifact.computeWaterlevelData"); 367 logger.info("WINFOArtifact.computeWaterlevelData");
369 368
370 Calculation1 calculation = new Calculation1(kms, qs, ws, refKm, up); 369 Calculation1 calc1 = new Calculation1(kms, qs, ws, refKm, up);
371 370
372 WQKms[] wqkms = calculation.calculate(wst); 371 return calc1.calculate(wst);
373
374 // TODO: report problems to user
375
376 return wqkms;
377 } 372 }
378 373
379 374
380 /** 375 /**
381 * Returns the data that is computed by a duration curve computation. 376 * Returns the data that is computed by a duration curve computation.
382 * 377 *
383 * @return the data computed by a duration curve computation. 378 * @return the data computed by a duration curve computation.
384 */ 379 */
385 public WQDay getDurationCurveData() 380 public CalculationResult getDurationCurveData() {
386 throws NullPointerException
387 {
388 logger.debug("WINFOArtifact.getDurationCurveData"); 381 logger.debug("WINFOArtifact.getDurationCurveData");
389 382
390 River r = getRiver(); 383 River r = getRiver();
391 384
392 if (r == null) { 385 if (r == null) {
393 throw new NullPointerException("Cannot determine river."); 386 return error(null, "Cannot determine river.");
394 } 387 }
395 388
396 Gauge g = getGauge(); 389 Gauge g = getGauge();
397 390
398 if (g == null) { 391 if (g == null) {
399 throw new NullPointerException("Cannot determine gauge."); 392 return error(null, "Cannot determine gauge.");
400 } 393 }
401 394
402 double[] locations = getLocations(); 395 double[] locations = getLocations();
403 396
404 if (locations == null) { 397 if (locations == null) {
405 throw new NullPointerException("Cannot determine location."); 398 return error(null, "Cannot determine location.");
406 } 399 }
407 400
408 WstValueTable wst = WstValueTableFactory.getTable(r); 401 WstValueTable wst = WstValueTableFactory.getTable(r);
409 if (wst == null) { 402 if (wst == null) {
410 throw new NullPointerException("No Wst found for selected river."); 403 return error(null, "No Wst found for selected river.");
411 } 404 }
412 405
413 return computeDurationCurveData(g, wst, locations[0]); 406 return computeDurationCurveData(g, wst, locations[0]);
414 } 407 }
415 408
420 * @param gauge The selected gauge. 413 * @param gauge The selected gauge.
421 * @param location The selected location. 414 * @param location The selected location.
422 * 415 *
423 * @return the computed data. 416 * @return the computed data.
424 */ 417 */
425 public static WQDay computeDurationCurveData( 418 public static CalculationResult computeDurationCurveData(
426 Gauge gauge, 419 Gauge gauge,
427 WstValueTable wst, 420 WstValueTable wst,
428 double location) 421 double location)
429 { 422 {
430 logger.info("WINFOArtifact.computeDurationCurveData"); 423 logger.info("WINFOArtifact.computeDurationCurveData");
434 int[] days = (int[]) obj[0]; 427 int[] days = (int[]) obj[0];
435 double[] qs = (double[]) obj[1]; 428 double[] qs = (double[]) obj[1];
436 429
437 Calculation3 calculation = new Calculation3(location, days, qs); 430 Calculation3 calculation = new Calculation3(location, days, qs);
438 431
439 // TODO: report the errors to the user.
440 return calculation.calculate(wst); 432 return calculation.calculate(wst);
441 } 433 }
442 434
443 435
444 /** 436 /**
475 /** 467 /**
476 * Returns the data that is computed by a discharge curve computation. 468 * Returns the data that is computed by a discharge curve computation.
477 * 469 *
478 * @return the data computed by a discharge curve computation. 470 * @return the data computed by a discharge curve computation.
479 */ 471 */
480 public WQKms getComputedDischargeCurveData() 472 public CalculationResult getComputedDischargeCurveData()
481 throws NullPointerException 473 throws NullPointerException
482 { 474 {
483 logger.debug("WINFOArtifact.getComputedDischargeCurveData"); 475 logger.debug("WINFOArtifact.getComputedDischargeCurveData");
484 476
485 River r = getRiver(); 477 River r = getRiver();
486 478
487 if (r == null) { 479 if (r == null) {
488 throw new NullPointerException("Cannot determine river."); 480 return error(new WQKms[0], "Cannot determine river.");
489 } 481 }
490 482
491 double[] locations = getLocations(); 483 double[] locations = getLocations();
492 484
493 if (locations == null) { 485 if (locations == null) {
494 throw new NullPointerException("Cannot determine location."); 486 return error(new WQKms[0], "Cannot determine location.");
495 } 487 }
496 488
497 WstValueTable wst = WstValueTableFactory.getTable(r); 489 WstValueTable wst = WstValueTableFactory.getTable(r);
498 if (wst == null) { 490 if (wst == null) {
499 throw new NullPointerException("No Wst found for selected river."); 491 return error(new WQKms[0], "No Wst found for selected river.");
500 } 492 }
501 493
502 WQKms wqkms = computeDischargeCurveData(wst, locations[0]); 494 return computeDischargeCurveData(wst, locations[0]);
503
504 return wqkms;
505 } 495 }
506 496
507 497
508 /** 498 /**
509 * Computes the data used to create computed discharge curves. 499 * Computes the data used to create computed discharge curves.
512 * @param location The location where the computation should be based on. 502 * @param location The location where the computation should be based on.
513 * 503 *
514 * @return an object that contains tuples of W/Q values at the specified 504 * @return an object that contains tuples of W/Q values at the specified
515 * location. 505 * location.
516 */ 506 */
517 public static WQKms computeDischargeCurveData( 507 public static CalculationResult computeDischargeCurveData(
518 WstValueTable wst, 508 WstValueTable wst,
519 double location) 509 double location)
520 { 510 {
521 logger.info("WINFOArtifact.computeDischargeCurveData"); 511 logger.info("WINFOArtifact.computeDischargeCurveData");
522 512
523 Calculation2 calculation = new Calculation2(location); 513 Calculation2 calculation = new Calculation2(location);
524 514
525 WQKms wqkms = calculation.calculate(wst); 515 return calculation.calculate(wst);
526 516 }
527 // TODO: Report errors to the user 517
528 518 protected static final CalculationResult error(Object data, String msg) {
529 return wqkms; 519 return new CalculationResult(data, new Calculation(msg));
530 } 520 }
531 521
532 /** 522 /**
533 * Returns the data computed by the discharge longitudinal section 523 * Returns the data computed by the discharge longitudinal section
534 * computation. 524 * computation.
535 * 525 *
536 * @return an array of WQKms object - one object for each given Q value. 526 * @return an array of WQKms object - one object for each given Q value.
537 */ 527 */
538 public WQKms [] getDischargeLongitudinalSectionData() { 528 public CalculationResult getDischargeLongitudinalSectionData() {
539 529
540 logger.debug("WINFOArtifact.getDischargeLongitudinalSectionData"); 530 logger.debug("WINFOArtifact.getDischargeLongitudinalSectionData");
541 531
542 River river = getRiver(); 532 River river = getRiver();
543 if (river == null) { 533 if (river == null) {
544 logger.error("No river selected."); 534 logger.debug("No river selected.");
545 return new WQKms[0]; 535 return error(new WQKms[0], "No river selected.");
546 } 536 }
547 537
548 WstValueTable table = WstValueTableFactory.getTable(river); 538 WstValueTable table = WstValueTableFactory.getTable(river);
549 if (table == null) { 539 if (table == null) {
550 logger.error("No wst found for selected river."); 540 logger.debug("No wst found for selected river.");
551 return new WQKms[0]; 541 return error(new WQKms[0], "No wst found for selected river.");
552 } 542 }
553 543
554 List<Segment> segments = getSegments(); 544 List<Segment> segments = getSegments();
555 545
556 if (segments == null) { 546 if (segments == null) {
557 logger.error("Cannot create segments."); 547 logger.debug("Cannot create segments.");
558 return new WQKms[0]; 548 return error(new WQKms[0], "Cannot create segments.");
559 } 549 }
560 550
561 double [] range = getFromToStep(); 551 double [] range = getFromToStep();
562 552
563 if (range == null) { 553 if (range == null) {
564 logger.error("Cannot figure out range."); 554 logger.debug("Cannot figure out range.");
565 return new WQKms[0]; 555 return error(new WQKms[0], "Cannot figure out range.");
566 } 556 }
567 557
568 Calculation4 calc4 = new Calculation4(segments, river, isQ()); 558 Calculation4 calc4 = new Calculation4(segments, river, isQ());
569 559
570 WQKms [] results = calc4.calculate(table, range[0], range[1], range[2]); 560 return calc4.calculate(table, range[0], range[1], range[2]);
571
572 return results;
573 } 561 }
574 } 562 }
575 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 563 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org