Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java @ 6374:48e92ff57f23
SedimentLoad*: Set range to fraction/fractions values.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Thu, 20 Jun 2013 09:06:23 +0200 |
parents | af13ceeba52a |
children | fa8425c8694c |
comparison
equal
deleted
inserted
replaced
6373:be283f9bc079 | 6374:48e92ff57f23 |
---|---|
11 import gnu.trove.TDoubleArrayList; | 11 import gnu.trove.TDoubleArrayList; |
12 | 12 |
13 import java.util.Calendar; | 13 import java.util.Calendar; |
14 import java.util.Date; | 14 import java.util.Date; |
15 import java.util.List; | 15 import java.util.List; |
16 import java.util.Map; | |
17 import java.util.TreeMap; | |
16 | 18 |
17 import net.sf.ehcache.Cache; | 19 import net.sf.ehcache.Cache; |
18 import net.sf.ehcache.Element; | 20 import net.sf.ehcache.Element; |
19 | 21 |
20 import org.apache.log4j.Logger; | 22 import org.apache.log4j.Logger; |
21 import org.hibernate.SQLQuery; | 23 import org.hibernate.SQLQuery; |
22 import org.hibernate.Session; | 24 import org.hibernate.Session; |
23 import org.hibernate.type.StandardBasicTypes; | 25 import org.hibernate.type.StandardBasicTypes; |
24 | 26 |
27 import org.dive4elements.river.model.MeasurementStation; | |
28 | |
25 import org.dive4elements.river.artifacts.cache.CacheFactory; | 29 import org.dive4elements.river.artifacts.cache.CacheFactory; |
30 | |
31 import org.dive4elements.river.artifacts.model.Range; | |
32 import org.dive4elements.river.artifacts.model.RiverFactory; | |
26 import org.dive4elements.river.artifacts.model.StaticSedimentLoadCacheKey; | 33 import org.dive4elements.river.artifacts.model.StaticSedimentLoadCacheKey; |
27 import org.dive4elements.river.backend.SessionHolder; | 34 import org.dive4elements.river.backend.SessionHolder; |
28 | 35 |
29 /** Pull Sediment Loads out of db. */ | 36 /** Pull Sediment Loads out of db. */ |
30 public class SedimentLoadFactory | 37 public class SedimentLoadFactory |
261 int eyear | 268 int eyear |
262 ) { | 269 ) { |
263 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached"); | 270 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached"); |
264 Session session = SessionHolder.HOLDER.get(); | 271 Session session = SessionHolder.HOLDER.get(); |
265 SQLQuery sqlQuery = null; | 272 SQLQuery sqlQuery = null; |
273 | |
274 List<MeasurementStation> allStations = RiverFactory.getRiver(river).getMeasurementStations(); | |
275 TreeMap<Double,MeasurementStation> floatStations = new TreeMap<Double, MeasurementStation>(); | |
276 TreeMap<Double,MeasurementStation> suspStations = new TreeMap<Double, MeasurementStation>(); | |
277 for (MeasurementStation measurementStation: allStations) { | |
278 if (measurementStation.getMeasurementType() == null || | |
279 measurementStation.getRange() == null) { | |
280 continue; | |
281 } | |
282 if (measurementStation.getMeasurementType().equals("Schwebstoff")) { | |
283 suspStations.put(measurementStation.getRange().getA().doubleValue(), measurementStation); | |
284 } | |
285 else if (measurementStation.getMeasurementType().equals("Geschiebe")) { | |
286 floatStations.put(measurementStation.getRange().getA().doubleValue(), measurementStation); | |
287 } | |
288 } | |
266 | 289 |
267 Calendar start = Calendar.getInstance(); | 290 Calendar start = Calendar.getInstance(); |
268 start.set(syear - 1, 11, 31); | 291 start.set(syear - 1, 11, 31); |
269 Calendar end = Calendar.getInstance(); | 292 Calendar end = Calendar.getInstance(); |
270 end.set(syear, 11, 30); | 293 end.set(syear, 11, 30); |
287 load = new SedimentLoad( | 310 load = new SedimentLoad( |
288 (String) row[0], | 311 (String) row[0], |
289 (Date) row[1], | 312 (Date) row[1], |
290 null, | 313 null, |
291 false); | 314 false); |
292 getValues("coarse", sqlQuery, load); | 315 getValues("coarse", sqlQuery, load, floatStations); |
293 getValues("fine_middle", sqlQuery, load); | 316 getValues("fine_middle", sqlQuery, load, floatStations); |
294 getValues("sand", sqlQuery, load); | 317 getValues("sand", sqlQuery, load, floatStations); |
295 getValues("suspended_sediment", sqlQuery, load); | 318 getValues("suspended_sediment", sqlQuery, load, suspStations); |
296 getValues("susp_sand_bed", sqlQuery, load); | 319 getValues("susp_sand_bed", sqlQuery, load, suspStations); |
297 getValues("susp_sand", sqlQuery, load); | 320 getValues("susp_sand", sqlQuery, load, suspStations); |
298 | 321 |
299 return load; | 322 return load; |
300 } | 323 } |
301 else if (type.equals("off_epoch")) { | 324 else if (type.equals("off_epoch")) { |
302 Calendar toStart = Calendar.getInstance(); | 325 Calendar toStart = Calendar.getInstance(); |
332 for (int i = 0; i < results.size(); i++) { | 355 for (int i = 0; i < results.size(); i++) { |
333 row = results.get(i); | 356 row = results.get(i); |
334 kms.add((Double)row[3]); | 357 kms.add((Double)row[3]); |
335 load.setLoadTotal((Double)row[3], (Double)row[2]); | 358 load.setLoadTotal((Double)row[3], (Double)row[2]); |
336 } | 359 } |
337 getValues("coarse", sqlQuery, load); | 360 getValues("coarse", sqlQuery, load, floatStations); |
338 getValues("fine_middle", sqlQuery, load); | 361 getValues("fine_middle", sqlQuery, load, floatStations); |
339 getValues("sand", sqlQuery, load); | 362 getValues("sand", sqlQuery, load, floatStations); |
340 getValues("suspended_sediment", sqlQuery, load); | 363 getValues("suspended_sediment", sqlQuery, load, suspStations); |
341 getValues("susp_sand_bed", sqlQuery, load); | 364 getValues("susp_sand_bed", sqlQuery, load, suspStations); |
342 getValues("susp_sand", sqlQuery, load); | 365 getValues("susp_sand", sqlQuery, load, floatStations); |
343 return load; | 366 return load; |
344 } | 367 } |
345 return new SedimentLoad(); | 368 return new SedimentLoad(); |
346 } | 369 } |
347 | 370 |
354 * @param load[out] SedimentLoad which to populate with values. | 377 * @param load[out] SedimentLoad which to populate with values. |
355 */ | 378 */ |
356 protected static void getValues ( | 379 protected static void getValues ( |
357 String fraction, | 380 String fraction, |
358 SQLQuery query, | 381 SQLQuery query, |
359 SedimentLoad load | 382 SedimentLoad load, |
383 TreeMap<Double, MeasurementStation> stations | |
360 ) { | 384 ) { |
361 query.setString("grain", fraction); | 385 query.setString("grain", fraction); |
362 List<Object[]> results = query.list(); | 386 List<Object[]> results = query.list(); |
363 for (int i = 0; i < results.size(); i++) { | 387 for (int i = 0; i < results.size(); i++) { |
364 Object[] row = results.get(i); | 388 Object[] row = results.get(i); |
365 double km = (Double)row[3]; | 389 double km = (Double)row[3]; |
390 MeasurementStation station = stations.get(km); | |
391 MeasurementStation nextStation = stations.ceilingEntry(km + 0.1d).getValue(); | |
392 Range range = null; | |
393 if (station == null) { | |
394 log.warn("No measurement station for " + fraction + " km " + km); | |
395 } | |
396 else { | |
397 if (nextRange != null) | |
398 range = new Range(station.getRange().getA().doubleValue(), | |
399 nextStation.getRange().getA().doubleValue()); | |
400 else { | |
401 // TODO end-of-river instead of B. | |
402 range = new Range(station.getRange().getA().doubleValue(), | |
403 station.getRange().getB().doubleValue()); | |
404 } | |
405 } | |
366 double v = -1; | 406 double v = -1; |
367 if (row[2] != null) { | 407 if (row[2] != null) { |
368 v = ((Double)row[2]).doubleValue(); | 408 v = ((Double)row[2]).doubleValue(); |
369 } | 409 } |
370 if (fraction.equals("coarse")) { | 410 if (fraction.equals("coarse")) { |
371 load.setCoarse(km, v); | 411 load.setCoarse(km, v, range); |
372 } | 412 } |
373 else if (fraction.equals("sand")) { | 413 else if (fraction.equals("sand")) { |
374 load.setSand(km, v); | 414 load.setSand(km, v, range); |
375 } | 415 } |
376 else if (fraction.equals("fine_middle")) { | 416 else if (fraction.equals("fine_middle")) { |
377 load.setFineMiddle(km, v); | 417 load.setFineMiddle(km, v, range); |
378 } | 418 } |
379 else if (fraction.equals("suspended_sediment")) { | 419 else if (fraction.equals("suspended_sediment")) { |
380 load.setSuspSediment(km, v); | 420 load.setSuspSediment(km, v, range); |
381 } | 421 } |
382 else if (fraction.equals("susp_sand")) { | 422 else if (fraction.equals("susp_sand")) { |
383 load.setSuspSand(km, v); | 423 load.setSuspSand(km, v, range); |
384 } | 424 } |
385 else if (fraction.equals("susp_sand_bed")) { | 425 else if (fraction.equals("susp_sand_bed")) { |
386 load.setSuspSandBed(km, v); | 426 load.setSuspSandBed(km, v, range); |
387 } | 427 } |
388 } | 428 } |
389 } | 429 } |
390 } | 430 } |
391 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 431 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |