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("MMM dd yyyy HH:mm:ss zzz");
  25.   /** Format for process times */
  26.   public static final SimpleDateFormat PROCESS_TIMESTAMP_FORMAT = new SimpleDateFormat(
  27.       "'(Process time: 'EEE MMM dd HH:mm:ss yyyy')'");

  28.   private ShakeMap shakemap;

  29.   /**
  30.    * Constructor
  31.    *
  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(new ZipInputStream(in)));
  54.       String firstLine = br.readLine();
  55.       br.close();

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

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

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

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

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

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

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

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

  109. }