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 Xiaoshen:The problem with this approach is that it doesnt copy the data to the new file. Is that your intention?
On 6/7/2010 9:10 AM, address@hidden wrote:
John: Thank you for your reply. I re-tested and it works. Following is a simple junit test(NetCDFTest.java), which opens an existing netcdf file("input.nc", attached with this email), modify the variable values, then dump out to a different netcdf file("output.nc"). I have converted both netcdf files to readable text format and confirmed the Java code indeed has changed the variable values. Again, I am worried about NetcdfFileWriteable setName(String newFilePathAndName) method, since it is deprecated. My code relies on this method. If netcdf library delete this method in the future release, my code will break. The API for this method suggests using NetcdfFileWriteable.createNew(String filename) I don't think it is equivalent, since createNew() creates a blank netcdf object. I have to "copy" everything from input.nc to output.nc. Thank you very much for your help. Xiaoshen <NetCDFTest.java> public class NetCDFTest extends TestCase { public void testModifyNCfile() throws IOException, InvalidRangeException { final String inputFileName = "./output/input.nc"; final String outputFileName = "./output/output.nc"; final NetcdfFileWriteable writableFile = NetcdfFileWriteable.openExisting(inputFileName); writableFile.setRedefineMode(true); final Variable ffgVariable = writableFile.findVariable("FFG"); final Array ffgVarArray = ffgVariable.read(); final Index ffgIndex = ffgVarArray.getIndex(); writableFile.setName(outputFileName); //setName() is deprecated. If commented out this method, "input.nc" will be overwritten writableFile.create(); //re-set variable FFG values ffgVarArray.setDouble(ffgIndex.set(0), 10.1); ffgVarArray.setDouble(ffgIndex.set(1), 10.2); ffgVarArray.setDouble(ffgIndex.set(2), 10.3); ffgVarArray.setDouble(ffgIndex.set(3), 10.4); writableFile.write("FFG", ffgVarArray); writableFile.flush(); writableFile.close(); } } </NetCDFTest.java> ==> input.nc text file<== netcdf input { dimensions: timeStep = 4 ; variables: double FFG(timeStep) ; FFG:start_time = "2010-01-12 12:00:00" ; // global attributes: :global_start_time = "2010-01-12 12:00:00" ; data: FFG = 1.1, 2.2, 3.3, 4.4 ; } ==> output.nc text file<== netcdf output { dimensions: timeStep = 4 ; variables: double FFG(timeStep) ; FFG:start_time = "2010-01-12 12:00:00" ; // global attributes: :global_start_time = "2010-01-12 12:00:00" ; data: FFG = 10.1, 10.2, 10.3, 10.4 ; } ----- Original Message ----- From: John Caron<address@hidden> Date: Friday, June 4, 2010 4:27 pm Subject: Re: [netcdf-java] NetcdfFileWriteable setName() is deprecated, but i don't know what else to useHi Xiaoshen : Are you sure that works? Can you send a small test case that does that? thanks John On 6/4/2010 10:52 AM, Xiaoshen Li wrote:Hi, I have a problem to achieve my goal. My goal is: opening anexistingnetcd file, make some modification to it, then output to adifferentnetcdf file. My java code is like the following: final NetcdfFileWriteable writableFile = NetcdfFileWriteable.openExisting(inputNetcdfFileName); //open an existing input netcdf file. writableFile is not blank, it has everything defined in the input netcdf file. writableFile.setRedefineMode(true); //did some modification, like changing variable values, etc writableFile.setName(outputFileName); //i want to writeout toa different netcdf file, i don't want to overwrite the originalinputnetcdf file The code above works fine for me. However, setName() is *Deprecated. * setName public void *setName*(java.lang.String filename) *Deprecated.* /use NetcdfFileWriteable.createNew(Stringfilename);/>Set the filename of a new file to be created: call before calling create(). *Parameters:* |filename| - name of new file to create. /NetcdfFileWriteable.createNew(String filename) /is differentthing.It returns a blank netcdf file object. final NetcdfFileWriteable writableFile = NetcdfFileWriteable.createNew(fileName); //now, writableFile isblank>How can I avoid using deprecated setName() to achieve my goal?Thankyou very much. _______________________________________________ netcdf-java mailing list address@hidden For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/_______________________________________________ netcdf-java mailing list address@hidden For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/