#====================================================================================
#                               Descargar Desde el Código de FTP
#                                   Download from FTP script
#------------------------------------------------------------------------------------
#
#(Español)
#   Este código introduce un archivo (. Txt) que contiene una lista de descarga 
#   enlaces como salida de 'Reverb Echo'. Para obtener información sobre cómo usar
#   Reverb, consultar el sitio web en [http://reverb.echo.nasa.gov/]
#
#(English)
#   This script inputs a (.txt) file containing a list of download
#   links as output by Reverb Echo. For information on how to use Reverb,
#   consult the website at [http://reverb.echo.nasa.gov/]
#
# Descripciones de entrada
#   ftptext = full path to text file downloaded from reverb
#               (data found by searching for product "MOD10A1")
#   output  = directory for files to be stored when downloaded
#
# Valores de entrada ejemplo
#   ftptext = 'C:/Users/jwely/NASA_DEVELOP_TESTBED/Datos/NASA_Datos/MODIS_2011.txt'
#   output  = 'C:/Users/jwely/NASA_DEVELOP_TESTBED/Datos/NASA_Datos/MODIS/2011'
#
#
#===================================================================================
#                       valores de entrada de usuario
#                            (User Input values)
#-----------------------------------------------------------------------------------

ftptext = r'C:\Users\jwely\Desktop\temp\test.txt'
output  = r'C:\Users\jwely\Desktop\temp'

#===================================================================================

def Download_MODIS(ftptext,output):
    import urllib
    
    ftp = open(ftptext,'r')
    a =ftp.readlines()
    b=[]
    for i in a:
        i.split("=")
    for site in a:
        url = site.rstrip()
        sub = url.split("/")
        leng = len(sub)
        name = sub[leng-1]
        if ".hdf" in sub[leng-1]:
            if not ".xml" in sub[leng-1]:
                try:
                    urllib.urlretrieve(url, output+"\\"+sub[leng-1])
                    print 'Download_MODIS: ' + sub[leng-1]+ ' Successfully downloaded'
                except:
                    print 'Download_MODIS: ' + sub[leng-1]+ ' failed to download'

    print 'Download_MODIS: Finished with all downloads!'

Download_MODIS(ftptext,output)
#=====================================================================================
