Skip to content

Commit f9f5d2b

Browse files
author
Kevin Milner
committed
utility for plotting mean map combinations of branch choices
1 parent ace1eca commit f9f5d2b

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package scratch.kevin.nshm23.uncertCorrFigures;
2+
3+
import java.awt.Color;
4+
import java.io.BufferedReader;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.StringTokenizer;
11+
import java.util.zip.ZipEntry;
12+
import java.util.zip.ZipFile;
13+
14+
import org.opensha.commons.data.xyz.GriddedGeoDataSet;
15+
import org.opensha.commons.geo.GriddedRegion;
16+
import org.opensha.commons.geo.Location;
17+
import org.opensha.commons.geo.LocationUtils;
18+
import org.opensha.commons.gui.plot.GeographicMapMaker;
19+
import org.opensha.commons.gui.plot.PlotCurveCharacterstics;
20+
import org.opensha.commons.gui.plot.PlotLineType;
21+
import org.opensha.commons.logicTree.LogicTree;
22+
import org.opensha.commons.logicTree.LogicTreeBranch;
23+
import org.opensha.commons.logicTree.LogicTreeNode;
24+
import org.opensha.commons.mapping.gmt.elements.GMT_CPT_Files;
25+
import org.opensha.commons.util.cpt.CPT;
26+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_FaultModels;
27+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.NSHM23_SegmentationModels;
28+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.logicTree.SupraSeisBValues;
29+
import org.opensha.sha.earthquake.rupForecastImpl.nshm23.util.NSHM23_RegionLoader;
30+
31+
import com.google.common.base.Preconditions;
32+
33+
public class MultiBranchAverageHazardPlotter {
34+
35+
public static void main(String[] args) throws IOException {
36+
System.setProperty("java.awt.headless", "true");
37+
File dir = new File("/project/scec_608/kmilner/nshm23/batch_inversions/2024_02_02-nshm23_branches-WUS_FM_v3");
38+
LogicTree<?> tree = LogicTree.read(new File(dir, "logic_tree_full_gridded.json"));
39+
File resultsFile = new File(dir, "results_hazard_full_gridded.zip");
40+
41+
if (args.length == 1)
42+
tree = tree.sample(Integer.parseInt(args[0]), true);
43+
44+
File plotsDir = new File(dir, "misc_plots");
45+
Preconditions.checkState(plotsDir.exists() || plotsDir.mkdir());
46+
File outputDir = new File(plotsDir, "branch_combination_hazard");
47+
Preconditions.checkState(outputDir.exists() || outputDir.mkdir());
48+
49+
String mapFileName = "map_pga_TWO_IN_50.txt";
50+
String mapLabel = "PGA, 2% in 50 Year";
51+
52+
GriddedRegion gridReg = new GriddedRegion(NSHM23_RegionLoader.loadFullConterminousWUS(), 0.1, GriddedRegion.ANCHOR_0_0);
53+
54+
List<LogicTreeNode[]> combinations = new ArrayList<>();
55+
56+
combinations.add(new LogicTreeNode[] { SupraSeisBValues.B_0p0 });
57+
combinations.add(new LogicTreeNode[] { NSHM23_SegmentationModels.NONE });
58+
combinations.add(new LogicTreeNode[] { SupraSeisBValues.B_0p0, NSHM23_SegmentationModels.NONE });
59+
combinations.add(new LogicTreeNode[] { SupraSeisBValues.B_1p0 });
60+
combinations.add(new LogicTreeNode[] { NSHM23_SegmentationModels.CLASSIC });
61+
combinations.add(new LogicTreeNode[] { SupraSeisBValues.B_1p0, NSHM23_SegmentationModels.CLASSIC });
62+
63+
double[] mean = new double[gridReg.getNodeCount()];
64+
double totWeight = 0d;
65+
66+
double[] combWeights = new double[combinations.size()];
67+
double[][] combMeans = new double[combinations.size()][gridReg.getNodeCount()];
68+
69+
ZipFile zip = new ZipFile(resultsFile);
70+
71+
for (int b=0; b<tree.size(); b++) {
72+
LogicTreeBranch<?> branch = tree.getBranch(b);
73+
if (b % 1000 == 0 || (tree.size() <= 1000 && b % 100 == 0))
74+
System.out.println("Loading branch "+b+"/"+tree.size());
75+
String fName = branch.buildFileName()+"/"+mapFileName;
76+
77+
ZipEntry entry = zip.getEntry(fName);
78+
Preconditions.checkNotNull(entry, "Entry is null: %s", fName);
79+
BufferedReader bRead = new BufferedReader(new InputStreamReader(zip.getInputStream(entry)));
80+
81+
GriddedGeoDataSet xyz = readMapReader(bRead, gridReg);
82+
83+
double weight = tree.getBranchWeight(branch);
84+
totWeight += weight;
85+
86+
for (int i=0; i<mean.length; i++)
87+
if (Double.isFinite(xyz.get(i)))
88+
mean[i] += weight*xyz.get(i);
89+
90+
for (int c=0; c<combinations.size(); c++) {
91+
boolean match = true;
92+
for (LogicTreeNode node : combinations.get(c)) {
93+
if (!branch.hasValue(node)) {
94+
match = false;
95+
break;
96+
}
97+
}
98+
if (match) {
99+
combWeights[c] += weight;
100+
for (int i=0; i<mean.length; i++)
101+
if (Double.isFinite(xyz.get(i)))
102+
combMeans[c][i] += weight*xyz.get(i);
103+
}
104+
}
105+
106+
bRead.close();
107+
}
108+
109+
zip.close();
110+
111+
scale(mean, 1d/totWeight);
112+
for (int c=0; c<combinations.size(); c++)
113+
scale(combMeans[c], 1d/combWeights[c]);
114+
115+
GeographicMapMaker mapMaker = new GeographicMapMaker(gridReg);
116+
Color outlineColor = new Color(0, 0, 0, 180);
117+
Color faultColor = new Color(0, 0, 0, 100);
118+
119+
mapMaker.setFaultSections(NSHM23_FaultModels.WUS_FM_v3.getFaultSections());
120+
121+
float outlineWidth = 2f;
122+
mapMaker.setRegionOutlineChar(new PlotCurveCharacterstics(PlotLineType.DASHED, 2f, outlineColor));
123+
mapMaker.setPoliticalBoundaryChar(new PlotCurveCharacterstics(PlotLineType.SOLID, outlineWidth, outlineColor));
124+
mapMaker.setSectTraceChar(new PlotCurveCharacterstics(PlotLineType.SOLID, 1f, faultColor));
125+
mapMaker.setSectOutlineChar(null);
126+
127+
CPT pDiffCPT = GMT_CPT_Files.DIVERGING_VIK_UNIFORM.instance().rescale(-50d, 50d);
128+
129+
for (int c=0; c<combinations.size(); c++) {
130+
GriddedGeoDataSet pDiff = new GriddedGeoDataSet(gridReg);
131+
132+
for (int i=0; i<pDiff.size(); i++) {
133+
double ref = mean[i];
134+
double test = combMeans[c][i];
135+
if (Double.isFinite(ref) && Double.isFinite(test) && ref > 0d && test > 0d) {
136+
pDiff.set(i, 100d*(test - ref)/ref);
137+
} else {
138+
pDiff.set(i, Double.NaN);
139+
}
140+
}
141+
142+
String name = "";
143+
String prefix = "";
144+
for (LogicTreeNode node : combinations.get(c)) {
145+
if (!name.isBlank()) {
146+
name += ", ";
147+
prefix += "_";
148+
}
149+
name += node.getShortName();
150+
prefix += node.getFilePrefix();
151+
}
152+
System.out.println("Writing for "+name);
153+
mapMaker.plotXYZData(pDiff, pDiffCPT, name+" / Mean, % Change, "+mapLabel);
154+
mapMaker.setWriteGeoJSON(false);
155+
mapMaker.plot(outputDir, prefix, " ");
156+
}
157+
}
158+
159+
private static GriddedGeoDataSet readMapReader(BufferedReader bRead, GriddedRegion gridReg) throws IOException {
160+
GriddedGeoDataSet xyz = new GriddedGeoDataSet(gridReg, false);
161+
String line = bRead.readLine();
162+
int index = 0;
163+
while (line != null) {
164+
line = line.trim();
165+
if (!line.startsWith("#")) {
166+
StringTokenizer tok = new StringTokenizer(line);
167+
double lon = Double.parseDouble(tok.nextToken());
168+
double lat = Double.parseDouble(tok.nextToken());
169+
double val = Double.parseDouble(tok.nextToken());
170+
Location loc = new Location(lat, lon);
171+
Preconditions.checkState(LocationUtils.areSimilar(loc, gridReg.getLocation(index)));
172+
xyz.set(index++, val);
173+
}
174+
line = bRead.readLine();
175+
}
176+
Preconditions.checkState(index == gridReg.getNodeCount());
177+
return xyz;
178+
}
179+
180+
private static void scale(double[] map, double scalar) {
181+
for (int i=0; i<map.length; i++)
182+
map[i] *= scalar;
183+
}
184+
}

0 commit comments

Comments
 (0)