-
Notifications
You must be signed in to change notification settings - Fork 41
/
get_laads.py
executable file
·166 lines (144 loc) · 4.2 KB
/
get_laads.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
import json
import logging
import optparse
import sys
from concurrent import futures
from pathlib import Path
import requests
from tqdm.auto import tqdm
__author__ = "J Gomez-Dans"
__copyright__ = "Copyright 2020 J Gomez-Dans"
__version__ = "1.0.0"
__license__ = "GPLv3"
__email__ = "[email protected]"
HELP_TEXT = """
SYNOPSIS
./get_laads.py [-h,--help]
[--verbose, -v]
[--product=PRODUCT, -p PRODUCT] [--year=YEAR, -y YEAR]
[--output=DIR_OUT, -o DIR_OUT] [--doys=DOY,DOY, -b DOY,DOY]
DESCRIPTION
A program to download MODIS data from the LAADS website using the HTTP
transport.
EXIT STATUS
No exit status yet, can't be bothered.
AUTHOR
J Gomez-Dans <[email protected]>
See also http://github.com/jgomezdans/get_modis/
"""
LOG = logging.getLogger(__name__)
OUT_HDLR = logging.StreamHandler(sys.stdout)
OUT_HDLR.setFormatter(logging.Formatter("%(asctime)s %(message)s"))
OUT_HDLR.setLevel(logging.INFO)
LOG.addHandler(OUT_HDLR)
LOG.setLevel(logging.DEBUG)
HEADERS = {"User-Agent": "get_modis Python %s" % __version__}
CHUNKS = 65536
URL = "https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/61"
def download_products(url, loc):
fname = loc / url.split("/")[-1]
r = requests.get(url)
with fname.open(mode="wb") as fp:
fp.write(r.content)
LOG.debug("Saved " + str(fname))
def download_filelist(url):
r = requests.get(url)
return json.loads(r.content)
def make_query(location, product, years, doys, n_threads=8):
location = Path(location)
grabber = lambda x: download_products(x, location)
if not location.exists():
raise IOError(f"Destination folder {location} does not exist!")
if type(doys) != list:
doys = [
doys,
]
if type(years) != list:
years = [
years,
]
for year in years:
LOG.info(f"Doing year {year}")
urls = [f"{URL}/{product}/{year}/{doy:03d}.json" for doy in doys]
dload_files = []
with futures.ThreadPoolExecutor(max_workers=n_threads) as executor:
dload_files = list(
tqdm(executor.map(download_filelist, urls), total=len(urls))
)
datas = list(zip(doys, dload_files))
req_products = [
f"{URL}/{years}/{doy:03}/{x['name']}"
for doy, z in datas
for x in z
]
LOG.info(
f"\tWill now download {len(req_products)} products in total..."
)
with futures.ThreadPoolExecutor(max_workers=n_threads) as executor:
dload_files = list(
tqdm(
executor.map(grabber, req_products),
total=len(req_products),
)
)
LOG.info("\tDone with this year!")
def main():
parser = optparse.OptionParser(
formatter=optparse.TitledHelpFormatter(), usage=HELP_TEXT
)
parser.add_option(
"-v",
"--verbose",
action="store_true",
default=False,
help="verbose output",
)
parser.add_option(
"-p",
"--product",
action="store",
dest="product",
type=str,
help="MODIS product name "
+ "(e.g. MOD05_L2)",
)
parser.add_option(
"-o",
"--output",
action="store",
dest="dir_out",
default=".",
type=str,
help="Output directory",
)
parser.add_option(
"-y",
"--year",
action="store",
dest="year",
type=str,
help="Years to consider (comma-separated)",
)
parser.add_option(
"-d",
"--doys",
action="store",
dest="doys",
type=str,
default=None,
help="Doys to consider (comma-separated)",
)
(options, args) = parser.parse_args()
if options.verbose:
LOG.setLevel(logging.DEBUG)
else:
LOG.setLevel(logging.INFO)
product = options.product.upper()
years = list(map(int, options.year.split(",")))
doys = list(map(int, options.doys.split(",")))
LOG.info("MODIS downloader by J Gomez-Dans...")
LOG.info("Starting downloading")
make_query(options.dir_out, product, years, doys)
if __name__ == "__main__":
main()