annotate flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java @ 499:cce054f27dac

Importer: Only accept main value types 'Q', 'W', 'D' and 'T' by default. flys-backend/trunk@1853 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 08 May 2011 21:34:43 +0000
parents b35c5dc0f8b7
children ee693b8fbb55
rev   line source
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.importer;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
2
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
3 import java.io.BufferedReader;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
4 import java.io.File;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
5 import java.io.FileInputStream;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
6 import java.io.InputStreamReader;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
7 import java.io.IOException;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
8 import java.math.BigDecimal;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
9 import java.text.NumberFormat;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 import java.text.ParseException;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
11
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12 import org.apache.log4j.Logger;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
13
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
14 import de.intevation.flys.importer.ImportDischargeTable;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
15 import de.intevation.flys.importer.ImportDischargeTableValue;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
17 import java.util.regex.Pattern;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
18 import java.util.regex.Matcher;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
19
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
20 import java.util.Date;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
21 import java.util.Calendar;
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
22
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
23 public class AtFileParser {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
24
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
25 public static final String ENCODING = "ISO-8859-1";
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
26
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
27 private static Logger logger = Logger.getLogger(AtFileParser.class);
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
28
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
29 private static NumberFormat nf = NumberFormat.getInstance();
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
30
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
31
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
32 // regular expression from hell to find out time range
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
33 public static final Pattern DATE_LINE = Pattern.compile(
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
34 "^\\*\\s*Abflu[^t]+tafel?\\s*([^\\d]+)" +
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
35 "(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4})\\s*(?:(?:bis)|-)?\\s*" +
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
36 "(?:(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4}))?\\s*.*$");
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
37
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
38 public AtFileParser() {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
39 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
40
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
41
492
14de9246b356 Importer: parse historical discharge tables, too. TODO: store them in database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
42 public ImportDischargeTable parse(File file) throws IOException {
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
43 return parse(file, "", 0);
492
14de9246b356 Importer: parse historical discharge tables, too. TODO: store them in database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
44 }
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
45
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
46 public ImportDischargeTable parse(
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
47 File file,
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
48 String prefix,
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
49 int kind
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
50 )
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
51 throws IOException {
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
52
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
53 logger.info("parsing AT file: " + file);
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
54
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
55 BufferedReader br = null;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
56
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
57 String line = null;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
58
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
59 boolean beginning = true;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
60
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
61 ImportDischargeTable dischargeTable =
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
62 new ImportDischargeTable(kind, prefix + file.getName());
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
63
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
64 Date from = null;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
65 Date to = null;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
66
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
67 try {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
68 br = new BufferedReader(
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
69 new InputStreamReader(
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
70 new FileInputStream(file), ENCODING));
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
71
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
72 while ((line = br.readLine()) != null) {
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
73
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
74 String tmp = line.trim();
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
75
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
76 if (tmp.length() == 0) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
77 continue;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
78 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
79
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
80 Matcher m = DATE_LINE.matcher(tmp);
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
81 if (m.matches()) {
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
82 from = guessDate(m.group(2), m.group(3), m.group(4));
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
83 to = guessDate(m.group(5), m.group(6), m.group(7));
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
84 if (from == null) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
85 Date t = from; from = to; to = t;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
86 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
87 continue;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
88 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
89
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
90 if (tmp.startsWith("#! name=")) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
91 // XXX Skip the name, because we don't know where to save
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
92 // it at the moment
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
93
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
94 //String name = tmp.substring(8);
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
95 continue;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
96 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
97
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
98 if (tmp.startsWith("#") || tmp.startsWith("*")) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
99 continue;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
100 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
101
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
102 String[] splits = tmp.replace(',', '.').split("\\s+");
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
103
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
104 if ((splits.length < 2) || (splits.length > 11)) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
105 logger.warn("Found an invalid row in the AT file.");
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
106 continue;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
107 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
108
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
109 String strW = splits[0].trim();
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
110 double W = nf.parse(strW).doubleValue();
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
111
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
112 /* shift is used to differenciate between lines with
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
113 * exactly 10 Qs and lines with less than 10 Qs. The shift
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
114 * is only modified when it is the first line.
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
115 */
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
116 int shift = 0;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
117
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
118 if (splits.length != 11 && beginning) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
119 shift = 11 - splits.length;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
120 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
121
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
122
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
123 for (int i = 1; i < splits.length; i++) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
124 double iW = W + shift + i;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
125 double iQ = nf.parse(splits[i].trim()).doubleValue();
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
126
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
127 dischargeTable.addDischargeTableValue(
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
128 new ImportDischargeTableValue(
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
129 new BigDecimal(iQ/100.0),
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
130 new BigDecimal(iW/100.0)));
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
131 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
132
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
133 beginning = false;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
134 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
135 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
136 catch (ParseException pe) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
137 logger.warn(pe.getMessage());
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
138 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
139 finally {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
140 if (br != null) {
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
141 br.close();
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
142 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
143 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
144
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 485
diff changeset
145 if (from != null) {
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
146 if (to != null && from.compareTo(to) > 0) {
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
147 Date t = from; from = to; to = t;
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
148 }
492
14de9246b356 Importer: parse historical discharge tables, too. TODO: store them in database.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
149 logger.info("from: " + from + " to: " + to);
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 485
diff changeset
150 ImportTimeInterval interval = new ImportTimeInterval(from, to);
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 485
diff changeset
151 dischargeTable.setTimeInterval(interval);
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 485
diff changeset
152 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 485
diff changeset
153
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
154 logger.info("Finished parsing AT file: " + file);
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
155
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
156 return dischargeTable;
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
157 }
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
158
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
159 public static Date guessDate(String day, String month, String year) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
160 if (day == null && month == null && year == null) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
161 return null;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
162 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
163
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
164 logger.debug("day: " + day + " month: " + month + " year: " + year);
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
165
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
166 int dayI = 15;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
167 if (day != null) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
168 try {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
169 dayI = Integer.parseInt(day.trim());
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
170 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
171 catch (NumberFormatException nfe) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
172 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
173 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
174
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
175 int monthI = 6;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
176 if (month != null) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
177 try {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
178 monthI = Integer.parseInt(month.trim());
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
179 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
180 catch (NumberFormatException nfe) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
181 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
182 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
183
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
184 int yearI = 1900;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
185 if (year != null) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
186 try {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
187 yearI = Integer.parseInt(year.trim());
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
188 if (yearI < 100) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
189 if (yearI < 20) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
190 yearI += 2000;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
191 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
192 else {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
193 yearI += 1900;
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
194 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
195 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
196 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
197 catch (NumberFormatException nfe) {
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
198 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
199 }
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
200
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
201 Calendar cal = Calendar.getInstance();
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
202 cal.set(yearI, monthI-1, dayI, 12, 0, 0);
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
203 long ms = cal.getTimeInMillis();
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 492
diff changeset
204 cal.setTimeInMillis(ms - ms%1000);
485
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
205 return cal.getTime();
6b231041dc18 Importer: Try to extract time ranges from at files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 198
diff changeset
206 }
198
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
207 }
d980e545ccab Added import code for importing discharge tables.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
208 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org