EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
HourlyCSVTimeSeries.java
1 /*******************************************************************************
2  * Copyright 2012 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package emlab.gen.trend;
17 
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
20 
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.data.neo4j.annotation.NodeEntity;
24 import org.springframework.transaction.annotation.Transactional;
25 
30 @NodeEntity
31 public class HourlyCSVTimeSeries implements HourlyTimeSeries {
32 
33  Logger logger = LoggerFactory.getLogger(HourlyCSVTimeSeries.class);
34 
35  private String filename;
36 
37  private double[] hourlyArray;
38 
39  @Transactional
40  private void readData() {
41 
42  this.persist();
43  logger.warn("Trying to read CSV file: " + filename);
44 
45  String data = new String();
46 
47  // Save the data in a long String
48  try {
49 
50  InputStreamReader inputStreamReader = new InputStreamReader(this.getClass().getResourceAsStream(filename));
51  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
52 
53  String line;
54 
55  while ((line = bufferedReader.readLine()) != null) {
56  data = data.concat(line + ",");
57  }
58  bufferedReader.close();
59 
60  String[] vals = data.split(",");
61  setHourlyArray(parseString(vals), 0);
62 
63  } catch (Exception e) {
64  logger.error("Couldn't read CSV file: " + filename);
65  e.printStackTrace();
66  }
67 
68  }
69 
77  public double[] getHourlyArray(long time) {
78  if (hourlyArray != null)
79  try {
80  return hourlyArray;
81  } catch (Exception e) {
82  logger.error("CSV File has wrong length (!= 8760 hours");
83  e.printStackTrace();
84  }
85  else {
86  readData();
87  return hourlyArray;
88  }
89  return null;
90 
91  }
92 
93  public void setHourlyArray(double[] hourlyArray, long time) {
94  this.hourlyArray = hourlyArray;
95  }
96 
97  private double[] parseString(String[] vals) throws Exception {
98 
99  if (vals.length == 8760) {
100  double[] doubleArrayData = new double[vals.length];
101  for (int i = 0; i <= vals.length - 1; i++) {
102  doubleArrayData[i] = Double.parseDouble(vals[i]);
103  }
104  return doubleArrayData;
105  } else {
106  throw new Exception();
107  }
108  }
109 
110  public String getFilename() {
111  return filename;
112  }
113 
114  public void setFilename(String filename) {
115  this.filename = filename;
116  }
117 
118 }