#!/usr/bin/env python

import numpy as np
import os
import calendar

from ftplib import FTP

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

year = 2008
month = 07

icare_username = 'john.doe'
icare_password = '********'

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

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

print 'Opening FTP connection'

ftp = FTP('ftp.icare.univ-lille1.fr')
ftp.login(icare_username,icare_password)

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

#----------------------------------------------------------------------------------------#
# download files 

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

	directory = '/DATA/LIENS/CALIOP/01kmCLay.v4.10/'+str(year)+'/' + \
				str(year)+'_'+ "%02d" % month + '_' + "%02d" % (day+1) + '/'

	ftp.cwd(directory)

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

	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)

print 'Closing FTP connection'
print ftp.close()

About / Au sujet de

Ben
Created:
6 décembre 2016 23:56:52

Updated:
6 décembre 2016 23:56:52

License / Licence

MIT License