-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSnapGraphAdapter.java
More file actions
643 lines (582 loc) · 28.6 KB
/
SnapGraphAdapter.java
File metadata and controls
643 lines (582 loc) · 28.6 KB
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package com.bc.calvalus.processing.beam;
import com.bc.calvalus.commons.CalvalusLogger;
import com.bc.calvalus.commons.DateUtils;
import com.bc.calvalus.processing.JobConfigNames;
import com.bc.calvalus.processing.ProcessorFactory;
import com.bc.calvalus.processing.executable.PropertiesHandler;
import com.bc.calvalus.processing.l2.ProductFormatter;
import com.bc.calvalus.processing.xml.XmlConvertible;
import com.bc.ceres.binding.BindingException;
import com.bc.ceres.binding.ConversionException;
import com.bc.ceres.binding.PropertyContainer;
import com.bc.ceres.binding.ValidationException;
import com.bc.ceres.binding.dom.DefaultDomConverter;
import com.bc.ceres.binding.dom.DomConverter;
import com.bc.ceres.binding.dom.DomElement;
import com.bc.ceres.binding.dom.XppDomElement;
import com.bc.ceres.core.ProgressMonitor;
import com.bc.ceres.resource.ReaderResource;
import com.bc.ceres.resource.Resource;
import com.bc.ceres.resource.ResourceEngine;
import com.thoughtworks.xstream.io.xml.xppdom.XppDom;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
import org.apache.hadoop.mapreduce.MapContext;
import org.apache.velocity.VelocityContext;
import org.esa.snap.core.datamodel.MetadataAttribute;
import org.esa.snap.core.datamodel.MetadataElement;
import org.esa.snap.core.datamodel.Product;
import org.esa.snap.core.gpf.Operator;
import org.esa.snap.core.gpf.OperatorException;
import org.esa.snap.core.gpf.annotations.Parameter;
import org.esa.snap.core.gpf.annotations.ParameterBlockConverter;
import org.esa.snap.core.gpf.annotations.ParameterDescriptorFactory;
import org.esa.snap.core.gpf.graph.Graph;
import org.esa.snap.core.gpf.graph.GraphContext;
import org.esa.snap.core.gpf.graph.GraphException;
import org.esa.snap.core.gpf.graph.GraphIO;
import org.esa.snap.core.gpf.graph.GraphProcessor;
import org.esa.snap.core.gpf.graph.Header;
import org.esa.snap.core.gpf.graph.HeaderSource;
import org.esa.snap.core.gpf.graph.HeaderTarget;
import org.esa.snap.core.gpf.graph.Node;
import org.esa.snap.core.gpf.graph.NodeContext;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* A processor adapter that uses a SNAP GPF {@code Graph} to process input products.
*
* @author MarcoZ
*/
public class SnapGraphAdapter extends SubsetProcessorAdapter {
private static final DateFormat N1_TIME_FORMAT = DateUtils.createDateFormat("yyyyMMdd_HHmmss");
private static final DateFormat YMD_DIR_FORMAT = DateUtils.createDateFormat("yyyy/MM/dd");
private GraphContext graphContext;
private Product targetProduct;
private boolean shallSaveTarget;
public SnapGraphAdapter(MapContext mapContext) {
super(mapContext);
if (getConfiguration().get(JobConfigNames.CALVALUS_REGION_GEOMETRY) != null) {
if (getConfiguration().get("calvalus.system.snap.dataio.reader.tileHeight") == null) {
System.setProperty("snap.dataio.reader.tileHeight", "64");
getLogger().info("Setting tileHeight to 64 for graph subsetting");
}
if ((getConfiguration().get("calvalus.system.snap.dataio.reader.tileWidth") == null
|| "*".equals(getConfiguration().get("calvalus.system.snap.dataio.reader.tileWidth")))
&& !getConfiguration().getBoolean(JobConfigNames.CALVALUS_INPUT_FULL_SWATH, false)) {
System.setProperty("snap.dataio.reader.tileWidth", "64");
getLogger().info("Setting tileWidth to 64 for graph subsetting");
}
}
}
@Override
public boolean supportsPullProcessing() {
return true;
}
@Override
public boolean processSourceProduct(MODE mode, ProgressMonitor pm) throws IOException {
pm.setSubTaskName("SNAP Level 2");
try {
Graph graph = createGraph();
if (graph == null) {
getLogger().info("Skip processing");
return false;
}
Header header = graph.getHeader();
graphContext = buildGraphContext(graph, header);
logGraphState();
HeaderTarget target = header.getTarget();
XppDom calvalusAppData = graph.getApplicationData("calvalus");
if (mode == MODE.EXECUTE && calvalusAppData != null) {
boolean success = executeGraphAndCollectOutput(graph, calvalusAppData, pm);
Product[] outputProducts;
try {
outputProducts = graphContext.getOutputProducts();
} catch (NullPointerException e) {
outputProducts = new Product[0];
}
if (success && target != null && target.getNodeId() != null && outputProducts.length > 0) {
//graphContext.getOutputProducts()[0];
targetProduct = getTargetProductFromGraph(graph, target.getNodeId());
return postprocessTargetProduct();
}
return success;
} else {
if (target == null || target.getNodeId() == null) {
throw new IllegalArgumentException("no 'target' product given in graph.");
}
shallSaveTarget = true;
targetProduct = getTargetProductFromGraph(graph, target.getNodeId());
return postprocessTargetProduct();
}
} catch (Exception e) {
throw new IOException("Error executing Graph: " + e.getMessage(), e);
} finally {
pm.done();
}
}
private void logGraphState() {
for (Iterator<NodeContext> iterator = graphContext.getInitNodeContextDeque().descendingIterator(); iterator.hasNext(); ) {
NodeContext nodeContext = iterator.next();
CalvalusProductIO.printProductOnStdout(nodeContext.getTargetProduct(),
"computed by node " + nodeContext.getNode().getId());
}
}
private boolean executeGraphAndCollectOutput(Graph graph, XppDom calvalusAppData, ProgressMonitor pm) throws Exception {
CalvalusGraphApplicationData appData = CalvalusGraphApplicationData.fromDom(calvalusAppData);
getLogger().info("Executing graph");
GraphProcessor processor = new GraphProcessor();
processor.executeGraph(graphContext, pm);
graphContext.dispose();
// collect results files
List<File> outputFileList = new ArrayList<>();
if (appData.outputFiles != null) {
for (String outputFile : appData.outputFiles) {
File file = new File(outputFile);
if (file.exists()) {
outputFileList.add(file);
} else {
getLogger().warning("outputFile '" + outputFile + "' does not exist.");
}
}
}
if (appData.outputNodes != null) {
for (OutputNodeRef ref : appData.outputNodes) {
Node node = graph.getNode(ref.nodeId);
if (node != null) {
DomElement configuration = node.getConfiguration();
if (configuration != null) {
DomElement domElement = configuration.getChild(ref.parameter);
if (domElement != null) {
File file = new File(domElement.getValue());
if (file.exists()) {
outputFileList.add(file);
} else {
getLogger().warning("outputNode referenced file '" + ref.nodeId + "." + ref.parameter + "' does not exist.");
}
} else {
getLogger().warning("outputNode parameter '" + ref.nodeId + "." + ref.parameter + "' does not exist.");
}
} else {
getLogger().warning("outputNode '" + ref.nodeId + "' has no configuration.");
}
} else {
getLogger().warning("outputNode '" + ref.nodeId + "' does not exist.");
}
}
}
MapContext mapContext = getMapContext();
if (appData.archiveFile != null) {
// create zip with all files
Path workPath = new Path(getWorkOutputDirectoryPath(), appData.archiveFile);
OutputStream outputStream = workPath.getFileSystem(mapContext.getConfiguration()).create(workPath);
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(outputStream));
zipOutputStream.setMethod(ZipEntry.DEFLATED);
try {
for (File outputFile : outputFileList) {
getLogger().info("adding to zip archive: " + outputFile.getName());
ZipEntry zipEntry = new ZipEntry(outputFile.getName().replace('\\', '/'));
FileInputStream inputStream = new FileInputStream(outputFile);
try {
zipOutputStream.putNextEntry(zipEntry);
ProductFormatter.copy(inputStream, zipOutputStream, mapContext);
zipOutputStream.closeEntry();
} finally {
inputStream.close();
}
}
} finally {
zipOutputStream.close();
}
} else {
// archive individual files
for (File outputFile : outputFileList) {
getLogger().info("copying to HDFS: " + outputFile.getName());
InputStream is = new BufferedInputStream(new FileInputStream(outputFile));
Path workPath = new Path(getWorkOutputDirectoryPath(), outputFile.getName());
OutputStream os = workPath.getFileSystem(mapContext.getConfiguration()).create(workPath);
ProductFormatter.copyAndClose(is, os, mapContext);
}
}
return true;
}
private boolean postprocessTargetProduct() throws IOException {
if (getConfiguration().getBoolean(JobConfigNames.CALVALUS_OUTPUT_SUBSETTING, false)) {
getLogger().info("output subsetting of split " + getInputPaths());
targetProduct = createSubsetFromOutput(targetProduct);
}
if (targetProduct.getSceneRasterWidth() == 0 || targetProduct.getSceneRasterHeight() == 0) {
getLogger().info("Skip processing");
return false;
}
getLogger().info(String.format("Processed product width = %d height = %d",
targetProduct.getSceneRasterWidth(),
targetProduct.getSceneRasterHeight()));
if (hasInvalidStartAndStopTime(targetProduct)) {
copySceneRasterStartAndStopTime(getInputProduct(), targetProduct, null);
}
return true;
}
private Product getTargetProductFromGraph(Graph graph, String targetNodeId) {
Node targetNode = graph.getNode(targetNodeId);
if (targetNode == null) {
throw new IllegalArgumentException("Specified targetNode '" + targetNodeId + "' does not exist in graph.");
}
return graphContext.getNodeContext(targetNode).getTargetProduct();
}
private GraphContext buildGraphContext(Graph graph, Header header) throws IOException, GraphException {
List<HeaderSource> sources = header.getSources();
Path inputPath = getInputPaths()[0];
Path qualifiedInputPath = inputPath.getFileSystem(getConfiguration()).makeQualified(inputPath);
Operator sourceProducts = new SourceProductContainerOperator();
for (HeaderSource headerSource : sources) {
String sourceId = headerSource.getName();
Path sourcePath = new Path(headerSource.getLocation());
String inputFormat = null;
if (headerSource.getDescription() != null && headerSource.getDescription().startsWith("format:")) {
inputFormat = headerSource.getDescription().substring("format:".length());
}
Path qualifiedSourcePath = sourcePath.getFileSystem(getConfiguration()).makeQualified(sourcePath);
Product sourceProduct;
if (qualifiedInputPath.equals(qualifiedSourcePath) ) {
// main input, maybe re-read with specific input format
if (inputFormat == null) {
sourceProduct = getInputProduct();
} else {
getConfiguration().set(JobConfigNames.CALVALUS_INPUT_FORMAT, inputFormat);
sourceProduct = CalvalusProductIO.readProduct(sourcePath, getConfiguration(), inputFormat);
}
if (getConfiguration().getBoolean(JobConfigNames.CALVALUS_INPUT_SUBSETTING, true)) {
getLogger().info("input subsetting of split " + sourcePath);
sourceProduct = createSubsetFromInput(sourceProduct);
}
} else {
// other source product
sourceProduct = CalvalusProductIO.readProduct(sourcePath, getConfiguration(), inputFormat);
}
sourceProducts.setSourceProduct(sourceId, sourceProduct);
}
return new GraphContext(graph, sourceProducts);
}
@Override
public Product openProcessedProduct() {
return targetProduct;
}
@Override
public void saveProcessedProducts(ProgressMonitor pm) throws IOException {
if (shallSaveTarget) {
saveTargetProduct(targetProduct, pm);
}
}
@Override
public void dispose() {
super.dispose();
if (graphContext != null) {
graphContext.dispose();
graphContext = null;
}
if (targetProduct != null) {
targetProduct.dispose();
targetProduct = null;
}
}
public Graph createGraph() throws GraphException, IOException {
Path inputPath = getInputPaths()[0];
CalvalusLogger.getLogger().info("Creating graph for input: " + inputPath);
Configuration conf = getConfiguration();
String processorParameters = conf.get(JobConfigNames.CALVALUS_L2_PARAMETERS);
ResourceEngine resourceEngine = new ResourceEngine();
VelocityContext velocityContext = resourceEngine.getVelocityContext();
final Properties processingParameters = PropertiesHandler.asProperties(processorParameters);
for (int i = 0; i < getInputParameters().length; i += 2) {
if ("output".equals(getInputParameters()[i])) {
// drop parameter, is used later in SubsetProcessorAdapter
} else if ("regionGeometry".equals(getInputParameters()[i])) {
conf.set("calvalus.regionGeometry", getInputParameters()[i+1]);
} else {
processingParameters.put(getInputParameters()[i], getInputParameters()[i + 1]);
}
}
for (String key : processingParameters.stringPropertyNames()) {
velocityContext.put(key, processingParameters.get(key));
}
velocityContext.put("system", System.getProperties());
velocityContext.put("configuration", conf);
velocityContext.put("parameterText", processorParameters);
velocityContext.put("parameters", processingParameters);
velocityContext.put("inputPath", inputPath);
velocityContext.put("outputPath", getOutputDirectoryPath());
velocityContext.put("workOutputPath", getWorkOutputDirectoryPath());
velocityContext.put("GlobalFunctions", new GlobalFunctions(getLogger()));
String graphPathAsString = conf.get(ProcessorFactory.CALVALUS_L2_PROCESSOR_FILES);
Path graphPath = new Path(graphPathAsString);
InputStream inputStream = graphPath.getFileSystem(conf).open(graphPath);
Reader inputReader = new BufferedReader(new InputStreamReader(inputStream));
Resource processedGraph = resourceEngine.processResource(new ReaderResource(graphPathAsString, inputReader));
String graphAsText = processedGraph.getContent();
System.out.println("graphAsText = \n" + graphAsText);
if (graphAsText.contains("CALVALUS_SKIP_PROCESSING yes")) {
return null;
}
StringReader graphReader = new StringReader(graphAsText);
try {
return GraphIO.read(graphReader);
} finally {
inputReader.close();
graphReader.close();
}
}
public static class CalvalusGraphApplicationData implements XmlConvertible {
@Parameter(itemAlias = "outputFile")
String[] outputFiles;
@Parameter(itemAlias = "outputNode", domConverter = OutputNodeDomConverter.class)
OutputNodeRef[] outputNodes;
@Parameter()
String archiveFile;
public static CalvalusGraphApplicationData fromXml(String xml) throws BindingException {
return createConverter().convertXmlToObject(xml, new CalvalusGraphApplicationData());
}
public static CalvalusGraphApplicationData fromDom(XppDom calvalusAppDataDom) throws ValidationException, ConversionException {
if (calvalusAppDataDom.getName().equals("applicationData")) {
calvalusAppDataDom = calvalusAppDataDom.getChild("executeParameters");
}
ParameterDescriptorFactory parameterDescriptorFactory = new ParameterDescriptorFactory();
CalvalusGraphApplicationData appData = new CalvalusGraphApplicationData();
PropertyContainer propertySet = PropertyContainer.createObjectBacked(appData, parameterDescriptorFactory);
propertySet.setDefaultValues();
DefaultDomConverter domConverter = new DefaultDomConverter(appData.getClass(), parameterDescriptorFactory);
domConverter.convertDomToValue(new XppDomElement(calvalusAppDataDom), propertySet);
return appData;
}
@Override
public String toXml() {
try {
return new ParameterBlockConverter().convertObjectToXml(this);
} catch (ConversionException e) {
throw new RuntimeException(e);
}
}
private static ParameterBlockConverter createConverter() {
return new ParameterBlockConverter(new ParameterDescriptorFactory(), "executeParameters");
}
}
public static class OutputNodeRef {
public final String nodeId;
public final String parameter;
public OutputNodeRef(String nodeId, String parameter) {
this.nodeId = nodeId;
this.parameter = parameter;
}
}
public static class OutputNodeDomConverter implements DomConverter {
@Override
public Class<?> getValueType() {
return OutputNodeRef[].class;
}
@Override
public Object convertDomToValue(DomElement parentElement, Object value) throws ConversionException, ValidationException {
DomElement[] outputNodes = parentElement.getChildren("outputNode");
List<OutputNodeRef> outputNodeRefList = new ArrayList<>();
for (DomElement outputNode : outputNodes) {
String id = outputNode.getAttribute("id");
String parameter = outputNode.getAttribute("parameter");
if (id != null && parameter != null) {
outputNodeRefList.add(new OutputNodeRef(id, parameter));
}
}
return outputNodeRefList.toArray(new OutputNodeRef[0]);
}
@Override
public void convertValueToDom(Object value, DomElement parentElement) throws ConversionException {
throw new IllegalStateException("Conversion to DOM no implemented");
}
}
/**
* This operator is only a container for the source product of the graph.
*/
private static class SourceProductContainerOperator extends Operator {
@Override
public void initialize() throws OperatorException {
// do nothing by intention
}
}
// TODO mz/nf 2013-11-13 use some sort of plugin concept to add new functions to the velocity context
public static class GlobalFunctions {
private final Logger logger;
public GlobalFunctions(Logger logger) {
this.logger = logger;
}
public Path createPath(String pathString) {
return new Path(pathString);
}
// MER_RR__1PRACR20030601_092632_000026422016_00480_06546_0000.N1
public Path findCoveringN1(String master, String archiveRoot, FileSystem fileSystem) {
Path result = null;
try {
final String masterStartString = master.substring(14, 29);
final String masterDurationString = master.substring(30, 38);
final long masterStart = N1_TIME_FORMAT.parse(masterStartString).getTime();
final long masterStop = masterStart + (Long.parseLong("1" + masterDurationString) - 100000000) * 1000;
logger.info("looking for slave of " + master + " in " + archiveRoot);
final Calendar calendar = DateUtils.createCalendar();
calendar.setTimeInMillis(masterStart);
final String thisDayDir = YMD_DIR_FORMAT.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, -1);
final String previousDayDir = YMD_DIR_FORMAT.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 2);
final String nextDayDir = YMD_DIR_FORMAT.format(calendar.getTime());
final FileStatus[] slaveFiles = fileSystem.listStatus(new Path[]{
new Path(archiveRoot, previousDayDir),
new Path(archiveRoot, thisDayDir),
new Path(archiveRoot, nextDayDir)
}, new PathFilter() {
@Override
public boolean accept(Path path) {
return path.getName().length() == 62 && path.getName().endsWith(".N1");
}
});
for (FileStatus slaveFile : slaveFiles) {
final String slave = slaveFile.getPath().getName();
final String slaveStartString = slave.substring(14, 29);
final String slaveDurationString = slave.substring(30, 38);
final long slaveStart = N1_TIME_FORMAT.parse(slaveStartString).getTime();
final long slaveStop = slaveStart + (Long.parseLong("1" + slaveDurationString) - 100000000) * 1000;
if (masterStart >= slaveStart && masterStop <= slaveStop) {
logger.info("covering slave " + slave + " found");
return slaveFile.getPath();
} else if ((masterStart + masterStop) / 2 >= slaveStart && (masterStart + masterStop) / 2 <= slaveStop) {
result = slaveFile.getPath();
}
}
} catch (ParseException e) {
throw new RuntimeException("failed to parse date in " + master, e);
} catch (IOException e) {
throw new RuntimeException("failed to read dirs below " + archiveRoot, e);
}
if (result != null) {
logger.info("intersecting slave " + result.getName() + " found");
} else {
logger.info("no slave found for " + master);
}
return result;
}
public Calendar getCalendar() {
return DateUtils.createCalendar();
}
public String formatDate(String format, Date date) {
return DateUtils.createDateFormat(format).format(date);
}
public Date parseDate(String format, String source) throws ParseException {
return DateUtils.createDateFormat(format).parse(source);
}
public String xmlEncode(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '&':
sb.append("&");
break;
// case '\"':
// sb.append(""");
// break;
// case '\'':
// sb.append("'");
// break;
default:
if (c > 0x7e) {
sb.append("&#" + ((int) c) + ";");
} else
sb.append(c);
}
}
return sb.toString();
}
public String convertProcessingGraphToXML(Product product) {
final MetadataElement metadataRoot = product.getMetadataRoot();
if (metadataRoot != null) {
final MetadataElement processingGraph = metadataRoot.getElement("Processing_Graph");
if (processingGraph != null) {
return convertMetadataToXML(processingGraph);
}
}
return "";
}
public String convertMetadataToXML(MetadataElement metadataElement) {
Element domElement = convertMetadataToDOM(metadataElement);
final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
return outputter.outputString(domElement);
}
public Element convertMetadataToDOM(MetadataElement metadataElement) {
final Element domElement = new Element(metadataElement.getName());
for (MetadataAttribute attribute : metadataElement.getAttributes()) {
domElement.addContent(new Element(attribute.getName()).setText(attribute.getData().getElemString()));
}
for (MetadataElement element : metadataElement.getElements()) {
domElement.addContent(convertMetadataToDOM(element));
}
return domElement;
}
public String configToString(Configuration jobConfig) {
StringBuilder accu = new StringBuilder();
for (Map.Entry<String,String> entry : jobConfig) {
accu.append(entry.getKey());
accu.append("=");
accu.append(xmlEncode(entry.getValue()).replaceAll("\n", " "));
accu.append("\n");
}
return accu.toString();
}
}
public static void main(String[] args) throws IOException {
ResourceEngine resourceEngine = new ResourceEngine();
VelocityContext velocityContext = resourceEngine.getVelocityContext();
velocityContext.put("system", System.getProperties());
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://calvalus");
conf.set("dfs.nameservices", "calvalus");
conf.set("dfs.ha.namenodes.calvalus", "nn1,nn2");
conf.set("dfs.namenode.rpc-address.calvalus.nn1", "master00:8020");
conf.set("dfs.namenode.rpc-address.calvalus.nn2", "master01:8020");
conf.set("dfs.client.failover.proxy.provider.calvalus", "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
velocityContext.put("configuration", conf);
Path inputPath = new Path("hdfs://calvalus/calvalus/eodata/MER_RR__1P/r03/2008/06/01/MER_RR__1PRACR20080601_122734_000026432069_00081_32700_0000.N1");
velocityContext.put("inputPath", inputPath);
velocityContext.put("GlobalFunctions", new GlobalFunctions(Logger.getLogger("test")));
Reader inputReader = new FileReader(args[0]);
Resource processedGraph = resourceEngine.processResource(new ReaderResource(args[0], inputReader));
String graphAsText = processedGraph.getContent();
System.out.println("graphAsText = \n" + graphAsText.trim());
}
}