NC文件的处理【netcdf】

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

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

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

 

码云地址 :https://gitee.com/pnunu/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>


读nc :

 

Read3DNetCDF.java

 

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) {
                }
        }
    }
}

 

 

Create3DNetCDF

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);
        }
    }
}


Write3DNetCDF

 

 

 

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();
        }
    }
}

 

 

 

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值