Created
June 25, 2014 14:32
-
-
Save kidpixo/db388a0ae8e12c96e1e0 to your computer and use it in GitHub Desktop.
conversion script fro dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/bin/bash | |
# | |
# 1. It assumes th data are in the Demo_Mapping_Data_Dump.txt file | |
# 2. It creates: | |
# - Orbits.txt : contains the orbits numbers and line numbers | |
# - Demo_Mapping_Data_Dump.csv : Demo_Mapping_Data_Dump.txt converted to csv with custom header | |
# - Demo_Mapping_Data_Dump_polygon.csv : added an WKT polygon to Demo_Mapping_Data_Dump.csv | |
# | |
# Orbit file: all lines with containing "Orbit: " | |
# Line Orbit | |
# 15 456 | |
# 160 457 | |
# 229 458 | |
# 316 459 | |
# 395 460 | |
# 492 461 | |
# 599 462 | |
if [ $# -lt 1 ]; then | |
echo "Error: provide at least an output file name as argument" | |
exit 1 | |
fi | |
if [ ! -f "$1" ]; then | |
echo "Error: file \"$1\" doesn't exist." | |
exit 1 | |
fi | |
outfile=$(echo $1 | cut -d "." -f 1) | |
# create the orbits position file | |
echo '1 0 DUMMY ' > Orbits.txt # dummy first line | |
grep -n --colour='never' "Orbit: " $1 | sed -e 's/[:()]/ /g' -e 's/Orbit//g' >> Orbits.txt | |
# print the header | |
echo "orbit;experiment_instrument;seqnr;image_time_UTC;peri_reltime;p1_lat_degrees;p1_long_degrees;p2_lat_degrees;p2_long_degrees;p3_lat_degrees;p3_long_degrees;p4_lat_degrees;p4_long_degrees;type;p5_lat_degrees;p5_long_degrees;p6_lat_degrees;p6_long_degrees;p7_lat_degrees;p7_long_degrees;p8_lat_degrees;p8_long_degrees;duration;altitude;SC_latitude;SC_longitude;SC_target_elev_azimuth_lat;SC_target_elev_azimuth_lon;distance;target_RA_DEC_from_SC_lat;target_RA_DEC_from_SC_lon;reflection;Sun_tgt_elev_azimuth_lat;Sun_tgt_elev_azimuth_lon;tgt_phase_elongation_lat;tgt_phase_elongation_lon;local_time;img_smear;tg_phase;perihelion;attdata | |
" > "$outfile".csv | |
# cycle through the orbit file | |
while read first_line | |
do | |
first=$(echo $first_line | awk -F" " '{print $1}') # extract the first line number | |
second_line=$(sed -n '1,/^'${first}' /!p' Orbits.txt | sed -n 1p) # get the second line | |
if [ -n "${second_line}" ] | |
then | |
orbit=$(echo $first_line | awk -F" " '{print $2}') # extract the second line number | |
orbit_type=$(echo $first_line | awk -F" " '{print $3}') # orbit type | |
second=$(echo $second_line | awk -F" " '{print $1}') # extract the second orbit line number | |
second=$(expr $second - 1) # set the second line to one line before | |
echo 'Actual Line,Subsequent Line, Orbit, Orbit_type :' $first,$second,$orbit,$orbit_type | |
sed -n "$first,$second"p $1 | sed -n -E -e '/^( )+[0-9]/ s/^/'$orbit' '$orbit_type' /p' | sed -E -e 's/( )+/;/g' >> "$outfile".csv | |
# else | |
# echo 'Empty Subsequent Line' | |
fi | |
done < Orbits.txt | |
rm Orbits.txt | |
############################################ | |
# create the csv file with polygons definition | |
# | |
# add polygon to the header | |
sed -n -e '1s/^/polygon;/p' "$outfile".csv > "$outfile"_polygons.csv | |
# get the fileds to build the polygon WKT | |
sed -n -e '1!p' "$outfile".csv | awk -F ';' '{print "POLYGON (("$7,$6","$9,$8","$20,$19","$22,$21","$7,$6"));"$0}' >> "$outfile"_polygons.csv | |
# Polygon WKT : | |
# p1, p2, p7, p8, p1 | |
# $5,$6","$7,$8","$18,$19","$20,$21","$5,$6 | |
# | |
# Demo_Mapping_Data_Dump_polygons.csv fields via csvkit (http://csvkit.readthedocs.org/) | |
# | |
# 5: p1 lat (degrees) | |
# 6: p1 long (degrees) | |
# 7: p2 lat (degrees) | |
# 8: p2 long (degrees) | |
# 9: p3 lat (degrees) | |
# 10: p3 long (degrees) | |
# 11: p4 lat (degrees) | |
# 12: p4 long (degrees) | |
# 13: type | |
# 14: p5 lat (degrees) | |
# 15: p5 long (degrees) | |
# 16: p6 lat (degrees) | |
# 17: p6 long (degrees) | |
# 18: p7 lat (degrees) | |
# 19: p7 long (degrees) | |
# 20: p8 lat (degrees) | |
# 21: p8 long (degrees) | |
# create the appropiate virtual file | |
sed s/dummy/"$outfile"_polygons/g dummy.vrt > "$outfile"_polygons.vrt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment