#
# Lowrance USR
# ============
#
# Binary data format, and slightly lossy because of the math to
# convert lat/long.
#
# File Name		  	File Contents
# ===============		========================
# lowrance.usr			USR version 2 format file.  Generated by GPSBabel from geocaching.loc input..
#				Used to validate that generation of a USR version 2 format file
#				is still consistent with previous releases of GPSBabel.
#				Contains 9 waypoints, 0 routes, 0 event marker icons, and 0 trails.
#
# lowrance-all.usr		USR verson 2 format file.  Used as input to validate generation of GPX file.
#				Also used to validate the ignoreicons option which ignores event marker icons.
#				Contains 3 waypoints, 1 route with 2 legs, 2 event marker icons and 3 trails.
#				Trail 'Trail 1' conains 0 points, Trail 'Bull Run' contains 97 points in a
#				single section, Trail 'Hike' contains 198 points in a single section.
#
# lowrance-enchilada.usr	USR version 2 format file.
#				Used to validate that GPX file generated from lowrance-all.usr
#				when converted back to USR v2 matches previous executions of GPSBabel.
#
# lowrance-ignoreicons.usr	USR version 2 format file.
#				Used to validate use of ignoreicons option to strip out Event Marker Icons.
#
# lowrance-v2.usr		USR version 2 format file.  Generated on an unknown Lowrance unit.
#				Contains 67 waypoints, 0 routes, 2 event marker icons, and 2 trails.
#				Each trail is named 'Trail 1'.
#				The first 'Trail 1' has 2000 points boken into 10 sections with 200 points
#				in each section.  The second 'Trail 1' has 1258 points broken into 7 sections,
#				with 200 points in 6 and 58 in the last.
#
# lowrance-v2-unicsv.txt	Comma seperated file generated from lowrance-v2.usr by GPSBabel.
#				Used to validate that the parsing of the USR version 2 file yields the same
#				results with this version as was captured previously.
#
# lowrance-v3.usr		USR version 3 format file.  Generated on the same Lowrance unit as lowrance-v2.usr.
#				Contains the same basic data as lowrance-v3.usr.
#
# lowrance-v3-unicsv.txt	Comma seperated file generated from lowrance-v3.usr by GPSBabel.
#				Used to validate that the parsing of the USR version 3 file yields the same
#				results with this version as was captured previously.
#
# lowrance-v4.usr		USR version 4 format file.  Generated on a Lowrance Hook 2.
#				Contains 92 waypoints, 5 routes, and 8 trails.
#
# lowrance-v4.gpx		Reference GPX version of the above USR file.
#
# lowrance-v5.usr		USR version 5 format file.  Generated on a Lowrance Hook 2.
#				Same data as lowrance-v4.usr.
#				Contains 92 waypoints, 5 routes, and 8 trails.
#
# lowrance-v5.gpx		Reference GPX version of the above USR file.  Also used to validate GPX output
#				from USR version 6 file.
#
# lowrance-v6.usr		USR version 6 format file.  Generated on a Lowrance Hook 2.
#				Same data as lowrance-v4.usr.
#				Contains 92 waypoints, 5 routes, and 8 trails.
#
#
# Consolidated all USR testing into a single file with the consolidation
# of support for all USR formats (2/3/4/5/6) with a single processing file
#
# Perform ${TMPDIR} cleanup
rm -f ${TMPDIR}/lowrance*

# Test ability to read non-Lowrance format and generate USR v2 file
gpsbabel -i geo -f ${REFERENCE}/geocaching.loc -o lowranceusr,wversion=2 -F ${TMPDIR}/lowrance1.usr
bincompare ${REFERENCE}/lowrance.usr ${TMPDIR}/lowrance1.usr

# Use the file just created to see if can write back the same data
gpsbabel -i lowranceusr -f ${TMPDIR}/lowrance1.usr -o lowranceusr,wversion=2 -F ${TMPDIR}/lowrance2.usr
# At a minimum unknown altitude not making the round trip, actual test validation commented out
# bincompare ${REFERENCE}/lowrance.usr ${TMPDIR}/lowrance2.usr

# Test ability to generate GPX from USR v2 and then generate USR v2 using that newly created GPX file
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-all.usr -o gpx,elevprec=6 -F ${TMPDIR}/lowrance-enchilada.gpx
gpsbabel -i gpx -f ${TMPDIR}/lowrance-enchilada.gpx -o lowranceusr,wversion=2 -F ${TMPDIR}/lowrance-enchilada.usr
bincompare ${REFERENCE}/lowrance-enchilada.usr ${TMPDIR}/lowrance-enchilada.usr

# Don't convert icons as waypts
gpsbabel -i lowranceusr,ignoreicons -f ${REFERENCE}/lowrance-all.usr -o gpx,elevprec=6 -F ${TMPDIR}/lowrance-enchilada.gpx
gpsbabel -i gpx -f ${TMPDIR}/lowrance-enchilada.gpx -o lowranceusr,wversion=2 -F ${TMPDIR}/lowrance-ignoreicons.usr
bincompare ${REFERENCE}/lowrance-ignoreicons.usr ${TMPDIR}/lowrance-ignoreicons.usr

