首页 > 代码库 > NC文件的处理【netcdf】
NC文件的处理【netcdf】
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 = http://www.mamicode.com/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
NC文件的处理【netcdf】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。