- 1 生成nc 格式的文件
- 1.1 代码
- 2 简单读取nc 格式的文件
- 1 首先是生成的nc格式文件使用panoply 软件打开的数据是:
第一个图解释: 以上就是使用代码生成的nc格式的文件,使用panoply 软件打 开之后,我们就看到了具体的信息; 一共有两个维度,一个是X ,一个是Y ,这两个维度的值代表 数组的长度;或者这样理解,一个维度就是一个数组,维度 的长度 就是数组长度,就是这个数组里面可以存放多少数据;
第二个图解释: 这个nc文件里面存放的使一个二维数组,里面有具体的值; 有多少个值,就是维度相乘 ,因为当前有两个维度X和Y; 通过第一个图可知X的值为6,y的值为12 ,所以,这个nc文件 里面会存放 6*12=72 个数据;通过第二个图可知,一共72个 数据; X的值为6,代表x这个数组,里面可以存放6个数据,那么存放 哪6个数据,从第二个图可以看到,存放的是[0,1,2,3,4,5]; y的值为12,代表y这个数组,里面可以存放12 个数据,存放哪 12 个数据,从第二个图可以看到,存放的是 [0,1,2,3,4,5,6,7,8,9,10,11] 二维数组里面的具体值是第二个图里面的值
以上的维度的设置,具体值的设置,都是代码里面设置的
1.1 代码package com.jing.common;
import ucar.nc2.Dimension;
import ucar.ma2.*;
import ucar.nc2.NetcdfFileWriter;
import ucar.nc2.Variable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//@RestController
//@RequestMapping("/")
public class Simple_xy_wr {
public static void main(String args[]) {
// 创建一个二维的数据
final int NX = 6; // x 轴 设置一个数组里面有几个值
final int NY = 12; // y 轴 设置一个数组里面有几个值
String filename = "simple_xy.nc"; // nc 文件名称
// NetcdfFileWriter 这个类是专门创建 nc 文件的
NetcdfFileWriter dataFile = null;
try {
// 参数1 : 文件格式 参数2:文件名称
// 先将输出nc文件的基本信息 写入
dataFile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, filename);
// Create netCDF dimensions, 创建 nc 文件的维度
Dimension xDim = dataFile.addDimension(null, "x", NX); // 维度X轴 对象 值为: x = 6;
Dimension yDim = dataFile.addDimension(null, "y", NY); // 维度Y轴 y = 12;
// define dimensions 将创建的维度对象放到 list里面
List dims = new ArrayList<>();
dims.add(xDim);
dims.add(yDim);
// Define a netCDF variable. The type of the variable in this case
// is ncInt (32-bit integer). 创建nc文件里面的变量
// 就是根据维度 创建变量 dataVariable 值为 int data(x=6, y=12);nr
Variable dataVariable = dataFile.addVariable(null, "data", DataType.INT, dims);
// create the file 创建文件
dataFile.create();
// This is the data array we will write. It will just be filled
// with a progression of numbers for this example.
// 这是我们将要写入的数据数组。在本例中,它将被填入一个连续的数字。
ArrayInt.D2 dataOut = new ArrayInt.D2(xDim.getLength(), yDim.getLength(),false);
// Create some pretend data. If this wasn't an example program, we
// would have some real data to write, for example, model output.
// 创建一些虚拟数据。如果这不是一个示例程序,我们将编写一些真实的数据,例如,模型输出。
int i, j;
// x =6 y=12 值=i * NY + j
for (i = 0; i < xDim.getLength(); i++) {
for (j = 0; j < yDim.getLength(); j++) {
dataOut.set(i, j, i * NY + j+5);
}
}
// Write the pretend data to the file. Although netCDF supports
// reading and writing subsets of data, in this case we write all
// the data in one operation.
//将虚拟数据写入文件。虽然netCDF支持//读取和写入数据的子集,但在本例中,我们在一个操作中写入所有//数据。
dataFile.write(dataVariable, dataOut);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidRangeException e) {
e.printStackTrace();
} finally {
if (null != dataFile)
try {
dataFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
System.out.println("*** SUCCESS writing example file simple_xy.nc!");
}
}
2 简单读取nc 格式的文件
package com.jing.common;
import ucar.ma2.ArrayInt;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import java.io.IOException;
import java.util.List;
//@RestController
//@RequestMapping("")
public class Simple_xy_rd {
public static void main(String args[]) throws IOException {
final int NX = 6; // x 轴 数组长度
final int NY = 12; // y 轴 数组长度
// This is the array we will read. 这就是我们将要读取的数组。
int[][] dataIn = new int[NX][NY];
// Open the file. The ReadOnly parameter tells netCDF we want
// read-only access to the file.
// 打开文件。ReadOnly参数告诉netCDF我们希望对文件进行只读访问。
NetcdfFile dataFile = null;
String filename = "D:\格点数据\nc\simple_xy.nc";
// Open the file.
try {
dataFile = NetcdfFile.open(filename, null);
// Retrieve the variable named "data" 检索名为“data”的变量
// int data(x=6, y=12);
Variable dataVar = dataFile.findVariable("data");
if (dataVar == null) {
System.out.println("Cant find Variable data");
return;
}
// Read all the values from the "data" variable into memory.
// 将“data”变量中的所有值读入内存。
// shape ={6,12}
int[] shape = dataVar.getShape();
// origin = {0,0}
int[] origin = new int[2];
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
ArrayInt.D2 dataArray;
//
dataArray = (ArrayInt.D2) dataVar.read(origin, shape);
// Check the values. 断言核对值
assert shape[0] == NX;
assert shape[1] == NY;
for (int j = 0; j < shape[0]; j++) {
for (int i = 0; i < shape[1]; i++) {
dataIn[j][i] = dataArray.get(j, i);
}
}
// printErWeiArray(dataIn);
// The file is closed no matter what by putting inside a try/catch block.
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (InvalidRangeException e) {
e.printStackTrace();
} finally {
if (dataFile != null)
try {
dataFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
System.out.println("*** SUCCESS reading example file simple_xy.nc!");
}
}



