In [1]:
from astropy.io import fits as pyfits
from astropy.time import Time
from astropy.table import Table
import ftputil
import time
import tarfile
import os
In [2]:
ChandraCaldbDir = "/pub/arcftp/caldb"
ChandraCaldbHost = "cda.harvard.edu"
CaldbWorkDir = "/Volumes/USRA16/CaldbWorkDir"
In [3]:
host = ftputil.FTPHost(ChandraCaldbHost, "anonymous", "mcorcoran@usra.edu")
In [4]:
#host.download("/pub/arcftp/caldb/caldb_4.7.2_main.tar.gz","/Volumes/USRA16/CaldbWorkDir/caldb_4.7.2_main.tar.gz",mode='b')
In [5]:
#host.download("/pub/arcftp/caldb/hrc_bkgrnd_4.7.0.tar.gz","/Volumes/USRA16/ChandraCaldbWorkDir/hrc_bkgrnd_4.7.0.tar.gz",mode='b')
In [4]:
ChandraCaldbFiles = host.listdir(ChandraCaldbDir)
host.close()
ChandraCaldbTarFiles = [f for f in ChandraCaldbFiles if '.tar' in f ]
ChandraCaldbTarFiles
Out[4]:
In [ ]:
print "Changing directory to {0}\n".format(CaldbWorkDir)
os.chdir(CaldbWorkDir)
print ("File List:")
print (ChandraCaldbTarFiles)
for f in ChandraCaldbTarFiles:
remotefile = ChandraCaldbDir+"/"+f
localfile = CaldbWorkDir+"/"+f
if os.path.exists(f):
print ("File {0} already exists; deleting prior to download".format(f))
os.remove(f)
print "\nGetting file "+f
getfile = True
inum = 0 # download counter
itrymax = 3 # maximum number of download attempts
while (getfile and inum<itrymax):
try:
getfile=False
host.download(remotefile,localfile,mode='b')
except IOError:
print "IOError on download of file {0}".format(remotefile)
getfile = True
inum =+ 1
#
# once file downloaded, then untar it and delete the tar file
#
print "Untarring {0}".format(f)
tarf = tarfile.open(f)
try:
tarf.extractall()
except:
print "ERROR UNTARRING {0}".format(localfile)
inum = 0
print "Deleting {0}".format(f)
os.remove(f)
if inum >=itrymax:
print "\nCould not retrieve {0} after {1} tries; Returning\n".format({remotefile, inum})
In [26]:
#
# don't forget the manifest and readme files
#
manfile = [f for f in ChandraCaldbFiles if 'MANIFEST' in f]
manfile = manfile[0]
readme = [f for f in ChandraCaldbFiles if 'README' in f]
readme = readme[0]
print manfile
print readme
In [31]:
maincaldb=[f for f in ChandraCaldbTarFiles if 'main' in f]
maincaldb = maincaldb[0]
tarf = tarfile.open(maincaldb)
In [32]:
tarf.extractall()
In [5]:
os.chdir(CaldbWorkDir)
In [9]:
acisbkg = [f for f in ChandraCaldbFiles if 'acis_bkgrnd' in f]
acisbkg = acisbkg[0]
tarf = tarfile.open(acisbkg)
tarf.extractall()
In [ ]:
hribkg = [f for f in ChandraCaldbFiles if 'hri_bkgrnd' in f]
hribkg = hribkg[0]
tarf = tarfile.open(hribkg)
tarf.extractall()
In [10]:
os.chdir('/Users/corcoran')
In [11]:
!pwd
In [ ]:
In [ ]: