/
Executor.java
547 lines (512 loc) · 28.9 KB
/
Executor.java
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package ro.cs.products;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import ro.cs.products.base.AbstractSearch;
import ro.cs.products.base.DownloadMode;
import ro.cs.products.base.ProductDescriptor;
import ro.cs.products.base.SensorType;
import ro.cs.products.base.TileMap;
import ro.cs.products.landsat.CollectionCategory;
import ro.cs.products.landsat.LandsatAWSSearch;
import ro.cs.products.landsat.LandsatCollection;
import ro.cs.products.landsat.LandsatProductDescriptor;
import ro.cs.products.landsat.LandsatProductDownloader;
import ro.cs.products.landsat.LandsatTilesMap;
import ro.cs.products.sentinel2.ProductStore;
import ro.cs.products.sentinel2.ProductType;
import ro.cs.products.sentinel2.S2L1CProductDescriptor;
import ro.cs.products.sentinel2.Sentinel2PreOpsDownloader;
import ro.cs.products.sentinel2.SentinelProductDownloader;
import ro.cs.products.sentinel2.SentinelTilesMap;
import ro.cs.products.sentinel2.amazon.AmazonSearch;
import ro.cs.products.sentinel2.angles.FillAnglesMethod;
import ro.cs.products.sentinel2.angles.ProductInspector;
import ro.cs.products.sentinel2.scihub.PreOpsSciHubSearch;
import ro.cs.products.sentinel2.scihub.SciHubSearch;
import ro.cs.products.util.Constants;
import ro.cs.products.util.Logger;
import ro.cs.products.util.NetUtils;
import ro.cs.products.util.Polygon2D;
import ro.cs.products.util.ReturnCode;
import ro.cs.products.util.Utilities;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Executor execution class.
*
* @author Cosmin Cara
*/
public class Executor {
private static Options options;
private static Properties props;
private static String version;
private static BatchProgressListener batchProgressListener;
private static ProgressListener fileProgressListener;
static {
options = new Options();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Executor.class.getResourceAsStream("parameters.def")))) {
HashMap<String, List<String[]>> groups = new HashMap<>();
reader.lines()
.filter(l -> {
String trimmed = l.trim();
return !trimmed.startsWith("#") &&
!(trimmed.startsWith("\r") || trimmed.startsWith("\n"));
})
.forEach(s -> {
String[] tokens = s.split(";");
if (tokens.length == 8) {
String key = tokens[0].trim();
if (!groups.containsKey(key)) {
groups.put(key, new ArrayList<>());
}
groups.get(key).add(tokens);
}
});
for (String key : groups.keySet()) {
if ("n/a".equals(key)) {
groups.get(key).forEach(values -> options.addOption(buildOption(values)));
} else {
OptionGroup group = new OptionGroup();
groups.get(key).forEach(values -> group.addOption(buildOption(values)));
options.addOptionGroup(group);
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(Integer.MAX_VALUE);
}
props = new Properties();
try {
props.load(Executor.class.getResourceAsStream("download.properties"));
version = props.getProperty("version");
} catch (IOException ignored) {
}
}
public static void main(String[] args) throws Exception {
if (args.length == 0) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("ProductDownload-" + version, options);
System.exit(0);
}
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args);
System.exit(execute(commandLine));
}
public static int execute(String[] args) throws Exception {
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args);
return execute(commandLine);
}
private static Option buildOption(String[] values) {
Option.Builder optionBuilder = Option.builder(values[1].trim())
.longOpt(values[2].trim())
.argName(values[4].trim())
.desc(values[7].trim())
.required(!Boolean.parseBoolean(values[6].trim()));
String cardinality = values[3].trim();
switch (cardinality) {
case "1":
optionBuilder.hasArg();
break;
case "n":
optionBuilder.hasArgs().valueSeparator(values[5].trim().charAt(1));
break;
default:
break;
}
return optionBuilder.build();
}
private static int execute(CommandLine commandLine) throws Exception {
int retCode = ReturnCode.OK;
String logFile = props.getProperty("master.log.file");
String folder;
boolean debugMode = getArgValue(commandLine, Constants.PARAM_VERBOSE, Boolean.class, false);
boolean searchMode = getArgValue(commandLine, Constants.PARAM_SEARCH_ONLY, Boolean.class, false);
Logger.CustomLogger logger;
SensorType sensorType = getArgValue(commandLine, Constants.PARAM_SENSOR, SensorType.class, SensorType.S2);
ProductType productType = getArgValue(commandLine, Constants.PARAM_S2_PRODUCT_TYPE, ProductType.class, ProductType.S2MSI1C);
DownloadMode downloadMode = getArgValue(commandLine, Constants.PARAM_DOWNLOAD_MODE, DownloadMode.class, DownloadMode.OVERWRITE);
LandsatCollection l8collection = null;
if (sensorType == SensorType.L8) {
l8collection = getArgValue(commandLine, Constants.PARAM_L8_COLLECTION, LandsatCollection.class, LandsatCollection.C1);
}
if (commandLine.hasOption(Constants.PARAM_INPUT_FOLDER) &&
(downloadMode != DownloadMode.COPY && downloadMode != DownloadMode.SYMLINK
&& downloadMode != DownloadMode.FILTERED_SYMLINK)) {
folder = commandLine.getOptionValue(Constants.PARAM_INPUT_FOLDER);
Utilities.ensureExists(Paths.get(folder));
Logger.initialize(Paths.get(folder, logFile).toAbsolutePath().toString(), debugMode);
logger = Logger.getRootLogger();
if (commandLine.hasOption(Constants.PARAM_VERBOSE)) {
printCommandLine(commandLine);
}
if (sensorType == SensorType.L8) {
logger.warn("Argument --input will be ignored for Landsat8");
} else {
String rootFolder = commandLine.getOptionValue(Constants.PARAM_INPUT_FOLDER);
FillAnglesMethod fillAnglesMethod = Enum.valueOf(FillAnglesMethod.class,
commandLine.hasOption(Constants.PARAM_FILL_ANGLES) ?
commandLine.getOptionValue(Constants.PARAM_FILL_ANGLES).toUpperCase() :
FillAnglesMethod.NONE.name());
if (!FillAnglesMethod.NONE.equals(fillAnglesMethod)) {
try {
Set<String> products = null;
if (commandLine.hasOption(Constants.PARAM_PRODUCT_LIST)) {
products = new HashSet<>();
for (String product : commandLine.getOptionValues(Constants.PARAM_PRODUCT_LIST)) {
if (!product.endsWith(".SAFE")) {
products.add(product + ".SAFE");
} else {
products.add(product);
}
}
}
ProductInspector inspector = new ProductInspector(rootFolder, fillAnglesMethod, products);
inspector.traverse();
} catch (IOException e) {
logger.error(e.getMessage());
retCode = ReturnCode.DOWNLOAD_ERROR;
}
}
}
} else {
folder = commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER);
Utilities.ensureExists(Paths.get(folder));
Logger.initialize(Paths.get(folder, logFile).toAbsolutePath().toString(), debugMode);
logger = Logger.getRootLogger();
printCommandLine(commandLine);
String proxyType = getArgValue(commandLine, Constants.PARAM_PROXY_TYPE, String.class,
nullIfEmpty(props.getProperty("proxy.type", null)));
String proxyHost = getArgValue(commandLine, Constants.PARAM_PROXY_HOST, String.class,
nullIfEmpty(props.getProperty("proxy.host", null)));
String proxyPort = getArgValue(commandLine, Constants.PARAM_PROXY_PORT, String.class,
nullIfEmpty(props.getProperty("proxy.port", null)));
String proxyUser = getArgValue(commandLine, Constants.PARAM_PROXY_USER, String.class,
nullIfEmpty(props.getProperty("proxy.user", null)));
String proxyPwd = getArgValue(commandLine, Constants.PARAM_PROXY_PASSWORD, String.class,
nullIfEmpty(props.getProperty("proxy.pwd", null)));
NetUtils.setProxy(proxyType, proxyHost, proxyPort == null ? 0 : Integer.parseInt(proxyPort), proxyUser, proxyPwd);
List<ProductDescriptor> products = new ArrayList<>();
Set<String> tiles = new HashSet<>();
Polygon2D areaOfInterest = new Polygon2D();
ProductStore source = getArgValue(commandLine, Constants.PARAM_DOWNLOAD_STORE, ProductStore.class, ProductStore.SCIHUB);
if (sensorType == SensorType.S2 && !commandLine.hasOption(Constants.PARAM_FLAG_SEARCH_AWS) && !commandLine.hasOption(Constants.PARAM_USER)) {
throw new MissingOptionException("Missing SciHub credentials");
}
String user = commandLine.getOptionValue(Constants.PARAM_USER);
String pwd = commandLine.getOptionValue(Constants.PARAM_PASSWORD);
NetUtils sciHubNetUtils = new NetUtils();
if (user != null && pwd != null && !user.isEmpty() && !pwd.isEmpty()) {
String authToken = "Basic " + new String(Base64.getEncoder().encode((user + ":" + pwd).getBytes()));
sciHubNetUtils.setAuthToken(authToken);
}
ProductDownloader downloader = sensorType.equals(SensorType.S2) ?
new SentinelProductDownloader(source, commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER),
props, sciHubNetUtils) :
new LandsatProductDownloader(commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER), props);
downloader.setDownloadMode(downloadMode);
String archive = getArgValue(commandLine, Constants.PARAM_INPUT_FOLDER, String.class, null);
if (archive != null) {
downloader.overrideBaseUrl(archive);
}
TileMap tileMap = sensorType == SensorType.S2 ?
SentinelTilesMap.getInstance() :
LandsatTilesMap.getInstance();
if (commandLine.hasOption(Constants.PARAM_TILE_SHAPE_FILE)) {
String tileShapeFile = commandLine.getOptionValue(Constants.PARAM_TILE_SHAPE_FILE);
if (Files.exists(Paths.get(tileShapeFile))) {
logger.debug(String.format("Reading %s tiles extents", sensorType));
tileMap.fromKmlFile(tileShapeFile);
logger.debug(String.valueOf(tileMap.getCount() + " tiles found"));
}
} else if (tileMap.getCount() == 0) {
logger.debug(String.format("Loading %s tiles extents", sensorType));
tileMap.read(Executor.class.getResourceAsStream(sensorType + "tilemap.dat"));
logger.debug(String.valueOf(tileMap.getCount() + " tile extents loaded"));
}
if (commandLine.hasOption(Constants.PARAM_AREA)) {
String[] points = commandLine.getOptionValues(Constants.PARAM_AREA);
for (String point : points) {
areaOfInterest.append(Double.parseDouble(point.substring(0, point.indexOf(","))),
Double.parseDouble(point.substring(point.indexOf(",") + 1)));
}
} else if (commandLine.hasOption(Constants.PARAM_AREA_FILE)) {
areaOfInterest = Polygon2D.fromWKT(new String(Files.readAllBytes(Paths.get(commandLine.getOptionValue(Constants.PARAM_AREA_FILE))), StandardCharsets.UTF_8));
} else if (commandLine.hasOption(Constants.PARAM_PRODUCT_LIST)) {
String[] productNames = commandLine.getOptionValues(Constants.PARAM_PRODUCT_LIST);
for (String productName : productNames) {
ProductDescriptor productDescriptor = sensorType == SensorType.S2 ?
new S2L1CProductDescriptor(productName) :
new LandsatProductDescriptor(productName);
products.add(productDescriptor);
}
} else if (commandLine.hasOption(Constants.PARAM_PRODUCT_LIST_FILE)) {
for (String line : Files.readAllLines(Paths.get(commandLine.getOptionValue(Constants.PARAM_PRODUCT_LIST_FILE)))) {
products.add(sensorType == SensorType.S2 ?
new S2L1CProductDescriptor(line) :
new LandsatProductDescriptor(line));
}
}
if (commandLine.hasOption(Constants.PARAM_TILE_LIST)) {
Collections.addAll(tiles, commandLine.getOptionValues(Constants.PARAM_TILE_LIST));
} else if (commandLine.hasOption(Constants.PARAM_TILE_LIST_FILE)) {
tiles.addAll(Files.readAllLines(Paths.get(commandLine.getOptionValue(Constants.PARAM_TILE_LIST_FILE))));
}
double clouds = getArgValue(commandLine, Constants.PARAM_CLOUD_PERCENTAGE, Double.class, Constants.DEFAULT_CLOUD_PERCENTAGE);
String sensingStart;
if (commandLine.hasOption(Constants.PARAM_START_DATE)) {
String dateString = commandLine.getOptionValue(Constants.PARAM_START_DATE);
LocalDate startDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);
long days = ChronoUnit.DAYS.between(startDate, LocalDate.now());
sensingStart = String.format(Constants.PATTERN_START_DATE, days);
} else {
sensingStart = Constants.DEFAULT_START_DATE;
}
String sensingEnd;
if (commandLine.hasOption(Constants.PARAM_END_DATE)) {
String dateString = commandLine.getOptionValue(Constants.PARAM_END_DATE);
LocalDate endDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);
long days = ChronoUnit.DAYS.between(endDate, LocalDate.now());
sensingEnd = String.format(Constants.PATTERN_START_DATE, days);
} else {
sensingEnd = Constants.DEFAULT_END_DATE;
}
int limit = getArgValue(commandLine, Constants.PARAM_RESULTS_LIMIT, Integer.class, Constants.DEFAULT_RESULTS_LIMIT);
ProductStore store = getArgValue(commandLine, Constants.PARAM_DOWNLOAD_STORE, ProductStore.class, ProductStore.SCIHUB);
downloader.setDownloadStore(store);
if (!searchMode) {
logger.info("Download will be attempted from %s", store);
}
downloader.shouldCompress(commandLine.hasOption(Constants.PARAM_FLAG_COMPRESS));
downloader.shouldDeleteAfterCompression(commandLine.hasOption(Constants.PARAM_FLAG_DELETE));
if (commandLine.hasOption(Constants.PARAM_FILL_ANGLES)) {
if (downloader instanceof SentinelProductDownloader) {
((SentinelProductDownloader) downloader).setFillMissingAnglesMethod(Enum.valueOf(FillAnglesMethod.class,
commandLine.hasOption(Constants.PARAM_FILL_ANGLES) ?
commandLine.getOptionValue(Constants.PARAM_FILL_ANGLES).toUpperCase() :
FillAnglesMethod.NONE.name()));
} else {
logger.warn("Argument --ma will be ignored for Landsat8");
}
}
int numPoints = areaOfInterest.getNumPoints();
tiles = tiles.stream().map(t -> t.startsWith("T") ? t.substring(1) : t).collect(Collectors.toSet());
if (products.size() == 0 && numPoints == 0 && tileMap.getCount() > 0) {
Rectangle2D rectangle2D = tileMap.boundingBox(tiles);
areaOfInterest.append(rectangle2D.getX(), rectangle2D.getY());
areaOfInterest.append(rectangle2D.getMaxX(), rectangle2D.getY());
areaOfInterest.append(rectangle2D.getMaxX(), rectangle2D.getMaxY());
areaOfInterest.append(rectangle2D.getX(), rectangle2D.getMaxY());
areaOfInterest.append(rectangle2D.getX(), rectangle2D.getY());
}
boolean searchPreOps = getArgValue(commandLine, Constants.PARAM_FLAG_PREOPS, Boolean.class, false);
searchPreOps &= !commandLine.hasOption(Constants.PARAM_FLAG_SEARCH_AWS);
boolean setAdditionalFilters = products.size() == 0;
String searchUrl;
AbstractSearch searchProvider;
logger.debug("No product provided, searching on the AOI");
if (sensorType == SensorType.L8) {
logger.info("Search will be attempted on AWS");
searchUrl = l8collection != null && l8collection.equals(LandsatCollection.C1) ?
props.getProperty(Constants.PROPERTY_NAME_LANDSAT_AWS_SEARCH_URL, Constants.PROPERTY_NAME_DEFAULT_LANDSAT_SEARCH_URL) :
props.getProperty(Constants.PROPERTY_NAME_LANDSAT_SEARCH_URL, Constants.PROPERTY_NAME_DEFAULT_LANDSAT_SEARCH_URL);
NetUtils netUtils = new NetUtils();
if (!netUtils.isAvailable(searchUrl)) {
logger.warn(searchUrl + " is not available!");
}
searchProvider = new LandsatAWSSearch(searchUrl);
if (commandLine.hasOption(Constants.PARAM_START_DATE)) {
searchProvider.setSensingStart(commandLine.getOptionValue(Constants.PARAM_START_DATE));
}
if (commandLine.hasOption(Constants.PARAM_END_DATE)) {
searchProvider.setSensingEnd(commandLine.getOptionValue(Constants.PARAM_END_DATE));
}
if (commandLine.hasOption(Constants.PARAM_TILE_LIST)) {
searchProvider.setTiles(tiles);
}
if (commandLine.hasOption(Constants.PARAM_L8_PRODUCT_TYPE)) {
searchProvider.setProductType(Enum.valueOf(CollectionCategory.class,
commandLine.getOptionValue(Constants.PARAM_L8_PRODUCT_TYPE)));
}
} else if (!commandLine.hasOption(Constants.PARAM_FLAG_SEARCH_AWS)) {
logger.info("Search will be attempted on SciHub");
searchUrl = props.getProperty(Constants.PROPERTY_NAME_SEARCH_URL, Constants.PROPERTY_DEFAULT_SEARCH_URL);
if (!sciHubNetUtils.isAvailable(searchUrl)) {
logger.warn(searchUrl + " is not available!");
searchUrl = props.getProperty(Constants.PROPERTY_NAME_SEARCH_URL_SECONDARY, Constants.PROPERTY_DEFAULT_SEARCH_URL_SECONDARY);
}
searchProvider = new SciHubSearch(searchUrl, productType);
SciHubSearch search = (SciHubSearch) searchProvider;
if (user != null && !user.isEmpty() && pwd != null && !pwd.isEmpty()) {
searchProvider = searchProvider.auth(user, pwd);
}
if (setAdditionalFilters) {
String interval = "[" + sensingStart + " TO " + sensingEnd + "]";
search.filter(Constants.SEARCH_PARAM_INTERVAL, interval).limit(limit);
if (commandLine.hasOption(Constants.PARAM_RELATIVE_ORBIT)) {
search.filter(Constants.SEARCH_PARAM_RELATIVE_ORBIT_NUMBER, commandLine.getOptionValue(Constants.PARAM_RELATIVE_ORBIT));
}
}
} else {
logger.info("Search will be attempted on AWS");
searchUrl = props.getProperty(Constants.PROPERTY_NAME_AWS_SEARCH_URL, Constants.PROPERTY_DEFAULT_AWS_SEARCH_URL);
searchProvider = new AmazonSearch(searchUrl);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.add(Calendar.DAY_OF_MONTH, Integer.parseInt(sensingStart.replace("NOW", "").replace("DAY", "")));
searchProvider.setSensingStart(dateFormat.format(calendar.getTime()));
calendar = Calendar.getInstance();
String endOffset = sensingEnd.replace("NOW", "").replace("DAY", "");
int offset = endOffset.isEmpty() ? 0 : Integer.parseInt(endOffset);
calendar.add(Calendar.DAY_OF_MONTH, offset);
searchProvider.setSensingEnd(dateFormat.format(calendar.getTime()));
if (commandLine.hasOption(Constants.PARAM_RELATIVE_ORBIT)) {
searchProvider.setOrbit(Integer.parseInt(commandLine.getOptionValue(Constants.PARAM_RELATIVE_ORBIT)));
}
}
if (searchProvider.getTiles() == null || searchProvider.getTiles().size() == 0) {
searchProvider.setAreaOfInterest(areaOfInterest);
}
searchProvider.filter(products);
searchProvider.setTiles(tiles);
searchProvider.setClouds(clouds);
if (searchPreOps) {
String preOpsSearchUrl = props.getProperty(Constants.PROPERTY_NAME_SEARCH_PREOPS_URL, Constants.PROPERTY_DEFAULT_SEARCH_PREOPS_URL);
NetUtils preOpsNetUtils = new NetUtils();
String authToken = "Basic " + new String(Base64.getEncoder().encode(("s2bguest:s2bguest").getBytes()));
preOpsNetUtils.setAuthToken(authToken);
if (!preOpsNetUtils.isAvailable(preOpsSearchUrl)) {
logger.warn(preOpsSearchUrl + " is not available!");
} else {
PreOpsSciHubSearch secondarySearch = new PreOpsSciHubSearch(preOpsSearchUrl, productType);
secondarySearch.auth("s2bguest", "s2bguest");
secondarySearch.copyFiltersFrom(searchProvider);
searchProvider.setAdditionalProvider(secondarySearch);
}
}
searchProvider.setRetrieveAllPages(commandLine.hasOption("all"));
products = searchProvider.execute();
if (searchMode) {
Path resultFile = Paths.get(folder).resolve("results.txt");
Files.write(resultFile,
products.stream()
.map(ProductDescriptor::getName)
.collect(Collectors.toList())
);
}
if (!searchMode) {
if (downloader instanceof SentinelProductDownloader) {
SentinelProductDownloader sentinelProductDownloader = (SentinelProductDownloader) downloader;
sentinelProductDownloader.setFilteredTiles(tiles, commandLine.hasOption(Constants.PARAM_FLAG_UNPACKED));
if (searchPreOps) {
NetUtils preOpsNetUtils = new NetUtils();
String authToken = "Basic " + new String(Base64.getEncoder().encode(("s2bguest:s2bguest").getBytes()));
preOpsNetUtils.setAuthToken(authToken);
Sentinel2PreOpsDownloader additional =
new Sentinel2PreOpsDownloader(source,
commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER),
props, preOpsNetUtils);
additional.copyOptionsFrom(sentinelProductDownloader);
sentinelProductDownloader.setAdditionalDownloader(additional);
}
}
if (commandLine.hasOption(Constants.PARAM_BAND_LIST)) {
downloader.setBandList(commandLine.getOptionValues(Constants.PARAM_BAND_LIST));
}
downloader.setProgressListener(batchProgressListener);
downloader.setFileProgressListener(fileProgressListener);
retCode = downloader.downloadProducts(products);
}
}
return retCode;
}
public static void setProgressListener(BatchProgressListener progressListener) {
batchProgressListener = progressListener;
}
public static void setFileProgressListener(ProgressListener progressListener) { fileProgressListener = progressListener; }
private static void printCommandLine(CommandLine cmd) {
Logger.getRootLogger().debug("Executing with the following arguments:");
for (Option option : cmd.getOptions()) {
if (option.hasArgs()) {
Logger.getRootLogger().debug(option.getOpt() + "=" + String.join(" ", option.getValues()));
} else if (option.hasArg()) {
Logger.getRootLogger().debug(option.getOpt() + "=" + option.getValue());
} else {
Logger.getRootLogger().debug(option.getOpt());
}
}
}
private static String nullIfEmpty(String string) {
return string != null ? (string.isEmpty() ? null : string) : null;
}
private static <T> T getArgValue(CommandLine cmd, String argName, Class<T> clazz, T defaultValue) {
if (!cmd.hasOption(argName)) {
return defaultValue;
} else {
final String optionValue = cmd.getOptionValue(argName);
if (Boolean.class.isAssignableFrom(clazz)) {
return clazz.cast(true);
} else if (Integer.class.isAssignableFrom(clazz)){
return clazz.cast(Integer.parseInt(optionValue));
} else if (Double.class.isAssignableFrom(clazz)) {
return clazz.cast(Double.parseDouble(optionValue));
} else {
return clazz.cast(optionValue);
}
}
}
private static <T extends Enum<T>> T getArgValue(CommandLine cmd, String argName, Class<T> clazz, T defaultValue) {
if (!cmd.hasOption(argName)) {
return defaultValue;
} else {
return Enum.valueOf(clazz, cmd.getOptionValue(argName));
}
}
private static String[] getArgValues(CommandLine cmd, String argName) {
return cmd.hasOption(argName) ? cmd.getOptionValues(argName) : null;
}
}