예제 #1
0
  /**
   * Creates a {@code SiteSet} from the comma-delimted site file designated by {@code path}.
   *
   * @param path to comma-delimited site data file
   * @throws IOException if problem encountered
   */
  static SiteSet fromCsv(Path path) throws IOException {
    checkNotNull(path);

    List<Site> siteList = new ArrayList<>();
    Builder builder = Site.builder();

    List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
    boolean firstline = true;
    List<Key> keyList = new ArrayList<>();
    for (String line : lines) {

      // skip comments
      if (line.startsWith("#")) continue;
      if (line.trim().isEmpty()) continue;

      List<String> values = Parsing.splitToList(line, Delimiter.COMMA);

      // set up key/column ordering
      if (firstline) {
        Set<Key> keys = EnumSet.of(NAME, LAT, LON, VS30, VS_INF, Z1P0, Z2P5);
        for (String keyStr : values) {
          Key key = Key.fromString(keyStr);
          checkState(keys.contains(key), "Illegal site property key [%s]", keyStr);
          keyList.add(key);
        }
        checkState(keyList.contains(LAT), "Site latitudes must be defined");
        checkState(keyList.contains(LON), "Site longitudes must be defined");
        firstline = false;
        continue;
      }

      int index = 0;
      double lat = 0.0;
      double lon = 0.0;
      for (Key key : keyList) {
        String value = values.get(index);
        switch (key) {
          case LAT:
            lat = Double.parseDouble(value);
            break;
          case LON:
            lon = Double.parseDouble(value);
            break;
          case NAME:
            builder.name(value);
            break;
          case VS30:
            builder.vs30(Double.parseDouble(value));
            break;
          case VS_INF:
            builder.vsInferred(Boolean.parseBoolean(value));
            break;
          case Z1P0:
            builder.z1p0(Double.parseDouble(value));
            break;
          case Z2P5:
            builder.z2p5(Double.parseDouble(value));
            break;
          default:
            throw new IllegalStateException("Unsupported site key: " + key);
        }
        index++;
      }
      builder.location(lat, lon);
      siteList.add(builder.build());
    }
    return new SiteSet(siteList);
  }
예제 #2
0
 @Override
 public final String toString() {
   return Parsing.enumLabelWithSpaces(this);
 }