-
Notifications
You must be signed in to change notification settings - Fork 0
/
modis_gdal.py
84 lines (61 loc) · 1.87 KB
/
modis_gdal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import ftplib
import gdal
import numpy as np
"""
Author: Pete
Date: 28th November 2012
Version: 0.0.0
{FEEDER} ~ Rewrite due to MRT dependance
~ Moving to GDAL for handling resampling, output and possible tile creation
~ totally beta but... Run the script and the tiles that cover the particular month and year
specified and it will download the files to where the script lies.
"""
data_dir = []
print 'Welcome to the Feeder : )'
ftp = ftplib.FTP("e4ftl01.cr.usgs.gov")
ftp.login("anonymous", "thisisme")
ftp.cwd("/MOLT/MOD09A1.005/")
print 'Loading data directories...'
ftp.dir(data_dir.append)
#print data_dir
dirnames = [i.split() [-1] for i in data_dir[1:]]
#print dirnames
dirinfo = np.array([ (len(k) == 3 and k) or [0,0,0] for k in [[ (unicode(j).isnumeric() and int(j)) or 0 for j in i.split()[-1].split('.')] for i in data_dir[1:]]])
#print dirinfo[:,0]
"""
Enter a year and month of interest
"""
###################################
aMonth = 11 # << EXAMPLE
aYear = 2012 # << EXAMPLE
###################################
yearMatch = dirinfo[:,0] == aYear
monthMatch = dirinfo[:,1] == aMonth
allMatch = yearMatch * monthMatch
w = np.where(allMatch)[0]
print 'Item numbers: ', w
print 'Year: %d' % aYear
print 'Month: %d' % aMonth
dir_i_want = np.array(dirnames)[w]
#print dir_i_want
def getDownload(block):
file.write(block)
tileOfInterest = 'h09v05'
for i in dir_i_want:
ftp.cwd(i)
this = []
ftp.dir(this.append)
filenames = [i.split() [-1] for i in this[1:]]
fileinfo = np.array([i.split('.') for i in this[1:]])
tileMatch = np.array([i[2] == tileOfInterest for i in fileinfo])
w = np.where(tileMatch) [0]
for f in np.array(filenames)[w]:
print 'Opening local file ' + f
file = open(f, 'wb')
print 'Getting ' + f
ftp.retrbinary('RETR %s'%f, getDownload)
ftp.cwd('..')
print 'Finishing up...'
ftp.quit()