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

📄 locationenginehypoinverse.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.trinet.util.locationengines;

/**
 * Calculate a location using the remote location server.
 */
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.net.*;
import javax.swing.*; // JFC "swing" library

import org.trinet.jdbc.*; // Allan's package
import org.trinet.util.*;
import org.trinet.hypoinv.*;
import org.trinet.jasi.*;
import org.trinet.jdbc.datatypes.DataString;
import org.trinet.jiggle.*;

public class LocationEngineHypoInverse
    extends LocationEngine {

  /** URL of remote solution service*/
  String serverAddress;
  /** Port of remote solution service*/
  int port;

  Socket socket;
  InputStream streamIn;
  OutputStream streamOut;

  /** Socket timeout interval, default = 5000 (5 sec). Without this socket
      reads would block forever if server didn't respond. */
  int SocketTimeoutMillis = 45000;

  /** The Original solution */
  Solution sol;

  JTextArea textArea; // optional output area for results
  String outstr;

  /** Holds status message. */
  String message;

  PhaseList phaseList; // list of phases used to do location

  ArcSummary arcsum = new ArcSummary(); // aww added new class for formatted raw ARC formats
  ArcStation arcstn = new ArcStation(); // aww added new class for formatted raw ARC formatsq

  /** String describing location method. */
  static final String locationMethod = "HYP2000";
  static final String datumHorizontal = "NAD27";
  static final String datumVertical = "AVERAGE";

  boolean debug = true;

  /** */
  public LocationEngineHypoInverse() {
    reset();
  }

  /**
   * serverIPAddress has the form: "131.215.66.154" or "splat.gps.caltech.edu"
   */
  public LocationEngineHypoInverse(String serverIPAddress, int serverPort) {
    setServer(serverIPAddress, serverPort);
    reset();
  }

  /** Set the remote locationserver. If it is a change, reestablish a connection. */
  public void setServer(String serverIPAddress, int serverPort) {
    // is it a change?
    if (!serverIPAddress.equals(serverAddress) ||
        port != serverPort) {

      disconnect(); // close any old socket
      serverAddress = serverIPAddress;
      port = serverPort;

      connect(); // open new one
    }
  }

  /** Clears out old solution message information. */
  public void reset() {
    outstr = "No solution.";
    message = "";
  }

  /**
   * We don't connect in the constructor because that wouldn't allow a status return
   */
  public boolean connect() {
    try {
      socket = new Socket(serverAddress, port); //"stream" (i.e. TCP) socket

      socket.setSoTimeout(SocketTimeoutMillis);

    }
    catch (Exception exc) {
      exc.printStackTrace();
      return false;
    }

    try {
      streamIn = socket.getInputStream();
      streamOut = socket.getOutputStream();
    }
    catch (IOException exc) {
      exc.printStackTrace();
      return false;
    }
    return true;
  }

  public void disconnect() {
    try {
      socket.close();
    }
    catch (Exception e) {} // so what...
  }

  /**
   * Calculate a solution given a Solution and an ArrivalList.
   * Returns true on success and false if phase list is emply.
   */
  public boolean solve(Solution sol) {
    this.sol = sol;
    return solve(sol.phaseList);
  }

  /**
   * Calculate a solution given a Solution and an ArrivalList.
   * Returns true on success and false if phase list is emply.
   */
  public boolean solve(Solution sol, PhaseList phaseList) {
    this.sol = sol;
    return solve(phaseList);
  }

  /**
   * Calculate a solution given an ArrivalList.
   * Returns true on success and false if phase list is emply.
   */
  public boolean solve(PhaseList phaseList) {

    boolean status;

    this.phaseList = phaseList;

    if (phaseList.isEmpty()) {
      message = "Cannot locate. No phases in phase list.";
      outstr = message;
      System.out.println(message);
      return false; // no phases
    }

    Phase ph[] = (Phase[]) phaseList.getArray();

    System.out.println("Locating with " + phaseList.size() +
                       " phases from phaseList");

// connect to the server
    if (!connect()) {
      message = "Server connection failed to " + serverAddress;
      outstr = message;
      System.out.println(message);
      return false;
    }
    else {
      if (debug)
        System.out.println("Connection made to " + serverAddress +
                           "  port " + port);
    }

// set a line telling the solution server what to call the remote file (this was
// done incase we want to keep and examine the remote files)

    String fileMsg = "|FILE| " + sol.id.longValue();

    writeMessage(fileMsg); // write phase data to the solserver socket

    sendPhaseList(phaseList);

// send a terminator line (Hypoinverse needs this)
// Set appropriate fixed flags
    writeMessage(HypoFormat.toTerminatorString(sol,
                                               sol.locationFixed.booleanValue(),
                                               sol.depthFixed.booleanValue(),
                                               getUseTrialLocation()));

    writeMessage("|EOF|"); // signal solserver that its the end of data

// <SOLUTION> read the returned results from the server
    if (debug)
      System.out.println(" ** Switching to read mode ** ");

    status = readResults();

// Calculate gap

    sol.gap.setValue(sol.getPhaseList().getMaximumAzimuthalGap());

// debug
    //System.out.println (phaseList.dumpArcToString());
    if (debug)
      System.out.println(" ** Disconnecting **");
    disconnect(); // release the server

    // reset flag since solution is now "fresh"
    sol.setStale(false);

    return status;
  }

  /** Returns a brief status message indicating the status of the location run.
   * Generally used for status bars, etc.*/
  public String getMessage() {
    return message;
  }

  /** Return Hypoinverse style output for this location run. It will be multi-string
   * output with "\n" line separators. */
  public String getResultString() {
    return outstr;
  }

  /**
   * Format and send a phase list.
   */
  protected boolean sendPhaseList(PhaseList phaseList) {

    boolean status = true;

    // Get array of assocated phases
    Phase ph[] = (Phase[]) phaseList.getAssociated(sol).toArray(new Phase[0]);

// send phase lines in Hypoinverse archive format (solserver must be set for
// this format)

    for (int i = 0; i < ph.length; i++) {
      if (ph[i].isDeleted())
        continue; // skip deleted phases

      String str = HypoFormat.toArchiveString(ph[i]); // format

      status |= writeMessage(str); // write phase data to the solserver socket

      if (debug)
        System.out.println(str); // debug
    }

    return status;
  }

  /**
   * Read a result stream from the socket. This is just a Hypoinverse .ARC file
   */

// TODO: handling textArea output directly here is bogus.

  protected boolean readResults() {
    BufferedReader reader = new BufferedReader(new InputStreamReader(streamIn));

    String header;
    outstr = "";

// read the 1st line, should be Hypoinverse event summary line
    try {
      header = reader.readLine();
    }
    catch (InterruptedIOException exc) { // socket timed out

      message = "Location server timed out.";

      outstr = " No solution was possible for this event.";
      outstr += "Socket timed out after " +
          SocketTimeoutMillis / 1000 + " seconds.";
      if (textArea != null)
        textArea.append(outstr + "\n");
      System.err.println(outstr);
      return false;

    }
    catch (IOException exc) {
      message = "Location server: socket read error.";

      outstr = " No solution was possible for this event.";
      outstr += "Socket read error:" + exc.getMessage();
      if (textArea != null)
        textArea.append(outstr + "\n");
      System.err.println(outstr);
      return false;
    }

    if (header == null) {
      message = " No solution was possible for this event.";
      outstr += " No solution was possible for this event.";

      System.out.println(outstr);
      if (textArea != null)
        textArea.append(outstr + "\n");
      return false; // no solution
    }

    // is there a header?

    // Make an arc formatted string for output (put in textarea if there is one.

    //    if (textArea != null) {

    outstr = "HYPOINVERSE SERVER ARC RESULTS ";

    if (arcsum.parseArcSummary(header)) {

      outstr += (arcsum.getFormattedErrorEllipseString() + "\n");
      outstr += (ArcSummary.getTitle() + "\n");
      outstr += (arcsum.getFormattedOriginString() + "\n");
    }
    else {
      outstr += ("Bad Arc Summary Header parseStatus" + "\n");
    }



    // copy location dependent info from newSol to old Solution
    // he orid, evid, commid, Event and Magnitude objects are unchanged
    outstr += (ArcStation.getTitle() + "\n");
    //    }

    // parse header into existing solution. The orid, evid, commid, Event
    // and Magnitude objects are unchanged.

    // Save event type (e.g. local, quarry) and restore it later
    // because it is cleared by call to clearLocationAttributes()

    // (there's a bug in DataString that doesn't copy the string but a reference)
    DataString savedEventType = new DataString(sol.eventType.toString());

⌨️ 快捷键说明

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