Example usage for org.joda.time Period Period

List of usage examples for org.joda.time Period Period

Introduction

In this page you can find the example usage for org.joda.time Period Period.

Prototype

public Period(Object period) 

Source Link

Document

Creates a period by converting or copying from another object.

Usage

From source file:org.mousephenotype.dcc.exportlibrary.datastructure.converters.DatatypeConverter.java

License:Apache License

public static String getDuration(long milis) {
    return daysHoursMinutes.print(new Period(milis));
}

From source file:org.mrgeo.cmd.buildpyramid.BuildPyramid.java

License:Apache License

@Override
@SuppressWarnings("squid:S1166") // Catching exceptions and logging error
public int run(String[] args, final Configuration conf, final ProviderProperties providerProperties) {
    log.info("BuildPyramid");

    long start = System.currentTimeMillis();

    Options options = BuildPyramid.createOptions();
    CommandLine line;/*ww w. j a v  a2s  .  co  m*/

    try {
        //if no arguments, print help
        if (args.length == 0) {
            throw new ParseException(null);
        }
        CommandLineParser parser = new PosixParser();
        line = parser.parse(options, args);
    } catch (ParseException e) {
        new HelpFormatter().printHelp("BuildPyramid <input>", options);
        return -1;
    }

    if (line == null || line.hasOption("h")) {
        new HelpFormatter().printHelp("ingest <options> <input>", options);
        return -1;
    }

    Aggregator aggregator = new MeanAggregator();
    if (line.hasOption("c")) {
        aggregator = new ModeAggregator();
    } else if (line.hasOption("s")) {
        aggregator = new SumAggregator();
    } else if (line.hasOption("n")) {
        aggregator = new NearestAggregator();
    } else if (line.hasOption("min")) {
        aggregator = new MinAggregator();
    } else if (line.hasOption("max")) {
        aggregator = new MaxAggregator();
    } else if (line.hasOption("minavgpair")) {
        aggregator = new MinAvgPairAggregator();
    }

    String input = null;
    for (String arg : line.getArgs()) {
        input = arg;
    }

    log.info("Input image: " + input);

    if (input != null) {
        try {
            // TODO: Need to obtain provider properties
            //if (!BuildPyramidDriver.build(input, aggregator, conf, providerProperties))

            // Validate that the user provided an image
            try {
                DataProviderFactory.getMrsImageDataProvider(input, DataProviderFactory.AccessMode.READ,
                        providerProperties);
            } catch (DataProviderNotFound e) {
                log.error(input + " is not an image");
                return -1;
            }
            if (!org.mrgeo.buildpyramid.BuildPyramid.build(input, aggregator, conf, providerProperties)) {
                log.error("BuildPyramid exited with error");
                return -1;
            }
        }

        catch (Exception e) {
            log.error("BuildPyramid exited with error", e);
            return -1;
        }
    }

    long end = System.currentTimeMillis();
    long duration = end - start;
    PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h:").appendMinutes()
            .appendSuffix("m:").appendSeconds().appendSuffix("s").toFormatter();
    String formatted = formatter.print(new Period(duration));
    log.info("BuildPyramid completed in " + formatted);

    return 0;
}

From source file:org.mrgeo.cmd.ingest.IngestImage.java

License:Apache License

