Skip to content

Commit 9fc9935

Browse files
committed
matchup fields intermediate
1 parent b3e81b8 commit 9fc9935

File tree

9 files changed

+256
-7
lines changed

9 files changed

+256
-7
lines changed

post-processing-tool/src/main/java/com/bc/fiduceo/post/plugin/era5/Era5PostProcessing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected void prepare(NetcdfFile reader, NetcdfFileWriter writer) {
116116
final MatchupFieldsConfiguration matchupFieldsConfig = configuration.getMatchupFields();
117117
if (matchupFieldsConfig != null) {
118118
matchupFields = new MatchupFields();
119-
matchupFields.prepare();
119+
matchupFields.prepare(matchupFieldsConfig, reader, writer);
120120
}
121121
}
122122

post-processing-tool/src/main/java/com/bc/fiduceo/post/plugin/era5/Era5PostProcessingPlugin.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ private static void parseMatchupFields(Element rootElement, Configuration config
196196
matchupFieldsConfiguration.set_fc_msshf_name(getElementValueTrimmed(msshfElement));
197197
}
198198

199+
final Element timeStepsPastElement = matchupFieldsElements.getChild("time_steps_past");
200+
if (timeStepsPastElement != null) {
201+
final String value = timeStepsPastElement.getValue();
202+
matchupFieldsConfiguration.setTime_steps_past(Integer.parseInt(value));
203+
}
204+
205+
final Element timeStepsFutureElement = matchupFieldsElements.getChild("time_steps_future");
206+
if (timeStepsFutureElement != null) {
207+
final String value = timeStepsFutureElement.getValue();
208+
matchupFieldsConfiguration.setTime_steps_future(Integer.parseInt(value));
209+
}
210+
final Element timeDimNameElement = matchupFieldsElements.getChild("time_dim_name");
211+
if (timeDimNameElement != null) {
212+
matchupFieldsConfiguration.setTime_dim_name(getElementValueTrimmed(timeDimNameElement));
213+
}
214+
199215
configuration.setMatchupFields(matchupFieldsConfiguration);
200216
}
201217
}
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
package com.bc.fiduceo.post.plugin.era5;
22

