⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 amplitudetn.java

📁 一个用java写的地震分析软件(无源码)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.trinet.jasi.TN;

import org.trinet.jasi.*;

import java.util.*;
import java.sql.*;

import org.trinet.jdbc.*;
import org.trinet.jdbc.datatypes.*;
import org.trinet.jdbc.table.*;
import org.trinet.util.*;

/**
 * A concrete Amplitude object represents an peak amplitude reading made by any
 * number of techniques.
 *
 *
 * Created: Thu May  4 17:05:15 2000
 *
 * @author Doug Given
 * @version */

public class AmplitudeTN extends Amplitude {

    /** the orid of association */
    protected DataLong orid   	= new DataLong();

    /** the ampid */
    protected DataLong ampid   	= new DataLong();

    /** True if this Amp was orginally read from or has been written to the dbase.
     Used to know if a commit requires an 'insert' or an 'update' */
    boolean fromDbase = false;

    /** True if this Amp's association was orginally read from the dbase. Used to know if
  a commit requires an 'insert' or an 'update' */
    boolean assocFromDbase = false;

    /** The standard start to most SQL queries, to get amps associated with an
        event, joins Amp & AssocAmO tables */
    static String sqlPrefixByEvent = "Select "+
  Amp.QUALIFIED_COLUMN_NAMES+ ","+
  AssocAmO.QUALIFIED_COLUMN_NAMES+
  " from Amp, AssocAmO where (Amp.ampid = AssocAmO.ampid) ";

    /** The standard start to most SQL queries, to get amps associated with a
        magnitude, joins Amp & AssocAmM tables */
    static String sqlPrefixByMag = "Select "+
  Amp.QUALIFIED_COLUMN_NAMES+ ","+
  AssocAmM.QUALIFIED_COLUMN_NAMES+
  " from Amp, AssocAmM where (Amp.ampid = AssocAmM.ampid) ";

    /** The standard start to most SQL queries, to get amps in a time window */
    static String sqlPrefixByTime = "Select "+
  Amp.QUALIFIED_COLUMN_NAMES+
  " from Amp ";

    /** order so channels appear together in the list. */
    static String sqlOrderSuffix = " order by net, sta ";

    static final int Type_AssocAmO = 1;
    static final int Type_AssocAmM = 2;

    /** Holds value describing if this is amp lookup is done with a join to the
     * AssocAm or AssocAmM table */
    static int joinType;

    boolean debug = false;
    //boolean debug = true;

    public AmplitudeTN() {

    }

// Concrete methods from Amplitude ---------------------------------

    /**
     * Extract from DataSource all amps associated with this Magnitude. This
     * does a join using magid and the AssocAmM link table.  */
    public AmpList getByMagnitude (Magnitude mag) {
           return getByMagnitude (mag, null);
    }

    /**
     * Extract from DataSource all amps associated with this Magnitude. This
     * does a join using magid and the AssocAmM link table.  */
    public AmpList getByMagnitude (Magnitude mag, String[] ampTypeList) {

  String sql = sqlPrefixByMag +
      " and (AssocAmM.magid = "+ mag.magid.toString()+")";

        sql += makeTypeClause(ampTypeList) + sqlOrderSuffix;

  joinType = Type_AssocAmM;
  AmpList lst = (AmpList) AmplitudeTN.getBySQL(sql);

  //System.out.println ("getByMagnitude: amps found ="+lst.size());

  // associate all found amps with this solution
  for (int i = 0; i<lst.size(); i++) {
      ((Amplitude)lst.get(i)).associateMag(mag);
  }

  return lst;
    }
    /**
     * Extract from DataSource all amps associated with the Magnitude of this
     * event, described by an id number. Returns null if there is no such
     * id. This does a join using the AssocAmM link table.  */
    public AmpList getByMagnitude (long id) {
           return getByMagnitude (id, null);
    }

    /**
     * Extract from DataSource all amps associated with the Magnitude of this
     * event, described by an id number. Returns null if there is no such
     * id. This does a join using the AssocAmM link table.  */
    public AmpList getByMagnitude (long id, String[] ampTypeList) {

  // must first  retreive the Solution, then look up prefor for this id
  Solution tsol = Solution.create().getById(id);

  if (tsol == null) return null;	// no such solution in DataSource

  return getByMagnitude(tsol.magnitude, ampTypeList);
    }

    /**
     * Extract from DataSource all amps associated with this Solution. This does
     * a join using the AssocAmO link table.  */
    public AmpList getBySolution (Solution tsol) {
        return getBySolution(tsol, null);
    }
    /**
     * Extract from DataSource all amps associated with this Solution. This does
     * a join using the AssocAmO link table.  */

