📄 amplitudetn.java
字号:
channelMag.residual = (DataDouble) assoc.getDataObject(AssocAmM.MAGRES);
// channelMag.importance = (DataDouble) assoc.getDataObject(AssocAmM.IMPORTANCE);
channelMag.correction = (DataDouble) assoc.getDataObject(AssocAmM.MAGCORR);
channelMag.weight = (DataDouble) assoc.getDataObject(AssocAmM.WEIGHT);
return;
}
/**
* Copy contents of this amplitude into a jdbc.Amp row object
*/
protected org.trinet.jdbc.table.Amp toAmpRow() {
// Always make a new row, get unique key #
long newId = SeqIds.getNextSeq("ampseq");
ampid.setValue(newId); // set ID for this.Phase
Amp ampRow = new Amp(newId);
stuffChannel(ampRow, getChannelObj());
// set flag to enable processing
ampRow.setUpdate(true);
ampRow.setValue(Amp.DATETIME, datetime);
// "rectify" - peak amp must be > 0.0
double ampval = Math.abs(value.doubleValue());
ampRow.setValue(Amp.AMPLITUDE, ampval);
ampRow.setValue(Amp.AMPTYPE, AmpType.getString(type));
// if zero-to-peak = 1 , if peak-to-peak = 0
if (halfAmp) {
ampRow.setValue(Amp.AMPMEAS, 1);
} else {
ampRow.setValue(Amp.AMPMEAS, 0);
}
ampRow.setValue(Amp.UNITS, Units.getString(units));
// the following can be null
if (!authority.isNull()) ampRow.setValue(Amp.AUTH, authority);
if (!processingState.isNull()) ampRow.setValue(Amp.RFLAG, processingState);
if (!source.isNull()) ampRow.setValue(Amp.SUBSOURCE, source);
if (!phaseType.isNull()) ampRow.setValue(Amp.IPHASE, phaseType);
if (!uncertainty.isNull()) ampRow.setValue(Amp.ERAMP, uncertainty);
if (!period.isNull()) ampRow.setValue(Amp.PER, period);
if (!snr.isNull()) ampRow.setValue(Amp.SNR, snr);
if (!quality.isNull()) ampRow.setValue(Amp.QUALITY, quality);
// IGNORING 'FLAGAMP', 'TAU'
if (clipped == 0) ampRow.setValue(Amp.CFLAG, "OS");
if (clipped == -1) ampRow.setValue(Amp.CFLAG, "BN");
if (clipped == 1) ampRow.setValue(Amp.CFLAG, "CL");
if (!windowStart.isNull()) ampRow.setValue(Amp.WSTART,windowStart );
if (!windowDuration.isNull()) ampRow.setValue(Amp.DURATION, windowDuration);
// if (!.isNull()) ampRow.setValue(Amp., );
return ampRow;
}
/** Put the channel name attributes in the Arrival DataTableRow */
protected void stuffChannel (Amp dtrow, Channel cn) {
// Constrained "NOT NULL" in schema
dtrow.setValue(Amp.STA, cn.getSta());
if (cn.getNet().length() > 0) dtrow.setValue(Amp.NET, cn.getNet());
if (cn.getAuth().length() > 0) dtrow.setValue(Amp.AUTH, cn.getAuth());
if (cn.getSubsource().length() > 0) dtrow.setValue(Amp.SUBSOURCE, cn.getSubsource());
if (cn.getChannel().length() > 0) dtrow.setValue(Amp.CHANNEL, cn.getChannel());
if (cn.getChannelsrc().length() > 0) dtrow.setValue(Amp.CHANNELSRC, cn.getChannelsrc());
if (cn.getSeedchan().length() > 0) dtrow.setValue(Amp.SEEDCHAN, cn.getSeedchan());
if (cn.getLocation().length() > 0) dtrow.setValue(Amp.LOCATION, cn.getLocation());
}
/**
* Copy DataObjects out of a jdbc.AssocAmM object into a Amplitude.
*/
protected org.trinet.jdbc.table.AssocAmM toAssocAmMRow() {
// keys are ampid and magid
AssocAmM assoc = new AssocAmM();
// Constrained "NOT NULL" in schema
assoc.setValue(AssocAmM.MAGID, magnitude.magid);
assoc.setValue(AssocAmM.AMPID, ampid);
assoc.setValue(AssocAmM.AUTH, authority);
// ALLOWED TO BE NULL
if (!source.isNull()) assoc.setValue(AssocAmM.SUBSOURCE, source);
if (!channelMag.value.isNull()) assoc.setValue(AssocAmM.MAG, channelMag.value);
if (!channelMag.residual.isNull()) assoc.setValue(AssocAmM.MAGRES, channelMag.residual);
// if (!channelMag.importance.isNull()) assoc.setValue(AssocAmM.IMPORTANCE, channelMag.importance);
if (!channelMag.correction.isNull()) assoc.setValue(AssocAmM.MAGCORR, channelMag.correction);
if (!channelMag.weight.isNull()) assoc.setValue(AssocAmM.WEIGHT, channelMag.weight);
if (!processingState.isNull()) assoc.setValue(AssocAmM.RFLAG, processingState);
return assoc;
}
/**
* Copy DataObjects out of a jdbc.AssocAmO object into a Amplitude.
*/
protected org.trinet.jdbc.table.AssocAmO toAssocAmORow() {
// keys are ampid and magid
AssocAmO assoc = new AssocAmO();
// Constrained "NOT NULL" in schema
long orid = ((SolutionTN)sol).getOrid();
assoc.setValue(AssocAmO.ORID, orid);
assoc.setValue(AssocAmO.AMPID, ampid);
// ALLOWED TO BE NULL
assoc.setValue(AssocAmO.AUTH, authority);
if (!source.isNull()) assoc.setValue(AssocAmO.SUBSOURCE, source);
if (!chan.dist.isNull()) assoc.setValue(AssocAmO.DELTA, getDistance());
if (!chan.azimuth.isNull()) assoc.setValue(AssocAmO.SEAZ, chan.azimuth);
if (!processingState.isNull()) assoc.setValue(AssocAmO.RFLAG, processingState);
return assoc;
}
/** Commit changes or delete to underlying DataSource. The action taken will
depend on the state of the amplitude. It may or may not be originally
from the data source, it may be deleted, unassociated, modified, or
unchanged. To write to the DataSource, the flag
DataSource.isWriteBackEnabled() must be true. This must be set with
DataSource.setWriteBackEnabled(true) BEFORE data is read.*/
/*
We will never really delete an amp row in the dbase because this would alter
a mag/amp data-set created by someone else. That would be rude. We only write
an NEW NetMag, AssocAmM rows connecting existing or new Amps to the NetMag.
*/
public boolean commit() {
// you can't write to the dbase
if (!DataSource.isWriteBackEnabled()) return false;
// debug
if (debug) System.out.println ("AmplitudeTN.commit() : fromDbase= "+ fromDbase+
" isDeleted()= "+isDeleted());
// <DELETE>
// Phase is deleted or not associated! So don't write new Amp or AssocAmM.
// Note we NEVER actually delete a dbase row, just don't write it out if its
// new or don't make association if the amp came from the dbase.
if (isDeleted()) return true;
// Must be associated with a Solution to save it.
if (!isAssociated()) return true;
// long magid = sol.magnitude.magid.longValue();
// "existing" amp (from the dbase)
if (fromDbase) {
if (hasChanged()) {
// Old amp had value or time modified, this shouldn't happen.
if (debug) System.out.println ("(changed amp) INSERT and assoc");
// change this flag to allow insert as a new amp
fromDbase = false;
return dbaseInsert(); // insert & associate
} else {
// if no changes to amp, just make new association
if (dbaseAssociate()) {
setUpdate(false); // mark as up-to-date
if (debug) System.out.println ("(amp unchanged) assoc only");
return true;
} else {
return false;
}
}
// virgin amp (not from dbase) <INSERT>
} else {
if (debug) System.out.println ("(new amp) INSERT and assoc");
return dbaseInsert(); // insert & associate
}
}
/**
* NEVER delete an amp. Never remove association with another NetMag. If
you don't like it just don't assoc it with your new NetMag. */
protected boolean dbaseDelete () {
if (fromDbase) {
return true;
} else { // not from dbase :. nothing to delete
return true;
}
}
protected boolean dbaseAssociate () {
return dbaseAssociateWithOrigin() &&
dbaseAssociateWithMag();
}
/**
* Make AssocAmO row for this Amp
*/
protected boolean dbaseAssociateWithOrigin () {
boolean status = false;
// assoc'ed with Solution
if (isAssociated() ) {
AssocAmO assocORow = toAssocAmORow();
assocORow.setProcessing(DataTableRowStates.INSERT);
status = (assocORow.insertRow(DataSource.getConnection()) > 0);
}
return status;
}
/**
* Make AssocAmM row for this Amp
*/
protected boolean dbaseAssociateWithMag () {
boolean status = false;
// assoc'ed with Mag
if (isAssociatedWithMag()) {
// Check that 'magid' has been set, it can't be null
if (getAssociatedMag() == null || getAssociatedMag().magid.isNull()) {
if (debug) System.out.println ("Associated Magnitude is null or has no magid.");
} else {
AssocAmM assocMRow = toAssocAmMRow();
assocMRow.setProcessing(DataTableRowStates.INSERT);
status = (assocMRow.insertRow(DataSource.getConnection()) > 0);
}
}
return status;
}
/**
* Insert a new row in the dbase. This will also write association to the
* event and the magnitude.
*/
protected boolean dbaseInsert () {
if (fromDbase) return false; // nothing to update
boolean status = true;
if (debug) System.out.println ("dbaseInsert: "+this.toString());
Amp ampRow = toAmpRow();
ampRow.setProcessing(DataTableRowStates.INSERT);
// write arrival row
status = (ampRow.insertRow(DataSource.getConnection()) > 0);
if (status) { // successful insert
status = dbaseAssociate(); // write association row
// now its "from" the dbase
setUpdate(false); // mark as up-to-date (fromDbase=true)
}
return status;
}
/**
* Return true if the amp has changed from what was
* read in from the dbase. Returns true if it was not originally from the
* dbase. */
protected boolean hasChanged () {
if (!fromDbase ||
value.isUpdate() ||
datetime.isUpdate()
) return true;
return false;
}
/**
* Set the isUpdate() flag for all data dbase members the given boolean value. */
protected void setUpdate (boolean tf) {
fromDbase = !tf;
value.setUpdate(tf);
datetime.setUpdate(tf);
}
// ///////////////////////////////////////////////////////
public static void main (String args[])
{
EnvironmentInfo.setApplicationName("AmpTest");
long evid = 0;
if (args.length <= 0) // no args
{
System.out.println ("Usage: java AmplitudeTN [evid])");
System.out.println ("Using evid "+ evid+" as a test...");
} else {
Integer val = Integer.valueOf(args[0]); // convert arg String
evid = (int) val.intValue();
}
System.out.println ("Making connection...");
DataSource ds = new TestDataSource();
// get the solution
Solution sol = Solution.create().getById(evid);
// Test getting amps in time window
/*
// This works but is slow, and gets amps from RT1 & RT2, + amps that were not
// used in the mag.
System.out.println (" Getting amps in window around event = "+evid);
double start = sol.datetime.doubleValue() - 60.0;
double stop = sol.datetime.doubleValue() + 60.0;
ArrayList amps = (ArrayList) Amplitude.create().getByTime(start, stop);
if (amps.size() == 0) {
System.out.println ("No amps in time window.");
} else {
for (int i = 0; i<amps.size(); i++)
{
System.err.println (amps.get(i).toString());
}
}
*/
System.out.println ("\n Getting amps for magnitude of evid = "+evid);
// Read amps for this event from the dbase
sol.magnitude.fetchAmps();
System.out.println ("Amp count = " + sol.magnitude.ampList.size());
if (sol.magnitude.ampList.size() <= 0) {
System.out.println ("No amps for this event.");
// System.exit(0);
} else {
System.out.println("Raw magnitude from dbase...");
System.out.println(sol.magnitude.neatDump() + "\n");
}
// Calc mag.
MagnitudeMethod ml = MagnitudeMethod.CreateMagnitudeMethod("org.trinet.util.magnitudeengines.SoCalML");
ml.ConfigureMagnitudeMethod();
MagnitudeEngine magEng = MagnitudeEngine.CreateMagnitudeEngine("org.trinet.util.magnitudeengines.LocalMagnitudeEngine");
magEng.ConfigureMagnitudeEngine(ml);
Magnitude newMag = magEng.solve(sol.magnitude);
System.out.println ("Relocation...") ;
System.out.println(sol.magnitude.neatDump() + "\n");
// test changing and writing a new mag with at least one modified amp
// get the last amp
Amplitude testAmp = (Amplitude)sol.magnitude.ampList.get(sol.magnitude.ampList.size()-1);
// dump some stuff
System.out.println (" Some stuff...\n"+
" mag.fromDbase = " + ((MagnitudeTN)sol.magnitude).fromDbase +
" testAmp.hasChanged = " + ((AmplitudeTN)testAmp).hasChanged() +
"");
System.out.println ("Commiting magnitude to dbase...");
try {
sol.magnitude.commit();
} catch (JasiCommitException ex) {}
// NOTE *******************************************************************
// There's no DataSource.commit() done here so the results are NOT
// Actually written to the dbase.
// ************************************************************************
// if (status) DataSource.commit();
DataSource.close();
}
} // AmplitudeTN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -