#!/usr/bin/env bash 

###################################################################
#Author         : Joaquín M.C. Belart
#Email          : joaquin@lmi.is
###################################################################

source ~/shgen/aspfunc.sh
source ~/shgen/aspfunc_declass.sh

do_mos="n"
do_crp="n"

main()
{
	if [ "$do_mos" == "y" ] ; then
        echo "Mosaicking segments..."
        ls *_{a..j}.tif | awk -F "_" '{print $1}' | sort | uniq > kh9pc.list
        while read scene
        do
            echo "Mosaicking all subscenes from $scene..."
            echo image_mosaic ${scene}_{a..j}.tif --ot byte --overlap-width 3000 -o $scene.tif
            image_mosaic ${scene}_{a..j}.tif --ot byte --overlap-width 3000 -o $scene.tif
        done < kh9pc.list
        rm kh9pc.list
    fi

	if [ "$do_crp" == "y" ] ; then
        mkdir -p crops
        historical_helper.py rotate-crop --input-path raw*/*A006.tif --output-path crops/A006_crp.tif --interest-points '3736.164 1100.491 347964.414 709.564 348092.868 22740.221 3876.914 23346.904'
        historical_helper.py rotate-crop --input-path raw*/*A007.tif --output-path crops/A007_crp.tif --interest-points '3810.640 918.024 348072.414 1395.747 348104.323 23245.858 3878.755 23109.426'
        historical_helper.py rotate-crop --input-path raw*/*A008.tif --output-path crops/A008_crp.tif --interest-points '3955.869 999.948 347934.959 1007.383 348019.232 22982.403 4072.050 23098.381'
        historical_helper.py rotate-crop --input-path raw*/*A009.tif --output-path crops/A009_crp.tif --interest-points '3903.736 1048.358 348185.868 959.383 348230.323 22928.403 3970.800 23183.063'
        historical_helper.py rotate-crop --input-path raw*/*A010.tif --output-path crops/A010_crp.tif --interest-points '4156.955 1011.119 348353.869 1090.292 348472.505 23227.858 4165.937 23109.426'
        historical_helper.py rotate-crop --input-path raw*/*A011.tif --output-path crops/A011_crp.tif --interest-points '4279.841 1089.320 348404.051 961.565 348490.505 23008.585 4337.141 23206.994'
        historical_helper.py rotate-crop --input-path raw*/*A012.tif --output-path crops/A012_crp.tif --interest-points '3706.373 1078.148 347969.868 747.746 348050.323 22745.130 3856.664 23065.244'
        historical_helper.py rotate-crop --input-path raw*/*A013.tif --output-path crops/A013_crp.tif --interest-points '3728.716 947.814 347921.868 1131.747 348012.687 23119.858 3873.232 23078.131'
        historical_helper.py rotate-crop --input-path raw*/*A014.tif --output-path crops/A014_crp.tif --interest-points '3732.440 1126.558 348074.596 854.655 348156.687 22720.585 3832.732 23183.063'
        historical_helper.py rotate-crop --input-path raw*/*A015.tif --output-path crops/A015_crp.tif --interest-points '3881.393 1048.358 347945.868 1094.656 348020.868 22946.403 3862.186 23184.903'
        historical_helper.py rotate-crop --input-path raw*/*A016.tif --output-path crops/A016_crp.tif --interest-points '3676.582 1227.101 347725.504 608.110 347793.414 22574.948 3722.277 23192.267'
        historical_helper.py rotate-crop --input-path raw*/*A017.tif --output-path crops/A017_crp.tif --interest-points '3926.078 1156.348 348065.868 926.656 348161.596 22887.494 3926.618 23094.699'
        historical_helper.py rotate-crop --input-path raw*/*A018.tif --output-path crops/A018_crp.tif --interest-points '3825.535 1174.967 347961.141 970.292 348061.777 22910.403 3897.164 23067.085'
        historical_helper.py rotate-crop --input-path raw*/*A024.tif --output-path crops/A024_crp.tif --interest-points '4004.279 1249.444 348196.778 924.474 348289.232 22784.403 4066.527 23155.449'
        historical_helper.py rotate-crop --input-path raw*/*A025.tif --output-path crops/A025_crp.tif --interest-points '3828.129 1171.292 347956.948 768.133 348032.425 22621.585 3953.311 23033.949'
        historical_helper.py rotate-crop --input-path raw*/*F006.tif --output-path crops/F006_crp.tif --interest-points '4045.671 1221.322 347841.141 492.473 347849.050 22718.948 3967.118 23253.017'
        historical_helper.py rotate-crop --input-path raw*/*F007.tif --output-path crops/F007_crp.tif --interest-points '4169.793 1070.050 347985.141 610.292 347994.686 22841.676 4061.005 23024.744'
        historical_helper.py rotate-crop --input-path raw*/*F008.tif --output-path crops/F008_crp.tif --interest-points '4022.399 1081.686 347860.777 464.110 347888.323 22610.948 3845.618 23157.290'
        historical_helper.py rotate-crop --input-path raw*/*F009.tif --output-path crops/F009_crp.tif --interest-points '3888.580 984.716 347572.777 573.201 347613.413 22804.039 3795.914 23092.858'
        historical_helper.py rotate-crop --input-path raw*/*F010.tif --output-path crops/F010_crp.tif --interest-points '4200.823 965.322 348009.141 859.019 348074.868 22894.040 4075.732 23098.381'
        historical_helper.py rotate-crop --input-path raw*/*F011.tif --output-path crops/F011_crp.tif --interest-points '4196.944 1025.444 347928.414 830.655 347947.232 22895.676 4099.664 23192.267'
        historical_helper.py rotate-crop --input-path raw*/*F012.tif --output-path crops/F012_crp.tif --interest-points '4026.277 975.019 347849.868 773.928 347909.595 22794.221 3943.186 23089.176'
        historical_helper.py rotate-crop --input-path raw*/*F013.tif --output-path crops/F013_crp.tif --interest-points '3909.914 1023.504 347889.141 505.564 347927.596 22697.676 3891.641 23078.131'
        historical_helper.py rotate-crop --input-path raw*/*F014.tif --output-path crops/F014_crp.tif --interest-points '4142.641 1097.201 348000.414 481.564 347960.323 22664.948 4038.914 23052.358'
        historical_helper.py rotate-crop --input-path raw*/*F015.tif --output-path crops/F015_crp.tif --interest-points '3801.307 1095.262 347681.868 797.928 347651.050 22939.858 3657.845 23140.722'
        historical_helper.py rotate-crop --input-path raw*/*F016.tif --output-path crops/F016_crp.tif --interest-points '3892.459 1244.595 347625.141 776.110 347670.686 22736.948 3714.913 23265.903'
        historical_helper.py rotate-crop --input-path raw*/*F017.tif --output-path crops/F017_crp.tif --interest-points '3591.853 1079.747 347537.868 815.383 347590.504 22722.221 3622.868 23135.199'
        historical_helper.py rotate-crop --input-path raw*/*F018.tif --output-path crops/F018_crp.tif --interest-points '3684.944 1114.656 347343.686 699.746 347418.686 22601.130 3657.845 23131.517'
        historical_helper.py rotate-crop --input-path raw*/*F024.tif --output-path crops/F024_crp.tif --interest-points '4068.944 1401.686 347858.595 616.837 347957.050 22516.039 3996.573 23343.222'
        historical_helper.py rotate-crop --input-path raw*/*F025.tif --output-path crops/F025_crp.tif --interest-points '4278.399 1244.595 348087.687 647.383 348195.959 22537.312 4200.914 23184.903'
        #historical_helper.py rotate-crop --input-path D3C1216-200533A019.tif --output-path A019_crp.tif --interest-points '3404.604 1180.349 346982.197 1450.860 347024.121 23273.391 3418.246 23061.108'
        #historical_helper.py rotate-crop --input-path D3C1216-200533A020.tif --output-path A020_crp.tif --interest-points '3542.355 1344.386 347045.748 1454.853 347079.022 23289.362 3609.234 23207.510'
        #historical_helper.py rotate-crop --input-path D3C1216-200533A021.tif --output-path A021_crp.tif --interest-points '3482.463 1334.404 347136.251 1417.587 347210.783 23235.459 3537.364 23198.526'
        #historical_helper.py rotate-crop --input-path D3C1216-200533A022.tif --output-path A022_crp.tif --interest-points '3973.575 1268.523 347582.112 1104.819 347657.974 22932.008 4040.454 23132.645'
        #historical_helper.py rotate-crop --input-path D3C1216-200533A023.tif --output-path A023_crp.tif --interest-points '3815.860 1294.476 347403.768 1083.524 347470.314 22906.055 3881.741 23159.597'
        #historical_helper.py rotate-crop --input-path D3C1216-200533F019.tif --output-path F019_crp.tif --interest-points '4096.353 1331.076 347289.641 648.311 347387.464 22472.839 4019.824 23202.186'
        #historical_helper.py rotate-crop --input-path D3C1216-200533F020.tif --output-path F020_crp.tif --interest-points '3534.369 1398.288 346647.801 496.586 346745.625 22321.113 3881.741 23159.597'
        #historical_helper.py rotate-crop --input-path D3C1216-200533F021.tif --output-path F021_crp.tif --interest-points '3694.080 1364.350 346876.721 734.822 346985.191 22560.680 3618.217 23243.445'
        #historical_helper.py rotate-crop --input-path D3C1216-200533F022.tif --output-path F022_crp.tif --interest-points '3364.676 1362.353 346569.277 680.254 346651.794 22494.799 3300.791 23243.445'
        #historical_helper.py rotate-crop --input-path D3C1216-200533F023.tif --output-path F023_crp.tif --interest-points '3280.828 1408.270 346473.450 897.195 346543.989 22718.395 3186.997 23294.353'
        mv *crp.tif crops/
    fi
}

main
