-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModisLADSFTP.py
91 lines (77 loc) · 2.89 KB
/
ModisLADSFTP.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
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""
May 2017
Tomás Acuña & Italo Moletto
mail: [email protected] // [email protected]
Modified from : http://www.science-emergence.com/Codes/Download-a-file-of-MODIS-MYD08-M3-from-ladsftp-using-python/
"""
from ftplib import FTP
import numpy as np
import os
import calendar
import glob
def ModisLADS(doy,year,version,time,product,MODIS_FOLD):
time='.'+time+'.'
# Create a local folder call MODIS
if not os.path.exists(MODIS_FOLD):
os.makedirs(MODIS_FOLD)
os.chdir(MODIS_FOLD)
####### Atention ############
# You must be login https://ladsweb.modaps.eosdis.nasa.gov/profile/login/ in order to connected to the FTP
####### Atention ############
for mm in range(0,len(product)):
prod_path=MODIS_FOLD + '\\' +product[mm]
# Create sub folder for each product
if not os.path.exists(prod_path):
os.makedirs(prod_path)
if not os.path.exists(prod_path+"\hdf"):
os.makedirs(prod_path+"\hdf")
os.chdir(prod_path+"\hdf")
else:
os.chdir(prod_path+"\hdf")
#----------------------------------------------------------------------------------------#
# download data from ladsftp
for yy in range(0,len(year)):
directory= 'allData/' + version[0] +'/' + product[mm] + '/' + str(year[yy]) + '/' + doy[0]
ftp = FTP('ladsftp.nascom.nasa.gov')
ftp.login('anonymous','')
ftp.cwd(directory)
yyy = []
ftp.retrlines('NLST', yyy.append)
# Search for specific time zone in the list of scenes
inx = [i for i, s in enumerate(yyy) if time in s]
ooo=["NA"]
ooo[0]=yyy[inx[0]]
if len(ooo) == 1:
file_name = ooo[0]
print('Download file: ', file_name)
e = 1
while e == 1 :
try:
ftp.retrbinary('RETR ' + file_name, open(file_name, 'wb').write)
e = 0
except:
print("Error while downloading")
#----------------------------------------------------------------------------------------#
# Closing FTP connection
print('Closing FTP connection')
ftp.close()
#Example
# Adquisition time of MODIS --> Review on Reverb or directly from the FTP
# example MOD07_L2.A2011001.2215.006.2015047180723.hdf
# Adquire at 2215 UT
# Day of year
doy = ['001']
# Years of data
year = [2010,2011]
# Product version
version=['6']
# version =['51','6']
time='2215'
#product to download
product=['MOD07_L2','MOD04_L2','MOD05_L2']
#create FOLDER
MODIS_FOLD='E:\MODIS'
MODIS_FOLD='C:\MODIS'
"""DOWNLOAD SELECTED IMAGERY"""
ModisLADS(doy,year,version,time,product,MODIS_FOLD)