TimeSeriesConverter.java [src/java/org/rti/timeseries/converter] Revision:   Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rti.timeseries.converter;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author hariharakumarrajanala
 */
public class TimeSeriesConverter {
    public static JSONObject convertTimeFormat(File file)throws IOException {
        
		System.out.println(file.getAbsolutePath());
		BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
		String s = bufferedReader.readLine();
		HashMap<String, HashMap> hashMap = new HashMap<String, HashMap>();
		while( s!=null) {
			if(s.matches("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9].[0-9]*")) {
				//System.out.println(s);
				if(hashMap.containsKey(s.substring(0, 10))) {
					HashMap innerHashMap = (HashMap) hashMap.get(s.substring(0,10));
					String key = s.substring(11,22);
					if(innerHashMap.containsKey(key)) {
						float value = Float.parseFloat(s.substring(23, s.length()));
						innerHashMap.put(key, Float.parseFloat((String) innerHashMap.get(key)) + value);
					} else {
						innerHashMap.put(key, s.substring(23, s.length()));
					}
				} else {
					HashMap<String, Float> innerHashMap = new HashMap<String, Float>();
					hashMap.put(s.substring(0,10), innerHashMap);
				}
			}
			s = bufferedReader.readLine();
		}	
		System.out.println(hashMap);
		JSONObject jsonObject = new JSONObject(hashMap);
		System.out.println(jsonObject);
                return jsonObject;
    }
    public static void main(String args[])throws IOException {
//		File file = new File("./data/TimeSeries/MCAN3.NHDES.MAP.1HOUR");
		File file = new File("/od/projects/RTI/RiverTrak/OpTableLight/TimeSeries/MCAN3.NHDES.MAP.1HOUR");
                try {
                        JSONObject jsonObject = convertTimeFormat(file);
			FileWriter fileWriter = new FileWriter(new File("/tmp/output.json"));
			fileWriter.write(jsonObject.toString());
			fileWriter.close();
		} catch(Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}