forked from Unidata/netcdf-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCFWriter2.java
More file actions
310 lines (256 loc) · 12.7 KB
/
Copy pathTestCFWriter2.java
File metadata and controls
310 lines (256 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (c) 1998-2025 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.nc2.dt.grid;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ucar.nc2.Attribute;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFileWriter;
import ucar.nc2.NetcdfFiles;
import ucar.nc2.Variable;
import ucar.nc2.constants.CF;
import ucar.nc2.grib.collection.Grib;
import ucar.nc2.time.CalendarDate;
import ucar.nc2.time.CalendarDateRange;
import ucar.nc2.util.DebugFlagsImpl;
import ucar.unidata.geoloc.LatLonPoint;
import ucar.unidata.geoloc.LatLonRect;
import ucar.unidata.geoloc.ProjectionPoint;
import ucar.unidata.geoloc.ProjectionRect;
import ucar.unidata.util.test.category.NeedsCdmUnitTest;
import ucar.unidata.util.test.TestDir;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Test CFGridWriter2
*
* @author caron
* @since 7/30/2014
*/
@Category(NeedsCdmUnitTest.class)
public class TestCFWriter2 {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void testSubset() throws Exception {
String fileIn = TestDir.cdmUnitTestDir + "ft/grid/testCFwriter.nc";
String fileOut = tempFolder.newFile().getAbsolutePath();
String varName = "Temperature";
try (ucar.nc2.dt.grid.GridDataset gds = GridDataset.open(fileIn)) {
List<String> gridList = new ArrayList<>();
gridList.add(varName);
NetcdfFileWriter writer = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, fileOut);
CFGridWriter2.writeFile(gds, gridList, new LatLonRect(LatLonPoint.create(30, -109), 10, 50), null, 1, null, null,
1, true, writer);
}
try (ucar.nc2.dt.grid.GridDataset result = GridDataset.open(fileOut)) {
GeoGrid grid = result.findGridByName(varName);
assertTrue(grid != null);
}
}
@Test
public void testSizeEstimate() throws Exception {
String fileIn = TestDir.cdmUnitTestDir + "ft/grid/testCFwriter.nc";
System.out.printf("Open %s%n", fileIn);
try (ucar.nc2.dt.grid.GridDataset gds = GridDataset.open(fileIn)) {
LatLonRect llbb = new LatLonRect(LatLonPoint.create(30, -109), 10, 50);
long totalSize = CFGridWriter2.makeSizeEstimate(gds, null, null, null, 1, null, null, 1, true);
long subsetSize = CFGridWriter2.makeSizeEstimate(gds, null, llbb, null, 1, null, null, 1, true);
System.out.printf("total size = %d%n", totalSize);
System.out.printf("subset size = %d%n", subsetSize);
assertTrue(subsetSize < totalSize);
String varName = "Temperature";
List<String> gridList = new ArrayList<>();
gridList.add(varName);
totalSize = CFGridWriter2.makeSizeEstimate(gds, gridList, null, null, 1, null, null, 1, true);
subsetSize = CFGridWriter2.makeSizeEstimate(gds, gridList, llbb, null, 1, null, null, 1, true);
System.out.printf("total size Temp only = %d%n", totalSize);
System.out.printf("subset size Temp only = %d%n", subsetSize);
assertTrue(subsetSize < totalSize);
}
}
@Test
public void testSizeEstimateTimeSubset() throws Exception {
String fileIn = TestDir.cdmUnitTestDir + "ft/grid/cg/cg.ncml";
System.out.printf("Open %s%n", fileIn);
try (ucar.nc2.dt.grid.GridDataset gds = GridDataset.open(fileIn)) {
CalendarDateRange dateRange = CalendarDateRange.of(CalendarDate.parseISOformat(null, "2006-06-07T12:00:00Z"),
CalendarDate.parseISOformat(null, "2006-06-07T13:00:00Z"));
long totalSize = CFGridWriter2.makeSizeEstimate(gds, null, null, null, 1, null, null, 1, true);
long subsetSize = CFGridWriter2.makeSizeEstimate(gds, null, null, null, 1, null, dateRange, 1, true);
System.out.printf("total size = %d%n", totalSize);
System.out.printf("subset size with date range = %d%n", subsetSize);
assertTrue(subsetSize < totalSize);
String varName = "CGusfc";
List<String> gridList = new ArrayList<>();
gridList.add(varName);
totalSize = CFGridWriter2.makeSizeEstimate(gds, gridList, null, null, 1, null, null, 1, true);
subsetSize = CFGridWriter2.makeSizeEstimate(gds, gridList, null, null, 1, null, dateRange, 1, true);
System.out.printf("total size CGusfc only = %d%n", totalSize);
System.out.printf("subset size CGusfc with date range = %d%n", subsetSize);
assertTrue(subsetSize < totalSize);
}
}
@Test
public void testSizeEstimateOnTP() throws Exception {
String fileIn = TestDir.cdmUnitTestDir + "gribCollections/tp/GFSonedega.ncx4";
System.out.printf("Open %s%n", fileIn);
try (GridDataset dataset = GridDataset.open(fileIn)) {
List<String> gridList = new ArrayList<>();
gridList.add("Cloud_mixing_ratio_isobaric");
// static public long makeSizeEstimate(ucar.nc2.dt.GridDataset gds, List<String> gridList,
// LatLonRect llbb, ProjectionRect projRect, int horizStride, Range zRange,
// CalendarDateRange dateRange, int stride_time, boolean addLatLon) throws IOException, InvalidRangeException {
CalendarDate date = CalendarDate.parseISOformat(null, "2015-03-26T06:00:00Z");
CalendarDateRange dateRange = CalendarDateRange.of(date, date);
long totalSize = CFGridWriter2.makeSizeEstimate(dataset, gridList, null, null, 1, null, null, 1, true);
long subsetSize = CFGridWriter2.makeSizeEstimate(dataset, gridList, null, null, 1, null, dateRange, 1, true);
System.out.printf("total size Temp only = %d%n", totalSize);
System.out.printf("subset size Temp only = %d%n", subsetSize);
assertTrue(subsetSize == totalSize / 2);
}
}
@Test
public void testWriteFileOnTP() throws Exception {
testFileSize(TestDir.cdmUnitTestDir + "gribCollections/tp/GFSonedega.ncx4", "Cloud_mixing_ratio_isobaric",
"2015-03-26T06:00:00Z", null, null, true);
}
// time_start=2014-10-01T21%3A00%3A00Z&time_end=2014-10-02T21%3A00%3A00Z
@Ignore("not visible on spock")
@Test
public void testWriteFileOnNarr() throws Exception {
testFileSize("B:/ncdc/0409/narr/Narr_A_fc.ncx4", "Accum_snow_surface", "2014-10-01T21:00:00Z", null, null, false);
testFileSize("B:/ncdc/0409/narr/Narr_A_fc.ncx4", "Accum_snow_surface", "2014-10-01T21:00:00Z",
"2014-10-02T21:00:00Z", null, false);
testFileSize("B:/ncdc/0409/narr/Narr_A_fc.ncx4", "Convective_cloud_cover_entire_atmosphere_3_Hour_Average",
"2014-10-01T21:00:00Z", "2014-10-02T21:00:00Z", null, false);
}
@Ignore("not visible on spock")
@Test
public void testWriteFileOnNarr2() throws Exception {
Grib.setDebugFlags(new DebugFlagsImpl("Grib/indexOnly"));
testFileSize("B:/ncdc/0409/narr/Narr_A_fc.ncx4",
"Accum_snow_surface,Convective_cloud_cover_entire_atmosphere_3_Hour_Average", "2014-10-01T21:00:00Z",
"2014-10-02T21:00:00Z", null, true);
Grib.setDebugFlags(new DebugFlagsImpl(""));
}
@Ignore("not visible on spock")
@Test
public void testWriteFileOnNarr3() throws Exception {
Grib.setDebugFlags(new DebugFlagsImpl("Grib/indexOnly"));
ProjectionRect rect = new ProjectionRect(ProjectionPoint.create(-5645, -4626), 11329, 8992);
testFileSize("B:/ncdc/0409/narr/Narr_A_fc.ncx4",
"Accum_snow_surface,Convective_cloud_cover_entire_atmosphere_3_Hour_Average", "2014-10-01T21:00:00Z",
"2014-10-02T21:00:00Z", rect, true);
Grib.setDebugFlags(new DebugFlagsImpl(""));
}
@Ignore("not visible on spock")
@Test
public void testWriteFileOnNarr4() throws Exception {
Grib.setDebugFlags(new DebugFlagsImpl("Grib/indexOnly"));
ProjectionRect rect = new ProjectionRect(ProjectionPoint.create(-5645, -4626), 11329, 8992);
testFileSize("B:/ncdc/0409/narr/Narr_A_fc.ncx4",
"Convective_cloud_cover_entire_atmosphere_3_Hour_Average,Accum_snow_surface", "2014-10-01T21:00:00Z",
"2014-10-02T21:00:00Z", rect, true);
Grib.setDebugFlags(new DebugFlagsImpl(""));
}
private void testFileSize(String fileIn, String gridNames, String startDate, String endDate, ProjectionRect rect,
boolean writeFile) throws Exception {
System.out.printf("Open %s%n", fileIn);
String fileOut = tempFolder.newFile().getAbsolutePath();
long subsetSize;
List<String> gridList = new ArrayList<>();
String[] grids = gridNames.split(",");
Collections.addAll(gridList, grids);
try (GridDataset dataset = GridDataset.open(fileIn)) {
CalendarDate date1 = CalendarDate.parseISOformat(null, startDate);
CalendarDate date2 = (endDate == null) ? date1 : CalendarDate.parseISOformat(null, endDate);
CalendarDateRange dateRange = CalendarDateRange.of(date1, date2);
// static public long makeSizeEstimate(ucar.nc2.dt.GridDataset gds, List<String> gridList,
// LatLonRect llbb, ProjectionRect projRect, int horizStride, Range zRange,
// CalendarDateRange dateRange, int stride_time, boolean addLatLon) throws IOException, InvalidRangeException {
// long totalSize = CFGridWriter2.makeSizeEstimate(dataset, null, null, null, 1, null, null, 1, true);
// System.out.printf(" estSize size all = %d%n", totalSize);
// long estSize = CFGridWriter2.makeSizeEstimate(dataset, gridList, null, rect, 1, null, dateRange, 1, false);
// System.out.printf(" estSize size %s only = %d%n", gridNames, estSize);
// static public long writeFile(ucar.nc2.dt.GridDataset gds, List<String> gridList,
// LatLonRect llbb, ProjectionRect projRect, int horizStride, Range zRange,
// CalendarDateRange dateRange, int stride_time, boolean addLatLon, NetcdfFileWriter writer) throws IOException,
// InvalidRangeException {
if (writeFile) {
NetcdfFileWriter writer = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, fileOut);
subsetSize = CFGridWriter2.writeFile(dataset, gridList, null, rect, 1, null, dateRange, 1, false, writer);
System.out.printf(" subset size %s only = %d%n", gridNames, subsetSize);
// assert subsetSize == estSize;
} else
return;
}
try (NetcdfFile ncfile = NetcdfFiles.open(fileOut)) {
long total = 0;
for (String grid : grids) {
Variable w = ncfile.getRootGroup().findVariableLocal(grid);
assert w != null;
total += w.getSize() * w.getElementSize();
System.out.printf(" actual size of %s = %d%n", grid, w.getSize() * w.getElementSize());
}
System.out.printf(" actual grids size = %d%n", total);
total = 0;
for (Variable v : ncfile.getVariables())
total += v.getSize() * v.getElementSize();
System.out.printf(" actual all variable size = %d%n", total);
}
}
@Test
@Category(NeedsCdmUnitTest.class)
public void testAxisProjUnitMismatch() throws Exception {
String fileOut = tempFolder.newFile().getAbsolutePath();
String varName = "HNW";
final double falseEastingMeters = 400000.0;
final double falseEastingKm = falseEastingMeters / 1000;
try (GridDataset gds = GridDataset.open(TestDir.cdmUnitTestDir + "ncss/test/falseEastingNorthingScaleReset.nc4")) {
Variable x = gds.getNetcdfFile().findVariable("x");
assertNotNull(x);
Attribute xUnits = x.findAttribute(CF.UNITS);
assertNotNull(xUnits);
assertThat(xUnits.getStringValue()).isEqualTo("m");
Variable proj = gds.getNetcdfFile().findVariable("lambert_conformal_conic");
assertNotNull(proj);
Attribute feAttr = proj.findAttribute(CF.FALSE_EASTING);
assertNotNull(feAttr);
// false_easting in meters
assertThat(feAttr.getNumericValue()).isEqualTo(falseEastingMeters);
// setup subset
List<String> gridList = new ArrayList<>();
gridList.add(varName);
NetcdfFileWriter writer = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, fileOut);
// write subset
CFGridWriter2.writeFile(gds, gridList, null, null, 1, null, null, 1, true, writer);
}
try (NetcdfFile ncf = NetcdfFiles.open(fileOut)) {
Variable proj = ncf.findVariable("lambert_conformal_conic");
assertNotNull(proj);
Attribute feAttr = proj.findAttribute(CF.FALSE_EASTING);
assertNotNull(feAttr);
// GeoX in km
Variable x = ncf.findVariable("x");
Attribute xUnits = x.findAttribute(CF.UNITS);
assertNotNull(xUnits);
assertThat(xUnits.getStringValue()).isEqualTo("km");
// false_easting in km
assertThat(feAttr.getNumericValue()).isEqualTo(falseEastingKm);
}
}
}