# Path: utils/s2p_mosaic.py
# def vrt_body_source(fname,band,src_x,src_y,src_w,src_h,dst_x,dst_y,dst_w,dst_h):
# def vrt_header(w,h,dataType='Float32'):
# def vrt_footer():
# def global_extent(tiles):
# def write_row_vrts(tiles,sub_img,vrt_basename,min_x,max_x):
# def write_main_vrt(vrt_row,vrt_name,min_x,max_x,min_y,max_y):
# def main(tiles_file,outfile,sub_img):


import numpy as np
import argparse
import os
import sys
import json
import shutil
import multiprocessing
import collections
import subprocess
import glob
import s2p
        import rasterio
from utils import s2p_mosaic
    percentile = np.nanpercentile(np.abs(diff), 99)
    print('99th percentile abs difference',percentile,'(tolerance='+str(percentile_tol)+')')
    assert(percentile <= percentile_tol)


def end2end(config,ref_dsm,absmean_tol=0.025,percentile_tol=1.):

    print('Configuration file: ',config)
    print('Reference DSM:',ref_dsm,os.linesep)

    test_cfg = s2p.read_config_file(config)
    s2p.main(test_cfg)

    outdir = test_cfg['out_dir']

    computed = s2p.common.gdal_read_as_array_with_nans(os.path.join(outdir,'dsm.tif'))
    expected = s2p.common.gdal_read_as_array_with_nans(ref_dsm)

    end2end_compare_dsm(computed,expected,absmean_tol,percentile_tol)


def end2end_mosaic(config,ref_height_map,absmean_tol=0.025,percentile_tol=1.):

    test_cfg = s2p.read_config_file(config)
    outdir = test_cfg['out_dir']
    s2p.main(test_cfg)

    tiles_file = os.path.join(outdir,'tiles.txt')
    global_height_map = os.path.join(outdir,'height_map.tif')


# Path: utils/s2p_mosaic.py
# def vrt_body_source(fname,band,src_x,src_y,src_w,src_h,dst_x,dst_y,dst_w,dst_h):
# def vrt_header(w,h,dataType='Float32'):
# def vrt_footer():
# def global_extent(tiles):
# def write_row_vrts(tiles,sub_img,vrt_basename,min_x,max_x):
# def write_main_vrt(vrt_row,vrt_name,min_x,max_x,min_y,max_y):
# def main(tiles_file,outfile,sub_img):


# Path: tests/test_s2p.py
import numpy as np
import argparse
import os
import sys
import json
import shutil
import multiprocessing
import collections
import subprocess
import glob
import s2p
        import rasterio
from utils import s2p_mosaic

    percentile = np.nanpercentile(np.abs(diff), 99)
    print('99th percentile abs difference',percentile,'(tolerance='+str(percentile_tol)+')')
    assert(percentile <= percentile_tol)


def end2end(config,ref_dsm,absmean_tol=0.025,percentile_tol=1.):

    print('Configuration file: ',config)
    print('Reference DSM:',ref_dsm,os.linesep)

    test_cfg = s2p.read_config_file(config)
    s2p.main(test_cfg)

    outdir = test_cfg['out_dir']

    computed = s2p.common.gdal_read_as_array_with_nans(os.path.join(outdir,'dsm.tif'))
    expected = s2p.common.gdal_read_as_array_with_nans(ref_dsm)

    end2end_compare_dsm(computed,expected,absmean_tol,percentile_tol)


def end2end_mosaic(config,ref_height_map,absmean_tol=0.025,percentile_tol=1.):

    test_cfg = s2p.read_config_file(config)
    outdir = test_cfg['out_dir']
    s2p.main(test_cfg)

    tiles_file = os.path.join(outdir,'tiles.txt')
    global_height_map = os.path.join(outdir,'height_map.tif')


    s2p_mosaic.main(tiles_file,global_height_map,'pair_1/height_map.tif')