#!/bin/sh
USERDIR=$1
BYTILE=${2:-0}


#if [ "$#" -nq 1 ]; then
#	echo "Usage " $0 " <Processs Directory>" 

#	exit
#fi
echo "Starting Process..."
echo $USERDIR"findProducts.json"
wkt=$(getJSONparameter.py $USERDIR"findProducts.json" wkt)
RED='\033[0;31m'
NC='\033[0m' # No Color
echo "wkt"
echo $wkt

echo $USERDIR
cd $USERDIR

#0.- Create Shape File From WKT
echo "${RED}Creating Shape file...${NC}"
if [ -d "myshape" ]; then
	rm -r myshape
fi

wktToShape.py $USERDIR"findProducts.json" "myshape"


#1.- Link L2A products 
echo ${RED}"Linking Products..."${NC}
if [ ! -d "L2A" ]; then
	mkdir L2A
fi
cd L2A
	rm *
cd ..
yes yes | findProducts.py $USERDIR"findProducts.json"


#2.- Extract Images
echo ${RED}"Extracting JP2 Images..."${NC}
if [ ! -d "jp2" ]; then
	mkdir jp2
fi
if [ $BYTILE -ne "0" ]; then
        echo "BY TILE"
	L2AProductListExtractData.sh $USERDIR"L2A/" $USERDIR"jp2/" 4 1

else
        echo "BY DATE"
	L2AProductListExtractData.sh $USERDIR"L2A/" $USERDIR"jp2/" 4 0
fi

#3.- Merge Images
echo ${RED}"\nMerging Images..."${NC}
if [ $BYTILE -ne "0" ]; then
	echo "BY TILE"
	cd $USERDIR"jp2/"
	if [ ! -d "../out" ]; then
		mkdir "../out"
	fi
#	ls -d */ | parallel -q --jobs 1 mergeImagesByDirectory.sh $USERDIR"jp2/"{} "$wtk" 4
	for i in $(ls -d */) ; do
		if [ ! -d "../out/"$i ]; then
			mkdir "../out/"$i
		fi
		mergeImagesByDirectory.sh $USERDIR"jp2/"$i ../../../out/$i "$wkt"

	done
else
	echo "BY DATE"
        cd $USERDIR"jp2/"
        if [ ! -d "../out" ]; then
                mkdir "../out"
        fi
        mergeImagesByDirectory.sh $USERDIR"jp2/" $USERDIR"out/" "$wkt" 4

fi

#4.- Mask raster with shape file
echo ${RED}"\nApplying Mask..."${NC}

if [ $BYTILE -ne "0" ]; then
        echo "BY TILE"
        cd $USERDIR"out/"
        for d in $(ls -d */) ; do
		cd $d
	        if [ ! -d "mask" ]; then
        	        mkdir mask
        	fi

	        for i in $(ls *.tif) ; do
        	        if [  ! -e mask/$i ]; then
                	        gdalwarp -cutline  $USERDIR"myshape/wkt.shp" -crop_to_cutline -dstalpha $i mask/$i
	                else
        	                echo "Passing " $i
                	fi
	        done
		cd ..
        done

else
        echo "BY DATE"
	cd $USERDIR"out/"
	if [ ! -d "mask" ]; then
        	mkdir mask
	fi

        for i in $(ls *.tif) ; do
		if [  ! -e mask/$i ]; then
			gdalwarp -cutline ../myshape/wkt.shp -crop_to_cutline -dstalpha $i mask/$i
		else
			echo "Passing " $i
		fi
	done
fi

#5.- Delete images with few data
echo ${RED}"\nDeleting Images..."${NC}

if [ $BYTILE -ne "0" ]; then
        echo "BY TILE"
        cd $USERDIR"out/"
	for i in $(ls -d */) ; do
		cd $i"mask"
			echo $i
	        if [ ! -d "SCL" ]; then
        	        mkdir SCL
        	fi
        	cp *SCL_60m.tif SCL
        	cd SCL
	       	ls *.tif | parallel --jobs 4 imageMissingData.py {} $USERDIR"findproducts.json" 70
	        if [ ! -d "../TCI" ]; then
        	        mkdir ../TCI
        	fi
        	for scl in $(ls *.tif)
        	do
                	fileprefix=$(echo $scl | (cut -d"_" -f1))
                	cp ../$fileprefix"_TCI_10m.tif" ../TCI/$fileprefix"_TCI_10m.tif"
        	done
	        cd $USERDIR"out/"

	done
else
        echo "BY DATE"
        cd $USERDIR"out/mask/"
	if [ ! -d "SCL" ]; then
        	mkdir SCL
	fi
	cp *SCL_60m.tif SCL
	cd SCL
	ls *.tif | parallel --jobs 4 imageMissingData.py {} $USERDIR"findproducts.json" 70

	if [ ! -d "../TCI" ]; then
        	mkdir ../TCI
	fi
	for scl in $(ls *.tif)
	do
        	fileprefix=$(echo $scl | (cut -d"_" -f1))
	        cp ../$fileprefix"_TCI_10m.tif" ../TCI/$fileprefix"_TCI_10m.tif"
                cd ../TCI
	done
