#!/usr/bin/env bash 

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

tif2pc()
{
	NAM=$(basename "$1" | sed 's/.\{4\}$//')
	gdal_translate -of XYZ $1 ${NAM}.xyz
	awk '$3!="0"' ${NAM}.xyz > ${NAM}.csv
    sed -i '/3.4028234663852886e+38/d' ${NAM}.csv
	cat > ${NAM}.vrt << EOF
<OGRVRTDataSource>
	<OGRVRTLayer name="${NAM}">
		<SrcDataSource>${NAM}.csv</SrcDataSource>
		<GeometryType>wkbPoint</GeometryType>
		<GeometryField encoding="PointFromColumns" x="field_1" y="field_2" z="field_3"/>
	</OGRVRTLayer>
</OGRVRTDataSource>
EOF
	rm ${NAM}.xyz
}

size_ras()
{
    gdalinfo $1 | grep "Size is" | awk '{print $3,$4}' | sed 's/,//g'
}

txe_ras()
{
    gdalinfo $1 | grep "Lower" | awk '{print $4}' | sed 's/,//g' | tr '\n' ' '
}

tye_ras()
{
    gdalinfo $1 | grep "Left" | awk '{print $5}' | sed -e 's/,//g' -e 's/)//g'| tr '\n' ' ' | awk '{print $2,$1}'
}

lavainterp_isn93()
{
#interp_delanuay_crp faf_20220813_0900_dem_faf_20220804_dem_nuth_x+1.08_y+0.10_z-0.25_align_diff_cl.tif rkns_20220813_0900.gpkg
    nam=$(basename $1 .tif)
    tif2pc $1

    gdal_grid -a linear -ot Float32 -of GTiff \
        -outsize $(size_ras $1) -txe $(txe_ras $1) -tye $(tye_ras $1) -a_srs EPSG:3057 \
        $nam.vrt ${nam}_nogaps.tif

    gdalwarp -overwrite -r bilinear -co compress=lzw -co predictor=2 -dstnodata 0 \
        -cutline $2 ${nam}_nogaps.tif ${nam}_clnogaps.tif

    #rm $nam.vrt $nam.csv ${nam}_nogaps.tif
}


frm_disa()
{
#1:dem 2:ddem 3:ddem_cl 4:lavafree 5:outlines 6:disanam
    bbox='336000 378000 344000 386000'
    outdir=/var/export-ftproot/joaquin/faf/${7}
    mkdir -p $outdir
    gdalwarp -overwrite -r cubic -tr 2 2 -te $bbox $1 $outdir/${7}_dem.tif
    gdalwarp -overwrite -r cubic -tr 2 2 -te $bbox $2 $outdir/${7}_ddem.tif
    gdalwarp -overwrite -r cubic -tr 2 2 -te $bbox $3 $outdir/${7}_ddem_cl.tif
    gdalwarp -overwrite -r cubic -tr 2 2 -te $bbox $4 $outdir/${7}_ddem_lavafree.tif
    cp $6 $outdir/${7}_ortho.tif
    ogr2ogr -f 'ESRI Shapefile' $outdir/${7}.shp $5
    lavareadme $outdir/readme.txt $8 $9
}

lavacl_in()
{
    nam=$(basename $1 .tif)
    gdalwarp -r cubic -cutline $2 -crop_to_cutline $demzstd $1 ${nam}_cl.tif
}

lavacl_out()
{
    nam=$(basename $1 .tif)
    gdalwarp -r cubic -cutline $2 -crop_to_cutline $demzstd $1 ${nam}_lavafree.tif
}

lavareadme()
{
    cat > $1 << EOF
This folder contains the orthomosaics, lava outlines, Digital Elevation Models and lava thickness maps of the eruption in Reykjanes (Meradalir).

The data is collected as part of the response plan organized by the Icelandic Civil Protection (Almannavarnir). First results are provided with a typical positional accuracy of 1 m. If needed, the data will be re-analyzed for more accurate results.

If using the data, please cite as below:
Data collected and processed by Sydney Gunnarson and Joaquin M.C. Belart. National Land Survey of Iceland, Icelandic Institute of Natural History and Institute of Earth Sciences. Based on the work of Pedersen et al., 2022.

Reference:
Pedersen, G. B. M., Belart, J. M. C., Óskarsson, B. V., Gudmundsson, M. T., Gies, N., Högnadóttir, T., et al. (2022). Volume, effusion rate, and lava transport during the 2021 Fagradalsfjall eruption: Results from near real-time photogrammetric monitoring. Geophysical Research Letters, 49, e2021GL097125. https://doi.org/10.1029/2021GL097125

Cartographic specifications:
Cartographic projection: ISN93/Lambert 1993 (EPSG: 3057, https://epsg.io/3057)
Horizontal and vertical reference frame: ISN2016/ISH2004, updated locally around the study area in April 2021 (after pre-eruptive deformations occurred)
Origin of elevations: Ellipsoid WGS84
Raster data format: GeoTIFF
Vector data format: ESRI Shapefile
EOF
}
