-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarbonDioxide.java
319 lines (263 loc) · 8.42 KB
/
CarbonDioxide.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Scanner;
import java.util.NoSuchElementException;
import org.json.*;
import org.json.simple.JSONValue;
public class CarbonDioxide
{
private static String localFileString = "data/CO2Data";
// Credit: NOAA
// https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_annmean_gl.txt
private static String fileURL = "https://gml.noaa.gov/webdata/ccgg/trends/co2/co2_annmean_gl.txt";
private static Scanner scanCO2;
private static int year = Calendar.getInstance().get(Calendar.YEAR);
// Get the average rate of change of carbon dioxide between two years
public static double getAvgRateOfChange (String yearOne, String yearTwo) throws MalformedURLException, IOException
{
double avgCO2One = getAvgPPM(yearOne);
double avgCO2Two = getAvgPPM(yearTwo);
double yearUno = Double.parseDouble(yearOne);
double yearDos = Double.parseDouble(yearTwo);
double rateOfChange = (avgCO2Two - avgCO2One) / (yearDos - yearUno);
return rateOfChange;
}
// Get the average rate of change of carbon dioxide between two years
public static double getAvgRateOfChange (int yearOne, int yearTwo) throws MalformedURLException, IOException
{
double avgCO2One = getAvgPPM(yearOne);
double avgCO2Two = getAvgPPM(yearTwo);
double yearUno = (double) yearOne;
double yearDos = (double) yearTwo;
double rateOfChange = (avgCO2Two - avgCO2One) / (yearDos - yearUno);
return rateOfChange;
}
public static double getAvgRateOfChangeOfRateOfChange (String yearOne, String yearTwo)
{
return 0.0;
}
public static double getAvgRateOfChangeOfRateOfChange (int yearOne, int yearTwo)
{
return 0.0;
}
public static double getAvgRateOfChangeOfRateOfChange (double rateOfChange, String yearOne, String yearTwo)
{
return 0.0;
}
public static double getAvgRateOfChangeOfRateOfChange (double rateOfChange, int yearOne, int yearTwo)
{
return 0.0;
}
public static String getLatestDate ()
{
return "";
}
// Load the file
public static boolean isLoaded () throws MalformedURLException, IOException
{
File CO2Data = null;
Scanner scanCO2Data = null;
try
{
CO2Data = new File(localFileString);
scanCO2Data = new Scanner(CO2Data);
}
catch (FileNotFoundException e)
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(fileURL).openStream());
fout = new FileOutputStream(localFileString);
byte data[] = new byte[1024];
int count;
while ( (count = in.read(data, 0, 1024) ) != -1)
{
fout.write(data, 0, count);
}
}
finally
{
if (in != null) in.close();
if (fout != null) fout.close();
}
try
{
CO2Data = new File(localFileString);
scanCO2Data = new Scanner(CO2Data);
}
catch (FileNotFoundException f)
{
f.printStackTrace();
System.out.println("Bad Request. Check connection and data source.");
return false;
}
}
scanCO2Data.close();
return true;
}
// Get the average CO2 PPM for any given year in data
public static double getAvgPPM (String yearDate) throws MalformedURLException, IOException
{
if(!isLoaded())
{
return -1.0;
}
File CO2File = new File(localFileString);
scanCO2 = new Scanner(CO2File);
while (scanCO2.hasNextLine())
{
try {
if (scanCO2.next().equals(yearDate))
{
return Double.parseDouble(scanCO2.next());
}
} catch (NoSuchElementException e) {
break;
}
}
return -1.0;
}
// Get the average CO2 PPM for any given year in data
public static double getAvgPPM (int yearDate) throws MalformedURLException, IOException
{
if(!isLoaded())
{
return -1.0;
}
File CO2File = new File(localFileString);
scanCO2 = new Scanner(CO2File);
while (scanCO2.hasNextLine())
{
try {
if (scanCO2.next().equals(Integer.toString(yearDate)))
{
return Double.parseDouble(scanCO2.next());
}
} catch (NoSuchElementException e) {
break;
}
}
return -1.0;
}
// Estimate CO2 PPM at a future date given a rate of change
public static double getFuturePPM (String yearDate, double rateOfChange) throws MalformedURLException, IOException
{
double yearDateDouble = Double.valueOf(yearDate);
return rateOfChange * (yearDateDouble - (double) (year - 1)) + getAvgPPM(year - 1);
}
// Estimate CO2 PPM at a future date given a rate of change
public static double getFuturePPM (int yearDate, double rateOfChange) throws MalformedURLException, IOException
{
return rateOfChange * ((double) yearDate - (double) (year - 1)) + getAvgPPM(year - 1);
}
// Estimate CO2 PPM at a future date given two years to find a rate of change
public static double getFuturePPM (String yearDate, String yearOne, String yearTwo) throws MalformedURLException, IOException
{
double rateOfChange = getAvgRateOfChange(yearOne, yearTwo);
double yearDateDouble = Double.valueOf(yearDate);
return rateOfChange * (yearDateDouble - (double) (year - 1)) + getAvgPPM(year - 1);
}
// Estimate CO2 PPM at a future date given two years to find a rate of change
public static double getFuturePPM (int yearDate, int yearOne, int yearTwo) throws MalformedURLException, IOException
{
double rateOfChange = getAvgRateOfChange(yearOne, yearTwo);
return rateOfChange * ((double) yearDate - (double) (year - 1)) + getAvgPPM(year - 1);
}
// Find the latest CO2 PPM in data
public static double getLatestPPM () throws MalformedURLException, IOException
{
if(!isLoaded())
{
return -1.0;
}
File CO2File = new File(localFileString);
scanCO2 = new Scanner(CO2File);
int countLines = 0;
while (scanCO2.hasNextLine())
{
scanCO2.nextLine();
++countLines;
}
scanCO2.reset();
scanCO2 = new Scanner(CO2File);
for (int i = 0; i < countLines - 1; ++i)
{
scanCO2.nextLine();
}
scanCO2.next();
return Double.parseDouble(scanCO2.next());
}
// Find the latest full year of CO2 PPM in data
public static int getLatestYearInData () throws MalformedURLException, IOException
{
isLoaded();
File CO2File = new File(localFileString);
scanCO2 = new Scanner(CO2File);
int countLines = 0;
while (scanCO2.hasNextLine())
{
scanCO2.nextLine();
++countLines;
}
scanCO2.reset();
scanCO2 = new Scanner(CO2File);
for (int i = 0; i < countLines - 1; ++i)
{
scanCO2.nextLine();
}
return Integer.parseInt(scanCO2.next());
}
// Generate a JSON file of past CO2 values to be used by the javascript application
public static void generateJSONFilePastValues () throws IOException, JSONException
{
JSONObject pastData = new org.json.JSONObject();
for (int i = 1979; i < year ; i = i + 4)
{
String intI = Integer.toString(i);
pastData.put(intI, getAvgPPM(intI) );
}
File file = new File("Site/pastCO2Data.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(pastData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of past CO2 values to be used by the javascript application
public static void generateJSONFileCurrentValue () throws MalformedURLException, JSONException, IOException
{
int latestYear = getLatestYearInData();
String yearString = Integer.toString(latestYear);
JSONObject currentData = new org.json.JSONObject();
currentData.put(yearString, getLatestPPM());
File file = new File("Site/currentCO2Data.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(currentData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of past CO2 values to be used by the javascript application
public static void generateJSONFileFutureValues (double rateOfChange) throws MalformedURLException, JSONException, IOException
{
int latestYear = getLatestYearInData();
JSONObject futureData = new org.json.JSONObject();
for (int i = latestYear + 4; i < 2070 ; i = i + 4)
{
String intI = Integer.toString(i);
futureData.put(intI, getFuturePPM(intI, rateOfChange) );
}
File file = new File("Site/futureCO2Data.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(futureData, fileWriter);
fileWriter.close();
}
}