📄 localmagnitudeengine.java
字号:
/*
This uses a WFlist but the pre and post filters of MagEng use an amp list in the
mag.
*/
// make a fresh, blank magnitude object
Magnitude newMag = getNewMag(sol);
// pre-filter the list NO GOOD, MAG HAS NO AMPS!
// magMethod.preScanAmpList(newMag);
// are there amps?
if (wfList.isEmpty()) return newMag;
// kill old amps
//if (!reuseOldAmps) sol.ampList = new AmpList();
// !!! have distances? Corrections?
System.out.println("wflist.size() = "+ wfList.size());
channelLookup(wfList);
calcDistances(wfList, sol.getLatLonZ());
Waveform wf[] = new Waveform[wfList.size()];
wfList.toArray(wf);
// make an array to hold the results for statistics later
double magList[] = new double[wf.length];
// inforce maxChannels
int chanKnt = wf.length;
if (chanKnt > magMethod.getMaxChannels()) {
chanKnt = magMethod.getMaxChannels(); // break out of loop
if (debug) System.out.println ("Will trim to maxChannels limit = "+chanKnt);
}
// Scan'em Dan-o
Amplitude amp;
for (int i=0; i<chanKnt; i++) {
// REJECT some channels up-front to save computation
// check maxDistance & maxChannels criteria
if ( wf[i].getDistance() > magMethod.getMaxDistance() ) {
if (debug) System.out.println
("Hit maxDistance limit, skip channels beyond " + magMethod.getMaxDistance());
break; // break out of loop
}
if ( !useChannel(wf[i].getChannelObj()) ) continue; // skip rest of loop
// scan the time-series for an amp
// ----
amp = calcAmpFromWaveform(sol, wf[i]);
// ----
// save it if it was good
if (amp != null && amp.channelMag != null) {
// reject amp with low SNR if value is set
if (magMethod.getMinSNR() != Double.MIN_VALUE &&
(!amp.snr.isNull() &&
amp.snr.doubleValue() < magMethod.getMinSNR())) {
if (debug) System.out.println("Rejecting low SNR: "+
amp.getChannelObj().toDelimitedSeedString(" ")+
" snr= " + amp.snr);
} else {
// this assoc's amp with mag AND solution
newMag.associate(amp);
}
}
} // end of for loop
// no contributing amps
if (newMag.ampList.size() < 1) {
newMag.setStale(false);
return newMag;
}
//
newMag = solve (newMag);
// make the newMag the primary one for the solution (saves the old one)
// This also associates newMag with the sol.
sol.setPreferredMagnitude(newMag);
return newMag;
}
/** Return 'true' if the channel should be scanned. */
public boolean useChannel (Channel chan) {
if (!magMethod.isIncludedComponent(chan)) {
return false;
} else {
return true;
}
}
/**
* Scan the time-series in the waveform for a peak amplitude, then use that peak
* to calculate the channel magnitude. If the MagnitudeMethod has a filter it
* will be applied to the wavefrom before it is scanned. Returns null on failure,
* e.g. no time-series.
*/
public Amplitude calcAmpFromWaveform (Solution sol, Waveform wf) {
boolean localLoad = false;
if (wf == null) return null;
// if there is no time-series, we load it here and unload it when done.
if (!wf.hasTimeSeries()) {
if (wf.loadTimeSeries()) { // try to load it
localLoad = true;
} else {
return null ; // nothing loaded
}
}
// convert/filter if appropreate
// Waveform filteredWf;
// if (magMethod.hasWaveformFilter()) {
// filteredWf = magMethod.getWaveformFilter().filter(wf);
// } else {
// filteredWf = wf;
// }
// Make a DEEP copy of the waveform so original unfiltered is preserved.
Waveform fwf = Waveform.copy(wf);
if (magMethod.hasWaveformFilter()) {
//fwf = magMethod.wfFilter.filter(fwf);
fwf = magMethod.getWaveformFilter().filter(fwf);
// } else {
// filteredWf = wf;
}
// failed
if (fwf == null) return null;
Amplitude amp;
// get peak in energy window
if (scanEnergyWindow) {
// DK - OLD CODE REFERENCED "fwf" here instead of "wf"
TimeSpan ts = wf.getEnergyTimeSpan(sol.datetime.doubleValue(),
wf.getDistance() );
amp = fwf.getPeakAmplitude(ts);
if (debug) System.out.println ("Scan: "+fwf.getChannelObj().toDelimitedSeedString(" ")+
" windowsize= "+ts.getDuration());
} else { // get peak in whole waveform
amp = fwf.getPeakAmplitude();
}
// amp can be 'null' if the energy window is outside the available time series
if (amp == null) return null;
// calc SNR
TimeSpan tsp = fwf.getPreEnergyTimeSpan( sol.datetime.doubleValue(),
fwf.getDistance() );
float noiseLevel = fwf.scanForNoiseLevel(tsp);
if (noiseLevel != Float.NaN) {
amp.snr.setValue( Math.abs( amp.value.doubleValue() ) / noiseLevel );
}
// TODO: should check for lop-sided waveform
// unload original time-series
if (localLoad) wf.unloadTimeSeries();
// destroy object
fwf = null;
return amp;
}
/**
* Lookup channel info for the amps.
*/
/* public void ampChannelLookup(Collection ampList) {
Amplitude amp[] = new Amplitude[ampList.size()];
ampList.toArray(amp);
for (int i = 0; i<amp.length; i++) {
amp[i].chan = channelLookup(amp[i].chan); // go to dbase for each one
}
}
*/
/**
* Lookup channel info for the Waveforms.
*/
/* public void wfChannelLookup(Collection wfList) {
Waveform wf[] = new Waveform[wfList.size()];
wfList.toArray(wf);
for (int i = 0; i<wf.length; i++) {
wf[i].chan = channelLookup(wf[i].chan);
}
}
*/
/**
* Lookup channel info for the Waveforms.
*/
public void channelLookup(Collection list) {
if (list == null || list.isEmpty()) return;
// Load channel list if that would be more efficient
if (list.size() > 100 && (chanList == null || chanList.isEmpty()) ) {
// TODO: this should be generalized, could use a list of components
// that are specified to contribute to Mag but parsing wildcards is a problem.
String compList[] = {"HH_", "E%", "HL_"};
setChannelList(ChannelList.getByComponent(compList));
// if (debug)
System.out.println ("Channel list got "+chanList.size());
}
Object thing = ((ArrayList)list).get(0);
if (thing instanceof Channelable) {
Channelable ob[] = new Channelable[list.size()];
list.toArray(ob);
for (int i = 0; i<ob.length; i++) {
if (ob[i] != null && !ob[i].getChannelObj().hasLatLonZ()) {
Channel ch = ob[i].getChannelObj();
ch = channelLookup(ch);
ob[i].setChannelObj(ch);
}
}
}
}
/**
* Lookup channel info for the Waveforms.
*/
public Channel channelLookup(Channel chan) {
if (chanList != null) { // use local list if available
Channel ch = chanList.lookUp(chan);
if (ch != null) return ch; // return it if sucessfull
} // if not fall thru to direct lookup
return Channel.create().lookUp(chan); // go to the data source
}
/**
* Calculate distance for each channel to the event hypocenter. The distance
* value is put in Amplitude.chan.distance.
*/
/* public void calcDistances (Collection ampList, LatLonZ latLonZ) {
Amplitude amp[] = new Amplitude[ampList.size()];
ampList.toArray(amp);
for (int i = 0; i<amp.length; i++) {
amp[i].chan.calcDistance(latLonZ);
}
}
*/
/**
* Calculate distance for each channel to the event hypocenter. The objects
* in the list must implement the Channelable interface. The distance
* value is put in Object.chan.distance.
*/
public void calcDistances (Collection list, double lat, double lon, double z) {
calcDistances (list, new LatLonZ (lat, lon, z));
}
/**
* Lookup channel info for the Waveforms.
*/
public void calcDistances(Collection list, LatLonZ latLonZ) {
Object thing = ((ArrayList)list).get(0);
Channel chan;
if (thing instanceof Channelable) {
Channelable ob[] = new Channelable[list.size()];
list.toArray(ob);
for (int i = 0; i<ob.length; i++) {
chan = ob[i].getChannelObj();
// look up latlonz if there is none
if (!chan.hasLatLonZ()) ob[i].setChannelObj(channelLookup(chan));
// ob[i].setChannel(channelLookup(ob[i].getChannelObj()));
ob[i].setDistance(ob[i].getChannelObj().calcDistance(latLonZ));
}
}
}
// ///////////////////////////////////////////////////////////////////
// test
public static void main (String args[])
{
String host = "makalu";
// String host = "k2";
EnvironmentInfo.setNetworkCode("CI");
EnvironmentInfo.setApplicationName("MagEng");
EnvironmentInfo.setAutomatic(false);
long evid = 0;
if (args.length <= 0) // no args
{
System.out.println ("Usage: java LocalMagnitudeEngine <evid> [<dbase>])");
System.exit(0);
} else {
Integer val = Integer.valueOf(args[0]); // convert arg String
evid = (int) val.intValue();
if (args.length > 1) host = args[1]; // dbase
}
System.out.println ("Making connection...");
DataSource init = new TestDataSource(); // make connection
init.setWriteBackEnabled(true);
if (init == null) {
System.exit(0);
}
System.out.println (" Getting evid = "+evid);
Solution sol = Solution.create().getById(evid);
System.out.println (" Getting amps for evid = "+evid);
// Read amps for this event from the dbase
// ArrayList amp = (ArrayList)Amplitude.create().getBySolution(sol);
//sol.magnitude.getAmps();
String list[] = {"WAU" } ; // get only W-A uncorrected amps
sol.magnitude.addAmps(Amplitude.create().getByMagnitude(sol.magnitude, list));
System.out.println ("Amp count = " + sol.magnitude.getAmpCount());
System.out.println ("Sta count = "+ sol.magnitude.ampList.getStationCount());
/*
for (int i = 0; i<sol.ampList.size(); i++)
{
System.err.println (sol.ampList.get(i).toString());
}
*/
System.out.println (" Original mag for: "+sol.toString());
System.out.println (sol.magnitude.toDumpString()) ;
System.out.println ("") ;
System.out.println (sol.magnitude.ampList.toNeatString());
System.out.println ("") ;
// Calc SoCal mag.
System.out.println (" Calculating mag (SoCalML) for: "+evid);
String DefaultMagMethodClass = "org.trinet.util.magnitudeengines.SoCalML";
String MLmagMethodClass = DefaultMagMethodClass;
String DefaultMagnitudeEngineClass = "org.trinet.util.magnitudeengines.LocalMagnitudeEngine";
String magEngineClass = DefaultMagnitudeEngineClass;
MagnitudeEngine magEng = MagnitudeEngine.CreateMagnitudeEngine(magEngineClass);
magEng.ConfigureMagnitudeEngine(MagnitudeMethod.CreateMagnitudeMethod(MLmagMethodClass));
// magEng.solve(sol.magnitude);
sol.magnitude = magEng.solve(sol);
System.out.println (sol.magnitude.toDumpString()) ;
System.out.println ("") ;
System.out.println (sol.magnitude.ampList.toNeatString());
/*
System.out.println ("sta count = "+ sol.magnitude.ampList.getStationCount());
System.out.println ("usedcount = "+ sol.magnitude.ampList.getStationUsedCount());
System.out.println ("chn count = "+ sol.magnitude.ampList.size());
System.out.println ("usedcount = "+ sol.magnitude.ampList.getChannelUsedCount());
*/
// commit the changes
//.// sol.magnitude.commit();
DataSource.close();
}
} // LocalMagnitudeEngine
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -