| | |
| | | import javax.annotation.Resource; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | |
| | | import java.util.*; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | @Service |
| | |
| | | |
| | | SimpleDateFormat YYYYMDHM = new SimpleDateFormat("yyyy M d H m "); |
| | | |
| | | List<String> MODES = new ArrayList<>(Arrays.asList("正态分布", "平均分布", "波动平均分布", "持续上升")); |
| | | |
| | | public int start(Simu simu) { |
| | | Date now = new Date(); |
| | | String date = StringHelper.YMDHMS2_FORMAT.format(now); |
| | |
| | | SimuData data = JSON.parseObject(simu.getData(), SimuData.class); |
| | | data.setInPath(date); |
| | | data.setOutPath(date); |
| | | data.setEpsg(4548); |
| | | data.setEnvelope(envelope); |
| | | data.setEpsg(config.getEpsg()); |
| | | |
| | | simu.setData(JSON.toJSONString(data)); |
| | | simu.setServiceName(date); |
| | |
| | | |
| | | update(simu, 1, "初始化参数"); |
| | | initArgs(data); |
| | | createRainfallFile(data); |
| | | createRainfallFile(simu, data); |
| | | |
| | | update(simu, 2, "调用求解器"); |
| | | callUwSolver(data); |
| | |
| | | f.mkdirs(); |
| | | } |
| | | |
| | | private void createRainfallFile(SimuData data) throws Exception { |
| | | private void createRainfallFile(Simu simu, SimuData data) throws Exception { |
| | | List<Rainfall> rainfalls = data.getRainfalls(); |
| | | if (null == rainfalls || rainfalls.size() < 2) return; // CollUtil.isEmpty |
| | | if (null == rainfalls || rainfalls.size() < 2) createRainfall(simu); |
| | | |
| | | String dat = config.getInPath() + File.separator + "Rainfalls" + File.separator + simu.getId() + ".dat"; |
| | | String rainfallFile = config.getInPath() + File.separator + data.getInPath() + File.separator + "rainfall.dat"; |
| | | if (new File(dat).exists()) { |
| | | Files.copy(Paths.get(dat), Paths.get(rainfallFile), StandardCopyOption.REPLACE_EXISTING); |
| | | return; |
| | | } |
| | | |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(config.getRainfallTitle()); |
| | |
| | | //list.add(prefix + YYYYMDHM.format(rainfalls.get(c).getTime()) + getMinVal(rainfalls.get(c).getIntensity() / unit, DIGIT)); |
| | | list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(rainfalls.get(c).getTime()), getMinVal(rainfalls.get(c).getIntensity() / unit, DIGIT))); |
| | | |
| | | String rainfallFile = config.getInPath() + File.separator + data.getInPath() + File.separator + "rainfall.dat"; |
| | | Files.write(Paths.get(rainfallFile), list, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | public String createRainfallCsv(String csvPath, String type, double total, double intensity, int hours) { |
| | | public String createRainfallCsv(String csvPath, String mode, double total, double intensity, int hours) { |
| | | // python 脚本名.py <参数1-csv文件名> <参数2-降雨模式:正态分布|平均分布|波动平均分布|持续上升> <参数3-降雨总量> <参数4-最大雨强> <参数5-降雨时间(分钟)> |
| | | String cmd = String.format("%s \"%s\" \"%s\" %f %f %d", config.getCreateRainfall(), csvPath, type, total, intensity, hours * 60); |
| | | String cmd = String.format("%s \"%s\" \"%s\" %f %f %d", config.getCreateRainfall(), csvPath, mode, total, intensity, hours * 60); |
| | | |
| | | return callBat(cmd); |
| | | } |
| | | |
| | | public void createRainfall(Simu simu) throws Exception { |
| | | SimuData data = JSON.parseObject(simu.getData(), SimuData.class); |
| | | if (null == data.getMode() || MODES.contains(data.getMode())) data.setMode(MODES.get(0)); |
| | | if (StringUtils.isEmpty(data.getIntensityUnit())) data.setIntensityUnit("mm/h"); |
| | | |
| | | Geometry geom = Geometry.CreateFromWkt(simu.getGeom()); |
| | | if (geom.GetGeometryType() == ogr.wkbMultiPolygon) geom = geom.GetGeometryRef(0); |
| | | double[] envelope = new double[4]; |
| | | geom.GetEnvelope(envelope); |
| | | data.setEnvelope(envelope); |
| | | data.setEpsg(config.getEpsg()); |
| | | |
| | | String basePath = config.getInPath() + File.separator + "Rainfalls"; |
| | | if (!new File(basePath).exists()) new File(basePath).mkdirs(); |
| | | if (null == simu.getCreateTime()) simu.setCreateTime(new Timestamp(new Date().getTime())); |
| | | |
| | | String csvPath = basePath + File.separator + simu.getId() + ".csv"; |
| | | int unit = StringUtils.isEmpty(data.getIntensityUnit()) || "mm/h".equals(data.getIntensityUnit()) ? 60 : 5; |
| | | createRainfallCsv(csvPath, data.getMode(), data.getTotal(), data.getIntensity() / unit, data.getDuration()); |
| | | |
| | | List<Double> list = getValues(csvPath); |
| | | if (!CollUtil.isEmpty(list)) { |
| | | data.setRainfalls(new ArrayList<>()); |
| | | setRainfalls(simu, data, list); |
| | | } |
| | | |
| | | simu.setData(JSON.toJSONString(data)); |
| | | } |
| | | |
| | | private List<Double> getValues(String csvPath) throws Exception { |
| | | if (!new File(csvPath).exists()) return null; |
| | | |
| | | List<String> list = Files.readAllLines(Paths.get(csvPath)); |
| | | list.remove(0); |
| | | //list.remove(list.size() - 1); |
| | | |
| | | return list.stream() |
| | | .map(s -> new BigDecimal(s).setScale(6, RoundingMode.HALF_DOWN).doubleValue()) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | private void setRainfalls(Simu simu, SimuData data, List<Double> vals) throws Exception { |
| | | String basePath = config.getInPath() + File.separator + "Rainfalls"; |
| | | String dat = basePath + File.separator + simu.getId() + ".dat"; |
| | | |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(data.getStartTime()); |
| | | |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(config.getRainfallTitle()); |
| | | |
| | | double centerX = ComHelper.getMinVal((data.getMinx() + data.getMaxx()) / 2, DIGIT); |
| | | double centerY = ComHelper.getMinVal((data.getMiny() + data.getMaxy()) / 2, DIGIT); |
| | | String prefix = config.getRainfallSite() + " " + centerX + " " + centerY + " "; |
| | | |
| | | Double total = 0.0; |
| | | for (int i = 0, c = vals.size(); i < c; i++) { |
| | | total += vals.get(i); |
| | | if (i % 15 == 0) { |
| | | data.getRainfalls().add(new Rainfall(cal.getTime(), vals.get(i), total)); |
| | | } |
| | | list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(cal.getTime()), vals.get(i))); |
| | | |
| | | cal.add(Calendar.MINUTE, 1); |
| | | } |
| | | |
| | | Files.write(Paths.get(dat), list, StandardCharsets.UTF_8); |
| | | } |
| | | } |