GridXYZHandler.java

  1. /**
  2.  * This class is currently not in use, but kept for posterity and the ability
  3.  * to add this support back in if it is deemed necessary.
  4.  */
  5. package gov.usgs.earthquake.shakemap;

  6. import java.util.zip.ZipInputStream;

  7. import gov.usgs.util.StreamUtils;

  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.BufferedReader;
  11. import java.math.BigDecimal;

  12. import java.text.SimpleDateFormat;

  13. /**
  14.  * Parser for ShakeMap grid.xyz metadata.
  15.  *
  16.  * id magnitude latitude longitude month day year hour:minute:second timezone
  17.  * lonMin latMin lonMax latMax (Process time: dow month day hour:minute:second
  18.  * year) eventDescription 2009232_290541 4.2 41.94 -114.09 AUG 20 2009 06:44:11
  19.  * GMT -115.327 41.0306 -112.844 42.8806 (Process time: Wed Aug 19 23:55:49
  20.  * 2009) 73.1 miles NE of WELLS-NV
  21.  */
  22. public class GridXYZHandler {

  23.     /** Format for event times */
  24.     public static final SimpleDateFormat EVENT_TIMESTAMP_FORMAT = new SimpleDateFormat(
  25.             "MMM dd yyyy HH:mm:ss zzz");
  26.     /** Format for process times */
  27.     public static final SimpleDateFormat PROCESS_TIMESTAMP_FORMAT = new SimpleDateFormat(
  28.             "'(Process time: 'EEE MMM dd HH:mm:ss yyyy')'");

  29.     private ShakeMap shakemap;

  30.     /**
  31.      * Constructor
  32.      * @param shakemap a shakemap
  33.      */
  34.     public GridXYZHandler(ShakeMap shakemap) {
  35.         this.shakemap = shakemap;
  36.     }

  37.     /** @return shakemap */
  38.     public ShakeMap getShakemap() {
  39.         return shakemap;
  40.     }

  41.     /** @param shakemap to set */
  42.     public void setShakemap(ShakeMap shakemap) {
  43.         this.shakemap = shakemap;
  44.     }

  45.     /**
  46.      * Read first line of grid.xyz file and set properties on ShakeMap object.
  47.      *
  48.      * @param in the grid.xyz input stream.
  49.      * @throws Exception if error occurs
  50.      */
  51.     public void parse(final InputStream in) throws Exception {
  52.         try {
  53.             BufferedReader br = new BufferedReader(new InputStreamReader(
  54.                     new ZipInputStream(in)));
  55.             String firstLine = br.readLine();
  56.             br.close();

  57.             String parts[] = firstLine.split(" ");
  58.             // id
  59.             shakemap.setEventSourceCode(parts[0]);

  60.             // magnitude
  61.             shakemap.setMagnitude(new BigDecimal(parts[1]));
  62.             // latitude
  63.             shakemap.setLatitude(new BigDecimal(parts[2]));
  64.             // longitude
  65.             shakemap.setLongitude(new BigDecimal(parts[3]));

  66.             // month day year hour:minute:second timezone
  67.             String[] eventTimestampParts = new String[5];
  68.             System.arraycopy(parts, 4, eventTimestampParts, 0, 5);
  69.             shakemap.setEventTime(EVENT_TIMESTAMP_FORMAT.parse(join(" ",
  70.                     eventTimestampParts)));

  71.             // lonMin
  72.             shakemap.setMinimumLongitude(new BigDecimal(parts[9]));
  73.             // latMin
  74.             shakemap.setMinimumLatitude(new BigDecimal(parts[10]));
  75.             // lonMax
  76.             shakemap.setMaximumLongitude(new BigDecimal(parts[11]));
  77.             // latMax
  78.             shakemap.setMaximumLatitude(new BigDecimal(parts[12]));

  79.             // (Process time: dow month day hour:minute:second year)
  80.             String[] processTimestampParts = new String[7];
  81.             System.arraycopy(parts, 13, processTimestampParts, 0, 7);
  82.             shakemap.setProcessTimestamp(PROCESS_TIMESTAMP_FORMAT.parse(join(" ",
  83.                     processTimestampParts)));

  84.             String eventDescription = "";
  85.             for (int i = 20; i < parts.length; i++) {
  86.                 eventDescription += parts[i] + " ";
  87.             }
  88.             shakemap.setEventDescription(eventDescription.trim());
  89.         } finally {
  90.             StreamUtils.closeStream(in);
  91.         }
  92.     }

  93.     /**
  94.      * Appends a string array of parts with a delimeter inbetween
  95.      * @param delimeter to add between parts
  96.      * @param parts string array to combine
  97.      * @return A string of delimited parts
  98.      */
  99.     protected String join(final String delimeter, final String[] parts) {
  100.         StringBuffer buf = new StringBuffer();
  101.         if (parts == null) {
  102.             return "";
  103.         }

  104.         buf.append(parts[0]);
  105.         for (int i = 1; i < parts.length; i++) {
  106.             buf.append(delimeter);
  107.             buf.append(parts[i]);
  108.         }
  109.         return buf.toString();
  110.     }

  111. }