-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileClimate.java
More file actions
56 lines (52 loc) · 2.21 KB
/
FileClimate.java
File metadata and controls
56 lines (52 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
public class FileClimate extends Climate {
public int date = 0;
public double lastHum = 0;
public Queue<double[]> pairs;// climate pair, {temp, humidity}
/**
* Initialize a climate class from 2 files exported from Mathematica.
* The climate file should be put into climate folder.
* @param temFileName Filename of temperature file
* @param humFileName Filename of humidity file
* @param beginDate it will generate climate data from this day
*/
public FileClimate(String temFileName, String humFileName, int beginDate) throws IOException{
this.pairs = new LinkedList<double[]>();
double[] pair = {0, 0};
String temLine, humLine;
File tem = new File(".\\climate\\" + temFileName);
File hum = new File(".\\climate\\" + humFileName);
BufferedReader temReader = new BufferedReader(new FileReader(tem));
BufferedReader humReader = new BufferedReader(new FileReader(hum));
for (int i = beginDate; i > 0; i--){
temReader.readLine();
humReader.readLine();
}
while((temLine = temReader.readLine()) != null && (humLine = humReader.readLine()) != null){
try{
pair[0] = Double.parseDouble(temLine.split("Quantity\\[")[1].split(",")[0]); //Get the number
double today = Double.parseDouble(humLine.split("8.\\]\",")[1]); //Get the number
pair[1] = Math.abs(today - this.lastHum);
this.lastHum = today;
this.pairs.offer(pair.clone());//clone it to record a new array
}catch(Exception e){
//do nothing
}
}
temReader.close();
humReader.close();
}
public FileClimate(String temFileName, String humFileName) throws IOException{
this(temFileName, humFileName, 0);
}
@Override
public double[] getClimate() {
//if(this.pairs.size()==1) this.pairs.offer(this.pairs.peek()); // Out put the last one if there is no more data available
return this.pairs.poll();
}
}