Skip to content

Commit 0dca8b3

Browse files
committed
assemble archive paths
1 parent 2f79ab9 commit 0dca8b3

File tree

4 files changed

+126
-22
lines changed

4 files changed

+126
-22
lines changed

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

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import com.bc.fiduceo.util.TimeUtils;
44

55
import java.io.File;
6+
import java.io.IOException;
67
import java.text.DecimalFormat;
78
import java.util.Calendar;
89

910
class ArchiveUtils {
1011

1112
private static final DecimalFormat twoDigitsFormat = new DecimalFormat("00");
13+
private static final DecimalFormat threeDigitsFormat = new DecimalFormat("000");
1214

1315
private static final String FILE_NAME_BEGIN = "ecmwf-era5_oper_";
1416

@@ -18,13 +20,34 @@ class ArchiveUtils {
1820
this.rootPath = rootPath;
1921
}
2022

21-
static String getFileName(String collection, String variable, String ymd, String hour) {
22-
return FILE_NAME_BEGIN + collection + "_" + ymd + hour + "00." + variable + ".nc";
23+
static String getFileName(String collection, String variable, String timeString) {
24+
return FILE_NAME_BEGIN + collection + "_" + timeString + "." + variable + ".nc";
2325
}
2426

25-
public String get(String variableType, int timeStamp) {
26-
final Calendar utcCalendar = TimeUtils.getUTCCalendar();
27-
utcCalendar.setTimeInMillis(timeStamp * 1000L);
27+
static String mapVariable(String variable) {
28+
switch (variable) {
29+
case "t2m":
30+
return "2t";
31+
case "u10":
32+
return "10u";
33+
case "v10":
34+
return "10v";
35+
case "siconc":
36+
return "ci";
37+
default:
38+
return variable;
39+
}
40+
}
41+
42+
static String getForecastTimeString(Calendar utcCalendar) {
43+
return null;
44+
}
45+
46+
static String getAnalysisTimeString(Calendar utcCalendar) {
47+
return null;
48+
}
49+
50+
static String getTimeString(String collection, Calendar utcCalendar) throws IOException {
2851
final int year = utcCalendar.get(Calendar.YEAR);
2952

3053
final int month = utcCalendar.get(Calendar.MONTH) + 1;
@@ -33,18 +56,55 @@ public String get(String variableType, int timeStamp) {
3356
final int day = utcCalendar.get(Calendar.DAY_OF_MONTH);
3457
final String dayString = twoDigitsFormat.format(day);
3558

36-
final int hour = utcCalendar.get(Calendar.HOUR_OF_DAY);
37-
final String hourString = twoDigitsFormat.format(hour);
59+
int hour = utcCalendar.get(Calendar.HOUR_OF_DAY);
60+
61+
if (collection.startsWith("an_")) {
62+
final String hourString = twoDigitsFormat.format(hour);
63+
return year + monthString + dayString + hourString + "00";
64+
} else if (collection.startsWith("fc_")) {
65+
int forecastTimeStep;
66+
if (hour <= 6) {
67+
utcCalendar.add(Calendar.HOUR_OF_DAY, -1);
68+
forecastTimeStep = 6 + hour;
69+
hour = 18;
70+
} else if(hour <= 18) {
71+
forecastTimeStep = hour - 6;
72+
hour = 6;
73+
} else {
74+
forecastTimeStep = hour - 18;
75+
hour = 18;
76+
}
77+
final String hourString = twoDigitsFormat.format(hour);
78+
79+
return year + monthString + dayString + hourString + threeDigitsFormat.format(forecastTimeStep);
80+
} else {
81+
throw new IOException("Unknown era5 collection: " + collection);
82+
}
83+
}
84+
85+
public String get(String variableType, int timeStamp) throws IOException {
86+
final Calendar utcCalendar = TimeUtils.getUTCCalendar();
87+
utcCalendar.setTimeInMillis(timeStamp * 1000L);
3888

3989
final int cutPoint = variableType.lastIndexOf("_");
4090
final String collection = variableType.substring(0, cutPoint);
41-
final String variable = variableType.substring(cutPoint + 1, variableType.length());
4291

43-
final String ymd = year + monthString + dayString;
44-
final String fileName = getFileName(collection, variable, ymd, hourString);
92+
String variable = variableType.substring(cutPoint + 1, variableType.length());
93+
variable = mapVariable(variable);
94+
95+
final String timeString = getTimeString(collection, utcCalendar);
96+
final String fileName = getFileName(collection, variable, timeString);
97+
98+
final int year = utcCalendar.get(Calendar.YEAR);
99+
100+
final int month = utcCalendar.get(Calendar.MONTH) + 1;
101+
final String monthString = twoDigitsFormat.format(month);
102+
103+
final int day = utcCalendar.get(Calendar.DAY_OF_MONTH);
104+
final String dayString = twoDigitsFormat.format(day);
45105

46106
return rootPath + File.separator + collection + File.separator +
47-
year + File.separator + monthString + File.separator + dayString + File.separator +
107+
year + File.separator+ monthString + File.separator + dayString + File.separator +
48108
fileName;
49109
}
50110
}

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
@@ -159,7 +159,7 @@ protected void prepare(NetcdfFile reader, NetcdfFileWriter writer) throws IOExce
159159
@Override
160160
protected void compute(NetcdfFile reader, NetcdfFileWriter writer) throws IOException, InvalidRangeException {
161161
if (satelliteFields != null) {
162-
satelliteFields.compute(configuration.getSatelliteFields(), reader, writer);
162+
satelliteFields.compute(configuration, reader, writer);
163163
}
164164

165165
if (matchupFields != null) {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ void prepare(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFile reader, Ne
5151
addTimeVariable(satFieldsConfig, writer);
5252
}
5353

54-
void compute(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFile reader, NetcdfFileWriter writer) throws IOException, InvalidRangeException {
54+
void compute(Configuration config, NetcdfFile reader, NetcdfFileWriter writer) throws IOException, InvalidRangeException {
55+
final SatelliteFieldsConfiguration satFieldsConfig = config.getSatelliteFields();
56+
final ArchiveUtils archiveUtils = new ArchiveUtils(config.getNWPAuxDir());
5557
// open input time variable
5658
// + read completely
5759
// + convert to ERA-5 time stamps
@@ -73,6 +75,7 @@ void compute(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFile reader, Ne
7375
final int[] shape = lonArray.getShape();
7476
final int[] size = {1, shape[1], shape[2]};
7577

78+
final Index index = era5TimeArray.getIndex();
7679
for (int m = 0; m < numMatches; m++) {
7780
final int[] offsets = {m, 0, 0};
7881
final Array lonLayer = lonArray.section(offsets, size);
@@ -82,14 +85,17 @@ void compute(SatelliteFieldsConfiguration satFieldsConfig, NetcdfFile reader, Ne
8285
final Rectangle era5RasterPosition = Era5PostProcessing.getEra5RasterPosition(geoRegion);
8386
final InterpolationContext interpolationContext = Era5PostProcessing.getInterpolationContext(lonLayer, latLayer);
8487

88+
index.set(m);
89+
final int era5Time = era5TimeArray.getInt(index);
90+
8591
// iterate over variables
8692
// - assemble variable file name
8793
// - read variable data extract
8894
// - interpolate (2d, 3d per layer)
8995
// - store to target raster
9096
final Set<String> variableKeys = variables.keySet();
9197
for (final String variableKey : variableKeys) {
92-
98+
final String nwpFilePath = archiveUtils.get(variableKey, era5Time);
9399
}
94100
}
95101
}

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,65 @@
22

33
import org.junit.Test;
44

5+
import java.io.File;
6+
import java.io.IOException;
7+
58
import static junit.framework.TestCase.assertEquals;
69

710
public class ArchiveUtilsTest {
811

12+
private static final String SEP = File.separator;
13+
914
@Test
10-
public void testConstructAndGet() {
11-
final ArchiveUtils archiveUtils = new ArchiveUtils("/archive/era5");
15+
public void testConstructAndGet() throws IOException {
16+
final ArchiveUtils archiveUtils = new ArchiveUtils("archive" + SEP + "era5");
1217

13-
assertEquals("/archive/era5\\an_ml\\2008\\05\\30\\ecmwf-era5_oper_an_ml_200805301100.q.nc", archiveUtils.get("an_ml_q", 1212145200));
14-
assertEquals("/archive/era5\\an_ml\\2008\\06\\02\\ecmwf-era5_oper_an_ml_200806021000.t.nc", archiveUtils.get("an_ml_t", 1212400800));
1518
// Friday, 30. May 2008 11:00:00
1619
// 1212145200
20+
String expected = assemblePath("archive", "era5", "an_ml", "2008", "05", "30", "ecmwf-era5_oper_an_ml_200805301100.q.nc");
21+
assertEquals(expected, archiveUtils.get("an_ml_q", 1212145200));
22+
23+
expected = assemblePath("archive", "era5", "an_sfc", "2008", "05", "30", "ecmwf-era5_oper_an_sfc_200805301100.2t.nc");
24+
assertEquals(expected, archiveUtils.get("an_sfc_t2m", 1212145200));
25+
26+
expected = assemblePath("archive", "era5", "fc_sfc", "2008", "05", "30", "ecmwf-era5_oper_fc_sfc_2008053006005.msnlwrf.nc");
27+
assertEquals(expected, archiveUtils.get("fc_sfc_msnlwrf", 1212145200));
28+
1729
// Monday, 2. June 2008 10:00:00
18-
//1212400800
30+
// 1212400800
31+
expected = assemblePath("archive", "era5", "an_ml", "2008", "06", "02", "ecmwf-era5_oper_an_ml_200806021000.t.nc");
32+
assertEquals(expected, archiveUtils.get("an_ml_t", 1212400800));
33+
34+
expected = assemblePath("archive", "era5", "fc_sfc", "2008", "06", "02", "ecmwf-era5_oper_fc_sfc_2008060206004.mslhf.nc");
35+
assertEquals(expected, archiveUtils.get("fc_sfc_mslhf", 1212400800));
36+
1937

20-
// an_ml/2008/05/30/ecmwf-era5_oper_an_ml_200805301100.q.nc
21-
// an_ml/2008/06/02/ecmwf-era5_oper_an_ml_200806021000.q.nc
2238
}
2339

2440
@Test
2541
public void testGetFileName() {
26-
assertEquals("ecmwf-era5_oper_an_ml_201108231900.q.nc", ArchiveUtils.getFileName("an_ml", "q", "20110823", "19"));
42+
assertEquals("ecmwf-era5_oper_an_ml_201108231900.q.nc", ArchiveUtils.getFileName("an_ml", "q", "201108231900"));
43+
}
44+
45+
@Test
46+
public void testMapVariable() {
47+
assertEquals("q", ArchiveUtils.mapVariable("q"));
48+
assertEquals("2t", ArchiveUtils.mapVariable("t2m"));
49+
assertEquals("10u", ArchiveUtils.mapVariable("u10"));
50+
assertEquals("10v", ArchiveUtils.mapVariable("v10"));
51+
assertEquals("ci", ArchiveUtils.mapVariable("siconc"));
52+
assertEquals("msl", ArchiveUtils.mapVariable("msl"));
53+
}
54+
55+
private String assemblePath(String... elements) {
56+
String path = "";
57+
58+
for (String element : elements) {
59+
path += element;
60+
path += SEP;
61+
}
62+
63+
// strip last separator tb 2020-11-23
64+
return path.substring(0, path.length() - 1);
2765
}
2866
}

0 commit comments

Comments
 (0)