Created
August 11, 2011 05:25
-
-
Save philipn/1138957 to your computer and use it in GitHub Desktop.
GeoTIFF metadata / ftp download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This likely isn't useful to anybody else. But it's an example of how to | |
adapt python's ftplib to grab just the first few kb of a given file. | |
In this case, all we need is the first few kb to get the metadata from a | |
GeoTIFF file. | |
""" | |
import ftplib | |
MAX_FILE_SIZE = 8192 * 12 | |
file_list = [] | |
cur_file = None | |
cur_filename = None | |
cur_file_size = 0 | |
class FTP(ftplib.FTP): | |
def retrbinary(self, cmd, callback, blocksize=8192, rest=None): | |
global cur_file_size | |
self.voidcmd('TYPE I') | |
conn = self.transfercmd(cmd, rest) | |
while 1: | |
data = conn.recv(blocksize) | |
if not data: | |
break | |
callback(data) | |
cur_file_size += blocksize | |
if cur_file_size > MAX_FILE_SIZE: | |
cur_file_size = 0 | |
break | |
conn.close() | |
self.abort() | |
return self.voidresp() | |
def process_line(line): | |
file_list.append(line.split()[-1]) | |
def handleDownload(block): | |
global cur_file, cur_file_size, cur_filename | |
if cur_file is None: | |
cur_file = open(cur_filename, 'w') | |
cur_file.write(block) | |
def download(dirnum): | |
global cur_filename, cur_file | |
ftp = FTP('n4ftl01u.ecs.nasa.gov') | |
ftp.login() | |
ftp.cwd('SAN2/ICEBRIDGE_FTP/IODMS1B_DMSgeotiff_v01/2009_AN_NASA/Flt_%s' % dirnum) | |
ftp.retrlines('LIST', process_line) | |
for filename in file_list: | |
cur_filename = filename | |
try: | |
ftp.retrbinary('RETR ' + filename, handleDownload) | |
cur_file.close() | |
cur_file = None | |
print "Got metadata for", filename | |
except Exception, s: | |
print str(s) | |
continue | |
for x in xrange(1000109, 1000130): | |
download(x) | |
download(1000133) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment