Debian Codegraph @

src/footleg/cavesurvey/data/reader/CompassParser.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2009-2017 Paul Fretwell - aka 'Footleg' ([email protected])
3 *
4 * This file is part of Cave Converter.
5 *
6 * Cave Converter is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Cave Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Cave Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package footleg.cavesurvey.data.reader;
20
21 import java.text.ParseException;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25
26 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
27 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
28 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
29 import footleg.cavesurvey.converter.Logger;
30 import footleg.cavesurvey.data.model.CaveSurvey;
31 import footleg.cavesurvey.data.model.Equate;
32 import footleg.cavesurvey.data.model.SurveyLeg;
33 import footleg.cavesurvey.data.model.SurveySeries;
34 import footleg.cavesurvey.tools.UtilityFunctions;
35
36 /**
37 * Parser for Compass format text data files.
38 *
39 * @author Footleg
40 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
41 * @since 1.6 (The Java version used)
42 *
43 */
44 public class CompassParser {
45 private Logger logger;
46
47 /**
48 * Class constructor
49 * @param logger Logging class to output information, warning and error messages to
50 */
51 public CompassParser( Logger logger ) {
52 super();
53 this.logger = logger;
54 }
55
56 /**
57 * Parse Compass format data into the cave data model
58 *
59 * @param surveyFileData ListArray of data lines from a Compass file
60 * @return Cave Survey object
61 * @throws ParseException Exception raised when information in a survey data file is not supported or valid for the format
62 */
63 public CaveSurvey parseFile( List<String> surveyFileData ) throws ParseException {
64 /**
65 * Read state codes:
66 * 0=starting new file
67 * 1=header lines
68 * 2=calibration data
69 * 3=data line header
70 * 4=data lines
71 */
72 int state = 0;
73
74 //Create cave survey object to hold data
75 CaveSurvey allSeries = new CaveSurvey(logger);
76
77 //Create a series instance to use as a pointer to the active series data is being read from
78 SurveySeries liveSeries = null;
79
80 //Declare flags to store status of legs being read
81 boolean duplicateFlag = false;
82 boolean ignoreLeg = false;
83 boolean splayFlag = false;
84 boolean surfaceFlag = false;
85 boolean backBearings = false;
86
87 //Loop through all data lines
88 for ( int i=0; i < surveyFileData.size(); i++ ) {
89 int lineNo = i + 1;
90 String dataLine = surveyFileData.get(i);
91
92 //Trim whitespace off line ends (unless a single form feed character)
93 if (dataLine.equalsIgnoreCase("\f") == false ) {
94 dataLine = dataLine.trim();
95 }
96 //Skip blank lines
97 if ( dataLine.length() > 0 ) {
98 switch (state) {
99 case 0:
100 //First line of series should be cave name
101 String caveName = dataLine.replace(' ', '_');
102 //Create series for cave name
103 liveSeries = new SurveySeries( caveName );
104 //Add to cave survey
105 allSeries.add( liveSeries );
106 state++;
107 break;
108 case 1:
109 //Header lines
110 caveName = dataLine.replace(' ', '_');
111 if ( caveName.compareTo( allSeries.get(0).getSeriesName() ) == 0 ) {
112 //Repeat of cave name, which we can ignore
113 }
114 else if ( dataLine.substring(0, 12).compareTo( "SURVEY NAME:" ) == 0 ) {
115 //Create series for this survey name
116 String seriesName = dataLine.substring(12).trim();
117 liveSeries = new SurveySeries( seriesName );
118
119 //Next line: Data and Comment
120 i++;
121 lineNo = i + 1;
122 String surveyDataComment = surveyFileData.get(i).trim();
123 int commentPos = surveyDataComment.indexOf("COMMENT:");
124 if ( surveyDataComment.substring(0, 12).compareTo( "SURVEY DATE:" ) == 0 ) {
125 //Check if comment was present on line
126 int dateEnd = commentPos;
127 if ( commentPos == -1 ) {
128 //No comment, so set commentPos to end of line
129 commentPos = surveyDataComment.length();
130 dateEnd = commentPos;
131 }
132 else {
133 //Set comment start position to after COMMENT:
134 commentPos = commentPos + 8;
135 }
136 if ( commentPos > 18 ) {
137 //Separate date and comment
138 String dateString = surveyDataComment.substring(12,dateEnd).trim();
139 String comment = surveyDataComment.substring( commentPos ).trim();
140 //Parse date
141 Date value = UtilityFunctions.stringToDate(dateString, "M d yy");
142 //Store date and comment in series
143 liveSeries.setSurveyDate(value);
144 liveSeries.setComment(comment);
145
146 //Survey team on next pair of lines
147 i++;
148 lineNo = i + 1;
149 String surveyTeam1 = surveyFileData.get(i).trim();
150 if ( surveyTeam1.substring(0, 12).compareTo( "SURVEY TEAM:" ) == 0 ) {
151 i++;
152 lineNo = i + 1;
153 String surveyTeam2 = surveyFileData.get(i).trim();
154 //TODO Store team details in survey series
155
156 //Header section done
157 state++;
158 }
159 else {
160 throw new ParseException( UtilityFunctions.formatFileParserMsg("Did not find expected 'SURVEY TEAM:' at start of line.", lineNo ), lineNo );
161 }
162 }
163 else {
164 throw new ParseException( UtilityFunctions.formatFileParserMsg("Did not find expected 'COMMENT:' or valid length date string on line.", lineNo ), lineNo );
165 }
166 }
167 else {
168 throw new ParseException( UtilityFunctions.formatFileParserMsg("Did not find expected 'SURVEY DATE:' at start of line.", lineNo ), lineNo );
169 }
170 }
171 else {
172 throw new ParseException( UtilityFunctions.formatFileParserMsg("Did not find expected 'SURVEY NAME:' line.", lineNo ), lineNo );
173 }
174
175 break;
176 case 2:
177 //calibration data
178 if ( dataLine.substring(0, 12).compareTo( "DECLINATION:" ) == 0 ) {
179 int formatPos = dataLine.indexOf("FORMAT:");
180 int formatEndPos = dataLine.length();
181 int corrections1Pos = dataLine.indexOf("CORRECTIONS:");
182 int corrections1EndPos = dataLine.length();
183 int corrections2Pos = dataLine.indexOf("CORRECTIONS2:");
184 int corrections2EndPos = dataLine.length();
185 int declinationEndPos = dataLine.length();
186 //Determine end positions of data items
187 if ( corrections2Pos > 12 ) {
188 corrections1EndPos = corrections2Pos;
189 if ( corrections1Pos > 12 ) {
190 formatEndPos = corrections1Pos;
191 }
192 else {
193 formatEndPos = corrections2Pos;
194 }
195 }
196 else {
197 if ( corrections1Pos > 12 ) {
198 formatEndPos = corrections1Pos;
199 }
200 }
201 //Process optional file format code
202 if ( formatPos > 12 ) {
203 declinationEndPos = formatPos;
204 String formatCode = dataLine.substring(formatPos+7,formatEndPos).trim();
205 //TODO Set series default units (once supported in series class)
206 //TODO Set default data order for series (once supported in series class)
207 //TODO Handle LRUD on toStns (currently assumes they are for fromStns)
208 }
209 //Process declination
210 String declination = dataLine.substring(12,declinationEndPos).trim();
211 liveSeries.setDeclination( - Double.parseDouble( declination ) );
212 //Process optional corrections
213 if ( corrections1Pos > 12 ) {
214 String corrections = dataLine.substring(corrections1Pos+12,corrections1EndPos).trim();
215 String[] data = UtilityFunctions.cleanAndSplitDataLine(corrections);
216 liveSeries.setCompassCalibration( - Double.parseDouble( data[0] ), BearingUnit.Degrees );
217 liveSeries.setClinoCalibration( - Double.parseDouble( data[1] ), GradientUnit.Degrees );
218 liveSeries.setTapeCalibration( - Double.parseDouble( data[2] ), LengthUnit.Feet );
219 }
220 //Process optional corrections2
221 if ( corrections2Pos > 12 ) {
222 String corrections2 = dataLine.substring(corrections2Pos+13,corrections2EndPos).trim();
223 String[] data = UtilityFunctions.cleanAndSplitDataLine(corrections2);
224 //TODO Handle corrections for second set of instruments used for back bearings
225 }
226 //Declination line done
227 state++;
228 }
229 else {
230 throw new ParseException( UtilityFunctions.formatFileParserMsg("Did not find expected 'DECLINATION:' line.", lineNo ), lineNo );
231 }
232
233 break;
234 case 3:
235 //data line header
236 //"FROM TO LENGTH BEARING INC LEFT UP DOWN RIGHT FLAGS COMMENTS"
237 //"FROM TO LENGTH BEARING DIP LEFT UP DOWN RIGHT AZM2 INC2 FLAGS COMMENTS"
238 //TODO Support different ordering of LRUD fields in Compass files
239 String[] headerData = UtilityFunctions.cleanAndSplitDataLine(dataLine);
240 if ( ( headerData[0].compareTo("FROM") == 0 )
241 && ( headerData[1].compareTo("TO") == 0 )
242 && ( headerData[2].compareTo("LENGTH") == 0 )
243 && ( headerData[3].compareTo("BEARING") == 0 )
244 && ( ( headerData[4].compareTo("INC") == 0 ) || ( headerData[4].compareTo("DIP") == 0 ) ) ) {
245 if ( ( headerData[9].compareTo("AZM2") == 0 )
246 && ( headerData[11].compareTo("FLAGS") == 0 )
247 && ( headerData[12].compareTo("COMMENTS") == 0 ) ) {
248 //Header for data with back bearings
249 backBearings = true;
250 //Ready to read data lines
251 state++;
252 }
253 else if ( ( headerData[9].compareTo("FLAGS") == 0 )
254 && ( headerData[10].compareTo("COMMENTS") == 0 ) ) {
255 //Header for data without back bearings
256 backBearings = false;
257 //Ready to read data lines
258 state++;
259 }
260 if ( ( headerData[5].compareTo("LEFT") == 0 )
261 && ( headerData[6].compareTo("UP") == 0 )
262 && ( headerData[7].compareTo("DOWN") == 0 )
263 && ( headerData[8].compareTo("RIGHT") == 0 ) ) {
264 //Header for data with back bearings
265 }
266 else {
267 throw new ParseException( UtilityFunctions.formatFileParserMsg(
268 "LRUD data heading indicates unsupported order. Currently only LEFT UP DOWN RIGHT field order is supported.", lineNo ), lineNo );
269 }
270 }
271 else {
272 throw new ParseException( UtilityFunctions.formatFileParserMsg("Did not find expected data heading line.", lineNo ), lineNo );
273 }
274 break;
275 case 4:
276 //data lines
277 //" 1 2 85.00 45.00 0.00 10.00 3.00 0.00 10.00 #|L#"
278 //" 7 8 17.00 201.00 0.00 0.00 5.00 0.00 4.00 tributary enters here on left "
279
280 //Check for end of series (form feed character)
281 if (dataLine.equalsIgnoreCase("\f") ) {
282 //Close series to cave name series and start searching for next one
283 allSeries.get(0).addSeries( liveSeries );
284 state = 1;
285 }
286 else {
287 //Process data line
288 String[] data = UtilityFunctions.cleanAndSplitDataLine(dataLine);
289 SurveyLeg leg = new SurveyLeg();
290
291 //Create record from the items
292 int index = 0;
293 while ( index < data.length ) {
294 String item = data[index];
295
296 //Put item into appropriate value
297 switch ( index ) {
298 case 0:
299 //TODO Add support for retaining station name when not a number
300 leg.setFromStn( UtilityFunctions.createStationFromNameForSeries( item, liveSeries ) );
301 break;
302 case 1:
303 //TODO Add support for retaining station name when not a number
304 leg.setToStn( UtilityFunctions.createStationFromNameForSeries( item, liveSeries ) );
305 break;
306 case 2:
307 leg.setLength( Double.parseDouble( item ), LengthUnit.Feet );
308 break;
309 case 3:
310 if ( item.compareTo("-") == 0 ) {
311 leg.setCompass( 0, BearingUnit.Degrees );
312 }
313 else {
314 leg.setCompass( Double.valueOf( item ), BearingUnit.Degrees );
315 }
316 break;
317 case 4:
318 String val = item;
319
320 //TODO Does compass support all these Survex like clino values?
321 double straightDown = -90;
322 double straightUp = 90;
323 double level = 0;
324 if ( val.compareToIgnoreCase("-V") == 0 ) {
325 leg.setClino( straightDown, GradientUnit.Degrees );
326 }
327 else if ( val.compareToIgnoreCase("down") == 0 ) {
328 leg.setClino( straightDown, GradientUnit.Degrees );
329 }
330 else if ( val.compareToIgnoreCase("d") == 0 ) {
331 leg.setClino( straightDown, GradientUnit.Degrees );
332 }
333 else if ( val.compareToIgnoreCase("+V") == 0 ) {
334 leg.setClino( straightUp, GradientUnit.Degrees );
335 }
336 else if ( val.compareToIgnoreCase("up") == 0 ) {
337 leg.setClino( straightUp, GradientUnit.Degrees );
338 }
339 else if ( val.compareToIgnoreCase("u") == 0 ) {
340 leg.setClino( straightUp, GradientUnit.Degrees );
341 }
342 else if ( val.compareToIgnoreCase("-") == 0 ) {
343 leg.setClino( level, GradientUnit.Degrees );
344 }
345 else if ( val.compareToIgnoreCase("level") == 0 ) {
346 leg.setClino( level, GradientUnit.Degrees );
347 }
348 else {
349 leg.setClino( Double.valueOf(val), GradientUnit.Degrees );
350 }
351 break;
352 case 5:
353 case 6:
354 case 7:
355 case 8:
356 //"FROM TO LENGTH BEARING INC LEFT UP DOWN RIGHT FLAGS COMMENTS"
357 //Ignore LRUD values which are -9999.00
358 if ( ( item.equals("-9999.00") == false ) && ( item.equals("-9.90") == false ) ) {
359 if (index == 5) {
360 leg.setLeft( Double.parseDouble( item ), LengthUnit.Feet );
361 }
362 else if (index == 6) {
363 leg.setUp( Double.parseDouble( item ), LengthUnit.Feet );
364 }
365 else if (index == 7) {
366 leg.setDown( Double.parseDouble( item ), LengthUnit.Feet );
367 }
368 else if (index == 8) {
369 leg.setRight( Double.parseDouble( item ), LengthUnit.Feet );
370 }
371 }
372 break;
373 case 9:
374 if ( backBearings ) {
375 //TODO Support for back bearings compass reading
376 String backBearing = item;
377 }
378 break;
379 case 10:
380 if ( backBearings ) {
381 //TODO Support for back bearings clino reading
382 String backClino = item;
383 }
384 break;
385 }
386 index++;
387 }
388
389 //Process flags and comments off end of string from fixed position
390 //(as comment will have been split into multiple data array items)
391 String commentFlagsData = "";
392
393 //Reinitialise all flags for each leg
394 duplicateFlag = false;
395 ignoreLeg = false;
396 splayFlag = false;
397 surfaceFlag = false;
398
399 //Determine if any flags or comments data exists on this line
400 int dataEndPos = surveyFileData.get(i).length();
401 if ( backBearings ) {
402 if ( data.length > 11 ) {
403 String firstItem = data[11];
404 dataEndPos = surveyFileData.get(i).indexOf(firstItem);
405 }
406 }
407 else {
408 if ( data.length > 9 ) {
409 String firstItem = data[9];
410 dataEndPos = surveyFileData.get(i).indexOf(firstItem);
411 }
412 }
413
414 if ( surveyFileData.get(i).length() > dataEndPos ) {
415 commentFlagsData = surveyFileData.get(i).substring( dataEndPos );
416 }
417
418 //Initialise comment to entire flags/comments data item
419 String comment = commentFlagsData;
420 int commentStartPos = 0;
421 if ( commentFlagsData.startsWith("#|") ) {
422 commentStartPos = commentFlagsData.indexOf("#", 2) + 1;
423 }
424 if ( commentStartPos > 3 ) {
425 //Process flags
426 String flags = commentFlagsData.substring(2, commentStartPos - 1);
427
428 //Handle 'L' which indicates duplicate
429 if ( flags.indexOf("L") > -1 ) {
430 duplicateFlag = true;
431 }
432
433 //Handle 'X' which indicates duplicate
434 if ( flags.indexOf("X") > -1 ) {
435 ignoreLeg = true;
436 }
437
438 //TODO Store flags 'P' and 'C' which mean do not plot, and do not apply loop closure
439
440 //Set comment to the text after the flags end character
441 comment = commentFlagsData.substring(commentStartPos).trim();
442 }
443
444 //Set leg comment
445 leg.setComment(comment);
446
447 //Check leg was found
448 if ( leg.getLength(LengthUnit.Metres) > -1 ) {
449 //Set flags for leg
450 leg.setDuplicate(duplicateFlag);
451 leg.setSplay(splayFlag);
452 leg.setSurface(surfaceFlag);
453 //Check if ignore leg flag was set
454 if ( ignoreLeg == false ) {
455 //Add leg to series
456 liveSeries.addLeg(leg);
457 }
458 }
459 }
460 break;
461 }
462 }
463 }
464
465 //Generate equates by matching station names
466 List<Equate> equates = new ArrayList<Equate>();
467 //Loop through all series
468 for ( int idx1 = 0; idx1 < allSeries.get(0).getInnerSeriesList().size(); idx1++ ) {
469 SurveySeries series = allSeries.get(0).getInnerSeries(idx1);
470
471 logger.logMessage("Searching for equivalent stations in series " + (idx1 + 1) + " of " +
472 allSeries.get(0).getInnerSeriesList().size() + "...");
473
474 //Loop through all station names in series
475 for ( int idxLeg = 0; idxLeg < series.legCount(); idxLeg++ ) {
476 for ( int idxStn = 0; idxStn < 2; idxStn++ ) {
477 String stn1Name = "";
478 if ( idxStn == 0 ) {
479 stn1Name = series.getLegRaw(idxLeg).getFromStn().getName();
480 }
481 else {
482 stn1Name = series.getLegRaw(idxLeg).getToStn().getName();
483 }
484
485 //Check for matching station name in all following series
486 for ( int idx2 = idx1 + 1; idx2 < allSeries.get(0).getInnerSeriesList().size(); idx2++ ) {
487 SurveySeries series2 = allSeries.get(0).getInnerSeries(idx2);
488 //Loop through all station names in series
489 for ( int idxLeg2 = 0; idxLeg2 < series2.legCount(); idxLeg2++ ) {
490 for ( int idxStn2 = 0; idxStn2 < 2; idxStn2++ ) {
491 String stn2Name = "";
492 if ( idxStn2 == 0 ) {
493 stn2Name = series2.getLegRaw(idxLeg2).getFromStn().getName();
494 }
495 else {
496 stn2Name = series2.getLegRaw(idxLeg2).getToStn().getName();
497 }
498
499 //Check for matching station name in all following series
500 if ( stn1Name.compareTo(stn2Name) == 0 ) {
501 //Matching stations found
502 String series1Name = allSeries.get(0).getSeriesName() + "." + series.getSeriesName();
503 String series2Name = allSeries.get(0).getSeriesName() + "." + series2.getSeriesName();
504 //Check if equate already added
505 boolean foundNewEquate = true;
506 for ( int idxEquates = 0; idxEquates < equates.size(); idxEquates++ ) {
507 Equate testEquate = equates.get(idxEquates);
508 if ( ( series1Name.compareTo(testEquate.getSeries1()) == 0 )
509 && ( series2Name.compareTo(testEquate.getSeries2()) == 0 )
510 && ( stn1Name.compareTo(testEquate.getStn1()) == 0 )
511 && ( stn2Name.compareTo(testEquate.getStn2()) == 0 ) ) {
512 foundNewEquate = false;
513 }
514 else if ( ( series1Name.compareTo(testEquate.getSeries2()) == 0 )
515 && ( series2Name.compareTo(testEquate.getSeries1()) == 0 )
516 && ( stn1Name.compareTo(testEquate.getStn2()) == 0 )
517 && ( stn2Name.compareTo(testEquate.getStn1()) == 0 ) ) {
518 foundNewEquate = false;
519 }
520 }
521
522 if ( foundNewEquate ) {
523 //Create and add equate
524 Equate newEquate = new Equate( series1Name, stn1Name, series2Name, stn2Name);
525 equates.add(newEquate);
526 }
527 }
528 }
529 }
530 }
531 }
532 }
533 }
534
535
536 //Process equates
537 UtilityFunctions.processEquates(equates, allSeries);
538
539 //Debug dump
540 UtilityFunctions.logSurveyDebugData(allSeries, logger);
541
542 //Completed file parsing
543 return allSeries;
544 }
545
546 }