From 50155d2d6da56ac59a672755a704ed1503ffe3f6 Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期四, 19 六月 2025 10:07:18 +0800
Subject: [PATCH] 修复返回的模拟结果中最大最小水深问题

---
 src/main/java/com/se/nsl/service/ResolveService.java |  553 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 542 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/se/nsl/service/ResolveService.java b/src/main/java/com/se/nsl/service/ResolveService.java
index 0c7d47a..d78c9e9 100644
--- a/src/main/java/com/se/nsl/service/ResolveService.java
+++ b/src/main/java/com/se/nsl/service/ResolveService.java
@@ -1,24 +1,57 @@
 package com.se.nsl.service;
 
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.FileUtil;
 import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSON;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.se.nsl.config.PropertiesConfig;
-import com.se.nsl.domain.po.DataPo;
+import com.se.nsl.domain.dto.*;
+import com.se.nsl.domain.po.Rainfall;
 import com.se.nsl.domain.po.Simu;
 import com.se.nsl.domain.po.SimuData;
+import com.se.nsl.domain.vo.ConfigVo;
+import com.se.nsl.domain.vo.RealTimeInput;
+import com.se.nsl.domain.vo.ResultVo;
+import com.se.nsl.helper.ComHelper;
+import com.se.nsl.helper.GdalHelper;
 import com.se.nsl.helper.StringHelper;
 import com.se.nsl.helper.WebHelper;
+import com.se.nsl.utils.AreaType;
+import com.se.nsl.utils.RainFallUnit;
+import com.se.nsl.utils.SimulateType;
+import com.se.nsl.utils.TimeFormatUtil;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.gdal.gdal.Band;
+import org.gdal.gdal.Dataset;
+import org.gdal.gdal.gdal;
+import org.gdal.gdalconst.gdalconstConstants;
 import org.gdal.ogr.Geometry;
 import org.gdal.ogr.ogr;
+import org.gdal.osr.CoordinateTransformation;
+import org.gdal.osr.SpatialReference;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.*;
 import java.sql.Timestamp;
-import java.util.Date;
+import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.temporal.ChronoUnit;
+import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -29,6 +62,20 @@
 
     @Resource
     PropertiesConfig config;
+
+    @Resource
+    UwService uwService;
+
+    @Resource
+    TestService testService;
+
+    private ObjectMapper mapper = new ObjectMapper();
+
+    Integer DIGIT = 1000000;
+
+    SimpleDateFormat YYYYMDHM = new SimpleDateFormat("yyyy M d H m ");
+
+    List<String> MODES = new ArrayList<>(Arrays.asList("姝f�佸垎甯�", "骞冲潎鍒嗗竷", "娉㈠姩骞冲潎鍒嗗竷", "鎸佺画涓婂崌"));
 
     public int start(Simu simu) {
         Date now = new Date();
@@ -45,8 +92,8 @@
         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);
@@ -75,18 +122,23 @@
 
     private void cope(Simu simu) {
         try {
-            DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class);
+            SimuData data = JSONUtil.toBean(simu.getData(), SimuData.class);
 
-            update(simu, 1, null);
-            //gedbService.copeVectors(token, data, db);
+            update(simu, 1, "鍒濆鍖栧弬鏁�");
+            initArgs(simu, data);
+            createRainfallFile(simu, data);
 
-            //update(simu, 3, null);
-            //gedbService.copeDem(token, data);
+            update(simu, 2, "璋冪敤姹傝В鍣�");
+            callUwSolver(simu, data);
 
-            //update(simu, 4, null);
-            //uwService.createRainFile(data);
+            update(simu, 3, "璋冪敤Zarr杞琓if");
+            callZarr2tif(data);
+
+            update(simu, 4, "瑙f瀽鏁版嵁");
+            createNsl(data);
 
             update(simu, 10, "瀹屾垚");
+            log.info("妯℃嫙瀹屾垚");
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
             update(simu, 20, ex.getMessage());
@@ -101,7 +153,486 @@
         simuService.updateById(simu);
     }
 
