1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38
39
40
41 public class TestMdbsyNetCDF2CSVConverter extends TestCase
42 {
43
44
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
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
59
60 private static final String CONVERTED_CSV_FILE_NAME = System.getProperty("user.dir") + "\\region_-24.25_138.1.csv";
61
62
63
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
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
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
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
108
109 private static final String NC_FILE_NAME = System.getProperty("user.dir") + "\\ABC.nc";
110
111
112
113
114
115
116 @Override
117 protected void setUp() throws Exception
118 {
119 super.setUp();
120
121
122 createDummyNCFile(NC_FILE_NAME);
123 }
124
125
126
127
128
129
130 @Override
131 protected void tearDown() throws Exception
132 {
133 super.tearDown();
134
135 System.setOut(null);
136
137
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
147 File csvFile = new File(CONVERTED_CSV_FILE_NAME);
148 if (csvFile.exists())
149 {
150 csvFile.delete();
151 }
152 }
153
154
155
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
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
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
195 false
196 ncFile.addDimension(null, dimension);
197
198 dimension = new Dimension(DIM_LONGITUDE, LONG_DIM_SIZE, true
199 ncFile.addDimension(null, dimension);
200
201 dimension = new Dimension(DIM_TIME, TIME_DIM_SIZE, true
202 ncFile.addDimension(null, dimension);
203
204
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
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
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 }