Merge "Issue #1781 - Fix Hydro Time Series slow table loads" into omaha_13.3.1

Former-commit-id: 1df2884be78544697f629fca8c7fd0d09ce78aac
This commit is contained in:
Lee Venable 2013-03-12 09:08:44 -05:00 committed by Gerrit Code Review
commit 28d74d91b0

View file

@ -29,6 +29,7 @@ import com.raytheon.viz.hydrocommon.HydroConstants;
/** /**
* Utility class for Stage/Discharge conversions. * Utility class for Stage/Discharge conversions.
*
* <pre> * <pre>
* *
* SOFTWARE HISTORY * SOFTWARE HISTORY
@ -39,6 +40,8 @@ import com.raytheon.viz.hydrocommon.HydroConstants;
* May 11,2011 9281 lbousaid nothing get display in right y-axis when * May 11,2011 9281 lbousaid nothing get display in right y-axis when
* there is no rating curve * there is no rating curve
* Jan 03,2013 15652 wkwock Fix stage to discharge * Jan 03,2013 15652 wkwock Fix stage to discharge
* Mar 12, 2013 1781 mpduff Fix stage2discharge flag preventing multiple db queries.
*
* *
* *
* </pre> * </pre>
@ -49,6 +52,7 @@ import com.raytheon.viz.hydrocommon.HydroConstants;
public class StageDischargeUtils { public class StageDischargeUtils {
private static String previousLid = null; private static String previousLid = null;
private static Rating ratingData = null; private static Rating ratingData = null;
private static String RATING_QUERY = "select lid,stage,discharge from rating where lid=':lid' order by stage asc"; private static String RATING_QUERY = "select lid,stage,discharge from rating where lid=':lid' order by stage asc";
@ -60,8 +64,10 @@ public class StageDischargeUtils {
/** /**
* Returns the corresponding stage value for the discharge value passed in. * Returns the corresponding stage value for the discharge value passed in.
* *
* @param gd the Graph Data * @param gd
* @param stage the Discharge Value * the Graph Data
* @param stage
* the Discharge Value
* @return the corresponding stage value * @return the corresponding stage value
*/ */
public static double getStageFromDischarge(GraphData gd, double stage) { public static double getStageFromDischarge(GraphData gd, double stage) {
@ -79,10 +85,14 @@ public class StageDischargeUtils {
public static boolean checkRatingTable(String lid) { public static boolean checkRatingTable(String lid) {
boolean retVal = false; boolean retVal = false;
try { try {
if ((ratingData == null) || !ratingData.getLid().equalsIgnoreCase(lid)) { if ((ratingData == null)
|| !ratingData.getLid().equalsIgnoreCase(lid)) {
ratingData = queryRatingData(lid); ratingData = queryRatingData(lid);
needToFindShiftAmount = true; needToFindShiftAmount = true;
/* Check the Rating object for data and return true if data are available*/ /*
* Check the Rating object for data and return true if data are
* available
*/
if (ratingData.getStage().size() > 2) { if (ratingData.getStage().size() > 2) {
retVal = true; retVal = true;
} }
@ -114,16 +124,19 @@ public class StageDischargeUtils {
} }
/** /**
* Convert the stage to discharge for the location and stage value passed in. * Convert the stage to discharge for the location and stage value passed
* in.
* *
* @param lid The Location ID * @param lid
* @param stage The Stage Value * The Location ID
* @param stage
* The Stage Value
* @return the corresponding discharge * @return the corresponding discharge
*/ */
public static double stage2discharge(String lid, double stage) { public static double stage2discharge(String lid, double stage) {
/* /*
* Check to determine if the stage value is missing. * Check to determine if the stage value is missing. If it is then
* If it is then return a flow value of missing. * return a flow value of missing.
*/ */
if (stage == HydroConstants.MISSING_VALUE) { if (stage == HydroConstants.MISSING_VALUE) {
return HydroConstants.RATING_CONVERT_FAILED; return HydroConstants.RATING_CONVERT_FAILED;
@ -132,14 +145,15 @@ public class StageDischargeUtils {
double discharge = HydroConstants.MISSING_VALUE; double discharge = HydroConstants.MISSING_VALUE;
/* /*
* If the lid passed in is NOT the same as the previous lid * If the lid passed in is NOT the same as the previous lid then copy
* then copy lid passed in to previous and get the rating curve * lid passed in to previous and get the rating curve
*/ */
if (!lid.equals(previousLid)) { if (!lid.equals(previousLid)) {
previousLid = lid; previousLid = lid;
try { try {
if ((ratingData == null) || !ratingData.getLid().equalsIgnoreCase(lid)) { if ((ratingData == null)
|| !ratingData.getLid().equalsIgnoreCase(lid)) {
needToFindShiftAmount = true; needToFindShiftAmount = true;
ratingData = queryRatingData(lid); ratingData = queryRatingData(lid);
} }
@ -154,19 +168,20 @@ public class StageDischargeUtils {
} }
/* /*
* if the pointer to the head is NULL then that means there * if the pointer to the head is NULL then that means there is NO rating
* is NO rating curve for that location * curve for that location
*/ */
if (ratingData == null) { if (ratingData == null) {
return HydroConstants.RATING_CONVERT_FAILED; return HydroConstants.RATING_CONVERT_FAILED;
} }
/* /*
* If there are less than 2 points (ie. one) then that means there * If there are less than 2 points (ie. one) then that means there is NO
* is NO usable rating curve for that location * usable rating curve for that location
*/ */
if (ratingData.getDischarge().size() < 2) { if (ratingData.getDischarge().size() < 2) {
//TODO - Log this message printf("Rating table has less than 2 points for LID=%s\n", lid); // TODO - Log this message
// printf("Rating table has less than 2 points for LID=%s\n", lid);
return HydroConstants.RATING_CONVERT_FAILED; return HydroConstants.RATING_CONVERT_FAILED;
} }
@ -175,9 +190,8 @@ public class StageDischargeUtils {
ArrayList<Double> stageRatingCurve = ratingData.getStage(); ArrayList<Double> stageRatingCurve = ratingData.getStage();
/* /*
* Determine if there is a shift factor for this rating curve. * Determine if there is a shift factor for this rating curve. If there
* If there is, then shift each point in the rating curve by this * is, then shift each point in the rating curve by this factor.
* factor.
*/ */
if (needToFindShiftAmount) { if (needToFindShiftAmount) {
ratingShiftArray = queryRatingShift(lid); ratingShiftArray = queryRatingShift(lid);
@ -192,8 +206,8 @@ public class StageDischargeUtils {
stageRatingCurve.set(i, d); stageRatingCurve.set(i, d);
} }
ratingData.setStage(stageRatingCurve); ratingData.setStage(stageRatingCurve);
needToFindShiftAmount = false;
} }
needToFindShiftAmount = false;
} }
ArrayList<Double> dischargeList = ratingData.getDischarge(); ArrayList<Double> dischargeList = ratingData.getDischarge();
@ -213,8 +227,8 @@ public class StageDischargeUtils {
if (stageDifference == 0) { if (stageDifference == 0) {
discharge = minDischarge; discharge = minDischarge;
} else { } else {
discharge = minDischarge - ((dischargeDifference/stageDifference) * discharge = minDischarge
(minStage - stage)); - ((dischargeDifference / stageDifference) * (minStage - stage));
} }
} }
@ -223,13 +237,16 @@ public class StageDischargeUtils {
* rating table then extrapolate the discharge * rating table then extrapolate the discharge
*/ */
if (stage > maxStage) { if (stage > maxStage) {
double prevStage = stageRatingCurve.get(stageRatingCurve.size() - 2); double prevStage = stageRatingCurve
double dischargeDifference = maxDischarge - dischargeList.get(dischargeList.size() - 2); .get(stageRatingCurve.size() - 2);
double dischargeDifference = maxDischarge
- dischargeList.get(dischargeList.size() - 2);
double stageDifference = maxStage - prevStage; double stageDifference = maxStage - prevStage;
if (stageDifference == 0) { if (stageDifference == 0) {
discharge = maxDischarge; discharge = maxDischarge;
} else { } else {
discharge = maxDischarge + ((dischargeDifference/stageDifference) * (stage - maxStage)); discharge = maxDischarge
+ ((dischargeDifference / stageDifference) * (stage - maxStage));
} }
} }
@ -249,8 +266,8 @@ public class StageDischargeUtils {
if (stageDifference == 0) { if (stageDifference == 0) {
discharge = lowerDischarge; discharge = lowerDischarge;
} else { } else {
discharge = lowerDischarge + ((dischargeDifference/stageDifference) * discharge = lowerDischarge
(stage - lowerStage)); + ((dischargeDifference / stageDifference) * (stage - lowerStage));
} }
break; break;
} }
@ -268,16 +285,19 @@ public class StageDischargeUtils {
} }
/** /**
* Convert the discharge to stage for the location and discharge value passed in. * Convert the discharge to stage for the location and discharge value
* passed in.
* *
* @param lid The Location ID * @param lid
* @param stage The Stage Value * The Location ID
* @param stage
* The Stage Value
* @return the corresponding discharge * @return the corresponding discharge
*/ */
public static double discharge2stage(String lid, double discharge) { public static double discharge2stage(String lid, double discharge) {
/* /*
* Check to see if the discharge value is bad, i.e. missing. * Check to see if the discharge value is bad, i.e. missing. If it is
* If it is bad, then return a stage value of missing. * bad, then return a stage value of missing.
*/ */
if (discharge < 0) { if (discharge < 0) {
return HydroConstants.RATING_CONVERT_FAILED; return HydroConstants.RATING_CONVERT_FAILED;
@ -286,15 +306,16 @@ public class StageDischargeUtils {
boolean needToFindShiftAmount = false; boolean needToFindShiftAmount = false;
/* /*
* If the lid passed in is NOT the same as the previous lid * If the lid passed in is NOT the same as the previous lid then copy
* then copy lid passed in to previous and get the rating curve * lid passed in to previous and get the rating curve
*/ */
if (!lid.equals(previousLid)) { if (!lid.equals(previousLid)) {
previousLid = lid; previousLid = lid;
needToFindShiftAmount = true; needToFindShiftAmount = true;
try { try {
if ((ratingData == null) || !ratingData.getLid().equalsIgnoreCase(lid)) { if ((ratingData == null)
|| !ratingData.getLid().equalsIgnoreCase(lid)) {
ratingData = queryRatingData(lid); ratingData = queryRatingData(lid);
needToFindShiftAmount = true; needToFindShiftAmount = true;
} }
@ -307,19 +328,20 @@ public class StageDischargeUtils {
} }
/* /*
* if the pointer to the head is NULL then that means there * if the pointer to the head is NULL then that means there is NO rating
* is NO rating curve for that location * curve for that location
*/ */
if (ratingData == null) { if (ratingData == null) {
return HydroConstants.RATING_CONVERT_FAILED; return HydroConstants.RATING_CONVERT_FAILED;
} }
/* /*
* If there are less than 2 points (ie. one) then that means there * If there are less than 2 points (ie. one) then that means there is NO
* is NO usable rating curve for that location * usable rating curve for that location
*/ */
if (ratingData.getDischarge().size() < 2) { if (ratingData.getDischarge().size() < 2) {
//TODO - Log this message printf("Rating table has less than 2 points for LID=%s\n", lid); // TODO - Log this message
// printf("Rating table has less than 2 points for LID=%s\n", lid);
return HydroConstants.RATING_CONVERT_FAILED; return HydroConstants.RATING_CONVERT_FAILED;
} }
@ -328,9 +350,8 @@ public class StageDischargeUtils {
ArrayList<Double> dischargeRatingCurve = ratingData.getDischarge(); ArrayList<Double> dischargeRatingCurve = ratingData.getDischarge();
/* /*
* Determine if there is a shift factor for this rating curve. * Determine if there is a shift factor for this rating curve. If there
* If there is, then shift each point in the rating curve by this * is, then shift each point in the rating curve by this factor.
* factor.
*/ */
if (needToFindShiftAmount) { if (needToFindShiftAmount) {
ratingShiftArray = queryRatingShift(lid); ratingShiftArray = queryRatingShift(lid);
@ -343,7 +364,8 @@ public class StageDischargeUtils {
ArrayList<Double> stageList = ratingData.getStage(); ArrayList<Double> stageList = ratingData.getStage();
double minDischarge = dischargeRatingCurve.get(0); double minDischarge = dischargeRatingCurve.get(0);
double maxDischarge = dischargeRatingCurve.get(dischargeRatingCurve.size() - 1); double maxDischarge = dischargeRatingCurve.get(dischargeRatingCurve
.size() - 1);
double minStage = stageList.get(0); double minStage = stageList.get(0);
double maxStage = stageList.get(stageList.size() - 1); double maxStage = stageList.get(stageList.size() - 1);
@ -358,34 +380,35 @@ public class StageDischargeUtils {
if (dischargeDifference == 0) { if (dischargeDifference == 0) {
stage = minStage; stage = minStage;
} else { } else {
stage = minStage - ((stageDifference/dischargeDifference) * stage = minStage
(minDischarge - discharge)); - ((stageDifference / dischargeDifference) * (minDischarge - discharge));
} }
} }
/* /*
* if the discharge value passed in is greater then the highest discharge * if the discharge value passed in is greater then the highest
* in the rating table then extrapolate the stage * discharge in the rating table then extrapolate the stage
*/ */
if (discharge > maxDischarge) { if (discharge > maxDischarge) {
double prevDischarge = dischargeRatingCurve.get(dischargeRatingCurve.size() - 2); double prevDischarge = dischargeRatingCurve
.get(dischargeRatingCurve.size() - 2);
double dischargeDifference = maxDischarge - prevDischarge; double dischargeDifference = maxDischarge - prevDischarge;
double stageDifference = maxStage - stageList.get(stageList.size() - 2); double stageDifference = maxStage
- stageList.get(stageList.size() - 2);
if (dischargeDifference == 0) { if (dischargeDifference == 0) {
stage = maxStage; stage = maxStage;
} else { } else {
stage = maxStage + ((stageDifference/dischargeDifference) * stage = maxStage
(discharge - maxDischarge)); + ((stageDifference / dischargeDifference) * (discharge - maxDischarge));
} }
} }
/* /*
if the discharge value passed in is between the lowest and highest * if the discharge value passed in is between the lowest and highest
discharge in the rating table then interpolate the stage * discharge in the rating table then interpolate the stage
*/ */
if ((discharge <= maxDischarge) && (discharge >= minDischarge)) if ((discharge <= maxDischarge) && (discharge >= minDischarge)) {
{
double lowerStage = minStage; double lowerStage = minStage;
double lowerDischarge = minDischarge; double lowerDischarge = minDischarge;
@ -393,15 +416,16 @@ public class StageDischargeUtils {
double nextDischarge = dischargeRatingCurve.get(i); double nextDischarge = dischargeRatingCurve.get(i);
double nextStage = stageList.get(i); double nextStage = stageList.get(i);
if ((discharge >= lowerDischarge) && (discharge <= nextDischarge)) { if ((discharge >= lowerDischarge)
&& (discharge <= nextDischarge)) {
double dischargeDifference = nextDischarge - lowerDischarge; double dischargeDifference = nextDischarge - lowerDischarge;
double stageDifference = nextStage - lowerStage; double stageDifference = nextStage - lowerStage;
if (dischargeDifference == 0) { if (dischargeDifference == 0) {
stage = lowerStage; stage = lowerStage;
} else { } else {
stage = lowerStage + ((stageDifference/dischargeDifference) * stage = lowerStage
(discharge - lowerDischarge)); + ((stageDifference / dischargeDifference) * (discharge - lowerDischarge));
} }
break; break;
} }
@ -417,23 +441,29 @@ public class StageDischargeUtils {
/** /**
* Get the data from the IHFS Rating table for the specified Location Id. * Get the data from the IHFS Rating table for the specified Location Id.
* *
* @param lid The Location ID * @param lid
* The Location ID
* @return The data from the rating table in IHFS * @return The data from the rating table in IHFS
* @throws VizException * @throws VizException
*/ */
private static Rating queryRatingData(String lid) throws VizException, NullPointerException { private static Rating queryRatingData(String lid) throws VizException,
NullPointerException {
/* Query the rating table */ /* Query the rating table */
Rating rating = new Rating(lid); Rating rating = new Rating(lid);
List<Object[]> results = DirectDbQuery.executeQuery(RATING_QUERY.replace(":lid", lid), HydroConstants.IHFS, QueryLanguage.SQL); List<Object[]> results = DirectDbQuery.executeQuery(
RATING_QUERY.replace(":lid", lid), HydroConstants.IHFS,
QueryLanguage.SQL);
if (results != null) { if (results != null) {
//the Rating constructor already add stage and discharge to it. so clear it... // the Rating constructor already add stage and discharge to it. so
// clear it...
rating.getStage().clear(); rating.getStage().clear();
rating.getDischarge().clear(); rating.getDischarge().clear();
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
Object[] sa = results.get(i); Object[] sa = results.get(i);
if (((sa[1] != null) || (sa[1] != "")) && ((sa[2] != null) || (sa[2] != ""))) { if (((sa[1] != null) || (sa[1] != ""))
&& ((sa[2] != null) || (sa[2] != ""))) {
rating.addStage((Double) sa[1]); rating.addStage((Double) sa[1]);
rating.addDischarge((Double) sa[2]); rating.addDischarge((Double) sa[2]);
} }
@ -444,17 +474,22 @@ public class StageDischargeUtils {
} }
/** /**
* Get the data from the IHFS RatingShift table for the specified Location Id. * Get the data from the IHFS RatingShift table for the specified Location
* Id.
* *
* @param lid The Location ID * @param lid
* @return The data from the ratingShift table in IHFS, null if no data available * The Location ID
* @return The data from the ratingShift table in IHFS, null if no data
* available
* @throws VizException * @throws VizException
*/ */
private static ArrayList<Object[]> queryRatingShift(String lid) { private static ArrayList<Object[]> queryRatingShift(String lid) {
/* Query the ratingShift table */ /* Query the ratingShift table */
ArrayList<Object[]> results = null; ArrayList<Object[]> results = null;
try { try {
results = (ArrayList<Object[]>)DirectDbQuery.executeQuery(RATING_SHIFT_QUERY.replace(":lid", lid), HydroConstants.IHFS, QueryLanguage.SQL); results = (ArrayList<Object[]>) DirectDbQuery.executeQuery(
RATING_SHIFT_QUERY.replace(":lid", lid),
HydroConstants.IHFS, QueryLanguage.SQL);
} catch (VizException e) { } catch (VizException e) {
e.printStackTrace(); e.printStackTrace();
} catch (NullPointerException e) { } catch (NullPointerException e) {