# Path: s2p/common.py
# def remove(target):
# def garbage_cleanup():
# def tmpfile(ext=''):
# def run(cmd, env=os.environ):
# def mkdir_p(path):
# def shellquote(s):
# def matrix_translation(x, y):
# def image_size_gdal(im):
# def gdal_read_as_array_with_nans(im):
# def image_zoom_out_morpho(im, f):
# def rasterio_write(path, array, profile={}, tags={}):
# def image_apply_homography(out, im, H, w, h):
# def image_qauto(im, out=None):
# def points_apply_homography(H, pts):
# def bounding_box2D(pts):
# def image_crop_gdal(im, x, y, w, h, out=None):
# def run_binary_on_list_of_points(points, binary, option=None, env_var=None):
# def get_rectangle_coordinates(im):
# def get_roi_coordinates(img, preview):
# def cargarse_basura(inputf, outputf):
# def print_elapsed_time(since_first_call=False):
# class RunFailure(Exception):
# 
# Path: s2p/rpc_utils.py
# def find_corresponding_point(model_a, model_b, x, y, z):
# def compute_height(model_a, model_b, x1, y1, x2, y2):
# def geodesic_bounding_box(rpc, x, y, w, h):
# def altitude_range_coarse(rpc, scale_factor=1):
# def min_max_heights_from_bbx(im, lon_m, lon_M, lat_m, lat_M, rpc):
# def altitude_range(rpc, x, y, w, h, margin_top=0, margin_bottom=0):
# def utm_zone(rpc, x, y, w, h):
# def utm_roi_to_img_roi(rpc, roi):
# def kml_roi_process(rpc, kml):
# def generate_point_mesh(col_range, row_range, alt_range):
# def ground_control_points(rpc, x, y, w, h, m, M, n):
# def corresponding_roi(rpc1, rpc2, x, y, w, h):
# def matches_from_rpc(rpc1, rpc2, x, y, w, h, n):
# def alt_to_disp(rpc1, rpc2, x, y, alt, H1, H2, A=None):
# def exogenous_disp_range_estimation(rpc1, rpc2, x, y, w, h, H1, H2, A=None,
#                                     margin_top=0, margin_bottom=0):
# def altitude_range_to_disp_range(m, M, rpc1, rpc2, x, y, w, h, H1, H2, A=None,
#                                  margin_top=0, margin_bottom=0):
# def rpc_from_geotiff(geotiff_path, outrpcfile='.rpc'):
#     HSTEP = 1
#     M = rpc.altOff + rpc.altScale
#     M = rpc.altOff + scale_factor * rpc.altScale
# 
# Path: s2p/masking.py
# def cloud_water_image_domain(x, y, w, h, rpc, roi_gml=None, cld_gml=None,
#                              wat_msk=None):
# def erosion(out, msk, radius):
#     H = common.matrix_translation(-x, -y)
# 
# Path: s2p/parallel.py
# def show_progress(a):
# def tilewise_wrapper(fun, *args, **kwargs):
# def launch_calls(fun, list_of_args, nb_workers, *extra_args):
# def launch_calls_simple(fun, list_of_args, nb_workers, *extra_args):


import os
import sys
import utm
import json
import copy
import shutil
import warnings
import numpy as np
            import tempfile     # TODO: fix for common.tmpfile failure
from s2p import common
from s2p import rpc_utils
from s2p import masking
from s2p import parallel
from s2p.config import cfg
    # verify that input files paths are defined
    if 'images' not in d or len(d['images']) < 2:
        print('ERROR: missing paths to input images')
        sys.exit(1)
    for i in range(len(d['images'])):
        img = d['images'][i]
        if not dict_has_keys(img, ['img']):
            print('ERROR: missing img paths for image', img)
            sys.exit(1)
        if not dict_has_keys(img, ['rpc']) or img['rpc'] == '':
            img['rpc'] = tempfile.mktemp('.rpc')
            rpc_utils.rpc_from_geotiff(img['img'], img['rpc'])
            print('INFO: trying reading rpc from image', img)

    # verify that roi or path to preview file are defined
    if 'full_img' in d and d['full_img']:
        sz = common.image_size_gdal(d['images'][0]['img'])
        d['roi'] = {'x': 0, 'y': 0, 'w': sz[0], 'h': sz[1]}
    elif 'roi' in d and dict_has_keys(d['roi'], ['x', 'y', 'w', 'h']):
        pass
    elif 'roi_utm' in d and dict_has_keys(d['roi_utm'], ['utm_band',
                                                         'hemisphere',
                                                         'x', 'y', 'w', 'h']):
        d['roi'] = rpc_utils.utm_roi_to_img_roi(d['images'][0]['rpc'],
                                                d['roi_utm'])
    elif 'roi_kml' in d:
        # this call defines cfg['ll_bbx'] and cfg['utm_bbx'] as side effects
        d['roi'] = rpc_utils.kml_roi_process(d['images'][0]['rpc'],
                                             d['roi_kml'])
    elif 'prv' in d['images'][0]:

