MUG.java
5.84 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class MUG {
int internalId;
int externalId;
float area;
int[] adj;
int[] presc;
int[][] years;
int[] wood_total;
int[] perc_r0_total;
int[] soilloss_total;
int time;
boolean valid;
static public int[] returnInsertable(Scanner myReader){
String data = myReader.nextLine();
String[] str_split = data.split(",", 0);
int size = str_split.length;
int[] toInsert = new int [size];
for(int i=0; i<size; i++) {
toInsert[i] = Integer.parseInt(str_split[i]);
}
return toInsert;
}
static public int[] returnInsertable2(Scanner myReader){
String data = myReader.nextLine();
String[] str_split = data.split(",", 0);
int size = str_split.length;
int[] toInsert = new int[size];
for(int i=0; i<size; i++) {
toInsert[i] = (int)Float.parseFloat(str_split[i]);
}
return toInsert;
}
static public float[] returnInsertableFloat(Scanner myReader){
String data = myReader.nextLine();
String[] str_split = data.split(",", 0);
int size = str_split.length;
float[] toInsert = new float [size];
for(int i=0; i<size; i++) {
toInsert[i] = Float.parseFloat(str_split[i]);
}
return toInsert;
}
static public int[] adjacencyReader(Scanner adjacencies, Scanner borders, int minBorder){
String dataAdj = adjacencies.nextLine();
String[] str_splitA = dataAdj.split(",", 0);
String dataBorders = borders.nextLine();
String[] str_splitB = dataBorders.split(",", 0);
int size = str_splitA.length;
int[] toInsert = new int [size];
toInsert[0] = -1;
int currentIndex = 0;
for(int i=0; i<size; i++) {
if(Float.parseFloat(str_splitB[i]) > minBorder){
toInsert[currentIndex] = Integer.parseInt(str_splitA[i]);
currentIndex++;
}
}
return toInsert;
}
static public int[] returnPeriods(String data){
String[] str_split = data.split(",", 0);
int size = str_split.length;
int[] toInsert = new int [size];
for(int i=0; i<size; i++) {
toInsert[i] = Integer.parseInt(str_split[i]);
}
return toInsert;
}
static public float[] returnByYear(String data){
String[] str_split = data.split(",", 0);
int size = str_split.length;
float[] toInsert = new float [size];
for(int i=0; i<size; i++) {
toInsert[i] = Float.parseFloat(str_split[i]);
}
return toInsert;
}
static public String[] returnByYearString(String data){
String[] str_split = data.split(",", 0);
int size = str_split.length;
String[] toInsert = new String [size];
for(int i=0; i<size; i++) {
toInsert[i] = (str_split[i]);
}
return toInsert;
}
static public void fillArray(MUG[] array, String dir) {
int index = 0;
try { // idea: pass a directory, which should contain files called
// graph_init, ugs_init, ...
File adj_init = new File(dir + "/adj_init.txt");
Scanner readerAdj = new Scanner(adj_init);
File ext_init = new File(dir + "/external_init.txt");
Scanner readerExt = new Scanner(ext_init);
File ugs_init = new File(dir + "/ugs_init.txt");
Scanner readerPresc = new Scanner(ugs_init);
File years_init = new File(dir + "/years_init.txt");
Scanner readerYears = new Scanner(years_init);
File area_init = new File(dir + "/area_init.txt");
Scanner readerArea = new Scanner(area_init);
File wood_total_init = new File(dir + "/wood_total_init.txt");
Scanner readerWoodTotal = new Scanner(wood_total_init);
File soilloss_init = new File(dir + "/soilloss_total_init.txt");
Scanner readerSoillossTotal = new Scanner(soilloss_init);
File perc_r0_init = new File(dir + "/perc_r0_total_init.txt");
Scanner readerPerc_r0 = new Scanner(perc_r0_init);
while (readerAdj.hasNextLine()) {
MUG toInsert = new MUG();
toInsert.valid = true;
toInsert.internalId = index;
toInsert.adj = returnInsertable(readerAdj);
toInsert.externalId = Integer.parseInt(readerExt.nextLine());
toInsert.area = Float.parseFloat(readerArea.nextLine());
toInsert.presc = returnInsertable(readerPresc);
String data = readerYears.nextLine();
String[] removeVSlash = data.split("\\|", 0);
String[] str_split = removeVSlash[1].split("/", 0);
int size = str_split.length;
toInsert.years = new int[size][];
for(int i = 0; i < size; i++) {
toInsert.years[i] = returnPeriods(str_split[i]);
}
toInsert.soilloss_total = returnInsertable2(readerSoillossTotal);
toInsert.perc_r0_total = returnInsertable2(readerPerc_r0);
toInsert.wood_total = returnInsertable2(readerWoodTotal);
array[index] = toInsert;
index++;
}
readerAdj.close();
readerPresc.close();
readerExt.close();
readerArea.close();
readerPerc_r0.close();
readerSoillossTotal.close();
readerWoodTotal.close();
readerYears.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}