DYFIProduct.java

  1. package gov.usgs.earthquake.dyfi;

  2. import gov.usgs.earthquake.product.Content;
  3. import gov.usgs.earthquake.product.Product;

  4. import java.io.InputStream;
  5. import java.math.BigDecimal;

  6. /**
  7.  * DYFIProduct object to add additional Product properties based on contents.
  8.  *
  9.  * This subclass of Product provides access to additional DYFI-specific
  10.  * attributes and loads these attributes, as well as additional Product
  11.  * attributes from ShakeMap source XML files.
  12.  */
  13. public class DYFIProduct extends Product {

  14.     /** References the event_data.xml file in the Product */
  15.     public static final String DYFI_EVENT_XML_ATTACHMENT = "event_data.xml";

  16.     /** Attribute for number of responses. Read from XML */
  17.     public static final String DYFI_NUM_RESP_ATTRIBUTE = "nresponses";
  18.     /** Attribute for max intensity. Read from XML */
  19.     public static final String DYFI_MAX_MMI_ATTRIBUTE = "max_intensity";

  20.     /** Internally set number of responses property */
  21.     public static final String DYFI_NUM_RESP_PROPERTY = "numResp";
  22.     /** Internally set max intensity property */
  23.     public static final String DYFI_MAX_MMI_PROPERTY = "maxmmi";

  24.     /**
  25.      * Creates a new DYFIProduct using the given <code>Product</code> as a
  26.      * basis. The given product must have a
  27.      * <code>DYFI_EVENT_XML_ATTACHMENT</code> in order to successfully create a
  28.      * DYFIProduct.
  29.      *
  30.      * @param product
  31.      *            The product serving as a basis for this instance.
  32.      */
  33.     public DYFIProduct(final Product product) {
  34.         super(product);

  35.         if (product.getProperties().get(DYFI_NUM_RESP_PROPERTY) == null
  36.                 || product.getProperties().get(DYFI_MAX_MMI_PROPERTY) == null) {
  37.             // only load xml if properties not set
  38.             loadEventXml();
  39.         }
  40.     }

  41.     /** Reads in DYFI Event EXL and parses it into a EventDataXMLHandler */
  42.     protected void loadEventXml() {
  43.         // Parse event_data.xml for more information about product
  44.         Content source = getContents().get(DYFI_EVENT_XML_ATTACHMENT);
  45.         if (source == null) {
  46.             throw new IllegalArgumentException("Given product lacked the "
  47.                     + "required file to be a DYFIProduct. ("
  48.                     + DYFI_EVENT_XML_ATTACHMENT + ")");
  49.         }

  50.         EventDataXMLHandler handler = new EventDataXMLHandler(this);
  51.         try (final InputStream in = source.getInputStream()) {
  52.             handler.parse(in);
  53.         } catch (Exception e) {
  54.             throw new IllegalArgumentException(e);
  55.         }
  56.     }

  57.     /**
  58.      * @return The number of felt reports submitted for this event.
  59.      */
  60.     public int getNumResponses() {
  61.         return Integer.parseInt(getProperties().get(DYFI_NUM_RESP_PROPERTY));
  62.     }

  63.     /**
  64.      * Set the number of felt reports submitted for this event.
  65.      *
  66.      * @param numResponses
  67.      *            The new number of submitted felt reports.
  68.      * @throws NumberFormatException
  69.      *             If the given <code>numResponses</code> could not be parsed
  70.      *             into an integer.
  71.      */
  72.     public void setNumResponses(final String numResponses)
  73.             throws NumberFormatException {
  74.         // This is a throw-away line, but makes sure the value being passed
  75.         // at least makes sense; an exception is thrown for garbage input.
  76.         Integer.parseInt(numResponses);

  77.         getProperties().put(DYFI_NUM_RESP_PROPERTY, numResponses);
  78.     }

  79.     /**
  80.      * @return The maximum reported intensity for this event.
  81.      */
  82.     public BigDecimal getMaxMMI() {
  83.         return new BigDecimal(getProperties().get(DYFI_MAX_MMI_PROPERTY));
  84.     }

  85.     /**
  86.      * Sets the maximum reported intensity for this event.
  87.      *
  88.      * @param maxMMI
  89.      *            The new maximum reported intensity.
  90.      * @throws NumberFormatException
  91.      *             If the given <code>maxMMI</code> could not be parsed
  92.      *             into a double.
  93.      */
  94.     public void setMaxMMI(final String maxMMI) throws NumberFormatException {
  95.         // This is a throw-away line, but makes sure the value being passed
  96.         // at least makes sense; an exception is thrown for garbage input.
  97.         Double.parseDouble(maxMMI);

  98.         getProperties().put(DYFI_MAX_MMI_PROPERTY, maxMMI);
  99.     }
  100. }