# Path: s2p/common.py
# def remove(target):
# def garbage_cleanup():
# def tmpfile(ext=''):
# def run(cmd, env=os.environ):
# def mkdir_p(path):
# def shellquote(s):
# def matrix_translation(x, y):
# def image_size_gdal(im):
# def gdal_read_as_array_with_nans(im):
# def image_zoom_out_morpho(im, f):
# def rasterio_write(path, array, profile={}, tags={}):
# def image_apply_homography(out, im, H, w, h):
# def image_qauto(im, out=None):
# def points_apply_homography(H, pts):
# def bounding_box2D(pts):
# def image_crop_gdal(im, x, y, w, h, out=None):
# def run_binary_on_list_of_points(points, binary, option=None, env_var=None):
# def get_rectangle_coordinates(im):
# def get_roi_coordinates(img, preview):
# def cargarse_basura(inputf, outputf):
# def print_elapsed_time(since_first_call=False):
# class RunFailure(Exception):
# 
# Path: s2p/rpc_utils.py
# def find_corresponding_point(model_a, model_b, x, y, z):
# def compute_height(model_a, model_b, x1, y1, x2, y2):
# def geodesic_bounding_box(rpc, x, y, w, h):
# def altitude_range_coarse(rpc, scale_factor=1):
# def min_max_heights_from_bbx(im, lon_m, lon_M, lat_m, lat_M, rpc):
# def altitude_range(rpc, x, y, w, h, margin_top=0, margin_bottom=0):
# def utm_zone(rpc, x, y, w, h):
# def utm_roi_to_img_roi(rpc, roi):
# def kml_roi_process(rpc, kml):
# def generate_point_mesh(col_range, row_range, alt_range):
# def ground_control_points(rpc, x, y, w, h, m, M, n):
# def corresponding_roi(rpc1, rpc2, x, y, w, h):
# def matches_from_rpc(rpc1, rpc2, x, y, w, h, n):
# def alt_to_disp(rpc1, rpc2, x, y, alt, H1, H2, A=None):
# def exogenous_disp_range_estimation(rpc1, rpc2, x, y, w, h, H1, H2, A=None,
#                                     margin_top=0, margin_bottom=0):
# def altitude_range_to_disp_range(m, M, rpc1, rpc2, x, y, w, h, H1, H2, A=None,
#                                  margin_top=0, margin_bottom=0):
# def rpc_from_geotiff(geotiff_path, outrpcfile='.rpc'):
#     HSTEP = 1
#     M = rpc.altOff + rpc.altScale
#     M = rpc.altOff + scale_factor * rpc.altScale
# 
# Path: s2p/masking.py
# def cloud_water_image_domain(x, y, w, h, rpc, roi_gml=None, cld_gml=None,
#                              wat_msk=None):
# def erosion(out, msk, radius):
#     H = common.matrix_translation(-x, -y)
# 
# Path: s2p/parallel.py
# def show_progress(a):
# def tilewise_wrapper(fun, *args, **kwargs):
# def launch_calls(fun, list_of_args, nb_workers, *extra_args):
# def launch_calls_simple(fun, list_of_args, nb_workers, *extra_args):


# Path: s2p/initialization.py
import os
import sys
import utm
import json
import copy
import shutil
import warnings
import numpy as np
            import tempfile     # TODO: fix for common.tmpfile failure
from s2p import common
from s2p import rpc_utils
from s2p import masking
from s2p import parallel
from s2p.config import cfg

    # verify that input files paths are defined
    if 'images' not in d or len(d['images']) < 2:
        print('ERROR: missing paths to input images')
        sys.exit(1)
    for i in range(len(d['images'])):
        img = d['images'][i]
        if not dict_has_keys(img, ['img']):
            print('ERROR: missing img paths for image', img)
            sys.exit(1)
        if not dict_has_keys(img, ['rpc']) or img['rpc'] == '':
            img['rpc'] = tempfile.mktemp('.rpc')
            rpc_utils.rpc_from_geotiff(img['img'], img['rpc'])
            print('INFO: trying reading rpc from image', img)

    # verify that roi or path to preview file are defined
    if 'full_img' in d and d['full_img']:
        sz = common.image_size_gdal(d['images'][0]['img'])
        d['roi'] = {'x': 0, 'y': 0, 'w': sz[0], 'h': sz[1]}
    elif 'roi' in d and dict_has_keys(d['roi'], ['x', 'y', 'w', 'h']):
        pass
    elif 'roi_utm' in d and dict_has_keys(d['roi_utm'], ['utm_band',
                                                         'hemisphere',
                                                         'x', 'y', 'w', 'h']):
        d['roi'] = rpc_utils.utm_roi_to_img_roi(d['images'][0]['rpc'],
                                                d['roi_utm'])
    elif 'roi_kml' in d:
        # this call defines cfg['ll_bbx'] and cfg['utm_bbx'] as side effects
        d['roi'] = rpc_utils.kml_roi_process(d['images'][0]['rpc'],
                                             d['roi_kml'])
    elif 'prv' in d['images'][0]:

        x, y, w, h = common.get_roi_coordinates(d['images'][0]['img'],