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

📄 waveclient.java

📁 一个用java写的地震分析软件(无源码)-used to write a seismic analysis software (without source)
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
* True implies data packets are checked for channel and time consistency.
* Default value == true;
*/
    public boolean isVerifyWaveforms() {
        return verifyWaveforms;
    }

/** Sets the default verification mode for any time series data retrieved from a server.
* True value implies all data packets are checked for channel and time consistency,
* which requires decoding data headers and adds overhead to the processing.
* If waveforms are reliably transferred from servers set state to false for faster response.
* Default value == true;
*/
    public boolean setWaveformVerify(boolean value) {
        return(verifyWaveforms = value);
    }

/** Returns the number of servers currently known by this WaveClient
*/
    public int numberOfServers() {
        return servers.size();

    }
/** Returns an array of Strings each string describes a server known by this WaveClient.
*/
    public String [] listServers() {
        String [] clients = new String [servers.size()];
        Set keySet = servers.keySet();
        Iterator iter = keySet.iterator();
        int index = 0;
        while (iter.hasNext()) {
            clients[index] = ((Client) iter.next()).toString();
            index++;
        }
        return clients;
    }

/** Adds the specified host and port number to the polled servers collection.
* Returns true if successful, false if error occurs, or server already is in list.
*/
    public boolean addServer(String host, int port) {
        return addServer(new Client(host, port)); // InetAddress ?
    }

/** Adds the host and port number specified in the input Client object to the servers collection.
* Servers in the collection are polled sequentially to locate the requested  data.
* If a server does not have the data, the next server in the iteration, if any is polled to satisfy a data request.
* Returns true if successful, false if error occurs, or server already is in list.
*/
    public boolean addServer(Client client) {

        boolean retVal = true;

        TCPConn conn = null;
        try {
            if (client == null)
                throw new NullPointerException("WaveClient.addServer() input Client parameter is null");
            if (servers.containsKey(client)) {
                System.err.println("Error WaveClient.addServer() : " + client.toString() + " already in list of servers");
                return false;
            }
            conn = new TCPConnClient(client);
            conn.setDefaultTimeout(maxTimeout);
        }
        catch (UnknownHostException ex) {
            System.err.println("Error WaveClient.addServer() Unknown host: " + client.host + " port: " + client.port);
            retVal = false;
        }
        catch (IOException ex) {
            System.err.println("Error WaveClient.addServer() IO error connecting to " + client.host + " port: " + client.port);
            System.err.println(ex.toString());
            retVal = false;
        }
        catch (NullPointerException ex) {
            System.err.println("WaveClient.addServer() input Client parameter is null");
            retVal = false;
        }
        catch (Exception ex) {
            System.err.println("Error WaveClient.addServer() caught exception connecting to " +client.host+ " port: " +client.port);
            System.err.println(ex.toString());
            ex.printStackTrace();
            retVal = false;
        }

        if (retVal == true) {
            if (servers.put(client, conn) != null) {
                System.err.println("Error WaveClient.addServer() : " + conn.toString() + " already in list of servers");
                retVal =  false;
            }
        }
        return retVal;
    }

/** Removes the specified host and port number from the polled server collection.
*   Returns true if successful, false if an error occurs or input parameter is null.
*/
    public boolean removeServer(String host, int port) {
        return removeServer(new Client(host, port)); // InetAddress ?
    }

/** Removes the host and port number specified in the specified Client object from the polled
* server collection and closes its socket connection.
* Returns true if successful, false if an error occurs or input parameter is null.
*/
    public boolean removeServer(Client client) {

        if (client == null) {
            System.err.println("WaveClient removeServer() input Client parameter is null");
            return false;
        }

        TCPConn conn = (TCPConn) servers.get(client);

        if (conn == null) {
            System.err.println("Error WaveClient.removeServer() server: " + client.toString() + " not found");
            return false;
        }
        else {
            conn.close();
            servers.remove(client);
        }
        return true;
    }

/** Closes all connections for the entire server list. */
    public void close() {
        // get all available servers
        Collection connList = servers.values();
        Iterator iter = connList.iterator();
        while (iter.hasNext()) {
            ((TCPConn) iter.next()).close();
        }
    }

