forked from jaredworthington/teamRocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.java
More file actions
183 lines (150 loc) · 4.41 KB
/
Converter.java
File metadata and controls
183 lines (150 loc) · 4.41 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.io.*;
import java.util.*;
import java.lang.*;
class Converter{
public static void main(String[] args) throws IOException{
BufferedReader br;
BufferedWriter bw;
try{
br = new BufferedReader(new FileReader("weatherData.csv"));
bw = new BufferedWriter(new FileWriter("outputWeatherData.csv"));
String line;
String header = "Year,Month,Day,T_max,T_min,Precip (mm),T_ave,Precip (cm),rh_ave";
System.out.println(header);
bw.write(header , 0, header.length());
bw.newLine();
line = br.readLine();
while (line!=null){
String year = "";
String month = "";
String day = "";
String tMax = "";
String tMin = "";
String precipMM = "";
String tAvg ="";
String precipCM = "";
String rhAvg = "rhAvg";
String temp = "";
String vp = "";
if(line.length()==0){
//do nothing
}
else if(line.charAt(0)!='1'&& line.charAt(0)!='2'){
//do nothing
}
else{
int i=0;
int j=0;
while(j<=line.length()){
if(j== line.length()){
vp = temp;
Double vpD = Double.parseDouble(vp);
//---------------------------------------------------------------
//-----Calculate relative humidity for the day-------------------
Double t_min = Double.parseDouble(tMin);
Double t_max = Double.parseDouble(tMax);
Double avgTemp = ((t_min + t_max) / 2);
Double up = (7.5*avgTemp)/(237.3+avgTemp);
Double u = Math.pow(10.0, up);
Double rh = 100*(vpD/u);
long f = rh.longValue();
if(rh== f){
rhAvg = String.format("%d",f);
}
else{
rhAvg = String.format("%s", rh); //parseFloat
}
//--------------------------------------------------------------
}
else if(line.charAt(j)==','){
if(i==0){
year =temp;
}
else if(i==1){
//calculate month and day
String doy = temp;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(year));
cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(doy));
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int m = cal.get(Calendar.MONTH);
month = Integer.toString(m+1);
day = Integer.toString(dayOfMonth);
}
else if(i==3){
//assign mm and calculate cm
precipMM = temp;
//parseFloat
Double mm = Double.parseDouble(precipMM);
Double cm = (mm/10);
//format the doubles
long u = cm.longValue();
if(cm == u){
precipCM = String.format("%d", u);
}
else{
precipCM =
String.format("%s", cm);
}
u = mm.longValue();
if(mm == u){
precipMM = String.format("%d",u);
}
else{
precipMM = String.format("%s", mm); //parseFloat
}
}
else if(i==6){
//assign maxTemp
tMax = temp;
}
else if(i==7){
//assign minTemp and calculate average temp
tMin = temp;
Double t_min = Double.parseDouble(tMin);
Double t_max = Double.parseDouble(tMax);
Double avgTemp = ((t_min + t_max) / 2);
long u = avgTemp.longValue();
if(avgTemp == u){
tAvg = String.format("%d",u);
}
else{
tAvg = String.format("%s", avgTemp); //parseFloat
}
u = t_min.longValue();
if(t_min == u){
tMin = String.format("%d",u);
}
else{
tMin = String.format("%s", t_min); //parseFloat
}
u = t_max.longValue();
if(t_max == u){
tMax = String.format("%d",u);
}
else{
tMax = String.format("%s", t_max); //parseFloat
}
}
temp = "";
i++;
}
else{
String c = Character.toString(line.charAt(j));
temp = temp + c;
}
j++;
}
String x = year+","+month+","+day+","+tMax+","+tMin+","+precipMM+","+tAvg+","+precipCM+","+rhAvg;
System.out.println(x);
bw.write(x, 0 , x.length());
bw.newLine();
}
line = br.readLine();
}
}
catch(FileNotFoundException e){
System.out.println("fileNotFound");
}
}
}