suntae kim
suntae kim

Reputation: 1

How to add global attribute to the netCDF file

I created 'testFile.nc' by using the 'makeFile.java'. I'm trying to add global attribute to the testFile.nc file.

so I've been tried two ways like this :

Way 1, Using the UpdateFile.java (NetcdfFile class)

Way 2, Using the UpdateFile2.java (NetcdfFileWriter class)

I got the same log like this :

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

It looks like the global attribute added. but actually i didn't.

Thanks in advance

source code - 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(); 

source code - 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();

source code - 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();

Upvotes: 0

Views: 943

Answers (1)

DopplerShift
DopplerShift

Reputation: 5843

You need to take the file out of redefine mode to force the new attributes to be written to disk. Try adding this line:

dataFile.setRedefineMode(false);

before closing the file.

Upvotes: 1

Related Questions