From 5b669468e8b661141ffbf6e9358fade67c33a151 Mon Sep 17 00:00:00 2001
From: Peter Powers <pmpowers@usgs.gov>
Date: Wed, 16 Feb 2022 13:35:45 -0700
Subject: [PATCH] export updates

---
 .../earthquake/nshmp/calc/EqRateExport.java   |  3 +-
 .../earthquake/nshmp/calc/HazardExport.java   | 28 ++++++++++++-------
 .../earthquake/nshmp/model/LoaderTests.java   |  2 +-
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/main/java/gov/usgs/earthquake/nshmp/calc/EqRateExport.java b/src/main/java/gov/usgs/earthquake/nshmp/calc/EqRateExport.java
index 1c1c4159..3629306b 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/calc/EqRateExport.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/calc/EqRateExport.java
@@ -7,6 +7,7 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
+import java.util.OptionalDouble;
 import java.util.function.Function;
 import java.util.logging.Logger;
 import java.util.stream.Collectors;
@@ -63,7 +64,7 @@ public final class EqRateExport {
     boolean rates = config.rate.valueFormat == ValueFormat.ANNUAL_RATE;
 
     this.log = log;
-    this.dir = HazardExport.createOutputDir(config.output.directory);
+    this.dir = HazardExport.createOutputDir(config.output.directory, OptionalDouble.empty());
     this.file = rates ? RATE_FILE : PROB_FILE;
     this.valueFormat = rates ? RATE_FORMAT : PROB_FORMAT;
     this.model = model;
diff --git a/src/main/java/gov/usgs/earthquake/nshmp/calc/HazardExport.java b/src/main/java/gov/usgs/earthquake/nshmp/calc/HazardExport.java
index 4e526fde..451012ec 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/calc/HazardExport.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/calc/HazardExport.java
@@ -20,6 +20,7 @@ import java.util.EnumSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.OptionalDouble;
 import java.util.Set;
 import java.util.function.Function;
 import java.util.stream.IntStream;
@@ -80,9 +81,10 @@ public final class HazardExport {
   private HazardExport(
       HazardModel model,
       CalcConfig config,
-      List<Site> sites) throws IOException {
+      List<Site> sites,
+      OptionalDouble vs30) throws IOException {
 
-    this.dir = createOutputDir(config.output.directory);
+    this.dir = createOutputDir(config.output.directory, vs30);
     this.model = model;
     this.config = config;
     this.exportGmm = config.output.dataTypes.contains(DataType.GMM);
@@ -105,14 +107,16 @@ public final class HazardExport {
    * @param model being run
    * @param config that specifies output options and formats
    * @param sites reference to the sites to be processed (not retained)
+   * @param vs30 optional vs30 value used when running multiple site classes
    * @param log shared logging instance from calling class
    */
   public static HazardExport create(
       HazardModel model,
       CalcConfig config,
-      List<Site> sites) throws IOException {
+      List<Site> sites,
+      OptionalDouble vs30) throws IOException {
 
-    return new HazardExport(model, config, sites);
+    return new HazardExport(model, config, sites, vs30);
   }
 
   /* Initialize output directories. */
@@ -163,15 +167,19 @@ public final class HazardExport {
   }
 
   /* Avoid clobbering exsting result directories via incrementing. */
-  static Path createOutputDir(Path dir) throws IOException {
+  static Path createOutputDir(Path dir, OptionalDouble vs30) throws IOException {
     int i = 1;
-    Path incrementedDir = dir;
-    while (Files.exists(incrementedDir)) {
-      incrementedDir = incrementedDir.resolveSibling(dir.getFileName() + "-" + i);
+    Path outDir = dir;
+    while (Files.exists(outDir)) {
+      outDir = outDir.resolveSibling(dir.getFileName() + "-" + i);
       i++;
     }
-    Files.createDirectories(incrementedDir);
-    return incrementedDir;
+    if (vs30.isPresent()) {
+      String vs30dir = "vs30-" + ((int) vs30.getAsDouble());
+      outDir = outDir.resolve(vs30dir);
+    }
+    Files.createDirectories(outDir);
+    return outDir;
   }
 
   /**
diff --git a/src/test/java/gov/usgs/earthquake/nshmp/model/LoaderTests.java b/src/test/java/gov/usgs/earthquake/nshmp/model/LoaderTests.java
index f065ddf6..d7518714 100644
--- a/src/test/java/gov/usgs/earthquake/nshmp/model/LoaderTests.java
+++ b/src/test/java/gov/usgs/earthquake/nshmp/model/LoaderTests.java
@@ -214,7 +214,7 @@ class LoaderTests {
       List<Site> sites) throws IOException {
 
     ExecutorService exec = initExecutor(config.performance.threadCount);
-    HazardExport handler = HazardExport.create(model, config, sites);
+    HazardExport handler = HazardExport.create(model, config, sites, OptionalDouble.empty());
     for (Site site : sites) {
       Hazard hazard = HazardCalcs.hazard(model, config, site, exec);
       handler.write(hazard);
-- 
GitLab