Skip to content

Commit 2df77fc

Browse files
committed
create and write era5 time series instants
1 parent 38345ef commit 2df77fc

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

core/src/test/java/com/bc/fiduceo/NCTestUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ public static Variable getVariable(String variableName, NetcdfFile netcdfFile, b
166166
return netcdfFile.findVariable(escapedName);
167167
}
168168

169+
public static void assert2DValueInt(int x, int y, int expected, Variable variable) throws IOException, InvalidRangeException {
170+
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
171+
final Array data = variable.read(new int[]{y, x}, new int[]{1, 1});
172+
assertEquals(expected, data.getInt(0));
173+
}
174+
169175
public static void assert3DValueDouble(int x, int y, int z, double expected, Variable variable) throws IOException, InvalidRangeException {
170176
assertNotNull("NetCDF Variable '" + variable.getShortName() + "' expected", variable);
171177
final Array data = variable.read(new int[]{z, y, x}, new int[]{1, 1, 1});

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
@@ -127,7 +127,7 @@ protected void compute(NetcdfFile reader, NetcdfFileWriter writer) throws IOExce
127127
}
128128

129129
if (matchupFields != null) {
130-
matchupFields.compute(configuration, reader);
130+
matchupFields.compute(configuration, reader, writer);
131131
}
132132
}
133133

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.bc.fiduceo.util.NetCDFUtils;
55
import ucar.ma2.Array;
66
import ucar.ma2.DataType;
7+
import ucar.ma2.Index;
78
import ucar.ma2.InvalidRangeException;
89
import ucar.nc2.*;
910

@@ -14,6 +15,8 @@
1415

1516
class MatchupFields {
1617

18+
private static final int SECS_PER_HOUR = 3600;
19+
1720
private Map<String, TemplateVariable> variables;
1821

1922
void prepare(MatchupFieldsConfiguration matchupFieldsConfig, NetcdfFile reader, NetcdfFileWriter writer) {
@@ -31,28 +34,53 @@ void prepare(MatchupFieldsConfiguration matchupFieldsConfig, NetcdfFile reader,
3134
addTimeVariable(matchupFieldsConfig, dimensions, writer);
3235
}
3336

34-
void compute(Configuration config, NetcdfFile reader) throws IOException, InvalidRangeException {
37+
void compute(Configuration config, NetcdfFile reader, NetcdfFileWriter writer) throws IOException, InvalidRangeException {
3538
final Era5Archive era5Archive = new Era5Archive(config.getNWPAuxDir());
3639
final MatchupFieldsConfiguration matchupConfig = config.getMatchupFields();
3740

3841
// allocate cache large enough to hold the time-series for one Era-5 variable
39-
final int cacheSize = matchupConfig.get_time_steps_future() + matchupConfig.get_time_steps_past() + 1;
40-
final VariableCache variableCache = new VariableCache(era5Archive, cacheSize);
42+
final int numTimeSteps = matchupConfig.get_time_steps_future() + matchupConfig.get_time_steps_past() + 1;
43+
final VariableCache variableCache = new VariableCache(era5Archive, numTimeSteps);
4144

4245
try {
4346
// open input time variable
4447
// + read completely
4548
// + convert to ERA-5 time stamps
46-
// - calculate time past and future timestamps for each matchup
47-
// - write to MMD
48-
final Array timeArray = VariableUtils.readTimeArray(matchupConfig.get_time_variable_name(), reader);
49-
final Array era5TimeArray = convertToEra5TimeStamp(timeArray);
49+
// + calculate time past and future timestamps for each matchup
50+
// + write to MMD
51+
final Variable nwpTimeVariable = NetCDFUtils.getVariable(writer, matchupConfig.get_nwp_time_variable_name());
52+
final Array targetTimeArray = createTimeArray(reader, matchupConfig, numTimeSteps, nwpTimeVariable);
53+
writer.write(nwpTimeVariable, targetTimeArray);
54+
55+
targetTimeArray.getIndex();
5056

51-
}finally {
57+
} finally {
5258
variableCache.close();
5359
}
5460
}
5561

62+
private static Array createTimeArray(NetcdfFile reader, MatchupFieldsConfiguration matchupConfig, int numTimeSteps, Variable nwpTimeVariable) throws IOException, InvalidRangeException {
63+
final Array timeArray = VariableUtils.readTimeArray(matchupConfig.get_time_variable_name(), reader);
64+
final Array era5TimeArray = convertToEra5TimeStamp(timeArray);
65+
final int numMatchups = era5TimeArray.getShape()[0];
66+
67+
final Array targetTimeArray = Array.factory(DataType.INT, nwpTimeVariable.getShape());
68+
final Index targetIndex = targetTimeArray.getIndex();
69+
70+
final int offset = -matchupConfig.get_time_steps_past();
71+
final Index index = era5TimeArray.getIndex();
72+
for (int i = 0; i < numMatchups; i++) {
73+
index.set(i);
74+
final int timeStamp = era5TimeArray.getInt(index);
75+
for (int k = 0; k < numTimeSteps; k++) {
76+
final int timeStep = timeStamp + (offset + k) * SECS_PER_HOUR;
77+
targetIndex.set(i, k);
78+
targetTimeArray.setInt(targetIndex, timeStep);
79+
}
80+
}
81+
return targetTimeArray;
82+
}
83+
5684
private void addTimeVariable(MatchupFieldsConfiguration matchupFieldsConfig, List<Dimension> dimensions, NetcdfFileWriter writer) {
5785
final String timeVariableName = matchupFieldsConfig.get_nwp_time_variable_name();
5886
final String escapedName = NetCDFUtils.escapeVariableName(timeVariableName);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private void removeOldest() throws IOException {
7979
}
8080

8181
if (entryToRemove != null) {
82+
System.out.println("close = " + entryToRemove.netcdfFile.getLocation());
8283
entryToRemove.netcdfFile.close();
8384

8485
cache.remove(toRemove);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static Array readTimeArray(String timeVariableName, NetcdfFile reader) throws IO
5050
} else {
5151
throw new IllegalArgumentException("Rank of time-variable not supported");
5252
}
53-
return timeArray;
53+
return timeArray.reduce();
5454
}
5555

5656
static Array convertToEra5TimeStamp(Array timeArray) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public void testAddEra5Variables() throws IOException, InvalidRangeException {
122122

123123
variable = NCTestUtils.getVariable("era5-mu-time", mmd);
124124
NCTestUtils.assertAttribute(variable, "units", "seconds since 1970-01-01");
125+
NCTestUtils.assert2DValueInt( 1, 1, 959796000, variable);
126+
NCTestUtils.assert2DValueInt( 2, 2, 959803200, variable);
127+
NCTestUtils.assert2DValueInt( 3, 2, 959806800, variable);
125128

126129
variable = NCTestUtils.getVariable("nwp_mu_u10", mmd);
127130
NCTestUtils.assertAttribute(variable, "units", "m s**-1");

0 commit comments

Comments
 (0)