/** Requests time series data for the specified data channel, start time and duration.
* If data is found the data is converted into FloatTimeSeries objects returned as elements added to the input collection.
* The input collection is cleared and new data appended.
* Returns TN_SUCCESS if successful, TN_NODATA if no data are found, else an TRINET error return code.
* <b>By default data packet headers are checked for data consistency, for faster response disable verification.</b>
* @see #setWaveformVerify(boolean)
* @see #isVerifyWaveforms()
* @see #getData(Channel, TimeWindow, Waveform)
* @exception java.lang.NullPointerException input collection parameter is null
* @exception java.lang.IllegalArgumentException Valid Channel/TimeWindow cannot be constructed from the input parameters.
*/
    public int getData(String net, String sta, String chn, Date beginTimestamp, int durationSeconds, Collection timeSeriesList) {
        if (timeSeriesList == null)
            throw new NullPointerException("WaveClient.getData() input FloatTimeSeries collection parameter is null");
        // Clear any existing FloatTimeSeries structures
        timeSeriesList.clear();

        Channel chan = null;
        try {
            chan = new Channel(net, sta, chn);
        }
        catch (IllegalArgumentException ex) {
            System.err.println("WaveClient.getData(): " + ex.getMessage());
            ex.fillInStackTrace();
            throw ex;
        }

        TimeWindow timeWindow = null;
        try {
            timeWindow = new TimeWindow( beginTimestamp, new Date( beginTimestamp.getTime() + (durationSeconds * 1000) ) );
        }
        catch (IllegalArgumentException ex) {
            System.err.println("WaveClient.getData(): " + ex.getMessage());
            ex.fillInStackTrace();
            throw ex;
        }

        Waveform waveform = new Waveform();

        // Retrieve the waveform data
        int retVal = getData(chan, timeWindow, waveform);
        if (retVal < TN_SUCCESS) {
            if (retVal == TN_NODATA) return retVal;
        }

        if (! waveform.trim(timeWindow)) {
            System.err.println("Error WaveClient.getData() Unable to trim waveform to time window");
            return TN_FAILURE;
        }
        chan.sampleRate =  waveform.getSampleRate();
        if (chan.sampleRate <= 0.0) {
            System.err.println("Error WaveClient.getData() waveform sample rate is undefined.");
            return TN_FAILURE;
        }

        List dataCountsList = new ArrayList();

        retVal = TN_SUCCESS;

        try {
            while (true) {
                Waveform waveformSegment = waveform.next();
                if (waveformSegment == null) {
                    if (timeSeriesList.size() > 0) retVal = TN_SUCCESS;
                    else {
                        retVal = TN_NODATA;
                        System.err.println("WaveClient.getData() end of data found for channel: " + chan.toString());
                    }
                    break;
                }
                if (! waveformSegment.getCounts(dataCountsList)) {
                    System.err.println("Error WaveClient.getData() Unable to get counts from waveform." );
                    retVal = TN_FAILURE;
                    break;
                }
                if (dataCountsList.size() != 1) {
                    System.err.println("Error WaveClient.getData() count list has " + dataCountsList.size() + " elements");
                    retVal = TN_FAILURE;
                    break;
                }
                // Note - Only one element, don't need iterator for list now.
                DataCounts dataCounts = (DataCounts) dataCountsList.get(0);
                if (dataCounts.dataList == null) {
                    System.err.println("Error WaveClient.getData() null data counts list.");
                    retVal = TN_FAILURE;
                    break;
                }
                // Changed block below to avoid extra object conversion re changes to Waveform - aww

                /*
                // Convert DataCount List of Number object counts to primitive array of counts
                    int numberOfSamples = dataCounts.dataList.size();
                    float [] samples = new float [numberOfSamples];
                    for (int index = 0; index < numberOfSamples; index++) {
                        samples[index] = ((Float) dataCounts.dataList.get(index)).floatValue();
                    }
                    FloatTimeSeries timeSeries =
                          new FloatTimeSeries( dataCounts.startTimestamp,
                                         new Date( dataCounts.startTimestamp.getTime() +
                                             (long) ( 1000. * (double)(numberOfSamples - 1) / waveform.getSampleRate() ) ),
                                             (float []) dataCounts.dataList.get(index);
                                             samples);
                */

                float [] samples =  (float []) dataCounts.dataList.get(0);
                int numberOfSamples = samples.length;
                FloatTimeSeries timeSeries =
                          new FloatTimeSeries( dataCounts.startTimestamp,
                                               new Date( dataCounts.startTimestamp.getTime() +
                                               (long) ( 1000. * (double)(numberOfSamples - 1) / waveform.getSampleRate() ) ),
                                               samples);
                timeSeriesList.add(timeSeries);
            }
        }
        catch (WaveformDataException ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace();
            retVal = TN_FAILURE;
        }
        catch (Exception ex) {
            System.err.println("Error WaveClient.getData() caught exception");
            ex.printStackTrace();
            retVal = TN_FAILURE;
        }
        return retVal;
    }

/** Requests a list of TimeRange objects describing the available timeseries for the specified channel string descriptors.
* Clears the input TimeRange list and appends the data returned in the server response, if any, to this list.
* Returns TN_SUCCESS if successful, TN_NODATA if no data is found, else a TRINET error return code.
* @exception java.lang.NullPointerException input collection parameter is null
* @exception java.lang.IllegalArgumentException Valid Channel cannot be constructed from the input parameters.
*/
    public int getTimes(String net, String sta, String chn, Collection timeRangeList) {
        if (timeRangeList == null)
            throw new NullPointerException("WaveClient.getTimes() input TimeRange collection parameter is null");
        // Clear any existing TimeRange structures
        timeRangeList.clear();

⌨️ 快捷键说明

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