-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetOffice.java
More file actions
245 lines (224 loc) · 9 KB
/
Copy pathMetOffice.java
File metadata and controls
245 lines (224 loc) · 9 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
import java.util.ArrayList;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* class MetOffice, to define a national meteorological office, e.g. the UK Met Office.
* Users can update and interrogate the data files for different weather stations
* through the main method in class MetOffice.
*
* @author (Chris)
* @version (a version number or a date)
*/
public class MetOffice
{
private ArrayList<WeatherStation> sWeatherStation;
private JFrame metOfficeFrame;
private JButton loadFile;
private JButton saveFile;
private DefaultListModel weatehrStationlistModel;
private JList weatehrStationList;
private JScrollPane scrollPane;
private JTextArea summaryText;
/**
* Constructor for objects of class MetOffice
*
*/
public MetOffice()
{
this.sWeatherStation = new ArrayList<WeatherStation> ();
metOfficeFrame = new JFrame ("UK Met Office");
metOfficeFrame.getContentPane ().setLayout (null);
metOfficeFrame.setSize (450, 400);
metOfficeFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
metOfficeFrame.setVisible (true);
summaryText = new JTextArea ();
summaryText.setBounds (10, 250, 400, 80);
summaryText.setEditable(false);
metOfficeFrame.getContentPane ().add (summaryText);
loadFile = new JButton ("Load File");
loadFile.setBounds (280, 40, 120, 30);
loadFile.setToolTipText ("Read weather satation file.");
loadFile.setEnabled (true);
loadFile.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
loadWeatherStationFileChooser ();
}
});
metOfficeFrame.getContentPane ().add (loadFile);
saveFile = new JButton ("Save File");
saveFile.setBounds (280, 80, 120, 30);
saveFile.setToolTipText ("Save selected weather satation file.");
saveFile.setEnabled (true);
saveFile.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
saveWeatherStationFileChooser ();
}
});
metOfficeFrame.getContentPane ().add (saveFile);
weatehrStationlistModel = new DefaultListModel ();
weatehrStationList = new JList (weatehrStationlistModel);
scrollPane = new JScrollPane (weatehrStationList);
scrollPane.setBounds(10, 40, 240, 200);
metOfficeFrame.getContentPane ().add (scrollPane);
try {
this.loadWeatherStationFolder ("file");
} catch (Exception e) {};
}
/**
* Add a weatherstation to the Met Office.
*
* @param station The weather station to be added.
*/
public void addWeatherStation (WeatherStation station) throws InvalidDataException
{
if (sWeatherStation.contains (station))
{
throw new InvalidDataException("This weatger station already been added");
}
sWeatherStation.add (station);
}
/**
* A get method - to get the weather station.
*
* @return If the weather staion o is equal to the parameter, name then the station, o is returned.
* @return If the weather staion o is not equal to the parameter, name then the station, null is returned.
*/
public WeatherStation getWeatherStation (String name)
{
for (WeatherStation o : sWeatherStation)
{
if (o.getName ().equals (name))
{
return o;
}
}
return null;
}
/**
* A get method - to find the largest annual rainfall
*
* @return The largest annual rainfall of the Met Offic.
*/
public WeatherStation getLargestAnnualRainfall (int year)
{
double dMax = -1;
WeatherStation sMaxSation = null;
for (WeatherStation obj : sWeatherStation)
{
try {
double dAnuRainFall = obj.getTRain (year);
sMaxSation = (dAnuRainFall > dMax) ? obj : sMaxSation;
dMax = (dAnuRainFall > dMax) ? dAnuRainFall : dMax;
} catch (InvalidDataException e) {}
}
return sMaxSation;
}
/**
* A get method - to find the average mean monthly temperature with a given position
*
* @return The average mean monthly temperature of the Met Office
*/
public double getAvgMeanMonthlyTemp (LatLong position)
{
double dSum = 0;
double iCount = 0;
for (WeatherStation o : sWeatherStation)
{
if (o.getPosition().getLat () >= position.getLat ())
{
double dMeanTemp = o.getAvgMeanMonthlyTemp ();
dSum += (dMeanTemp == WeatherObservation.missingData) ? 0 : dMeanTemp;
iCount += (dMeanTemp == WeatherObservation.missingData) ? 0 : 1;
}
}
return (iCount > 0) ? (dSum /iCount) : WeatherObservation.missingData;
}
public void loadWeatherStationFile (String strFilePath) throws IOException, InvalidDataException
{
String line;
String strInfo;
WeatherStation newWeatherStation;
FileReader file = new FileReader (strFilePath);
BufferedReader buffer = new BufferedReader (file);
strInfo = buffer.readLine () + " ";
strInfo += buffer.readLine ();
try {
newWeatherStation = new WeatherStation (strInfo);
} catch (InvalidDataException e) {
throw new InvalidDataException ("Parser File failed (" + strFilePath + ") -> " + e.getMessage ());
}
while ((line = buffer.readLine()) != null)
{
try {
WeatherObservation newWeatherObservation = new WeatherObservation (line);
newWeatherStation.addObservation (newWeatherObservation);
} catch (InvalidDataException e) {};
}
addWeatherStation (newWeatherStation);
if (weatehrStationlistModel != null) {
weatehrStationlistModel.addElement (newWeatherStation.getName());
}
}
public void loadWeatherStationFolder (String strFilePath) throws IOException, InvalidDataException
{
File folder = new File (strFilePath);
String[] strFileList = folder.list ();
for (String s : strFileList) {
loadWeatherStationFile (strFilePath + "\\" + s);
}
}
public void loadWeatherStationFileChooser ()
{
JFileChooser chooser = new JFileChooser ();
chooser.setCurrentDirectory (new java.io.File ("."));
chooser.setDialogTitle ("Read weather station file");
chooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed (false);
if (chooser.showOpenDialog(metOfficeFrame) == JFileChooser.APPROVE_OPTION) {
try {
loadWeatherStationFile (chooser.getSelectedFile ().toString ());
} catch (Exception eException) {
JOptionPane.showMessageDialog (null, eException.getMessage ());
}
}
}
public void saveWeatherStationFile (WeatherStation ws, String strFilePath) throws IOException
{
FileWriter fw = new FileWriter (strFilePath);
String strNewLine = System.getProperty ("line.separator");
fw.write (ws.getName () + strNewLine);
fw.write ("Location " + ws.getPosition ().toString () + " " + ws.getSeaLevel() + "m amsl" + strNewLine);
fw.write ("Missing data (more than 2 days missing in month) is marked by ---." + strNewLine);
fw.write ("yyyy mm tmax tmin af rain sun" + strNewLine);
fw.write (" degC degC days mm hours" + strNewLine);
for (WeatherObservation o : ws.observations()) {
fw.write (o.toString () + strNewLine);
}
fw.flush ();
fw.close ();
}
public void saveWeatherStationFileChooser ()
{
JFileChooser chooser = new JFileChooser ();
chooser.setCurrentDirectory (new java.io.File ("."));
chooser.setDialogTitle ("Save weather station file");
chooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed (false);
if (chooser.showSaveDialog(metOfficeFrame) == JFileChooser.APPROVE_OPTION) {
if(weatehrStationList.getSelectedIndex() != -1) {
try {
WeatherStation ws = getWeatherStation (weatehrStationList.getSelectedValue ().toString ());
saveWeatherStationFile (ws , chooser.getSelectedFile ().toString ());
} catch (Exception eException) {
JOptionPane.showMessageDialog (null, eException.getMessage ());
}
}
}
}
public static void main (String[] args)
{
MetOffice sUKMetOffice = new MetOffice ();
}
}