Repository navigation
Expand file tree
/
Copy pathArcticIce.java
More file actions
571 lines (489 loc) · 18.7 KB
/
Copy pathArcticIce.java
File metadata and controls
571 lines (489 loc) · 18.7 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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 org.json.*;
import org.json.simple.JSONValue;
public class ArcticIce {
private static String localFileString1 = "data/ArcticData1";
// Credit: NASA
// https://climate.nasa.gov/system/internal_resources/details/original/1270_minimum_extents_and_area_north_SBA_reg_20171001_2_.txt
private static String fileURL1 = "https://climate.nasa.gov/system/internal_resources/details/original/1270_minimum_extents_and_area_north_SBA_reg_20171001_2_.txt";
private static String localFileString2 = "data/ArcticData2";
// Credit: Colorado University
// ftp://sidads.colorado.edu/pub/DATASETS/NOAA/G10006/sea_ice_CDR_data_set_overview_v1.1.txt
private static String fileURL2 = "ftp://sidads.colorado.edu/pub/DATASETS/NOAA/G10006/sea_ice_CDR_data_set_overview_v1.1.txt";
private static int year = Calendar.getInstance().get(Calendar.YEAR);
// Find the rate of change of the arctic ice extent due to a rate of change in global temperature
public static double getRateOfChangeOfExtentMinimumFromGlobalTemperature () throws MalformedURLException, IOException
{
double globalTempIn1979 = GlobalTemperature.getLandOceanTemperatureIndex(1979);
double globalTempIn1980 = GlobalTemperature.getLandOceanTemperatureIndex(1980);
double globalTempIn1981 = GlobalTemperature.getLandOceanTemperatureIndex(1981);
double extentMinimumIn1979 = getExtentMinimum(1979);
double extentMinimumIn1980 = getExtentMinimum(1980);
double extentMinimumIn1981 = getExtentMinimum(1981);
double globalTempCurrentMinus2 = GlobalTemperature.getLandOceanTemperatureIndex(year - 2);
double globalTempCurrentMinus3 = GlobalTemperature.getLandOceanTemperatureIndex(year - 3);
double globalTempCurrentMinus4 = GlobalTemperature.getLandOceanTemperatureIndex(year - 4);
double extentMinimumCurrentMinus2 = getExtentMinimum(year - 2);
double extentMinimumCurrentMinus3 = getExtentMinimum(year - 3);
double extentMinimumCurrentMinus4 = getExtentMinimum(year - 4);
// Square kilometer change per degree celsius increase
// The arctic ice extent decreases by X for every celsius increase on average
// Finds the average arctic ice extent change from 1979 to current and divides it by the average global temperature increase from 1979 to current
return ( ( ((extentMinimumCurrentMinus2 + extentMinimumCurrentMinus3 + extentMinimumCurrentMinus4) / 3.0)
- ((extentMinimumIn1979 + extentMinimumIn1980 + extentMinimumIn1981) / 3.0) )
/ ( ((globalTempCurrentMinus2 + globalTempCurrentMinus3 + globalTempCurrentMinus4) / 3.0)
- ((globalTempIn1979 + globalTempIn1980 + globalTempIn1981) / 3.0)) );
}
public static double getRateOfChangeOfAreaMinimumFromGlobalTemperature () throws MalformedURLException, IOException
{
double globalTempIn1979 = GlobalTemperature.getLandOceanTemperatureIndex(1979);
double globalTempIn1980 = GlobalTemperature.getLandOceanTemperatureIndex(1980);
double globalTempIn1981 = GlobalTemperature.getLandOceanTemperatureIndex(1981);
double areaMinimumIn1979 = getAreaMinimum(1979);
double areaMinimumIn1980 = getAreaMinimum(1980);
double areaMinimumIn1981 = getAreaMinimum(1981);
double globalTempCurrentMinus2 = GlobalTemperature.getLandOceanTemperatureIndex(year - 2);
double globalTempCurrentMinus3 = GlobalTemperature.getLandOceanTemperatureIndex(year - 3);
double globalTempCurrentMinus4 = GlobalTemperature.getLandOceanTemperatureIndex(year - 4);
double areaMinimumCurrentMinus2 = getExtentMinimum(year - 2);
double areaMinimumCurrentMinus3 = getExtentMinimum(year - 3);
double areaMinimumCurrentMinus4 = getExtentMinimum(year - 4);
// Square kilometer change per degree celsius increase
// The arctic ice area decreases by X for every celsius increase on average
// Finds the average arctic ice area change from 1979 to current and divides it by the average global temperature increase from 1979 to current
return ( ( ((areaMinimumCurrentMinus2 + areaMinimumCurrentMinus3 + areaMinimumCurrentMinus4) / 3.0)
- ((areaMinimumIn1979 + areaMinimumIn1980 + areaMinimumIn1981) / 3.0) )
/ ( ((globalTempCurrentMinus2 + globalTempCurrentMinus3 + globalTempCurrentMinus4) / 3.0)
- ((globalTempIn1979 + globalTempIn1980 + globalTempIn1981) / 3.0)) );
}
// Load the file
public static boolean isLoaded () throws MalformedURLException, IOException
{
File arctic = null;
Scanner scanArctic = null;
try
{
arctic = new File(localFileString1);
scanArctic = new Scanner(arctic);
}
catch (FileNotFoundException e)
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(fileURL1).openStream());
fout = new FileOutputStream(localFileString1);
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
{
arctic = new File(localFileString1);
scanArctic = new Scanner(arctic);
}
catch (FileNotFoundException f)
{
f.printStackTrace();
System.out.println("Bad Request. Check connection and data source.");
return false;
}
}
try
{
arctic = new File(localFileString2);
scanArctic = new Scanner(arctic);
}
catch (FileNotFoundException e)
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(fileURL2).openStream());
fout = new FileOutputStream(localFileString2);
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
{
arctic = new File(localFileString2);
scanArctic = new Scanner(arctic);
}
catch (FileNotFoundException f)
{
f.printStackTrace();
System.out.println("Bad Request. Check connection and data source.");
return false;
}
}
scanArctic.close();
return true;
}
// Find the minimum extent of arctic ice given a year in data
public static double getExtentMinimum (String yearDate) throws MalformedURLException, IOException
{
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded() == true) { scnr = new Scanner(thisFile); }
else { return 0.0; } // Could not parse file
}
double suspectedValue = 0.0;
double actualValue = 0.0;
int counter = 0;
while (scnr.hasNext()) {
if (scnr.next().contains(yearDate)) {
scnr.next(); scnr.next();
suspectedValue = Double.parseDouble(scnr.next());
if (scnr.next().contains(yearDate)) {
actualValue = suspectedValue;
} else {
scnr.reset();
for (int i = 0; i < counter - 1; ++i)
{
scnr.next();
}
actualValue = Double.parseDouble(scnr.next());
}
}
++counter;
}
scnr.close();
return actualValue;
}
// Find the minimum extent of arctic ice given a year in data
public static double getExtentMinimum (int yearDate) throws MalformedURLException, IOException
{
String yearDateString = Integer.toString(yearDate);
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded() == true) { scnr = new Scanner(thisFile); }
else { return -1.0; } // Could not parse file
}
double suspectedValue = 0.0;
double actualValue = 0.0;
int counter = 0;
while (scnr.hasNext()) {
if (scnr.next().contains(yearDateString)) {
scnr.next(); scnr.next();
suspectedValue = Double.parseDouble(scnr.next());
if (scnr.next().contains(yearDateString)) {
actualValue = suspectedValue;
} else {
scnr.reset();
for (int i = 0; i < counter - 1; ++i)
{
scnr.next();
}
actualValue = Double.parseDouble(scnr.next());
}
}
++counter;
}
scnr.close();
return actualValue;
}
// Find the minimum area of arctic ice given a year in data
public static double getAreaMinimum (String yearDate) throws MalformedURLException, FileNotFoundException, IOException
{
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded() == true) { scnr = new Scanner(thisFile); }
else { return 0.0; } // Could not parse file
}
while (scnr.hasNext())
{
if (scnr.next().contains(yearDate))
{
scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();
double value = Double.parseDouble(scnr.next());
scnr.close();
return value;
} else {
scnr.next();
}
}
scnr.close();
return -1.0;
}
// Find the minimum area of arctic ice given a year in data
public static double getAreaMinimum (int yearDate) throws MalformedURLException, IOException
{
String yearDateString = Integer.toString(yearDate);
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded() == true) { scnr = new Scanner(thisFile); }
else { return 0.0; } // Could not parse file
}
while (scnr.hasNext())
{
if (scnr.next().contains(yearDateString))
{
scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();
double value = Double.parseDouble(scnr.next());
scnr.close();
return value;
} else {
scnr.next();
}
}
scnr.close();
return -1.0;
}
// Find the arctic ice extent minimum at a year in the future due to a projected change in temperature
public static double getFutureExtentMinimum (String yearDate) throws NumberFormatException, MalformedURLException, IOException
{
double projectedGlobalTempYearDate = GlobalTemperature.getFutureLandOceanTemperatureIndex(Integer.parseInt(yearDate));
double currentGlobalTemp = GlobalTemperature.getLatestLandOceanTemperatureIndex();
double changeInGlobalTemp = projectedGlobalTempYearDate - currentGlobalTemp;
return getLatestExtentMinimum() + (changeInGlobalTemp * getRateOfChangeOfExtentMinimumFromGlobalTemperature());
}
// Find the arctic ice extent minimum at a year in the future due to a projected change in temperature
public static double getFutureExtentMinimum (int yearDate) throws MalformedURLException, IOException
{
double projectedGlobalTempYearDate = GlobalTemperature.getFutureLandOceanTemperatureIndex(yearDate);
double currentGlobalTemp = GlobalTemperature.getLatestLandOceanTemperatureIndex();
double changeInGlobalTemp = projectedGlobalTempYearDate - currentGlobalTemp;
return getLatestExtentMinimum() + (changeInGlobalTemp * getRateOfChangeOfExtentMinimumFromGlobalTemperature());
}
// Find the arctic ice area minimum at a year in the future due to a projected change in temperature
public static double getFutureAreaMinimum (String yearDate) throws NumberFormatException, MalformedURLException, IOException
{
double projectedGlobalTempYearDate = GlobalTemperature.getFutureLandOceanTemperatureIndex(Integer.parseInt(yearDate));
double currentGlobalTemp = GlobalTemperature.getLatestLandOceanTemperatureIndex();
double changeInGlobalTemp = projectedGlobalTempYearDate - currentGlobalTemp;
return getLatestAreaMinimum() + (changeInGlobalTemp * getRateOfChangeOfAreaMinimumFromGlobalTemperature());
}
// Find the arctic ice area minimum at a year in the future due to a projected change in temperature
public static double getFutureAreaMinimum (int yearDate) throws MalformedURLException, IOException
{
double projectedGlobalTempYearDate = GlobalTemperature.getFutureLandOceanTemperatureIndex(yearDate);
double currentGlobalTemp = GlobalTemperature.getLatestLandOceanTemperatureIndex();
double changeInGlobalTemp = projectedGlobalTempYearDate - currentGlobalTemp;
return getLatestAreaMinimum() + (changeInGlobalTemp * getRateOfChangeOfAreaMinimumFromGlobalTemperature());
}
// Find the latest arctic ice extent minimum in data
public static double getLatestExtentMinimum () throws MalformedURLException, FileNotFoundException, IOException
{
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded() == true) { scnr = new Scanner(thisFile); }
else { return -1.0; } // Could not parse file
}
int countLines = 0;
while (scnr.hasNextLine())
{
scnr.nextLine();
++countLines;
}
scnr.reset();
scnr = new Scanner(thisFile);
for (int i = 0; i < countLines - 1; ++i)
{
scnr.nextLine();
}
scnr.next();scnr.next();scnr.next();
double value = Double.parseDouble(scnr.next());
scnr.close();
return value;
}
// Find the latest arctic ice area minimum in data
public static double getLatestAreaMinimum () throws MalformedURLException, IOException
{
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded() == true) { scnr = new Scanner(thisFile); }
else { return -1.0; } // Could not parse file
}
int countLines = 0;
while (scnr.hasNextLine())
{
scnr.nextLine();
++countLines;
}
scnr.reset();
scnr = new Scanner(thisFile);
for (int i = 0; i < countLines - 1; ++i)
{
scnr.nextLine();
}
scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();scnr.next();
double value = Double.parseDouble(scnr.next());
scnr.close();
return value;
}
// Find the latest year that has been released in data
public static int getLatestYearInData () throws MalformedURLException, FileNotFoundException, IOException
{
Scanner scnr;
scnr = null;
File thisFile = new File(localFileString1);
try {
scnr = new Scanner(thisFile);
} catch (FileNotFoundException file0) {
file0.printStackTrace();
if(isLoaded()) { scnr = new Scanner(thisFile); }
else { return -1; } // Could not parse file
}
int countLines = 0;
while (scnr.hasNextLine())
{
scnr.nextLine();
++countLines;
}
scnr.reset();
scnr = new Scanner(thisFile);
for (int i = 0; i < countLines - 1; ++i)
{
scnr.nextLine();
}
int value = Integer.parseInt(scnr.next());
scnr.close();
return value;
}
// Generate a JSON file of past arctic ice extent values to be used by the javascript application
public static void generateJSONFileExtentPastValues () 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, getExtentMinimum(intI) );
}
File file = new File("Site/pastArcticIceExtentData.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(pastData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of the current arctic ice extent values to be used by the javascript application
public static void generateJSONFileExtentCurrentValue () throws MalformedURLException, FileNotFoundException, JSONException, IOException
{
int latestYear = getLatestYearInData();
String yearString = Integer.toString(latestYear);
JSONObject currentData = new org.json.JSONObject();
currentData.put(yearString, getLatestExtentMinimum());
File file = new File("Site/currentArcticIceExtentData.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(currentData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of future arctic ice extent values to be used by the javascript application
public static void generateJSONFileExtentFutureValues () throws NumberFormatException, MalformedURLException, JSONException, IOException
{
int latestYear = getLatestYearInData();
JSONObject futureData = new org.json.JSONObject();
for (int i = latestYear + 4; i < 2045 ; i = i + 4)
{
String intI = Integer.toString(i);
futureData.put(intI, getFutureExtentMinimum(intI) );
}
File file = new File("Site/futureArcticIceExtentData.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(futureData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of past arctic ice area values to be used by the javascript application
public static void generateJSONFileAreaPastValues () throws MalformedURLException, FileNotFoundException, JSONException, IOException
{
JSONObject pastData = new org.json.JSONObject();
for (int i = 1979; i < year ; i = i + 4)
{
String intI = Integer.toString(i);
pastData.put(intI, getAreaMinimum(intI) );
}
File file = new File("Site/pastArcticIceAreaData.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(pastData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of the current arctic ice area values to be used by the javascript application
public static void generateJSONFileAreaCurrentValue () throws MalformedURLException, JSONException, IOException
{
int latestYear = getLatestYearInData();
String yearString = Integer.toString(latestYear);
JSONObject currentData = new org.json.JSONObject();
currentData.put(yearString, getLatestAreaMinimum());
File file = new File("Site/currentArcticIceAreaData.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(currentData, fileWriter);
fileWriter.close();
}
// Generate a JSON file of future arctic ice area values to be used by the javascript application
public static void generateJSONFileAreaFutureValues () throws NumberFormatException, MalformedURLException, JSONException, IOException
{
int latestYear = getLatestYearInData();
JSONObject futureData = new org.json.JSONObject();
for (int i = latestYear + 4; i < 2045 ; i = i + 4)
{
String intI = Integer.toString(i);
futureData.put(intI, getFutureAreaMinimum(intI) );
}
File file = new File("Site/futureArcticIceAreaData.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
JSONValue.writeJSONString(futureData, fileWriter);
fileWriter.close();
}
}