1   /**
2    * Copyright 2010, CSIRO Australia.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package au.csiro.netcdf;
18  
19  import java.io.IOException;
20  
21  import ucar.ma2.DataType;
22  import ucar.nc2.Dimension;
23  import ucar.nc2.NetcdfFileWriteable;
24  
25  public class CreateFile
26  {
27  
28      private static String dimensionName = "lat";
29      private static int dimensionSize = 20;
30      private static String variableName = "temp";
31      private static DataType variableDataType = DataType.getType("Float");
32  
33      public static void main(String[] args) throws IOException
34      {
35          NetcdfFileWriteable ncFile = null;
36  
37          String outputFilename = args[0];
38          ncFile = NetcdfFileWriteable.createNew(outputFilename, true);
39          try
40          {
41              Dimension dimension = new Dimension(dimensionName, dimensionSize, true /* isShared */,
42                      false /* isUnlimited */, false /* isVariableLength */);
43              ncFile.addDimension(null, dimension);
44              ncFile.create();
45          }
46          finally
47          {
48              ncFile.close();
49          }
50  
51          
52          ncFile = NetcdfFileWriteable.openExisting(outputFilename, true);
53          try
54          {
55              ncFile.setRedefineMode(true);
56              ncFile.addVariable(dimensionName, variableDataType, dimensionName);
57              ncFile.addVariable(variableName, variableDataType, dimensionName);
58              //ncFile.setRedefineMode(false);
59              ncFile.create();
60          }
61          finally
62          {
63              ncFile.close();
64          }
65      }
66  
67  }