    public AmpList getBySolution (Solution tsol, String[] ampTypeList) {

  long orid = ((SolutionTN)tsol).getOrid();
  String sql = sqlPrefixByEvent +
      " and (AssocAmO.orid = "+ orid +")";

        sql += makeTypeClause(ampTypeList) + sqlOrderSuffix;

  joinType = Type_AssocAmO;
  AmpList lst = AmplitudeTN.getBySQL(sql);

  // associate all found amps with this solution
  for (int i = 0; i<lst.size(); i++) {
      ((Amplitude)lst.get(i)).associate(tsol);
  }

  return lst;
    }

    /** Return the SQL clause to get amps only of the types in the list.
    * For example given String list[] = {"PGA", "PGD" }  this will return
    * " and ((amptype like 'PGA') or (amptype like 'PGD') )*/
    String makeTypeClause (String[] ampTypeList) {

           String str = "";
           if (ampTypeList == null || ampTypeList.length == 0) return str;
           str += " and (";
           for (int i = 0; i < ampTypeList.length; i++) {
               if (i > 0) str += " or ";
               str += "(Amp.amptype like '" + ampTypeList[i] + "')";
           }
           str += ")";

 //          System.out.println ( "makeTypeQuery /"+str+"/");

           return str;

    }
    /**
     * Extract from DataSource all amps associated with this Solution ID. This
     * does a join using the AssocAmO link table.  */
    public AmpList getBySolution (long id) {
          return getBySolution (id, null);
    }
    /**
     * Extract from DataSource all amps associated with this Solution ID. This
     * does a join using the AssocAmO link table.
     * This does NOT get amps associated with the preferred magnitude. */
    public AmpList getBySolution (long id, String[] ampTypeList) {

  // must first  retreive the Solution, then look up prefor for this id
  Solution tsol = Solution.create().getById(id);

  if (tsol == null) return null;	// no such solution in DataSource

  return getBySolution(tsol, ampTypeList);
    }

    /**
     * Returns array of Amplitudes within this time window using default DataSource
     * connection. Times are seconds in
     * UNIX epoch time. Returns null if amplitudes are found. <br>
     * (NOTE: in TriNet this gets amps from ALL sources (RT1, RT2) so there will be duplicate
     * readings.) */
     // This is quite SLOW, need dbase indexing?
     public AmpList getByTime(double start, double stop) {

  return getByTime(DataSource.getConnection(), start, stop);
    }

    /**
     * Returns array of Amplitudes within this time window. Times are seconds in
     * UNIX epoch time.  Returns null if no amplitudes are found.<br>
     * (NOTE: in TriNet this gets amps from ALL sources (RT1, RT2) so there will be duplicate
     * readings.)  */
     // This is quite SLOW, need dbase indexing?
     public AmpList getByTime(Connection conn, double start, double stop) {

           String sql = sqlPrefixByTime + " where DATETIME BETWEEN " +
               StringSQL.valueOf(start) + " AND " + StringSQL.valueOf(stop) +
               " order by Datetime";

        return getBySQL(conn, sql);

    }

    /**
     * Returns array of Amps based on the results of the SQL query to an
     * NCDCv1.5 data base. It must be of a form that returns the full set
     * of columns.  Returns null if no data is found.  Uses default connection
     * created by DataSource.  */
    protected static AmpList getBySQL(String sql)
    {
  if (DataSource.getConnection() == null)
      {
    System.err.println ("* No DataSource is open.");
    return null;
      }
  return getBySQL(DataSource.getConnection(), sql);
    }

    /** Return an Amplitude with the given unique ID number. Returns null
     *  if there is no such amplitude. */
    public Amplitude getById(long id) {

        String sql = sqlPrefixByTime + " where ampid = "+id;
        AmpList ampList = getBySQL(DataSource.getConnection(), sql);

  if (ampList.isEmpty()) return null;

  return (Amplitude) ampList.get(0);
    }

    /**
     * Returns array of Amps based on the results of the SQL query to an
     * NCDCv1.5 data base. It must be of a form that returns the full set
     * of columns.  Returns null if no data is found.  Uses default connection
     * created by DataSource.  */
    protected static AmpList getBySQL(Connection conn, String sql)
    {
  AmpList ampList = new AmpList();

  // Debug
//	System.out.println ("SQL: "+sql);

  try {
      if ( conn.isClosed() )	// check that valid connection exists
    {
        System.err.println ("* DataSource connection is closed");
        return null;
    }

      Statement sm = conn.createStatement();

      // NOTE: if ExecuteSQL.setSelectForUpdate(true) was set in DataSource
      // this will lock the selected rows until commit() or close() are called
      ResultSetDb rsdb = new ResultSetDb(ExecuteSQL.rowQuery(sm, sql));

      if (rsdb == null) return null;	// nothing found

         // note: this can't tell if it its adding an amp a second time
         // if its not the same object instance
      while ( rsdb.getResultSet().next() ) {

    ampList.add(AmplitudeTN.parseResultSet(rsdb));
      }

      sm.close();

  }
  catch (SQLException ex) {
      System.err.println(ex);
      ex.printStackTrace();
  }

  return ampList;
    }

