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,步长为所有。