-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownload.py
96 lines (67 loc) · 2.51 KB
/
Download.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
92
93
94
95
import os
import calendar
import datetime
def date_range(year,month):
"""
Get date range for a particular month and year.
---
Returns
- dateini: initial date [string, %Y-%m-%d]
- dateend final date [string, %Y-%m-%d]
"""
_, numdays = calendar.monthrange(year, month)
dateini = (datetime.date(year,month,1)).strftime("%Y-%m-%d")
dateend = (datetime.date(year,month,numdays)).strftime("%Y-%m-%d")
return dateini,dateend
def build_command(datemin,datemax,path,filename):
"""
Build python motuclient command to download data from
http://marine.copernicus.eu/services-portfolio/access-to-products
Adapted from Dante Napolitano's download_aviso.py script.
---
Return
A string with the command
"""
string ='python -m motuclient --motu http://my.cmems-du.eu/motu-web/Motu
--service-id SEALEVEL_GLO_PHY_L4_REP_OBSERVATIONS_008_047-DGF --product-id
dataset-duacs-rep-global-merged-allsat-phy-l4 --date-min ' + datemin + '
--date-max ' + datemax + ' --out-dir ' + path + ' --out-name ' + filename +
' --user <USER> --pwd <PASSWORD>'
return string
def download_data(download_command, datamin):
"""
Execute the python command in encoded in the string download_command
and logs whether the download was successful/unsuccessful.
Adapted from Dante Napolitano's download_aviso.py script.
"""
print(' ')
print('Downloading ' + datamin[:7] + ' data...')
print(' ')
out = os.system(download_command)
if out == 0:
print(' ')
print('Succesfully downloaded ' + datamin[:7] + ' data.')
print(' ')
else:
print(' ')
print('Failed to download ' + datamin[:7] + ' data.')
print(' ')
def create_directory(directory):
try:
os.stat(directory)
except:
os.mkdir(directory)
if __name__=="__main__":
create_directory('Data')
for year in range(1995,2019):
create_directory('./data/' + str(year))
for month in range(1,13):
if month < 10:
path = './Data/' + str(year) + '/0' + str(month)
else:
path = './Data/' + str(year) + '/' + str(month)
create_directory(path)
datemin, datemax = date_range(year,month)
filename = str(month) + '.zip'
download_command = build_command(datemin,datemax,path,filename)
download_data(download_command,datemin)