Downloading NOAA data using Python

Anthony Ozerov's Website 🤔 / Research Papers Projects Thoughts About

In several projects, I have had to download climate data files from NOAA's FTP servers. Here is some example code for doing that in Python:

for i in range (1950, 2019):
    ftp 
= FTP('ftp.cdc.noaa.gov')
    ftp
.login()
    ftp
.cwd('/Datasets/ncep.reanalysis.dailyavgs/surface')
    file 
= open("[name-of-directory]/y%s.nc" %i, 'wb')
    ftp
.retrbinary('RETR air.sig995.%s.nc' %i, file.write)
    ftp
.quit()

Here, I have it set up to pull the files from air.sig.995.1950.nc to air.sig.995.2019.nc from the directory /Datasets/ncep.reanalysis.dailyavgs/surface on the server ftp.cdc.noaa.gov, naming them from y1950.nc to y2019.nc and putting them in the directory [name-of-directory]. The parameters for the functions can be changed to whatever you like to fit your needs.

Sometimes it is necessary to combine all of the .nc files into one, for which I use the Linux program ncrcat.