static Optional<String> run(String[] args) { int argCount = args.length; if (argCount < 3 || argCount > 4) { return Optional.of(USAGE); } try { Logging.init(); Logger log = Logger.getLogger(DeaggCalc.class.getName()); Path tempLog = HazardCalc.createTempLog(); FileHandler fh = new FileHandler(tempLog.getFileName().toString()); fh.setFormatter(new Logging.ConsoleFormatter()); log.getParent().addHandler(fh); log.info(PROGRAM + ": initializing..."); Path modelPath = Paths.get(args[0]); HazardModel model = HazardModel.load(modelPath); CalcConfig config = model.config(); if (argCount == 4) { Path userConfigPath = Paths.get(args[3]); config = CalcConfig.Builder.copyOf(model.config()) .extend(CalcConfig.Builder.fromFile(userConfigPath)) .build(); } log.info(config.toString()); log.info(""); Sites sites = HazardCalc.readSites(args[1], config, log); log.info("Sites: " + sites); double returnPeriod = Double.valueOf(args[2]); Path out = calc(model, config, sites, returnPeriod, log); // transfer log and write config Files.move(tempLog, out.resolve(PROGRAM + ".log")); config.write(out); log.info(PROGRAM + ": finished"); return Optional.absent(); } catch (Exception e) { StringBuilder sb = new StringBuilder() .append(NEWLINE) .append(PROGRAM + ": error") .append(NEWLINE) .append(" Arguments: ") .append(Arrays.toString(args)) .append(NEWLINE) .append(NEWLINE) .append(Throwables.getStackTraceAsString(e)) .append(USAGE); return Optional.of(sb.toString()); } }
/* * Compute hazard curves using the supplied model, config, and sites. Method * returns the path to the directory where results were written. * * TODO consider refactoring to supply an Optional<Double> return period to * HazardCalc.calc() that will trigger deaggregations if the value is present. */ private static Path calc( HazardModel model, CalcConfig config, Sites sites, double returnPeriod, Logger log) throws IOException { ExecutorService execSvc = null; ThreadCount threadCount = config.performance.threadCount; if (threadCount != ThreadCount.ONE) { execSvc = newFixedThreadPool(threadCount.value()); log.info("Threads: " + ((ThreadPoolExecutor) execSvc).getCorePoolSize()); } else { log.info("Threads: Running on calling thread"); } Optional<Executor> executor = Optional.<Executor>fromNullable(execSvc); log.info(PROGRAM + ": calculating ..."); ResultHandler handler = ResultHandler.create(config, sites, log); for (Site site : sites) { Hazard hazard = HazardCalc.calc(model, config, site, executor); Deaggregation deagg = calc(hazard, returnPeriod); handler.add(hazard, Optional.of(deagg)); log.fine(hazard.toString()); } handler.expire(); log.info( String.format( PROGRAM + ": %s sites completed in %s", handler.resultsProcessed(), handler.elapsedTime())); if (threadCount != ThreadCount.ONE) { execSvc.shutdown(); } return handler.outputDir(); }