如何将全局属性添加到netCDF文件

时间:2015-11-23 02:28:12

标签: netcdf

我创建了' testFile.nc'使用' makeFile.java'。 我试图将全局属性添加到testFile.nc文件中。

所以我尝试了两种这样的方法:

方式1, 使用UpdateFile.java(NetcdfFile类)

方式2, 使用UpdateFile2.java(NetcdfFileWriter类)

我得到了这样的日志:

aa>>>yo = "face"
aa>>>versionD = 1.2
aa>>>versionF = 1.2f
aa>>>versionI = 1
aa>>>versionS = 2S
aa>>>versionB = 3B
aa>>>creator = "testValue"

看起来像添加了全局属性。但实际上我并没有。

提前致谢

源代码 - makeFile.java

// TODO Auto-generated method stub
String location = "C:/data/my/testFile.nc";
NetcdfFileWriter writer = netcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, location, null);

// add global attributes
writer.addGroupAttribute(null, new Attribute("yo", "face"));
writer.addGroupAttribute(null, new Attribute("versionD", 1.2));
writer.addGroupAttribute(null, new Attribute("versionF", (float) 1.2));
writer.addGroupAttribute(null, new Attribute("versionI", 1));
writer.addGroupAttribute(null, new Attribute("versionS", (short) 2));
writer.addGroupAttribute(null, new Attribute("versionB", (byte) 3));
writer.create();
writer.close(); 

源代码 - updateFile.java

String location = "C:/data/my/testFile.nc";
NetcdfFileWriter dataFile = NetcdfFileWriter.openExisting(location);
Attribute att = new Attribute("creator","testValue");
dataFile.setRedefineMode(true);
Group gr = dataFile.addGroup(null, "");
gr.addAttribute(att);
List<Attribute> al = gr.getAttributes();
for(int i=0 ; i < al.size(); i++){
    debug("aa",al.get(i).toString());
}
dataFile.close();

源代码 - updateFile2.java

String location = "C:/data/my/testFile.nc";
NetcdfFileWriter dataFile = NetcdfFileWriter.openExisting(location);
dataFile.setRedefineMode(true);

Group gr = dataFile.addGroup(null,"");
Attribute att = new Attribute("creator","testValue");
dataFile.addGroupAttribute(gr, att);
Attribute aa = dataFile.findGlobalAttribute("creator");
Attribute bb = dataFile.findGlobalAttribute("versionD");
dataFile.deleteGroupAttribute(null, "versionD");
dataFile.setRedefineMode(true);
dataFile.renameVariable("versionD", "versionKK");
dataFile.flush();

1 个答案:

答案 0 :(得分:0)

您需要将文件从重新定义模式中取出以强制将新属性写入磁盘。尝试添加以下行:

dataFile.setRedefineMode(false);

关闭文件之前。