#!/bin/bash

source ~/shgen/mm_cameras.sh

#The script mm_cameras.sh contains a large database of camera calib. reports from Iceland. The fiducial coordinates from the calib. report closest in time to the survey are added into a xml file for MicMac.
create_cam_lmi_1979

#Create the template fiducials, using the image 2-8535.tif as reference. The coordinates are hand-picked in QGIS, using the plugin "Coordinate capturer"
create_px_4fid_template 2-8535.tif 1005.649 5944.503 6781.542 147.243 12575.063 5929.585 6797.768 11717.049

#Gradual search for the pattern in MicMac. Start from a high threshold and a small search window. The images missing are then searched gradually with lower thresholds and higher search windows
for thr in 0.9 0.8 0.7
do
    for win in 10 20 50 100 200 500
    do
        mm3d Kugelhupf ".*.tif" Ori-InterneScan/MeasuresIm-2-8536.tif.xml SearchIncertitude=$win Threshold=$thr
    done
done

#The command ReSampFid in MicMac produces the affine transformation and crops the images
ls *.tif \
| awk '{print "mm3d ReSampFid "$1" 0.02"}' \
| parallel -j32

#This is an optional command, to add a black border at the edge of the image, so the photogrammetric processing does not take into account some remaining edges or marks in the images.
ls OIS*.tif \
| awk '{print "convert "$1 " -crop +90+90 -crop -90-90 "$1"_crp.tif"}' \
| parallel -j16

#Renaming of files
rename 's/.tif_crp.tif/_crp.tif/g' *_crp.tif
rename 's/OIS-Reech_//' *_crp.tif
rm OIS*.tif
