Hi Xiaoshen:
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 use
Hi 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:
existingHi,
I have a problem to achieve my goal. My goal is: opening an
netcd file, make some modification to it, then output to adifferent
out tonetcdf 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 write
a different netcdf file, i don't want to overwrite the originalinput
filename);/>netcdf file
The code above works fine for me. However, setName() is *Deprecated. *
setName
public void *setName*(java.lang.String filename)
*Deprecated.* /use NetcdfFileWriteable.createNew(String
thing.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 different
blank>It returns a blank netcdf file object.
final NetcdfFileWriteable writableFile =
NetcdfFileWriteable.createNew(fileName); //now, writableFile is
How can I avoid using deprecated setName() to achieve my goal?Thank
you 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/
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata inquiry tracking system and then made publicly available through the web. If you do not want to have your interactions made available in this way, you must let us know in each email you send to us.