Omaha #3992 Only retrieve NLDN data. Deduplicate primary keys.

Change-Id: I877dc47ce08428858b6119e5d2a617ecb75591be

Former-commit-id: aec9063739 [formerly 2692dfc7871b2c326b6e59ff7a3fc4ccc07b7b71]
Former-commit-id: f19c123a16
This commit is contained in:
Nathan Bowler 2015-02-26 13:39:47 -05:00
parent 4fe784a886
commit 6c39e62875
2 changed files with 150 additions and 27 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ohd Plug-in
Bundle-SymbolicName: com.raytheon.uf.edex.ohd
Bundle-Version: 1.14.0.qualifier
Bundle-Version: 1.15.0.qualifier
Bundle-Vendor: RAYTHEON
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: com.raytheon.edex.util,

View file

@ -1,19 +1,19 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
@ -22,11 +22,14 @@ package com.raytheon.uf.edex.ohd.pproc;
import java.io.File;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.HibernateException;
import org.opengis.metadata.spatial.PixelOrientation;
import com.raytheon.uf.common.dataplugin.binlightning.BinLightningRecord;
import com.raytheon.uf.common.dataplugin.binlightning.LightningConstants;
import com.raytheon.uf.common.dataplugin.persist.DefaultPathProvider;
import com.raytheon.uf.common.dataplugin.persist.IPersistable;
import com.raytheon.uf.common.dataquery.db.QueryResult;
@ -46,9 +49,9 @@ import com.vividsolutions.jts.geom.Coordinate;
/**
* Service implementation for gathering the lightning datasets from files in
* HDF5 format and inserting them into the ifhs lightning table.
*
*
* <pre>
*
*
* SOFTWARE HISTORY
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
@ -60,9 +63,12 @@ import com.vividsolutions.jts.geom.Coordinate;
* Aug 20, 2014 3549 njensen Fixed spelling in exceptions
* Sep 17, 2014 3015 bclement improved exception handling
* Dec 04, 2014 3015 njensen Corrected usage of Coordinate(x, y)
*
* Feb 25, 2015 3992 nabowle Limit getMostRecentStrikes to NLDN.
* Deduplicate lightning data in a
* single BinLightningRecord.
*
* </pre>
*
*
* @author jnjanga
* @version 1.0
*/
@ -90,14 +96,15 @@ public class MpeLightningSrv {
/**
* Check the metadata Database for new lightning entries.
*
*
* @return rows returned from the query
*/
private QueryResultRow[] getMostRecentStrikes() throws EdexException {
QueryResult rs = null;
CoreDao coreDao = new CoreDao(DaoConfig.DEFAULT);
final String lgtSQL = "select datauri from binlightning "
+ "where reftime > (now()- interval \'30 minutes \')";
+ "where reftime > (now()- interval \'30 minutes \')"
+ "and source = '" + LightningConstants.DEFAULT_SOURCE + "'";
try {
rs = (QueryResult) coreDao.executeNativeSql(lgtSQL, true);
} catch (Exception e) {
@ -109,7 +116,7 @@ public class MpeLightningSrv {
/**
* Inserts a single record into ihfs's lightning table.
*
*
* @param dataURI
* @throws EdexException
*/
@ -143,40 +150,62 @@ public class MpeLightningSrv {
float[] longitudes = ltngRec.getLongitudes();
long[] obstimes = ltngRec.getObsTimes();
byte[] strikes = ltngRec.getPulseCounts();
int[] intensities = ltngRec.getIntensities();
Map<LightningData, Integer> lightningData = new HashMap<>();
// convert latitude and longitude to grid coordinate
HRAP hrap = HRAP.getInstance();
PixelOrientation po = PixelOrientation.CENTER;
short[] x_hgrids = new short[latitudes.length];
short[] y_hgrids = new short[longitudes.length];
Coordinate gridCell = new Coordinate();
LightningData data;
Integer oldIntensity;
for (int i = 0; i < latitudes.length; i++) {
float lat = latitudes[i];
float lon = longitudes[i];
Coordinate c = new Coordinate(lon, lat);
gridCell = hrap.latLonToGridCoordinate(c, po);
x_hgrids[i] = (short) gridCell.x;
y_hgrids[i] = (short) gridCell.y;
data = new LightningData((short) gridCell.x,
(short) gridCell.y, obstimes[i], strikes[i]);
// deduplicate lightning data that will create the same
// primary keys.
oldIntensity = lightningData.get(data);
if (oldIntensity == null) {
lightningData.put(data, intensities[i]);
} else {
logger.debug("dataURI " + dataURI
+ " has multiple lightning data for "
+ "ihfs.Lightning pk (" + data.getX() + ", "
+ data.getY() + ", " + data.getObstime() + ")");
if (intensities[i] > oldIntensity.intValue()) {
/*
* highest intensity data is retained. #put() does not
* replace keys, so because only some of the fields are
* used for hashcode and equals, we must remove the old
* key before putting the new key.
*/
lightningData.remove(data);
lightningData.put(data, intensities[i]);
}
}
}
// set up query first
StringBuilder sql = new StringBuilder("INSERT INTO lightning "
+ " (x_hgrid, y_hgrid, obstime, no_of_strike) VALUES");
// form tuples
for (int j = 0; j < x_hgrids.length; j++) {
for (LightningData lightning : lightningData.keySet()) {
// need to convert obstime from seconds
// to timestamp type for table insertion
Timestamp ts = new Timestamp(obstimes[j]);
String tuple = "(" + x_hgrids[j] + "," + y_hgrids[j]
+ ", TIMESTAMP '" + ts.toString() + "' ," + strikes[j]
+ ")";
if (j != x_hgrids.length - 1) {
tuple = tuple + ",";
} else {
tuple = tuple + ";";
}
Timestamp ts = new Timestamp(lightning.getObstime());
String tuple = "(" + lightning.getX() + "," + lightning.getY()
+ ", TIMESTAMP '" + ts.toString() + "' ,"
+ lightning.getStrikes() + ")";
tuple = tuple + ",";
sql.append(tuple);
}
sql.replace(sql.length() - 1, sql.length(), ";");
// insert all the tuples into the 'lightning' table in ihfs
// database.
@ -202,7 +231,7 @@ public class MpeLightningSrv {
/**
* Populates ifhs' lightning table with the resultset obtained from querying
* metadata's binlighting table.
*
*
* @param rows
* @throws EdexException
*/
@ -219,7 +248,7 @@ public class MpeLightningSrv {
/**
* run at scheduled timer.
*
*
* @throws EdexException
*/
public void runOnSchedule() throws EdexException {
@ -229,4 +258,98 @@ public class MpeLightningSrv {
QueryResultRow[] rows = getMostRecentStrikes();
ifhsInsertMostRecentStrikes(rows);
}
/**
* Class to simplify deduplicating lightning data in a
* {@link BinLightningRecord} that generate the same ihfs lightning primary
* key.
*/
private static class LightningData {
private short x;
private short y;
private long obstime;
private byte strikes;
public LightningData(short x, short y, long time, byte strikes) {
super();
this.x = x;
this.y = y;
this.obstime = time;
this.strikes = strikes;
}
/**
* @return the x
*/
public short getX() {
return x;
}
/**
* @return the y
*/
public short getY() {
return y;
}
/**
* @return the obstime
*/
public long getObstime() {
return obstime;
}
/**
* @return the strikes
*/
public byte getStrikes() {
return strikes;
}
/**
* Generate a hashcode using the ihfs primary key fields: x, y, and
* time.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (obstime ^ (obstime >>> 32));
result = prime * result + x;
result = prime * result + y;
return result;
}
/**
* Determine equality using the ihfs primary key fields: x, y, and time.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
LightningData other = (LightningData) obj;
if (obstime != other.obstime) {
return false;
}
if (x != other.x) {
return false;
}
if (y != other.y) {
return false;
}
return true;
}
}
}