/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.jaamsim.input;

import java.util.ArrayList;

import com.jaamsim.datatypes.DoubleVector;
import com.jaamsim.units.DimensionlessUnit;
import com.jaamsim.units.Unit;
import com.jaamsim.units.UserSpecifiedUnit;
import com.sandwell.JavaSimulation.TimeSeriesData;

public class TimeSeriesDataInput extends Input<TimeSeriesData> {
	private Class<? extends Unit> unitType;

	private double maxValue = Double.POSITIVE_INFINITY;
	private double minValue = Double.NEGATIVE_INFINITY;

	public TimeSeriesDataInput(String key, String cat, TimeSeriesData def) {
		super(key, cat, def);
		unitType = DimensionlessUnit.class;
	}

	@Override
	public void parse(KeywordIndex kw) throws InputErrorException {

		if (unitType == UserSpecifiedUnit.class)
			throw new InputErrorException(INP_ERR_UNITUNSPECIFIED);

		long startingYearOffset = -1;
		long lastTime = -1;

		DoubleVector times = new DoubleVector(kw.numArgs()/4);
		DoubleVector values = new DoubleVector(kw.numArgs()/4);

		// Determine records in the time series
		// Records have form: (e.g.) yyyy-MM-dd HH:mm value units
		// where units are optional
		ArrayList<String> each = new ArrayList<String>();
		for (int i=0; i < kw.numArgs(); i++) {

			//skip over opening brace if present
			if (kw.getArg(i).equals("{") )
				continue;

			each.clear();

			// Load one record into 'each' containing an individual timeseries record
			for (int j = i; j < kw.numArgs(); j++, i++){
				if (kw.getArg(j).equals("}"))
					break;

				each.add(kw.getArg(j));
			}

			// Check the number of entries in the record
			Input.assertCountRange(each, 2, 3);

			long recordus = Input.parseRFC8601DateTime(each.get(0));
			// Make sure the times are in increasing order
			if (recordus <= lastTime)
				throw new InputErrorException( "The times must be given in increasing order on " + each.get(0));

			lastTime = recordus;

			// set the offset to the number of whole years from the first record
			if (startingYearOffset == -1) {
				startingYearOffset = recordus / Input.usPerYr;
				startingYearOffset *= Input.usPerYr;
			}

			long usOffset = recordus - startingYearOffset;

			each.remove(0);  // We've finished parsing the date at this point
			DoubleVector v = Input.parseDoubles(each, minValue, maxValue, unitType);

			times.add(usOffset / 3.6e9d); // convert to hours 3600 secs * 1e6 us
			values.add(v.get(0));
		}

		// Set the value to a new time series data object
		value = new TimeSeriesData( times, values );
	}

	public void setUnitType(Class<? extends Unit> u) {
		unitType = u;
	}
}

@@ -735,24 +735,59 @@
 onlessUnit;%0A
+import com.jaamsim.units.TimeUnit;%0A
 import com.j
@@ -2155,50 +2155,331 @@
 %09// 
-Check the number of entries in the record%0A
+Time input in RFC8601 date/time format%0A%09%09%09long recordus;%0A%09%09%09if (Input.isRFC8601DateTime(each.get(0))) %7B%0A%09%09%09%09Input.assertCountRange(each, 2, 3);%0A%09%09%09%09recordus = Input.parseRFC8601DateTime(each.get(0));%0A%09%09%09%09each.remove(0);%0A%09%09%09%7D%0A%09%09%09// Time input in number/unit format%0A%09%09%09else %7B%0A%09%09%09%09// Parse the unit portion of the time input%0A%09
 %09%09%09I
@@ -2510,24 +2510,298 @@
 ch, 
-2, 3);%0A%0A%09%09%09long 
+3, 4);%0A%09%09%09%09TimeUnit unit = Input.tryParseEntity(each.get(1), TimeUnit.class);%0A%09%09%09%09if (unit == null)%0A%09%09%09%09%09throw new InputErrorException(INP_ERR_NOUNITFOUND, each.get(1), %22TimeUnit%22);%0A%0A%09%09%09%09// Parse the numeric portion of the time input%0A%09%09%09%09double factor = unit.getConversionFactorToSI();%0A%09%09%09%09
 reco
@@ -2799,32 +2799,40 @@
 %0A%09%09%09%09recordus = 
+(long) (
 Input.parseRFC86
@@ -2830,37 +2830,118 @@
 arse
-RFC8601DateTime(each.get(0));
+Double(each.get(0), 0.0, Double.POSITIVE_INFINITY, factor)*1e6);%0A%09%09%09%09each.remove(0);%0A%09%09%09%09each.remove(0);%0A%09%09%09%7D%0A
 %0A%09%09%09
@@ -3406,73 +3406,38 @@
 %0A%09%09%09
-each.remove(0);  // We've finished parsing the date at this point
+// Value portion of the record
 %0A%09%09%09
@@ -3510,16 +3510,63 @@
 Type);%0A%0A
+%09%09%09// Store the time and value for this record%0A
 %09%09%09times

allow TimeSeries to accept times in number/unit format as well as RFC8601 format