📄 mcacaldb.java
字号:
package org.trinet.util.magnitudeengines;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.trinet.jasi.*;
import org.trinet.jasi.coda.TN.*;
import org.trinet.jdbc.table.*;
import org.trinet.util.*;
public class McaCalDb {
public static final String SQL_CODA_QUERY_STRING =
"select coda.COID,coda.STA,coda.NET,coda.AUTH,coda.SUBSOURCE,coda.SEEDCHAN,coda.LOCATION," +
"coda.AFIX,coda.AFREE,coda.QFIX,coda.QFREE,coda.NSAMPLE,coda.DATETIME," +
"origin.EVID,assoccoo.ORID,netmag.MAGID,netmag.MAGNITUDE " +
"FROM coda,assoccoo,origin,netmag WHERE " +
"coda.ALGORITHM='MCA'AND coda.COID=assoccoo.COID AND origin.orid=assoccoo.ORID AND netmag.ORID=assoccoo.ORID AND " +
"netmag.MAGTYPE='l' AND (NET=? AND STA=? AND SEEDCHAN=?) ";
public static final String SQL_CODA_ORDER_BY_STRING = " ORDER BY netmag.MAGNITUDE";
public static final String SQL_STN_QUERY_STRING = "SELECT DISTINCT STA,NET,SEEDCHAN FROM CODA WHERE ";
public static final String SQL_STN_ORDER_BY_STRING = " ORDER BY coda.net,coda.STA,coda.SEEDCHAN";
protected boolean rejectDisabled = true;
protected boolean verbose = true;
//protected Concat cc = new Concat();
protected PrintStream log;
protected Statement preparedDbStatement;
protected int totalChannelCount;
protected int processedChannelCount;
protected int acceptedChannelCount;
protected ArrayList stnChlList = new ArrayList(1024);
protected class OutputStats {
DataStnChl stnChl;
int codaCount;
double medianQFree;
double medianQFreeDeviation;
double medianAFree;
double medianAFreeDeviation;
double medianAFix;
double medianAFixDeviation;
double calibrFitSlope;
double calibrFitAFix;
String statusString;
protected void reset() {
stnChl = null;
codaCount = 0;
medianQFree = 0.;
medianQFreeDeviation = 0.;
medianAFree = 0.;
medianAFreeDeviation = 0.;
medianAFix = 0.;
medianAFixDeviation = 0.;
calibrFitSlope = 0.;
calibrFitAFix = 0.;
statusString = "";
}
public String toString() {
StringBuffer sb = new StringBuffer(132);
sb.append(stnChl.toAbbrvStnChlString()).append(" ");
Concatenate.format(sb, codaCount, 4).append(" ");
Concatenate.format(sb, medianAFix, 5, 2).append(" ");
Concatenate.format(sb, medianAFixDeviation, 5, 2).append(" ");
Concatenate.format(sb, medianAFree, 5, 2).append(" ");
Concatenate.format(sb, medianAFreeDeviation, 5, 2).append(" ");
Concatenate.format(sb, medianQFree, 5, 2).append(" ");
Concatenate.format(sb, medianQFreeDeviation, 5, 2).append(" ");
Concatenate.format(sb, calibrFitSlope, 5, 2).append(" ");
Concatenate.format(sb, calibrFitAFix, 5, 2).append(" ");
sb.append(statusString);
return sb.toString();
}
public String getHeaderString() {
//return "STN CNT AFIX STD AFREE STD QFREE STD SLOPE INTERCEPT";
return "STN CNT AFIX STD AFREE STD QFREE STD SLOPE INTERCEPT";
}
}
protected DateRange currentInputDateRange;
protected DateRange currentCalibrDateRange;
protected OutputStats currentStats;
protected Collection currentInputDataCollection = new ArrayList(512);
protected class InputData {
DataStnChl stnChl = new DataStnChl();
double aFix;
double qFix;
double aFree;
double qFree;
int windows;
double time;
long coid;
long evid;
long orid;
long magid;
double ml;
public String getHeader() {
return "staChl afix qfix afree qfree nc coid evid orid magid ml";
}
public String toString() {
StringBuffer sb = new StringBuffer(132);
sb.append(stnChl.toStnChlString()).append(" ");
sb.append(aFix).append(" ");
sb.append(qFix).append(" ");
sb.append(aFree).append(" ");
sb.append(qFree).append(" ");
sb.append(windows).append(" ");
sb.append(coid).append(" ");
sb.append(evid).append(" ");
sb.append(orid).append(" ");
sb.append(magid).append(" ");
sb.append(ml).append(" ");
sb.append(EpochTime.toNoZoneYYYYString(time));
return sb.toString();
}
}
public McaCalDb() { }
public McaCalDb(java.util.Date startDate, java.util.Date endDate) {
setDateRange(startDate, endDate);
}
public void setVerbose(boolean value) {
verbose = value;
}
public void enableReject() {
rejectDisabled = false;
}
public void setDateRange(DateRange dateRange) {
if (dateRange == null) currentInputDateRange = new DateRange();
else this.currentInputDateRange = dateRange;
}
public void setDateRange(java.util.Date startDate, java.util.Date endDate) {
if (currentInputDateRange == null) currentInputDateRange = new DateRange(startDate, endDate);
else {
currentInputDateRange.setMin(startDate);
currentInputDateRange.setMax(endDate);
}
}
private void closeIO() {
if (preparedDbStatement != null) {
try {
preparedDbStatement.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
log.flush();
log.close();
}
protected int loadChannelListFromDb() {
return loadChannelListFromDb(DataSource.getConnection());
}
private String createSQLStnQueryString() {
StringBuffer sb = new StringBuffer(132);
sb.append(SQL_STN_QUERY_STRING);
if (currentInputDateRange.isLimited()) {
sb.append(org.trinet.jdbc.table.Coda.toDateTimeConstraintSQLWhereClause("CODA",
currentInputDateRange.getMinDate(), currentInputDateRange.getMaxDate()));
}
sb.append(SQL_STN_ORDER_BY_STRING);
return sb.toString();
}
protected int loadChannelListFromDb(Connection conn) {
stnChlList.clear(); // reuse existing collection
if (conn == null) {
System.err.println ("McaCalDb no connection is open.");
return 0;
}
try {
if (conn.isClosed()) {
System.err.println ("McaCalDb DataSource connection is closed");
return 0;
}
Statement sm = conn.createStatement();
ResultSet rsdb = sm.executeQuery(createSQLStnQueryString());
if (rsdb == null) return 0; // no data found
while (rsdb.next()) {
DataStnChl dsc = new DataStnChl();
dsc.setSta(rsdb.getString(1));
dsc.setNet(rsdb.getString(2));
dsc.setSeedchan(rsdb.getString(3));
stnChlList.add(dsc);
}
rsdb.close();
sm.close();
}
catch (SQLException ex) {
System.err.println(ex);
ex.printStackTrace();
}
return stnChlList.size();
}
protected boolean initializeIO() throws IOException {
log = new PrintStream(new BufferedOutputStream(new FileOutputStream("mcacaldb.log")));
System.setOut(log);
System.setErr(log);
currentStats = new OutputStats(); // channel fit results data stored in instance of OutputStats
currentInputDataCollection.clear();
processedChannelCount = 0;
acceptedChannelCount = 0;
//channelList = loadChannelListFromDb().getArray();
totalChannelCount = loadChannelListFromDb();
log.println("Input channel count: " + totalChannelCount);
if (currentInputDateRange == null || ! currentInputDateRange.hasMinLimit())
currentCalibrDateRange = new DateRange(new java.util.Date(System.currentTimeMillis()), null);
else currentCalibrDateRange = currentInputDateRange;
return true;
}
/*
protected Channel [] channelList;
protected DataStnChl currentInputDataStnChl = new DataStnChl();
protected DataStnChl getCurrentStnChl(int idx) {
currentInputDataStnChl.setUpdateAllValues(false);
currentInputDataStnChl.setNullAllValues(true);
currentInputDataStnChl.setValue(DataStnChl.STA, channelList[idx].getSta());
currentInputDataStnChl.setValue(DataStnChl.NET, channelList[idx].getNet());
currentInputDataStnChl.setValue(DataStnChl.SEEDCHAN, channelList[idx].getSeedchan());
return currentInputDataStnChl;
}
*/
protected DataStnChl getCurrentStnChl(int idx) {
return (DataStnChl) stnChlList.get(idx);
}
/** Input database station channel Coda data and generate the database MCA calibration data. */
public boolean calibrate() throws IOException {
if (! initializeIO()) return false;
boolean status = false;
for (int idx = 0; idx < totalChannelCount; idx++) {
currentStats.reset();
int codaCount = getDataFromDb(getCurrentStnChl(idx));
if (codaCount <= 0) continue;
InputData [] inputDataList = (InputData []) currentInputDataCollection.toArray(new InputData[codaCount]);
status = processInputData(inputDataList);
if (! status) break;
}
log.println("Number of channels processed: " + processedChannelCount);
log.println("Number of channels accepted : " + acceptedChannelCount);
closeIO();
return status;
}
private boolean processInputData(InputData [] inputDataList) {
boolean status = calcCurrentChannelStats(inputDataList);
if (verbose) {
printChannelStats();
printChannelMagResiduals(inputDataList);
}
if (status || rejectDisabled) {
if (! writeChannelStatsToDb()) return false;
acceptedChannelCount++;
}
processedChannelCount++;
//if (processedChannelCount == 5) return false; // TEST DEBUG UNCOMMENT FOR LIMITED CHANNEL OUTPUT
return true;
}
private boolean calcCurrentChannelStats(InputData [] inputDataList) {
int currentCodaCount = inputDataList.length;
if (currentCodaCount == 0) return true;
calcL1Norm(inputDataList);
currentStats.stnChl = inputDataList[0].stnChl;
currentStats.codaCount = currentCodaCount;
double [] x = new double[currentCodaCount];
for (int idx = 0; idx < currentCodaCount; idx++) {
x[idx] = inputDataList[idx].aFix - inputDataList[idx].ml;
}
currentStats.medianAFix = Stats.median(x, currentCodaCount);
currentStats.medianAFixDeviation = Stats.meanDeviation(x, currentCodaCount);
for (int idx = 0; idx < currentCodaCount; idx++) {
x[idx] = inputDataList[idx].aFree - inputDataList[idx].ml;
}
currentStats.medianAFree = Stats.median(x, currentCodaCount);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -