#!/usr/bin/env python

from ftplib import FTP

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

InpArgLIst = sys.argv

#----------------------------------------------------------------------------------------#

download_myd06_c6 = True
download_myd03 = True
download_myd021km = True
download_myd06_c5 = True

year = 2005 
month = 4 
day = 1 
hour = 13 
minute = 15 

#----------------------------------------------------------------------------------------#

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

print 'Year: ', year
print 'month: ', month
print 'day: ', day
print 'hour: ', hour
print 'minute: ', minute

print 'Opening FTP connection'

ftp = FTP('ladsftp.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
print ind

#----------------------------------------------------------------------------------------#
# get MYD06 C6 File

if download_myd06_c6:

	directory = 'allData/6/MYD06_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")

#----------------------------------------------------------------------------------------#
# get MYD03 File 

if download_myd03:

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

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

	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")

#----------------------------------------------------------------------------------------#
# get MYD021km File 

if download_myd021km:

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

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

	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")

#----------------------------------------------------------------------------------------#
# get MYD06 C51 File

if download_myd06_c5:

	directory = '/allData/51/MYD06_L2/'+str(year)+'/'+day_of_year
	ftp.cwd(directory)

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

	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:
1 juin 2016 21:55:45

Updated:
1 juin 2016 21:55:45

License / Licence

MIT License