Issue #1781 - Fix Hydro Time Series slow table loads

Change-Id: Icc0b916459a6636d9571417d102c8ac0226fb93d

Former-commit-id: acb0a63e9bc71e815c321b6a5aacf8ac339a752a
This commit is contained in:
Mike Duff 2013-03-12 09:03:08 -05:00
parent 41c7be5267
commit cac9bd6119

View file

@ -29,39 +29,45 @@ import com.raytheon.viz.hydrocommon.HydroConstants;
/**
* Utility class for Stage/Discharge conversions.
*
* <pre>
*
*
* SOFTWARE HISTORY
*
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 7, 2008 1194 mpduff Initial creation
* May 11,2011 9281 lbousaid nothing get display in right y-axis when
* there is no rating curve
* Jan 03,2013 15652 wkwock Fix stage to discharge
* Mar 12, 2013 1781 mpduff Fix stage2discharge flag preventing multiple db queries.
*
*
*
* </pre>
*
*
* @author mpduff
* @version 1.0
* @version 1.0
*/
public class StageDischargeUtils {
private static String previousLid = 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_SHIFT_QUERY = "select lid,date,shift_amount from ratingshift where lid = ':lid' and active='T' order by date desc";
private static boolean needToFindShiftAmount = false;
/**
* Returns the corresponding stage value for the discharge value passed in.
*
* @param gd the Graph Data
* @param stage the Discharge Value
* @param gd
* the Graph Data
* @param stage
* the Discharge Value
* @return the corresponding stage value
*/
public static double getStageFromDischarge(GraphData gd, double stage) {
@ -72,36 +78,40 @@ public class StageDischargeUtils {
break;
}
}
return ystage;
}
public static boolean checkRatingTable(String lid) {
boolean retVal = false;
try {
if ((ratingData == null) || !ratingData.getLid().equalsIgnoreCase(lid)) {
if ((ratingData == null)
|| !ratingData.getLid().equalsIgnoreCase(lid)) {
ratingData = queryRatingData(lid);
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) {
retVal = true;
}
/*return false if there is no data */
/* return false if there is no data */
else {
retVal = false;
retVal = false;
}
} else {
if (ratingData.getLid().equalsIgnoreCase(lid)) {
ratingData = queryRatingData(lid);
needToFindShiftAmount = true;
if (ratingData.getStage().size() > 2) {
retVal = true;
} else {
retVal = false;
}
ratingData = queryRatingData(lid);
needToFindShiftAmount = true;
if (ratingData.getStage().size() > 2) {
retVal = true;
} else {
retVal = false;
}
}
}
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@ -109,192 +119,203 @@ public class StageDischargeUtils {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVal;
}
/**
* 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 stage The Stage Value
* @param lid
* The Location ID
* @param stage
* The Stage Value
* @return the corresponding discharge
*/
public static double stage2discharge(String lid, double stage) {
/*
* Check to determine if the stage value is missing.
* If it is then return a flow value of missing.
*/
if ( stage == HydroConstants.MISSING_VALUE ) {
return HydroConstants.RATING_CONVERT_FAILED ;
}
double discharge = HydroConstants.MISSING_VALUE;
/*
* If the lid passed in is NOT the same as the previous lid
* then copy lid passed in to previous and get the rating curve
* Check to determine if the stage value is missing. If it is then
* return a flow value of missing.
*/
if (!lid.equals(previousLid)) {
previousLid = lid;
try {
if ((ratingData == null) || !ratingData.getLid().equalsIgnoreCase(lid)) {
needToFindShiftAmount = true;
ratingData = queryRatingData(lid);
}
} catch (VizException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Log no rating data here
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* if the pointer to the head is NULL then that means there
* is NO rating curve for that location
*/
if (ratingData == null) {
return HydroConstants.RATING_CONVERT_FAILED;
}
/*
* If there are less than 2 points (ie. one) then that means there
* is NO usable rating curve for that location
*/
if (ratingData.getDischarge().size() < 2) {
//TODO - Log this message printf("Rating table has less than 2 points for LID=%s\n", lid);
return HydroConstants.RATING_CONVERT_FAILED;
}
if (stage == HydroConstants.MISSING_VALUE) {
return HydroConstants.RATING_CONVERT_FAILED;
}
ArrayList<Object[]> ratingShiftArray = null;
double shiftAmount = 0;
ArrayList<Double> stageRatingCurve = ratingData.getStage();
/*
* Determine if there is a shift factor for this rating curve.
* If there is, then shift each point in the rating curve by this
* factor.
*/
if (needToFindShiftAmount) {
ratingShiftArray = queryRatingShift(lid);
if ((ratingShiftArray != null) && (ratingShiftArray.size() > 0)) {
Object[] shiftData = ratingShiftArray.get(0);
shiftAmount = (Double)shiftData[2];
double d;
for (int i = 0; i < stageRatingCurve.size(); i++) {
d = stageRatingCurve.get(i);
d += shiftAmount;
stageRatingCurve.set(i, d);
}
ratingData.setStage(stageRatingCurve);
needToFindShiftAmount = false;
}
}
ArrayList<Double> dischargeList = ratingData.getDischarge();
double minStage = stageRatingCurve.get(0);
double maxStage = stageRatingCurve.get(stageRatingCurve.size() - 1);
double minDischarge = dischargeList.get(0);
double maxDischarge = dischargeList.get(dischargeList.size() - 1);
/*
* if the stage value passed in is less then the lowest stage in the
* rating table then extrapolate the discharge
*/
if (stage < minStage) {
double nextStage = stageRatingCurve.get(1);
double stageDifference = nextStage - stage;
double dischargeDifference = dischargeList.get(1) - minDischarge;
if (stageDifference == 0) {
discharge = minDischarge;
} else {
discharge = minDischarge - ((dischargeDifference/stageDifference) *
(minStage - stage));
}
}
double discharge = HydroConstants.MISSING_VALUE;
/*
* if the stage value passed in is greater then the highest stage in the
* rating table then extrapolate the discharge
*/
if (stage > maxStage) {
double prevStage = stageRatingCurve.get(stageRatingCurve.size() - 2);
double dischargeDifference = maxDischarge - dischargeList.get(dischargeList.size() - 2);
double stageDifference = maxStage - prevStage;
if (stageDifference == 0) {
discharge = maxDischarge;
} else {
discharge = maxDischarge + ((dischargeDifference/stageDifference) * (stage - maxStage));
}
}
/*
* if the stage value passed in is between the lowest and highest stage
* in the rating table then interpolate the discharge
*/
if ( (stage >= minStage) && (stage <= (maxStage)) ) {
double lowerStage = minStage;
double lowerDischarge = minDischarge;
for (int i = 1; i < stageRatingCurve.size(); i++) {
double nextStage = stageRatingCurve.get(i);
double nextDischarge = dischargeList.get(i);
if ((stage >= lowerStage) && (stage <= nextStage)) {
double dischargeDifference = nextDischarge - lowerDischarge;
double stageDifference = nextStage - lowerStage;
if (stageDifference == 0) {
discharge = lowerDischarge;
} else {
discharge = lowerDischarge + ((dischargeDifference/stageDifference) *
(stage - lowerStage));
}
break;
}
lowerStage = nextStage;
lowerDischarge = nextDischarge;
}
}
/* If for some reason the discharge is < 0 then return missing */
if (discharge < 0) {
return HydroConstants.RATING_CONVERT_FAILED;
}
return discharge;
/*
* If the lid passed in is NOT the same as the previous lid then copy
* lid passed in to previous and get the rating curve
*/
if (!lid.equals(previousLid)) {
previousLid = lid;
try {
if ((ratingData == null)
|| !ratingData.getLid().equalsIgnoreCase(lid)) {
needToFindShiftAmount = true;
ratingData = queryRatingData(lid);
}
} catch (VizException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Log no rating data here
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* if the pointer to the head is NULL then that means there is NO rating
* curve for that location
*/
if (ratingData == null) {
return HydroConstants.RATING_CONVERT_FAILED;
}
/*
* If there are less than 2 points (ie. one) then that means there is NO
* usable rating curve for that location
*/
if (ratingData.getDischarge().size() < 2) {
// TODO - Log this message
// printf("Rating table has less than 2 points for LID=%s\n", lid);
return HydroConstants.RATING_CONVERT_FAILED;
}
ArrayList<Object[]> ratingShiftArray = null;
double shiftAmount = 0;
ArrayList<Double> stageRatingCurve = ratingData.getStage();
/*
* Determine if there is a shift factor for this rating curve. If there
* is, then shift each point in the rating curve by this factor.
*/
if (needToFindShiftAmount) {
ratingShiftArray = queryRatingShift(lid);
if ((ratingShiftArray != null) && (ratingShiftArray.size() > 0)) {
Object[] shiftData = ratingShiftArray.get(0);
shiftAmount = (Double) shiftData[2];
double d;
for (int i = 0; i < stageRatingCurve.size(); i++) {
d = stageRatingCurve.get(i);
d += shiftAmount;
stageRatingCurve.set(i, d);
}
ratingData.setStage(stageRatingCurve);
}
needToFindShiftAmount = false;
}
ArrayList<Double> dischargeList = ratingData.getDischarge();
double minStage = stageRatingCurve.get(0);
double maxStage = stageRatingCurve.get(stageRatingCurve.size() - 1);
double minDischarge = dischargeList.get(0);
double maxDischarge = dischargeList.get(dischargeList.size() - 1);
/*
* if the stage value passed in is less then the lowest stage in the
* rating table then extrapolate the discharge
*/
if (stage < minStage) {
double nextStage = stageRatingCurve.get(1);
double stageDifference = nextStage - stage;
double dischargeDifference = dischargeList.get(1) - minDischarge;
if (stageDifference == 0) {
discharge = minDischarge;
} else {
discharge = minDischarge
- ((dischargeDifference / stageDifference) * (minStage - stage));
}
}
/*
* if the stage value passed in is greater then the highest stage in the
* rating table then extrapolate the discharge
*/
if (stage > maxStage) {
double prevStage = stageRatingCurve
.get(stageRatingCurve.size() - 2);
double dischargeDifference = maxDischarge
- dischargeList.get(dischargeList.size() - 2);
double stageDifference = maxStage - prevStage;
if (stageDifference == 0) {
discharge = maxDischarge;
} else {
discharge = maxDischarge
+ ((dischargeDifference / stageDifference) * (stage - maxStage));
}
}
/*
* if the stage value passed in is between the lowest and highest stage
* in the rating table then interpolate the discharge
*/
if ((stage >= minStage) && (stage <= (maxStage))) {
double lowerStage = minStage;
double lowerDischarge = minDischarge;
for (int i = 1; i < stageRatingCurve.size(); i++) {
double nextStage = stageRatingCurve.get(i);
double nextDischarge = dischargeList.get(i);
if ((stage >= lowerStage) && (stage <= nextStage)) {
double dischargeDifference = nextDischarge - lowerDischarge;
double stageDifference = nextStage - lowerStage;
if (stageDifference == 0) {
discharge = lowerDischarge;
} else {
discharge = lowerDischarge
+ ((dischargeDifference / stageDifference) * (stage - lowerStage));
}
break;
}
lowerStage = nextStage;
lowerDischarge = nextDischarge;
}
}
/* If for some reason the discharge is < 0 then return missing */
if (discharge < 0) {
return HydroConstants.RATING_CONVERT_FAILED;
}
return discharge;
}
/**
* 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 stage The Stage Value
* @param lid
* The Location ID
* @param stage
* The Stage Value
* @return the corresponding discharge
*/
public static double discharge2stage(String lid, double discharge) {
/*
* Check to see if the discharge value is bad, i.e. missing.
* If it is bad, then return a stage value of missing.
* Check to see if the discharge value is bad, i.e. missing. If it is
* bad, then return a stage value of missing.
*/
if ( discharge < 0 ) {
return HydroConstants.RATING_CONVERT_FAILED ;
if (discharge < 0) {
return HydroConstants.RATING_CONVERT_FAILED;
}
double stage = HydroConstants.MISSING_VALUE;
boolean needToFindShiftAmount = false;
/*
* If the lid passed in is NOT the same as the previous lid
* then copy lid passed in to previous and get the rating curve
* If the lid passed in is NOT the same as the previous lid then copy
* lid passed in to previous and get the rating curve
*/
if (!lid.equals(previousLid)) {
previousLid = lid;
needToFindShiftAmount = true;
try {
if ((ratingData == null) || !ratingData.getLid().equalsIgnoreCase(lid)) {
if ((ratingData == null)
|| !ratingData.getLid().equalsIgnoreCase(lid)) {
ratingData = queryRatingData(lid);
needToFindShiftAmount = true;
}
@ -307,19 +328,20 @@ public class StageDischargeUtils {
}
/*
* if the pointer to the head is NULL then that means there
* is NO rating curve for that location
* if the pointer to the head is NULL then that means there is NO rating
* curve for that location
*/
if (ratingData == null) {
return HydroConstants.RATING_CONVERT_FAILED;
}
/*
* If there are less than 2 points (ie. one) then that means there
* is NO usable rating curve for that location
* If there are less than 2 points (ie. one) then that means there is NO
* usable rating curve for that location
*/
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;
}
@ -328,22 +350,22 @@ public class StageDischargeUtils {
ArrayList<Double> dischargeRatingCurve = ratingData.getDischarge();
/*
* Determine if there is a shift factor for this rating curve.
* If there is, then shift each point in the rating curve by this
* factor.
* Determine if there is a shift factor for this rating curve. If there
* is, then shift each point in the rating curve by this factor.
*/
if (needToFindShiftAmount) {
ratingShiftArray = queryRatingShift(lid);
ratingShiftArray = queryRatingShift(lid);
if ((ratingShiftArray != null) && (ratingShiftArray.size() > 0)) {
Object[] shiftData = ratingShiftArray.get(0);
shiftAmount = (Double)shiftData[2];
shiftAmount = (Double) shiftData[2];
}
}
ArrayList<Double> stageList = ratingData.getStage();
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 maxStage = stageList.get(stageList.size() - 1);
@ -358,103 +380,116 @@ public class StageDischargeUtils {
if (dischargeDifference == 0) {
stage = minStage;
} else {
stage = minStage - ((stageDifference/dischargeDifference) *
(minDischarge - discharge));
stage = minStage
- ((stageDifference / dischargeDifference) * (minDischarge - discharge));
}
}
/*
* if the discharge value passed in is greater then the highest discharge
* in the rating table then extrapolate the stage
* if the discharge value passed in is greater then the highest
* discharge in the rating table then extrapolate the stage
*/
if (discharge > maxDischarge) {
double prevDischarge = dischargeRatingCurve.get(dischargeRatingCurve.size() - 2);
double prevDischarge = dischargeRatingCurve
.get(dischargeRatingCurve.size() - 2);
double dischargeDifference = maxDischarge - prevDischarge;
double stageDifference = maxStage - stageList.get(stageList.size() - 2);
double stageDifference = maxStage
- stageList.get(stageList.size() - 2);
if (dischargeDifference == 0) {
stage = maxStage;
} else {
stage = maxStage + ((stageDifference/dischargeDifference) *
(discharge - maxDischarge));
stage = maxStage
+ ((stageDifference / dischargeDifference) * (discharge - maxDischarge));
}
}
/*
if the discharge value passed in is between the lowest and highest
discharge in the rating table then interpolate the stage
*/
if ((discharge <= maxDischarge) && (discharge >= minDischarge))
{
double lowerStage = minStage;
double lowerDischarge = minDischarge;
for (int i = 1; i < dischargeRatingCurve.size(); i++) {
double nextDischarge = dischargeRatingCurve.get(i);
double nextStage = stageList.get(i);
if ((discharge >= lowerDischarge) && (discharge <= nextDischarge)) {
double dischargeDifference = nextDischarge - lowerDischarge;
double stageDifference = nextStage - lowerStage;
}
if (dischargeDifference == 0) {
stage = lowerStage;
} else {
stage = lowerStage + ((stageDifference/dischargeDifference) *
(discharge - lowerDischarge));
}
break;
}
lowerStage = nextStage;
lowerDischarge = nextDischarge;
}
}
stage += shiftAmount;
return stage;
/*
* if the discharge value passed in is between the lowest and highest
* discharge in the rating table then interpolate the stage
*/
if ((discharge <= maxDischarge) && (discharge >= minDischarge)) {
double lowerStage = minStage;
double lowerDischarge = minDischarge;
for (int i = 1; i < dischargeRatingCurve.size(); i++) {
double nextDischarge = dischargeRatingCurve.get(i);
double nextStage = stageList.get(i);
if ((discharge >= lowerDischarge)
&& (discharge <= nextDischarge)) {
double dischargeDifference = nextDischarge - lowerDischarge;
double stageDifference = nextStage - lowerStage;
if (dischargeDifference == 0) {
stage = lowerStage;
} else {
stage = lowerStage
+ ((stageDifference / dischargeDifference) * (discharge - lowerDischarge));
}
break;
}
lowerStage = nextStage;
lowerDischarge = nextDischarge;
}
}
stage += shiftAmount;
return stage;
}
/**
* 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
* @throws VizException
*/
private static Rating queryRatingData(String lid) throws VizException, NullPointerException {
private static Rating queryRatingData(String lid) throws VizException,
NullPointerException {
/* Query the rating table */
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) {
//the Rating constructor already add stage and discharge to it. so clear it...
rating.getStage().clear();
rating.getDischarge().clear();
// the Rating constructor already add stage and discharge to it. so
// clear it...
rating.getStage().clear();
rating.getDischarge().clear();
for (int i = 0; i < results.size(); i++) {
Object[] sa = results.get(i);
if (((sa[1] != null) || (sa[1] != "")) && ((sa[2] != null) || (sa[2] != ""))) {
rating.addStage((Double)sa[1]);
rating.addDischarge((Double)sa[2]);
if (((sa[1] != null) || (sa[1] != ""))
&& ((sa[2] != null) || (sa[2] != ""))) {
rating.addStage((Double) sa[1]);
rating.addDischarge((Double) sa[2]);
}
}
}
return rating;
}
/**
* 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
* @return The data from the ratingShift table in IHFS, null if no data available
* @param lid
* The Location ID
* @return The data from the ratingShift table in IHFS, null if no data
* available
* @throws VizException
*/
private static ArrayList<Object[]> queryRatingShift(String lid) {
/* Query the ratingShift table */
ArrayList<Object[]> results = null;
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) {
e.printStackTrace();
} catch (NullPointerException e) {