NC文件的处理【netcdf】
2021-06-09 01:02
标签:nap art eating exist static array 开始 factory www. NC是气象领域数据的标准格式之一。 能够更好的存储格点数据。 下面为测试NC文件的读写。 git:https://git.oschina.net/ipnunu/nctest pom.xml 联系方式 QQ:398269786 个人微信公共号:pnunu NC文件的处理【netcdf】 标签:nap art eating exist static array 开始 factory www. 原文地址:http://www.cnblogs.com/acmwangpeng/p/7305002.htmlproject 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.0modelVersion>
groupId>ncgroupId>
artifactId>nctestartifactId>
version>0.0.1-SNAPSHOTversion>
packaging>jarpackaging>
name>nctestname>
url>http://maven.apache.orgurl>
properties>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
repositories>
repository>
id>jeesite-reposid>
name>Jeesite Repositoryname>
url>http://maven.aliyun.com/nexus/content/groups/publicurl>
repository>
repositories>
dependencies>
dependency>
groupId>edu.ucargroupId>
artifactId>netcdfartifactId>
version>4.2.20version>
dependency>
dependency>
groupId>org.apache.commonsgroupId>
artifactId>com.springsource.org.apache.commons.langartifactId>
version>2.6.0version>
dependency>
dependency>
groupId>org.apache.directory.studiogroupId>
artifactId>org.apache.commons.ioartifactId>
version>2.4version>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>4.11version>
scope>testscope>
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 ) {
for (i = 0; i ) {
for (j = 0; 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();
}
}
}
上一篇:js转base64(数字)