Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public static long dateString2Millis(String dateString, TimeZone tz) throws Pars
}

private static SimpleDateFormat getDateFormatForStringLength(String dateString) throws ParseException {
if (dateString.length() == 8) return new SimpleDateFormat("yyyyMMdd", Locale.UK);
if (dateString.length() == 12) return new SimpleDateFormat("yyyyMMddHHmm", Locale.UK);
if (dateString.length() == 14) return new SimpleDateFormat("yyyyMMddHHmmss", Locale.UK);
if (dateString.length() == 19) return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.ini4j.Config;
import org.ini4j.Ini;
Expand Down Expand Up @@ -52,13 +53,18 @@ public class DFlowFMMduInputFile {
private File inputFile = null;
private final Ini ini = new Ini();
private final static String COMMENT_CHAR = "#";
private static final Map<String, Double> timeMap;
private static final Map<String, Double> timeToMjdMap;
private static final Map<String, Long> millisMap;

static {
timeMap = new HashMap<String, Double>();
timeMap.put("S", 1.0 / (60.0*60.0*24.0) );
timeMap.put("M", 1.0 / (60.0*24.0) );
timeMap.put("H", 1.0 / 24.0 );
timeToMjdMap = new HashMap<>();
timeToMjdMap.put("S", 1.0 / (60.0*60.0*24.0) );
timeToMjdMap.put("M", 1.0 / (60.0*24.0) );
timeToMjdMap.put("H", 1.0 / 24.0 );
millisMap = new HashMap<>();
millisMap.put("S", 1000L);
millisMap.put("M", 60000L);
millisMap.put("H", 3600000L);
Config.getGlobal().setEscape(false);
}

Expand Down Expand Up @@ -116,15 +122,31 @@ public Double getReferenceDateInMjd() {
}
return dateInMjd;
}

public long getReferenceDateInMillis() {
String dateString = this.get("time", "RefDate");
try {
return TimeUtils.dateString2Millis(dateString, TimeZone.getTimeZone("UTC"));
} catch (Exception e) {
throw new RuntimeException("Error parsing reference date: " + dateString);
}
}

public Double getTimeToMjdFactor() {
String timeUnit = this.get("time","Tunit");
Double factor = timeMap.get(timeUnit);
Double factor = timeToMjdMap.get(timeUnit);
if (factor == null) {
throw new RuntimeException("Incorrect Tunit in MDU-file: " + timeUnit);
}
return factor;
}

public long getTimeStepMillis() {
String timeUnit = this.get("time","Tunit");
Long factor = millisMap.get(timeUnit);
if (factor == null) throw new RuntimeException("Incorrect Tunit in MDU-file: " + timeUnit);
return factor;
}

public void put(String sectionName, String key, String value) {
//logger.debug("Put [" + sectionName + "] " + key + " = " + value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import org.openda.interfaces.IDataObject;
import org.openda.interfaces.IExchangeItem;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.openda.utils.Results;
import org.openda.utils.generalJavaUtils.StringUtilities;

/**
Expand Down Expand Up @@ -140,27 +138,24 @@ public IExchangeItem[] getExchangeItems() {
private double getDoubleTime(String offset, String datetime) {
if (mduOptions.contains(SECTION_TIME, offset)) {
String time = mduOptions.get(SECTION_TIME, offset);
return Double.parseDouble(time) * mduOptions.getTimeToMjdFactor() + mduOptions.getReferenceDateInMjd();
double doubleTime = Double.parseDouble(time);
long timeStepMillis = mduOptions.getTimeStepMillis();
long referenceDateInMillis = mduOptions.getReferenceDateInMillis();
long timeMillis = (long) (doubleTime * timeStepMillis + referenceDateInMillis) * 1000 / 1000;
return TimeUtils.date2Mjd(new Date(timeMillis));
}

if (mduOptions.contains(SECTION_TIME, datetime)) {
String time = mduOptions.get(SECTION_TIME, datetime);
String timeUnit = mduOptions.get(SECTION_TIME, "Tunit");
if ("H".equalsIgnoreCase(timeUnit)) {
time = time.substring(0, time.length() - REPLACE_MINUTES_AND_SECONDS.length()) + REPLACE_MINUTES_AND_SECONDS;
}

if ("M".equalsIgnoreCase(timeUnit)) {
time = time.substring(0, time.length() - REPLACE_SECONDS.length()) + REPLACE_SECONDS;
}

try {
String time = mduOptions.get(SECTION_TIME, datetime);
String timeUnit = mduOptions.get(SECTION_TIME, "Tunit");
if ("H".equalsIgnoreCase(timeUnit)) return TimeUtils.date2Mjd(time.substring(0, time.length() - REPLACE_MINUTES_AND_SECONDS.length()) + REPLACE_MINUTES_AND_SECONDS);
if ("M".equalsIgnoreCase(timeUnit)) return TimeUtils.date2Mjd(time.substring(0, time.length() - REPLACE_SECONDS.length()) + REPLACE_SECONDS);
return TimeUtils.date2Mjd(time);
} catch (Exception e) {
throw new RuntimeException(String.format("Error parsing %s " + mduOptions.get(SECTION_TIME, datetime), datetime));
}
}

throw new RuntimeException(String.format("Neither %s nor %s are specified in %s", offset, datetime, SECTION_TIME));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ public void testDflowfmParseExtInputFile() {

Double factor = mduOptions.getTimeToMjdFactor();
assertEquals( 24.0 * 60.0, 1.0/factor);

long refDateMillis = mduOptions.getReferenceDateInMillis();
assertEquals(662688000000L, refDateMillis);

long timeStepMillis = mduOptions.getTimeStepMillis();
assertEquals(60000, timeStepMillis);
}
}
Loading