@Override
@SuppressWarnings("squid:S1166") // Exceptions caught and error message printed
public int run(String[] args, Configuration conf, ProviderProperties providerProperties) {
    try {/* w  w  w.  ja v a2  s.  com*/
        long start = System.currentTimeMillis();

        CommandLine line;
        try {
            CommandLineParser parser = new GnuParser();
            line = parser.parse(options, args);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            new HelpFormatter().printHelp("ingest <options> <input>", options);

            return -1;
        }

        if (line == null || line.hasOption("h")) {
            new HelpFormatter().printHelp("ingest <options> <input>", options);
            return -1;
        }

        boolean overrideNodata = line.hasOption("nd");
        double[] nodataOverride = null;
        if (overrideNodata) {
            String str = line.getOptionValue("nd");
            String[] strElements = str.split(",");
            nodataOverride = new double[strElements.length];
            for (int i = 0; i < nodataOverride.length; i++) {
                try {
                    nodataOverride[i] = parseNoData(strElements[i]);
                } catch (NumberFormatException nfe) {
                    System.out.println("Invalid nodata value: " + strElements[i]);
                    return -1;
                }
            }
        }

        boolean categorical = line.hasOption("c");
        boolean skipCatLoad = line.hasOption("sc");
        boolean skipPyramids = line.hasOption("sp");
        boolean recurse = !line.hasOption("nr");

        skippreprocessing = line.hasOption("sk");
        String output = line.getOptionValue("o");

        log.debug("categorical: " + categorical);
        log.debug("skip category loading: " + skipCatLoad);
        log.debug("skip pyramids: " + skipPyramids);
        log.debug("output: " + output);

        ArrayList<String> inputs = new ArrayList<String>();

        int zoomlevel = -1;
        if (line.hasOption("z")) {
            zoomlevel = Integer.parseInt(line.getOptionValue("z"));
        }

        if (skippreprocessing && zoomlevel < 1) {
            log.error("Need to specify zoomlevel to skip preprocessing");
            return -1;
        }
        IngestInputProcessor iip = new IngestInputProcessor(conf, nodataOverride, zoomlevel, skippreprocessing);

        try {
            for (String arg : line.getArgs()) {
                iip.processInput(arg, recurse);
            }
            inputs.addAll(JavaConversions.asJavaCollection(iip.getInputs()));
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            return -1;
        }

        log.info("Ingest inputs (" + inputs.size() + ")");
        for (String input : inputs) {
            log.info("   " + input);
        }

        if (line.hasOption("t")) {
            String rawTags = line.getOptionValue("t");

            String splittags[] = rawTags.split(",");
            for (String t : splittags) {
                String[] s = t.split(":");
                if (s.length != 2) {
                    log.error("Bad tag format.  Should be: k1:v1,k2:v2,...  is: " + rawTags);
                    return -1;
                }

                tags.put(s[0], s[1]);
            }
        }

        quick = quick || line.hasOption("q");
        local = local || line.hasOption("lc");

        String protectionLevel = line.getOptionValue("pl");

        if (inputs.size() > 0) {
            try {
                final boolean success;
                if (quick) {
                    //            success = IngestImage.quickIngest(inputs.get(0), output, categorical,
                    //                conf, overrideNodata, nodata, tags, protectionLevel, providerProperties);
                    log.error("Quick Ingest is not yet implemented");
                    return -1;
                } else if (local) {
                    success = org.mrgeo.ingest.IngestImage.localIngest(
                            inputs.toArray(new String[inputs.size()]), output, categorical, skipCatLoad, conf,
                            iip.getBounds(), iip.getZoomlevel(), iip.tilesize(), iip.getNodata(),
                            iip.getBands(), iip.getTiletype(), tags, protectionLevel, providerProperties);
                } else {
                    success = org.mrgeo.ingest.IngestImage.ingest(inputs.toArray(new String[inputs.size()]),
                            output, categorical, skipCatLoad, conf, iip.getBounds(), iip.getZoomlevel(),
                            iip.tilesize(), iip.getNodata(), iip.getBands(), iip.getTiletype(), tags,
                            protectionLevel, providerProperties);
                }

                if (!success) {
                    log.error("IngestImage exited with error");
                    return 1;
                }

                if (!skipPyramids) {
                    Aggregator aggregator = new MeanAggregator();
                    if (line.hasOption("c")) {
                        aggregator = new ModeAggregator();
                    } else if (line.hasOption("s")) {
                        aggregator = new SumAggregator();
                    } else if (line.hasOption("n")) {
                        aggregator = new NearestAggregator();
                    } else if (line.hasOption("min")) {
                        aggregator = new MinAggregator();
                    } else if (line.hasOption("max")) {
                        aggregator = new MaxAggregator();
                    } else if (line.hasOption("minavgpair")) {
                        aggregator = new MinAvgPairAggregator();
                    }

                    BuildPyramid.build(output, aggregator, conf, providerProperties);
                }
            } catch (Exception e) {
                log.error("IngestImage exited with error", e);
                return 1;
            }
        }

        long end = System.currentTimeMillis();
        long duration = end - start;
        PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h:")
                .appendMinutes().appendSuffix("m:").appendSeconds().appendSuffix("s").toFormatter();
        String formatted = formatter.print(new Period(duration));
        log.info("IngestImage complete in " + formatted);

        return 0;
    } catch (Exception e) {
        log.error("IngestImage exited with error", e);
    }

    return -1;
}

From source file:org.mrgeo.cmd.ingestvector.IngestVector.java

License:Apache License

@Override
public int run(String[] args, Configuration conf, Properties providerProperties) {
    log.info("IngestVector");

    try {/*from ww  w . j  a va2s  .  c  om*/
        config = conf;
        long start = System.currentTimeMillis();

        Options options = IngestVector.createOptions();
        CommandLine line = null;
        try {
            CommandLineParser parser = new GnuParser();
            line = parser.parse(options, args);
        } catch (ParseException e) {
            new HelpFormatter().printHelp("IngestVector <input>", options);
            return -1;
        }

        if (line != null) {
            if (line.hasOption("v")) {
                LoggingUtils.setDefaultLogLevel(LoggingUtils.INFO);
            }
            if (line.hasOption("d")) {
                LoggingUtils.setDefaultLogLevel(LoggingUtils.DEBUG);
            }

            if (line.hasOption("l")) {
                System.out.println("Using local runner");
                HadoopUtils.setupLocalRunner(config);
            }

            int zoomlevel = -1;
            if (line.hasOption("z")) {
                zoomlevel = Integer.valueOf(line.getOptionValue("z"));
            }

            boolean skipPyramids = line.hasOption("sp");
            boolean recurse = !line.hasOption("nr");
            String output = line.getOptionValue("o");

            log.debug("skip pyramids: " + skipPyramids);
            log.debug("output: " + output);

            // need to initialize before looking for the files...
            GeotoolsVectorUtils.initialize();

            String protectionLevel = line.getOptionValue("pl");
            if (line.hasOption("osm")) {
                String[] inputs = line.getArgs();
                if (!line.hasOption("z")) {
                    System.out.println("You must specify a zomm level with -z when ingesting OSM data");
                    return -1;
                }
                if (line.hasOption("l")) {
                    System.out.println("Using local runner");
                    HadoopUtils.setupLocalRunner(config);
                }
                OSMTileIngester.ingestOsm(inputs, output, config, zoomlevel, protectionLevel,
                        providerProperties);
            } else {
                List<String> inputs = new LinkedList<String>();

                for (String arg : line.getArgs()) {
                    inputs.addAll(getInputs(arg, recurse));
                }

                log.info("Ingest inputs (" + inputs.size() + ")");
                for (String input : inputs) {
                    log.info("   " + input);
                }

                if (inputs.size() > 0) {
                    // Ingest non-OSM data
                    try {
                        final boolean success;
                        if (line.hasOption("lc")) {
                            success = IngestVectorDriver.localIngest(inputs.toArray(new String[inputs.size()]),
                                    output, config, zoomlevel, protectionLevel);
                        } else {
                            success = IngestVectorDriver.ingest(inputs.toArray(new String[inputs.size()]),
                                    output, config, zoomlevel, protectionLevel, providerProperties);
                        }
                        if (!success) {
                            log.error("IngestVector exited with error");
                            return 1;
                        }

                        //            if (!skipPyramids)
                        //            {
                        //              BuildPyramidDriver.build(output, aggregator, getConf());
                        //            }
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("IngestVector exited with error");
                        return 1;
                    }
                }
            }
        }

        long end = System.currentTimeMillis();
        long duration = end - start;
        PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h:")
                .appendMinutes().appendSuffix("m:").appendSeconds().appendSuffix("s").toFormatter();
        String formatted = formatter.print(new Period(duration));
        log.info("IngestVector complete in " + formatted);

        return 0;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return -1;

}

From source file:org.mrgeo.cmd.quantiles.Quantiles.java

License:Apache License

@Override
@SuppressWarnings("squid:S1166") // Exception caught and error message printed
public int run(String[] args, final Configuration conf, final ProviderProperties providerProperties) {
    log.info("quantiles");

    long start = System.currentTimeMillis();

    Options options = Quantiles.createOptions();
    CommandLine line;//from   ww w.j  a v  a2  s. c o  m

    try {
        //if no arguments, print help
        if (args.length == 0) {
            throw new ParseException(null);
        }
        CommandLineParser parser = new PosixParser();
        line = parser.parse(options, args);
    } catch (ParseException e) {
        new HelpFormatter().printHelp("quantiles <options> <input>", options);
        return -1;
    }

    if (line == null || line.hasOption("h")) {
        new HelpFormatter().printHelp("quantiles <options> <input>", options);
        return -1;
    }

    String input = null;
    for (String arg : line.getArgs()) {
        input = arg;
    }

    log.info("Input image: " + input);

    if (input != null) {
        try {
            // TODO: Need to obtain provider properties
            //if (!BuildPyramidDriver.build(input, aggregator, conf, providerProperties))

            // Validate that the user provided an image
            try {
                DataProviderFactory.getMrsImageDataProvider(input, DataProviderFactory.AccessMode.READ,
                        providerProperties);
            } catch (DataProviderNotFound e) {
                log.error(input + " is not an image");
                return -1;
            }
            Path outputPath = HadoopFileUtils.createUniqueTmpPath(conf);
            try {
                int numQuantiles = Integer.parseInt(line.getOptionValue("numQuantiles"));
                if (line.hasOption("fraction")) {
                    float fraction = Float.parseFloat(line.getOptionValue("fraction"));
                    if (!org.mrgeo.quantiles.Quantiles.compute(input, outputPath.toString(), numQuantiles,
                            fraction, conf, providerProperties)) {
                        log.error("Quantiles exited with error");
                        return -1;
                    }
                    printResults(outputPath, conf);
                } else {
                    if (!org.mrgeo.quantiles.Quantiles.compute(input, outputPath.toString(), numQuantiles, conf,
                            providerProperties)) {
                        log.error("Quantiles exited with error");
                        return -1;
                    }
                    printResults(outputPath, conf);
                }
            } finally {
                HadoopFileUtils.delete(conf, outputPath);
            }
        } catch (IOException e) {
            log.error("Quantiles exited with error", e);
            return -1;
        }

    }

    long end = System.currentTimeMillis();
    long duration = end - start;
    PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h:").appendMinutes()
            .appendSuffix("m:").appendSeconds().appendSuffix("s").toFormatter();
    String formatted = formatter.print(new Period(duration));
    log.info("BuildPyramid completed in " + formatted);

    return 0;
}

From source file:org.mrgeo.cmd.server.WebServer.java

License:Apache License

@Override
public int run(String[] args, Configuration conf, ProviderProperties providerProperties) {
    try {/*from  w w  w.  jav  a 2  s  .  c  o m*/
        long start = System.currentTimeMillis();

        CommandLine line = null;
        try {
            CommandLineParser parser = new GnuParser();
            line = parser.parse(options, args);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            new HelpFormatter().printHelp("webserver <options> <operation>", options);
            return -1;
        }

        if (line == null || line.hasOption("h")) {
            new HelpFormatter().printHelp("webserver <options> <operation>", options);
            return -1;
        }

        int httpPort = 8080;
        if (line.hasOption("p")) {
            try {
                httpPort = Integer.parseInt(line.getOptionValue("p", "8080"));
            } catch (NumberFormatException nfe) {
                System.err.println("Invalid HTTP port specified: " + line.getOptionValue("p"));
                return -1;
            }
        }
        if (line.hasOption("v")) {
            LoggingUtils.setDefaultLogLevel(LoggingUtils.INFO);
        }
        if (line.hasOption("d")) {
            LoggingUtils.setDefaultLogLevel(LoggingUtils.DEBUG);
        }

        runWebServer(httpPort);

        long end = System.currentTimeMillis();
        long duration = end - start;
        PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h:")
                .appendMinutes().appendSuffix("m:").appendSeconds().appendSuffix("s").toFormatter();
        String formatted = formatter.print(new Period(duration));
        log.info("IngestImage complete in " + formatted);

        return 0;
    } catch (Exception e) {
        log.error("Exception thrown", e);
    }

    return -1;
}

From source file:org.n52.ses.wsn.dissemination.updateinterval.UpdateIntervalDisseminationMethod.java

License:Open Source License

private long parseDuration(String textContent) {
    return new Period(textContent).toStandardSeconds().getSeconds() * 1000;
}

From source file:org.openadaptor.auxil.metrics.ComponentMetrics.java

License:Open Source License

private String formatDuration(long duration) {
    StringBuffer sb = new StringBuffer();
    if (duration == 0) {
        sb.append(LESS_THAN_ONE);/*from   w  w w .  j  av  a2 s . c  o  m*/
        sb.append(MILLISECONDS);
    } else if (duration == UNKNOWN_LONG) {
        sb.append(NOT_APPLICABLE);
    } else {
        sb.append(periodFormatter.print(new Period(duration)));
    }
    return sb.toString();
}

From source file:org.openadaptor.auxil.metrics.ComponentMetrics.java

License:Open Source License

private String formatDuration(long duration, long durationMin, long durationMax, long durationLast) {
    StringBuffer sb = new StringBuffer();
    if (duration == 0) {
        sb.append(LESS_THAN_ONE);//from ww  w . j a  v  a2s.c  o  m
        sb.append(MILLISECONDS);
    } else if (duration == UNKNOWN_LONG) {
        sb.append(NOT_APPLICABLE);
    } else if (duration == durationMin) {
        sb.append(formatDuration(duration));
    } else {
        sb.append(periodFormatter.print(new Period(duration)));
        sb.append(" (min: ");
        sb.append(formatDuration(durationMin));
        sb.append(", max: ");
        sb.append(periodFormatter.print(new Period(durationMax)));
        if (durationLast != UNKNOWN_LONG) {
            sb.append(", last: ");
            sb.append(formatDuration(durationLast));
        }
        sb.append(")");
    }
    return sb.toString();
}

From source file:org.openmrs.module.chaiui.ChaiUiUtils.java

License:Open Source License

/**
 * Formats a duration//from   w  w  w  . ja v  a  2s.  co m
 * @param time the time in milliseconds
 * @return the formatted duration
 */
public String formatDuration(long time) {
    Period period = new Period(time);
    return String.format(DURATION_FORMAT, period.getHours(), period.getMinutes(), period.getSeconds());
}