需求:从一个nc文件中取出我想要的变量,导出到另外一个nc文件。
下面是基本代码:
@GetMapping("/export")
public String varExport(@ModelAttribute FileSaveVars params ) throws IOException, InvalidRangeException {
// 1° 设置原始文件路径+转存文件路径String filePath = "/Users/caowei/workspace/jszone/大气系统/ncdemo/src/main/resources/static/MERRA2_400.tavg1_2d_flx_Nx.20180101.SUB.nc";String savePath = "/Users/caowei/workspace/test.nc";
// 2° 读取文件NetcdfFile ncFile = null;ncFile = NetcdfFile.open(filePath, null);
// 3° 获取需要转存的变量Variable latVar = ncFile.findVariable("lat");
// 4° 读取变量的一些信息
// 4°-1 长度int latitudeCount = (int) latVar.getSize();
// 4°-2 读取变量数组数据ArrayDouble.D1 latArray = (ArrayDouble.D1) latVar.read();
// 5° 构造输出保存文件NetcdfFileWriter ncWrite = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, savePath);
// 6° 设置文件的一些参数
// 6°-1纬度Dimension latitudeDimension = ncWrite.addDimension(null, "latitude", (int) latArray.getSize());
// 6°-2变量Variable latitudeVar = ncWrite.addVariable(null, "latitude", DataType.DOUBLE, "latitude");
// 6°-3设置属性latitudeVar.addAttribute(new Attribute("standard_name", "latitude"));latitudeVar.addAttribute(new Attribute("long_name", "latitude"));latitudeVar.addAttribute(new Attribute("units", "degrees_north"));latitudeVar.addAttribute(new Attribute("axis", "Y"));
// 7°创建ncWrite.create();
// 8° 构造数据ArrayDouble.D1 latitudeData = latArray;
// 9°将数据写入ncWrite.write(latitudeVar, latitudeData);
// 10°关闭保存ncWrite.close();return "OK";
}
下面是抽出一个变量进行导出
@GetMapping("/export")public String varExport(@ModelAttribute FileSaveVars params ) throws IOException, InvalidRangeException {
// 1° 设置原始文件路径+转存文件路径String filePath = "/Users/caowei/workspace/jszone/大气系统/ncdemo/src/main/resources/static/MERRA2_400.tavg1_2d_flx_Nx.20180101.SUB.nc";String savePath = "/Users/caowei/workspace/test.nc";
// 2° 读取文件NetcdfFile ncFile = null;ncFile = NetcdfFile.open(filePath, null);
// 3° 设置需要转存的变量String varName = "lon";
// 4° 获取需要转存的变量对象Variable varObject = ncFile.findVariable(varName);
// 5° 读取变量的一些信息
// 5°-1 长度int varCount = (int) varObject.getSize();
// 6° 构造输出保存文件NetcdfFileWriter ncWrite = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, savePath);
// 7° 设置文件的一些参数
// 7°-1纬度Dimension varDimension = ncWrite.addDimension(null, varObject.getName().toString(), varCount);
// 7°-2变量Variable varVar = ncWrite.addVariable(null, varObject.getName().toString(), varObject.getDataType(), varDimension.getName());
// 7°-3设置属性List<Attribute> AttrList = varObject.getAttributes();for (Attribute att : AttrList) {varVar.addAttribute(att);}
// 8°创建ncWrite.create();
// 9°将数据写入ncWrite.write(varVar, varObject.read());
// 10°关闭保存ncWrite.close();return "OK";
}
下面再次进行升级,可以直接导出单个三纬变量
@GetMapping("/export")
public String varExport(@ModelAttribute FileSaveVars params ) throws IOException, InvalidRangeException {
// 1° 设置原始文件路径+转存文件路径String filePath = "/Users/caowei/workspace/jszone/大气系统/ncdemo/src/main/resources/static/MERRA2_400.tavg1_2d_flx_Nx.20180101.SUB.nc";String savePath = "/Users/caowei/workspace/test.nc";
// 2° 读取文件NetcdfFile ncFile = NetcdfFile.open(filePath, null);
// 3° 设置需要转存的变量String varName = "QLML";
// 4° 获取需要转存的变量对象Variable varObject = ncFile.findVariable(varName);
// 5° 构造输出保存文件NetcdfFileWriter ncWrite = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, savePath);
// 6° 设置文件的一些参数
// 7°-1纬度for(Dimension dimension : varObject.getDimensions()){ncWrite.addDimension(null, dimension.getShortName(), dimension.getLength());}
// 6°-2变量Variable varVar = ncWrite.addVariable(null, varObject.getShortName(), varObject.getDataType(), varObject.getDimensionsString());
// 6°-3设置属性List<Attribute> AttrList = varObject.getAttributes();for (Attribute att : AttrList) {varVar.addAttribute(att);}
// 7°创建ncWrite.create();
// 8°将数据写入ncWrite.write(varVar, varObject.read());
// 9°关闭保存ncWrite.close();return "OK";
}