forked from Unidata/netcdf-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGribCdmIndexUpdating.java
More file actions
191 lines (164 loc) · 7.15 KB
/
Copy pathTestGribCdmIndexUpdating.java
File metadata and controls
191 lines (164 loc) · 7.15 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
/*
* Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.nc2.grib;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import thredds.featurecollection.FeatureCollectionConfig;
import thredds.featurecollection.FeatureCollectionType;
import thredds.inventory.CollectionUpdateType;
import ucar.nc2.Dimension;
import ucar.nc2.Group;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFiles;
import ucar.nc2.Variable;
import ucar.nc2.grib.collection.GribCdmIndex;
import ucar.unidata.util.test.category.NeedsCdmUnitTest;
import ucar.unidata.util.test.TestDir;
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Test GribCdmIndex scanning
*
* @author caron
* @since 10/15/2014
*/
@RunWith(Parameterized.class)
@Category(NeedsCdmUnitTest.class)
public class TestGribCdmIndexUpdating {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> getTestParameters() {
List<Object[]> result = new ArrayList<>();
// file partition
String dataDir = TestDir.cdmUnitTestDir + "gribCollections/changing/filePartition/";
FeatureCollectionConfig config = new FeatureCollectionConfig("TestGribCdmIndex", "changing/filePartition",
FeatureCollectionType.GRIB1, dataDir + "GFS_CONUS_80km_#yyyyMMdd_HHmm#.grib1", null, null, null, "file", null);
result
.add(new Object[] {dataDir, "GFS_CONUS_80km_20141024_1200.grib1", config, "Relative_humidity_isobaric", 4, 3});
/*
* directory partition
* dataDir = TestDir.cdmUnitTestDir + "ncss/GFS/CONUS_80km/";
* config = new FeatureCollectionConfig("TestGribCdmIndex", "GFS_CONUS_80km/TestGribCdmIndex",
* FeatureCollectionType.GRIB1,
* dataDir + "GFS_CONUS_80km_#yyyyMMdd_HHmm#.grib1", null, null, null, null, null); rm
* result.add(new Object[]{dataDir, "GFS_CONUS_80km_20120227_0000.grib1", config,
* "TestGribCdmIndex-CONUS_80km.ncx2", "VAR_7-0-2-52_L100", 10, 9}); //
*/
// directory partition
// String dataDir2 = "B:/motherlode/rfc/";
// FeatureCollectionConfig config2 = new FeatureCollectionConfig("RFC", "grib/NPVU/RFC",
// FeatureCollectionType.GRIB1,
// "B:/motherlode/rfc/**/.*grib1$", "yyyyMMdd#.grib1#", null, "directory", null, null);
// result.add(new Object[]{dataDir2, "kalr/NPVU_RFC_KALR_NWS_152_20141003.grib1", config2, "RFC-rfc.ncx2",
// "VAR_9-161-128-237_L1_I1_Hour_S4", 336, 310});
return result;
}
@Parameterized.AfterParam
public static void after(String dataDir, String newModel, FeatureCollectionConfig config, String varName, int orgLen,
int remLen) {
// cleanup index files created during test for a given parameter input
cleanUpIndexFiles(GribCdmIndex.getTopIndexFileFromConfig(config).getPath());
}
///////////////////////////////////////
String dataDir;
String newModel;
FeatureCollectionConfig config;
String indexFile;
String varName;
int orgLen, remLen;
public TestGribCdmIndexUpdating(String dataDir, String newModel, FeatureCollectionConfig config, String varName,
int orgLen, int remLen) throws IOException {
this.dataDir = dataDir;
this.newModel = newModel;
this.config = config;
this.varName = varName;
this.orgLen = orgLen;
this.remLen = remLen;
GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
this.indexFile = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
}
@Test
public void testRemoveFileFromCollectionAlways() throws IOException {
testRemoveFileFromCollection(CollectionUpdateType.always, orgLen, remLen);
}
@Test
public void testRemoveFileFromCollectionTest() throws IOException {
testRemoveFileFromCollection(CollectionUpdateType.test, orgLen, remLen);
}
@Test
public void testRemoveFileFromCollectionTestOnly() throws IOException {
testRemoveFileFromCollection(CollectionUpdateType.testIndexOnly, orgLen, orgLen);
}
// test when a file gets removed from a collection, ie GribCollectionBuilder
private void testRemoveFileFromCollection(CollectionUpdateType updateType, int orgLen, int remLen)
throws IOException {
System.out.printf("testRemoveFileFromCollection = %s%n", updateType);
File newModelFile = new File(dataDir + newModel);
String newModelSave = dataDir + newModel + ".save";
File newModelFileSave = new File(newModelSave);
try {
// remove one of the files from the scan
if (newModelFile.exists() && !newModelFileSave.exists()) {
boolean ok = newModelFile.renameTo(newModelFileSave);
if (!ok)
throw new IOException("cant rename file " + newModelFile);
} else if (!newModelFile.exists() && newModelFileSave.exists()) {
System.out.println("already renamed");
} else if (!newModelFile.exists() && !newModelFileSave.exists()) {
throw new IOException("missing " + newModelFile.getPath());
} else if (newModelFile.exists() && newModelFileSave.exists()) {
boolean ok = newModelFile.delete();
if (!ok)
throw new IOException("cant delete file " + newModelFile.getPath());
}
// index this
boolean changed = GribCdmIndex.updateGribCollection(config, updateType, logger);
System.out.printf("changed = %s%n", changed);
// open the resulting index
try (NetcdfFile ncfile = NetcdfFiles.open(indexFile)) {
System.out.printf("opened = %s%n", ncfile.getLocation());
Group g = ncfile.findGroup("TwoD");
assert g != null;
Variable v = g.findVariableLocal(varName);
Assert.assertNotNull(varName, v);
Dimension dim0 = v.getDimension(0);
Assert.assertEquals(v.getFullName(), remLen, dim0.getLength());
}
// new file arrives
boolean ok = newModelFileSave.renameTo(newModelFile);
if (!ok)
throw new IOException("cant rename file");
// redo the index
boolean changed2 = GribCdmIndex.updateGribCollection(config, updateType, logger);
System.out.printf("changed2 = %s%n", changed2);
// open the resulting index
try (NetcdfFile ncfile = NetcdfFiles.open(indexFile)) {
System.out.printf("opened = %s%n", ncfile.getLocation());
Group g = ncfile.findGroup("TwoD");
assert g != null;
Variable v = g.findVariableLocal(varName);
assert v != null;
Dimension dim0 = v.getDimension(0);
assert dim0.getLength() == orgLen : dim0.getLength() + " should be " + orgLen;
}
} finally { // leave it it the way we found it
boolean ok = newModelFileSave.renameTo(newModelFile);
}
}
private static void cleanUpIndexFiles(String indexFile) {
File indexDir = new File(indexFile).getParentFile();
Arrays.stream(indexDir.listFiles()).filter(f -> f.getName().endsWith(GribCdmIndex.NCX_SUFFIX))
.forEach(File::delete);
}
}