Issue #1394: Reduce GFE Grid Database calls

Change-Id: I7462cca18b1b46169b25611dddbad844895be0cd

Former-commit-id: ac41861f0b [formerly 3c3c695f24] [formerly ac41861f0b [formerly 3c3c695f24] [formerly 0670ac79fa [formerly bc127f06236cb1e09dc0c87fee578e4a0cb60063]]]
Former-commit-id: 0670ac79fa
Former-commit-id: 912b2ec928 [formerly 0cdb86b6ac]
Former-commit-id: 9e837ef99c
This commit is contained in:
Richard Peter 2012-12-07 11:17:22 -06:00
parent 5e23a89003
commit d3b4ae2fdb
5 changed files with 325 additions and 244 deletions

View file

@ -90,12 +90,12 @@ public class IFPServerConfigManager {
*/ */
public static IFPServerConfig getServerConfig(String siteID) public static IFPServerConfig getServerConfig(String siteID)
throws GfeConfigurationException { throws GfeConfigurationException {
if (configMap.containsKey(siteID)) { IFPServerConfig config = configMap.get(siteID);
return configMap.get(siteID); if (config == null) {
} else {
throw new GfeConfigurationException("Site " + siteID throw new GfeConfigurationException("Site " + siteID
+ " has no active GFE configuration."); + " has no active GFE configuration.");
} }
return config;
} }
/** /**

View file

@ -31,18 +31,16 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.hibernate.Criteria;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property; import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionCallbackWithoutResult;
@ -74,7 +72,6 @@ import com.raytheon.uf.common.dataplugin.gfe.type.Pair;
import com.raytheon.uf.common.dataplugin.gfe.util.GfeUtil; import com.raytheon.uf.common.dataplugin.gfe.util.GfeUtil;
import com.raytheon.uf.common.dataplugin.grid.GridConstants; import com.raytheon.uf.common.dataplugin.grid.GridConstants;
import com.raytheon.uf.common.dataplugin.grid.GridInfoConstants; import com.raytheon.uf.common.dataplugin.grid.GridInfoConstants;
import com.raytheon.uf.common.dataplugin.grid.GridInfoRecord;
import com.raytheon.uf.common.dataplugin.grid.GridRecord; import com.raytheon.uf.common.dataplugin.grid.GridRecord;
import com.raytheon.uf.common.dataplugin.level.Level; import com.raytheon.uf.common.dataplugin.level.Level;
import com.raytheon.uf.common.dataplugin.level.LevelFactory; import com.raytheon.uf.common.dataplugin.level.LevelFactory;
@ -108,12 +105,25 @@ import com.raytheon.uf.edex.database.query.DatabaseQuery;
* with A1. * with A1.
* 10/10/12 #1260 randerso Added check to ensure db can be created before * 10/10/12 #1260 randerso Added check to ensure db can be created before
* adding it to the inventory * adding it to the inventory
* 12/06/12 #1394 rjpeter Optimized D2D grid access.
* </pre> * </pre>
* *
* @author bphillip * @author bphillip
* @version 1.0 * @version 1.0
*/ */
public class GFEDao extends DefaultPluginDao { public class GFEDao extends DefaultPluginDao {
// hibernate query to find grid info record for the given datasetId and
// parameter
private static final String HQL_D2D_GRID_PARM_QUERY = "select parameter.abbreviation, id from GridInfoRecord "
+ "where datasetId = :datasetId AND (lower(parameter.abbreviation) = :abbrev OR lower(parameter_abbreviation) like :hourAbbrev)";
// hibernate query to find the times for the GridRecord for the given
// info.id, id returned to allow easy lookup of the record associated with
// the time
private static final String HQL_D2D_GRID_TIME_QUERY = "select dataTime, id from GridRecord "
+ "where info.id = :info_id AND dataTime.refTime = :refTime order by dataTime.fcstTime";
private static final Pattern WIND_PATTERN = Pattern.compile("wind");
public GFEDao() throws PluginException { public GFEDao() throws PluginException {
super("gfe"); super("gfe");
@ -300,7 +310,12 @@ public class GFEDao extends DefaultPluginDao {
} }
} }
if (sess != null) { if (sess != null) {
try {
sess.close(); sess.close();
} catch (Exception e) {
statusHandler.error(
"Error occurred closing database session", e);
}
} }
} }
@ -611,85 +626,116 @@ public class GFEDao extends DefaultPluginDao {
} }
/** /**
* Retrieves a GridRecord from the grib metadata database based on a ParmID * Retrieves a GridRecord from the grib metadata database based on a ParmID,
* and a TimeRange * TimeRange, and GridParmInfo.
* *
* @param id * @param id
* The parmID of the desired GridRecord * The parmID of the desired GridRecord
* @param timeRange * @param timeRange
* The timeRange of the desired GridRecord * The timeRange of the desired GridRecord
* @param info
* The GridParmInfo for the requested d2d grid.
* @return The GridRecord from the grib metadata database * @return The GridRecord from the grib metadata database
* @throws DataAccessLayerException * @throws DataAccessLayerException
* If errors occur while querying the metadata database * If errors occur while querying the metadata database
*/ */
public GridRecord getD2DGrid(ParmID id, TimeRange timeRange, public GridRecord getD2DGrid(ParmID id, TimeRange timeRange,
GridParmInfo info) throws DataAccessLayerException { GridParmInfo info) throws DataAccessLayerException {
List<GridRecord> records = queryByD2DParmId(id); Session s = null;
try {
s = getHibernateTemplate().getSessionFactory().openSession();
// TODO: clean up so we only make one db query
SortedMap<DataTime, Integer> rawTimes = queryByD2DParmId(id, s);
List<TimeRange> gribTimes = new ArrayList<TimeRange>(); List<TimeRange> gribTimes = new ArrayList<TimeRange>();
for (GridRecord record : records) { for (DataTime dt : rawTimes.keySet()) {
gribTimes.add(record.getDataTime().getValidPeriod()); gribTimes.add(dt.getValidPeriod());
} }
for (int i = 0; i < records.size(); i++) {
TimeRange gribTime = records.get(i).getDataTime().getValidPeriod();
try { try {
if (isMos(id)) { if (isMos(id)) {
TimeRange time = info.getTimeConstraints().constraintTime( for (Map.Entry<DataTime, Integer> timeEntry : rawTimes
gribTime.getEnd()); .entrySet()) {
TimeRange gribTime = timeEntry.getKey()
.getValidPeriod();
TimeRange time = info.getTimeConstraints()
.constraintTime(gribTime.getEnd());
if (timeRange.getEnd().equals(time.getEnd()) if (timeRange.getEnd().equals(time.getEnd())
|| !info.getTimeConstraints().anyConstraints()) { || !info.getTimeConstraints().anyConstraints()) {
GridRecord retVal = records.get(i); GridRecord retVal = (GridRecord) s.get(
GridRecord.class, timeEntry.getValue());
retVal.setPluginName(GridConstants.GRID); retVal.setPluginName(GridConstants.GRID);
return retVal; return retVal;
} }
}
} else if (D2DGridDatabase.isNonAccumDuration(id, gribTimes)) { } else if (D2DGridDatabase.isNonAccumDuration(id, gribTimes)) {
for (Map.Entry<DataTime, Integer> timeEntry : rawTimes
.entrySet()) {
TimeRange gribTime = timeEntry.getKey()
.getValidPeriod();
if (timeRange.getStart().equals(gribTime.getEnd()) if (timeRange.getStart().equals(gribTime.getEnd())
|| timeRange.equals(gribTime)) { || timeRange.equals(gribTime)) {
GridRecord retVal = records.get(i); GridRecord retVal = (GridRecord) s.get(
GridRecord.class, timeEntry.getValue());
retVal.setPluginName(GridConstants.GRID); retVal.setPluginName(GridConstants.GRID);
return retVal; return retVal;
} }
}
} else { } else {
TimeRange time = info.getTimeConstraints().constraintTime( for (Map.Entry<DataTime, Integer> timeEntry : rawTimes
gribTime.getStart()); .entrySet()) {
TimeRange gribTime = timeEntry.getKey()
.getValidPeriod();
TimeRange time = info.getTimeConstraints()
.constraintTime(gribTime.getStart());
if ((timeRange.getStart().equals(time.getStart()) || !info if ((timeRange.getStart().equals(time.getStart()) || !info
.getTimeConstraints().anyConstraints())) { .getTimeConstraints().anyConstraints())) {
GridRecord retVal = records.get(i); GridRecord retVal = (GridRecord) s.get(
GridRecord.class, timeEntry.getValue());
retVal.setPluginName(GridConstants.GRID); retVal.setPluginName(GridConstants.GRID);
return retVal; return retVal;
} }
} }
}
} catch (GfeConfigurationException e) { } catch (GfeConfigurationException e) {
throw new DataAccessLayerException( throw new DataAccessLayerException(
"Error getting configuration for " "Error getting configuration for "
+ id.getDbId().getSiteId(), e); + id.getDbId().getSiteId(), e);
} }
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
statusHandler.error(
"Error occurred closing database session", e);
}
}
} }
return null; return null;
} }
/** /**
* Gets a list of GridRecords from the grib metadata database which match * Gets a SortedMap of DataTime and GridRecord ids from the grib metadata
* the given ParmID * database which match the given ParmID. Session passed to allow reuse
* across multiple calls.
* *
* @param id * @param id
* The ParmID to search with * The ParmID to search with
* @param s
* The database session to use
* @return The list of GridRecords from the grib metadata database which * @return The list of GridRecords from the grib metadata database which
* match the given ParmID * match the given ParmID
* @throws DataAccessLayerException * @throws DataAccessLayerException
* If errors occur while querying the metadata database * If errors occur while querying the metadata database
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<GridRecord> queryByD2DParmId(ParmID id) public SortedMap<DataTime, Integer> queryByD2DParmId(ParmID id, Session s)
throws DataAccessLayerException { throws DataAccessLayerException {
Session s = null;
try {
String levelName = GridTranslator.getLevelName(id.getParmLevel()); String levelName = GridTranslator.getLevelName(id.getParmLevel());
double[] levelValues = GridTranslator.getLevelValue(id double[] levelValues = GridTranslator.getLevelValue(id.getParmLevel());
.getParmLevel());
boolean levelOnePresent = (levelValues[0] != Level boolean levelOnePresent = (levelValues[0] != Level
.getInvalidLevelValue()); .getInvalidLevelValue());
boolean levelTwoPresent = (levelValues[1] != Level boolean levelTwoPresent = (levelValues[1] != Level
@ -713,22 +759,17 @@ public class GFEDao extends DefaultPluginDao {
if (level == null) { if (level == null) {
logger.warn("Unable to query D2D parms, ParmID " + id logger.warn("Unable to query D2D parms, ParmID " + id
+ " does not map to a level"); + " does not map to a level");
return new ArrayList<GridRecord>(); return new TreeMap<DataTime, Integer>();
} }
s = getHibernateTemplate().getSessionFactory().openSession(); Query modelQuery = s.createQuery(HQL_D2D_GRID_PARM_QUERY);
Criteria modelCrit = s.createCriteria(GridInfoRecord.class);
Criterion baseCrit = Restrictions
.eq(GridInfoConstants.LEVEL, level);
DatabaseID dbId = id.getDbId(); DatabaseID dbId = id.getDbId();
try { try {
IFPServerConfig config = IFPServerConfigManager IFPServerConfig config = IFPServerConfigManager
.getServerConfig(dbId.getSiteId()); .getServerConfig(dbId.getSiteId());
baseCrit = Restrictions.and(baseCrit, Restrictions.eq( modelQuery.setString(GridInfoConstants.DATASET_ID,
GridInfoConstants.DATASET_ID, config.d2dModelNameMapping(dbId.getModelName()));
config.d2dModelNameMapping(dbId.getModelName())));
} catch (GfeConfigurationException e) { } catch (GfeConfigurationException e) {
throw new DataAccessLayerException( throw new DataAccessLayerException(
"Error occurred looking up model name mapping", e); "Error occurred looking up model name mapping", e);
@ -739,80 +780,77 @@ public class GFEDao extends DefaultPluginDao {
abbreviation = id.getParmName(); abbreviation = id.getParmName();
} }
abbreviation = abbreviation.toLowerCase(); abbreviation = abbreviation.toLowerCase();
Criterion abbrevCrit = Restrictions modelQuery.setString("abbrev", abbreviation);
.and(baseCrit, modelQuery.setString("hourAbbrev", abbreviation + "%hr");
Restrictions.or( List<?> results = modelQuery.list();
Restrictions Integer modelId = null;
.sqlRestriction("lower(parameter_abbreviation) = '"
+ abbreviation + "'"),
Restrictions
.sqlRestriction("lower(parameter_abbreviation) like '"
+ abbreviation + "%hr'")));
modelCrit.add(abbrevCrit);
List<?> results = modelCrit.list();
GridInfoRecord model = null;
if (results.size() == 0) { if (results.size() == 0) {
return new ArrayList<GridRecord>(0); return new TreeMap<DataTime, Integer>();
} else if (results.size() > 1) { } else if (results.size() > 1) {
// hours matched, take hour with least number that matches exact // hours matched, take hour with least number that matches exact
// param // param
Pattern p = Pattern.compile("^" + abbreviation + "(\\d+)hr$"); Pattern p = Pattern.compile("^" + abbreviation + "(\\d+)hr$");
int lowestHr = -1; int lowestHr = -1;
for (GridInfoRecord m : (List<GridInfoRecord>) results) { for (Object[] rows : (List<Object[]>) results) {
String param = m.getParameter().getAbbreviation() String param = ((String) rows[0]).toLowerCase();
.toLowerCase();
if (param.equals(abbreviation) && (lowestHr < 0)) { if (param.equals(abbreviation) && (lowestHr < 0)) {
model = m; modelId = (Integer) rows[1];
} else { } else {
Matcher matcher = p.matcher(param); Matcher matcher = p.matcher(param);
if (matcher.matches()) { if (matcher.matches()) {
int hr = Integer.parseInt(matcher.group(1)); int hr = Integer.parseInt(matcher.group(1));
if ((lowestHr < 0) || (hr < lowestHr)) { if ((lowestHr < 0) || (hr < lowestHr)) {
model = m; modelId = (Integer) rows[1];
lowestHr = hr; lowestHr = hr;
} }
} }
} }
} }
} else { } else {
model = (GridInfoRecord) results.get(0); modelId = (Integer) ((Object[]) results.get(0))[1];
} }
Criteria recordCrit = s.createCriteria(GridRecord.class); Query timeQuery = s.createQuery(HQL_D2D_GRID_TIME_QUERY);
baseCrit = Restrictions.eq("info", model); timeQuery.setInteger("info_id", modelId);
baseCrit = Restrictions.and( timeQuery.setParameter("refTime", dbId.getModelTimeAsDate());
baseCrit, List<Object[]> timeResults = timeQuery.list();
Restrictions.eq("dataTime.refTime", if (timeResults.isEmpty()) {
dbId.getModelTimeAsDate())); return new TreeMap<DataTime, Integer>();
recordCrit.add(baseCrit);
recordCrit.addOrder(Order.asc("dataTime.fcstTime"));
return recordCrit.list();
} finally {
if (s != null) {
s.flush();
s.close();
} }
SortedMap<DataTime, Integer> dataTimes = new TreeMap<DataTime, Integer>();
for (Object[] rows : timeResults) {
dataTimes.put((DataTime) rows[0], (Integer) rows[1]);
} }
return dataTimes;
} }
public List<TimeRange> queryTimeByD2DParmId(ParmID id) public List<TimeRange> queryTimeByD2DParmId(ParmID id)
throws DataAccessLayerException { throws DataAccessLayerException {
List<TimeRange> timeList = new ArrayList<TimeRange>(); List<TimeRange> timeList = new ArrayList<TimeRange>();
Session s = null;
try {
s = getHibernateTemplate().getSessionFactory().openSession();
if (id.getParmName().equalsIgnoreCase("wind")) { if (id.getParmName().equalsIgnoreCase("wind")) {
ParmID uWindId = new ParmID(id.toString().replace("wind", "uW")); String idString = id.toString();
List<TimeRange> uTimeList = new ArrayList<TimeRange>(); Matcher idWindMatcher = WIND_PATTERN.matcher(idString);
List<DataTime> results = executeD2DParmQuery(uWindId);
for (DataTime o : results) { ParmID uWindId = new ParmID(idWindMatcher.replaceAll("uW"));
SortedMap<DataTime, Integer> results = queryByD2DParmId(
uWindId, s);
List<TimeRange> uTimeList = new ArrayList<TimeRange>(
results.size());
for (DataTime o : results.keySet()) {
uTimeList.add(new TimeRange(o.getValidPeriod().getStart(), uTimeList.add(new TimeRange(o.getValidPeriod().getStart(),
3600 * 1000)); 3600 * 1000));
} }
ParmID vWindId = new ParmID(id.toString().replace("wind", "vW")); ParmID vWindId = new ParmID(idWindMatcher.replaceAll("vW"));
List<TimeRange> vTimeList = new ArrayList<TimeRange>(); results = queryByD2DParmId(vWindId, s);
results = executeD2DParmQuery(vWindId); Set<TimeRange> vTimeList = new HashSet<TimeRange>(
for (DataTime o : results) { results.size(), 1);
for (DataTime o : results.keySet()) {
vTimeList.add(new TimeRange(o.getValidPeriod().getStart(), vTimeList.add(new TimeRange(o.getValidPeriod().getStart(),
3600 * 1000)); 3600 * 1000));
} }
@ -827,18 +865,20 @@ public class GFEDao extends DefaultPluginDao {
return timeList; return timeList;
} }
ParmID sWindId = new ParmID(id.toString().replace("wind", "ws")); ParmID sWindId = new ParmID(idWindMatcher.replaceAll("ws"));
List<TimeRange> sTimeList = new ArrayList<TimeRange>(); results = queryByD2DParmId(sWindId, s);
results = executeD2DParmQuery(sWindId); List<TimeRange> sTimeList = new ArrayList<TimeRange>(
for (DataTime o : results) { results.size());
for (DataTime o : results.keySet()) {
sTimeList.add(new TimeRange(o.getValidPeriod().getStart(), sTimeList.add(new TimeRange(o.getValidPeriod().getStart(),
3600 * 1000)); 3600 * 1000));
} }
ParmID dWindId = new ParmID(id.toString().replace("wind", "wd")); ParmID dWindId = new ParmID(idWindMatcher.replaceAll("wd"));
List<TimeRange> dTimeList = new ArrayList<TimeRange>(); results = queryByD2DParmId(dWindId, s);
results = executeD2DParmQuery(dWindId); Set<TimeRange> dTimeList = new HashSet<TimeRange>(
for (DataTime o : results) { results.size(), 1);
for (DataTime o : results.keySet()) {
dTimeList.add(new TimeRange(o.getValidPeriod().getStart(), dTimeList.add(new TimeRange(o.getValidPeriod().getStart(),
3600 * 1000)); 3600 * 1000));
} }
@ -848,36 +888,33 @@ public class GFEDao extends DefaultPluginDao {
timeList.add(new TimeRange(tr.getStart(), tr.getStart())); timeList.add(new TimeRange(tr.getStart(), tr.getStart()));
} }
} }
} else {
if (!timeList.isEmpty()) { SortedMap<DataTime, Integer> results = queryByD2DParmId(id, s);
return timeList; if (isMos(id)) {
for (DataTime o : results.keySet()) {
timeList.add(new TimeRange(o.getValidPeriod().getEnd(),
o.getValidPeriod().getDuration()));
} }
} else { } else {
List<DataTime> results = executeD2DParmQuery(id); for (DataTime o : results.keySet()) {
for (DataTime o : results) {
if (isMos(id)) {
timeList.add(new TimeRange(o.getValidPeriod().getEnd(), o
.getValidPeriod().getDuration()));
} else {
timeList.add(o.getValidPeriod()); timeList.add(o.getValidPeriod());
} }
}
}
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
statusHandler.error(
"Error occurred closing database session", e);
}
} }
} }
return timeList; return timeList;
} }
private List<DataTime> executeD2DParmQuery(ParmID id)
throws DataAccessLayerException {
List<DataTime> times = new ArrayList<DataTime>();
List<GridRecord> records = queryByD2DParmId(id);
for (GridRecord record : records) {
times.add(record.getDataTime());
}
return times;
}
public void purgeGFEGrids(final DatabaseID dbId) { public void purgeGFEGrids(final DatabaseID dbId) {
txTemplate.execute(new TransactionCallbackWithoutResult() { txTemplate.execute(new TransactionCallbackWithoutResult() {
@Override @Override

View file

@ -108,12 +108,12 @@ public class D2DGridDatabase extends VGridDatabase {
.getNamedHandler("GFEPerformanceLogger"); .getNamedHandler("GFEPerformanceLogger");
/** The remap object used for resampling grids */ /** The remap object used for resampling grids */
private Map<Integer, RemapGrid> remap = new HashMap<Integer, RemapGrid>(); private final Map<Integer, RemapGrid> remap = new HashMap<Integer, RemapGrid>();
/** The destination GridLocation (The local GFE grid coverage) */ /** The destination GridLocation (The local GFE grid coverage) */
private GridLocation outputLoc; private GridLocation outputLoc;
private List<ParmID> parms; private final List<ParmID> parms;
public static final String GRID_LOCATION_CACHE_KEY = "GfeLocations"; public static final String GRID_LOCATION_CACHE_KEY = "GfeLocations";
@ -394,7 +394,7 @@ public class D2DGridDatabase extends VGridDatabase {
public ServerResponse<List<IGridSlice>> getGridData(ParmID id, public ServerResponse<List<IGridSlice>> getGridData(ParmID id,
List<TimeRange> timeRanges) { List<TimeRange> timeRanges) {
List<IGridSlice> data = new ArrayList<IGridSlice>(); List<IGridSlice> data = new ArrayList<IGridSlice>(timeRanges.size());
ServerResponse<List<IGridSlice>> sr = new ServerResponse<List<IGridSlice>>(); ServerResponse<List<IGridSlice>> sr = new ServerResponse<List<IGridSlice>>();
for (TimeRange tr : timeRanges) { for (TimeRange tr : timeRanges) {
GridParmInfo gpi = getGridParmInfo(id).getPayload(); GridParmInfo gpi = getGridParmInfo(id).getPayload();
@ -415,7 +415,7 @@ public class D2DGridDatabase extends VGridDatabase {
public ServerResponse<List<IGridSlice>> getGridData(ParmID id, public ServerResponse<List<IGridSlice>> getGridData(ParmID id,
List<TimeRange> timeRanges, boolean convertUnit) { List<TimeRange> timeRanges, boolean convertUnit) {
List<IGridSlice> data = new ArrayList<IGridSlice>(); List<IGridSlice> data = new ArrayList<IGridSlice>(timeRanges.size());
ServerResponse<List<IGridSlice>> sr = new ServerResponse<List<IGridSlice>>(); ServerResponse<List<IGridSlice>> sr = new ServerResponse<List<IGridSlice>>();
for (TimeRange tr : timeRanges) { for (TimeRange tr : timeRanges) {
GridParmInfo gpi = getGridParmInfo(id).getPayload(); GridParmInfo gpi = getGridParmInfo(id).getPayload();
@ -429,6 +429,7 @@ public class D2DGridDatabase extends VGridDatabase {
+ " TimeRange: " + tr, e); + " TimeRange: " + tr, e);
} }
} }
sr.setPayload(data); sr.setPayload(data);
return sr; return sr;
} }
@ -550,7 +551,7 @@ public class D2DGridDatabase extends VGridDatabase {
RemapGrid remap = getOrCreateRemap(d2dRecord.getLocation()); RemapGrid remap = getOrCreateRemap(d2dRecord.getLocation());
retVal = remap.remap(bdata, fillV, gpi.getMaxValue(), retVal = remap.remap(bdata, fillV, gpi.getMaxValue(),
gpi.getMinValue(), gpi.getMinValue()); gpi.getMinValue(), gpi.getMinValue());
if (convertUnit && d2dRecord != null) { if (convertUnit && (d2dRecord != null)) {
convertUnits(d2dRecord, retVal, gpi.getUnitObject()); convertUnits(d2dRecord, retVal, gpi.getUnitObject());
} }
} catch (Exception e) { } catch (Exception e) {
@ -651,7 +652,7 @@ public class D2DGridDatabase extends VGridDatabase {
String mappedModel = config.d2dModelNameMapping(dbId.getModelName()); String mappedModel = config.d2dModelNameMapping(dbId.getModelName());
if (uRecord != null && vRecord != null) { if ((uRecord != null) && (vRecord != null)) {
// Gets the raw grid data from the D2D grib HDF5 files // Gets the raw grid data from the D2D grib HDF5 files
Grid2DFloat uData = getRawGridData(uRecord); Grid2DFloat uData = getRawGridData(uRecord);
Grid2DFloat vData = getRawGridData(vRecord); Grid2DFloat vData = getRawGridData(vRecord);
@ -688,7 +689,7 @@ public class D2DGridDatabase extends VGridDatabase {
"Unable to retrieve wind grids from D2D database", e); "Unable to retrieve wind grids from D2D database", e);
} }
if (sRecord != null && dRecord != null) { if ((sRecord != null) && (dRecord != null)) {
// Gets the raw grid data from the D2D grib HDF5 files // Gets the raw grid data from the D2D grib HDF5 files
Grid2DFloat sData = getRawGridData(sRecord); Grid2DFloat sData = getRawGridData(sRecord);
Grid2DFloat dData = getRawGridData(dRecord); Grid2DFloat dData = getRawGridData(dRecord);
@ -914,8 +915,8 @@ public class D2DGridDatabase extends VGridDatabase {
// no-op // no-op
} }
public static boolean isNonAccumDuration(ParmID id, List<TimeRange> times) public static boolean isNonAccumDuration(ParmID id,
throws GfeConfigurationException { Collection<TimeRange> times) throws GfeConfigurationException {
boolean isAccum = false; boolean isAccum = false;
try { try {
isAccum = IFPServerConfigManager isAccum = IFPServerConfigManager
@ -925,6 +926,8 @@ public class D2DGridDatabase extends VGridDatabase {
} catch (GfeConfigurationException e) { } catch (GfeConfigurationException e) {
throw e; throw e;
} }
if (!isAccum) {
boolean isDuration = false; boolean isDuration = false;
for (TimeRange time : times) { for (TimeRange time : times) {
if (time.getDuration() > 0) { if (time.getDuration() > 0) {
@ -935,4 +938,7 @@ public class D2DGridDatabase extends VGridDatabase {
return !isAccum && isDuration; return !isAccum && isDuration;
} }
return !isAccum;
}
} }

View file

@ -21,6 +21,7 @@ package com.raytheon.edex.plugin.gfe.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import com.raytheon.uf.common.dataplugin.level.Level; import com.raytheon.uf.common.dataplugin.level.Level;
import com.raytheon.uf.common.dataplugin.level.LevelFactory; import com.raytheon.uf.common.dataplugin.level.LevelFactory;
@ -34,7 +35,7 @@ import com.raytheon.uf.common.dataplugin.level.LevelFactory;
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* May 5, 2008 njensen Initial creation * May 5, 2008 njensen Initial creation
* Aug 22, 2008 1502 dglazesk Changed to JAXB unmarshalling * Aug 22, 2008 1502 dglazesk Changed to JAXB unmarshalling
* * Dec 06, 2012 1394 rjpeter Attend static compiled patterns.
* </pre> * </pre>
* *
* @author njensen * @author njensen
@ -45,15 +46,19 @@ public class GridTranslator {
private static final List<String> NEEDS_ZERO = Arrays.asList("BLD", "WBZ"); private static final List<String> NEEDS_ZERO = Arrays.asList("BLD", "WBZ");
private static final Pattern ANY_NUMBER = Pattern.compile("[0-9]");
private static final Pattern NOT_NUMBER = Pattern.compile("[^0-9]");
private GridTranslator() { private GridTranslator() {
} }
public static String getLevelName(String shorthand) { public static String getLevelName(String shorthand) {
return shorthand.replaceAll("[0-9]*", ""); return ANY_NUMBER.matcher(shorthand).replaceAll("");
} }
public static double[] getLevelValue(String shorthand) { public static double[] getLevelValue(String shorthand) {
String levelString = shorthand.replaceAll("[^0-9]", ""); String levelString = NOT_NUMBER.matcher(shorthand).replaceAll("");
int length = levelString.length(); int length = levelString.length();
double[] retVal = new double[2]; double[] retVal = new double[2];
retVal[0] = Level.getInvalidLevelValue(); retVal[0] = Level.getInvalidLevelValue();
@ -126,9 +131,9 @@ public class GridTranslator {
} else { } else {
tmp.append(name); tmp.append(name);
if (levelOne != Level.getInvalidLevelValue() if ((levelOne != Level.getInvalidLevelValue())
&& (levelOne != 0 && ((levelOne != 0)
|| levelTwo != Level.getInvalidLevelValue() || NEEDS_ZERO || (levelTwo != Level.getInvalidLevelValue()) || NEEDS_ZERO
.contains(name))) { .contains(name))) {
tmp.append(String.valueOf(Math.round(levelOne))); tmp.append(String.valueOf(Math.round(levelOne)));
} }
@ -166,7 +171,7 @@ public class GridTranslator {
+ level + level
+ " " + " "
+ Arrays.toString(levels) + Arrays.toString(levels)
+ (l1 == levels[0] && l2 == levels[1] ? " passed" + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed"
: " failed")); : " failed"));
} }
} }
@ -187,7 +192,7 @@ public class GridTranslator {
+ level + level
+ " " + " "
+ Arrays.toString(levels) + Arrays.toString(levels)
+ (l1 == levels[0] && l2 == levels[1] ? " passed" + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed"
: " failed")); : " failed"));
} }
} }
@ -197,26 +202,50 @@ public class GridTranslator {
l2 = -999999; l2 = -999999;
level = GridTranslator.getShortLevelName(levelName, l1, l2); level = GridTranslator.getShortLevelName(levelName, l1, l2);
levels = GridTranslator.getLevelValue(level); levels = GridTranslator.getLevelValue(level);
System.out.println(levelName + " " + l1 + " " + l2 + ": " + level + " " System.out.println(levelName
+ " "
+ l1
+ " "
+ l2
+ ": "
+ level
+ " "
+ Arrays.toString(levels) + Arrays.toString(levels)
+ (l1 == levels[0] && l2 == levels[1] ? " passed" : " failed")); + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed"
: " failed"));
levelName = "BLD"; levelName = "BLD";
l1 = 0; l1 = 0;
l2 = -999999; l2 = -999999;
level = GridTranslator.getShortLevelName(levelName, l1, l2); level = GridTranslator.getShortLevelName(levelName, l1, l2);
levels = GridTranslator.getLevelValue(level); levels = GridTranslator.getLevelValue(level);
System.out.println(levelName + " " + l1 + " " + l2 + ": " + level + " " System.out.println(levelName
+ " "
+ l1
+ " "
+ l2
+ ": "
+ level
+ " "
+ Arrays.toString(levels) + Arrays.toString(levels)
+ (l1 == levels[0] && l2 == levels[1] ? " passed" : " failed")); + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed"
: " failed"));
levelName = "WBZ"; levelName = "WBZ";
l1 = 0; l1 = 0;
l2 = -999999; l2 = -999999;
level = GridTranslator.getShortLevelName(levelName, l1, l2); level = GridTranslator.getShortLevelName(levelName, l1, l2);
levels = GridTranslator.getLevelValue(level); levels = GridTranslator.getLevelValue(level);
System.out.println(levelName + " " + l1 + " " + l2 + ": " + level + " " System.out.println(levelName
+ " "
+ l1
+ " "
+ l2
+ ": "
+ level
+ " "
+ Arrays.toString(levels) + Arrays.toString(levels)
+ (l1 == levels[0] && l2 == levels[1] ? " passed" : " failed")); + ((l1 == levels[0]) && (l2 == levels[1]) ? " passed"
: " failed"));
} }
} }

