Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Simple script for calculating and reformatting fixes' coordinates for the FlightGear/X-Plane fix database (fix.dat).
- # Note the script is very sensitive about the source format. Current version supports (to an extent) basic web- and PDF based FIX-lists.
- # Examples can be seen here: http://eaip.eans.ee/2012-03-08/html/eAIP/EE-ENR-4.4-en-GB.html#ENR-4.4 and http://www.lfv.se/AIP/ENR/ENR%204/ES_ENR_4_4_en.pdf
- #
- # If you encounter any kind of incorrect functionality, please contact me.
- #
- # Simply copy the data from the web or PDF file into the source file (defined below) and run the script. Result is a destination file (also defined below).
- #
- # The online web-based FIX list assumes the designators and coordinates are all on a separate lines (it should copy that way from the web browser by default).
- # The PDF-based FIX list on the other hand assumes the designator is followed by coordinates on the same line. Same line may also contain other designators and coordinates.
- #
- #
- # The source file contents from web-based fix list should look something like this (without the #-signs at the beginning of course):
- #------------------------------------------------------------------------------------
- #ABSAK <<----- the source file shall begin with the first FIX designator.
- #
- #581843N
- #
- #0270210E
- # EETU IFR PROC <<----- note the lines in between the designators and coordinates can contain anything.
- #ANAMA
- #
- #582459N
- #
- #0242842E
- # M857, P607, UM857, UP607 (and so on)
- #------------------------------------------------------------------------------------
- # ... and from a PDF list something like this:
- #------------------------------------------------------------------------------------
- #ABDOB 49°54.00′N 006°34.90′E IAP ETAD ABGAS 48°35.62′N 010°23.50′E TB6 ADAMS 49°47.89′N 011°56.00′E IAP ETIC <<----- still the fist line has to start with a FIX designator
- #ALOPO 50°16.82′N 011°46.08′E TL10
- #BADLI 49°03.82′N 008°26.85′E SID ETAD
- #BADOB 54°06.10′N 011°56.93′E IAP ETNL
- #BADSU 53°59.22′N 012°39.17′E IAP ETNL
- #BANIM 49°43.10′N 008°36.53′E TB6, TL8
- #AMT FÜR FLUGSICHERUNG DER BUNDESWEHR <<----- These random lines inbetween are not a problem
- #AMDT 02/12
- #ENR 4.4 - 2
- #28 JUL 2011
- #MILITÄRISCHES LUFTFAHRTHANDBUCH DEUTSCHLAND
- #MILITARY AERONAUTICAL INFORMATION PUBLICATION GERMANY
- #Name Koordinaten Zweck
- #Name Coordinates Purpose
- #1 2 3
- #LAGIP 53°49.48′N 011°53.88′E IAP ETNL
- #------------------------------------------------------------------------------------
- # Note the script may make mistakes, since it assumes the fix name could consist of two separate words (not mentioning the fact it's not well written by no means :) ).
- # It also might not identify the coordinates or even misinterpret them.
- #
- # Don't forget to make this file executable!
- #
- # PS the FIX tables are located in AIPs under ENROUTE submenu (paragraph 4.4)
- #
- # Written by Laur Aliste <laur.aliste ät gmail>
- #------------------------------------------------------------------------------------
- #------------------------------------------------------------------------------------
- # Define the name and location of the source file:
- source_to_copy_FIX_data_to="$HOME/Downloads/FG_data/fix_source"
- # Define the name and location of the destination file:
- dest="$HOME/Downloads/FG_data/fix_final"
- #######################################################################################
- ######## Don't edit anything from here on if you're not sure what you're doing ########
- #######################################################################################
- source="/tmp/.fix_reformatter_source"
- source_intermediate="/tmp/.fix_reformatter_intermediate"
- clear
- count=0
- cp $source_to_copy_FIX_data_to $source
- echo "DUMMY" >> $source
- # Delete empty lines:
- sed '/^$/d' $source > $source_intermediate
- # Delete lines starting with space or a tab:
- sed '/^[ \t]\+/d' $source_intermediate > $source
- # Define the source format:
- set $(cat $source | sed -n 1p)
- third_arg_digits=$(echo $3 | sed "s/[^0-9]//g")
- if [[ "$1" =~ ^[A-Z\ ]+$ ]] && [[ "${#1}" =~ ^[4-9]+$ ]] && [[ -z "$third_arg_digits" ]]; then
- file_format="online_AIP"
- echo "Assuming we're dealing here with a FIX list taken from a an online AIP having a classic web-based AIP format."
- # Delete lines containing "," or ":"
- sed -e '/'[:,]'/d' < $source > $source_intermediate
- mv $source_intermediate $source
- elif [[ "$1" =~ ^[A-Z\ ]+$ ]] && [[ "${#third_arg_digits}" =~ ^[6-9]+$ ]]; then
- file_format="PDF_source"
- echo "Assuming we're dealing here with a FIX list taken from a PDF file (or similar)."
- else
- echo "========================================================"
- echo " It seems the source format provided isn't supported"
- echo " by this script. Read the script header for more info."
- echo "========================================================"; exit 1
- fi
- > $source_intermediate
- # Organise the source file for the calculation script:
- if [[ "$file_format" == "online_AIP" ]]; then
- while [ $count -lt 40 ] ;do
- count=0
- designator=$(echo $(cat $source | sed -n 1p))
- until [[ "$designator" =~ ^[A-Z\ ]+$ ]] && [[ "${#designator}" =~ ^[4-9]+$ ]]; do
- if [ $count -le 40 ]; then
- sed -i 1d $source
- designator=$(echo $(cat $source | sed -n 1p))
- let count=$count+1
- else
- if [ -z "$(cat $source | sed -n 1p)" ]; then
- designator="DUMMY"
- fi
- fi
- done
- sed -i 1d $source
- latitude=$(cat $source | sed -n 1p)
- latitude_digits=$(echo $latitude | sed "s/[^0-9]//g")
- until [[ "$latitude_digits" =~ ^[0-9]+$ ]] && [[ "${#latitude_digits}" =~ ^[6-8]+$ ]] ; do
- if [ $count -le 40 ]; then
- sed -i 1d $source
- latitude=$(cat $source | sed -n 1p)
- latitude_digits=$(echo $latitude | sed "s/[^0-9]//g")
- let count=$count+1
- else
- if [ -z "$(cat $source | sed -n 1p)" ]; then
- latitude_digits="0000000"
- fi
- fi
- done
- sed -i 1d $source
- longitude=$(cat $source | sed -n 1p)
- longitude_digits=$(echo $longitude | sed "s/[^0-9]//g")
- until [[ $longitude_digits =~ ^[0-9]+$ ]] && [[ ${#longitude_digits} =~ ^[7-9]+$ ]]; do
- if [ $count -le 40 ]; then
- sed -i 1d $source
- longitude=$(cat $source | sed -n 1p)
- longitude_digits=$(echo $longitude | sed "s/[^0-9]//g")
- let count=$count+1
- else
- if [ -z "$(cat $source | sed -n 1p)" ]; then
- longitude_digits="00000000"
- fi
- fi
- done
- sed -i 1d $source
- echo "$designator" >> $source_intermediate
- echo "$latitude" >> $source_intermediate
- echo "$longitude" >> $source_intermediate
- done
- elif [[ "$file_format" == "PDF_source" ]]; then
- set $(cat $source | sed -n 1p)
- str="$@"
- until [[ "$str" == "DUMMY" ]]; do
- until [[ "${#str}" == "1" ]]; do
- set $str
- third_variable_digits=$(echo $3 | sed "s/[^0-9]//g")
- if [[ "$1" =~ ^[A-Z]+$ ]] && [[ "$third_variable_digits" =~ ^[0-9]+$ ]] && [ ${#third_variable_digits} -ge 6 ]; then
- second_variable_digits=$(echo $2 | sed "s/[^0-9]//g")
- if [[ "$2" =~ ^[A-Z]+$ ]]; then
- designator=$(echo "$1 $2")
- latitude=$3
- longitude=$4
- remove="$1 $2"
- elif ! [[ "${#third_variable_digits}" =~ ^[6-9]+$ ]]; then
- echo "======================================================"
- echo " An error has occurred. Sorry."
- echo "======================================================"; exit 1
- else
- designator=$1
- latitude=$2
- longitude=$3
- remove="$1"
- fi
- echo "$designator" >> $source_intermediate
- echo "$latitude" >> $source_intermediate
- echo "$longitude" >> $source_intermediate
- else
- remove="$1"
- fi
- str=" $@ "
- for remove_designators_from_the_beginning in $remove ; do
- str=${str/ $remove_designators_from_the_beginning / }
- done
- done
- sed -i 1d $source
- set $(cat $source | sed -n 1p)
- str="$@"
- done
- # Write 'DUMMY' to the end of the source. It'll mark the end of the math loop:
- echo "DUMMY" >> $source_intermediate
- fi
- mv $source_intermediate $source
- # Wipe the old destination file:
- > $dest
- designator=$(cat $source | sed -n 1p)
- # Start the math loop:
- while : ;do
- latitude_line=$(cat $source | sed -n 2p)
- # Trim preceeding and succeeding tabs:
- trim() { echo $latitude_line; }
- latitude_line="$(trim '$latitude_line')"
- # Extract the number of items and number of digits in coordinate value in order to identify it:
- latitude_digits=$(echo "$latitude_line" | sed "s/[^0-9]//g")
- nr_of_latitude_digits=${#latitude_digits}
- nr_of_latitude_items=${#latitude_line}
- longitude_line=$(cat $source | sed -n 3p)
- trim2() { echo $longitude_line; }
- longitude_line="$(trim2 '$longitude_line')"
- longitude_digits=$(echo "$longitude_line" | sed "s/[^0-9]//g")
- # Identify coordinate system by examining the latitude coordinate:
- if [[ "$nr_of_latitude_items" == "7" ]] && [[ "$nr_of_latitude_digits" == "6" ]]; then
- format="HHMMSS"
- elif [[ "$nr_of_latitude_items" == "9" ]] && [[ "$nr_of_latitude_digits" == "7" ]] && [[ "$(echo $latitude_line | cut -c7)" == "." ]]; then
- format="HHMMSS.S"
- elif [[ "$nr_of_latitude_items" == "10" ]] && [[ "$nr_of_latitude_digits" == "8" ]] && [[ "$(echo $latitude_line | cut -c7)" == "." ]]; then
- format="HHMMSS.SS"
- elif [[ "$nr_of_latitude_items" == "10" ]] && [[ "$nr_of_latitude_digits" == "6" ]] && [[ "$(echo $latitude_line | cut -c7)" == "." ]]; then
- format="HH°MM.MM′"
- elif [[ "$nr_of_latitude_items" == "13" ]] && [[ "$nr_of_latitude_digits" == "8" ]]; then
- format="HH°MM'SS.SS′′"
- elif [[ "$nr_of_latitude_items" == "10" ]] && [[ "$nr_of_latitude_digits" == "6" ]]; then
- format="HH°MM'SS′′"
- else
- echo "======================================================"
- echo "The coordinate system could not be identified or isn't"
- echo "supported by this script. Sorry."
- echo -e "Unidentified coordinate is: $latitude_line\n"
- echo -e "If the coordinate above doesn't look anything like"
- echo -e "a coordinate should, then the script has misinterpreted"
- echo -e "the source-file. I'd reccommend you removing or editing"
- echo -e "corresponding line from the source file and try again."
- echo "======================================================"; exit 1
- fi
- count=0
- # Define latitude values (hr, min):
- hr_lat="$(( 10#$(echo $latitude_digits | cut -c1-2) ))"
- min_lat=$(echo $latitude_digits | cut -c3-4)
- # Define longitude values (hr, min):
- hr_lon="$(( 10#$(echo $longitude_digits | cut -c1-3) ))"
- min_lon=$(echo $longitude_digits | cut -c4-5)
- # Define hemispheres:
- lat_hemisphere=$(echo "$latitude_line" | sed "s/[^NS]//g")
- lon_hemisphere=$(echo "$longitude_line" | sed "s/[^WE]//g")
- case "$format" in
- "HHMMSS") # Define latitude values (sec):
- sec_lat=$(echo $latitude_digits | cut -c5-6)
- # Define longitude values (sec):
- sec_lon=$(echo $longitude_digits | cut -c6-7)
- ;;
- "HHMMSS.SS") # Define latitude values (sec):
- sec_lat=$(echo $latitude_digits | cut -c5-6).$(echo $latitude_digits | cut -c7-8)
- # Define longitude values (sec):
- sec_lon=$(echo $longitude_digits | cut -c6-7).$(echo $longitude_digits | cut -c8-9)
- ;;
- "HHMMSS.S") # Define latitude values (sec):
- sec_lat=$(echo $latitude_digits | cut -c5-6).$(echo $latitude_digits | cut -c7)
- # Define longitude values (sec):
- sec_lon=$(echo $longitude_digits | cut -c6-7).$(echo $longitude_digits | cut -c8)
- ;;
- "HH°MM.MM′") # Define latitude values (min, sec):
- min_lat=$(echo $latitude_digits | cut -c3-4).$(echo $latitude_digits | cut -c5-6)
- sec_lat="0"
- # Define longitude values (min, sec):
- min_lon=$(echo $longitude_digits | cut -c4-5).$(echo $latitude_digits | cut -c6-7)
- sec_lon="0"
- ;;
- "HH°MM'SS.SS′′") # Define latitude values (sec):
- sec_lat=$(echo $latitude_digits | cut -c5-6).$(echo $latitude_digits | cut -c7-8)
- # Define longitude values (sec):
- sec_lon=$(echo $longitude_digits | cut -c6-7).$(echo $longitude_digits | cut -c8-9)
- ;;
- "HH°MM'SS′′") # Define latitude values (sec):
- sec_lat=$(echo $latitude_digits | cut -c5-6)
- # Define longitude values (sec):
- sec_lon=$(echo $longitude_digits | cut -c6-7)
- ;;
- *) echo "========================================================"
- echo " It looks like the coordinates provided aren't supported"
- echo -e " by this script :(\n"
- echo " The coordinates in the source file can't be read by the"
- echo " script. Check the destination file and look what is the"
- echo " last fix finished. Then find it in the source file and"
- echo " see whether the problem can be fixed manually."
- echo "========================================================"; exit 1
- ;;
- esac
- # Define the number of digits in the hour value of coordinate (if it originally contained 0 or 00 in the beginning, it gets removed in the calculations. This variable is later used for restoring these zeroes:
- nr_of_digits_in_long=${#hr_lon}
- nr_of_digits_in_lat=${#hr_lat}
- # Calculate and define the latitude coordinate:
- lat_value=$(echo "scale=6; ($sec_lat/60 + $min_lat)/60 + $hr_lat" | bc )
- # If the latitude coordinate had initially 0 or 00 as a first digit, restore them in front of the result (the 0 was previously removed in calculations):
- if [[ "$nr_of_digits_in_lat" == "1" ]] && [[ "$hr_lat" == "0" ]]; then
- lat_value="00$lat_value"
- elif [[ "$nr_of_digits_in_lat" == "1" ]]; then
- lat_value="0$lat_value"
- fi
- # Make the latitude value negative, if it's located on southern hemisphere:
- if [[ "$lat_hemisphere" == "S" ]]; then
- lat_value="-$lat_value"
- else
- lat_value=" $lat_value"
- fi
- # Calculate and define the longitude coordinate:
- long_value=$(echo "scale=6; ($sec_lon/60 + $min_lon)/60 + $hr_lon" | bc )
- # If the longitude coordinate had initially 0, 00 or 000 as a first digit, restore them in front of the result (the 0 was previously removed in calculations):
- if [[ "$nr_of_digits_in_long" == "1" ]] && [[ "$hr_lon" == "0" ]]; then
- long_value="000$long_value"
- elif [[ "$nr_of_digits_in_long" == "1" ]]; then
- long_value="00$long_value"
- elif [[ "$nr_of_digits_in_long" == "2" ]]; then
- long_value="0$long_value"
- fi
- # Make the longitude value negative, if it's western longitude:
- if [[ "$lon_hemisphere" == "W" ]]; then
- long_value=" -$long_value"
- else
- long_value=" $long_value"
- fi
- # Write the final result into destination file:
- echo "$lat_value$long_value $designator" >> $dest
- sed -i 1d $source
- designator=$(cat $source | sed -n 1p)
- # Start deleting loop (it deletes first lines until new FIX is encountered; then it'll be processed again and so on...):
- until [[ "$designator" =~ ^[A-Z\ ]+$ ]] && [[ "${#designator}" =~ ^[4-9]+$ ]] && [[ "$designator" != "DUMMY" ]]; do
- # If 'DUMMY' is encountered in the source file, the loop gets closed:
- if [[ "$designator" == "DUMMY" ]]; then
- rm $source
- echo "======================================================"
- echo " List has been successfully finished :)"
- echo -e " (or at least it looks that way)\n"
- echo " Result was saved into $dest"
- echo "==============================================================="; exit 0
- elif [ $count -lt 5 ]; then
- sed -i 1d $source
- designator=$(cat $source | sed -n 1p)
- let count=$count+1
- else
- echo "======================================================"
- echo " An error has occurred :("
- echo " Look into source and/or destination file and see"
- echo " whether the problem can be fixed manually."
- echo "======================================================"; exit 1
- fi
- done
- done
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement