java netcdf精度_NetCDF 介绍

java netcdf精度_NetCDF 介绍附件文档 4NetCDFJava4 1 概述 Overview 参考网址 http www unidata ucar edu software netcdf java documentatio htmTheNetCDF Javalibraryi CDM ageneralizat O

附件文档:

4 NetCDF Java

4.1 概述(Overview)

参考网址:http://www.unidata.ucar.edu/software/netcdf-java/documentation.htm

The NetCDF-Java library implements a Common Data Model (CDM), a generalization of the NetCDF, OpenDAP and HDF5 data models. The library is a prototype for the NetCDF-4 project, which provides a C language API for the “data access layer” of the CDM, on top of the HDF5 file format. The NetCDF-Java library is a 100% Java framework for reading netCDF and other file formats into the CDM, as well as writing to the netCDF-3 file format. The NetCDF-Java library also implements NcML, which allows you to add metadata to CDM datasets, as well as to create virtual datasets through aggregation.

NetCDF-Java库实现了CDM(通用数据模型),CDM包括NetCDF,OpenDAP,HDF5数据模型,它是NetCDF-4项目的一个原型,NetCDF-4项目是紧跟HDF5文件格式后采用C语言作为CDM的的数据访问层API。在CDM中NetCDF-Java包是完全用Java架构来读取NetCDF和其他格式文件,和写netCDF-3格式文件一样。它还实现了NCML,允许你为CDM数据集添加元数据,和通过运算生成实际数据一样。

4.2 CDM(通用数据模型)

参考网址:http://www.unidata.ucar.edu/software/netcdf-java/CDM/index.html

Unidata’s Common Data Model (CDM) is an abstract data model for scientific datasets. It merges the netCDF, OPeNDAP, and HDF5 data models to create a common API for many types of scientific data. The NetCDF Java library is an implementation of the CDM which can read many file formats besides netCDF. We call these CDM files, a shorthand for files that can be read by the NetCDF Java library and accessed through the CDM data model.

Unidata社区的CDM(通用数据模型)是一个科学数据的抽象数据模型,它包括了NetCDF, OPeNDAP, HDF5数据模型并为不同类型的科学数据创建了一个通用的API。NetCDF Java库实现了CDM,CDM除了能读取NetCDF格式外还有其他类型文件,我们把这些CDM文件作为那些能被NetCDF Java库读取和访问的的文件的简称。

4.3 NetCDF-Java/CDM  Architecture

4.4 CDM-FILES

General: NetCDF, OPeNDAP, HDF5, NetCDF4, HDF4, HDF-EOS

Gridded: GRIB-1, GRIB-2, GEMPAK

Radar: NEXRAD 2&3, DORADE, CINRAD, Universal Format, TDWR

Point: BUFR, ASCII

Satellite: DMSP, GINI, McIDAS AREA

Misc: GTOPO, Lightning, etc

Others in development (partial):

AVHRR, GPCP, GACP, SRB, SSMI, HIRS (NCDC)

4.5 Data Access Layer Object Model

4.6 NetCDF举例

4.6.1 下载

下载地址: http://www.unidata.ucar.edu/software/netcdf-java/documentation.htm

在线API: http://www.unidata.ucar.edu/software/netcdf-java/v4.2/javadoc/index.html

目前最新版本为4.2.20

最新版本需要JDK6

4.6.2  生成NC文件

package my.demo;

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 CreateNetcdf {

@SuppressWarnings(“unchecked”)

public static void main(String[] args) throws Exception {

String filename = “testWrite.nc”;

NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(filename,true); // add

// dimensions

Dimension latDim = ncfile.addDimension(“lat”, 3);

Dimension lonDim = ncfile.addDimension(“lon”, 3); // define

// Variable

ArrayList dims = new ArrayList();

dims.add(latDim);

dims.add(lonDim);

ncfile.addVariable(“temperature”, DataType.DOUBLE, dims);

ncfile.addVariableAttribute(“temperature”, “units”, “K”); // add a

// 1D

// attribute

// of

// length

// 3

Array data = Array.factory(int.class, new int[] { 3 }, new int[] { 1,2,3 });

ncfile.addVariableAttribute(“temperature”, “scale”, data);

// add a string-valued variable: char svar(80)

Dimension svar_len = ncfile.addDimension(“svar_len”, 80);

dims = new ArrayList();

dims.add(svar_len);

ncfile.addVariable(“svar”, DataType.CHAR, dims);

// string array: char names(3, 80)

Dimension names = ncfile.addDimension(“names”, 3);

ArrayList dima = new ArrayList();

dima.add(names);

dima.add(svar_len);

ncfile.addVariable(“names”, DataType.CHAR, dima);

// how about a scalar variable?

ncfile.addVariable(“scalar”, DataType.DOUBLE, new ArrayList()); // add

// global

// attributes

ncfile.addGlobalAttribute(“yo”, “face”);

ncfile.addGlobalAttribute(“versionD”, new Double(1.2));

ncfile.addGlobalAttribute(“versionF”, new Float(1.2));

ncfile.addGlobalAttribute(“versionI”, new Integer(1));

ncfile.addGlobalAttribute(“versionS”, new Short((short) 2));

ncfile.addGlobalAttribute(“versionB”, new Byte((byte) 3)); // create

// the

// file

try {

ncfile.create();

} catch (IOException e) {

System.err.println(“ERROR creating file ” + ncfile.getLocation()+ “\n” + e);

}

}

}

会生成一个testWrite.nc文件,该文件不能直接打开,可以通过下载的包中netcdfUI-4.2.jar打开:

4.6.3 读取NC文件

package my.demo;

import java.io.IOException;

import java.util.List;

import ucar.nc2.Dimension;

import ucar.nc2.NetcdfFile;

import ucar.nc2.Variable;

public class ReadNetcdf {

public static void main(String[] args) {

String filename = “D:\\work\\netcdf\\testWrite.nc”;

NetcdfFile ncfile = null;

try {

ncfile = NetcdfFile.open(filename);

//read dimensions

List list =  ncfile.getDimensions();

for(Dimension d : list){

System.out.println(“name=”+d.getName()+” length=”+d.getLength());

}

//read variables

List variables = ncfile.getVariables();

System.out.println();

for(Variable v : variables){

System.out.println(“name=”+v.getName()+” NameAndDimension=”+v.getNameAndDimensions()+” ElementSize=”+v.getElementSize());

}

} catch (IOException ioe) {

} finally {

if (null != ncfile)

try {

ncfile.close();

} catch (IOException ioe) {

}

}

}

}

运行打印如下:

name=lat length=3

name=lon length=3

name=svar_len length=80

name=names length=3

name=temperature NameAndDimension=temperature(lat=3, lon=3) ElementSize=8

name=svar NameAndDimension=svar(svar_len=80) ElementSize=1

name=names NameAndDimension=names(names=3, svar_len=80) ElementSize=1

name=scalar NameAndDimension=scalar ElementSize=8

4.6.4 读写文件

package my.demo;

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 WriteDataToNetcdf {

/

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

NetcdfFileWriteable ncfile = NetcdfFileWriteable.openExisting(“D:\\work\\netcdf\\testWrite.nc”, true);

Dimension latDim = ncfile.getDimensions().get(0);

Dimension lonDim = ncfile.getDimensions().get(1);

ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength());

int i, j;

Index ima = A.getIndex();

for (i = 0; i 

for (j = 0; j 

A.setDouble(ima.set(i, j), (double) (2));

}

}

int[] origin = new int[2];

try {

ncfile.write(“temperature”, origin, A);

ncfile.close();

} catch (IOException e) {

System.err.println(“ERROR writing file”);

} catch (InvalidRangeException e) {

e.printStackTrace();

}

}

}

