Debian Codegraph @

src/footleg/cavesurvey/data/reader/SurvexParser.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.Iterator;
25 import java.util.List;
26
27 import footleg.cavesurvey.converter.CaveConverter;
28 import footleg.cavesurvey.converter.Logger;
29 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
30 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
31 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
32 import footleg.cavesurvey.data.model.CaveSurvey;
33 import footleg.cavesurvey.data.model.Equate;
34 import footleg.cavesurvey.data.model.SurveyLeg;
35 import footleg.cavesurvey.data.model.SurveySeries;
36 import footleg.cavesurvey.tools.UtilityFunctions;
37
38 /**
39 * Parser for Survex format text data files.
40 *
41 * @author Footleg
42 * @version 2017.01.12 (ISO 8601 YYYY.MM.DD)
43 * @since 1.6 (The Java version used)
44 *
45 * @to.do
46 * TODO Parse entrance flags
47 * TODO Parse fix flags
48 * TODO Parse calibration comments fields
49 * TODO Parse team fields
50 * TODO Parse instrument fields
51 */
52 public class SurvexParser {
53 private Logger logger;
54
55 /**
56 * Class constructor
57 * @param logger Logging class to output information, warning and error messages to
58 */
59 public SurvexParser( Logger logger ) {
60 super();
61 this.logger = logger;
62 }
63
64 private class DataBlockProperties {
65
66 public DataBlockProperties(boolean duplicateFlag, boolean splayFlag, boolean surfaceFlag, boolean noSurvey) {
67 super();
68 this.duplicateFlag = duplicateFlag;
69 this.splayFlag = splayFlag;
70 this.surfaceFlag = surfaceFlag;
71 this.noSurvey = noSurvey;
72 }
73
74 public DataBlockProperties(DataBlockProperties parentBlock) {
75 super();
76 this.duplicateFlag = parentBlock.duplicateFlag;
77 this.splayFlag = parentBlock.splayFlag;
78 this.surfaceFlag = parentBlock.surfaceFlag;
79 this.noSurvey = parentBlock.noSurvey;
80 this.parentBlock = parentBlock;
81 }
82
83 private boolean duplicateFlag = false;
84 private boolean splayFlag = false;
85 private boolean surfaceFlag = false;
86 private boolean noSurvey = false;
87 private DataBlockProperties parentBlock;
88
89 }
90
91 /**
92 * Parse survex format data into the cave data model
93 *
94 * @param surveyFileData ListArray of data lines from a survex file
95 * @param multifileRefs ArrayList of strings indicating the original file and line number for each data line in the
96 * surveyFileData ArrayList passed in. Used to report errors in logger for original survey data files when parsing multi-file Survex projects.
97 * @return Cave Survey object
98 * @throws ParseException Exception raised when information in a survey data file is not supported or valid for the format
99 */
100 public CaveSurvey parseFile( List<String> surveyFileData, List<String> multifileRefs ) throws ParseException {
101 /**
102 * Read state codes:
103 * 0=starting new file
104 * 1=inside begin/end block
105 * 2=LRUD block
106 */
107
108 int state = 0;
109
110 //Create cave survey object to hold data
111 CaveSurvey allSeries = new CaveSurvey(logger);
112
113 //Create stack to hold open series while processing data lines
114 List<SurveySeries> seriesStack = new ArrayList<SurveySeries>();
115
116 //Create stack to hold open series names while processing data lines
117 List<String> nameStack = new ArrayList<String>();
118
119 //Create list for equates
120 List<Equate> equates = new ArrayList<Equate>();
121
122 //Create a series instance to use as a pointer to the active series data is being read from
123 SurveySeries liveSeries = null;
124
125 //Create list to hold data order for active data block
126 List<String> dataOrder = new ArrayList<String>();
127
128 //Variable to hold values for parent block when inside an anonymous block
129 DataBlockProperties blockProps = new DataBlockProperties( false, false, false, false );
130
131 //Loop through all data lines
132 for ( int fileDataIdx=0; fileDataIdx < surveyFileData.size(); fileDataIdx++ ) {
133 //Get data line, file line number and line reference for the next data line from the survey data array list
134 int lineNo = fileDataIdx + 1;
135 String dataLine = surveyFileData.get(fileDataIdx);
136 String lineRef = "" + lineNo;
137 if ( multifileRefs != null ) {
138 lineRef = multifileRefs.get( fileDataIdx );
139 }
140
141 //Discard text after comment character
142 //TODO Keep comments and add into data model for legs, series and file heading
143 int commentPos = dataLine.indexOf(';');
144 if ( commentPos > -1 ) {
145 dataLine = dataLine.substring(0, commentPos);
146 }
147 //Trim whitespace off line ends
148 dataLine = dataLine.trim();
149 //Skip blank lines
150 if ( dataLine.length() > 0 ) {
151 //Check line for commands
152 if ( dataLine.charAt(0) == '*' ) {
153 //Process line into individual items trimming off initial '*'
154 String[] data = UtilityFunctions.cleanAndSplitDataLine( dataLine.substring(1) );
155 //Get command keyword
156 String cmd = data[0];
157
158 //Check for expected commands
159 if ( cmd.compareToIgnoreCase("BEGIN") == 0 ) {
160 //Start of new block
161 state = 1;
162 if (data.length == 2) {
163 //Create series
164 liveSeries = new SurveySeries(data[1]);
165 //Add name to stack
166 nameStack.add(data[1]);
167 //Get calibrations from last series and apply to new child series (if present)
168 double tapeCal = 0.0;
169 double compassCal = 0.0;
170 double clinoCal = 0.0;
171 double declinationCal = 0.0;
172 //Check if a parent series exists
173 if (seriesStack.size() > 0) {
174 SurveySeries series = seriesStack.get( seriesStack.size() - 1 );
175 tapeCal = series.getTapeCalibration(LengthUnit.Metres);
176 compassCal = series.getCompassCalibration(BearingUnit.Degrees);
177 clinoCal = series.getClinoCalibration(GradientUnit.Degrees);
178 declinationCal = series.getDeclination();
179 dataOrder = series.getDataOrder();
180 }
181 //Put new series onto stack
182 seriesStack.add(liveSeries);
183 //Apply calibrations from parent series.
184 //These will get overwritten if this series has it's own calibrations.
185 liveSeries.setTapeCalibration(tapeCal, LengthUnit.Metres);
186 liveSeries.setCompassCalibration(compassCal, BearingUnit.Degrees);
187 liveSeries.setClinoCalibration(clinoCal, GradientUnit.Degrees);
188 liveSeries.setDeclination(declinationCal);
189 liveSeries.setDataOrder(dataOrder);
190 //Copy flag settings into new child series and store parent settings to retrieve at series end
191 DataBlockProperties newBlock = new DataBlockProperties( blockProps );
192 //Make new properties block the current one
193 blockProps = newBlock;
194 }
195 else if (data.length < 2) {
196 //Anonymous block begin. Put properties from last block into new block as parent
197 DataBlockProperties newBlock = new DataBlockProperties( blockProps );
198 //then swap new block to current
199 blockProps = newBlock;
200 // throw new ParseException( UtilityFunctions.formatFileParserMsg("BEGIN/END blocks without names are not supported.", lineRef ), lineNo );
201 }
202 else {
203 //Do not support begin/end blocks names with white space
204 throw new ParseException( UtilityFunctions.formatFileParserMsg("BEGIN/END blocks names containing spaces are not supported.", lineRef ), lineNo );
205 }
206 }
207 else if ( cmd.compareToIgnoreCase("END") == 0 ) {
208 //End block
209 if ( data.length == 1 ) {
210 //Anonymous block ended, so just restore parent block flags
211 blockProps = blockProps.parentBlock;
212 }
213 else {
214 //Named block ended
215 String blockEndName = data[1];
216 //Check end matches end of section name
217 String currentBlockName = nameStack.get( nameStack.size() - 1 );
218 if ( currentBlockName.compareToIgnoreCase(blockEndName) == 0 ) {
219 //Found matching end block, so close series
220 //Remove live series from stack, as it is closed
221 SurveySeries endedSeries = seriesStack.remove( seriesStack.size() - 1 );
222 nameStack.remove( nameStack.size() - 1 );
223 //Restore block properties from parent series block
224 blockProps = blockProps.parentBlock;
225 if ( seriesStack.size() > 0) {
226 //Series is inside another, so make that live and add finished series to it
227 liveSeries = seriesStack.get( seriesStack.size() - 1 );
228 liveSeries.addSeries( endedSeries );
229 //Return state to 1
230 state = 1;
231 }
232 else {
233 //No other series on stack, so add to main cave survey
234 allSeries.add( endedSeries );
235 //Clear reference to live series
236 liveSeries = null;
237 //Return state to 0
238 state = 0;
239 }
240 }
241 else {
242 //Names of begin end blocks do not match
243 throw new ParseException( UtilityFunctions.formatFileParserMsg("Names of begin end blocks do not match. Begin=" +
244 currentBlockName + " End=" + blockEndName + ".", lineRef ), lineNo );
245 }
246 }
247 }
248 else if ( cmd.compareToIgnoreCase("EQUATE") == 0 ) {
249 //Get two parts of the equate, expanding to full nested series names
250 String fullSeriesPrefix = fullNestedSeriesName(nameStack);
251 Equate equate = new Equate( fullSeriesPrefix, data[1], fullSeriesPrefix, data[2] );
252 //Add to cache
253 equates.add(equate);
254 }
255 else if ( cmd.compareToIgnoreCase("DATA") == 0 ) {
256 //Reset nosurvey flag
257 blockProps.noSurvey = false;
258 //Check data command type
259 if ( data[1].compareToIgnoreCase("PASSAGE") == 0 ) {
260 //LRUD data block
261 state = 2;
262 }
263 else if ( data[1].compareToIgnoreCase("NORMAL") == 0 ) {
264 state = 1;
265 //Check data order
266 //TODO Add support for backcompass and backclino in normal data
267 logger.logMessage( "Found normal data header at line " + lineNo + ". Checking format." );
268
269 //Build array of expected categories here, and then loop through items picking off those that match until done.
270 //We will then know if all were found and the format is valid.
271 dataOrder.clear();
272 List<String> allOrderKeys = new ArrayList<String>();
273 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_FROMSTN );
274 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_TOSTN );
275 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_LENGTH );
276 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_BEARING );
277 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_CLINO );
278 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_IGNOREALL );
279
280 for (int dataIdx = 2; dataIdx < data.length; dataIdx++) {
281 //Get next item (including translating alternative terms)
282 String item = data[dataIdx].toUpperCase();
283 if ( item.compareTo("TAPE") == 0 ) {
284 item = CaveConverter.DATA_ORDER_CAT_LENGTH;
285 }
286 else if ( item.compareTo("COMPASS") == 0 ) {
287 item = CaveConverter.DATA_ORDER_CAT_BEARING;
288 }
289 else if ( item.compareTo("CLINO") == 0 ) {
290 item = CaveConverter.DATA_ORDER_CAT_CLINO;
291 }
292 //Look for item in remaining unused keys list
293 int foundIdx = allOrderKeys.indexOf( item );
294 if ( foundIdx > -1 ) {
295 //Add this item to dataOrder list, and remove from unused keys list
296 dataOrder.add( item );
297 allOrderKeys.remove( foundIdx );
298 }
299 else {
300 //Item not found, so unsupported data normal block
301 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported survex normal data order. Term '" + item + "' is not supported.", lineRef ), lineNo );
302 }
303 }
304 //Add to live series, as primary unless that is already set to diving,
305 //in which case set as secondary
306 List<String> checkDataOrder = liveSeries.getDataOrder();
307 if ( checkDataOrder.size() > 0
308 && ( UtilityFunctions.dataOrderIsDiving( checkDataOrder ) ) ) {
309 liveSeries.setDataOrder2(dataOrder);
310 }
311 else {
312 liveSeries.setDataOrder(dataOrder);
313 }
314 }
315 else if ( data[1].compareToIgnoreCase("DIVING") == 0 ) {
316 state = 1;
317 //Check data order
318 //TODO Add support for backcompass and backclino in diving data
319 logger.logMessage( "Found diving data header at line " + lineNo + ". Checking format." );
320
321 //Build array of expected categories here, and then loop through items picking off those that match until done.
322 //We will then know if all were found and the format is valid.
323 dataOrder.clear();
324 List<String> allOrderKeys = new ArrayList<String>();
325 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_FROMSTN );
326 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_TOSTN );
327 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_LENGTH );
328 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_BEARING );
329 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_FROMDEPTH );
330 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_TODEPTH );
331 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_DEPTHCHANGE );
332 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_IGNOREALL );
333
334 for (int dataIdx = 2; dataIdx < data.length; dataIdx++) {
335 //Get next item (including translating alternative terms)
336 String item = data[dataIdx].toUpperCase();
337 if ( item.compareTo("TAPE") == 0 ) {
338 item = CaveConverter.DATA_ORDER_CAT_LENGTH;
339 }
340 else if ( item.compareTo("COMPASS") == 0 ) {
341 item = CaveConverter.DATA_ORDER_CAT_BEARING;
342 }
343 //Look for item in remaining unused keys list
344 int foundIdx = allOrderKeys.indexOf( item );
345 if ( foundIdx > -1 ) {
346 //Add this item to dataOrder list, and remove from unused keys list
347 dataOrder.add( item );
348 allOrderKeys.remove( foundIdx );
349 //If a diving depth term, then remove alternative form
350 if ( item.compareTo(CaveConverter.DATA_ORDER_CAT_DEPTHCHANGE) == 0 ) {
351 //Remove from and to depth items from list of remaining valid terms
352 int altIdx = allOrderKeys.indexOf( CaveConverter.DATA_ORDER_CAT_FROMDEPTH );
353 if ( altIdx > -1 ) {
354 allOrderKeys.remove( altIdx );
355 }
356 altIdx = allOrderKeys.indexOf( CaveConverter.DATA_ORDER_CAT_TODEPTH );
357 if ( altIdx > -1 ) {
358 allOrderKeys.remove( altIdx );
359 }
360 }
361 else if ( item.compareTo(CaveConverter.DATA_ORDER_CAT_FROMDEPTH) == 0 ) {
362 //Remove depth change item from list of remaining valid terms
363 int altIdx = allOrderKeys.indexOf( CaveConverter.DATA_ORDER_CAT_DEPTHCHANGE );
364 if ( altIdx > -1 ) {
365 allOrderKeys.remove( altIdx );
366 }
367 //Check toDepth comes after fromDepth as parser only supports this ordering of depths in diving data
368 altIdx = allOrderKeys.indexOf( CaveConverter.DATA_ORDER_CAT_TODEPTH );
369 if ( altIdx == -1 ) {
370 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported survex diving data order. todepth before fromdepth is not supported.", lineRef ), lineNo );
371 }
372 }
373 }
374 else {
375 //Item not found, so unsupported data normal block
376 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported survex diving data order. Term '" + item + "' is not supported.", lineRef ), lineNo );
377 }
378 }
379 //Add to live series, as primary unless that is already normal,
380 //in which case set as secondary
381 List<String> checkDataOrder = liveSeries.getDataOrder();
382 if ( checkDataOrder.size() > 0
383 && ( UtilityFunctions.dataOrderIsDiving( checkDataOrder ) == false ) ) {
384 liveSeries.setDataOrder2(dataOrder);
385 }
386 else {
387 liveSeries.setDataOrder(dataOrder);
388 }
389 }
390 else if ( data[1].compareToIgnoreCase("NOSURVEY") == 0 ) {
391 state = 1;
392 //Check data order
393 //TODO Add support for nosurvey station format
394 logger.logMessage( "Found nosurvey data header at line " + lineNo + ". Checking format." );
395
396 //Build array of expected categories here, and then loop through items picking off those that match until done.
397 //We will then know if all were found and the format is valid.
398 dataOrder.clear();
399 List<String> allOrderKeys = new ArrayList<String>();
400 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_FROMSTN );
401 allOrderKeys.add( CaveConverter.DATA_ORDER_CAT_TOSTN );
402
403 for (int dataIdx = 2; dataIdx < data.length; dataIdx++) {
404 //Get next item
405 String item = data[dataIdx].toUpperCase();
406 //Look for item in remaining unused keys list
407 int foundIdx = allOrderKeys.indexOf( item );
408 if ( foundIdx > -1 ) {
409 //Add this item to dataOrder list, and remove from unused keys list
410 dataOrder.add( item );
411 allOrderKeys.remove( foundIdx );
412 }
413 else {
414 //Item not found, so unsupported data normal block
415 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported survex nosurvey data order. Term '" + item + "' is not supported.", lineRef ), lineNo );
416 }
417 }
418 //Set nosurvey flag
419 blockProps.noSurvey = true;
420 }
421 else {
422 //Other data settings not currently supported (assumes file use default order)
423 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported survex data command: " + data[1], lineRef ), lineNo );
424 }
425 }
426 else if ( cmd.compareToIgnoreCase("CALIBRATE") == 0 ) {
427 //Process calibration command
428 if (data.length == 3) {
429 String type = data[1];
430 double value = Double.parseDouble( data[2] );
431 if ( type.compareToIgnoreCase("tape") == 0 ) {
432 //Set tape calibration in active series
433 liveSeries.setTapeCalibration(value, liveSeries.getLengthUnit() );
434 }
435 else if ( type.compareToIgnoreCase("declination") == 0 ) {
436 //Set declination calibration in active series
437 liveSeries.setDeclination(value);
438 }
439 else if ( type.compareToIgnoreCase("compass") == 0 ) {
440 //Set compass calibration in active series
441 liveSeries.setCompassCalibration(value, liveSeries.getBearingUnit() );
442 }
443 else if ( type.compareToIgnoreCase("clino") == 0 ) {
444 //Set compass calibration in active series
445 liveSeries.setClinoCalibration(value, liveSeries.getGradientUnit() );
446 }
447 //TODO Add support for calibration scale factors
448 }
449 else {
450 //Invalid calibration lie
451 throw new ParseException( UtilityFunctions.formatFileParserMsg("CALIBRATE command did not contain a single instrument type plus value.", lineRef ), lineNo );
452 }
453 }
454 else if ( cmd.compareToIgnoreCase("DATE") == 0 ) {
455 //Process date
456 if (data.length > 1) {
457 Date value = UtilityFunctions.stringToDate(data[1], "yyyy.MM.dd");
458 liveSeries.setSurveyDate(value);
459 }
460 else {
461 logger.logMessage( "DATE command without further data skipped for line: " + dataLine);
462 }
463 }
464 else if ( cmd.compareToIgnoreCase("UNITS") == 0 ) {
465 //Determine unit being set
466 if ( data.length > 2 ) {
467 //Find unit in line
468 int unitIdx = 0;
469 for (int i = 2; i < data.length; i++) {
470 if ( ( data[i].compareToIgnoreCase("METRES") == 0 )
471 || ( data[i].compareToIgnoreCase("METERS") == 0 )
472 || ( data[i].compareToIgnoreCase("METRIC") == 0 )
473 || ( data[i].compareToIgnoreCase("YARDS") == 0 )
474 || ( data[i].compareToIgnoreCase("FEET") == 0 )
475 || ( data[i].compareToIgnoreCase("DEGS") == 0 )
476 || ( data[i].compareToIgnoreCase("DEGREES") == 0 )
477 || ( data[i].compareToIgnoreCase("GRADS") == 0 )
478 || ( data[i].compareToIgnoreCase("MILS") == 0 )
479 || ( data[i].compareToIgnoreCase("MINUTES") == 0 )
480 || ( data[i].compareToIgnoreCase("PERCENT") == 0 )
481 || ( data[i].compareToIgnoreCase("PERCENTAGE") == 0 ) ) {
482 //Found unit, store index and then look back through items to set for this unit
483 unitIdx = i;
484 break;
485 }
486 }
487
488 if (unitIdx > 0) {
489 for (int i = 1; i < unitIdx; i++) {
490 if ( data[i].compareToIgnoreCase("TAPE") == 0 || data[i].compareToIgnoreCase("LENGTH") == 0 ) {
491 //TAPE/LENGTH, BACKTAPE/BACKLENGTH, COUNTER/COUNT, DEPTH, DX/EASTING, DY/NORTHING, DZ/ALTITUDE in YARDS|FEET|METRIC|METRES|METERS (default: METRES)
492 if ( data[unitIdx].compareToIgnoreCase("METRES") == 0 || data[unitIdx].compareToIgnoreCase("METERS") == 0 || data[unitIdx].compareToIgnoreCase("METRIC") == 0 ) {
493 liveSeries.setLengthUnit( LengthUnit.Metres );
494 }
495 else if ( data[unitIdx].compareToIgnoreCase("FEET") == 0 ) {
496 liveSeries.setLengthUnit( LengthUnit.Feet );
497 }
498 else if ( data[unitIdx].compareToIgnoreCase("YARDS") == 0 ) {
499 liveSeries.setLengthUnit( LengthUnit.Yards );
500 }
501 else {
502 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported length unit '" + data[unitIdx] + "'.", lineRef ), lineNo );
503 }
504 }
505 else if ( data[i].compareToIgnoreCase("DEPTH") == 0 ) {
506 //TAPE/LENGTH, BACKTAPE/BACKLENGTH, COUNTER/COUNT, DEPTH, DX/EASTING, DY/NORTHING, DZ/ALTITUDE in YARDS|FEET|METRIC|METRES|METERS (default: METRES)
507 if ( data[unitIdx].compareToIgnoreCase("METRES") == 0 || data[unitIdx].compareToIgnoreCase("METERS") == 0 || data[unitIdx].compareToIgnoreCase("METRIC") == 0 ) {
508 liveSeries.setDepthUnit( LengthUnit.Metres );
509 }
510 else if ( data[unitIdx].compareToIgnoreCase("FEET") == 0 ) {
511 liveSeries.setDepthUnit( LengthUnit.Feet );
512 }
513 else if ( data[unitIdx].compareToIgnoreCase("YARDS") == 0 ) {
514 liveSeries.setDepthUnit( LengthUnit.Yards );
515 }
516 else {
517 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported depth unit '" + data[unitIdx] + "'.", lineRef ), lineNo );
518 }
519 }
520 else if ( data[i].compareToIgnoreCase("COMPASS") == 0 || data[i].compareToIgnoreCase("BEARING") == 0 ) {
521 //COMPASS/BEARING, BACKCOMPASS/BACKBEARING, DECLINATION in DEG|DEGREES|GRADS|MILS|MINUTES (default: DEGREES)
522 if ( data[unitIdx].compareToIgnoreCase("DEGS") == 0 || data[unitIdx].compareToIgnoreCase("DEGREES") == 0 ) {
523 liveSeries.setBearingUnit( BearingUnit.Degrees );
524 }
525 else if ( data[unitIdx].compareToIgnoreCase("GRADS") == 0 || data[unitIdx].compareToIgnoreCase("MILS") == 0 ) {
526 liveSeries.setBearingUnit( BearingUnit.Grads );
527 }
528 else if ( data[unitIdx].compareToIgnoreCase("MINUTES") == 0 ) {
529 liveSeries.setBearingUnit( BearingUnit.Minutes );
530 }
531 else {
532 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported bearing unit '" + data[unitIdx] + "'.", lineRef ), lineNo );
533 }
534 }
535 else if ( data[i].compareToIgnoreCase("CLINO") == 0 || data[i].compareToIgnoreCase("GRADIENT") == 0 ) {
536 //CLINO/GRADIENT, BACKCLINO/BACKGRADIENT in DEG|DEGREES|GRADS|MILS|PERCENT|PERCENTAGE (default: DEGREES)
537 if ( data[unitIdx].compareToIgnoreCase("DEGS") == 0 || data[unitIdx].compareToIgnoreCase("DEGREES") == 0 ) {
538 liveSeries.setGradientUnit( GradientUnit.Degrees );
539 }
540 else if ( data[unitIdx].compareToIgnoreCase("GRADS") == 0 || data[unitIdx].compareToIgnoreCase("MILS") == 0 ) {
541 liveSeries.setGradientUnit( GradientUnit.Grads );
542 }
543 else if ( data[unitIdx].compareToIgnoreCase("PERCENT") == 0 || data[unitIdx].compareToIgnoreCase("PERCENTAGE") == 0 ) {
544 liveSeries.setGradientUnit( GradientUnit.Percent );
545 }
546 else if ( data[unitIdx].compareToIgnoreCase("MINUTES") == 0 ) {
547 liveSeries.setGradientUnit( GradientUnit.Minutes );
548 }
549 else {
550 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported gradient unit '" + data[unitIdx] + "'.", lineRef ), lineNo );
551 }
552 }
553 else {
554 throw new ParseException( UtilityFunctions.formatFileParserMsg("Unsupported unit type '" + data[i] + "'.", lineRef ), lineNo );
555 }
556 }
557 }
558 else {
559 throw new ParseException( UtilityFunctions.formatFileParserMsg("UNITS command did not contain a category of measurement plus value.", lineRef ), lineNo );
560 }
561
562 }
563
564 }
565 else if ( cmd.compareToIgnoreCase("FLAGS") == 0 ) {
566 //Process flags
567 boolean notPrefixed = false;
568 for (int iFlags = 1; iFlags < data.length; iFlags++) {
569 //Read all flags settings to determine what is being turned on or off
570 if (data[iFlags].compareToIgnoreCase("NOT") == 0 ) {
571 notPrefixed = true;
572 }
573 else if (data[iFlags].compareToIgnoreCase("DUPLICATE") == 0 ) {
574 blockProps.duplicateFlag = (notPrefixed == false);
575 notPrefixed = false;
576 }
577 else if (data[iFlags].compareToIgnoreCase("SPLAY") == 0 ) {
578 blockProps.splayFlag = (notPrefixed == false);
579 notPrefixed = false;
580 }
581 else if (data[iFlags].compareToIgnoreCase("SURFACE") == 0 ) {
582 blockProps.surfaceFlag = (notPrefixed == false);
583 notPrefixed = false;
584 }
585 else {
586 //Reset notPrefixed flag if any other value
587 notPrefixed = false;
588 }
589 }
590 }
591 else {
592 //Ignore other commands inside begin end block
593 //TODO Add support for FIX stations
594 //TODO Add support for ENTRANCE stations
595 //TODO Add support for topofil clino calibration scale factor
596 logger.logMessage("Unsupported Survex command ignored: " + cmd);
597 }
598 }
599 else {
600 //Data line
601 //logger.logMessage("Data line " + CaveConverter.padNumber(lineNo, 4) + ": " + dataLine);
602
603 if ( liveSeries != null ) {
604 //Process line into individual items
605 String[] data = UtilityFunctions.cleanAndSplitDataLine(dataLine);
606
607 switch (state) {
608 case 1:
609 //Create new survey leg
610 SurveyLeg leg = new SurveyLeg();
611 double fromDepth = 0;
612
613 //Create record from the items
614 int index = 0;
615 while ( index < data.length ) {
616 String item = data[index];
617 //Check for end of line
618 if ( item.charAt(0) == ';' ) {
619 //Comments, so ignore rest of line
620 //TODO Add support for leg comments
621 index = data.length;
622 }
623 else {
624 //Check data order was determined
625 if ( dataOrder.size() == 0 ) {
626 //No data order specified, so set to default
627 dataOrder.add( CaveConverter.DATA_ORDER_CAT_FROMSTN );
628 dataOrder.add( CaveConverter.DATA_ORDER_CAT_TOSTN );
629 dataOrder.add( CaveConverter.DATA_ORDER_CAT_LENGTH );
630 dataOrder.add( CaveConverter.DATA_ORDER_CAT_BEARING );
631 dataOrder.add( CaveConverter.DATA_ORDER_CAT_CLINO );
632 }
633 else if ( blockProps.noSurvey == false && dataOrder.size() <5 ) {
634 throw new ParseException( UtilityFunctions.formatFileParserMsg("Last data order command did not contain enough items for a survey leg.", lineRef ), lineNo );
635 }
636 else if ( ( dataOrder.size() < index + 1 )
637 && ( dataOrder.get( dataOrder.size() - 1 ).equals( CaveConverter.DATA_ORDER_CAT_IGNOREALL ) ) == false ) {
638 throw new ParseException( UtilityFunctions.formatFileParserMsg("Last data order command did not contain enough items for data line.", lineRef ), lineNo );
639 }
640 //Put item into appropriate value
641 String itemCat = CaveConverter.DATA_ORDER_CAT_IGNOREALL;
642 if ( index < dataOrder.size() ) {
643 itemCat = dataOrder.get( index );
644 }
645 if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_FROMSTN ) ) {
646 //TODO Add support for retaining station name when not a number
647 leg.setFromStn( UtilityFunctions.createStationFromNameForSeries( data[index], liveSeries ) );
648 //Set nosurvey legs flag for nosurvey leg when setting fromStn
649 if ( blockProps.noSurvey ) {
650 leg.setNosurvey( true );
651 }
652 }
653 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_TOSTN ) ) {
654 //TODO Add support for retaining station name when not a number
655 leg.setToStn( UtilityFunctions.createStationFromNameForSeries( data[index], liveSeries ) );
656 }
657 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_LENGTH ) ) {
658 double length = Double.parseDouble( data[index] );
659 if ( length < 0 ) {
660 logger.logMessage("Warning: Negative leg length (" + data[index] +
661 ") read from Survex file at line " + lineNo + ".");
662 }
663 leg.setLength( length, liveSeries.getLengthUnit() );
664 }
665 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_BEARING ) ) {
666 if ( data[index].compareTo("-") == 0 ) {
667 leg.setCompass( 0, BearingUnit.Degrees );
668 }
669 else {
670 leg.setCompass( Double.valueOf( data[index] ), liveSeries.getBearingUnit() );
671 }
672 }
673 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_CLINO ) ) {
674 //Trim data item after ';' if present
675 String val = data[index];
676 int commentCharPos = val.indexOf(';');
677 if ( commentCharPos > 0 ) {
678 val = val.substring(0, commentCharPos);
679 }
680 double straightDown = -90;
681 double straightUp = 90;
682 double level = 0;
683 if ( val.compareToIgnoreCase("-V") == 0 ) {
684 leg.setClino( straightDown, GradientUnit.Degrees );
685 }
686 else if ( val.compareToIgnoreCase("down") == 0 ) {
687 leg.setClino( straightDown, GradientUnit.Degrees );
688 }
689 else if ( val.compareToIgnoreCase("d") == 0 ) {
690 leg.setClino( straightDown, GradientUnit.Degrees );
691 }
692 else if ( val.compareToIgnoreCase("+V") == 0 ) {
693 leg.setClino( straightUp, GradientUnit.Degrees );
694 }
695 else if ( val.compareToIgnoreCase("up") == 0 ) {
696 leg.setClino( straightUp, GradientUnit.Degrees );
697 }
698 else if ( val.compareToIgnoreCase("u") == 0 ) {
699 leg.setClino( straightUp, GradientUnit.Degrees );
700 }
701 else if ( val.compareToIgnoreCase("-") == 0 ) {
702 leg.setClino( level, GradientUnit.Degrees );
703 }
704 else if ( val.compareToIgnoreCase("level") == 0 ) {
705 leg.setClino( level, GradientUnit.Degrees );
706 }
707 else {
708 leg.setClino( Double.valueOf( val ), liveSeries.getGradientUnit() );
709 }
710 }
711 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_FROMDEPTH ) ) {
712 //Store fromDepth, it will be added to leg when toDepth is read (parser only supports fromDepth before toDepth in data ordering)
713 fromDepth = Double.parseDouble( data[index] );
714 }
715 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_TODEPTH ) ) {
716 double toDepth = Double.parseDouble( data[index] );
717 leg.setDepths( fromDepth, toDepth, liveSeries.getDepthUnit() );
718 }
719 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_DEPTHCHANGE ) ) {
720 double depthChange = Double.parseDouble( data[index] );
721 leg.setDepthChange( depthChange, liveSeries.getDepthUnit() );
722 }
723 else if ( itemCat.equals( CaveConverter.DATA_ORDER_CAT_IGNOREALL ) ) {
724 //TODO Comment on line end?
725 }
726 }
727
728 index++;
729 }
730
731 //Check leg was found
732 if ( blockProps.noSurvey || leg.getLength( LengthUnit.Metres ) > -1 ) {
733 //Set flags for leg
734 leg.setDuplicate(blockProps.duplicateFlag);
735 leg.setSplay(blockProps.splayFlag);
736 leg.setSurface(blockProps.surfaceFlag);
737 //Add leg to series
738 liveSeries.addLeg(leg);
739 }
740 break;
741 case 2:
742 //Add data to LRUD cache
743 /**
744 * TODO Need to store all the LRUD lines in groups to match with
745 * the legs once the series is complete. Need to match legs to lrud
746 * from two consecutive lines to be sure the LRUD is for that leg, and
747 * not another leg from the same station. Create the LRUD group in
748 * the command parsing switch, as that is where we know a new LRUD group has
749 * been started.
750 */
751 break;
752 }
753
754 }
755 else {
756 //Data line outside of series
757 throw new ParseException( UtilityFunctions.formatFileParserMsg("Data line found outside of any begin/end block.", lineRef ), lineNo );
758 }
759
760 }
761 }
762 }
763
764 //Process equates
765 UtilityFunctions.processEquates(equates, allSeries);
766
767 //Debug dump
768 UtilityFunctions.logSurveyDebugData(allSeries, logger);
769
770 //Completed file parsing
771 return allSeries;
772 }
773
774 //Get series names in stack to generate full series name
775 private String fullNestedSeriesName(List<String> stack) {
776 String name = "";
777
778 Iterator<String> stackIterator = stack.listIterator();
779 while ( stackIterator.hasNext() ) {
780 name += "." + stackIterator.next();
781 }
782 //Remove initial dot
783 return name.substring(1);
784 }
785
786 }