View file

@ -23,6 +23,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
@ -56,11 +57,19 @@ public class DataFieldTableLookup {
private static DataFieldTableLookup instance; private static DataFieldTableLookup instance;
private Map<String, String> data2cdl = new HashMap<String, String>(); private final Pattern DASH = Pattern.compile("-");
private Map<String, String> cdl2data = new HashMap<String, String>(); private final Pattern UNDERSCORE = Pattern.compile("_");
private Map<String, String> data2name = new HashMap<String, String>(); private final Pattern NEWLINE = Pattern.compile("\n");
private final Pattern PIPE = Pattern.compile("\\|");
private final Map<String, String> data2cdl = new HashMap<String, String>();
private final Map<String, String> cdl2data = new HashMap<String, String>();
private final Map<String, String> data2name = new HashMap<String, String>();
public static synchronized DataFieldTableLookup getInstance() { public static synchronized DataFieldTableLookup getInstance() {
if (instance == null) { if (instance == null) {
@ -87,13 +96,13 @@ public class DataFieldTableLookup {
} catch (IOException e) { } catch (IOException e) {
statusHandler.handle(Priority.PROBLEM, e.getLocalizedMessage(), e); statusHandler.handle(Priority.PROBLEM, e.getLocalizedMessage(), e);
} }
for (String line : contents.split("\n")) { for (String line : NEWLINE.split(contents)) {
line = line.trim(); line = line.trim();
if (line.startsWith("//") || line.startsWith("#")) { if (line.startsWith("//") || line.startsWith("#")) {
// ignore comments // ignore comments
continue; continue;
} }
String[] parts = line.split("\\|"); String[] parts = PIPE.split(line);
if (parts.length < 2) { if (parts.length < 2) {
// invalid line // invalid line
continue; continue;
@ -121,7 +130,7 @@ public class DataFieldTableLookup {
public String lookupCdlName(String dataField) { public String lookupCdlName(String dataField) {
String retVal = data2cdl.get(dataField); String retVal = data2cdl.get(dataField);
if (retVal == null) { if (retVal == null) {
retVal = data2cdl.get(dataField.replace("-", "_")); retVal = data2cdl.get(DASH.matcher(dataField).replaceAll("_"));
if (retVal == null) { if (retVal == null) {
retVal = dataField; retVal = dataField;
} }
@ -132,7 +141,7 @@ public class DataFieldTableLookup {
public String lookupDataName(String cdlField) { public String lookupDataName(String cdlField) {
String retVal = cdl2data.get(cdlField); String retVal = cdl2data.get(cdlField);
if (retVal != null) { if (retVal != null) {
retVal = retVal.replace("_", "-"); retVal = UNDERSCORE.matcher(retVal).replaceAll("-");
} }
return retVal; return retVal;
} }
@ -140,7 +149,7 @@ public class DataFieldTableLookup {
public String lookupName(String dataField) { public String lookupName(String dataField) {
String retVal = data2name.get(dataField); String retVal = data2name.get(dataField);
if (retVal == null) { if (retVal == null) {
retVal = data2name.get(dataField.replace("-", "_")); retVal = data2name.get(DASH.matcher(dataField).replaceAll("_"));
} }
return retVal; return retVal;
} }