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

NC文件的处理【netcdf】的更多相关文章

  1. 制作nc文件(Matlab)

    首先看一个nc文件中包含哪些部分,例如一个标准的 FVCOM 输入文件 wind.nc: netcdf wind { dimensions: nele = 36858 ; node = 18718 ; ...

  2. 基于GDAL库,读取.nc文件(以海洋表温数据为例)C++版

    对于做海洋数据处理的同学,会经常遇到nc格式的文件,nc文件的格式全称是NetCDF,具体的详细解释请查询官网[https://www.unidata.ucar.edu/software/netcdf ...

  3. python之工作举例:通过复制NC文件来造数据

    # 通过对NC文件复制来造数据 import os, shutil # 遍历的根目录 root_dir = "D:\\test_data\\DISASTER\\" # 获取NC文件 ...

  4. java读取nc文件的问题,前端ajax 发送参数进行交互的实例

    1.问题背景: 需要解析nc文件的数据源,获取一个三维数据,并计算器开发值. java 后台处理: 定以一个实例来接收解析的数据并返回给前端. package cn.edu.shou.domain; ...

  5. Windows下nc文件传输

    起初用的一下命令: 接收端:nc –n –l –p port –vv > xxx 发送端:nc –n ip port < yyy 但是发现数据传输完成后,不会自动断开连接,要手动的断开,这 ...

  6. nc 文件的nan识别

    表现形式:print()结果为 --      打印type为numpy.ma.core.MaskedConstant 使用 if type(x) == np.ma.core.MaskedConsta ...

  7. NetCDF 入门

    一.概述  NetCDF全称为network Common Data Format,中文译法为“网络通用数据格式”,对程序员来说,它和zip.jpeg.bmp文件格式类似,都是一种文件格式的标准.ne ...

  8. netcdf入门(转)

    一.概述  NetCDF全称为network Common Data Format,中文译法为“网络通用数据格式”,对程序员来说,它和zip.jpeg.bmp文件格式类似,都是一种文件格式的标准.ne ...

  9. 基于GDAL库,读取海洋风场数据(.nc格式)c++版

    经过这一段时间的对海洋数据的处理,接触了大量的与海洋相关的数据,例如海洋地形.海洋表面温度.盐度.湿度.云场.风场等数据,除了地形数据是grd格式外,其他的都是nc格式的数据.本文将以海洋风场数据为例 ...

随机推荐

  1. HDU1800 Flying to the Mars 【贪心】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. SolidEdge如何在零件上写字 如何绘制文字

    在草图状态下,插入-文字轮廓   可以按这两个按钮调节文字的大小和位置   之后你可以通过长出或除料把文字凸起或者凹下去   如果你要制作路径文字(比如环形文字),则先绘制一条圆或一段圆弧,并设为构造 ...

  3. Python基础二--基本控制语句

    基本接触每一种语言,都须要做的:1.print 一个"Hello world!" 2.了解主要的数据类型 3.学习控制语句. 当我们学习控制语句,一般都离不开if,for ,whi ...

  4. linux输入子系统(5) - 学习框架

    注:本系列转自: http://www.ourunix.org/post/290.html input子系统学习系列文章,是我在实际开发过程中遇到也是必须啃下去的第一个Linux驱动,所以有必要记载下 ...

  5. Javascript对象的技巧和陷阱

    创建对象的3种方法 方法1 直接创建 var obj = { name: "mike", age: 10 } 方法2 用new创建 var ob = new Date(); 方法3 ...

  6. 2017 Multi-University Training Contest - Team 1 (5/12)

    官方题解 1001. Add More Zero #pragma comment(linker, "/STACK:1024000000,1024000000") #include& ...

  7. 4408: [Fjoi 2016]神秘数

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 452  Solved: 273 [Submit][Stat ...

  8. 减肥 day1

    今天是我减肥第一天,现在体重是147斤, 早晨吃了一碗面,喝了一碗奶,中午吃了一个apple. 6点钟去打篮球,晚上去食堂稍微吃一点东西.

  9. XMU C语言程序设计实践(4)

    以下实验二选一. 1.使用队列实现迷宫算法,找到最短路径. 2.实现顺序队列和链队列的所有基本操作,InitQueue(&Q):DestroyQueue(&Q):ClearQueue( ...

  10. HDU1495 非常可乐 —— BFS + 模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 非常可乐 Time Limit: 2000/1000 MS (Java/Others)    M ...