#
# Another variation of Lowrance.  Compare v2 and v3.  These reference
# files were saved on the same units as v2 and v3.
#
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v2.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v2-unicsv.txt
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v3.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v3-unicsv.txt
# Compare generated to reference
compare ${REFERENCE}/lowrance-v2-unicsv.txt ${TMPDIR}/lowrance-v2-unicsv.txt
# Compare generated to reference
compare ${REFERENCE}/lowrance-v3-unicsv.txt ${TMPDIR}/lowrance-v3-unicsv.txt
# cut the depth column from V3 and then compare the resultant v3 with v2
tail -n +2 ${TMPDIR}/lowrance-v2-unicsv.txt | sed "s/[0-9]*,//" | sort -t , -k 3 > ${TMPDIR}/lowrance-v2-unicsv-sorted.txt
tail -n +2 ${TMPDIR}/lowrance-v3-unicsv.txt | sed "s/[0-9]*,//" | sort -t , -k 3 | cut -d , -f 1,2,3,4,6,7 > ${TMPDIR}/lowrance-v3-unicsv-nodepth.txt
compare ${TMPDIR}/lowrance-v2-unicsv-sorted.txt ${TMPDIR}/lowrance-v3-unicsv-nodepth.txt

# Ideally, there'd be a test for v2 vs. v3 writes, but their numeric
# instability makes this icky.

#
# ${REFERENCE}/lowrance-hook2-v2.usr, ${REFERENCE}/lowrance-hook2-v3.usr,
# ${REFERENCE}/lowrance-hook2-v4.usr, ${REFERENCE}/lowrance-hook2-v5.usr, ${REFERENCE}/lowrance-hook2-v6.usr
# AND ${REFERENCE}/lowrance-hook2.gpx were generated on a single Lowrance Hook2 System.
# This system has the ability to select any one of those formats for database export.
# All files should contain the same basic data with increasing amounts of detail as you
# move from USR v2 => USR v3 => USR v4 => USR v5 => USR v6
#
# Perform USR version 4 test
#         -------------
#
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v4.usr -o gpx -F ${TMPDIR}/lowrance-v4.gpx
compare ${REFERENCE}/lowrance-v4.gpx ${TMPDIR}/lowrance-v4.gpx

gpsbabel -i gpx -f ${TMPDIR}/lowrance-v4.gpx -o lowranceusr,wversion=4 -F ${TMPDIR}/lowrance-v4.usr
gpsbabel -i lowranceusr -f ${TMPDIR}/lowrance-v4.usr -o gpx -F ${TMPDIR}/lowrance-v4~usr.gpx
compare ${TMPDIR}/lowrance-v4.gpx ${TMPDIR}/lowrance-v4~usr.gpx

#
# Perform USR version 5 test
#         -------------
#
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v5.usr -o gpx -F ${TMPDIR}/lowrance-v5.gpx
compare ${REFERENCE}/lowrance-v5.gpx ${TMPDIR}/lowrance-v5.gpx

# Should have generated the same GPX file as USR version 4 but the order is different for some reason
# so use another way to compare to ensure have EXACTLY the same data
#
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v4.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v4-unicsv.txt
tail -n +2 ${TMPDIR}/lowrance-v4-unicsv.txt | sed "s/[0-9]*,//" | sort -t , -k 3 > ${TMPDIR}/lowrance-v4-unicsv-sorted.txt
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v5.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v5-unicsv.txt
tail -n +2 ${TMPDIR}/lowrance-v5-unicsv.txt | sed "s/[0-9]*,//" | sort -t , -k 3 > ${TMPDIR}/lowrance-v5-unicsv-sorted.txt
compare ${TMPDIR}/lowrance-v4-unicsv-sorted.txt ${TMPDIR}/lowrance-v5-unicsv-sorted.txt

#
# Perform USR version 6 test
#         -------------
#
# Should generate the SAME GPX file as USR version 5
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v6.usr -o gpx -F ${TMPDIR}/lowrance-v6.gpx
compare ${REFERENCE}/lowrance-v5.gpx ${TMPDIR}/lowrance-v6.gpx

# Should have generated the same GPX file as USR version 4 but the order is different for some reason
# so use another way to compare to ensure have EXACTLY the same data
#
gpsbabel -i lowranceusr -f ${REFERENCE}/lowrance-v6.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v6-unicsv.txt
tail -n +2 ${TMPDIR}/lowrance-v6-unicsv.txt | sed "s/[0-9]*,//" | sort -t , -k 3 > ${TMPDIR}/lowrance-v6-unicsv-sorted.txt
compare ${TMPDIR}/lowrance-v4-unicsv-sorted.txt ${TMPDIR}/lowrance-v6-unicsv-sorted.txt