该方法为Variable temperature进行赋值,可以将修改后的testWrite.nc在netcdfUI-4.2.jar中查看:

double temperature(lat=3, lon=3);

:units = “K”;

:scale = 1, 2, 3; // int

data:

{

{2.0, 2.0, 2.0},

{2.0, 2.0, 2.0},

{2.0, 2.0, 2.0}

}

4.6.5 读取二维数据

通过v.read()可以读取数据:

Variable v = ncfile.findVariable(varName);

Variable v = ncfile.findVariable(varName);

Array data = v.read(“0:2:1, 0:19:1”);

package my.demo;

import java.io.IOException;

import ucar.ma2.Array;

import ucar.nc2.NCdumpW;

import ucar.nc2.NetcdfFile;

import ucar.nc2.Variable;

public class ReadData {

public static void main(String[] args) {

String filename = “D:\\work\\netcdf\\testWrite.nc”;

NetcdfFile ncfile = null;

try {

ncfile = NetcdfFile.open(filename);

//find variable

String variable = “temperature”;

Variable varBean = ncfile.findVariable(variable);

//Reading data from a Variable

if(null != varBean) {

Array all = varBean.read();

Array data = varBean.read(“0:2:1, 0:2:1”);

Array data1 = varBean.read(“0:2:2, 0:2:2”);

System.out.println(“读取所有:\n”+NCdumpW.printArray(all, variable, null));

System.out.println(“x轴从0到2 跨度为1 y轴从0到2 跨度为1:\n”+NCdumpW.printArray(data, variable, null));

System.out.println(“x轴从0到2 跨度为2 y轴从0到2 跨度为2:\n”+NCdumpW.printArray(data1, variable, null));

}

if(null != varBean) {

int[] origin = new int[] { 0 , 0};

int[] size = new int[] { 3,3};

Array data2D = varBean.read(origin, size);

System.out.println(“读取所有:\n”+NCdumpW.printArray(data2D, variable, null));

}

if(null != varBean) {

int[] origin = new int[] { 1 , 1};

int[] size = new int[] { 2,1};

Array data2D = varBean.read(origin, size);

System.out.println(“读取从第二行第二列开始为起点x数量为1,y数量为2:\n”+NCdumpW.printArray(data2D, variable, null));

}

System.out.println(“由此可得结论:维上的起点都以数组0开始,且阵列顺序在坐标中是从右至左\n如:int[] size = new int[] { 2,1},1代表x轴,2代表的是y轴….”);

} catch (Exception ioe) {

ioe.printStackTrace();

} finally {

if (null != ncfile)

try {

ncfile.close();

} catch (IOException ioe) {

}

}

}

}

打印结果如下:根据结果可以知道read(“0:2:2, 0:2:2”)和read(origin, size)的差别

读取所有:

temperature =

{

{0.0, 1.0, 2.0},

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0}

}

x轴从0到2 跨度为1 y轴从0到2 跨度为1:

temperature =

{

{0.0, 1.0, 2.0},

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0}

}

x轴从0到2 跨度为2 y轴从0到2 跨度为2:

temperature =

{

{0.0, 2.0},

{2.0, 4.0}

}

读取所有:

temperature =

{

{0.0, 1.0, 2.0},

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0}

}

读取从第二行第二列开始为起点x数量为1,y数量为2:

temperature =

{

{2.0},

{3.0}

}

由此可得结论:维上的起点都以数组0开始,且阵列顺序在坐标中是从右至左

如:int[] size = new int[] { 2,1},1代表x轴,2代表的是y轴….

4.6.6 多维NetCDF(三维)

4.6.6.1 创建

创建三维NetCDF文件:

package my.demo;

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 {

@SuppressWarnings(“unchecked”)

public static void main(String[] args) throws Exception {

String filename = “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);

}

}

}

4.6.6.2 写数据

package my.demo;

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(“D:\\work\\netcdf\\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();

}

}

}

对应的CDL格式如下:

