#!/usr/bin/env python
from __future__ import print_function
from subprocess import Popen, PIPE, call
from threading import Thread
import os
import time
import sys


def ExMDT(nombre,long_min,lat_max,long_max,lat_min):
    """
        Función que extrae mediante un mínimo rectángulo envolvente un mdt y lo
        guarda en un ftp para su posterior descarga
    Parameters
    ----------
    nombre : nombre del mdt
    long_min : longitud mínima
    lat_max : latitud máxima
    long_max : longitud máxima
    lat_min : latitud mínima

    Returns
    -------
    success: none 
    """

    result = os.system('gdal_translate -b 1 -of GTiff -projwin {} {} {} {} -projwin_srs EPSG:4326 {} {}'.format(
        str(long_min), str(lat_max), str(long_max), str(lat_min),
        'GMS/tools/DEM/VRTs/MDT_ESP_5m.vrt',
        'static/GMS/DEM/' + nombre + '.tif'))

    return result

#ExMDT("luisssss","-3.72509","40.10427","-3.66557","40.078761")