From a9b46fb128aebd36813c1bd0ce32bf56b887b9d6 Mon Sep 17 00:00:00 2001
From: Peter Powers <pmpowers@usgs.gov>
Date: Fri, 2 Sep 2016 10:31:48 -0600
Subject: [PATCH] added system section stats

---
 src/org/opensha2/calc/DeaggContributor.java | 26 ++++++++++++++++++---
 src/org/opensha2/calc/DeaggDataset.java     |  5 +---
 src/org/opensha2/calc/Deaggregator.java     | 22 +++++++++++++----
 3 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/org/opensha2/calc/DeaggContributor.java b/src/org/opensha2/calc/DeaggContributor.java
index 19e6dbbdc..4c3cbda18 100644
--- a/src/org/opensha2/calc/DeaggContributor.java
+++ b/src/org/opensha2/calc/DeaggContributor.java
@@ -430,9 +430,13 @@ abstract class DeaggContributor {
   static final class SystemContributor extends DeaggContributor {
 
     final SectionSource section;
+    final Location location;
+    final double azimuth;
 
     SystemContributor(
         SectionSource section,
+        Location location,
+        double azimuth,
         double rate,
         double residual,
         double rScaled,
@@ -441,6 +445,8 @@ abstract class DeaggContributor {
 
       super(rate, residual, rScaled, mScaled, εScaled);
       this.section = section;
+      this.location = location;
+      this.azimuth = azimuth;
     }
 
     @Override
@@ -450,13 +456,20 @@ abstract class DeaggContributor {
 
     @Override
     StringBuilder appendTo(StringBuilder sb, double toPercent, String indent) {
-      double contribution = total() * toPercent;
+      double total = total();
+      double contribution = total * toPercent;
       if (contribution < CONTRIBUTOR_LIMIT) {
         return sb;
       }
+      double rBar = rScaled / total;
+      double mBar = mScaled / total;
+      double εBar = εScaled / total;
       sb.append(String.format(
           DeaggExport.CONTRIB_SOURCE_FMT,
-          indent + section.name(), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, contribution));
+          indent + section.name(),
+          rBar, mBar, εBar,
+          location.lon(), location.lat(), azimuth,
+          contribution));
       sb.append(NEWLINE);
       return sb;
     }
@@ -464,9 +477,13 @@ abstract class DeaggContributor {
     static final class Builder extends DeaggContributor.Builder {
 
       SectionSource section;
+      Location location;
+      double azimuth;
 
-      Builder section(SectionSource section) {
+      Builder section(SectionSource section, Location location, double azimuth) {
         this.section = section;
+        this.location = location;
+        this.azimuth = azimuth;
         return this;
       }
 
@@ -495,6 +512,8 @@ abstract class DeaggContributor {
       DeaggContributor build() {
         return new SystemContributor(
             section,
+            location,
+            azimuth,
             rate,
             residual,
             rScaled,
@@ -524,6 +543,7 @@ abstract class DeaggContributor {
 
     @Override
     public int size() {
+      //TODO perhaps this should return the number of participating ruptures
       return 1;
     }
 
diff --git a/src/org/opensha2/calc/DeaggDataset.java b/src/org/opensha2/calc/DeaggDataset.java
index 9ad7d650c..785bad0dd 100644
--- a/src/org/opensha2/calc/DeaggDataset.java
+++ b/src/org/opensha2/calc/DeaggDataset.java
@@ -452,9 +452,6 @@ final class DeaggDataset {
     }
   }
 
-  // TODO lat, lon, and az fields only need to be added from the
-  // first child added to any sourceMap
-
   /*
    * Specialized builder that combines datasets across Gmms for a single
    * SourceSet. The model supplied must be one of the datasets to be combined,
@@ -566,7 +563,7 @@ final class DeaggDataset {
 
       /* Put new. */
       DeaggContributor.Builder sourceContributor = new SystemContributor.Builder()
-          .section(sc.section)
+          .section(sc.section, sc.location, sc.azimuth)
           .add(sc.rate, sc.residual, sc.rScaled, sc.mScaled, sc.εScaled);
       childMap.put(sc.section, sourceContributor);
     }
diff --git a/src/org/opensha2/calc/Deaggregator.java b/src/org/opensha2/calc/Deaggregator.java
index aa7a2ea0c..143ecca3e 100644
--- a/src/org/opensha2/calc/Deaggregator.java
+++ b/src/org/opensha2/calc/Deaggregator.java
@@ -13,6 +13,7 @@ import org.opensha2.eq.model.ClusterSource;
 import org.opensha2.eq.model.GmmSet;
 import org.opensha2.eq.model.Source;
 import org.opensha2.eq.model.SourceSet;
+import org.opensha2.eq.model.SystemSourceSet;
 import org.opensha2.geo.Location;
 import org.opensha2.geo.Locations;
 import org.opensha2.gmm.Gmm;
@@ -245,7 +246,8 @@ final class Deaggregator {
 
     /*
      * Fetch site-specific source attributes so that they don't need to be
-     * recalculated multiple times downstream.
+     * recalculated multiple times downstream. Safe covariant cast assuming
+     * switch handles variants.
      */
     Source source = ((SourceInputList) inputs).parent;
     Location location = source.location(site.location);
@@ -254,7 +256,6 @@ final class Deaggregator {
     /* Add sources/contributors to builders. */
     for (Gmm gmm : gmmKeys) {
       double[] data = gmmData.get(gmm);
-      /* Safe covariant cast assuming switch handles variants. */
       DeaggContributor.Builder contributor = new SourceContributor.Builder()
           .source(source, location, azimuth)
           .add(data[0], data[1], data[2], data[3], data[4]);
@@ -294,6 +295,9 @@ final class Deaggregator {
 
   private Map<Gmm, DeaggDataset> processSystemSources() {
 
+    /* Safe covariant cast assuming switch handles variants. */
+    SystemSourceSet systemSources = (SystemSourceSet) sources;
+    
     Map<Gmm, DeaggDataset.Builder> builders = createBuilders(gmmSet.gmms(), model);
     for (DeaggDataset.Builder builder : builders.values()) {
       SourceSetContributor.Builder parent = new SourceSetContributor.Builder();
@@ -319,12 +323,22 @@ final class Deaggregator {
 
     for (int sectionIndex : inputs.sectionIndices) {
 
+      /*
+       * Fetch site-specific source attributes so that they don't need to be
+       * recalculated multiple times downstream. Safe covariant cast assuming
+       * switch handles variants.
+       */
+      SectionSource section = new SectionSource(sectionIndex);
+      Location location = Locations.closestPoint(
+          site.location,
+          systemSources.section(sectionIndex).getUpperEdge());
+      double azimuth = Locations.azimuth(site.location, location);
+
       /* Create system contributors for section and attach to parent. */
       Map<Gmm, SystemContributor.Builder> contributors = new EnumMap<>(Gmm.class);
-      SectionSource section = new SectionSource(sectionIndex);
       for (Gmm gmm : gmmKeys) {
         SystemContributor.Builder contributor = new SystemContributor.Builder()
-            .section(section);
+            .section(section, location, azimuth);
         contributors.put(gmm, contributor);
         builders.get(gmm).addChildContributor(contributor);
       }
-- 
GitLab