    /**
     * Parse a resultset row that contains a concatinated Arrival/AssocArO
     */
    protected static Amplitude parseResultSet (ResultSetDb rsdb) {

  int offset = 0;

  // get jdbc objects from the result set
  Amp amp = new Amp();
  amp = (Amp) amp.parseOneRow(rsdb, offset);

  // build up the Amplitude from the jdbc objects
  AmplitudeTN ampNew = new AmplitudeTN();

  ampNew.parseAmp(amp);

  offset += Amp.MAX_FIELDS;

  // Parse depends on whether you did joint with AssocAmO or AssocAmM table.
  if (joinType == Type_AssocAmO) {

      AssocAmO  assoc = new AssocAmO();
      assoc = (AssocAmO) assoc.parseOneRow(rsdb, offset);
      ampNew.parseAssocAmO(assoc);
      if (DataSource.isWriteBackEnabled()) assoc.setMutable(true);

  } else if (joinType == Type_AssocAmM) {
      AssocAmM  assoc = new AssocAmM();
      assoc = (AssocAmM) assoc.parseOneRow(rsdb, offset);
      ampNew.parseAssocAmM(assoc);
      if (DataSource.isWriteBackEnabled()) assoc.setMutable(true);
  }

  // allow changes to this DataTableRow. Need to set this because we are
  // not using our own rather than org.trinet.jdbc parsing methods
  if (DataSource.isWriteBackEnabled()) amp.setMutable(true);

  // remember that we got this from the dbase
  ampNew.fromDbase = true;

  return (Amplitude) ampNew;
    }

    /**
     * Copy DataObjects out of a jdbc.Amp object into this Amplitude.
     */

    protected void parseAmp(Amp amp) {

     // call setChannel to synch with master channel list
  setChannelObj(ChannelTN.parseNameFromDataTableRow(amp));

        this.authority    = (DataString) amp.getDataObject(Amp.AUTH);
        this.source       = (DataString) amp.getDataObject(Amp.SUBSOURCE);

  this.ampid        = (DataLong)   amp.getDataObject(Amp.AMPID);

  this.datetime	   = (DataDouble) amp.getDataObject(Amp.DATETIME);

  this.processingState = (DataString) amp.getDataObject(Amp.RFLAG);

  this.phaseType    = (DataString) amp.getDataObject(Amp.IPHASE);

  this.value	   = (DataDouble) amp.getDataObject(Amp.AMPLITUDE);

  // Set AmpType value
  String ampType  = amp.getDataObject(Amp.AMPTYPE).toString();
  this.type       = AmpType.getInt(ampType);

  // if zero-to-peak = 1 , if peak-to-peak = 0
  String half  = amp.getDataObject(Amp.AMPMEAS).toString();
  if (half.equals("0")) this.halfAmp=false;

  // Set units
  String unitstr  = amp.getDataObject(Amp.UNITS).toString();
  this.units = Units.getInt(unitstr);

  this.uncertainty  = (DataDouble) amp.getDataObject(Amp.ERAMP);

  // IGNORING 'FLAGAMP', 'TAU'

  this.period  = (DataDouble) amp.getDataObject(Amp.PER);
  this.snr     = (DataDouble) amp.getDataObject(Amp.SNR);
  this.quality = (DataDouble) amp.getDataObject(Amp.QUALITY);

  String clip  = amp.getDataObject(Amp.CFLAG).toString();
  if      (clip.equalsIgnoreCase("OS")) { this.clipped =  ON_SCALE; }   //on scale
  else if (clip.equalsIgnoreCase("BN")) { this.clipped =  CLIPPED; }   //clipped
  else if (clip.equalsIgnoreCase("CL")) { this.clipped =  BELOW_NOISE; }   //below noise

  this.windowStart	  = (DataDouble) amp.getDataObject(Amp.WSTART);
  this.windowDuration	  = (DataDouble) amp.getDataObject(Amp.DURATION);

  return;
    }

/**
 * Copy DataObjects out of a jdbc.AssocAmO object into a Amplitude.
 */

protected void parseAssocAmO(AssocAmO assoc) {

    this.orid         = (DataLong)   assoc.getDataObject(AssocAmO.ORID);
    this.chan.dist    = (DataDouble) assoc.getDataObject(AssocAmO.DELTA);
    this.chan.azimuth = (DataDouble) assoc.getDataObject(AssocAmO.SEAZ);

    return;
}

/**
 * Copy DataObjects out of a jdbc.AssocAmO object into a Amplitude.
 */
protected void parseAssocAmM(AssocAmM assoc) {

    channelMag.value      = (DataDouble) assoc.getDataObject(AssocAmM.MAG);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -