From ff0e7ceac70feb2f21c0d4137d63b83c3ea80867 Mon Sep 17 00:00:00 2001 From: hky <13673834656@163.com> Date: Wed, 11 Oct 2023 08:22:31 +0800 Subject: [PATCH] =?UTF-8?q?9.23=E5=89=8D=E5=90=8E=E7=AB=AF=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E4=BA=A4=20=E8=AF=A6=E6=83=85=E8=A7=81?= =?UTF-8?q?=E5=91=A8=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseAPI/service/IBizBaseAPIService.java | 18 +++++ .../service/impl/BizBaseAPIServiceImpl.java | 58 +++++++++++++ .../service/impl/BizCmaqServiceImpl.java | 81 ++----------------- .../bizWrf/controller/BizWrfController.java | 81 +++---------------- .../bizWrf/service/IBizWrfService.java | 1 - .../service/impl/BizWrfServiceImpl.java | 55 ------------- .../controller/RunProcessController.java | 5 +- .../java/org/jeecg/modules/util/NcUtil.java | 69 ++++++++++++++++ 8 files changed, 169 insertions(+), 199 deletions(-) create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/IBizBaseAPIService.java create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/impl/BizBaseAPIServiceImpl.java create mode 100644 jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/util/NcUtil.java diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/IBizBaseAPIService.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/IBizBaseAPIService.java new file mode 100644 index 00000000..3fa5433a --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/IBizBaseAPIService.java @@ -0,0 +1,18 @@ +package org.jeecg.modules.project.calculateConfig.baseAPI.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.project.calculateConfig.bizConfigChemistry.entity.BizConfigChemistry; + +import java.io.IOException; + +/** + * @Description: 化爆配置表 + * @Author: jeecg-boot + * @Date: 2023-09-20 + * @Version: V1.0 + */ +public interface IBizBaseAPIService { + + int getLayerByHeight(String enginId,Integer height) throws IOException; + +} diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/impl/BizBaseAPIServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/impl/BizBaseAPIServiceImpl.java new file mode 100644 index 00000000..f2f71be0 --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/baseAPI/service/impl/BizBaseAPIServiceImpl.java @@ -0,0 +1,58 @@ +package org.jeecg.modules.project.calculateConfig.baseAPI.service.impl; + +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.project.baseConfig.bizEngineering.entity.BizEngineering; +import org.jeecg.modules.project.baseConfig.bizEngineering.service.IBizEngineeringService; +import org.jeecg.modules.project.calculateConfig.baseAPI.service.IBizBaseAPIService; +import org.jeecg.modules.project.calculateConfig.bizConfigChemistry.entity.BizConfigChemistry; +import org.jeecg.modules.project.calculateConfig.bizConfigChemistry.mapper.BizConfigChemistryMapper; +import org.jeecg.modules.project.calculateConfig.bizWrf.entity.BizWrf; +import org.jeecg.modules.project.calculateConfig.bizWrf.mapper.BizWrfMapper; +import org.jeecg.modules.project.calculateConfig.bizWrf.service.IBizWrfService; +import org.jeecg.modules.util.NcUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import ucar.nc2.NetcdfFile; +import ucar.nc2.dataset.NetcdfDataset; + +import java.io.IOException; +import java.util.Date; + +/** + * @Description: 化爆配置表 + * @Author: jeecg-boot + * @Date: 2023-09-20 + * @Version: V1.0 + */ +@Service +public class BizBaseAPIServiceImpl implements IBizBaseAPIService { + + @Autowired + private BizWrfMapper bizWrfService; + + @Autowired + private IBizEngineeringService bizEngineeringService; + + @Value("${spring.baseHome}") + private String baseHome; + @Value("${spring.localFilePrefix}") + private String localFilePrefix; + + @Override + public int getLayerByHeight(String enginId,Integer height) throws IOException { + BizEngineering engineering = bizEngineeringService.getById(enginId); + BizWrf bizWrf = bizWrfService.selectOne(new LambdaQueryWrapper().eq(BizWrf::getEngineeringId, enginId)); + String localFilePath = localFilePrefix + "/" + engineering.getCreateBy() + "/" + engineering.getEngineeringName() + "/"; + + DateTime startTime = DateUtil.parse(bizWrf.getStartTime(), "yyyy-MM-dd_hh:mm:ss"); + String metcr03dName= "METCRO3D_d01_" + DateUtil.format(new Date(startTime.getTime()), "yyMMdd")+".nc"; + + NetcdfFile ncfile = NetcdfDataset.open(localFilePath + metcr03dName); + NcUtil.getNCByName(ncfile,"ZF",0); + return 0; + } +} diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizCmaq/service/impl/BizCmaqServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizCmaq/service/impl/BizCmaqServiceImpl.java index d81695d5..baaa23ff 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizCmaq/service/impl/BizCmaqServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizCmaq/service/impl/BizCmaqServiceImpl.java @@ -13,6 +13,7 @@ import org.jeecg.modules.project.baseConfig.bizEngineering.entity.BizEngineering import org.jeecg.modules.project.baseConfig.bizEngineering.service.IBizEngineeringService; import org.jeecg.modules.project.calculateConfig.bizOpenfoam.entity.BizOpenfoam; import org.jeecg.modules.project.calculateConfig.bizOpenfoam.service.IBizOpenfoamService; +import org.jeecg.modules.util.NcUtil; import org.jeecg.modules.util.SFTPUtil; import org.springframework.beans.factory.annotation.Value; import org.jeecg.modules.project.calculateConfig.bizWrf.entity.BizWrf; @@ -269,6 +270,8 @@ public class BizCmaqServiceImpl extends ServiceImpl impl // String ncLocalName = "CCTM_ACONC_v532_2016_12SE1_"+DateUtil.format(new Date(startTimeSecs), ymdFormat).replace("-","")+".nc"; sftpUtil.download(allRunPath + "CMAQ/data/output/v532_2016_12SE1/",ncName,targetFilePath + ncName); } + String metcr03dName= "METCRO3D_d01_" + DateUtil.format(new Date(startTimeSecs), yymdFormat)+".nc"; + sftpUtil.download(allRunPath + "CMAQ/data/input/mcip/",metcr03dName,targetFilePath + metcr03dName); sftpUtil.logout(); } catch (Exception e) { bizEngineeringService.updateErrorStatus(engineeringId,9); @@ -298,11 +301,11 @@ public class BizCmaqServiceImpl extends ServiceImpl impl // String ncNameWrf = "wrfout_d01_" + bizWrf.getStartTime(); String ncNameWrf = "wrfout_d01_2016-07-01_00_00_00"; NetcdfFile griddot2d = NetcdfDataset.open(targetFilePath + ncNameWrf); - List>> coAllList = getNCByName(ncfile, "CO", layer); - List>> no2AllList = getNCByName(ncfile, "NO2", layer); - List>> no3AllList = getNCByName(ncfile, "NO3", layer); - List>> xlatAllList = getNCByName(griddot2d, "XLAT", layer); - List>> xlongAllList = getNCByName(griddot2d, "XLONG", layer); + List>> coAllList = NcUtil.getNCByName(ncfile, "CO", layer); + List>> no2AllList = NcUtil.getNCByName(ncfile, "NO2", layer); + List>> no3AllList = NcUtil.getNCByName(ncfile, "NO3", layer); + List>> xlatAllList = NcUtil.getNCByName(griddot2d, "XLAT", layer); + List>> xlongAllList = NcUtil.getNCByName(griddot2d, "XLONG", layer); for (int l = 0; l < coAllList.size(); l++) { List result = new ArrayList<>(); @@ -406,72 +409,4 @@ public class BizCmaqServiceImpl extends ServiceImpl impl sftpUtil.logout(); } - public static List>> getNCByName(NetcdfFile ncfile, String name,int layer){ - Variable variable = ncfile.findVariable(name); - List>> resultAll = new ArrayList<>(); - - // 读取nc数据到数组 - Array data = null; - try { - data = variable.read(); - } catch (IOException e) { - e.printStackTrace(); - } - // 获取参数和索引,其中shape的前三个参数分别是时间、纬度、经度 - int[] shape = data.getShape(); - Index index = data.getIndex(); - - // 将三维数组降维,并用String数组提取数据 - // 按时间 - // 按维度 - if(shape.length == 3){ - // 将三维数组降维,并用String数组提取数据 - // 按时间 - // 按维度 - for (int i = 0; i < shape[0]; i++) { - List> resultPiece = new ArrayList<>(); - for (int j = 0; j < shape[1]; j++) { - List strings = new ArrayList<>(); - // 按经度 - for (int k = 0; k < shape[2]; k++) { - // 按照对应索引获取数据并转换为string类型添加到数组中 - Double dval = Math.round(data.getDouble(index.set(i, j, k)) * 1000) /1000d; - strings.add(dval); - } - resultPiece.add(strings); - } - resultAll.add(resultPiece); - } - }else{ - for (int i = 0; i < shape[0]; i++) { - List> resultPiece = new ArrayList<>(); - for (int j = 0; j < shape[2]; j++) { - List strings = new ArrayList<>(); - // 按经度 - for (int k = 0; k < shape[3]; k++) { - // 按照对应索引获取数据并转换为string类型添加到数组中 - Double dval = 0.0; - if(name.equals("LATD") || name.equals("LOND")){ - dval = Math.round(data.getFloat(index.set(i, layer, j, k)) * 10000) /10000d; - }else{ - try { - dval = Math.round(data.getDouble(index.set(i, layer, j, k)) * 10000000) /10000000d; - }catch (Exception e){ - e.printStackTrace(); - } - } - strings.add(dval); - } - resultPiece.add(strings); - } - resultAll.add(resultPiece); - } - } - return resultAll; - } - - public static void main(String[] args) { - - } - } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/controller/BizWrfController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/controller/BizWrfController.java index 5a60a660..a110b9c6 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/controller/BizWrfController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/controller/BizWrfController.java @@ -30,6 +30,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; +import org.jeecg.modules.util.NcUtil; import org.jeecg.modules.util.SFTPUtil; import org.jeecg.common.system.base.controller.JeecgController; import org.springframework.beans.factory.annotation.Autowired; @@ -443,10 +444,10 @@ public class BizWrfController extends JeecgController { // String ncNameWrf = "wrfout_d01_" + newStartTime; String ncNameWrf = "wrfout_d01_2016-07-01_00_00_00"; NetcdfFile ncfile = NetcdfDataset.open(localFilePath + ncNameWrf); - List>> xlatAllList = getNCByName(ncfile, "XLAT",layer); - List>> xlongAllList = getNCByName(ncfile, "XLONG",layer); - List>> uAllList = getNCByName(ncfile, "U",layer); - List>> vAllList = getNCByName(ncfile, "V",layer); + List>> xlatAllList = NcUtil.getNCByName(ncfile, "XLAT",layer); + List>> xlongAllList = NcUtil.getNCByName(ncfile, "XLONG",layer); + List>> uAllList = NcUtil.getNCByName(ncfile, "U",layer); + List>> vAllList = NcUtil.getNCByName(ncfile, "V",layer); for (int l=0; l result = new ArrayList<>(); @@ -507,9 +508,9 @@ public class BizWrfController extends JeecgController { // String ncNameWrf = "wrfout_d01_" + newStartTime; String ncNameWrf = "wrfout_d01_2016-07-01_00_00_00"; NetcdfFile ncfile = NetcdfDataset.open(localFilePath + ncNameWrf); - List>> hgtAllList = getNCByName(ncfile, "HGT",0); - List>> xlatAllList = getNCByName(ncfile, "XLAT",0); - List>> xlongAllList = getNCByName(ncfile, "XLONG",0); + List>> hgtAllList = NcUtil.getNCByName(ncfile, "HGT",0); + List>> xlatAllList = NcUtil.getNCByName(ncfile, "XLAT",0); + List>> xlongAllList = NcUtil.getNCByName(ncfile, "XLONG",0); for (int l=0;l result = new ArrayList<>(); for (int i = 0; i < hgtAllList.get(l).size(); i++) { @@ -561,10 +562,10 @@ public class BizWrfController extends JeecgController { // String ncNameWrf = "wrfout_d01_" + newStartTime; String ncNameWrf = "wrfout_d01_2016-07-01_00_00_00"; NetcdfFile ncfile = NetcdfDataset.open(localFilePath + ncNameWrf); - List>> uAllList = getNCByName(ncfile, "U",0); - List>> vAllList = getNCByName(ncfile, "V",0); - List>> pAllList = getNCByName(ncfile, "P",0); - List>> tAllList = getNCByName(ncfile, "T",0); + List>> uAllList = NcUtil.getNCByName(ncfile, "U",0); + List>> vAllList = NcUtil.getNCByName(ncfile, "V",0); + List>> pAllList = NcUtil.getNCByName(ncfile, "P",0); + List>> tAllList = NcUtil.getNCByName(ncfile, "T",0); Variable xlatVariable = ncfile.findVariable("XLAT"); //时间、纬度、经度 @@ -718,61 +719,6 @@ public class BizWrfController extends JeecgController { return super.importExcel(request, response, BizWrf.class); } - public static List>> getNCByName(NetcdfFile ncfile, String name,int layer){ - Variable variable = ncfile.findVariable(name); - List>> resultAll = new ArrayList<>(); - // 读取nc数据到数组 - Array data = null; - try { - data = variable.read(); - } catch (IOException e) { - e.printStackTrace(); - } - - // 获取参数和索引,其中shape的前三个参数分别是时间、纬度、经度 - int[] shape = data.getShape(); - Index index = data.getIndex(); - - if(shape.length == 3){ - // 将三维数组降维,并用String数组提取数据 - // 按时间 - // 按维度 - for (int i = 0; i < shape[0]; i++) { - List> resultPiece = new ArrayList<>(); - for (int j = 0; j < shape[1]; j++) { - List strings = new ArrayList<>(); - // 按经度 - for (int k = 0; k < shape[2]; k++) { - // 按照对应索引获取数据并转换为string类型添加到数组中 - Double dval = Math.round(data.getFloat(index.set(i, j, k)) * 1000) /1000d; - strings.add(dval); - } - resultPiece.add(strings); - } - resultAll.add(resultPiece); - } - }else{ - // 将三维数组降维,并用String数组提取数据 - // 按时间 - // 按维度 - for (int i = 0; i < shape[0]; i++) { - List> resultPiece = new ArrayList<>(); - for (int j = 0; j < shape[2]; j++) { - List strings = new ArrayList<>(); - // 按经度 - for (int k = 0; k < shape[3]; k++) { - // 按照对应索引获取数据并转换为string类型添加到数组中 - Double dval = Math.round(data.getFloat(index.set(i,layer, j, k)) * 1000) /1000d; - strings.add(dval); - } - resultPiece.add(strings); - } - resultAll.add(resultPiece); - } - } - return resultAll; - } - public void sftpUpload(String file,String cshFilePath,String fileName) throws FileNotFoundException, SftpException { SFTPUtil sftpUtil = new SFTPUtil(); sftpUtil.login(username, password,ip,port); @@ -790,8 +736,7 @@ public class BizWrfController extends JeecgController { // String wmhr = RemoteExecuteCommand.runRemoteLinuxCmd("192.168.115.128", "wmhr", "123456", "cd /home/wmhr/OpenFOAM-8/run/admin/工程001/;ls"); // System.out.println(wmhr); - String wmhr = RemoteExecuteCommand.runRemoteLinuxCmd("192.168.115.128", "wmhr", "123456", "ps -ef | grep vmtoolsd"); + String wmhr = RemoteExecuteCommand.runRemoteLinuxCmd("192.168.115.128", "wmhr", "123456", "ps -ef | grep wrf.exe"); System.out.println(wmhr); - } } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/IBizWrfService.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/IBizWrfService.java index 9ca5c373..133b6c52 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/IBizWrfService.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/IBizWrfService.java @@ -21,6 +21,5 @@ public interface IBizWrfService extends IService { Result runMetgrid(String allRunPath, String engineeringId)throws IOException; Result runReal(String allRunPath, String engineeringId)throws IOException; Result runWrf(String allRunPath, String engineeringId) throws IOException; - List>> getNCByName(NetcdfFile ncfile, String name, int layer); } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/impl/BizWrfServiceImpl.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/impl/BizWrfServiceImpl.java index 6b972b4b..c077c60a 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/impl/BizWrfServiceImpl.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateConfig/bizWrf/service/impl/BizWrfServiceImpl.java @@ -178,60 +178,5 @@ public class BizWrfServiceImpl extends ServiceImpl impleme sftpUtil.logout(); return Result.error(wrfLog); } - @Override - public List>> getNCByName(NetcdfFile ncfile, String name, int layer){ - Variable variable = ncfile.findVariable(name); - List>> resultAll = new ArrayList<>(); - // 读取nc数据到数组 - Array data = null; - try { - data = variable.read(); - } catch (IOException e) { - e.printStackTrace(); - } - - // 获取参数和索引,其中shape的前三个参数分别是时间、纬度、经度 - int[] shape = data.getShape(); - Index index = data.getIndex(); - - if(shape.length == 3){ - // 将三维数组降维,并用String数组提取数据 - // 按时间 - // 按维度 - for (int i = 0; i < shape[0]; i++) { - List> resultPiece = new ArrayList<>(); - for (int j = 0; j < shape[1]; j++) { - List strings = new ArrayList<>(); - // 按经度 - for (int k = 0; k < shape[2]; k++) { - // 按照对应索引获取数据并转换为string类型添加到数组中 - Double dval = Math.round(data.getFloat(index.set(i, j, k)) * 1000) /1000d; - strings.add(dval); - } - resultPiece.add(strings); - } - resultAll.add(resultPiece); - } - }else{ - // 将三维数组降维,并用String数组提取数据 - // 按时间 - // 按维度 - for (int i = 0; i < shape[0]; i++) { - List> resultPiece = new ArrayList<>(); - for (int j = 0; j < shape[2]; j++) { - List strings = new ArrayList<>(); - // 按经度 - for (int k = 0; k < shape[3]; k++) { - // 按照对应索引获取数据并转换为string类型添加到数组中 - Double dval = Math.round(data.getFloat(index.set(i,layer, j, k)) * 1000) /1000d; - strings.add(dval); - } - resultPiece.add(strings); - } - resultAll.add(resultPiece); - } - } - return resultAll; - } } diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateResult/runProcess/controller/RunProcessController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateResult/runProcess/controller/RunProcessController.java index 0532046e..ebd4ec0c 100644 --- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateResult/runProcess/controller/RunProcessController.java +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/project/calculateResult/runProcess/controller/RunProcessController.java @@ -22,6 +22,7 @@ import org.jeecg.modules.project.calculateConfig.bizWrf.service.IBizWrfService; import org.jeecg.modules.project.calculateResult.bizResultDiffuse.entity.BizResultDiffuse; import org.jeecg.modules.project.calculateResult.bizResultDiffuse.service.IBizResultDiffuseService; import org.jeecg.modules.project.calculateResult.runProcess.VO.ProgressVO; +import org.jeecg.modules.util.NcUtil; import org.jeecg.modules.util.SFTPUtil; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; @@ -150,7 +151,7 @@ public class RunProcessController extends JeecgController>> xlatAllList = bizWrfService.getNCByName(ncfile, "XLAT",0); + List>> xlatAllList = NcUtil.getNCByName(ncfile, "XLAT",0); percent = nowDay * 24 - 24 + xlatAllList.size() * 100 / sumHour; for (int i = 0; i < 4; i++) { progressList.set(i,100); @@ -170,7 +171,7 @@ public class RunProcessController extends JeecgController>> xlatAllList = bizWrfService.getNCByName(ncfile, "CO",0); + List>> xlatAllList = NcUtil.getNCByName(ncfile, "CO",0); percent = nowDay * 24 - 24 + xlatAllList.size() * 100 / (sumHour -24); for (int i = 0; i < 9; i++) { progressList.set(i,100); diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/util/NcUtil.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/util/NcUtil.java new file mode 100644 index 00000000..1dd9b66d --- /dev/null +++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/util/NcUtil.java @@ -0,0 +1,69 @@ +package org.jeecg.modules.util; + +import ucar.ma2.Array; +import ucar.ma2.Index; +import ucar.nc2.NetcdfFile; +import ucar.nc2.Variable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public final class NcUtil { + + public static List>> getNCByName(NetcdfFile ncfile, String name, int layer){ + Variable variable = ncfile.findVariable(name); + List>> resultAll = new ArrayList<>(); + // 读取nc数据到数组 + Array data = null; + try { + data = variable.read(); + } catch (IOException e) { + e.printStackTrace(); + } + + // 获取参数和索引,其中shape的前三个参数分别是时间、纬度、经度 + int[] shape = data.getShape(); + Index index = data.getIndex(); + + if(shape.length == 3){ + // 将三维数组降维,并用String数组提取数据 + // 按时间 + // 按维度 + for (int i = 0; i < shape[0]; i++) { + List> resultPiece = new ArrayList<>(); + for (int j = 0; j < shape[1]; j++) { + List strings = new ArrayList<>(); + // 按经度 + for (int k = 0; k < shape[2]; k++) { + // 按照对应索引获取数据并转换为string类型添加到数组中 + Double dval = Math.round(data.getFloat(index.set(i, j, k)) * 1000) /1000d; + strings.add(dval); + } + resultPiece.add(strings); + } + resultAll.add(resultPiece); + } + }else{ + // 将三维数组降维,并用String数组提取数据 + // 按时间 + // 按维度 + for (int i = 0; i < shape[0]; i++) { + List> resultPiece = new ArrayList<>(); + for (int j = 0; j < shape[2]; j++) { + List strings = new ArrayList<>(); + // 按经度 + for (int k = 0; k < shape[3]; k++) { + // 按照对应索引获取数据并转换为string类型添加到数组中 + Double dval = Math.round(data.getFloat(index.set(i,layer, j, k)) * 1000) /1000d; + strings.add(dval); + } + resultPiece.add(strings); + } + resultAll.add(resultPiece); + } + } + return resultAll; + } + +}