当前位置: 首页 > news >正文

java解析nc气象数据

1.1pom.xml
        <dependency><groupId>edu.ucar</groupId><artifactId>netcdfAll</artifactId><version>5.4.1</version></dependency>
1.2 netcdf使用
    /** @param  type 0 ,1, 2  wind 1 or 2  其他 0 .* @return Map* */public Map initJson(String ncFileFullPath, String name, long timeIndex, byte type) {// 打开 NetCDF 文件try (NetcdfFile ncFile = NetcdfFile.open(ncFileFullPath)) {//遍历nc文件中的每个变量,输出json数据// 读取其中的变量Variable uVar = ncFile.findVariable(name);Variable latVar = ncFile.findVariable("lat");Variable lonVar = ncFile.findVariable("lon");Variable timeVar = ncFile.findVariable("time");Array latdata = latVar.read();Double[] latList = Arrays.stream(latdata.toString().split(" ")).map(s -> Double.parseDouble(s)).toArray(Double[]::new);Array londata = lonVar.read();Double[] lonList = Arrays.stream(londata.toString().split(" ")).map(s -> Double.parseDouble(s)).toArray(Double[]::new);// 获取变量的维度int[] shape = uVar.getShape();int[] stepshape = {1, shape[1], shape[2]};int[] origin = {(int) timeIndex, 0, 0};Section section = new Section(origin, stepshape);Array data = uVar.read(section);
//            ArrayList<Number> list = new ArrayList<>();
//            for(String s : data.toString().split(" ")){
//                list.add(Double.parseDouble(s));
//            }Double[] list = Arrays.stream(data.toString().split(" ")).map(s -> new BigDecimal(s).setScale(2, RoundingMode.HALF_UP).doubleValue()).toArray(Double[]::new);Map hashMap = new HashMap();Map headerMap = new HashMap();List<Attribute> attributes = timeVar.attributes().getAttributes();for (Attribute stu : attributes) {headerMap.put(stu.getName(), Arrays.asList(stu.getValues().toString()));}Array tempTime = timeVar.read();headerMap.put("refTime", initrefTime(tempTime.getInt((int) timeIndex), headerMap.get("units").toString()));headerMap.put("ny", shape[1]);headerMap.put("nx", shape[2]);headerMap.put("lo1", lonList[0]);headerMap.put("lo2", lonList[lonList.length - 1]);headerMap.put("la1", latList[latList.length - 1]);headerMap.put("la2", latList[0]);headerMap.put("dx", new BigDecimal(Double.toString((lonList[lonList.length - 1] - lonList[0]) / shape[2])).setScale(3, RoundingMode.HALF_UP).doubleValue());headerMap.put("dy", new BigDecimal(Double.toString((latList[latList.length - 1] - latList[0]) / shape[1])).setScale(3, RoundingMode.HALF_UP).doubleValue());if (type == 1) {headerMap.put("COMMENT", "U");headerMap.put("parameterCategory", 2);headerMap.put("parameterNumber", 2);} else if (type == 2) {headerMap.put("COMMENT", "V");headerMap.put("parameterCategory", 2);headerMap.put("parameterNumber", 3);}hashMap.put("data", list);hashMap.put("header", headerMap);return hashMap;//          // 使用 Gson 将数据转换为 JSON 格式
//            Gson gson = new GsonBuilder()
//                    .serializeSpecialFloatingPointValues() // 允许序列化NaN和Infinity
//                    .create();
//            String json = gson.toJson(arrayList);
//          //输出 JSON 数据到控制台
//            System.out.println(json.toString());//String jsonfile =it + "1.json";
//          // 将 JSON 数据写入到文件
//                try (FileWriter writer = new FileWriter(jsonfile)) {
//                    writer.write(json);
//                }} catch (Exception e) {e.printStackTrace();}return null;}

重点讲解 timeIndex 参数  nc 文件中一个文件包含多个日期数据,获取变量的时间维度范围是整数,而 timeIndex是根据文件开始时间将时间转为时间维度index后的数值。

核心查询代码为         

            int[] stepshape = {1, shape[1], shape[2]};
int[] origin = {(int) timeIndex, 0, 0};
Section section = new Section(origin, stepshape);

表示时间维度开始 timeIndex  步长为1,经度纬度的维度开始0,步长为所有。

http://www.dtcms.com/a/290297.html

相关文章:

  • HOT100——图篇Leetcode207. 课程表
  • Trae开发uni-app+Vue3+TS项目飘红踩坑
  • Cosmos
  • PostgreSQL 终端命令详解及实际应用案例
  • 【LINUX操作系统】搭建web网络服务器
  • Softhub软件下载站实战开发(二十):Docker部署全攻略
  • 前后端分离项目进阶1---前端
  • 对称加密技术详解:原理、算法与实际应用
  • 在本地WSL中的CentOS 7子系统中部署Ewomail邮件服务器
  • 面试150 全排列
  • Claude Code 启动提示 Note: Claude Code might not be available in your country. 解决
  • mac安装node的步骤
  • 线程池与ThreadPoolExecutor源码解析(上)
  • Consumer<T>
  • Chatbox AI使用指南与功能详解:打造你的专属智能工作平台
  • Zabbix企业级分布式监控
  • OpenCV学习(二)-二维、三维识别
  • 技术演进中的开发沉思-41 MFC系列:定制 AppWizard
  • 【爬虫】06 - 自动化爬虫selenium
  • Zabbix 企业级分布式监控系统深度解析
  • 计算机发展史:人工智能时代的智能变革与无限可能
  • Laravel 后台登录 403 Forbidden 错误深度解决方案-优雅草卓伊凡|泡泡龙
  • NVM的安装使用:nvm管理多个 Node.js 版本的工具
  • gRPC深度解析:原理、实践与性能优化指南
  • 将 RustFS 用作 GitLab 对象存储后端
  • uniapp使用uni-ui怎么修改默认的css样式比如多选框及样式覆盖小程序/安卓/ios兼容问题
  • 测量误差溯源:系统误差与随机误差的数学建模与分离方法
  • 大模型——Prompt 优化还是模型微调
  • 【PTA数据结构 | C语言版】求单源最短路的Dijkstra算法
  • AI学习--本地部署ollama