-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNetCDFReaderTest.java
More file actions
174 lines (147 loc) · 6.06 KB
/
NetCDFReaderTest.java
File metadata and controls
174 lines (147 loc) · 6.06 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
package tw.fondus.commons.nc;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import tw.fondus.commons.nc.util.key.DimensionName;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
/**
* The unit test of NerCDF reader.
*
* @author Brad Chen
*
*/
public class NetCDFReaderTest {
private static final String url = "src/test/resources/QPESUMS_QPE.nc";
@BeforeAll
public static void setUp() {
Path path = Paths.get( url );
Assertions.assertTrue( Files.exists( path ) );
}
@Test
public void testBottom() throws Exception {
try ( NetCDFReader reader = NetCDFReader.read( Paths.get( url ) ) ){
Assertions.assertNotNull( reader.getNetCDF() );
Assertions.assertEquals( url, reader.getPath() );
}
}
@Test
public void testIs() throws Exception {
try ( NetCDFReader reader = NetCDFReader.readDataset( url ) ){
Assertions.assertAll( "2D NetCDF Is",
() -> Assertions.assertTrue( reader.isWGS84() ),
() -> Assertions.assertTrue( reader.is2D() ),
() -> Assertions.assertFalse( reader.is1D() )
);
}
try ( NetCDFReader reader = NetCDFReader.readDataset( "src/test/resources/Tide_6M_CWB.nc" ) ){
Assertions.assertAll( "1D NetCDF Is",
() -> Assertions.assertFalse( reader.isWGS84() ),
() -> Assertions.assertFalse( reader.is2D() ),
() -> Assertions.assertTrue( reader.is1D() )
);
}
}
@Test
public void testTimes() throws Exception {
try ( NetCDFReader reader = NetCDFReader.readDataset( url ) ){
Assertions.assertTrue( reader.hasTime() );
Assertions.assertTrue( reader.findTimes().size() > 0 );
}
}
@Test
public void testDimensionLength() throws Exception {
try ( NetCDFReader reader = NetCDFReader.readDataset( url ) ){
Assertions.assertEquals( 144, reader.getDimensionLength( DimensionName.TIME ) );
Assertions.assertEquals( 441, reader.getDimensionLength( DimensionName.X ) );
Assertions.assertEquals( 561, reader.getDimensionLength( DimensionName.Y ) );
}
}
@Test
public void testRead() throws Exception {
try ( NetCDFReader reader = NetCDFReader.read( url )){
Assertions.assertAll( "Get All",
() -> Assertions.assertFalse( reader.getGlobalAttributes().isEmpty() ),
() -> Assertions.assertFalse( reader.getDimensions().isEmpty() ),
() -> Assertions.assertFalse( reader.getVariables().isEmpty() )
);
Assertions.assertAll( "Find",
() -> Assertions.assertTrue( reader.findGlobalAttribute( "references" ).isPresent() ),
() -> Assertions.assertTrue( reader.findDimension( "time" ).isPresent() ),
() -> Assertions.assertTrue( reader.findVariable( "precipitation_radar" ).isPresent() )
);
Assertions.assertAll( "Has",
() -> Assertions.assertTrue( reader.hasGlobalAttribute( "references" ) ),
() -> Assertions.assertTrue( reader.hasDimension( "time" ) ),
() -> Assertions.assertTrue( reader.hasVariable( "x" ) ),
() -> Assertions.assertTrue( reader.hasVariable( "y" ) ),
() -> Assertions.assertTrue( reader.hasVariable( "precipitation_radar" ) )
);
Assertions.assertAll( "Read",
() -> Assertions.assertTrue( reader.readVariable( "time" ).isPresent() ),
() -> Assertions.assertTrue( reader.readVariable( "x" ).isPresent() ),
() -> Assertions.assertTrue( reader.readVariable( "y" ).isPresent() ),
() -> Assertions.assertTrue( reader.readVariable( "precipitation_radar" ).isPresent() )
);
}
}
@Test
public void testReadFirstValue() throws Exception {
try ( NetCDFReader reader = NetCDFReader.read( url )){
Assertions.assertTrue( reader.findFirstX().isPresent() );
Assertions.assertTrue( reader.findFirstY().isPresent() );
reader.findFirstX().ifPresent( value -> Assertions.assertEquals( new BigDecimal( "118.00625" ), value ) );
reader.findFirstY().ifPresent( value -> Assertions.assertEquals( new BigDecimal( "19.99375" ), value ) );
}
}
@Test
public void testReadLastValue() throws Exception {
try ( NetCDFReader reader = NetCDFReader.read( url )){
Assertions.assertTrue( reader.findLastX().isPresent() );
Assertions.assertTrue( reader.findLastY().isPresent() );
reader.findLastX().ifPresent( value -> Assertions.assertEquals( new BigDecimal( "123.50625" ), value ) );
reader.findLastY().ifPresent( value -> Assertions.assertEquals( new BigDecimal( "26.99375" ), value ) );
}
}
@Test
public void testFindCoordinates() throws Exception {
try ( NetCDFReader reader = NetCDFReader.read( url )){
Optional<List<BigDecimal>> optionalY = reader.findYCoordinates();
Optional<List<BigDecimal>> optionalX = reader.findXCoordinates();
Assertions.assertTrue( optionalY.isPresent() );
Assertions.assertTrue( optionalX.isPresent() );
optionalY.ifPresent( y -> {
reader.findFirstY().ifPresent( firstY -> Assertions.assertEquals( firstY, y.get( 0 ) ) );
reader.findLastY().ifPresent( lastY -> Assertions.assertEquals( lastY, y.get( y.size() - 1 ) ) );
} );
optionalX.ifPresent( x -> {
reader.findFirstX().ifPresent( firstX -> Assertions.assertEquals( firstX, x.get( 0 ) ) );
reader.findLastX().ifPresent( lastX -> Assertions.assertEquals( lastX, x.get( x.size() - 1 ) ) );
} );
}
try ( NetCDFReader reader = NetCDFReader.read( "src/test/resources/Tide_6M_CWB.nc" )){
Optional<List<BigDecimal>> optionalY = reader.findLatCoordinates();
Optional<List<BigDecimal>> optionalX = reader.findLonCoordinates();
Assertions.assertTrue( optionalY.isPresent() );
Assertions.assertTrue( optionalX.isPresent() );
}
}
@Test
public void testFindStationId() throws Exception {
String url = "src/test/resources/Tide_6M_CWB.nc";
Path path = Paths.get( url );
Assertions.assertTrue( Files.exists( path ) );
try ( NetCDFReader reader = NetCDFReader.read( url ) ){
Optional<List<String>> optional = reader.findStationIds();
Assertions.assertTrue( optional.isPresent() );
optional.ifPresent( ids -> {
Assertions.assertEquals( "1102", ids.get( 0 ) );
Assertions.assertEquals( "1116", ids.get( 1 ) );
} );
}
}
}