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.wron;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.File;
21  import java.io.IOException;
22  
23  import junit.framework.TestCase;
24  import ucar.ma2.DataType;
25  import ucar.nc2.Attribute;
26  import ucar.nc2.Dimension;
27  import ucar.nc2.NetcdfFile;
28  import ucar.nc2.NetcdfFileWriteable;
29  import ucar.nc2.Variable;
30  import au.csiro.netcdf.NcWriteVariable;
31  import au.csiro.netcdf.util.Util;
32  
33  /**
34   * This class is a unit test suite to verify that the {@link MdbsyNetCDF2CSVConverter} operates correctly.
35   * 
36   * Copyright 2010, CSIRO Australia
37   * 
38   * @author Robert Bridle on 08/07/2010
39   * @version $Revision: 78 $ $Date: 2010-07-24 16:23:13 +1000 (Sat, 24 Jul 2010) $
40   */
41  public class TestMdbsyNetCDF2CSVConverter extends TestCase
42  {
43      /**
44       * The testing value for the latitude coordinate dimension.
45       */
46      private static final String DIM_LATITUDE = "lat";
47      private static final String VAR_LATITUDE = DIM_LATITUDE;
48      private static final String VAR_LATITUDE_VALUE = "-24.25";
49  
50      /**
51       * The testing value for the longitude coordinate dimension.
52       */
53      private static final String DIM_LONGITUDE = "long";
54      private static final String VAR_LONGITUDE = DIM_LONGITUDE;
55      private static final String VAR_LONGITUDE_VALUE = "138.10";
56      
57      /**
58       * The expected name of the converted csv file.
59       */
60      private static final String CONVERTED_CSV_FILE_NAME = System.getProperty("user.dir") + "\\region_-24.25_138.1.csv";
61  
62      /**
63       * The testing value for the time coordinate dimension.
64       */
65      private static final String DIM_TIME = "time";
66      private static final String VAR_TIME = DIM_TIME;
67      private static final String VAR_TIME_VALUES = "0" + System.getProperty("line.separator") + "1"
68              + System.getProperty("line.separator") + "2" + System.getProperty("line.separator") + "3"
69              + System.getProperty("line.separator") + "4" + System.getProperty("line.separator") + "5"
70              + System.getProperty("line.separator") + "6" + System.getProperty("line.separator") + "7"
71              + System.getProperty("line.separator") + "8" + System.getProperty("line.separator") + "9";
72  
73      /**
74       * The testing value for a variable defined over three coordinate dimensions (lat, long, time).
75       */
76      private static final String VAR_APET = "APET";
77      private static final String VAR_APET_VALUES = "0.000" + System.getProperty("line.separator") + "0.111"
78              + System.getProperty("line.separator") + "0.222" + System.getProperty("line.separator") + "0.333"
79              + System.getProperty("line.separator") + "0.444" + System.getProperty("line.separator") + "0.555"
80              + System.getProperty("line.separator") + "0.666" + System.getProperty("line.separator") + "0.777"
81              + System.getProperty("line.separator") + "0.888" + System.getProperty("line.separator") + "0.999";
82  
83      
84      /**
85       * The expected output converted csv structure.
86       */
87      private static String[] EXPECTED_CSV_FILE = { "Date,  APET", 
88          "1895-01-01,  0.0", 
89          "1895-01-02,  0.111",
90          "1895-01-03,  0.222", 
91          "1895-01-04,  0.333", 
92          "1895-01-05,  0.444", 
93          "1895-01-06,  0.555",
94          "1895-01-07,  0.666", 
95          "1895-01-08,  0.777", 
96          "1895-01-09,  0.888", 
97          "1895-01-10,  0.999" };
98      
99      /**
100      * The testing value for the dimension sizes.
101      */
102     private static final int LAT_DIM_SIZE = 1;
103     private static final int LONG_DIM_SIZE = 1;
104     private static final int TIME_DIM_SIZE = 10;
105 
106     /**
107      * The testing value for the netCDF file to write to.
108      */
109     private static final String NC_FILE_NAME = System.getProperty("user.dir") + "\\ABC.nc";
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see junit.framework.TestCase#setUp()
115      */
116     @Override
117     protected void setUp() throws Exception
118     {
119         super.setUp();
120 
121         // create dummy netCDF file with content.
122         createDummyNCFile(NC_FILE_NAME);
123     }
124 
125     /*
126      * (non-Javadoc)
127      * 
128      * @see junit.framework.TestCase#tearDown()
129      */
130     @Override
131     protected void tearDown() throws Exception
132     {
133         super.tearDown();
134 
135         System.setOut(null);
136 
137         // delete dummy netCDF file
138         File ncFile = new File(NC_FILE_NAME);
139         if (ncFile.exists())
140         {
141             NetcdfFile netcdfFile = NetcdfFile.open(NC_FILE_NAME);
142             netcdfFile.close();
143             ncFile.delete();
144         }
145         
146         // delete converted csv file
147         File csvFile = new File(CONVERTED_CSV_FILE_NAME);
148         if (csvFile.exists())
149         {
150             csvFile.delete();
151         }
152     }
153 
154     /**
155      * Test execute a valid NcWriteVariable command using Double datatype
156      */
157     public final void testNetCDF2CSVConversion() throws Exception
158     {
159         MdbsyNetCDF2CSVConverter.main(new String[] { "-i", System.getProperty("user.dir"), "-o",
160                 System.getProperty("user.dir") });
161 
162         // check that the converted csv file was created.
163         assertTrue("The expected converted csv file does not exist: " + CONVERTED_CSV_FILE_NAME, Util
164                 .fileExists(CONVERTED_CSV_FILE_NAME));
165         
166         String[][] csvMatrix = MdbsyNetCDF2CSVConverter.readLookupFile(new File(CONVERTED_CSV_FILE_NAME));
167         
168         for (int row = 0; row < csvMatrix.length; row++)
169         {
170             StringBuffer line = new StringBuffer();
171             for (int col = 0; col < csvMatrix[0].length; col++)
172             {
173                 line.append(csvMatrix[row][col]);
174                 if(col < (csvMatrix[0].length-1))
175                 {
176                     line.append(", ");
177                 }
178             }
179             
180             assertEquals("The expected csv output is not correct.", EXPECTED_CSV_FILE[row], line.toString());
181         }
182     }
183 
184     /**
185      * Makes a dummy netCDF file with known contents.
186      */
187     private void createDummyNCFile(String outputFilename) throws IOException
188     {
189         NetcdfFileWriteable ncFile = null;
190 
191         ncFile = NetcdfFileWriteable.createNew(outputFilename, true);
192         try
193         {
194             Dimension dimension = new Dimension(DIM_LATITUDE, LAT_DIM_SIZE, true /* isShared */,
195                     false /* isUnlimited */, false /* isVariableLength */);
196             ncFile.addDimension(null, dimension);
197 
198             dimension = new Dimension(DIM_LONGITUDE, LONG_DIM_SIZE, true /* isShared */, false /* isUnlimited */, false /* isVariableLength */);
199             ncFile.addDimension(null, dimension);
200 
201             dimension = new Dimension(DIM_TIME, TIME_DIM_SIZE, true /* isShared */, false /* isUnlimited */, false /* isVariableLength */);
202             ncFile.addDimension(null, dimension);
203 
204             // define coordinate variables.
205             ncFile.addVariable(VAR_LATITUDE, DataType.FLOAT, DIM_LATITUDE);
206             ncFile.addVariable(VAR_LONGITUDE, DataType.FLOAT, DIM_LONGITUDE);
207             Variable timeVariable = ncFile.addVariable(VAR_TIME, DataType.INT, DIM_TIME);
208             ncFile.addVariableAttribute(timeVariable.getName(), new Attribute("units", "days since 1895-01-01 0:0:0"));
209 
210             // variable is defined over three coordinates dimensions.
211             ncFile.addVariable(VAR_APET, DataType.FLOAT, DIM_LATITUDE + " " + DIM_LONGITUDE + " " + DIM_TIME);
212             ncFile.create();
213         }
214         finally
215         {
216             ncFile.close();
217         }
218 
219         // populate the coordinate variables with string of int values.
220         NcWriteVariable command = new NcWriteVariable();
221         command.execute(new File(outputFilename), VAR_LATITUDE, "0-" + String.valueOf(LAT_DIM_SIZE - 1),
222                 new ByteArrayInputStream(VAR_LATITUDE_VALUE.getBytes("UTF-8")), false);
223         command.execute(new File(outputFilename), VAR_LONGITUDE, "0-" + String.valueOf(LONG_DIM_SIZE - 1),
224                 new ByteArrayInputStream(VAR_LONGITUDE_VALUE.getBytes("UTF-8")), false);
225         command.execute(new File(outputFilename), VAR_TIME, "0-" + String.valueOf(TIME_DIM_SIZE - 1),
226                 new ByteArrayInputStream(VAR_TIME_VALUES.getBytes("UTF-8")), false);
227 
228         try
229         {
230             command.execute(new File(outputFilename), VAR_APET, "0-" + String.valueOf(LAT_DIM_SIZE - 1) + ", 0-"
231                     + String.valueOf(LONG_DIM_SIZE - 1) + ", 0-" + String.valueOf(TIME_DIM_SIZE - 1),
232                     new ByteArrayInputStream(VAR_APET_VALUES.getBytes("UTF-8")), false);
233         }
234         catch (IllegalArgumentException iae)
235         {
236             iae.printStackTrace();
237             fail("could not create file: " + outputFilename + " to being testing of netCDF 2 CSV converter");
238         }
239     }
240 }