double temperature(time=2, lat=3, lon=3);

:units = “K”;

:scale = 1, 2, 3; // int

data:

{

{

{0.0, 1.0, 2.0},

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0}

},

{

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0},

{3.0, 4.0, 5.0}

}

}

4.6.6.3 读数据

package my.demo;

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 = “D:\\work\\netcdf\\test3D.nc”;

NetcdfFile ncfile = null;

try {

ncfile = NetcdfFile.open(filename);

String variable = “temperature”;

Variable varBean = ncfile.findVariable(variable);

//read all data

if(null != varBean) {

Array all = varBean.read();

System.out.println(“读取所有:\n”+NCdumpW.printArray(all, variable, null));

}

if(null != varBean) {

int[] origin = new int[] { 0,1,1};

int[] size = new int[] { 2,2,2};

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[] { 0,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) {

}

}

}

}

打印:

读取所有:

temperature =

{

{

{0.0, 1.0, 2.0},

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0}

},

{

{1.0, 2.0, 3.0},

{2.0, 3.0, 4.0},

{3.0, 4.0, 5.0}

}

}

读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为2,2,2:

temperature =

{

{

{2.0, 3.0},

{3.0, 4.0}

},

{

{3.0, 4.0},

{4.0, 5.0}

}

}

读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为1,2,2并转为二维:

temperature =

{

{2.0, 3.0},

{3.0, 4.0}

}

4.7 NetCDF-NCML(Modifying existing files)

通过NCML标记语言可以对NetCDF文件修改

参考网址:http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Tutorial.html

4.8 NCML- Aggregation

通过NCML合并存在的多个NetCDF文件

参考网站:http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Aggregation.html

4.9 NetCDF-IOSP(I/O Service Provide)

参考网址:http://www.unidata.ucar.edu/software/netcdf-java/tutorial/IOSPoverview.html

4.9.1 Overview

A client uses the NetcdfFile, NetcdfDataset, or one of the Scientific Feature Type APIs to read data from a CDM file. These provide a rich and sometimes complicated API to the client. Behind the scenes, when any of these APIs actually read from a dataset, however, they use a very much simpler interface, the I/O Service Provider or IOSP for short. The Netcdf Java library has many implementations of this interface, one for each different file format that it knows how to read. This design pattern is called a Service Provider.

IOSPs are managed by the NetcdfFile class. When a client requests a dataset (by calling NetcdfFile.open), the file is opened as a ucar.unidata.io.RandomAccessFile (an improved version of java.io.RandomAccessFile). Each registered IOSP is then asked “is this your file?” by calling isValidFile( ucar.unidata.io.RandomAccessFile). The first one that returns true claims it. When you implement isValidFile() in your IOSP, it must be very fast and accurate.

4.9.2 IOServiceProvider

package ucar.nc2.iosp;

import ucar.ma2.Section;

import ucar.ma2.InvalidRangeException;

import ucar.ma2.StructureDataIterator;

import ucar.nc2.ParsedSectionSpec;

import ucar.nc2.Structure;

import java.io.IOException;

import java.nio.channels.WritableByteChannel;

/

* This is the service provider interface for the low-level I/O access classes (read only).

* This is only used by service implementors.

*

* The NetcdfFile class manages all registered IOServiceProvider classes.

* When NetcdfFile.open() is called:

 the file is opened as a ucar.unidata.io.RandomAccessFile;

 the file is handed to the isValidFile() method of each registered

* IOServiceProvider class (until one returns true, which means it can read the file).

 the open() method on the resulting IOServiceProvider class is handed the file.

*

* @see ucar.nc2.NetcdfFile#registerIOProvider(Class) ;

*

* @author caron

*/

public interface IOServiceProvider {

/

* Check if this is a valid file for this IOServiceProvider.

* You must make this method thread safe, ie dont keep any state.

*

* @param raf RandomAccessFile

* @return true if valid.

* @throws java.io.IOException if read error

*/

public boolean isValidFile( ucar.unidata.io.RandomAccessFile raf) throws IOException;

}

