当前位置: 首页 > 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.lryc.cn/news/594866.html

相关文章:

  • numpy库的基础知识
  • 用Dify构建气象智能体:从0到1搭建AI工作流实战指南
  • React-useEffect的闭包陷阱(stale closure)
  • react 录音功能
  • 《Linux 环境下 Nginx 多站点综合实践:域名解析、访问控制与 HTTPS 加密部署》​
  • 大模型——Prompt 优化还是模型微调
  • Ubuntu 22.04 安装 Docker (安装包形式)
  • WPF 项目设置应用程序图标和设置程序集图标
  • 移星科技 modbus-tcp 转 modbus-Rtu模块
  • (数据结构)线性表(中):SLIst单链表
  • tcpdump 命令解析(随手记)
  • IOPaint+CPolar:零公网IP也能搭建专属AI图像编辑平台
  • 高级技术【Java】【反射】【注解】【动态代理】
  • 复习博客:JVM
  • 【Project】ELK 7.17.16 日志分析系统部署
  • 阿里云平台使用的ack创建的pod与服务器中的MongoDB不在同一网段如何解决
  • 【图像处理基石】什么是相机的内外参数?
  • 单表查询-分页提前获取数据
  • 自动化与安全 - 将 Terraform 集成到 CI/CD
  • 安装pytorch(cpu版)
  • 电科金仓2025发布会,国产数据库的AI融合进化与智领未来
  • 【Lucene】SimScorer
  • 【Spring AI】Advisors API—顾问(即拦截器)
  • 轨迹优化 | 基于边界中间值问题(BIVP)的路径平滑求解器(附C++/Python仿真)
  • 6.String、StringBuffer、StringBuilder区别及使用场景
  • C++学习笔记(六:数组)
  • AI Agent与MCP Service技术进展结构化分析报告(2025Q2)
  • 解决win10下Vmware虚拟机在笔记本睡眠唤醒后ssh连接不上的问题
  • 项目研发进度安排
  • 音视频学习(四十二):H264帧间压缩技术