NetCDF
NetCDF(Network Common Data Form):一种自描述、自包含的数据格式,广泛用于存储气象和气候模型输出、观测数据等。NetCDF支持多维度数组、属性和变量,便于跨平台共享。
java实现
主要利用netCDF-Java 5.6.0 实现。
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Test;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReadNcTest {
@Test
void readNcToJson() throws Exception {
//获取 nc 文件的路径
String url = "325china.nc";
//转成 json 后存放的目录
//注意:无论 windows 还是 linux 下的路径,都统一使用 / 进行分割
String destDir = "d:/";
//使用 PanoplyWin 工具,打开 nc 文件,可以看到里面有 3 个变量
List<String> datalist = new ArrayList<>(Arrays.asList("lon", "lat", "water_u"));
//针对 3 个变量的数据,分别生成 json 文件
exportJson(url, destDir, datalist);
}
private void exportJson(String ncFileFullPath, String destDir, List<String> dataItem) {
// 打开 NetCDF 文件
try (NetcdfFile ncFile = NetcdfFile.open(ncFileFullPath)) {
//遍历nc文件中的每个变量,输出json数据
for (String it : dataItem) {
// 读取其中的变量
Variable var = ncFile.findVariable(it);
// 获取变量的维度
int[] shape = var.getShape();
int[] origin = new int[shape.length];
// 读取变量的值
Object data = var.read(origin, shape).get1DJavaArray(var.getDataType().getPrimitiveClassType());
// 使用 Gson 将数据转换为 JSON 格式
// Gson gson = new Gson();
Gson gson = new GsonBuilder()
.serializeSpecialFloatingPointValues() // 允许序列化NaN和Infinity
.create();
String json = gson.toJson(data);
// 输出 JSON 数据到控制台
// System.out.println(json);
String jsonfile = "aaa.json";
// 将 JSON 数据写入到文件
try (FileWriter writer = new FileWriter(jsonfile)) {
writer.write(json);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
GRIB
GRIB(GRIdded Binary):主要用于气象预报产品,包括GRIB1和GRIB2两种版本。它以高效的方式存储大量的气象格点数据,如温度、风速等,是气象预报中心之间交换数据的标准格式。
需求:转json给webgis使用。
读取:wgrib2,Panoply等工具读取可视化。
工具:可以用为grib2json,这是java写的,开源的可以直接集成到自己程序里。也可以打包使用。
1.grib2json使用
1.1数据下载
https://nomads.ncep.noaa.gov/
点击对应数据下载。
1.2工具下载
gitgub
1.2.打包
此实用程序使用 netCDF-Java GRIB 解码器,它是 THREDDS 项目的一部分 由大学大气研究公司/Unidata 提供。说明是java源代码。需要自己打包。
在目录里运行打包命令,打成jar包。
mvn package
mvn没有需下载maven工具。
1.3运行
把打包文件解压,进入grib2json-master\target\grib2json-0.8.0-SNAPSHOT\grib2json-0.8.0-SNAPSHOT\bin文件夹里运行。也可以设置全局变量,这样就不需要进入此文件夹运行了。
grib2json --help
Usage: grib2json [options] FILE
[--compact -c] : enable compact Json formatting
[--data -d] : print GRIB record data
[--filter.category --fc value] : select records with this numeric category
[--filter.parameter --fp value] : select records with this numeric parameter
[--filter.surface --fs value] : select records with this numeric surface type
[--filter.value --fv value] : select records with this numeric surface value
[--help -h] : display this help
[--names -n] : print names of numeric codes
[--output -o value] : write output to the specified file (default is stdout)
[--verbose -v] : enable logging to stdout
1.4转换
命令行输入正确文件路径。
grib2json -d -n -o test.json D:/ex.grib2
或直接用java运行jar包
java -jar grib2json-0.8.0-SNAPSHOT.jar -d -n -o test.json D:/ex.grib2
2.java代码实现
本工具依赖netCDF-Java, 源码里有实现可以抽离出使用。
其他数据介绍与解析
数据
1. HDF(Hierarchical Data Format):类似于NetCDF,支持复杂的数据结构,适用于大规模科学数据集的存储和分发。HDF4和HDF5是两个主要版本。
2. CSV(Comma-Separated Values):简单的文本格式,易于阅读和处理,常用于数据交换,但缺乏自描述性,不包含元数据。
3. GeoTIFF:一种地理空间图像格式,用于存储栅格数据,如卫星图像或气象地图,包含地理坐标信息。
4. BUFR(Binary Universal Form for the Representation of Meteorological Data):国际气象组织推荐的一种二进制编码格式,特别适用于气象观测数据的交换。
读取软件工具:
- Panoply:NASA开发的免费软件,可以打开、查看和转换NetCDF、HDF、GRIB等格式数据。
- HDFView:打开HDF4 HDF5。
- NCAR Command Language (NCL):提供丰富的脚本语言功能,用于处理NetCDF、GRIB等气象数据。
- GDAL/OGR:一个开源地理空间处理库,提供了广泛的格式转换能力,支持gis领域数据与气象数据得格式转换能力,支持NetCDF、HDF、GeoTIFF等格式的转换。
编程语言库
- Python: 利用netCDF4, pygrib, h5py等库进行数据读写和格式转换。xarray库提供了更高级的数据结构,方便处理NetCDF和GRIB数据。
- java:netCDF-Java ,
netcdfAll-4.6.11.jar
是一个强大的Java库,专门用于处理NetCDF格式的数据,同时也支持标准的GRIB1和GRIB2格式数据的解码。jHDF:纯Java实现的HDF5库使用。HDF5等. - js