3+
import ucar.nc2.NetcdfFile;
4+
import ucar.nc2.NetcdfFileWriter;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
39
class MatchupFields {
410

5-
void prepare() {
11+
private Map<String, TemplateVariable> variables;
12+
13+
void prepare(MatchupFieldsConfiguration matchupFieldsConfig, NetcdfFile reader, NetcdfFileWriter writer) {
14+
matchupFieldsConfig.verify();
15+
setDimensions(matchupFieldsConfig, writer, reader);
16+
17+
variables = getVariables(matchupFieldsConfig);
18+
// @todo 1 tb/tb ad variables here 2020-12-04
619

720
}
821

922
void compute() {
1023

1124
}
25+
26+
// package access for testing purpose only tb 2020-12-02
27+
void setDimensions(MatchupFieldsConfiguration matchupFieldsConfig, NetcdfFileWriter writer, NetcdfFile reader) {
28+
final int time_steps_past = matchupFieldsConfig.getTime_steps_past();
29+
final int time_steps_future = matchupFieldsConfig.getTime_steps_future();
30+
final int time_dim_length = time_steps_past + time_steps_future + 1;
31+
final String time_dim_name = matchupFieldsConfig.getTime_dim_name();
32+
33+
writer.addDimension(time_dim_name, time_dim_length);
34+
}
35+
36+
// package access for testing purpose only tb 2020-12-03
37+
Map<String, TemplateVariable> getVariables(MatchupFieldsConfiguration configuration) {
38+
final HashMap<String, TemplateVariable> variablesMap = new HashMap<>();
39+
40+
variablesMap.put("an_sfc_u10", new TemplateVariable(configuration.get_an_u10_name(), "m s**-1", "10 metre U wind component", null, false));
41+
variablesMap.put("an_sfc_v10", new TemplateVariable(configuration.get_an_v10_name(), "m s**-1", "10 metre V wind component", null, false));
42+
variablesMap.put("an_sfc_siconc", new TemplateVariable(configuration.get_an_siconc_name(), "(0 - 1)", "Sea ice area fraction", "sea_ice_area_fraction", false));
43+
variablesMap.put("an_sfc_sst", new TemplateVariable(configuration.get_an_sst_name(), "K", "Sea surface temperature", null, false));
44+
variablesMap.put("fc_sfc_metss", new TemplateVariable(configuration.get_fc_metss_name(), "N m**-2", "Mean eastward turbulent surface stress", null, false));
45+
variablesMap.put("fc_sfc_mntss", new TemplateVariable(configuration.get_fc_mntss_name(), "N m**-2", "Mean northward turbulent surface stress", null, false));
46+
variablesMap.put("fc_sfc_mslhf", new TemplateVariable(configuration.get_fc_mslhf_name(), "W m**-2", "Mean surface latent heat flux", null, false));
47+
variablesMap.put("fc_sfc_msnlwrf", new TemplateVariable(configuration.get_fc_msnlwrf_name(), "W m**-2", "Mean surface net long-wave radiation flux", null, false));
48+
variablesMap.put("fc_sfc_msnswrf", new TemplateVariable(configuration.get_fc_msnswrf_name(), "W m**-2", "Mean surface net short-wave radiation flux", null, false));
49+
variablesMap.put("fc_sfc_msshf", new TemplateVariable(configuration.get_fc_msshf_name(), "W m**-2", "Mean surface sensible heat flux", null, false));
50+
51+
return variablesMap;
52+
}
1253
}

post-processing-tool/src/main/java/com/bc/fiduceo/post/plugin/era5/MatchupFieldsConfiguration.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.bc.fiduceo.post.plugin.era5;
22

3+
import org.esa.snap.core.util.StringUtils;
4+
35
class MatchupFieldsConfiguration {
46

57
private String an_u10_name;
@@ -13,6 +15,10 @@ class MatchupFieldsConfiguration {
1315
private String fc_msnswrf_name;
1416
private String fc_msshf_name;
1517

18+
private int time_steps_past;
19+
private int time_steps_future;
20+
private String time_dim_name;
21+
1622
MatchupFieldsConfiguration() {
1723
an_u10_name = "nwp_mu_u10";
1824
an_v10_name = "nwp_mu_v10";
@@ -24,6 +30,10 @@ class MatchupFieldsConfiguration {
2430
fc_msnlwrf_name = "nwp_mu_msnlwrf";
2531
fc_msnswrf_name = "nwp_mu_msnswrf";
2632
fc_msshf_name = "nwp_mu_msshf";
33+
34+
time_steps_past = -1;
35+
time_steps_future = -1;
36+
time_dim_name = null;
2737
}
2838

2939
String get_an_u10_name() {
@@ -105,4 +115,41 @@ String get_fc_msshf_name() {
105115
void set_fc_msshf_name(String fc_msshf_name) {
106116
this.fc_msshf_name = fc_msshf_name;
107117
}
118+
119+
int getTime_steps_past() {
120+
return time_steps_past;
121+
}
122+
123+
void setTime_steps_past(int time_steps_past) {
124+
this.time_steps_past = time_steps_past;
125+
}
126+
127+
int getTime_steps_future() {
128+
return time_steps_future;
129+
}
130+
131+
void setTime_steps_future(int time_steps_future) {
132+
this.time_steps_future = time_steps_future;
133+
}
134+
135+
String getTime_dim_name() {
136+
return time_dim_name;
137+
}
138+
139+
void setTime_dim_name(String time_dim_name) {
140+
this.time_dim_name = time_dim_name;
141+
}
142+
143+
boolean verify() {
144+
if (time_steps_past < 0) {
145+
throw new IllegalArgumentException("time steps past not configured");
146+
}
147+
if (time_steps_future < 0) {
148+
throw new IllegalArgumentException("time steps future not configured");
149+
}
150+
if (StringUtils.isNullOrEmpty(time_dim_name)){
151+
throw new IllegalArgumentException("time dimension name not configured");
152+
}
153+
return true;
154+
}
108155
}

post-processing-tool/src/main/java/com/bc/fiduceo/post/plugin/era5/SatelliteFields.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static Array convertToEra5TimeStamp(Array timeArray) {
6666
}
6767

6868
void prepare(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFile reader, NetcdfFileWriter writer) {
69+
satFieldsConfig.verify();
6970
setDimensions(satFieldsConfig, writer, reader);
7071

7172
variables = getVariables(satFieldsConfig);
@@ -82,7 +83,6 @@ void prepare(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFile reader, Ne
8283

8384
void compute(Configuration config, NetcdfFile reader, NetcdfFileWriter writer) throws IOException, InvalidRangeException {
8485
final SatelliteFieldsConfiguration satFieldsConfig = config.getSatelliteFields();
85-
// @todo 2 tb/tb ensure valid range 2020-11-25
8686
final int numLayers = satFieldsConfig.get_z_dim();
8787
final Era5Archive era5Archive = new Era5Archive(config.getNWPAuxDir());
8888
final VariableCache variableCache = new VariableCache(era5Archive, 52); // 4 * 13 variables tb 2020-11-25
@@ -321,7 +321,6 @@ List<Dimension> getDimensions(TemplateVariable template) {
321321

322322
// package access for testing purpose only tb 2020-12-02
323323
void setDimensions(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFileWriter writer, NetcdfFile reader) {
324-
satFieldsConfig.verify();
325324
final Dimension xDim = writer.addDimension(satFieldsConfig.get_x_dim_name(), satFieldsConfig.get_x_dim());
326325
final Dimension yDim = writer.addDimension(satFieldsConfig.get_y_dim_name(), satFieldsConfig.get_y_dim());
327326

post-processing-tool/src/test/java/com/bc/fiduceo/post/PostProcessingToolIntegrationTest_Era5.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ public void testAddEra5Variables() throws IOException, InvalidRangeException {
8282
NCTestUtils.assert3DValueDouble(1, 0, 0, 4087, variable);
8383

8484
NCTestUtils.assertDimension(FiduceoConstants.MATCHUP_COUNT, 7, mmd);
85+
86+
// satellite fields
8587
NCTestUtils.assertDimension("left", 5, mmd);
8688
NCTestUtils.assertDimension("right", 7, mmd);
8789
NCTestUtils.assertDimension("up", 23, mmd);
8890

89-
// @todo 1 tb/tb add assertions
90-
//
9191
variable = NCTestUtils.getVariable("nwp_q", mmd);
9292
NCTestUtils.assertAttribute(variable, "units", "kg kg**-1");
9393
NCTestUtils.assert4DVariable(variable.getFullName(), 2, 0, 0, 0, 2.067875129796448E-6, mmd);
@@ -116,6 +116,9 @@ public void testAddEra5Variables() throws IOException, InvalidRangeException {
116116
NCTestUtils.assertAttribute(variable, "units", "seconds since 1970-01-01");
117117
NCTestUtils.assert1DValueLong(2, 1212400800, variable);
118118
NCTestUtils.assert1DValueLong(6, 1212145200, variable);
119+
120+
// matchup fields
121+
NCTestUtils.assertDimension("the_time", 54, mmd);
119122
}
120123
}
121124

@@ -143,6 +146,9 @@ private void writeConfiguration() throws IOException {
143146
" <latitude_variable>amsre.latitude</latitude_variable>" +
144147
" </satellite-fields>" +
145148
" <matchup-fields>" +
149+
" <time_steps_past>41</time_steps_past>" +
150+
" <time_steps_future>12</time_steps_future>" +
151+
" <time_dim_name>the_time</time_dim_name>" +
146152
" </matchup-fields>" +
147153
" </era5>\n" +
148154
" </post-processings>\n" +

post-processing-tool/src/test/java/com/bc/fiduceo/post/plugin/era5/Era5PostProcessingPluginTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public void testCreateConfiguration_matchupFields() throws JDOMException, IOExce
125125
" <fc_sfc_msnlwrf>longWave</fc_sfc_msnlwrf>" +
126126
" <fc_sfc_msnswrf>shortWave</fc_sfc_msnswrf>" +
127127
" <fc_sfc_msshf>heat_flux</fc_sfc_msshf>" +
128+
"" +
129+
" <time_steps_past>14</time_steps_past>" +
130+
" <time_steps_future>15</time_steps_future>" +
131+
" <time_dim_name>sapperlot</time_dim_name>" +
128132
" </matchup-fields>" +
129133
"</era5>";
130134
final Element rootElement = TestUtil.createDomElement(XML);
@@ -143,6 +147,10 @@ public void testCreateConfiguration_matchupFields() throws JDOMException, IOExce
143147
assertEquals("longWave", matchupConfig.get_fc_msnlwrf_name());
144148
assertEquals("shortWave", matchupConfig.get_fc_msnswrf_name());
145149
assertEquals("heat_flux", matchupConfig.get_fc_msshf_name());
150+
151+
assertEquals(14, matchupConfig.getTime_steps_past());
152+
assertEquals(15, matchupConfig.getTime_steps_future());
153+
assertEquals("sapperlot", matchupConfig.getTime_dim_name());
146154
}
147155

148156
@Test

post-processing-tool/src/test/java/com/bc/fiduceo/post/plugin/era5/MatchupFieldsConfigurationTest.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.junit.Before;
44
import org.junit.Test;
55

6-
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.*;
77

88
public class MatchupFieldsConfigurationTest {
99

@@ -27,6 +27,9 @@ public void testConstructionAndDefaultValues() {
2727
assertEquals("nwp_mu_msnlwrf", config.get_fc_msnlwrf_name());
2828
assertEquals("nwp_mu_msnswrf", config.get_fc_msnswrf_name());
2929
assertEquals("nwp_mu_msshf", config.get_fc_msshf_name());
30+
31+
assertEquals(-1, config.getTime_steps_past());
32+
assertEquals(-1, config.getTime_steps_future());
3033
}
3134

3235
@Test
@@ -88,4 +91,67 @@ public void testSetGet_fc_msshf() {
8891
config.set_fc_msshf_name("heffalump");
8992
assertEquals("heffalump", config.get_fc_msshf_name());
9093
}
94+
95+
@Test
96+
public void testSetGetTimeStepsPast() {
97+
config.setTime_steps_past(12);
98+
assertEquals(12, config.getTime_steps_past());
99+
}
100+
101+
@Test
102+
public void testSetGetTimeStepsFuture() {
103+
config.setTime_steps_future(13);
104+
assertEquals(13, config.getTime_steps_future());
105+
}
106+
107+
@Test
108+
public void testSetGetTimeDimName() {
109+
config.setTime_dim_name("Rolex");
110+
assertEquals("Rolex", config.getTime_dim_name());
111+
}
112+
113+
@Test
114+
public void testVerify() {
115+
config.setTime_steps_past(13);
116+
config.setTime_steps_future(14);
117+
config.setTime_dim_name("ticktock");
118+
119+
assertTrue(config.verify());
120+
}
121+
122+
@Test
123+
public void testVerify_missingTimeStepsPast() {
124+
// config.setTime_steps_past(13);
125+
config.setTime_steps_future(14);
126+
config.setTime_dim_name("ticktock");
127+
try {
128+
assertTrue(config.verify());
129+
fail("IllegalArgumentException expected");
130+
} catch (IllegalArgumentException expected) {
131+
}
132+
}
133+
134+
@Test
135+
public void testVerify_missingTimeStepsFuture() {
136+
config.setTime_steps_past(13);
137+
// config.setTime_steps_future(14);
138+
config.setTime_dim_name("ticktock");
139+
try {
140+
assertTrue(config.verify());
141+
fail("IllegalArgumentException expected");
142+
} catch (IllegalArgumentException expected) {
143+
}
144+
}
145+
146+
@Test
147+
public void testVerify_missingTimeDimName() {
148+
config.setTime_steps_past(13);
149+
config.setTime_steps_future(14);
150+
// config.setTime_dim_name("ticktock");
151+
try {
152+
assertTrue(config.verify());
153+
fail("IllegalArgumentException expected");
154+
} catch (IllegalArgumentException expected) {
155+
}
156+
}
91157
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.bc.fiduceo.post.plugin.era5;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import ucar.nc2.Dimension;
6+
import ucar.nc2.NetcdfFileWriter;
7+
8+
import java.util.Map;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertNull;
13+
import static org.mockito.ArgumentMatchers.anyInt;
14+
import static org.mockito.ArgumentMatchers.anyString;
15+
import static org.mockito.Mockito.*;
16+
import static org.mockito.Mockito.verifyNoMoreInteractions;
17+
18+
public class MatchupFieldsTest {
19+
20+
private MatchupFieldsConfiguration config;
21+
private MatchupFields matchupFields;
22+
23+
@Before
24+
public void setUp() {
25+
config = new MatchupFieldsConfiguration();
26+
matchupFields = new MatchupFields();
27+
}
28+
29+
@Test
30+
public void testGetVariables() {
31+
config.set_fc_mslhf_name("messelfh");
32+
33+
final Map<String, TemplateVariable> variables = matchupFields.getVariables(config);
34+
assertEquals(10, variables.size());
35+
36+
TemplateVariable template = variables.get("an_sfc_siconc");
37+
assertEquals("sea_ice_area_fraction", template.getStandardName());
38+
assertEquals("(0 - 1)", template.getUnits());
39+
assertEquals("Sea ice area fraction", template.getLongName());
40+
assertEquals("nwp_mu_siconc", template.getName());
41+
assertFalse(template.is3d());
42+
43+
template = variables.get("fc_sfc_mslhf");
44+
assertNull(template.getStandardName());
45+
assertEquals("W m**-2", template.getUnits());
46+
assertEquals("Mean surface latent heat flux", template.getLongName());
47+
assertEquals("messelfh", template.getName());
48+
assertFalse(template.is3d());
49+
}
50+
51+
@Test
52+
public void testSetGetDimensions() {
53+
config.setTime_dim_name("tihime");
54+
config.setTime_steps_future(12);
55+
config.setTime_steps_past(29);
56+
final int timeDimLenght = 12 + 29 +1;
57+
58+
final NetcdfFileWriter writer = mock(NetcdfFileWriter.class);
59+
when(writer.addDimension(config.getTime_dim_name(), timeDimLenght)).thenReturn(new Dimension(config.getTime_dim_name(), timeDimLenght));
60+
61+
matchupFields.setDimensions(config, writer, null);
62+
63+
verify(writer, times(1)).addDimension("tihime", timeDimLenght);
64+
verifyNoMoreInteractions(writer);
65+
}
66+
}

0 commit comments

Comments
 (0)