Skip to content

Commit

Permalink
#377 Updated grid point data object to access all files with a given …
Browse files Browse the repository at this point in the history
…prefix and output changed values to all files at finish
  • Loading branch information
michael-topsom-deltares committed Jul 11, 2024
1 parent 04ed1e3 commit 445bb30
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,81 @@
package org.openda.model_wanda_seawat;

import org.openda.exchange.AbstractDataObject;
import org.openda.exchange.DoubleExchangeItem;
import org.openda.exchange.DoublesExchangeItem;
import org.openda.interfaces.IExchangeItem;
import org.openda.utils.io.AsciiFileUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.*;

public class WandaSeawatPointDataObject extends AbstractDataObject {
private static final int FIRST_VALUE_OFFSET = 2;
private static final int FIRST_VALUE_OFFSET = 1;
private String header;
private List<Double> heights;
private List<Double> radii;
private TreeMap<String, File> fileNamesToFiles;

@Override
public void initialize(File workingDir, String[] arguments) {
File file = new File(workingDir, arguments[0]);
List<String> lines = AsciiFileUtils.readLines(file);
if (!workingDir.exists() || !workingDir.isDirectory()) {
return;
}

if (lines.size() < 2) {
if (arguments.length == 0 || arguments[0] == null) {
return;
}

List<Double> columns = readHeader(lines);
for (String line : lines.subList(1, lines.size())) {
String[] values = line.split("\\s+");
String filePrefix = arguments[0];

fileNamesToFiles = new TreeMap<>();

if (values.length < 1) {
for (File file : workingDir.listFiles()) {
if (!file.getName().startsWith(filePrefix)) {
continue;
}

double row = Double.parseDouble(values[1]);
fileNamesToFiles.putIfAbsent(file.getName(), file);
}

Map<String, List<Double>> keyToValues = new HashMap<>();
for (Map.Entry<String, File> entry : fileNamesToFiles.entrySet()) {
List<String> lines = AsciiFileUtils.readLines(entry.getValue());

if (lines.size() < 2) {
return;
}

header = lines.get(0);
radii = readHeader(lines);
heights = new ArrayList<>();
for (String line : lines.subList(1, lines.size())) {
String[] values = line.trim().split("\\s+");

if (values.length < 1) {
continue;
}

for (int valueIndex = FIRST_VALUE_OFFSET; valueIndex < values.length; valueIndex++) {
String value = values[valueIndex];
String key = String.format("depth%s_radius%s", row, columns.get(valueIndex - FIRST_VALUE_OFFSET));
DoubleExchangeItem doubleExchangeItem = new DoubleExchangeItem(key, Double.parseDouble(value));
exchangeItems.putIfAbsent(key, doubleExchangeItem);
double height = Double.parseDouble(values[0]);
heights.add(height);

for (int valueIndex = FIRST_VALUE_OFFSET; valueIndex < values.length; valueIndex++) {
String value = values[valueIndex];
String key = String.format("depth%s_radius%s", height, radii.get(valueIndex - FIRST_VALUE_OFFSET));
List<Double> valueList = keyToValues.computeIfAbsent(key, v -> new ArrayList<>());
valueList.add(Double.parseDouble(value));

if (!Objects.equals(fileNamesToFiles.lastKey(), entry.getKey())) {
continue;
}

DoublesExchangeItem doublesExchangeItem = new DoublesExchangeItem(key, IExchangeItem.Role.InOut, valueList.stream().mapToDouble(Double::doubleValue).toArray());
exchangeItems.putIfAbsent(key, doublesExchangeItem);
}
}
}
}
Expand All @@ -55,6 +96,32 @@ private List<Double> readHeader(List<String> lines) {

@Override
public void finish() {
// Do nothing
DecimalFormat decimalFormat = new DecimalFormat(".00000000E00", new DecimalFormatSymbols(Locale.UK));

int fileCount = 0;
for (Map.Entry<String, File> entry : fileNamesToFiles.entrySet()) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(entry.getValue()))) {
bufferedWriter.write(header + "\n");

for (Double height : heights) {
bufferedWriter.write(String.format("%10s", String.format(Locale.UK, "%5.4f", height)));

for (Double radius : radii) {
String key = String.format("depth%s_radius%s", height, radius);
DoublesExchangeItem doublesExchangeItem = (DoublesExchangeItem) exchangeItems.get(key);
String formattedNumber = "0" + decimalFormat.format(doublesExchangeItem.getValuesAsDoubles()[fileCount]);
if (!formattedNumber.contains("E-")) {
formattedNumber = formattedNumber.replace("E", "E+");
}
bufferedWriter.write(String.format("%24s", formattedNumber));
}

bufferedWriter.write("\n");
}
fileCount++;
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.openda.model_wanda_seawat;

import junit.framework.TestCase;
import org.openda.exchange.DoubleExchangeItem;
import org.openda.exchange.DoublesExchangeItem;
import org.openda.utils.OpenDaTestSupport;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WandaSeawatPointDataObjectTest extends TestCase {
OpenDaTestSupport testData = null;
Expand All @@ -15,16 +19,27 @@ protected void setUp() {
testRunDataDir = new File(testData.getTestRunDataDir(), "WandaSeawatPointDataObject");
}

public void test_givenWandaSeawatData_whenInitialized_thenExchangeItemsLoaded() {
File file = new File(testRunDataDir, "HTO_TEMP_20220531000100.ASC");
public void test_givenWandaSeawatData_whenInitialized_thenExchangeItemsLoaded() throws IOException {
File file = new File(testRunDataDir, "HTO_TEMP_");
WandaSeawatPointDataObject wandaSeawatPointDataObject = new WandaSeawatPointDataObject();
wandaSeawatPointDataObject.initialize(testRunDataDir, new String[]{file.getName()});
assertEquals("Exchange items all loaded", 2160, wandaSeawatPointDataObject.getExchangeItemIDs().length);
String key = wandaSeawatPointDataObject.getExchangeItemIDs()[19 * 72 + 1];
assertEquals("Get key from 20th row of 30 and 2nd column of 72", "depth-298.0_radius0.7624999881", key);
Object exchangeItemObject = wandaSeawatPointDataObject.getDataObjectExchangeItem(key);
assertTrue(exchangeItemObject instanceof DoubleExchangeItem);
DoubleExchangeItem doubleExchangeItem = (DoubleExchangeItem) exchangeItemObject;
assertEquals("Get value from 20th row of 30 and 2nd column of 72", 0.10554802E+02, doubleExchangeItem.getValue());
assertTrue(exchangeItemObject instanceof DoublesExchangeItem);
DoublesExchangeItem doublesExchangeItem = (DoublesExchangeItem) exchangeItemObject;
assertEquals("Get first value from 20th row of 30 and 2nd column of 72", 0.10554802E+02, doublesExchangeItem.getValuesAsDoubles()[0]);
assertEquals("Get second value from 20th row of 30 and 2nd column of 72", 0.12345600E+02, doublesExchangeItem.getValuesAsDoubles()[1]);

doublesExchangeItem.setValuesAsDoubles(new double[]{0.10554802E+02, 0.65432100E+02});
wandaSeawatPointDataObject.finish();

File output = new File(testRunDataDir, "HTO_TEMP_20220531000200.ASC");
try (Stream<String> stream = Files.lines(output.toPath())) {
String lines = stream.collect(Collectors.joining("\n"));
assertTrue("Updated value present in ini file", lines.contains(" -298.0000 0.16334927E+02 0.65432100E+02 0.10028160E+02"));
assertTrue("Existing values present in ini file", lines.contains(" -302.0000 0.10001448E+02 0.10000043E+02 0.10000001E+02"));
}
}
}

0 comments on commit 445bb30

Please sign in to comment.