Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Milner committed Aug 21, 2024
1 parent da7b716 commit 1919138
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 16 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/opensha/sha/cybershake/HazardCurveFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class HazardCurveFetcher {

DBAccess db;
HazardCurve2DB curve2db;
HazardCurve2DB curvePointsDB;
CybershakeSiteInfo2DB site2db;

List<Integer> curveIDs;
Expand Down Expand Up @@ -64,7 +65,11 @@ public HazardCurveFetcher(DBAccess db, int erfID, int rupVarScenarioID, int sgtV
}

public HazardCurveFetcher(DBAccess db, List<CybershakeRun> runs, int[] datasetIDs, int imTypeID) {
this.initDBConnections(db);
this(db, runs, datasetIDs, imTypeID, null);
}

public HazardCurveFetcher(DBAccess db, List<CybershakeRun> runs, int[] datasetIDs, int imTypeID, DBAccess customCurvesDB) {
this.initDBConnections(db, customCurvesDB);
List<Integer> curveIDs = new ArrayList<>();
for (int datasetID : datasetIDs) {
for (CybershakeRun run : runs) {
Expand Down Expand Up @@ -100,7 +105,7 @@ private void init(List<Integer> curveIDs, int imTypeID) {
siteIDs.add(siteID);
}
sites.add(site2db.getSiteFromDB(siteID));
DiscretizedFunc curve = curve2db.getHazardCurve(id);
DiscretizedFunc curve = curvePointsDB.getHazardCurve(id);
Preconditions.checkNotNull(curve, "Curve is null? Curve ID=%s, site ID=%s", id, siteID);
funcs.add(curve);
runIDs.add(curve2db.getRunIDForCurve(id));
Expand Down Expand Up @@ -142,8 +147,16 @@ public List<DiscretizedFunc> getCurvesForSite(int siteID) {
}

private void initDBConnections(DBAccess db) {
initDBConnections(db, null);
}

private void initDBConnections(DBAccess db, DBAccess customCurvesDB) {
this.db = db;
curve2db = new HazardCurve2DB(db);
if (customCurvesDB == null)
curvePointsDB = curve2db;
else
curvePointsDB = new HazardCurve2DB(customCurvesDB);
site2db = new CybershakeSiteInfo2DB(db);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,23 @@ public RunIDFetcher runFetcher() {
// TODO require Vsitop?
return new RunIDFetcher(this.getDB()).noTestSites().unique(false)
.hasHazardCurves(this.getDatasetIDs()).forStatus(Status.VERIFIED);
// .hasHazardCurves(this.getDatasetIDs()).hasAmplitudes(null).hasHazardCurves();
}
},
STUDY_24_8_TESTS(cal(2024, 8), 121, "August 2024 Experiments", "study_24_8_tests",
"Test calculations",
64, 15,
new CaliforniaRegions.CYBERSHAKE_MAP_REGION(),
Cybershake_OpenSHA_DBApplication.PRODUCTION_HOST_NAME) {
@Override
public AbstractERF buildNewERF() {
return MeanUCERF2_ToDB.createUCERF2ERF();
}
@Override
public RunIDFetcher runFetcher() {
// TODO require Vsitop?
return new RunIDFetcher(this.getDB()).noTestSites().unique(false)
.hasHazardCurves(this.getDatasetIDs()).forStatus(Status.VERIFIED);
// .hasHazardCurves(this.getDatasetIDs()).hasAmplitudes(null).hasHazardCurves();
}
};
Expand Down Expand Up @@ -724,6 +741,11 @@ public GregorianCalendar getDate() {
return date;
}

public synchronized DBAccess getSQLiteDB(File sqLiteFile) throws IOException {
db = Cybershake_OpenSHA_DBApplication.getSQLiteDB(sqLiteFile);
return db;
}

public synchronized DBAccess getDB() {
if (db == null)
db = Cybershake_OpenSHA_DBApplication.getDB(dbHost);
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/opensha/sha/cybershake/db/HazardCurve2DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ private ArrayList<Integer> getAllHazardCurveIDs(String whereClause) {
}

try {
rs.next();
boolean valid = rs.next();
if (!valid)
return null;
while (!rs.isAfterLast()) {
int id = rs.getInt("Hazard_Curve_ID");
int id = rs.getInt(1);
boolean skip = false;
for (int oldID : ids) {
if (oldID == id) {
Expand Down Expand Up @@ -358,7 +360,7 @@ public int getSiteIDFromCurveID(int hcID) {
rs.next();
if (rs.isAfterLast())
return -1;
id = rs.getInt("Run_ID");
id = rs.getInt(1);
rs.close();
} catch (SQLException e) {
// e.printStackTrace();
Expand Down Expand Up @@ -425,8 +427,8 @@ public DiscretizedFunc getHazardCurve(int id) {
while (rs.next()) {
if (hazardFunc == null)
hazardFunc = new ArbitrarilyDiscretizedFunc();
double x = rs.getDouble("X_Value");
double y = rs.getDouble("Y_Value");
double x = rs.getDouble(1);
double y = rs.getDouble(2);
hazardFunc.set(x, y);
}
rs.close();
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/opensha/sha/cybershake/db/RunIDFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,22 @@ public RunIDFetcher unique(boolean useFirst) {
public List<CybershakeRun> fetch() {
String sql = buildSelectSQL();

System.out.println(sql);

List<CybershakeRun> runs = new ArrayList<>();

HashSet<Integer> runIDs = new HashSet<>();

// for some reason, sqlite joins won't prepend the R. column name
String prefix = db.isSQLite() ? "" : "R.";

ResultSet rs = null;
try {
ResultSet rs = db.selectData(sql);
rs = db.selectData(sql);
boolean valid = rs.next();

while (valid) {
CybershakeRun run = CybershakeRun.fromResultSet(rs, "R.");
CybershakeRun run = CybershakeRun.fromResultSet(rs, prefix);
Preconditions.checkState(!runIDs.contains(run.getRunID()), "Duplicate runID=%s. SQL:%s", run.getRunID(), sql);
runs.add(run);
runIDs.add(run.getRunID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.opensha.sha.cybershake.CyberShakeSiteBuilder;
import org.opensha.sha.cybershake.CyberShakeSiteBuilder.Vs30_Source;
import org.opensha.sha.cybershake.calc.HazardCurveComputation;
import org.opensha.sha.cybershake.calc.mcer.CyberShakeSiteRun;
import org.opensha.sha.cybershake.constants.CyberShakeStudy;
import org.opensha.sha.cybershake.db.CybershakeIM;
import org.opensha.sha.cybershake.db.CybershakeIM.CyberShakeComponent;
Expand Down Expand Up @@ -100,6 +101,8 @@ public class CyberShakeScenarioShakeMapGenerator {

private List<Site> gmpeSites;

private boolean calcGMPEatCS;

private static final double SP_CORR_SIGMA_DEFAULT = 0.6;
private int numRandomFields = 0;
private double spatialCorrSigma = SP_CORR_SIGMA_DEFAULT;
Expand Down Expand Up @@ -218,6 +221,8 @@ else if (periods[p] == -1)
} else {
region = study.getRegion();
}

calcGMPEatCS = cmd.hasOption("calc-gmpe-at-cs-sites");
}

private double[] cbLimitParse(String str) {
Expand All @@ -237,6 +242,12 @@ public void plot() throws SQLException, IOException, ClassNotFoundException, GMT

GeoDataSet[] gmpeXYZs = gmpe == null ? null : calcGMPE();

GeoDataSet[] gmpeXYZsAtCS = null;
if (calcGMPEatCS) {
Preconditions.checkState(gmpe != null, "GMPE is null but option to calculate GMPE at CS sites was selected?");
gmpeXYZsAtCS = calcGMPEatCS();
}

GMT_InterpolationSettings interpSettings = GMT_InterpolationSettings.getDefaultSettings();
InterpDiffMapType[] typesToPlot;
if (gmpe == null)
Expand Down Expand Up @@ -272,6 +283,9 @@ else if (periods[p] == -1d)
if (gmpe != null)
ArbDiscrGeoDataSet.writeXYZFile(gmpeXYZs[p], new File(outputDir, prefix+"_gmpe_amps.txt"));

if (gmpeXYZsAtCS != null)
ArbDiscrGeoDataSet.writeXYZFile(gmpeXYZsAtCS[p], new File(outputDir, prefix+"_gmpe_amps_at_cs_sites.txt"));

if (noPlot)
continue;

Expand Down Expand Up @@ -739,6 +753,40 @@ private GeoDataSet[] calcGMPE() throws IOException {
return xyz;
}

private GeoDataSet[] calcGMPEatCS() throws IOException {
System.out.println("Calculating GMPE at CS sites");

List<CyberShakeSiteRun> sites = CyberShakeSiteBuilder.buildSites(study, vsSource, runs);

GeoDataSet[] xyz = new GeoDataSet[periods.length];
for (int p=0; p<xyz.length; p++)
xyz[p] = new ArbDiscrGeoDataSet(false);

gmpe.setParamDefaults();
ProbEqkRupture rup = study.getERF().getSource(sourceID).getRupture(rupID);

gmpe.setEqkRupture(rup);

for (CyberShakeSiteRun site : sites) {
gmpe.setSite(site);
for (int p=0; p<periods.length; p++) {
if (periods[p] > 0) {
gmpe.setIntensityMeasure(SA_Param.NAME);
SA_Param saParam = (SA_Param)gmpe.getIntensityMeasure();
SA_Param.setPeriodInSA_Param(saParam, periods[p]);
} else if (periods[p] == 0d) {
gmpe.setIntensityMeasure(PGA_Param.NAME);
} else if (periods[p] == -1d) {
gmpe.setIntensityMeasure(PGV_Param.NAME);
}
double value = Math.exp(gmpe.getMean());
xyz[p].set(site.getLocation(), value);
}
}

return xyz;
}

private static String getStudyList() {
List<String> names = new ArrayList<>();
for (CyberShakeStudy study : CyberShakeStudy.values())
Expand Down Expand Up @@ -821,6 +869,8 @@ private static Options createOptions() {
gmpeSitesOp.setRequired(false);
ops.addOption(gmpeSitesOp);

ops.addOption(null, "calc-gmpe-at-cs-sites", false, "Optional flag to calculate and write out GMPE ground motions exactly at the CyberShake sites.");

Option noPlotOp = new Option("np", "no-plot", false, "Skip plotting and only write out data files");
noPlotOp.setRequired(false);
ops.addOption(noPlotOp);
Expand Down Expand Up @@ -911,7 +961,8 @@ public static void main(String[] args) {
// String argz = "--study STUDY_22_12_HF --source-id 231 --rupture-id 66 --rupture-var-id 41"
// + " --output-dir /home/kevin/CyberShake/caloes_shakemaps/palos-verdes"
String argz = "--study STUDY_22_12_HF --source-id 215 --rupture-id 35 --rupture-var-id 0"
+ " --output-dir /home/kevin/CyberShake/caloes_shakemaps/newport-inglewood"
// + " --output-dir /home/kevin/CyberShake/caloes_shakemaps/newport-inglewood"
+ " --output-dir /tmp/cs_test"
// + "-spatialVal --spatial-corr-fields 1"
// + " --spacing 0.01"
+ " --spacing 0.005"
Expand All @@ -921,7 +972,9 @@ public static void main(String[] args) {
// + " --period 0.01,0.3,1"
// + " --colorbar-max 0.5,1.0,0.7"
+ " --region "+studyRegFile.getAbsolutePath()
+ " --download-interpolated";
+ " --download-interpolated"
+ " --calc-gmpe-at-cs-sites"
;

// String argz = "--help";
args = argz.split(" ");
Expand Down
Loading

0 comments on commit 1919138

Please sign in to comment.