-    private void initArgs(){
+    /**
+     * 鍒濆鍖栧弬鏁�
+     */
+    public void initArgs(Simu simu, SimuData data) throws IOException {
+        String inPath = config.getInPath() + File.separator + data.getInPath();
+        createDir(inPath);
+        createDir(inPath + File.separator + "depth");
+        createDir(inPath + File.separator + "velocity");
+        createDir(config.getOutPath() + File.separator + data.getOutPath());
 
+        Short areaType = simu.getAreaType();
+        AreaType at = AreaType.of(areaType);
+        String terrainTif = config.getSourceDem();
+        String landuseTif = config.getSourceLanduse();
+        String stationTif = null;
+        if (at == AreaType.KEY_DITCH) {
+            File keyDitchDir = new File(config.getKeyDitch());
+            String areaName = simu.getAreaName();
+            Optional<File> first = Arrays.stream(keyDitchDir.listFiles()).filter(f -> f.getName().equals(areaName)).findFirst();
+            if (first.isPresent()) {
+                File targetTifDir = first.get();
+                File[] files = targetTifDir.listFiles();
+                for (File file : files) {
+                    String name = file.getName();
+                    if (name.toLowerCase().contains("dem")) {
+                        terrainTif = file.getAbsolutePath();
+                    }
+                    if (name.toLowerCase().contains("landuse")) {
+                        landuseTif = file.getAbsolutePath();
+                    }
+                    if (name.toLowerCase().contains("station")) {
+                        stationTif = file.getAbsolutePath();
+                    }
+                }
+            }
+        }
+
+        Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
+        if (geom.GetGeometryType() == ogr.wkbMultiPolygon) geom = geom.GetGeometryRef(0);
+        SpatialReference dstSR = GdalHelper.createSpatialReference(config.getEpsg());
+
+        CoordinateTransformation ct = CoordinateTransformation.CreateCoordinateTransformation(GdalHelper.SR4326, dstSR);
+        geom.Transform(ct);
+        String wkt = geom.ExportToWkt();
+
+        String terrainFile = inPath + File.separator + config.getTerrainFile();
+
+        Dataset dsDem = gdal.Open(terrainTif, gdalconstConstants.GA_ReadOnly);
+        ComHelper.Resample(dsDem, null, terrainFile, null, wkt, null, null);
+        dsDem.delete();
+
+        String landuseFile = inPath + File.separator + config.getLanduseFile();
+
+        Dataset dsLanduse = gdal.Open(landuseTif, gdalconstConstants.GA_ReadOnly);
+        ComHelper.Resample(dsLanduse, null, landuseFile, null, wkt, null, null);
+        dsLanduse.delete();
+
+        if (stationTif != null) {
+            String stationFile = inPath + File.separator + "Station.tif";
+            Dataset dsStation = gdal.Open(stationTif, gdalconstConstants.GA_ReadOnly);
+            ComHelper.Resample(dsStation, null, stationFile, null, wkt, null, null);
+            dsStation.delete();
+        }
+    }
+
+    public void updateTif(Simu simu, SimuData data2) {
+        Dataset ds = gdal.Open(config.getSourceLanduse(), gdalconstConstants.GA_Update); // 浠ヨ鍐欐ā寮忔墦寮�TIFF鏂囦欢
+
+        Band band = ds.GetRasterBand(1);
+        if (band.GetRasterDataType() != gdalconstConstants.GDT_Byte) {
+            System.err.println("閿欒锛氶潪Byte绫诲瀷鏁版嵁");
+            ds.delete();
+            return;
+        }
+
+        int width = band.getXSize();
+        int height = band.getYSize();
+
+        // 璇诲彇Byte绫诲瀷鏁版嵁
+        byte[] data = new byte[width * height];
+        band.ReadRaster(0, 0, width, height, data);
+
+        // 鏇挎崲0鍊间负8锛堟敞鎰廕ava鐨刡yte鏄湁绗﹀彿鐨勶紝闇�澶勭悊0-255鑼冨洿锛�
+        for (int i = 0; i < data.length; i++) {
+            if ((data[i] & 0xFF) == 0) {  // 鏃犵鍙锋瘮杈�
+                data[i] = (byte) 8;
+            }
+        }
+
+        band.WriteRaster(0, 0, width, height, data); // 鍐欏洖鏁版嵁骞朵繚瀛�
+        band.SetNoDataValue(8.0);  // 璁剧疆鏂癗odata鍊�
+        band.FlushCache(); // 寮哄埗鍐欏叆鏇存敼
+        ds.delete();
+    }
+
+    private void createDir(String path) {
+        File f = new File(path);
+        if (f.exists() && f.isDirectory()) {
+            FileUtil.del(f);
+        }
+        f.mkdirs();
+    }
+
+    public void createRainfallFile(Simu simu, SimuData data) throws Exception {
+        List<Rainfall> rainfalls = data.getRainfalls();
+        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());
+
+        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 + " ";
+        //濡傛灉娌℃湁鍗曚綅鎴栬�呭崟浣嶄负mm/h鍒欐寜鐓� mm/h璁$畻锛屽惁鍒欐寜鐓m/5min璁$畻
+//        int unit = StringUtils.isEmpty(data.getIntensityUnit()) || "mm/h".equals(data.getIntensityUnit()) ? 60 : 5;
+        String iu = data.getIntensityUnit();
+        RainFallUnit rfUnit = RainFallUnit.of(iu);
+        data.setIntensityUnit(rfUnit.getUnit());
+        int unit = rfUnit.getC();
+        int c = rainfalls.size() - 1;
+        for (int i = 0; i < c; i++) {
+            Rainfall r1 = rainfalls.get(i);
+            Rainfall r2 = rainfalls.get(i + 1);
+
+            list.addAll(calcRainfall(r1, r2, prefix, unit));
+        }
+        //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)));
+//        list.add(0, "1 " + (list.size() - 1));
+
+        Files.write(Paths.get(rainfallFile), list, StandardCharsets.UTF_8);
+    }
+
+    public static double getMinVal(double val, double radix) {
+        return ((long) Math.floor(val * radix)) / radix;
+    }
+
+    // beijing 116.0 40.0 2025 1 1 0 13 1.666666
+    private List<String> calcRainfall(Rainfall r1, Rainfall r2, String prefix, int unit) {
+        long mins = Math.abs(r2.getTime().getTime() - r1.getTime().getTime()) / (1000 * 60); // 璁$畻鍒嗛挓鏁�
+        double diff = (r2.getIntensity() - r1.getIntensity()) / mins / unit; //ComHelper.getMinVal((r1.getIntensity() - r2.getIntensity()) / mins / unit, DIGIT);
+
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(r1.getTime());
+
+        List<String> list = new ArrayList<>();
+        double intensity = ComHelper.getMinVal(r1.getIntensity() / unit, DIGIT);
+        for (int i = 0; i < mins; i++) {
+            //list.add(prefix + YYYYMDHM.format(cal.getTime()) + getMinVal((intensity + diff * i), DIGIT));
+            list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(cal.getTime()), getMinVal((intensity + diff * i), DIGIT)));
+            cal.add(Calendar.MINUTE, 1);
+        }
+
+        return list;
+    }
+
+    /**
+     * 璋冪敤UWSolver
+     */
+    public String callUwSolver(Simu simu, SimuData data) throws Exception {
+        File uwBat = new File(config.getUwSolverBat());
+
+        int duration = 3600 * data.getDuration(); // 绉掓暟
+        if (null != data.getRainfalls() && data.getRainfalls().size() > 1) {
+            List<Rainfall> rainfalls = data.getRainfalls();
+            int size = rainfalls.size();
+            Rainfall last = rainfalls.get(size - 1);
+            Rainfall first = rainfalls.get(0);
+            Instant end = last.getTime().toInstant();
+            Instant start = first.getTime().toInstant();
+            long diff = ChronoUnit.SECONDS.between(end, start);
+            duration = (int) (Math.abs(diff));
+        }
+
+        String inPath = config.getInPath() + File.separator + data.getInPath();
+        String terrainFile = inPath + File.separator + config.getTerrainFile();
+        String landuseFile = inPath + File.separator + config.getLanduseFile();
+        String rainfallFile = (inPath + File.separator + "rainfall.dat");
+        String saveName = inPath + File.separator + "result.zarr";
+//        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile, saveName, duration, config.getSaveFrames());
+        Integer saveFrameInterval = config.getSaveFrameInterval();
+        int saveFrames = duration / 60 / saveFrameInterval;
+        Short type = simu.getType();
+        SimulateType simulateType = SimulateType.of(type);
+        String saveMode = simulateType.getSaveMode();
+        String startTime = TimeFormatUtil.formatDate(data.getStartTime());
+        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile,
+                saveName, duration, saveFrames, saveMode, startTime);
+        String configFile = config.getInPath() + File.separator + data.getInPath() + File.separator + data.getInPath() + ".json";
+//        ComHelper.writeJson(configFile, JSON.toJSONString(vo));
+        mapper.writeValue(new File(configFile), vo);
+        String cmd = String.format("%s \"%s\"", config.getUwSolverBat(), configFile);
+        return callBat2(cmd);
+    }
+
+    /**
+     * 璋冪敤zarr2tif
+     */
+    public String callZarr2tif(SimuData data) throws Exception {
+        String inPath = config.getInPath() + File.separator + data.getInPath();
+        String zarrFile = inPath + File.separator + "result.zarr";
+        String geotiffDir = inPath + File.separator + "depth";
+        String terrainFile = inPath + File.separator + config.getTerrainFile();
+        String jsonPath = inPath + File.separator + "zarr2tif.json";
+        Zarr2Tif zarr2Tif = new Zarr2Tif(zarrFile, geotiffDir, terrainFile, Collections.emptyList());
+//        ComHelper.writeJson(jsonPath, JSON.toJSONString(zarr2Tif));
+        mapper.writeValue(new File(jsonPath), zarr2Tif);
+        String cmd = String.format("%s \"%s\"", config.getZarr2tifBat(), jsonPath);
+
+        return callBat2(cmd);
+    }
+
+    private String callBat2(String cmd) {
+        try {
+            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", cmd);
+            pb.redirectErrorStream(true); // 鍚堝苟閿欒娴佸埌鏍囧噯杈撳嚭
+
+            Process process = pb.start();
+            process.getOutputStream().close();
+
+            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    log.info(line);
+                }
+            }
+
+            int exitCode = process.waitFor();
+
+            return "ok"; // sb.toString();
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
+    }
+
+    private String callBat(String cmd) {
+        try {
+            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", cmd);
+            pb.redirectErrorStream(true); // 鍚堝苟閿欒娴佸埌鏍囧噯杈撳嚭
+
+            Process process = pb.start();
+            process.getOutputStream().close();
+
+            /*StringBuilder sb = new StringBuilder();
+            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    System.out.println(line);
+                    sb.append(line);
+                }
+            }*/
+
+            int exitCode = process.waitFor();
+
+            return "ok"; // sb.toString();
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
+    }
+
+    private void createNsl(SimuData data) throws Exception {
+        testService.test(data);
+    }
+
+    private void procTifs(String tifPath, String outPath, Date startTime) {
+        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(startTime);
+
+        for (File file : new File(tifPath).listFiles()) {
+            if (!file.exists() || !file.isDirectory()) continue;
+
+            File tif = new File(tifPath + "\\" + file.getName() + File.separator + "depth.tif");
+            if (!tif.exists() || tif.isDirectory()) continue;
+
+            calendar.add(Calendar.SECOND, 1);
+            String newName = df.format(calendar.getTime());
+            String newFile = outPath + File.separator + newName + ".tif";
+
+            System.out.println(newFile);
+            tif.renameTo(new File(newFile));
+            file.delete();
+        }
+    }
+
+    public String createRainfallCsv(String csvPath, String mode, double total, double intensity, int hours) {
+        // python 鑴氭湰鍚�.py <鍙傛暟1-csv鏂囦欢鍚�> <鍙傛暟2-闄嶉洦妯″紡:姝f�佸垎甯億骞冲潎鍒嗗竷|娉㈠姩骞冲潎鍒嗗竷|鎸佺画涓婂崌> <鍙傛暟3-闄嶉洦鎬婚噺> <鍙傛暟4-鏈�澶ч洦寮�> <鍙傛暟5-闄嶉洦鏃堕棿(鍒嗛挓)>
+        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);
+        }
+//        list.add(0, "1 " + (list.size() - 1));
+
+        Files.write(Paths.get(dat), list, StandardCharsets.UTF_8);
+    }
+
+    //瀹炴椂妯℃嫙
+    public String realTimeSimulate(RealTimeInput input) throws IOException {
+        long currentTime = System.currentTimeMillis();
+        //鏍规嵁鏈嶅姟鎵惧埌鎸囧畾鐨勬枃浠跺す
+        String serviceName = input.getServiceName();
+        File serviceNameDir = new File(config.getInPath(), serviceName);
+        //鐢熸垚涓�涓柊鐨勯洦閲忔枃浠�,闇�瑕佸師鍏堥洦閲忔枃浠剁殑涓�浜涗俊鎭紝鎵�浠ラ渶瑕佸厛璇诲彇鏃х殑
+        String[] values = readTheOldFirstLineRainfallValue(serviceNameDir);
+        File newDatFile = generateNewRainfallFile(input, values, serviceNameDir, currentTime);
+
+        //鐢熸垚涓�涓柊鐨勭敓鎴恴arr鐨勯厤缃枃浠�
+        File newConfigFile = generateNewZarrConfigFile(serviceNameDir, serviceName, currentTime, newDatFile);
+        //鎵ц姹傝В鍣ㄨ繍绠�
+        String cmd = String.format("%s \"%s\"", config.getUwSolverBat(), newConfigFile);
+        callBat2(cmd);
+
+        //鐢熸垚涓�涓柊鐨剒arr杞瑃if鐨刯son鏂囦欢
+        File newZarr2TifJson = generateNewZarr2TifJson(serviceNameDir, currentTime);
+        //鎵цzarr杞瑃if
+        String zarr2TifCmd = String.format("%s \"%s\"", config.getZarr2tifBat(), newZarr2TifJson);
+        callBat2(zarr2TifCmd);
+        //杩斿洖鏂扮殑layer.json鍚嶇О
+        return generateLayerJsonAndPng(serviceName, serviceNameDir, currentTime);
+
+    }
+
+    private String generateLayerJsonAndPng(String serviceName, File serviceNameDir, long currentTime) throws IOException {
+        ResultDto resultDto = new ResultDto();
+        resultDto.setServiceName(serviceName);
+        File temp = Paths.get(config.getOutPath(), serviceName, "temp").toFile();
+        if (!temp.exists()) temp.mkdir();
+        resultDto.setTemp(temp.getAbsolutePath());
+        resultDto.setOutPath(Paths.get(config.getOutPath(), serviceName).toString());
+        File dem = new File(serviceNameDir, "DEM.tif");
+        resultDto.setTerrainFile(dem.getAbsolutePath());
+        File newDepthDir = new File(serviceNameDir + File.separator + "depth_" + currentTime);
+        File[] files = newDepthDir.listFiles();
+        resultDto.setWaterPath(newDepthDir.getAbsolutePath());
+        LayerDto layerDto = new LayerDto(config.getVer(), 4548, config.getSizes());
+        String newLayerJsonName = "layer_" + currentTime + ".json";
+        layerDto.setName(newLayerJsonName);
+        testService.processRealTime(resultDto, layerDto);
+        return newLayerJsonName;
+    }
+
+    private File generateNewZarr2TifJson(File serviceNameDir, long currentTime) throws IOException {
+        File srcZarr2TifJson = new File(serviceNameDir, "zarr2tif.json");
+        Zarr2Tif zarr2Tif = mapper.readValue(srcZarr2TifJson, Zarr2Tif.class);
+        //淇敼zarr2tif瀵硅薄涓殑瀛楁
+//        String stamp = TimeFormatUtil.formatTime(currentTime, "yyyy-MM-dd HH:mm:ss");
+//        zarr2Tif.setStart_timestamp(stamp);
+//        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
+        String newZarrPath = serviceNameDir + File.separator + "result.zarr";
+        zarr2Tif.setZarr_file(newZarrPath);
+        zarr2Tif.setGeotiff_dir(serviceNameDir + File.separator + "depth_" + currentTime);
+        File newZarr2TifJson = new File(serviceNameDir, "zarr2tif_" + currentTime + ".json");
+        mapper.writeValue(newZarr2TifJson, zarr2Tif);
+        return newZarr2TifJson;
+    }
+
+    private File generateNewZarrConfigFile(File serviceNameDir, String serviceName, long currentTime, File newDatFile) throws IOException {
+        File configFile = new File(serviceNameDir, serviceName + ".json");
+        ConfigVo configVo = mapper.readValue(configFile, ConfigVo.class);
+        int simulateTime = 300; //妯℃嫙鏃堕棿锛岄粯璁や负5min锛屽嵆300s
+        int intervalTime = 60; //姣忓抚鐨勯棿闅旀椂闂达紝榛樿涓�60s,60s鐢熸垚涓�甯�
+        configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file
+        ResultVo result = configVo.getResult();
+        Integer oldDuration = configVo.getDuration();
+//        result.setSave_start(oldDuration); //璧峰鏃堕棿瑕佸湪涓婃鏃堕棿鐨勫熀纭�涓婂紑濮�
+        configVo.setDuration(oldDuration + simulateTime); //鍥哄畾涓�5min
+        result.setSave_interval(intervalTime);
+        result.setSave_frames(result.getSave_frames() + (simulateTime / intervalTime)); //淇濈暀5甯э紝鍦ㄥ師鏉ョ殑鍩虹涓婂鍔�5甯�
+//        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
+        String newZarrPath = serviceNameDir + File.separator + "result.zarr";
+        result.setSave_name(newZarrPath);
+        File newConfigFile = new File(serviceNameDir, currentTime + ".json");
+        mapper.writeValue(newConfigFile, configVo);
+        return newConfigFile;
+    }
+
+    private File generateNewRainfallFile(RealTimeInput input, String[] values, File serviceNameDir, long currentTime) throws IOException {
+        String station = values[0];
+        double lon = Double.parseDouble(values[1]);
+        double lat = Double.parseDouble(values[2]);
+        String title = config.getRainfallTitle();
+        List<String> newLines = new ArrayList<>();
+        newLines.add(title);
+        List<RealTimeInput.RealTimeData> data = input.getData();
+        for (RealTimeInput.RealTimeData rd : data) {
+            LocalDateTime dateTime = rd.getDateTime();
+            int year = dateTime.getYear();
+            int month = dateTime.getMonthValue();
+            int day = dateTime.getDayOfMonth();
+            int hour = dateTime.getHour();
+            int minute = dateTime.getMinute();
+            double intensity = rd.getIntensity(); //淇濈暀鎸囧畾浣嶆暟灏忔暟
+            String l = String.format("%s %s %s %s %s %s %s %s %s",
+                    station, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity));
+            newLines.add(l);
+        }
+        File newDatFile = new File(serviceNameDir, "rainfall_" + currentTime + ".dat");
+        if (!newDatFile.exists()) newDatFile.createNewFile();
+        Files.write(newDatFile.toPath(), newLines, StandardOpenOption.TRUNCATE_EXISTING);
+        return newDatFile;
+    }
+
+    private static String[] readTheOldFirstLineRainfallValue(File serviceNameDir) throws IOException {
+        File srcRailfallFile = new File(serviceNameDir, "rainfall.dat");
+        List<String> lines = Files.readAllLines(srcRailfallFile.toPath());
+        String secondLine = lines.get(1);
+        return secondLine.split(" ");
     }
 }

--
Gitblit v1.9.3