-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarthQuakeClient.java
138 lines (124 loc) · 5.59 KB
/
EarthQuakeClient.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
import java.util.*;
import edu.duke.*;
public class EarthQuakeClient {
public EarthQuakeClient() {
// TODO Auto-generated constructor stub
}
public ArrayList<QuakeEntry> filterByMagnitude(ArrayList<QuakeEntry> quakeData, double magMin) {
ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
for (QuakeEntry qe : quakeData) {
if (qe.getMagnitude() > magMin) {
answer.add(qe);
}
}
return answer;
}
public ArrayList<QuakeEntry> filterByDistanceFrom(ArrayList<QuakeEntry> quakeData, double distMax, Location from) {
ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
for (QuakeEntry qe : quakeData) {
if (qe.getLocation().distanceTo(from) < distMax) {
answer.add(qe);
}
}
return answer;
}
public ArrayList<QuakeEntry> filterByDepth(ArrayList<QuakeEntry> quakeData, double minDepth, double maxDepth) {
ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
for (QuakeEntry qe : quakeData) {
double depth = qe.getDepth();
if (depth > minDepth && depth < maxDepth) {
answer.add(qe);
}
}
return answer;
}
public ArrayList<QuakeEntry> filterByPhrase(ArrayList<QuakeEntry> quakeData, String where, String phrase) {
ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
for (QuakeEntry qe : quakeData) {
String title = qe.getInfo();
if ((where.equals("start") && title.startsWith(phrase)) || (where.equals("end") && title.endsWith(phrase)) || (where.equals("any") && title.contains(phrase))) {
answer.add(qe);
}
}
return answer;
}
public void dumpCSV(ArrayList<QuakeEntry> list){
System.out.println("Latitude,Longitude,Magnitude,Info");
for(QuakeEntry qe : list){
System.out.printf("%4.2f,%4.2f,%4.2f,%s\n", qe.getLocation().getLatitude(),qe.getLocation().getLongitude(),qe.getMagnitude(), qe.getInfo());
}
}
public void bigQuakes() {
EarthQuakeParser parser = new EarthQuakeParser();
String source = "E:/Coursera/nov20quakedata.atom";
ArrayList<QuakeEntry> list = parser.read(source);
System.out.println("read data for " + list.size() + " quakes");
double magMin = 5.0;
ArrayList<QuakeEntry> bigQuakes = filterByMagnitude(list, magMin);
System.out.println("Quakes larger than " + magMin + ":");
for (QuakeEntry qe : bigQuakes) {
System.out.println(qe);
}
System.out.println("Found " + bigQuakes.size() + " quakes that match that criteria");
}
public void closeToMe() {
EarthQuakeParser parser = new EarthQuakeParser();
String source = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atom";
ArrayList<QuakeEntry> list = parser.read(source);
System.out.println("read data for " + list.size() + " quakes");
// This location is Durham, NC
Location city = new Location(35.988, -78.907);
double distMax = 1000.0;
ArrayList<QuakeEntry> closeQuakes = filterByDistanceFrom(list, distMax, city);
System.out.println("Quakes within " + distMax + " kilometers of Durham, NC:");
for (QuakeEntry qe : closeQuakes) {
System.out.println("Distance: " + qe.getLocation().distanceTo(city) + " km, " + qe.getInfo());
}
}
public void quakesOfDepth() {
EarthQuakeParser parser = new EarthQuakeParser();
String source = "E:/Coursera/nov20quakedata.atom";
ArrayList<QuakeEntry> list = parser.read(source);
System.out.println("read data for " + list.size() + " quakes");
double minDepth = -4000.0;
double maxDepth = -2000.0;
ArrayList<QuakeEntry> depthQuakes = filterByDepth(list, minDepth, maxDepth);
System.out.println("Find quakes with depth between " + minDepth + " and " + maxDepth);
for (QuakeEntry qe : depthQuakes) {
System.out.println(qe);
}
System.out.println("Found " + depthQuakes.size() + " quakes that match that criteria");
}
public void quakesByPhrase(String phrase, String where) {
EarthQuakeParser parser = new EarthQuakeParser();
String source = "E:/Coursera/nov20quakedata.atom";
ArrayList<QuakeEntry> list = parser.read(source);
System.out.println("read data for " + list.size() + " quakes");
ArrayList<QuakeEntry> phraseQuakes = filterByPhrase(list, where, phrase);
System.out.println("Find quakes with phrase '" + phrase + "' at " + where);
for (QuakeEntry qe : phraseQuakes) {
System.out.println(qe);
}
System.out.println("Found " + phraseQuakes.size() + " quakes that match '" + phrase + "' at " + where);
}
public void quakesByPhraseQuestion3() {
quakesByPhrase("Quarry Blast", "start");
}
public void quakesByPhraseQuestion4() {
quakesByPhrase("Alaska", "end");
}
public void quakesByPhraseQuestion5() {
quakesByPhrase("Can", "any");
}
public void createCSV(){
EarthQuakeParser parser = new EarthQuakeParser();
String source = "E:/Coursera/nov20quakedata.atom";
//String source = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.atom";
ArrayList<QuakeEntry> list = parser.read(source);
dumpCSV(list);
System.out.println("# quakes read: " + list.size());
for (QuakeEntry qe : list) {
System.out.println(qe);
}
}
}