fi
#6.-Creating Thumbnails
#echo ${RED}"Creating Thumbnails"${NC}
#if [ $BYTILE -ne "0" ]; then
#        echo "BY TILE"
#        cd $USERDIR"out/"
#	for i in $(ls -d */) ; do
#                cd $i"mask"
#		if [ ! -d "SCL/thumbnails" ]; then
#			mkdir SCL/thumbnails
#	        fi
#        	if [ ! -d "TCI/thumbnails" ]; then
#                	mkdir TCI/thumbnails
#	        fi
#	        cd TCI
#        	mogrify -format jpg -quality 100 *.tif
#	        mogrify -path ./thumbnails -resize 800x600!  *.jpg
#		cd thumbnails
#		ffmpeg -pattern_type glob -i "*.jpg" -r 1 -vcodec libx264 $USERDIR"video.mp4"

#		cd $USERDIR"out/"
#	done
#else
#        echo "BY DATE"
#        cd $USERDIR"out/mask/"
#        if [ ! -d "SCL/thumbnails" ]; then
#                mkdir SCL/thumbnails
#        fi
#        if [ ! -d "TCI/thumbnails" ]; then
#                mkdir TCI/thumbnails
#        fi

#	cd TCI
#        mogrify -format jpg -quality 100 *.tif
#	mogrify -path ./thumbnails -resize 10%  *.jpg

#fi


#7.-Extract SCL information
echo ${RED}"Extracting SCL information..."${NC}

if [ $BYTILE -ne "0" ]; then
        echo "BY TILE"
	cd $USERDIR"out/"

        for i in $(ls -d */) ; do
	        cd ../out/$i"mask/SCL"
	        if [ ! -d "json" ]; then
        	        mkdir json
	        fi
        	SCLimageListToJSON.sh $USERDIR"out/"$i"mask/SCL/" $USERDIR"findProducts.json" 6
	        mv *.json json
        	cd json
		tile=$(echo $i | cut -d "/" -f1)
	        mergeL2ASCL_JSON.py $USERDIR"out/"$i"mask/SCL/json/" > $USERDIR$tile"_sclData.json"
		echo $tile
		echo "_sclData.json"
		cd $USERDIR"out/"
	done
else
        echo "BY DATE"
	cd $USERDIR"out/mask/SCL/"
	if [ ! -d "json" ]; then
        	mkdir json
	fi
	SCLimageListToJSON.sh $USERDIR"out/mask/SCL/" $USERDIR"findProducts.json" 6
	mv *.json json
	cd json
	mergeL2ASCL_JSON.py $USERDIR"out/mask/SCL/json/" > $USERDIR"scl_data.json"
fi
exit
##8.- Split SCL
#echo ${RED}"Spliting SCL image..."${NC}
#if [ $BYTILE -ne "0" ]; then
#        echo "BY TILE"

#else
#        echo "BY DATE"
#        cd $USERDIR"out/mask/SCL/"
#        if [ ! -d "split" ]; then
#		mkdir split
#	fi

#	
#        for f in $(ls  *.tif) ; do
#		echo $f
#		splitSCL.py $f split/
#	done
#fi

#9.-Create Tiles
#echo ${RED}"Creating Tiles..."${NC}
#if [ $BYTILE -ne "0" ]; then
#        echo "BY TILE"
#        cd $USERDIR"out/"
#        for d in $(ls -d */) ; do
#		cd $d"mask/TCI/"

#        	if [ ! -d "tiles" ]; then
#                	mkdir tiles
#	        fi

#	        for i in $(ls  *.tif) ;
#        	do
#                	cd tiles
#	                gdal2tiles.py ../$i
#        	        cd ..
#	        done
#        	cd $USERDIR"out/"
#		cd $d"mask/SCL/"
#                if [ ! -d "tiles" ]; then
#                        mkdir tiles
#                fi

#                for i in $(ls  *.tif) ;
#                do
#                        cd tiles
#                        gdal2tiles.py ../$i
#                        cd ..
#                done
#                cd $USERDIR"out/"

#        done
#else
#        echo "BY DATE"
#        cd $USERDIR"out/mask/TCI/"
#        if [ ! -d "tiles" ]; then
#                mkdir tiles
#        fi
#	for i in $(ls  *.tif) ;
#	do
#		cd tiles
#		gdal2tiles.py --zoom="10-15" ../$i
#		cd ..
#	done

#        cd $USERDIR"out/mask/SCL/split/"
#        if [ ! -d "tiles" ]; then
#                mkdir tiles
#        fi
#        for i in $(ls  *.tif) ;
#        do
#                cd tiles
#                gdal2tiles.py --zoom="10-15" ../$i
#                cd ..
#        done

#	
#fi

exit
##9.-Create ZIP file
#echo "Compresing Files in " $USERDIR "..."
#cd $USERDIR
#zip -r myzip . --exclude *.json --exclude *.zip --exclude *thumbnails* --exclude *.tif -x ./jp2\* ./L2A\* \*\*/\*/SCL/\*^C
#echo "END"

#mv *SCL_60m.tif json
#cd json
