#!/usr/bin/env python

from ftplib import FTP

import numpy as np
import os
import calendar
import pprint
import sys

InpArgLIst = sys.argv

#----------------------------------------------------------------------------------------#
# user inputs (case 1): date provided in the  script

year = 2015
month = 07
day = 9
hour = 00
minute = 10 

#----------------------------------------------------------------------------------------#
# user inputs (case 2): directly in command line

if len(InpArgLIst) == 6 :
	year = int( InpArgLIst[1] )
	month = int( InpArgLIst[2] )
	day = int( InpArgLIst[3] )
	hour = int( InpArgLIst[4] ) 
	minute = int( InpArgLIst[5] ) 

#----------------------------------------------------------------------------------------#
# Opening FTP connection

print( 'Opening FTP connection' )

ftp = FTP('ladsftp.nascom.nasa.gov')
#ftp = FTP('ftp://ladsweb.nascom.nasa.gov')
ftp.login('anonymous','')

day_of_year = 0 
for i in range(month-1):
    day_of_year = day_of_year + calendar.monthrange(year,i+1)[1]

day_of_year = day_of_year + day

day_of_year = "%03d" % day_of_year

ind = str(year) + day_of_year + '.' +  "%02d" % hour + "%02d" % minute

#----------------------------------------------------------------------------------------#
# get MYD04 C6 File

directory = 'allData/6/MYD04_L2/'+str(year)+'/'+day_of_year
ftp.cwd(directory)
print directory

yyy = []
ftp.retrlines('NLST', yyy.append)
#pprint.pprint(yyy)

matching = [s for s in yyy if ind in s]
print matching
print matching[0]

e = 1
n = 0 
while e == 1 and n < 10 :
	try:
		ftp.retrbinary('RETR ' + matching[0], open(matching[0], 'wb').write)
		e = 0 
	except:
		n = n + 1
		print("Error while downloading")

#----------------------------------------------------------------------------------------#
# Closing FTP connection

print 'Closing FTP connection'
ftp.close()

About / Au sujet de

Ben
Created:
13 avril 2017 22:20:13

Updated:
13 avril 2017 22:20:13

License / Licence

MIT License