NC是气象领域数据的标准格式之一。

能够更好的存储格点数据。

下面为测试NC文件的读写。

git:https://git.oschina.net/ipnunu/nctest

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>nc</groupId>
<artifactId>nctest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>nctest</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- 设定主仓库,按设定顺序进行查找。 -->
<repositories>
<repository>
<id>jeesite-repos</id>
<name>Jeesite Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories> <dependencies>
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.2.20</version>
</dependency>
<!--
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf4</artifactId>
<version>4.5.5</version>
</dependency>
-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>com.springsource.org.apache.commons.lang</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package nc.test.netCDF3;

import java.io.IOException;
import ucar.ma2.Array;
import ucar.nc2.NCdumpW;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable; public class Read3DNetCDF {
public static void main(String[] args) {
String filename = "c:\\nunu\\nc\\HNDW1KM-north-2016090204-70m.nc";
NetcdfFile ncfile = null;
try {
ncfile = NetcdfFile.open(filename);
String variable = "wd70";
Variable varBean = ncfile.findVariable(variable);
// read all data
if (null != varBean) {
Array all = varBean.read();
System.out.println(all.getSize());
//System.out.println("读取所有:\n" + NCdumpW.printArray(all, variable, null));
}
if (null != varBean) {
int[] origin = new int[] { 2, 1, 1 };
int[] size = new int[] { 50, 50, 50 };
Array data2D = varBean.read(origin, size);
System.out.println(
"读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为2,2,2:\n" + NCdumpW.printArray(data2D, variable, null));
}
// invoke reduce trans 3D to 2D
if (null != varBean) {
int[] origin = new int[] { 12, 1, 1 };
int[] size = new int[] { 1, 2, 2 };
Array data2D = varBean.read(origin, size).reduce().reduce();
System.out.println(
"读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为1,2,2并转为二维:\n" + NCdumpW.printArray(data2D, variable, null));
}
} catch (Exception ioe) {
ioe.printStackTrace();
} finally {
if (null != ncfile)
try {
ncfile.close();
} catch (IOException ioe) {
}
}
}
}
package nc.test.netCDF3;

import java.io.IOException;
import java.util.ArrayList;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFileWriteable; public class Create3DNetCDF { public static void main(String[] args) throws Exception {
String filename = "c:\\nunu\\nc\\test3D.nc";
NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(filename, true); // add
Dimension timeDim = ncfile.addDimension("time", 2);
Dimension latDim = ncfile.addDimension("lat", 3);
Dimension lonDim = ncfile.addDimension("lon", 3); // define
ArrayList dims = new ArrayList();
dims.add(timeDim);
dims.add(latDim);
dims.add(lonDim);
ncfile.addVariable("temperature", DataType.DOUBLE, dims);
ncfile.addVariableAttribute("temperature", "units", "K"); // add a
Array data = Array.factory(int.class, new int[] { 3 }, new int[] { 1, 2, 3 });
ncfile.addVariableAttribute("temperature", "scale", data);
try {
ncfile.create();
} catch (IOException e) {
System.err.println("ERROR creating file " + ncfile.getLocation() + "\n" + e);
}
}
}
package nc.test.netCDF3;

import java.io.IOException;
import ucar.ma2.ArrayDouble;
import ucar.ma2.Index;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFileWriteable; public class Write3DNetCDF {
public static void main(String[] args) throws IOException {
NetcdfFileWriteable ncfile = NetcdfFileWriteable.openExisting("c:\\nunu\\nc\\test3D.nc", true);
Dimension timeDim = ncfile.getDimensions().get(0);
Dimension latDim = ncfile.getDimensions().get(1);
Dimension lonDim = ncfile.getDimensions().get(2);
ArrayDouble A = new ArrayDouble.D3(timeDim.getLength(), latDim.getLength(), lonDim.getLength());
int k, i, j;
Index ima = A.getIndex();
for (k = 0; k < timeDim.getLength(); k++) {
for (i = 0; i < latDim.getLength(); i++) {
for (j = 0; j < lonDim.getLength(); j++) {
A.setDouble(ima.set(k, i, j), (double) (k + i + j));
}
}
}
int[] origin = new int[3];
try {
ncfile.write("temperature", origin, A);
ncfile.close();
} catch (IOException e) {
System.err.println("ERROR writing file");
} catch (InvalidRangeException e) {
e.printStackTrace();
}
}
}

联系方式

QQ:398269786

个人微信公共号:pnunu

最新文章

  1. 【原创】开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器
  2. JDK6环境下升级项目到springframework4.x和tomcat7.x
  3. 【转】MongoDB安全配置
  4. Linux_linux中profile、bashrc、bash_profile之间的区别和联系(转)
  5. [SoapUI] SoapUI JDBC REST 连接 Netezza
  6. linux与windows的文本文件之间的转换
  7. 使用SAE部署Flask,使用非SAE flask版本和第三方依赖包的方法
  8. Oracle Spatial-元数据及SDO_GEOMETRY
  9. IOS开发之路三(XML解析之GDataXML的使用)
  10. java代码开发完成后,代码走查规范
  11. 蓝桥杯比赛java 练习《立方变自身》
  12. 智能指针std::weak_ptr
  13. E - Reachability from the Capital
  14. 对mysql数据库中字段为空的处理
  15. Cocos Creator两个类相互引用(调用)
  16. jQuery警告/确认/提示弹出对话框效果(替换传统JavaScript下的提示框)
  17. 为什么会有object这么一个根基类
  18. 读取.properties配置文件并保存到另一个.properties文件内
  19. python学习笔记2---函数
  20. ajax发送js类型的数据

热门文章

  1. ipython结合virtualenv使用
  2. 自己动手实现浏览器,21天自制chromium:起手篇
  3. 通过BSSID和无线流量传输后门Payload
  4. mysql中游标在存储过程中的具体使用方法
  5. ADO.NET 对数据操作 以及如何通过C# 事务批量导入数据
  6. FragmentSharedFabTransition
  7. CustomView
  8. mtk机型的一次救砖经历
  9. ribbon负载均衡进行服务消费
  10. 0 lrwxrwxrwx. 1 root root 13 Nov 20 12:44 scala -&gt; scala-2.12.4