comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/DA50Parser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/DA50Parser.java@03c824858a40
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.importer.parsers;
2
3 import java.util.ArrayList;
4 import java.util.Map;
5 import java.util.List;
6 import java.util.TreeMap;
7
8 import java.io.File;
9 import java.io.IOException;
10
11 import org.apache.log4j.Logger;
12
13 import de.intevation.flys.importer.XY;
14
15 import de.intevation.artifacts.common.utils.FileTools;
16
17 import de.intevation.flys.utils.EpsilonComparator;
18
19
20 /**
21 * To create cross-sections, generate: Map<double,list<xy>> from files
22 * in da50 format.
23 */
24 public class DA50Parser extends LineParser implements CrossSectionParser
25 {
26 /** Private logger. */
27 private static Logger logger = Logger.getLogger(DA50Parser.class);
28
29 /** The current line to which add points. */
30 private List<XY> currentLine;
31
32 /** Data collected so far, last element will be currentLine. */
33 protected Map<Double, List<XY>> data;
34
35
36 /** Trivial constructor. */
37 public DA50Parser() {
38 data = new TreeMap<Double, List<XY>>(EpsilonComparator.CMP);
39 }
40
41
42 /** Get the description of the cross section parsed. */
43 @Override
44 public String getDescription() {
45 return FileTools.removeExtension(getFileName());
46 }
47
48
49 /** Get the year of this cross sections measurement. */
50 @Override
51 public Integer getYear() {
52 return null;
53 }
54
55
56 /**
57 * Return the data parsed.
58 * @return map of stations (km) to list of points.
59 */
60 @Override
61 public Map<Double, List<XY>> getData() {
62 return data;
63 }
64
65
66 /** Walk a directory tree and attempt parsing all *.d50 files. */
67 public void parseDA50s(File root, final Callback callback) {
68
69 FileTools.walkTree(root, new FileTools.FileVisitor() {
70 @Override
71 public boolean visit(File file) {
72 // TODO check presence of TIM file.
73 if (file.isFile() && file.canRead()
74 && file.getName().toLowerCase().endsWith(".d50")
75 && (callback == null || callback.accept(file))) {
76 reset();
77 try {
78 parse(file);
79 logger.info("parsing done");
80 if (callback != null) {
81 callback.parsed(DA50Parser.this);
82 }
83 }
84 catch (IOException ioe) {
85 logger.error("IOException while parsing file");
86 return false;
87 }
88 }
89 return true;
90 }
91 });
92 }
93
94
95 /** Called before consuming first line of file. */
96 public void reset() {
97 data.clear();
98 currentLine = new ArrayList<XY>();
99 }
100
101
102 /**
103 * Called for each line. Try to extract info from a da50 line.
104 */
105 @Override
106 protected void handleLine(int lineNum, String line) {
107 String pointId = line.substring(0,2);
108 String streetId = line.substring(2,9);
109 String station = line.substring(9,18);
110 String free = line.substring(18,20);
111 String gkLRight = line.substring(20,30);
112 String gkLHigh = line.substring(30,40);
113 String gkRRight = line.substring(40,50);
114 String gkRHigh = line.substring(50,60);
115 String distance = line.substring(60,70);
116
117 // TODO Intersect/Correlate these with e.g. TIM files.
118 // TODO note that as-is these points are really useless.
119 currentLine = new ArrayList<XY>();
120 currentLine.add(new XY(0, 10,0));
121 currentLine.add(new XY(Double.parseDouble(distance), 10, 1));
122 }
123
124
125 /** Called when file is fully consumed. */
126 @Override
127 protected void finish() {
128 logger.info("Parsed " + data.size() + " lines");
129 }
130
131
132 /** Parses files given as arguments. */
133 public static void main(String [] args) {
134
135 DA50Parser parser = new DA50Parser();
136
137 logger.warn("Start parsing files.");
138 for (String arg: args) {
139 parser.parseDA50s(new File(arg), null);
140 logger.warn("Parsing a file.");
141 }
142 logger.error("Finished parsing files.");
143 }
144 }
145 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org