其他方法见官网介绍或API文档。

4.9.3 AbstractIOServiceProvider

Your implementataion class should extend ucar.nc2.iosp.AbstractIOServiceProvider. This provides default implementation of some of the methods, so minimally, you only have to implement 4 methods:

public class MyIosp extends ucar.nc2.iosp.AbstractIOServiceProvider {

1)  public boolean isValidFile(RandomAccessFile raf) throws IOException {}

2)  public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {}

3)  public Array readData(Variable v2, Section wantSection) throws IOException, InvalidRangeException {}

4)  public void close() throws IOException {}

5)  public String getFileTypeId() {}

5)  public String getFileTypeVersion() {}

5)  public String getFileTypeDescription();

}

4.9.4 IOSP-Example

通过IOSP对数据处理生成NetCDF文件已经读取NetCDF数据例子:

参考网址:

http://www.unidata.ucar.edu/software/netcdf-java/tutorial/index.html

图中的例子为雷电数据,卫星数据,雷达数据相关

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216290.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午12:10
下一篇 2026年3月18日 下午12:10


相关推荐

  • springboot|springboot连接mysql数据库

    springboot|springboot连接mysql数据库javaDEMO本网站记录了最全的各种JavaDEMO,保证下载,复制就是可用的,包括基础的,集合的,spring的,Mybatis的等等各种,助力你从菜鸟到大牛,记得收藏哦~~https://www.javastudy.cloud配置springboot连接mysql数据库主要分以下几步:添加gradle/maven依赖配置数据库url,用户名,密码等等在Cont…

    2022年6月25日
    33
  • springboot 连接sftp服务器

    springboot 连接sftp服务器1 ftp 和 sftp 主要区别 FTP 是一种文件传输协议 一般是为了方便数据共享的 包括一个 FTP 服务器和多个 FTP 客户端 FTP 客户端通过 FTP 协议在服务器上下载资源 而 SFTP 协议是在 FTP 的基础上对数据进行加密 使得传输的数据相对来说更安全 但是这种安全是以牺牲效率为代价的 也就是说 SFTP 的传输效率比 FTP 要低 不过现实使用当中 没有发现多大差别 个人肤浅的认为就是 一 FTP 要安装 S

    2026年3月26日
    2
  • 本地计算机上的task scheduler服务启动后停止_task

    本地计算机上的task scheduler服务启动后停止_task1.如果对应服务依赖都正常情况下,请参考下面解决方案进入注册表(cmd–>regedit,依次找到HKEY_LOCAL_MACHINE\HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\RPC\INTERNET,删除INTERNET,重启服务器注:删除前请导出备份…

    2022年10月11日
    5
  • Python爬虫框架Scrapy获得定向打击批量招聘信息

    Python爬虫框架Scrapy获得定向打击批量招聘信息

    2022年1月13日
    57
  • weka 基础

    weka 基础1 简介 nbsp WEKA 的全名是怀卡托智能分析环境 WaikatoEnvir 它的源代码可通过 http www cs waikato ac nz ml weka 得到 同时 weka 也是新西兰的一种鸟名 而 WEKA 的主要开发者来自新西兰 nbsp 详见 nbsp http www china pub com computers commo

    2026年3月17日
    2
  • 高性能风控数据平台设计

    高性能风控数据平台设计各行各业 各个领域 各个渠道 都需要有一系列的完整的风险控制 以保证事情向好的方向发展 而免受不可预估的经济和财产损失而绰手不及 这时候一套完备的风控系统应运而生 以解决实际在生产业务中的各种难题 作为事物的主体 可以采取各种措施和方法 消灭或减少风险事件发生的各种可能性 或减少风险事件发生时造成的损失 这就是风险控制 与金钱打交道的金融行业 风险控制更加重要 金融的本质就是风控 因此 可以看到

    2026年2月1日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号