#!/bin/bash

#You should currently be in a fresh directory that has all files for only one water basin downloaded from Yan's ftp server
#e.g. the only files here are 
#NHN_08HE0X1_1_0_HN_NLFLOW_1.0.xml
#NHN_08HE0X1_1_0_HN_NLFLOW_1.1.xml
#and NHN_08HE0X1_1_0_HN_NLFLOW_1.2.xml

#The final merge command should appear similar to (but without \ line endings):
#~/osmosis-0.35/bin/osmosis \
#--read-xml-0.5 enableDateParsing=no file="NHN_08HE0X1_1_0_HN_NLFLOW_1.0.xml" --migrate --sort \
#--read-xml-0.5 enableDateParsing=no file="NHN_08HE0X1_1_0_HN_NLFLOW_1.1.xml" --migrate --sort \
#--read-xml-0.5 enableDateParsing=no file="NHN_08HE0X1_1_0_HN_NLFLOW_1.2.xml" --migrate --sort \
#--merge --merge \   #Note one less merge than the number of input files
#--write-xml file="NHN_NLFLOW_COMPLETE.xml"

#The final bounding boxing command should appear similar to:
#~/osmosis-0.35/bin/osmosis \
#--read-xml enableDateParsing=no file="NHN_NLFLOW_COMPLETE.xml" \
#--tee 24 \
#--bounding-box completeWays=yes completeRelations=yes top=49.6 left=-126.0 bottom=49.4 right=-125.8 --write-xml file=Split_49.4_-125.8.osm \
#--bounding-box completeWays=yes completeRelations=yes top=49.8 left=-126.0 bottom=49.6 right=-125.8 --write-xml file=Split_49.6_-125.8.osm \
#--bounding-box completeWays=yes completeRelations=yes top=50.0 left=-126.0 bottom=49.8 right=-125.8 --write-xml file=Split_49.8_-125.8.osm \
#...20 more times...
#--bounding-box completeWays=yes completeRelations=yes top=50.2 left=-127.0 bottom=50.0 right=-126.8 --write-xml file=Split_50.0_-126.8.osm

#You should end up with Split_X_Y.osm files where X is lat and Y is long of the bottom-right corner of the bbox

SIZE=0.2 #degrees wide and tall you want the bounding boxes to be (user can change this)
IN_FILE_COUNT=0 #will count how many files there are
MERGE_COMMAND="~/osmosis-0.35/bin/osmosis " #place where osmosis can be found, append rest of command onto this

for FILES in NHN*.xml
do
  printf -v NEXT_FILE " --read-xml-0.5 enableDateParsing=no file=\"%s\" --migrate --sort " $FILES
  MERGE_COMMAND=$MERGE_COMMAND$NEXT_FILE
  IN_FILE_COUNT=$(($IN_FILE_COUNT+1)) #counting for how many merges we will require
done
for i in `seq 1 $(($IN_FILE_COUNT-1))` #need one less merge than number of files
do
  MERGE_COMMAND=$MERGE_COMMAND`echo " --merge"`
done
MERGE_COMMAND=$MERGE_COMMAND`echo " --write-xml file=\"NHN_NLFLOW_COMPLETE.osm\""`

echo "Merge command is" $MERGE_COMMAND

$MERGE_COMMAND

BOTTOM=`grep "<node" NHN_NLFLOW_COMPLETE.osm| sed -e 's/^.*lat="//' | sed -e 's/".*$//'|sort -n|head -1`
ROUNDED_BOTTOM=$(echo "scale=2; $BOTTOM/1" | bc -l) #round the bottom down to nearest 0.01 or else we get really long floats for bbox

TOP=`grep "<node" NHN_NLFLOW_COMPLETE.osm| sed -e 's/^.*lat="//' | sed -e 's/".*$//'|sort -n|tail -1`
#echo "orig bottom:" $BOTTOM "rounded bottom:" $ROUNDED_BOTTOM "while top is" $TOP

RIGHT=`grep "<node" NHN_NLFLOW_COMPLETE.osm| sed -e 's/^.*lon="//' | sed -e 's/".*$//'|sort -n|tail -1`
ROUNDED_RIGHT=$(echo "scale=2; $RIGHT/1" | bc -l)

LEFT=`grep "<node" NHN_NLFLOW_COMPLETE.osm| sed -e 's/^.*lon="//' | sed -e 's/".*$//'|sort -n|head -1`
#echo "orig right:" $RIGHT "rounded right:" $ROUNDED_RIGHT "while left is" $LEFT

SPLIT_COMMAND="" #build up the command line for the bbox splitting
NUM_SPLITS=0 #count how many splits, so we know the value for tee
for i in `seq $ROUNDED_RIGHT -$SIZE $LEFT`
do
  for j in `seq $ROUNDED_BOTTOM $SIZE $TOP`
  do
    BOX_TOP=`echo "$j+$SIZE"|bc -l`
    BOX_LEFT=`echo "$i-$SIZE"|bc -l`
    printf -v NEXT_BOX " --bounding-box completeWays=yes completeRelations=yes top=%s left=%s bottom=%s right=%s --write-xml file=Split_%s_%s.osm" $BOX_TOP $BOX_LEFT $j $i $j $i
    SPLIT_COMMAND=$SPLIT_COMMAND$NEXT_BOX
    NUM_SPLITS=$(($NUM_SPLITS+1))
  done
done

SPLIT_COMMAND="~/osmosis-0.35/bin/osmosis --read-xml enableDateParsing=no file=\"NHN_NLFLOW_COMPLETE.osm\" --tee "$NUM_SPLITS$SPLIT_COMMAND
echo "Split Command is" $SPLIT_COMMAND
$SPLIT_COMMAND

