| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class CompassParser { |
| 45 |
private Logger logger; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public CompassParser( Logger logger ) { |
| 52 |
super(); |
| 53 |
this.logger = logger; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public CaveSurvey parseFile( List<String> surveyFileData ) throws ParseException { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
int state = 0; |
| 73 |
|
| 74 |
|
| 75 |
CaveSurvey allSeries = new CaveSurvey(logger); |
| 76 |
|
| 77 |
|
| 78 |
SurveySeries liveSeries = null; |
| 79 |
|
| 80 |
|
| 81 |
boolean duplicateFlag = false; |
| 82 |
boolean ignoreLeg = false; |
| 83 |
boolean splayFlag = false; |
| 84 |
boolean surfaceFlag = false; |
| 85 |
boolean backBearings = false; |
| 86 |
|
| 87 |
|
| 88 |
for ( int i=0; i < surveyFileData.size(); i++ ) { |
| 89 |
int lineNo = i + 1; |
| 90 |
String dataLine = surveyFileData.get(i); |
| 91 |
|
| 92 |
|
| 93 |
if (dataLine.equalsIgnoreCase("\f") == false ) { |
| 94 |
dataLine = dataLine.trim(); |
| 95 |
} |
| 96 |
|
| 97 |
if ( dataLine.length() > 0 ) { |
| 98 |
switch (state) { |
| 99 |
case 0: |
| 100 |
|
| 101 |
String caveName = dataLine.replace(' ', '_'); |
| 102 |
|
| 103 |
liveSeries = new SurveySeries( caveName ); |
| 104 |
|
| 105 |
allSeries.add( liveSeries ); |
| 106 |
state++; |
| 107 |
break; |
| 108 |
case 1: |
| 109 |
|
| 110 |
caveName = dataLine.replace(' ', '_'); |
| 111 |
if ( caveName.compareTo( allSeries.get(0).getSeriesName() ) == 0 ) { |
| 112 |
|
| 113 |
} |
| 114 |
else if ( dataLine.substring(0, 12).compareTo( "SURVEY NAME:" ) == 0 ) { |
| 115 |
|
| 116 |
String seriesName = dataLine.substring(12).trim(); |
| 117 |
liveSeries = new SurveySeries( seriesName ); |
| 118 |
|
| 119 |
|
| 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 |
|
| 126 |
int dateEnd = commentPos; |
| 127 |
if ( commentPos == -1 ) { |
| 128 |
|
| 129 |
commentPos = surveyDataComment.length(); |
| 130 |
dateEnd = commentPos; |
| 131 |
} |
| 132 |
else { |
| 133 |
|
| 134 |
commentPos = commentPos + 8; |
| 135 |
} |
| 136 |
if ( commentPos > 18 ) { |
| 137 |
|
| 138 |
String dateString = surveyDataComment.substring(12,dateEnd).trim(); |
| 139 |
String comment = surveyDataComment.substring( commentPos ).trim(); |
| 140 |
|
| 141 |
Date value = UtilityFunctions.stringToDate(dateString, "M d yy"); |
| 142 |
|
| 143 |
liveSeries.setSurveyDate(value); |
| 144 |
liveSeries.setComment(comment); |
| 145 |
|
| 146 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 202 |
if ( formatPos > 12 ) { |
| 203 |
declinationEndPos = formatPos; |
| 204 |
String formatCode = dataLine.substring(formatPos+7,formatEndPos).trim(); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
String declination = dataLine.substring(12,declinationEndPos).trim(); |
| 211 |
liveSeries.setDeclination( - Double.parseDouble( declination ) ); |
| 212 |
|
| 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 |
|
| 221 |
if ( corrections2Pos > 12 ) { |
| 222 |
String corrections2 = dataLine.substring(corrections2Pos+13,corrections2EndPos).trim(); |
| 223 |
String[] data = UtilityFunctions.cleanAndSplitDataLine(corrections2); |
| 224 |
|
| 225 |
} |
| 226 |
|
| 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 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 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 |
|
| 249 |
backBearings = true; |
| 250 |
|
| 251 |
state++; |
| 252 |
} |
| 253 |
else if ( ( headerData[9].compareTo("FLAGS") == 0 ) |
| 254 |
&& ( headerData[10].compareTo("COMMENTS") == 0 ) ) { |
| 255 |
|
| 256 |
backBearings = false; |
| 257 |
|
| 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 |
|
| 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 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
if (dataLine.equalsIgnoreCase("\f") ) { |
| 282 |
|
| 283 |
allSeries.get(0).addSeries( liveSeries ); |
| 284 |
state = 1; |
| 285 |
} |
| 286 |
else { |
| 287 |
|
| 288 |
String[] data = UtilityFunctions.cleanAndSplitDataLine(dataLine); |
| 289 |
SurveyLeg leg = new SurveyLeg(); |
| 290 |
|
| 291 |
|
| 292 |
int index = 0; |
| 293 |
while ( index < data.length ) { |
| 294 |
String item = data[index]; |
| 295 |
|
| 296 |
|
| 297 |
switch ( index ) { |
| 298 |
case 0: |
| 299 |
|
| 300 |
leg.setFromStn( UtilityFunctions.createStationFromNameForSeries( item, liveSeries ) ); |
| 301 |
break; |
| 302 |
case 1: |
| 303 |
|
| 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 |
|
| 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 |
|
| 357 |
|
| 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 |
|
| 376 |
String backBearing = item; |
| 377 |
} |
| 378 |
break; |
| 379 |
case 10: |
| 380 |
if ( backBearings ) { |
| 381 |
|
| 382 |
String backClino = item; |
| 383 |
} |
| 384 |
break; |
| 385 |
} |
| 386 |
index++; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
String commentFlagsData = ""; |
| 392 |
|
| 393 |
|
| 394 |
duplicateFlag = false; |
| 395 |
ignoreLeg = false; |
| 396 |
splayFlag = false; |
| 397 |
surfaceFlag = false; |
| 398 |
|
| 399 |
|
| 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 |
|
| 419 |
String comment = commentFlagsData; |
| 420 |
int commentStartPos = 0; |
| 421 |
if ( commentFlagsData.startsWith("#|") ) { |
| 422 |
commentStartPos = commentFlagsData.indexOf("#", 2) + 1; |
| 423 |
} |
| 424 |
if ( commentStartPos > 3 ) { |
| 425 |
|
| 426 |
String flags = commentFlagsData.substring(2, commentStartPos - 1); |
| 427 |
|
| 428 |
|
| 429 |
if ( flags.indexOf("L") > -1 ) { |
| 430 |
duplicateFlag = true; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
if ( flags.indexOf("X") > -1 ) { |
| 435 |
ignoreLeg = true; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
comment = commentFlagsData.substring(commentStartPos).trim(); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
leg.setComment(comment); |
| 446 |
|
| 447 |
|
| 448 |
if ( leg.getLength(LengthUnit.Metres) > -1 ) { |
| 449 |
|
| 450 |
leg.setDuplicate(duplicateFlag); |
| 451 |
leg.setSplay(splayFlag); |
| 452 |
leg.setSurface(surfaceFlag); |
| 453 |
|
| 454 |
if ( ignoreLeg == false ) { |
| 455 |
|
| 456 |
liveSeries.addLeg(leg); |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
break; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
List<Equate> equates = new ArrayList<Equate>(); |
| 467 |
|
| 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 |
|
| 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 |
|
| 486 |
for ( int idx2 = idx1 + 1; idx2 < allSeries.get(0).getInnerSeriesList().size(); idx2++ ) { |
| 487 |
SurveySeries series2 = allSeries.get(0).getInnerSeries(idx2); |
| 488 |
|
| 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 |
|
| 500 |
if ( stn1Name.compareTo(stn2Name) == 0 ) { |
| 501 |
|
| 502 |
String series1Name = allSeries.get(0).getSeriesName() + "." + series.getSeriesName(); |
| 503 |
String series2Name = allSeries.get(0).getSeriesName() + "." + series2.getSeriesName(); |
| 504 |
|
| 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 |
|
| 524 |
Equate newEquate = new Equate( series1Name, stn1Name, series2Name, stn2Name); |
| 525 |
equates.add(newEquate); |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
UtilityFunctions.processEquates(equates, allSeries); |
| 538 |
|
| 539 |
|
| 540 |
UtilityFunctions.logSurveyDebugData(allSeries, logger); |
| 541 |
|
| 542 |
|
| 543 |
return allSeries; |
| 544 |
} |
| 545 |
|
| 546 |
} |