#!/usr/bin/env python

from ftplib import FTP

import numpy as np
import os
import calendar

#----------------------------------------------------------------------------------------#
# inputs

year = 2012
month = 2

#----------------------------------------------------------------------------------------#
# Create directory 

os.system( 'mkdir ' + str(year) )

for day in np.arange(calendar.monthrange(year,month)[1]):
	d = './' + str(year) + '/' + str(year) + '_' + "%02d" % month + \
		 '_' + "%02d" % (day+1) + '/'
	os.system('mkdir ' + d)

#----------------------------------------------------------------------------------------#
# download files from NASA ladsftp 

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

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]

#----------------------------------------------------------------------------------------#
# download MYD06 C6 File

if download_myd06_c6:

	for day in np.arange(calendar.monthrange(year,month)[1]):

		directory = 'allData/6/MYD06_L2/'+str(year)+'/'+"%03d"%(day_of_year+day+1)
		ftp.cwd(directory)
		print directory

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

		if len(yyy) != 288:
			print 'Warning missing data !'
		for j in np.arange(len(yyy)):
			file_name = yyy[j]
			print file_name	
			
			ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)
			command = 'mv ' + file_name + ' ' + './' + str(year) + '/' + str(year) + \
			'_' + "%02d" % month + '_' + "%02d" % (day+1) + '/' + file_name 
			os.system(command)

#----------------------------------------------------------------------------------------#
# download MYD03 File 

if download_myd03:

	for day in np.arange(calendar.monthrange(year,month)[1]):

		directory = '/allData/6/MYD03/'+str(year)+'/'+"%03d"%(day_of_year+day+1)
				
		ftp.cwd(directory)

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

		print 'len(yyy)', len(yyy)

		if len(yyy) != 288:
			print 'Warning missing data !'
		for j in np.arange(len(yyy)):
			file_name = yyy[j]
			print file_name	
			
			ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)
			command = 'mv ' + file_name + ' ' + './' + str(year) + '/' + str(year) + \
			'_' + "%02d" % month + '_' + "%02d" % (day+1) + '/' + file_name 
			os.system(command)

#----------------------------------------------------------------------------------------#
# download MYD021km File 

if download_myd021km:

	for day in np.arange(calendar.monthrange(year,month)[1]):

		directory = '/allData/6/MYD021KM/'+str(year)+'/'+"%03d"%(day_of_year+day+1)
		ftp.cwd(directory)

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

		print 'len(yyy)', len(yyy)

		if len(yyy) != 288:
			print 'Warning missing data !'
		for j in np.arange(len(yyy)):
			file_name = yyy[j]
			print file_name	
			
			ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)
			command = 'mv ' + file_name + ' ' + './' + str(year) + '/' + str(year) + \
			'_' + "%02d" % month + '_' + "%02d" % (day+1) + '/' + file_name 
			os.system(command)

#----------------------------------------------------------------------------------------#
# download MYD06 C51 File

if download_myd06_c5:

	for day in np.arange(calendar.monthrange(year,month)[1]):

		directory = '/allData/51/MYD06_L2/'+str(year)+'/'+"%03d"%(day_of_year+day+1)
		ftp.cwd(directory)

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

		print 'len(yyy)', len(yyy)

		if len(yyy) != 288:
			print 'Warning missing data !'
		for j in np.arange(len(yyy)):
			file_name = yyy[j]
			print file_name	
			
			ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)
			command = 'mv ' + file_name + ' ' + './' + str(year) + '/' + str(year) + \
			'_' + "%02d" % month + '_' + "%02d" % (day+1) + '/' + file_name 
			os.system(command)

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

print 'Closing FTP connection'
ftp.close()

About / Au sujet de

Ben
Created:
14 juin 2016 20:28:13

Updated:
14 juin 2016 20:28:13

License / Licence

MIT License