Commit 5ff1c545d7e340bd3fcd91b95c1f04ba0e5490de

Authored by Olivier Hagolle
1 parent 6db456c5
Exists in master

Improvement : knowing the exact overpass day is not needed anymore

Showing 1 changed file with 71 additions and 23 deletions   Show diff stats
download_landsat_scene.py
... ... @@ -5,8 +5,10 @@
5 5 Landsat Data download from earth explorer
6 6 Incorporates jake-Brinkmann improvements
7 7 """
8   -import os,sys,math,urllib2,urllib,time
  8 +import os,sys,math,urllib2,urllib,time,math
9 9 import optparse
  10 +import datetime
  11 +
10 12 ###########################################################################
11 13 class OptionParser (optparse.OptionParser):
12 14  
... ... @@ -17,11 +19,10 @@ class OptionParser (optparse.OptionParser):
17 19 if getattr(self.values, option.dest) is None:
18 20 self.error("%s option not supplied" % option)
19 21  
20   -#############################"Connection à Earth explorer sans proxy
  22 +#############################"Connection to Earth explorer with proxy
21 23  
22 24 def connect_earthexplorer_proxy(proxy_info,usgs):
23 25  
24   - print "proxy_info=", proxy_info
25 26 # contruction d'un "opener" qui utilise une connexion proxy avec autorisation
26 27 proxy_support = urllib2.ProxyHandler({"http" : "http://%(user)s:%(pass)s@%(host)s:%(port)s" % proxy_info,
27 28 "https" : "http://%(user)s:%(pass)s@%(host)s:%(port)s" % proxy_info})
... ... @@ -47,7 +48,7 @@ def connect_earthexplorer_proxy(proxy_info,usgs):
47 48 return
48 49  
49 50  
50   -#############################"Connection à Earth explorer sans proxy
  51 +#############################"Connection to Earth explorer without proxy
51 52  
52 53 def connect_earthexplorer_no_proxy(usgs):
53 54 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
... ... @@ -121,10 +122,47 @@ def downloadChunks(url,rep,nom_fic):
121 122 return False
122 123  
123 124 return rep,nom_fic
  125 +
  126 +
  127 +##################
  128 +def cycle_day(path):
  129 + """ provides the day in cycle given the path number
  130 + """
  131 + cycle_day_path1 = 5
  132 + cycle_day_increment = 7
  133 + nb_days_after_day1=cycle_day_path1+cycle_day_increment*(path-1)
124 134  
  135 + cycle_day_path=math.fmod(nb_days_after_day1,16)
  136 + if path>=98: #change date line
  137 + cycle_day_path+=1
  138 + return(cycle_day_path)
  139 +
  140 +
  141 +
  142 +###################
  143 +def next_overpass(date1,path,sat):
  144 + """ provides the next overpass for path after date1
  145 + """
  146 + date0_L5 = datetime.datetime(1985,5,4)
  147 + date0_L7 = datetime.datetime(1999,1,11)
  148 + date0_L8 = datetime.datetime(2013,5,1)
  149 + if sat=='LT5':
  150 + date0=date0_L5
  151 + elif sat=='LE7':
  152 + date0=date0_L7
  153 + elif sat=='LC8':
  154 + date0=date0_L8
  155 + next_day=math.fmod((date1-date0).days-cycle_day(path)+1,16)
  156 + if next_day!=0:
  157 + date_overpass=date1+datetime.timedelta(16-next_day)
  158 + else:
  159 + date_overpass=date1
  160 + return(date_overpass)
  161 +
  162 +
  163 +######################################################################################
  164 +############### main ########################
125 165 ######################################################################################
126   -###############main
127   -#################################################################################
128 166  
129 167 ################Lecture des arguments
130 168 if len(sys.argv) == 1:
... ... @@ -144,12 +182,10 @@ else:
144 182 help="liste filename",default=None)
145 183 parser.add_option("-s", "--scene", dest="scene", action="store", type="string", \
146 184 help="coordonnees WRS2 de la scene (ex 198030)", default=None)
147   - parser.add_option("-a", "--annee", dest="annee", action="store", type="int", \
148   - help="annee")
149   - parser.add_option("-d", "--doy_deb", dest="doy_deb", action="store", type="int", \
150   - help="first landsat day of year for tile ")
151   - parser.add_option("-f","--doy_fin", dest="doy_fin", action="store", type="int", \
152   - help="last day of year for tile")
  185 + parser.add_option("-d", "--start_date", dest="start_date", action="store", type="string", \
  186 + help="start date, fmt('20131223')")
  187 + parser.add_option("-f","--end_date", dest="end_date", action="store", type="string", \
  188 + help="end date, fmt('20131223')")
153 189 parser.add_option("-u","--usgs_passwd", dest="usgs", action="store", type="string", \
154 190 help="USGS earthexplorer account and password file")
155 191 parser.add_option("-p","--proxy_passwd", dest="proxy", action="store", type="string", \
... ... @@ -161,8 +197,6 @@ else:
161 197 parser.check_required("-o")
162 198 if options.option=='scene':
163 199 parser.check_required("-d")
164   - parser.check_required("-a")
165   - parser.check_required("-f")
166 200 parser.check_required("-s")
167 201 parser.check_required("-u")
168 202  
... ... @@ -186,12 +220,13 @@ try:
186 220 if passwd.endswith('\n'):
187 221 passwd=passwd[:-1]
188 222 usgs={'account':account,'passwd':passwd}
189   - print usgs
190 223 f.close()
191 224 except :
192 225 print "error with usgs password file"
193 226 sys.exit(-2)
194 227  
  228 +
  229 +
195 230 if options.proxy != None :
196 231 try:
197 232 f=file(options.proxy)
... ... @@ -209,27 +244,40 @@ if options.proxy != None :
209 244 except :
210 245 print "error with proxy password file"
211 246 sys.exit(-3)
  247 +
212 248 ############Telechargement des produits par scene
213 249 if options.option=='scene':
214 250 produit='LC8'
215 251 station='LGN'
216 252 path=options.scene[0:3]
217 253 row=options.scene[3:6]
  254 +
  255 + year_start =int(options.start_date[0:4])
  256 + month_start=int(options.start_date[4:6])
  257 + day_start =int(options.start_date[6:8])
  258 + date_start=datetime.datetime(year_start,month_start, day_start)
218 259  
  260 + if options.end_date!= None:
  261 + year_end =int(options.end_date[0:4])
  262 + month_end=int(options.end_date[4:6])
  263 + day_end =int(options.end_date[6:8])
  264 + date_end =datetime.datetime(year_end,month_end, day_end)
  265 + else:
  266 + date_end=datetime.datetime.now()
219 267  
220   -
221 268 rep_scene="%s/SCENES/%s_%s/GZ"%(rep,path,row)
222 269 print rep_scene
223 270 if not(os.path.exists(rep_scene)):
224 271 os.makedirs(rep_scene)
225 272 if produit.startswith('LC8'):repert='4923'
226 273 if produit.startswith('LE7'):repert='3373'
  274 +
  275 + curr_date=next_overpass(date_start,int(path),produit)
227 276  
228   - doy=options.doy_deb
229   -
230   - while (doy < options.doy_fin) and (doy < 366) :
231   - date_asc="%04d%03d"%(options.annee,doy)
232   - doy+=16
  277 + while (curr_date < date_end) :
  278 + date_asc=curr_date.strftime("%Y%j")
  279 + print date_asc
  280 + curr_date=curr_date+datetime.timedelta(16)
233 281 for version in ['00','01']:
234 282 nom_prod=produit+options.scene+date_asc+station+version
235 283 url="http://earthexplorer.usgs.gov/download/%s/%s/STANDARD/EE"%(repert,nom_prod)
... ... @@ -242,9 +290,9 @@ if options.option==&#39;scene&#39;:
242 290 try:
243 291 downloadChunks(url,"%s"%rep_scene,nom_prod+'.tgz')
244 292 except TypeError:
245   - print ' produit %s non trouve'%nom_prod
  293 + print ' product %s not found'%nom_prod
246 294 else :
247   - print ' produit %s deja telecharge'%nom_prod
  295 + print ' product %s already downloaded'%nom_prod
248 296 break
249 297  
250 298 ############Telechargement par liste
... ...