/******************************************************************************* * Copyright (c) 2011 IRIS/DMC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * IRIS/DMC- initial API and implementation * * ACKNOWLEDGEMENT * This software was developed as part of a project supported by * Cooperative Agreement Number G10AC00533 from the United States * Geological Survey. Its contents are solely the responsibility of * the authors and the USGS is not responsible for the efficacy, * safety, or suitability of this software. ******************************************************************************/ import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; import java.text.CharacterIterator; import java.text.StringCharacterIterator; import java.util.TimeZone; import java.util.Calendar; import javax.xml.stream.XMLStreamException; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; public class PrintQueue { private XMLWriter writer; private Query query; private JdbcTemplate jdbcTemplate; private boolean resultStreamed = false; private Queue q = new LinkedList(); private String old_net = ""; private String old_sta = ""; private String current_net = ""; private String current_sta = ""; private boolean current_sta_open = false; int SelectedNbStations = 0; public PrintQueue(JdbcTemplate jdbcTemplate, XMLWriter writer, Query query) { this.writer = writer; this.query = query; this.jdbcTemplate = jdbcTemplate; } public void add(Node node) throws IOException { if (node instanceof Station && !this.q.isEmpty()) { print(); writer.flush(); this.q.clear(); } this.q.add(node); resultStreamed = true; } public void add(List nodes) throws IOException { this.q.addAll(nodes); } private boolean documentStarted = false; @SuppressWarnings("unchecked") private void print() throws IOException { if (!documentStarted) { startDocument(); documentStarted = true; } Station station = null; StationEpoch epoch = null; DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinuteSecond().withZone(DateTimeZone.UTC); DateTime creationDate = null; int index = 0; while (!this.q.isEmpty()) { Node node = q.poll(); if (node instanceof Station) { station = (Station) node; this.old_net = this.current_net; this.current_net = station.getNetCode(); if (this.old_net != this.current_net) { if (this.old_net != "") { if (query.getLevel() > Query.NETWORK_LEVEL) { writer.endElement(); // ENDING STATION this.current_sta_open = false; } writer.endElement(); // ENDING NETWORK writer.flush(); } final int SNS = this.SelectedNbStations; MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("net", this.current_net); new NamedParameterJdbcTemplate(jdbcTemplate).query( SQL.networkSql, params, new ResultSetExtractor() { @Override public Object extractData(ResultSet rs) throws SQLException, DataAccessException { try { rs.next(); Map attributes = new LinkedHashMap(); attributes.put("code", rs.getString(SQL.net)); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinuteSecond().withZone(DateTimeZone.UTC); DateTime s_net = new DateTime(rs.getTimestamp(SQL.net_sdate, calendar).getTime()); DateTime e_net = new DateTime(rs.getTimestamp(SQL.net_edate, calendar).getTime()); attributes.put("startDate", s_net.toString(fmt)); attributes.put("endDate", e_net.toString(fmt)); writer.startElement(Schema.NETWORK, attributes); writer.startElement("Description"); writer.value(rs.getString(SQL.net_desc)); writer.endElement(); writer.startElement("TotalNumberStations"); writer.value(rs.getInt(SQL.sta_count)); writer.endElement(); writer.startElement("SelectedNumberStations"); if (query.getLevel() > Query.NETWORK_LEVEL) { writer.value(SNS); } else { writer.value(0); } writer.endElement(); } catch (IOException e) { throw new StationRetrievalException( e.getMessage()); } return null; } } ); } } else if (node instanceof StationEpoch) { this.old_sta = this.current_sta; this.current_sta = station.getStaCode(); if ( this.current_sta_open == true ) { writer.endElement(); // ENDING STATION writer.flush(); } epoch = (StationEpoch) node; Map attributes = new HashMap(); // attributes.put("swap_order_16", epoch.getSwapOrder16()); // attributes.put("swap_order_32", epoch.getSwapOrder32()); attributes.put("code", station.getStaCode()); attributes.put("startDate", epoch.getStarted().toString(fmt)); attributes.put("endDate", epoch.getEnded().toString(fmt)); writer.startElement(Schema.STATION, attributes); this.current_sta_open = true; writer.startElement(Schema.LAT); writer.value(epoch.getLatitude()); writer.endElement(); writer.startElement(Schema.LON); writer.value(epoch.getLongitude()); writer.endElement(); writer.startElement(Schema.ELEVATION); writer.value(epoch.getElevation()); writer.endElement(); writer.startElement(Schema.SITE); // writer.startElement(Schema.COUNTRY); // writer.value(epoch.getSite()); // writer.endElement(); // writer.endElement(); writer.startElement(Schema.NAME); writer.value(epoch.getSite()); writer.endElement(); writer.endElement(); writer.startElement(Schema.CREATION_DATE); writer.value(epoch.getStation().getCreationDate().toString(fmt)); writer.endElement(); // writer.startElement("TotalNumberChannels"); // writer.value(epoch.getChannels().size()); // writer.endElement(); } else if (node instanceof Channel) { Channel channel = (Channel) node; // Map attributes = new HashMap(); // attributes.put("code", channel.getCode()); // attributes.put("locationCode", channel.getLocation()); // writer.startElement(Schema.CHANNEL, attributes); // writer.startElement(Schema.CREATION_DATE); // writer.value(channel.getCreated().toString(fmt)); // writer.endElement(); for (ChannelEpoch ce : channel.getEpochs()) { int CFlag = 1; if (query.getStartAfter() != null) { if (query.getStartAfter().isAfter(ce.getStarted())) CFlag = 0; } if (query.getStartBefore() != null) { if (ce.getStarted().isAfter(query.getStartBefore())) CFlag = 0; } if (query.getEndAfter() != null) { if (query.getEndAfter().isAfter(ce.getEnded())) CFlag = 0; } if (query.getEndBefore() != null) { if (ce.getEnded().isAfter(query.getEndBefore())) CFlag = 0; } if (query.getTimeWindow() != null) { if ( ((ce.getStarted().isAfter(query.getTimeWindow()[0])) && (ce.getStarted().isBefore(query.getTimeWindow()[1]))) || ((ce.getEnded().isAfter(query.getTimeWindow()[0])) && (ce.getEnded().isBefore(query.getTimeWindow()[1]))) || ((ce.getStarted().isBefore(query.getTimeWindow()[0])) && (ce.getEnded().isAfter(query.getTimeWindow()[1]))) ) CFlag = 1; else CFlag = 0; } if (CFlag == 1) { Map attributes = new HashMap(); attributes.put("code", channel.getCode()); String xloc = channel.getLocation().replace(" ", ""); attributes.put("locationCode", xloc); // attributes.put("startDate", ce.getStarted().toString(fmt)); // attributes.put("endDate", ce.getEnded().toString(fmt)); attributes.put("startDate", ce.getStarted().toString().substring(0,19)); attributes.put("endDate", ce.getEnded().toString().substring(0,19)); final DateTime s_user = ce.getStarted(); final DateTime e_user = ce.getEnded(); final String ss_user = ce.getStarted().toString().substring(0,19); final String se_user = ce.getEnded().toString().substring(0,19); writer.startElement(Schema.EPOCH, attributes); // Data Availability if (query.getAvailability() == true) { writer.startElement("DataAvailability"); MapSqlParameterSource dparams = new MapSqlParameterSource(); dparams.addValue("net", this.current_net); dparams.addValue("sta", this.current_sta); dparams.addValue("cha", channel.getCode()); dparams.addValue("loc", channel.getLocation()); new NamedParameterJdbcTemplate(jdbcTemplate).query( SQL.dataSql, dparams, new ResultSetExtractor() { @Override public Object extractData(ResultSet drs) throws SQLException, DataAccessException { try { if (drs.next()) { Map dattributes = new LinkedHashMap(); Calendar dcalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); DateTimeFormatter dfmt = ISODateTimeFormat.dateHourMinuteSecond().withZone(DateTimeZone.UTC); DateTime s_data = new DateTime(drs.getTimestamp(SQL.data_sdate, dcalendar).getTime()); DateTime e_data = new DateTime(drs.getTimestamp(SQL.data_edate, dcalendar).getTime()); if (s_data.isAfter(s_user)) { dattributes.put("start", s_data.toString(dfmt)); } else { dattributes.put("start", ss_user); } if (e_data.isAfter(e_user)) { dattributes.put("end", se_user); } else { dattributes.put("end", e_data.toString(dfmt)); } writer.startElement("Extent", dattributes); writer.endElement(); } } catch (IOException e) { throw new StationRetrievalException( e.getMessage()); } return null; } } ); writer.endElement(); } writer.startElement(Schema.LAT); writer.value(ce.getLatitude()); writer.endElement(); writer.startElement(Schema.LON); writer.value(ce.getLongitude()); writer.endElement(); writer.startElement(Schema.ELEVATION); writer.value(ce.getElevation()); writer.endElement(); writer.startElement(Schema.DEPTH); writer.value(ce.getDepth()); writer.endElement(); writer.startElement(Schema.AZIMUTH); writer.value(ce.getAzimuth()); writer.endElement(); writer.startElement(Schema.DIP); writer.value(ce.getDip()); writer.endElement(); if (ce.getFlags() != null) { CharacterIterator it = new StringCharacterIterator(ce.getFlags().trim()); for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { switch (ch) { case 'T': writer.startElement(Schema.FLAGS); writer.value("TRIGGERED"); writer.endElement(); break; case 'C': writer.startElement(Schema.FLAGS); writer.value("CONTINUOUS"); writer.endElement(); break; case 'H': writer.startElement(Schema.FLAGS); writer.value("HEALTH"); writer.endElement(); break; case 'G': writer.startElement(Schema.FLAGS); writer.value("GEOPHYSICAL"); writer.endElement(); break; case 'W': writer.startElement(Schema.FLAGS); writer.value("WEATHER"); writer.endElement(); break; case 'F': writer.startElement(Schema.FLAGS); writer.value("FLAG"); writer.endElement(); break; case 'S': writer.startElement(Schema.FLAGS); writer.value("SYNTHESIZED"); writer.endElement(); break; case 'I': writer.startElement(Schema.FLAGS); writer.value("INPUT"); writer.endElement(); break; case 'E': writer.startElement(Schema.FLAGS); writer.value("EXPERIMENTAL"); writer.endElement(); break; case 'M': writer.startElement(Schema.FLAGS); writer.value("MAINTENANCE"); writer.endElement(); break; case 'B': writer.startElement(Schema.FLAGS); writer.value("BEAM"); writer.endElement(); break; } } } writer.startElement(Schema.SAMPLE_RATE); writer.value(ce.getSampleRate()); writer.endElement(); writer.startElement(Schema.CLOCK_DRIFT); writer.value(ce.getClockDrift()); writer.endElement(); writer.startElement("CalibrationUnits"); writer.startElement("Name"); writer.value("A"); writer.endElement(); writer.startElement("Description"); writer.value("Amperes"); writer.endElement(); writer.endElement(); if (null != ce.getInstrument()) { writer.startElement(Schema.SENSOR); writer.startElement("Description"); writer.value(ce.getInstrument()); writer.endElement(); writer.endElement(); } writer.startElement("Response"); if (ce.getB58().getSensitivity() != null) { // InstrumentSensitivity writer.startElement("InstrumentSensitivity"); if (ce.getB58().getSensitivity() != null) { writer.startElement(Schema.SENSITIVITY_VALUE); writer.value(ce.getB58().getSensitivity()); writer.endElement(); } if (ce.getB58().getFrequency() != null) { writer.startElement(Schema.FREQUENCY); writer.value(ce.getB58().getFrequency()); writer.endElement(); } if ((ce.getB58().getUnits() != null) || (ce.getB58().getDescription() != null)) { writer.startElement(Schema.INPUT_UNITS); if (ce.getB58().getUnits() != null) { writer.startElement("Name"); writer.value(ce.getB58().getUnits()); writer.endElement(); } if (ce.getB58().getDescription() != null) { writer.startElement("Description"); writer.value(ce.getB58().getDescription()); writer.endElement(); } writer.endElement(); } writer.startElement(Schema.OUTPUT_UNITS); writer.startElement("Name"); writer.value("COUNTS"); writer.endElement(); writer.startElement("Description"); writer.value("Digital Counts"); writer.endElement(); writer.endElement(); writer.endElement(); } else { // InstrumentPolynomial StationEpoch s = (StationEpoch) ce.getChannel().getEpoch(); final String input_units_n = ce.getB58().getUnits(); final String input_units_d = ce.getB58().getDescription(); String netCode = s.getStation().getNetCode(); String staCode = s.getStation().getStaCode(); String chan = ce.getChannel().getCode(); String loc = ce.getChannel().getLocation(); DateTime start = ce.getStarted(); DateTime end = ce.getEnded(); MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("net", netCode); params.addValue("sta", staCode); params.addValue("chan", chan); params.addValue("loc", loc); params.addValue("start", new Timestamp(start.getMillis())); params.addValue("end", new Timestamp(end.getMillis())); new NamedParameterJdbcTemplate(jdbcTemplate).query( SQL.responseSqlPoly, params, new ResultSetExtractor() { @Override public Object extractData(ResultSet rsip) throws SQLException, DataAccessException { try { if (rsip.next()) { if ((rsip.getInt(SQL.STAGE) == 0) && (rsip.getInt(SQL.BTYPE) == 62)) { doBlock62IP(rsip, input_units_n, input_units_d); } } } catch (IOException e) { throw new StationRetrievalException( e.getMessage()); } catch (XMLStreamException e) { throw new StationRetrievalException( e.getMessage()); } return null; } } ); } // Response Stages if (query.getLevel() > Query.CHANNEL_LEVEL) { StationEpoch s = (StationEpoch) ce.getChannel().getEpoch(); String netCode = s.getStation().getNetCode(); String staCode = s.getStation().getStaCode(); String chan = ce.getChannel().getCode(); String loc = ce.getChannel().getLocation(); DateTime start = ce.getStarted(); DateTime end = ce.getEnded(); MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("net", netCode); params.addValue("sta", staCode); params.addValue("chan", chan); params.addValue("loc", loc); params.addValue("start", new Timestamp(start.getMillis())); params.addValue("end", new Timestamp(end.getMillis())); new NamedParameterJdbcTemplate(jdbcTemplate).query( SQL.responseSql, params, new ResultSetExtractor() { @Override public Object extractData(ResultSet rs) throws SQLException, DataAccessException { try { doResponse(rs); } catch (IOException e) { throw new StationRetrievalException( e.getMessage()); } catch (XMLStreamException e) { throw new StationRetrievalException( e.getMessage()); } return null; } } ); } writer.endElement(); // ENDING RESPONSE writer.endElement(); // ENDING CHANNEL writer.flush(); } } } } } public void empty() throws IOException { this.q.clear(); } public void close() throws IOException { // make sure its empty print(); endDocument(); } private void startDocument() throws IOException { Map attributes = new HashMap(); attributes.put("xmlns", "http://www.fdsn.org/xml/station/1"); attributes.put("schemaVersion", "1.0"); attributes.put("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); if (query.getAvailability() == true) { attributes.put("xsi:schemaLocation", "http://www.fdsn.org/xml/station/1 http://www.fdsn.org/xml/station/fdsn-station_availability-1.0.xsd"); } else { attributes.put("xsi:schemaLocation", "http://www.fdsn.org/xml/station/1 http://www.fdsn.org/xml/station/fdsn-station-1.0.xsd"); } writer.startDocument(); writer.startElement(Schema.MESSAGE, attributes); writer.startElement("Source"); writer.value("NCEDC"); writer.endElement(); writer.startElement("Sender"); writer.value("NCEDC"); writer.endElement(); writer.startElement("Module"); writer.value("NCEDC WEB SERVICE: fdsnws-station | version: 1.1"); writer.endElement(); writer.startElement("ModuleURI"); writer.value(query.toString().toString()); writer.endElement(); DateTime now = new DateTime(); DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinuteSecond() .withZone(DateTimeZone.UTC); writer.startElement(Schema.CREATED); writer.value(now.toString(fmt)); writer.endElement(); } private void endDocument() throws IOException { if (query.getLevel() > Query.NETWORK_LEVEL) { writer.endElement(); // ENDING STATION } writer.endElement(); // ENDING NETWORK writer.endElement(); // ENDING FDSN writer.endDocument(); writer.flush(); } public boolean isResultStreamed() { return resultStreamed; } private void doResponse(ResultSet rs) throws SQLException, IOException, XMLStreamException { int currentStage; int stage = -1;// Do this to pass the first time int previouseSequence = -1; // boolean advanceCursor = true; String stageUnit = null; if (!rs.next()) { return; } do {// System.out.println("response1"); if (rs.isAfterLast()) { break; } int sequence = 10;// rs.getInt("CSEQ"); if (sequence < previouseSequence) { break; } else { previouseSequence = sequence; } stage = rs.getInt(SQL.STAGE); Map attributes = new HashMap(); attributes.put(Schema.STAGE, "" + stage); this.writer.startElement(Schema.RESPONSE, attributes); attributes = null; currentStage = stage; String units = rs.getString(SQL.RESPONSE_UNITS_IN_NAME); String tunits = rs.getString(SQL.RESPONSE_UNITS_IN_TEXT); if (stage == 1) { stageUnit = units; } if (stage == 0) { units = stageUnit; } do {// System.out.println("response2"); if (rs.isAfterLast() || currentStage != rs.getInt(SQL.STAGE)) { break; } int type = rs.getInt(SQL.BTYPE); switch (type) { case 53: doBlock53(rs); break; case 54: doBlock54(rs); break; case 55: doBlock55(rs); break; case 56: doBlock56(rs); break; case 57: doBlock57(rs); break; case 58: doBlock58(rs, units); break; case 61: doBlock61(rs); break; case 62: doBlock62(rs, units, tunits); break; default: if (!rs.isAfterLast()) { rs.next(); } break; } this.writer.flush(); } while (true); this.writer.endElement(); } while (true); } private void doBlock53(ResultSet rs) throws SQLException, IOException, XMLStreamException { int repeat = rs.getInt(SQL.REPEAT1) + rs.getInt(SQL.REPEAT2); if (repeat == 0) { if (!rs.isAfterLast()) { rs.next(); } return; } this.writer.startElement(Schema.POLES_ZEROS); if ((rs.getString(SQL.RESPONSE_UNITS_IN_NAME) != null) || (rs.getString(SQL.RESPONSE_UNITS_IN_TEXT) != null)) { writer.startElement(Schema.INPUT_UNITS); if (rs.getString(SQL.RESPONSE_UNITS_IN_NAME) != null) { writer.startElement("Name"); writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_NAME)); writer.endElement(); } if (rs.getString(SQL.RESPONSE_UNITS_IN_TEXT) != null) { writer.startElement("Description"); writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_TEXT)); writer.endElement(); } writer.endElement(); } if ((rs.getString(SQL.RESPONSE_UNITS_OUT_NAME) != null) || (rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT) != null)) { writer.startElement(Schema.OUTPUT_UNITS); if (rs.getString(SQL.RESPONSE_UNITS_OUT_NAME) != null) { writer.startElement("Name"); writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_NAME)); writer.endElement(); } if (rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT) != null) { writer.startElement("Description"); writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT)); writer.endElement(); } writer.endElement(); } this.writer.startElement("PzTransferFunctionType"); this.writer.value("LAPLACE (RADIANS/SECOND)"); this.writer.endElement(); this.writer.startElement(Schema.NORMALIZATION_FACTOR); this.writer.value(rs.getString(SQL.N_FACTOR)); this.writer.endElement(); this.writer.startElement(Schema.NORMALIZATION_FREQ); this.writer.value(rs.getString(SQL.N_FREQ)); this.writer.endElement(); int index = 0; List poles = new ArrayList(); int cnt = 0; do { if (index++ >= repeat) { break; } if (rs.getInt(SQL.BTYPE) != 53) { throw new StationParsingException("Error parsing B53:" + rs.getString(SQL.NETWORK) + ": " + rs.getString(SQL.STATION) + ": " + rs.getString(SQL.CHANNEL) + ": " + rs.getString(SQL.LOCATION) + ": " + rs.getString("STARTTIME") + ": " + rs.getString("ENDTIME") + ": " + repeat + ":" + +rs.getInt(SQL.BTYPE) + ":" + rs.getRow() + " round:" + index); } if ("z".equalsIgnoreCase(rs.getString(SQL.RTYPE))) { Map attributes = new HashMap(); attributes.put(Schema.NUMBER, "" + cnt++); this.writer.startElement(Schema.ZERO, attributes); attributes.clear(); attributes = null; this.writer.startElement(Schema.REAL); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); this.writer.startElement(Schema.IMAGINARY); this.writer.value(rs.getString(SQL.VALUE2)); this.writer.endElement(); this.writer.endElement(); } else if ("p".equalsIgnoreCase(rs.getString(SQL.RTYPE))) { poles.add(rs.getString(SQL.VALUE1)); poles.add(rs.getString(SQL.VALUE2)); } else { // TODO:should throw exception } } while (rs.next()); // Print poles if any cnt = 0; if (poles != null && !poles.isEmpty()) { for (int i = 0; i < poles.size(); i++) { Map attributes = new HashMap(); attributes.put(Schema.NUMBER, "" + cnt++); this.writer.startElement(Schema.POLE, attributes); attributes.clear(); attributes = null; this.writer.startElement(Schema.REAL); this.writer.value(poles.get(i)); this.writer.endElement(); this.writer.startElement(Schema.IMAGINARY); this.writer.value(poles.get(i + 1)); this.writer.endElement(); this.writer.endElement(); i++; } poles.clear(); } this.writer.endElement(); poles = null; } private void doBlock54(ResultSet rs) throws SQLException, IOException, XMLStreamException { if (rs.getInt(SQL.BTYPE) != 54) { throw new StationParsingException("Error parsing B54:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow()); } int repeat = rs.getInt(SQL.REPEAT1) + rs.getInt(SQL.REPEAT2); this.writer.startElement(Schema.COEFFICIENTS); if ((rs.getString(SQL.RESPONSE_UNITS_IN_NAME) != null) || (rs.getString(SQL.RESPONSE_UNITS_IN_TEXT) != null)) { writer.startElement(Schema.INPUT_UNITS); if (rs.getString(SQL.RESPONSE_UNITS_IN_NAME) != null) { writer.startElement("Name"); writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_NAME)); writer.endElement(); } if (rs.getString(SQL.RESPONSE_UNITS_IN_TEXT) != null) { writer.startElement("Description"); writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_TEXT)); writer.endElement(); } writer.endElement(); } if ((rs.getString(SQL.RESPONSE_UNITS_OUT_NAME) != null) || (rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT) != null)) { writer.startElement(Schema.OUTPUT_UNITS); if (rs.getString(SQL.RESPONSE_UNITS_OUT_NAME) != null) { writer.startElement("Name"); writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_NAME)); writer.endElement(); } if (rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT) != null) { writer.startElement("Description"); writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT)); writer.endElement(); } writer.endElement(); } writer.startElement("CfTransferFunctionType"); writer.value("DIGITAL"); writer.endElement(); int index = 0; do {// System.out.println(index+" "+rs.getRow()); // We do this to deal with blocks thats are split because they are // too long int btype = rs.getInt(SQL.BTYPE); if (index < repeat) { } else { if (btype == 54) { repeat = repeat + rs.getInt(SQL.REPEAT1); } else { break; } } if (btype != 54) { throw new StationParsingException("Error parsing B54:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + repeat + ":" + rs.getInt(SQL.BTYPE) + ":" + rs.getRow() + " round:" + index); } if ("n".equalsIgnoreCase(rs.getString(SQL.RTYPE))) { this.writer.startElement(Schema.NUMERATOR); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); } else if ("d".equalsIgnoreCase(rs.getString(SQL.RTYPE))) { this.writer.startElement(Schema.DENOMINATOR); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); } index++; } while (rs.next()); this.writer.endElement(); } private void doBlock55(ResultSet rs) throws SQLException, IOException, XMLStreamException { int repeat = rs.getInt(SQL.REPEAT1) + rs.getInt(SQL.REPEAT2); if (repeat == 0) { return; } this.writer.startElement(Schema.RESPONSE_LIST); int index = 0; do { if (index++ >= repeat) { break; } if (rs.getInt(SQL.BTYPE) != 55) { throw new StationParsingException("Error parsing B55:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow() + " round:" + index); } this.writer.startElement(Schema.RESPONSE_LIST_ELEMENT); this.writer.startElement(Schema.FREQUENCY); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); this.writer.startElement(Schema.AMPLITUDE); this.writer.value(rs.getString(SQL.VALUE2)); this.writer.endElement(); this.writer.startElement(Schema.PHASE); this.writer.value(rs.getString(SQL.VALUE4)); this.writer.endElement(); this.writer.endElement(); } while (rs.next()); this.writer.endElement(); } private void doBlock56(ResultSet rs) throws SQLException, IOException, XMLStreamException { this.writer.startElement(Schema.GENERICS); this.writer.startElement(Schema.INPUT_UNITS); this.writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_NAME)); this.writer.endElement(); this.writer.startElement(Schema.OUTPUT_UNITS); this.writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_NAME)); this.writer.endElement(); this.writer.startElement(Schema.SENSITIVITY); this.writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_NAME)); this.writer.endElement(); this.writer.startElement(Schema.FREE_FREQ); this.writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_NAME)); this.writer.endElement(); this.writer.startElement(Schema.HIGH_PASS); this.writer.startElement(Schema.PASS_TYPE); this.writer.startElement(Schema.CORNER_FREQ); this.writer.value((rs.getString(SQL.VALUE2))); this.writer.endElement(); this.writer.endElement(); this.writer.endElement(); this.writer.startElement(Schema.LOW_PASS); this.writer.startElement(Schema.PASS_TYPE); this.writer.startElement(Schema.CORNER_FREQ); this.writer.value((rs.getString(SQL.VALUE1))); this.writer.endElement(); this.writer.endElement(); this.writer.endElement(); this.writer.endElement(); this.writer.flush(); if (!rs.isAfterLast()) { rs.next(); } } private void doBlock57(ResultSet rs) throws SQLException, IOException, XMLStreamException { if (rs.getInt(SQL.BTYPE) != 57) { throw new StationParsingException("Error parsing B57:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow()); } this.writer.startElement(Schema.DECIMATION); this.writer.startElement(Schema.INPUT_SAMPLE_RATE); this.writer .value(Integer.toString(rs.getInt(SQL.RESPONSE_SAMPLE_RATE))); this.writer.endElement(); this.writer.startElement(Schema.FACTOR); this.writer.value(Integer.toString(rs.getInt(SQL.D_FACTOR))); this.writer.endElement(); this.writer.startElement(Schema.OFFSET); this.writer.value(Integer.toString(rs.getInt(SQL.D_OFFSET))); this.writer.endElement(); this.writer.startElement(Schema.DELAY); this.writer.value(Float.toString(rs.getFloat(SQL.EST_DELAY))); this.writer.endElement(); this.writer.startElement(Schema.CORRECTION); this.writer.value(Float.toString(rs.getFloat(SQL.CORRECTION))); this.writer.endElement(); this.writer.endElement(); this.writer.flush(); if (!rs.isAfterLast()) { rs.next(); } } private void doBlock58(ResultSet rs, String units) throws SQLException, IOException, XMLStreamException { if (rs.getInt(SQL.BTYPE) != 58) { throw new StationParsingException("Error parsing B57:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow()); } this.writer.startElement(Schema.STAGE_SENSITIVITY); this.writer.startElement(Schema.SENSITIVITY_VALUE); this.writer.value(rs.getString(SQL.SENS_GAIN)); this.writer.endElement(); // this.writer.startElement(Schema.GAIN_UNITS); // this.writer.value(units); // this.writer.endElement(); this.writer.startElement(Schema.FREQUENCY); this.writer.value(rs.getString(SQL.FREQUENCY)); this.writer.endElement(); this.writer.endElement(); this.writer.flush(); if (!rs.isAfterLast()) { rs.next(); } } private void doBlock61(ResultSet rs) throws SQLException, IOException, XMLStreamException { int repeat = rs.getInt(SQL.REPEAT1); if (repeat == 0) { return; } Map attributes = new HashMap(); if (rs.getString(SQL.NAME) != null) { attributes.put("name", rs.getString(SQL.NAME).replace(".", "_")); } this.writer.startElement(Schema.FIR, attributes); if ((rs.getString(SQL.RESPONSE_UNITS_IN_NAME) != null) || (rs.getString(SQL.RESPONSE_UNITS_IN_TEXT) != null)) { writer.startElement(Schema.INPUT_UNITS); if (rs.getString(SQL.RESPONSE_UNITS_IN_NAME) != null) { writer.startElement("Name"); writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_NAME)); writer.endElement(); } if (rs.getString(SQL.RESPONSE_UNITS_IN_TEXT) != null) { writer.startElement("Description"); writer.value(rs.getString(SQL.RESPONSE_UNITS_IN_TEXT)); writer.endElement(); } writer.endElement(); } if ((rs.getString(SQL.RESPONSE_UNITS_OUT_NAME) != null) || (rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT) != null)) { writer.startElement(Schema.OUTPUT_UNITS); if (rs.getString(SQL.RESPONSE_UNITS_OUT_NAME) != null) { writer.startElement("Name"); writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_NAME)); writer.endElement(); } if (rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT) != null) { writer.startElement("Description"); writer.value(rs.getString(SQL.RESPONSE_UNITS_OUT_TEXT)); writer.endElement(); } writer.endElement(); } this.writer.startElement(Schema.SYMMETRY); String symetry = rs.getString(SQL.SYMMETRY); if ("e".equalsIgnoreCase(symetry)) { this.writer.value(Schema.SYMMETRY_EVEN); } else if ("o".equalsIgnoreCase(symetry)) { this.writer.value(Schema.SYMMETRY_ODD); } else { this.writer.value(Schema.SYMMETRY_NONE); } symetry = null; this.writer.endElement(); int index = 0; do { if (index++ >= repeat) { break; } if (rs.getInt(SQL.BTYPE) != 61) { throw new StationParsingException("Error parsing B61:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow() + " round:" + index); } Map attributes2 = new HashMap(); attributes2.put("i", "" + (index-1)); this.writer.startElement(Schema.NUMERATOR_COEFFICIENT, attributes2); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); } while (rs.next()); this.writer.endElement(); this.writer.flush(); } private void doBlock62(ResultSet rs, String units, String tunits) throws SQLException, IOException, XMLStreamException { // Polynomial int repeat = rs.getInt(SQL.REPEAT1) + rs.getInt(SQL.REPEAT2); if (repeat == 0) { return; } this.writer.startElement(Schema.POLYNOMIAL); // Units if (units != null) { writer.startElement(Schema.INPUT_UNITS); writer.startElement("Name"); writer.value(units); writer.endElement(); if (tunits != null) { writer.startElement("Description"); writer.value(tunits); writer.endElement(); } writer.endElement(); } writer.startElement(Schema.OUTPUT_UNITS); writer.startElement("Name"); writer.value("V"); writer.endElement(); writer.startElement("Description"); writer.value("Volts"); writer.endElement(); writer.endElement(); String type = rs.getString(SQL.SYMMETRY); this.writer.startElement(Schema.APPROXIMATION_TYPE); if ("m".equalsIgnoreCase(type)) { this.writer.value(Schema.APPROXIMATION_TYPE_DEFAULT); } else { this.writer.value("UNKNOWN");// This will not validate } this.writer.endElement(); type = null; this.writer.startElement(Schema.FREQ_LOWER_BOUND); // this.writer.value((rs.getString(SQL.VALUE1))); this.writer.value(0); this.writer.endElement(); this.writer.startElement(Schema.FREQ_UPPER_BOUND); this.writer.value((rs.getString(SQL.VALUE2))); this.writer.endElement(); this.writer.startElement(Schema.APPROX_LOWER_BOUND); this.writer.value((rs.getString(SQL.VALUE3))); this.writer.endElement(); this.writer.startElement(Schema.APPROX_UPPER_BOUND); this.writer.value((rs.getString(SQL.VALUE4))); this.writer.endElement(); this.writer.startElement(Schema.MAX_ERROR); this.writer.value((rs.getString(SQL.VALUE5))); this.writer.endElement(); // if (!rs.next()) { // return; // } int index = 0; do { if (index++ >= repeat) { break; } if (rs.getInt(SQL.BTYPE) != 62) { throw new StationParsingException("Error parsing B62:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow() + " round:" + index); } Map attributes = new HashMap(); attributes.put("number", "" + (index-1)); this.writer.startElement(Schema.COEFFICIENT, attributes); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); attributes = null; } while (rs.next()); this.writer.endElement(); this.writer.flush(); } private void doBlock62IP(ResultSet rs, String units, String tunits) throws SQLException, IOException, XMLStreamException { // InstrumentPolynomial int repeat = rs.getInt(SQL.REPEAT1) + rs.getInt(SQL.REPEAT2); if (repeat == 0) { return; } this.writer.startElement("InstrumentPolynomial"); // Units if (units != null) { writer.startElement(Schema.INPUT_UNITS); writer.startElement("Name"); writer.value(units); writer.endElement(); if (tunits != null) { writer.startElement("Description"); writer.value(tunits); writer.endElement(); } writer.endElement(); } writer.startElement(Schema.OUTPUT_UNITS); writer.startElement("Name"); writer.value("COUNTS"); writer.endElement(); writer.startElement("Description"); writer.value("Digital Counts"); writer.endElement(); writer.endElement(); String type = rs.getString(SQL.SYMMETRY); this.writer.startElement(Schema.APPROXIMATION_TYPE); if ("m".equalsIgnoreCase(type)) { this.writer.value(Schema.APPROXIMATION_TYPE_DEFAULT); } else { this.writer.value("UNKNOWN");// This will not validate } this.writer.endElement(); type = null; this.writer.startElement(Schema.FREQ_LOWER_BOUND); // this.writer.value((rs.getString(SQL.VALUE1))); this.writer.value(0); this.writer.endElement(); this.writer.startElement(Schema.FREQ_UPPER_BOUND); this.writer.value((rs.getString(SQL.VALUE2))); this.writer.endElement(); this.writer.startElement(Schema.APPROX_LOWER_BOUND); this.writer.value((rs.getString(SQL.VALUE3))); this.writer.endElement(); this.writer.startElement(Schema.APPROX_UPPER_BOUND); this.writer.value((rs.getString(SQL.VALUE4))); this.writer.endElement(); this.writer.startElement(Schema.MAX_ERROR); this.writer.value((rs.getString(SQL.VALUE5))); this.writer.endElement(); // if (!rs.next()) { // return; // } int index = 0; do { if (index++ >= repeat) { break; } if (rs.getInt(SQL.BTYPE) != 62) { throw new StationParsingException("Error parsing B62:" + rs.getString(SQL.NETWORK) + " : " + rs.getString(SQL.STATION) + " : " + rs.getString(SQL.CHANNEL) + " : " + rs.getString(SQL.LOCATION) + " : " + rs.getString("STARTTIME") + " : " + rs.getString("ENDTIME") + " : " + rs.getInt(SQL.BTYPE) + ":" + rs.getRow() + " round:" + index); } Map attributes = new HashMap(); attributes.put("number", "" + (index-1)); this.writer.startElement(Schema.COEFFICIENT, attributes); this.writer.value(rs.getString(SQL.VALUE1)); this.writer.endElement(); attributes = null; } while (rs.next()); this.writer.endElement(); this.writer.flush(); } }