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.
Roy Mendelssohn wrote:
Hi Guys:Are there any equivalent standalone tools that operate like ncdump and ncgen but read/write NcML? What I am looking for is something easy and scriptable that I can just go through our existing directories to generate the NcML for each file, and then ultimately add some of the georefencing info.TIA, -Roy M
Hi Roy: This all pertains to the NetCDF-Java library: You can generate NcML from a netcdf file by using NetcdfFile.writeNcML().You can read NcML directly, just pass NetcdfFile.open() an NcML file ending in .xml or .ncml.
You can write a NetcdfFile from a NcML file by opening it as above, then using ucar.nc2.FileWriter.writeToFile().
You can do all all of this interactively by using the NcML Tab on the ToolUI program at
http://www.unidata.ucar.edu/content/software/netcdf-java/v2.2/webstart/index.html
For your case, where I assume you want to add extra metadata, 2 paths suggest itself:
1. Open the NetCDF file, add the extra metadata, then write it out using FileWriter. (No NcML needed, do it all in Java) This might lend itself to automatically doing a large amount of files.
2. Create the NcML file, add the extra metadata, then read it in and write it back out using FileWriter to get a NetCDF file. For example I have a file called example.nc, which has this NcML (whicj i generated from ToolsUI):
<?xml version="1.0" encoding="UTF-8"?><netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:C:/dev/netcdf-java-2.2/test/data/dataset/example.nc">
<dimension name="time" length="4" isUnlimited="true" /> <dimension name="lat" length="3" /> <dimension name="lon" length="4" /> <attribute name="title" type="String" value="Example Data" /> <variable name="rh" shape="time lat lon" type="int"> <attribute name="long_name" type="String" value="relative humidity" /> <attribute name="units" type="String" value="percent" /> </variable> <variable name="T" shape="time lat lon" type="double"> <attribute name="long_name" type="String" value="surface temperature" /> <attribute name="units" type="String" value="degC" /> </variable> <variable name="lat" shape="lat" type="float"> <attribute name="units" type="String" value="degrees_north" /> </variable> <variable name="lon" shape="lon" type="float"> <attribute name="units" type="String" value="degrees_east" /> </variable> <variable name="time" shape="time" type="int"> <attribute name="units" type="String" value="hours" /> </variable> </netcdf> Now I add the "Conventions" global attribute : <?xml version="1.0" encoding="UTF-8"?><netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:C:/dev/netcdf-java-2.2/test/data/dataset/example.nc">
<dimension name="time" length="4" isUnlimited="true" /> <dimension name="lat" length="3" /> <dimension name="lon" length="4" /> <attribute name="title" type="String" value="Example Data" /> <attribute name="Conventions" type="String" value="COARDS" /> <variable name="rh" shape="time lat lon" type="int"> <attribute name="long_name" type="String" value="relative humidity" /> <attribute name="units" type="String" value="percent" /> </variable> <variable name="T" shape="time lat lon" type="double"> <attribute name="long_name" type="String" value="surface temperature" /> <attribute name="units" type="String" value="degC" /> </variable> <variable name="lat" shape="lat" type="float"> <attribute name="units" type="String" value="degrees_north" /> </variable> <variable name="lon" shape="lon" type="float"> <attribute name="units" type="String" value="degrees_east" /> </variable> <variable name="time" shape="time" type="int"> <attribute name="units" type="String" value="hours" /> </variable> </netcdf>I actually dont need to add all information from the original file to the NcML file, because the default is to read existing metadata in, so equivilently:
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:C:/dev/netcdf-java-2.2/test/data/dataset/example.nc">
<attribute name="Conventions" type="String" value="COARDS" /> </netcdf>If you need to add a new variable, just make sure you add its data values also:
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:C:/dev/netcdf-java-2.2/test/data/dataset/example.nc">
<attribute name="Conventions" type="String" value="COARDS" /> <variable name="newvar" shape="time" type="float"> <values>1.0 4.5 7.8</values> </variable> </netcdf>Once you have an NcML file, you can use FileWriter.main() in batch mode to generate a NetCDF file:
> java -classpath toolsUI.jar ucar.nc2.FileWriter <ncml input filename> <netcdf output filename>