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 = 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】的更多相关文章
- 制作nc文件(Matlab)
首先看一个nc文件中包含哪些部分,例如一个标准的 FVCOM 输入文件 wind.nc: netcdf wind { dimensions: nele = 36858 ; node = 18718 ; ...
- 基于GDAL库,读取.nc文件(以海洋表温数据为例)C++版
对于做海洋数据处理的同学,会经常遇到nc格式的文件,nc文件的格式全称是NetCDF,具体的详细解释请查询官网[https://www.unidata.ucar.edu/software/netcdf ...
- python之工作举例:通过复制NC文件来造数据
# 通过对NC文件复制来造数据 import os, shutil # 遍历的根目录 root_dir = "D:\\test_data\\DISASTER\\" # 获取NC文件 ...
- java读取nc文件的问题,前端ajax 发送参数进行交互的实例
1.问题背景: 需要解析nc文件的数据源,获取一个三维数据,并计算器开发值. java 后台处理: 定以一个实例来接收解析的数据并返回给前端. package cn.edu.shou.domain; ...
- Windows下nc文件传输
起初用的一下命令: 接收端:nc –n –l –p port –vv > xxx 发送端:nc –n ip port < yyy 但是发现数据传输完成后,不会自动断开连接,要手动的断开,这 ...
- nc 文件的nan识别
表现形式:print()结果为 -- 打印type为numpy.ma.core.MaskedConstant 使用 if type(x) == np.ma.core.MaskedConsta ...
- NetCDF 入门
一.概述 NetCDF全称为network Common Data Format,中文译法为“网络通用数据格式”,对程序员来说,它和zip.jpeg.bmp文件格式类似,都是一种文件格式的标准.ne ...
- netcdf入门(转)
一.概述 NetCDF全称为network Common Data Format,中文译法为“网络通用数据格式”,对程序员来说,它和zip.jpeg.bmp文件格式类似,都是一种文件格式的标准.ne ...
- 基于GDAL库,读取海洋风场数据(.nc格式)c++版
经过这一段时间的对海洋数据的处理,接触了大量的与海洋相关的数据,例如海洋地形.海洋表面温度.盐度.湿度.云场.风场等数据,除了地形数据是grd格式外,其他的都是nc格式的数据.本文将以海洋风场数据为例 ...
随机推荐
- mysql修改删除列,删除有外键依赖的列
–重命名表rename table t_softwareport to software_port; –建立外键alter table software_port add constraint fk_ ...
- SolidWorks如何绘制抽壳零件
1 绘制一个零件,点击抽壳 2 你可以一个一个面选,也可以直接选中一个零件,对他的所有面都薄壳处理(右击弹出菜单选择确定即可) 3 可以用剖视图检查是否抽壳成功 4 对于复杂的零件,一个一 ...
- react 使用 moment 进行 日期格式化
在react中使用得先导入: import moment from 'moment'; 调用: npm install moment var moment = require('moment'); m ...
- socket 网络编程高速入门(一)教你编写基于UDP/TCP的服务(client)通信
由于UNIX和Win的socket大同小异,为了方便和大众化,这里先介绍Winsock编程. socket 网络编程的难点在入门的时候就是对基本函数的了解和使用,由于这些函数的结构往往比較复杂,參数大 ...
- APACHE局域网配置域名访问
/** * * @email 514320008@qq.com * @author jshaibozhong * */ 1,打开APACHE的目录 \Apache2\conf\extra\httpd ...
- Python基础二--基本控制语句
基本接触每一种语言,都须要做的:1.print 一个"Hello world!" 2.了解主要的数据类型 3.学习控制语句. 当我们学习控制语句,一般都离不开if,for ,whi ...
- C开发人员眼中的SICP学习
谈谈自己看SICP的一些体会 第一章 构造过程抽象 这一章事实上和C语言全然等价, 不打算深入学习LISP的能够高速略过. 思想上没有太多新的东西. 这一章最核心的价值就是以下3句话, 理解了这一章 ...
- JSP与HTML的差别
HTML(Hypertext Markup Language)文本标记语言,它是静态页面,和JavaScript一样解释性语言,为什么说是解释性 语言呢?由于.仅仅要你有一个浏览器那么它就能够正常显示 ...
- iOS 各种编译错误汇总
1.error: macro names must be identifiers YourProject_prefix.pch 原因: 因为你弄脏了预处理器宏,在它处于<Multiple Val ...
- Iterator && Iterable Collection && Map
Java集合类库将集合的接口与实现分离.同样的接口,可以有不同的实现. Java集合类的基本接口是Collection接口.而Collection接口必须实现Iterable接口. 以下图表示集合框架 ...