<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 How to download CMEMS SLTAC products from the CMEMS ftp server ?
some python imports
Step1:  init the connection to the ftp server
Step2:  What is in the current directory ?
Step3:  change to the directory of level 4 delayed-time global maps, and list the content.
Grids are organized as subdirs following years
Step4:  get one file, and unzip it
Step5:  close the connection to the ftp server
Step6:  Now we are going to download several files.
As an example here, every first and fifteenth of each month for five years are downloaded but you could build any combination thanks to Python.
However you can't use '*' to expand filenames in ftplib, so we have to list all files in the current directory first.
<ASSISTANT_TASK:>
Python Code:
from ftplib import FTP
import os
import numpy as np
Explanation: How to download CMEMS SLTAC products from the CMEMS ftp server ?
some python imports
End of explanation
ftp = FTP('ftp.sltac.cls.fr') 
ftp.login('pprandi','PierreCMEMS2017')
Explanation: init the connection to the ftp server
End of explanation
ftp.retrlines('LIST')
Explanation: What is in the current directory ?
End of explanation
ftp.cwd('Core/SEALEVEL_GLO_PHY_L4_REP_OBSERVATIONS_008_047/dataset-duacs-rep-global-merged-allsat-phy-l4-v3/2016/')
ftp.retrlines('LIST')
Explanation: change to the directory of level 4 delayed-time global maps, and list the content.
Grids are organized as subdirs following years
End of explanation
filename="dt_global_allsat_phy_l4_20160925_20170209.nc.gz"
downloadDir = "/Users/pierre/Documents/Presentations/210706_CMEMSArctic/data_tmp"
fHandle = open("%s/%s" %(downloadDir, filename), "wb")
ftp.retrbinary("RETR " + filename , fHandle.write)
fHandle.close()
os.system("gzip -d %s/%s" %(downloadDir, filename))
Explanation: get one file, and unzip it
End of explanation
ftp.quit()
Explanation: close the connection to the ftp server
End of explanation
# get back to parent directory on the ftp server
ftp = FTP('ftp.sltac.cls.fr') 
ftp.login('pprandi','PierreCMEMS2017')
ftp.cwd('Core/SEALEVEL_GLO_PHY_L4_REP_OBSERVATIONS_008_047/dataset-duacs-rep-global-merged-allsat-phy-l4-v3')
# get two files a month for five years
for year in np.arange(2010, 2016, 1):
    ftp.cwd("%4i" %year)
    remoteFilesList = ftp.nlst()
    for month in np.arange(1, 13, 1):
        for day in [1, 15]:
            
            print("downloading year %s, month %s, day %s" %(year, month, day))
            pattern = "dt_global_allsat_phy_l4_%4i%02i%02i_" %(year, month, day)
            index = [i for i, elem in enumerate(remoteFilesList) if pattern in elem][0]
            remoteFilename = remoteFilesList[index]
            localFilename = "%s/dt_global_allsat_phy_l4_%4i%02i%02i.nc.gz" %(downloadDir, year, month, day)
            if not os.path.isfile(localFilename[0:-3]):
                fHandle = open("%s" %localFilename, "wb")
                ftp.retrbinary("RETR " + remoteFilename , fHandle.write)
                fHandle.close()
                os.system("gzip -d %s" %localFilename)
    ftp.cwd("..")
    
ftp.quit()
# download one along track file
ftp = FTP('ftp.sltac.cls.fr') 
ftp.login('pprandi','PierreCMEMS2017')
ftp.cwd('/Core/SEALEVEL_GLO_PHY_L3_REP_OBSERVATIONS_008_045/dataset-duacs-rep-global-alg-phy-l3-v3/2016/')
ftp.retrlines('LIST')
filename="dt_global_alg_phy_vfec_l3_20160925_20170209.nc.gz"
downloadDir = "/Users/pierre/Documents/Presentations/201706_CMEMSArctic/data_tmp"
fHandle = open("%s/%s" %(downloadDir, filename), "wb")
ftp.retrbinary("RETR " + filename , fHandle.write)
fHandle.close()
os.system("gzip -d %s/%s" %(downloadDir, filename))
ftp.quit()
Explanation: Now we are going to download several files.
As an example here, every first and fifteenth of each month for five years are downloaded but you could build any combination thanks to Python.
However you can't use '*' to expand filenames in ftplib, so we have to list all files in the current directory first.
End of explanation
<END_TASK>