This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Hi Jin, first, make sure you have the latest Netcdf-JAVA library and your directory "data" is already created. Then, I think you are trying to set the root group for the file and you don't need to do that. You just need: writer.addGroupAttribute(null, new Attribute("lat", 100)); so, when you want to get the root group of a file you can either look for a null group: Group root1 = file.findGroup(null); or an empty String: Group root1 = file.findGroup(""); And for getting the global attributes you can also use: file.getGlobalAttributes(); If this is the case your code could be: NetcdfFileWriter writer = NetcdfFileWriter.createNew(Version.netcdf4, "data/foo.nc"); writer.addGroupAttribute(null, new Attribute("lat", 100)); writer.create(); writer.flush(); writer.close(); NetcdfFile file = NetcdfFile.open("data/foo.nc"); List<Attribute> attr = file.getGlobalAttributes(); However, if you want to add a group to the root group, I'd suggest not calling it "root" (it's pretty confusing) and then your code could be something like this: NetcdfFileWriter writer = NetcdfFileWriter.createNew(Version.netcdf4, "data/foo.nc"); Group root = writer.addGroup(null, ""); //Gets the root group Group newGroup = writer.addGroup(root, "new_group");//Adds a new group to the root group writer.addGroupAttribute(newGroup, new Attribute("lat", 100)); writer.create(); writer.flush(); writer.close(); NetcdfFile file = NetcdfFile.open("data/foo.nc"); newGroup = file.findGroup("new_group"); List<Attribute> attr = newGroup.getAttributes(); Cheers! Marcos Hermida. Ticket Details =================== Ticket ID: SXF-400193 Department: Support netCDF Java Priority: Normal Status: Open