How to upload from ICARE a MODIS L2 granule using ftp and python 3 ?

Published: November 25, 2019

DMCA.com Protection Status

Example of how to upload with ftp a MODIS granule from ICARE server to my local machine in python 3.

Table of contents

Import python modules

from ftplib import FTP
from datetime import date

import numpy as np
import calendar

Granule date & time

year = 2008
month = 1
day = 8

hour = 14
minutes = 20

Retrieve count of days:

d = date(year, month, day) - date(year, 1, 1)

File name

file = 'MYD06_L2.A{:04d}{:03d}.{:02d}{:02d}'.format(year,d.days+1,hour,minutes)

Path to data

path_to = '/DATA/LIENS/MODIS/MYD06_L2/{:04d}/{:04d}_{:02d}_{:02d}'.format(year,year,month,day)

ICARE id:

icare_username = 'johndoe'
icare_password = '********'

Upload the granule using ftp

ftp = FTP('ftp.icare.univ-lille1.fr')

ftp.login(icare_username,icare_password)

ftp.cwd(path_to)

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

for j in np.arange(len(yyy)):

    file_name = yyy[j]

    if file in file_name:

        ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)

ftp.close()

Example of python script

#!/usr/bin/env python

from ftplib import FTP
from datetime import date

import numpy as np
import calendar

year = 2008
month = 1
day = 8

hour = 14
minutes = 20

d = date(year, month, day) - date(year, 1, 1)

path_to = '/DATA/LIENS/MODIS/MYD06_L2/{:04d}/{:04d}_{:02d}_{:02d}'.format(year,year,month,day)

file = 'MYD06_L2.A{:04d}{:03d}.{:02d}{:02d}'.format(year,d.days+1,hour,minutes)

icare_username = 'johndoe'
icare_password = '********'

ftp = FTP('ftp.icare.univ-lille1.fr')

ftp.login(icare_username,icare_password)

ftp.cwd(path_to)

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

for j in np.arange(len(yyy)):

    file_name = yyy[j]

    if file in file_name:

        ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)

ftp.close()