
JP2=IMG_PHR1A_P_201207191534034_SEN_565234101-002_R1C1.JP2
RPC=RPC_PHR1A_P_201207191534034_SEN_565234101-002.XML

OUT=`basename  $JP2 .JP2`.TIF
TMP=`mktemp /tmp/kk.XXXX.TIF`
SZX=`cat $RPC | grep LAST_COL | cut -f2 -d'>' | cut -f1 -d'<'`
SZY=`cat $RPC | grep LAST_ROW | cut -f2 -d'>' | cut -f1 -d'<'`
otbcli_ExtractROI -in $JP2 -out $TMP uint16 -startx 0 -starty 0 -sizex $SZX -sizey $SZY

#convert to tiled with GDAL
 # tiffcp DOES NOT WORK
 #tiffcp -l 256 -w 256 $TMP $OUT
#gdal_translate -of GTiff -co "TILED=YES COMPRESS=LZW" $TMP $OUT
#gdal_translate -of GTiff -co "TILED=YES" -co "COMPRESS=LZW" -co "PREDICTOR=2" $TMP $OUT
gdal_translate -of GTiff -co "TILED=YES" -co "COMPRESS=DEFLATE" -co "PREDICTOR=2" $TMP $OUT
rm $TMP

# test crop
#gdal_translate -of GTiff -srcwin 20000 20000 2000 2000 $OUT test.tif

#test crop : does not work for very large files 
#tiffcrop -U px -z 20000,20000,2056,2056  $OUT test.tif
#tiffcp -s test.tif tests.tif

