Example usage for org.joda.time DateTimeZone forID

List of usage examples for org.joda.time DateTimeZone forID

Introduction

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

Prototype

@FromString
public static DateTimeZone forID(String id) 

Source Link

Document

Gets a time zone instance for the specified time zone id.

Usage

From source file:gov.noaa.pfel.coastwatch.Projects.java

License:Open Source License

/** Convert FED Rockfish CTD .csv data files to .nc (from Lynn 2013-03-28
 * from /Volumes/PFEL_Shared_Space/PFEL_Share/Lynn2Bob/Rockfish_CTD.tar.gz).
 */// w w w.ja v a 2  s. co m
public static void convertRockfish20130328() throws Throwable {
    String2.log("\nProjects.convertRockfish20130328()");
    String dir = "C:/u00/data/points/rockfish20130328/";
    String outerName = "ERD_CTD_HEADER_2008_to_2011"; //.csv -> .nc
    String innerName = "ERD_CTD_CAST_2008_to_2011"; //.csv -> .nc

    //read the outer .csv files
    Table outer = new Table();
    outer.readASCII(dir + outerName + ".csv", 0, 1, null, null, null, null, false); //simplify
    Test.ensureEqual(outer.getColumnNamesCSVString(),
            "CRUISE,CTD_INDEX,CTD_NO,STATION,CTD_DATE,CTD_LAT,CTD_LONG,CTD_BOTTOM_DEPTH,BUCKET_TEMP,BUCKET_SAL,TS_TEMP,TS_SAL",
            "Unexpected outer column names");
    String2.log("outer (5 rows) as read:\n" + outer.dataToCSVString(5));

    //convert to short 
    String colNames[] = { "CTD_INDEX", "CTD_NO", "STATION", "CTD_BOTTOM_DEPTH" };
    for (int coli = 0; coli < colNames.length; coli++) {
        int col = outer.findColumnNumber(colNames[coli]);
        outer.setColumn(col, new ShortArray(outer.getColumn(col)));
    }

    //convert to floats 
    colNames = new String[] { "BUCKET_TEMP", "BUCKET_SAL", "TS_TEMP", "TS_SAL" };
    for (int coli = 0; coli < colNames.length; coli++) {
        int col = outer.findColumnNumber(colNames[coli]);
        outer.setColumn(col, new FloatArray(outer.getColumn(col)));
    }

    //convert date time "5/5/2008 9:10" to time   
    DateTimeFormatter dtf = DateTimeFormat.forPattern("M/d/yyyy H:mm")
            .withZone(DateTimeZone.forID("America/Los_Angeles"));
    //GMT: erddap/convert/time.html says "5/5/2008 19:10" = 1.2100146E9
    //  if 7 hours different in summer...
    Test.ensureEqual(dtf.parseMillis("5/5/2008 12:10") / 1000.0, 1.2100146E9, //erddap/convert/time.html
            "trouble with DateTimeFormatter");
    int nOuter = outer.nRows();
    {
        int col = outer.findColumnNumber("CTD_DATE");
        PrimitiveArray oldTimePA = outer.getColumn(col);
        DoubleArray newTimePA = new DoubleArray();
        for (int row = 0; row < nOuter; row++)
            newTimePA.add(dtf.parseMillis(oldTimePA.getString(row)) / 1000.0);
        outer.setColumn(col, newTimePA);
        outer.columnAttributes(col).set("units", "seconds since 1970-01-01T00:00:00Z");
    }

    //convert lat and lon from dddmm.mmmm to decimal degrees
    colNames = new String[] { "CTD_LAT", "CTD_LONG" };
    for (int coli = 0; coli < colNames.length; coli++) {
        int col = outer.findColumnNumber(colNames[coli]);
        PrimitiveArray pa = outer.getColumn(col);
        FloatArray fa = new FloatArray();
        Test.ensureEqual(Math.floor(1234.5 / 100.0) + (1234.5 % 100.0) / 60.0, 12.575, "");
        float scale = coli == 0 ? 1 : -1; //lon are originally degrees_west!
        for (int row = 0; row < nOuter; row++) {
            double d = pa.getDouble(row);
            if (d < 0)
                throw new SimpleException("d<0 requires more testing");
            fa.add(scale * Math2.doubleToFloatNaN(Math.floor(d / 100.0) + (d % 100.0) / 60.0));
        }
        outer.setColumn(col, fa);
    }

    //save the outer as .nc
    String2.log("outer (5 rows) before save:\n" + outer.toCSVString(5));
    outer.saveAsFlatNc(dir + outerName + ".nc", "row", false); //convertToFakeMissingValues

    //just keep the outer columns needed for inner table
    StringArray desired = StringArray.fromCSV("CRUISE,CTD_INDEX,CTD_NO,STATION,CTD_DATE,CTD_LAT,CTD_LONG");
    Test.ensureEqual(outer.reorderColumns(desired, true), desired.size(),
            "Not all desired columns were found.");

    //read inner table
    Table inner = new Table();
    inner.readASCII(dir + innerName + ".csv", 0, 1, null, null, null, null, false); //simplify
    Test.ensureEqual(inner.getColumnNamesCSVString(),
            "CRUISE,CTD_INDEX,CTD_DEPTH,TEMPERATURE,SALINITY,DENSITY,DYN_HGT,IRRAD,FLUOR_VOLT,TRANSMISSIVITY,CHLOROPHYLL,OXYGEN_VOLT,OXYGEN",
            "Unexpected inner column names");

    //convert to short 
    colNames = new String[] { "CTD_INDEX", "CTD_DEPTH" };
    for (int coli = 0; coli < colNames.length; coli++) {
        int col = inner.findColumnNumber(colNames[coli]);
        inner.setColumn(col, new ShortArray(inner.getColumn(col)));
    }

    //convert to floats 
    colNames = new String[] { "TEMPERATURE", "SALINITY", "DENSITY", "DYN_HGT", "IRRAD", "FLUOR_VOLT",
            "TRANSMISSIVITY", "CHLOROPHYLL", "OXYGEN_VOLT", "OXYGEN" };
    for (int coli = 0; coli < colNames.length; coli++) {
        int col = inner.findColumnNumber(colNames[coli]);
        inner.setColumn(col, new FloatArray(inner.getColumn(col)));
    }

    //add outer info to inner table
    inner.join(2, 0, "", outer); //nKeys, keyCol, String mvKey, Table lookUpTable

    //save inner table
    String2.log("inner (5 rows) before save:\n" + inner.toCSVString(5));
    inner.saveAsFlatNc(dir + innerName + ".nc", "row", false); //convertToFakeMissingValues

    String2.log("\n*** Projects.convertRockfish20130328() finished successfully");
}

From source file:gov.usgs.anss.query.cwb.messages.MessageFormatter.java

License:Open Source License

public static String miniSeedSummary(DateTime now, Collection<MiniSeed> miniSeed) {

    // 02:07:45.992Z Query on NZAPZ  HHZ10 000431 mini-seed blks 2009 001:00:00:00.0083 2009 001:00:30:00.438  ns=180044

    Iterator<MiniSeed> iter = miniSeed.iterator();

    if (!iter.hasNext()) {
        return String.format("%sZ No mini-seed blocks returned.", now.toString(nowFormat));
    }//from   www  . jav  a2 s  .  co m

    MiniSeed ms = iter.next();
    int numSamples = ms.getNsamp();

    DateTime begin = new DateTime(ms.getGregorianCalendar().getTimeInMillis(), DateTimeZone.forID("UTC"));

    while (iter.hasNext()) {
        ms = iter.next();
        numSamples += ms.getNsamp();
    }

    DateTime end = new DateTime(ms.getEndTime().getTimeInMillis(), DateTimeZone.forID("UTC"));

    return String.format("%sZ Query on %s %06d mini-seed blks %s %s ns=%d", now.toString(nowFormat),
            ms.getSeedName(), miniSeed.size(), begin.toString(msFormat), end.toString(msFormat), numSamples);
}

From source file:gov.usgs.anss.query.EdgeQueryClient.java

License:Open Source License

/** do a query.  The command line arguments are passed in as they are for the query tool
 * a files is created unless -t null is specified.  In that case the return is an ArrayList
 * containing ArrayLists<MiniSeed> for each channel returned
 *@param args The String array with args per the documentation
 *@return The ArrayList with ArrayLists of miniseed one for each channel returned.
 *///from   w  w  w  .  jav a 2  s  . com
public static ArrayList<ArrayList<MiniSeed>> query(EdgeQueryOptions options) {

    String line = "";

    long msSetup = 0;
    long msConnect = 0;
    long msTransfer = 0;
    long msOutput = 0;
    long msCommand = 0;
    long startTime = System.currentTimeMillis();
    long startPhase = startTime;

    byte[] b = new byte[4096];
    Outputer out = null;
    if (df6 == null) {
        df6 = new DecimalFormat("000000");
    }
    GregorianCalendar jan_01_2007 = new GregorianCalendar(2007, 0, 1);

    ArrayList<ArrayList<MiniSeed>> blksAll = null;
    String filename = "";
    BufferedReader infile = null;

    // TODO: Push this into EdgeQueryOptions in favour of a command line iterator.
    try {
        infile = new BufferedReader(options.getAsReader());
    } catch (FileNotFoundException ex) {
        logger.severe("did not find the input file=" + options.filenamein);
    }

    // the "in" BufferedReader will give us the command lines we need for the other end
    try {
        // for each line of input, read it, reformat it with single quotes, send to server
        int nline = 0;
        int totblks = 0;
        // particularly for the DCC we want this program to not error out if we cannot connect to the server
        // So make sure we can connect and print messages
        Socket ds = null;
        while (ds == null) {
            try {
                ds = new Socket(options.host, options.port);
            } catch (IOException e) {
                ds = null;
                if (e != null) {
                    if (e.getMessage() != null) {
                        if (e.getMessage().indexOf("Connection refused") >= 0) {
                            logger.warning("Got a connection refused. " + options.host + "/" + options.port
                                    + "  Is the server up?  Wait 20 and try again");
                        }
                    } else {
                        logger.warning("Got IOError opening socket to server e=" + e);
                    }
                } else {
                    logger.warning("Got IOError opening socket to server e=" + e);
                }
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException ex) {
                    // This isn't necessarily a major issue, and for the purposes
                    // of sleep, we really don't care.
                    logger.log(Level.FINE, "sleep interrupted.", ex);
                }
            }
        }
        InputStream in = ds.getInputStream(); // Get input and output streams
        OutputStream outtcp = ds.getOutputStream();
        msConnect += (System.currentTimeMillis() - startPhase);
        startPhase = System.currentTimeMillis();
        while ((line = infile.readLine()) != null) {
            if (line.length() < 2) {
                continue;
            }
            nline++;

            options = new EdgeQueryOptions(line);
            if (!options.isValid()) {
                logger.severe("Error @line " + nline);
                return null;
            }

            out = options.getOutputter();
            if (out == null) {
                blksAll = new ArrayList<ArrayList<MiniSeed>>(20);
            } else if (out instanceof SacOutputer) {
                ((SacOutputer) out).doQuery();
                continue;
            }

            // The length at which our compare for changes depends on the output file mask
            Comparator nsclComparator = options.getNsclComparator();

            long maxTime = 0;
            int ndups = 0;
            line = options.getSingleQuotedCommand();
            try {
                msSetup += (System.currentTimeMillis() - startPhase);
                startPhase = System.currentTimeMillis();
                boolean perfStart = true;
                outtcp.write(line.getBytes());
                int iblk = 0;
                NSCL nscl = null;
                boolean eof = false;
                MiniSeed ms = null;
                int npur = 0;
                ArrayList<MiniSeed> blks = new ArrayList<MiniSeed>(100);

                while (!eof) {
                    try {
                        // Try to read a mini-seed, if it failes mark eof
                        if (read(in, b, 0, (options.gapsonly ? 64 : 512))) {

                            if (b[0] == '<' && b[1] == 'E' && b[2] == 'O' && b[3] == 'R' && b[4] == '>') {
                                eof = true;
                                ms = null;

                                logger.fine("EOR found");

                            } else {
                                ms = new MiniSeed(b);
                                logger.finest("" + ms);
                                if (!options.gapsonly && ms.getBlockSize() != 512) {
                                    read(in, b, 512, ms.getBlockSize() - 512);
                                    ms = new MiniSeed(b);
                                }
                                iblk++;
                                totblks++;
                            }
                        } else {
                            eof = true; // still need to process this last channel THIS SHOULD NEVER  HAPPEN unless socket is lost
                            ms = null;
                            logger.warning("   *** Unexpected EOF Found");
                            if (out != null) {
                                System.exit(1); // error out with no file
                            }
                        }
                        if (perfStart) {
                            msCommand += (System.currentTimeMillis() - startPhase);
                            startPhase = System.currentTimeMillis();
                            perfStart = false;

                        }
                        logger.finest(iblk + " " + ms);
                        if (!options.quiet && iblk % 1000 == 0 && iblk > 0) {
                            // This is a user-feedback counter.
                            System.out.print("\r            \r" + iblk + "...");
                        }

                        if (eof || (nscl != null && (ms == null ? true
                                : nsclComparator.compare(nscl, NSCL.stringToNSCL(ms.getSeedName())) != 0))) {
                            msTransfer += (System.currentTimeMillis() - startPhase);
                            startPhase = System.currentTimeMillis();
                            if (!options.quiet) {
                                // TODO could go into a helper method
                                int nsgot = 0;
                                if (blks.size() > 0) {
                                    Collections.sort(blks);
                                    logger.finer(blks.size() + " " + iblk);
                                    for (int i = 0; i < blks.size(); i++) {
                                        nsgot += (blks.get(i)).getNsamp();
                                    }
                                    logger.finest("" + (MiniSeed) blks.get(blks.size() - 1));
                                    System.out.print('\r');
                                    DateTime dt = new DateTime().withZone(DateTimeZone.forID("UTC"));

                                    logger.info(hmsFormat.print(dt.getMillis()) + " Query on " + nscl + " "
                                            + df6.format(blks.size()) + " mini-seed blks "
                                            + (blks.get(0) == null ? "Null"
                                                    : ((MiniSeed) blks.get(0)).getTimeString())
                                            + " "
                                            + (blks.get((blks.size() - 1)) == null ? "Null"
                                                    : (blks.get(blks.size() - 1)).getEndTimeString())
                                            + " " + " ns=" + nsgot);
                                } else {
                                    System.out.print('\r');
                                    logger.info("Query on " + options.getSeedname() + " returned 0 blocks!");
                                }

                            }

                            if (blks.size() > 0) {
                                MiniSeed ms2 = blks.get(0);
                                if (out == null) { // Get the array list output
                                    ArrayList<MiniSeed> newBlks = new ArrayList<MiniSeed>(blks.size());
                                    for (int i = 0; i < blks.size(); i++) {
                                        newBlks.add(i, blks.get(i));
                                    }
                                    blksAll.add(newBlks);
                                } else { // create the output file
                                    if (options.getType() == OutputType.ms
                                            || options.getType() == OutputType.dcc
                                            || options.getType() == OutputType.dcc512
                                            || options.getType() == OutputType.msz) {
                                        filename = Filename.makeFilename(options.filemask, nscl, ms2);
                                    } else {
                                        filename = Filename.makeFilename(options.filemask, nscl,
                                                options.getBegin());
                                    }

                                    //filename = lastComp;
                                    // TODO - should happen in the makeFilename methods.
                                    filename = filename.replaceAll(" ", "_");

                                    logger.finest(((MiniSeed) blks.get(0)).getTimeString() + " to "
                                            + ((MiniSeed) blks.get(blks.size() - 1)).getTimeString() + " "
                                            + (((MiniSeed) blks.get(0)).getGregorianCalendar().getTimeInMillis()
                                                    - ((MiniSeed) blks.get(blks.size() - 1))
                                                            .getGregorianCalendar().getTimeInMillis())
                                                    / 1000L);

                                    // Due to a foul up in data in Nov, Dec 2006 it is possible the Q330s got the
                                    // same baler block twice, but the last 7 512's of the block zeroed and the other
                                    // correct.  Find these and purge the bad ones.

                                    if (!options.gapsonly) {
                                        for (int i = blks.size() - 1; i >= 0; i--) {
                                            if (blks.get(i).getBlockSize() == 4096 && // Has to be a big block or it does not happen
                                                    blks.get(i).getGregorianCalendar()
                                                            .compareTo(jan_01_2007) < 0
                                                    && blks.get(i).getUsedFrameCount() < blks.get(i)
                                                            .getB1001FrameCount()
                                                    && blks.get(i).getUsedFrameCount() <= 7
                                                    && blks.get(i).getB1001FrameCount() > 7) {
                                                blks.remove(i);
                                                npur++;
                                            }
                                        }
                                    }
                                    logger.finer("Found " + npur + " recs with on first block of 4096 valid");
                                    blks.trimToSize();
                                    //for(int i=0; i<blks.size(); i++) logger.finest(((MiniSeed) blks.get(i)).toString());
                                    // TODO: Change the signature to pass options only once.

                                    out.makeFile(nscl, filename, blks);
                                }
                            }
                            maxTime = 0;
                            if (blks.size() > 0) {
                                blks.clear();
                                System.gc(); // Lots of memory just abandoned.  Try garbage collector
                            }
                            msOutput += (System.currentTimeMillis() - startPhase);
                            startPhase = System.currentTimeMillis();
                        }

                        // If this block is the first in a new component, clear the blks array
                        //if(!lastComp.substring(0,compareLength).equals(
                        //    ms.getSeedName().substring(0,compareLength))) blks.clear();
                        /* in late 2007 there was some files which were massively duplicated by block.
                         * to prevent this from blowing memory when there are so may we eliminate and duplicate
                         * blocks here.  If it is massively out of order , all of these block checks will slow things
                         * down.
                         **/

                        boolean isDuplicate = false;
                        if (ms != null) {
                            if (ms.getTimeInMillis() <= maxTime) { // No need to check duplicates if this is newest seen
                                if (!options.gapsonly) {
                                    if (blks.size() >= 1) {
                                        for (int i = blks.size() - 1; i >= 0; i--) {
                                            if (ms.isDuplicate(blks.get(i))) {
                                                isDuplicate = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (!isDuplicate && ms.getIndicator().compareTo("D ") >= 0) {
                                    blks.add(ms);
                                } else {
                                    ndups++;
                                }
                            } else {
                                if (ms.getIndicator().compareTo("D ") >= 0) {
                                    blks.add(ms); // If its not D or better, its been zapped!
                                }
                                maxTime = ms.getTimeInMillis();
                            }
                            nscl = NSCL.stringToNSCL(ms.getSeedName());
                        }
                    } catch (IllegalSeednameException e) {
                        logger.severe("Seedname exception making a seed record e=" + e.getMessage());
                    }
                } // while(!eof)
                if (!options.quiet && iblk > 0) {
                    logger.info(iblk + " Total blocks transferred in "
                            + (System.currentTimeMillis() - startTime) + " ms "
                            + (iblk * 1000L / Math.max(System.currentTimeMillis() - startTime, 1)) + " b/s "
                            + npur + " #dups=" + ndups);
                }
                if (out == null) {
                    return blksAll; // If called in no file output mode, return the blocks
                }
                blks.clear();
            } catch (UnknownHostException e) {
                logger.severe("EQC main: Host is unknown=" + options.host + "/" + options.port);
                if (out != null) {
                    System.exit(1);
                }
                return null;
            } catch (IOException e) {
                if (e.getMessage().equalsIgnoreCase("Connection refused")) {
                    logger.severe(
                            "The connection was refused.  Server is likely down or is blocked. This should never happen.");
                    return null;
                } else {
                    logger.severe(e + " EQC main: IO error opening/reading socket=" + options.host + "/"
                            + options.port);
                    if (out != null) {
                        System.exit(1);
                    }
                }
            }
        } // End of readline
        outtcp.write("\n".getBytes()); // Send end of request marker to query
        if (ds.isClosed()) {
            try {
                ds.close();
            } catch (IOException e) {
            }
        }
        if (options.perfMonitor) {
            long msEnd = System.currentTimeMillis() - startPhase;
            logger.info("Perf setup=" + msSetup + " connect=" + msConnect + " Cmd=" + msCommand + " xfr="
                    + msTransfer + " out=" + msOutput + " last=" + msEnd + " tot="
                    + (msSetup + msConnect + msTransfer + msOutput + msEnd) + " #blks=" + totblks + " #lines="
                    + nline);
        }
        return null;
    } catch (IOException e) {
        logger.severe(e + " IOError reading input lines.");
    }
    return null;
}

From source file:graphene.util.time.JodaTimeUtil.java

License:Apache License

public static void test_localDate_shift_joda_tz() {
    System.out.println("Test LocalDate with shifted JodaTime timezone");
    final DateTimeZone originalTZ = DateTimeZone.getDefault();
    final DateTimeZone losAngelesTZ = DateTimeZone.forID("America/Los_Angeles");

    DateTimeZone.setDefault(losAngelesTZ);
    final LocalDate ld0 = new LocalDate(losAngelesTZ);
    System.out.println(/* ww  w  . j av a2 s.  c  o m*/
            "ld0 LocalDate(losAngelesTZ) = " + ld0 + " when default TZ = " + DateTimeZone.getDefault());

    DateTimeZone.setDefault(losAngelesTZ);
    final LocalDate ld1 = new LocalDate();
    System.out.println(
            "ld1 LocalDate()             = " + ld1 + " when default TZ = " + DateTimeZone.getDefault());

    final java.sql.Date d0 = toSQLDate(ld1);
    System.out
            .println("d0 toSQLDate(ld0)           = " + d0 + " when default TZ = " + DateTimeZone.getDefault());
    final java.sql.Date d1 = toSQLDate(ld1);
    System.out
            .println("d1 toSQLDate(ld1)           = " + d1 + " when default TZ = " + DateTimeZone.getDefault());
    DateTimeZone.setDefault(originalTZ);
    System.out
            .println("d1 toSQLDate(ld1)           = " + d1 + " when default TZ = " + DateTimeZone.getDefault());

    DateTimeZone.setDefault(originalTZ);
    final LocalDate ld2 = toLocalDate(d1);
    System.out.println(
            "ld2 toLocalDate(d1)         = " + ld2 + " when default TZ = " + DateTimeZone.getDefault());

    DateTimeZone.setDefault(originalTZ);
    if (!ld2.equals(ld1)) {
        throw new IllegalStateException();
    }
}

From source file:graphene.util.time.JodaTimeUtil.java

License:Apache License

public static DateTime toDateTime(final java.sql.Timestamp ts, final String timeZoneID) {
    // TODO - confirm this conversion always works, esp. across timezones
    final DateTime dt = (ts == null ? null : new DateTime(ts, DateTimeZone.forID(timeZoneID)));
    return dt;/*from   www  .ja v a2  s .  c  om*/
}

From source file:graphene.util.time.JodaTimeUtil.java

License:Apache License

public static DateTime toDateTime(final java.util.Date d, final String timeZoneID) {
    // TODO - confirm this conversion always works, esp. across timezones
    final DateTime dt = (d == null ? null : new DateTime(d, DateTimeZone.forID(timeZoneID)));
    return dt;//from ww w . j  a va2s .co m
}

From source file:griffon.plugins.jodatime.editors.DateTimeZonePropertyEditor.java

License:Apache License

private void handleAsString(String str) {
    if (isBlank(str)) {
        super.setValueInternal(null);
        return;//from   www . ja v a 2  s.c o  m
    }

    try {
        super.setValueInternal(DateTimeZone.forID(str));
    } catch (IllegalArgumentException e) {
        throw illegalValue(str, DateTimeZone.class, e);
    }
}

From source file:griffon.plugins.jodatime.JodatimeExtension.java

License:Apache License

public static DateTimeZone toDateTimeZone(String string) {
    return DateTimeZone.forID(string);
}

From source file:griffon.plugins.scaffolding.atoms.DateTimeZoneValue.java

License:Apache License

@Override
public void setValue(Object value) {
    if (value == null || value instanceof DateTimeZone) {
        super.setValue(value);
    } else if (value instanceof TimeZone) {
        super.setValue(DateTimeZone.forTimeZone((TimeZone) value));
    } else if (value instanceof CharSequence) {
        super.setValue(DateTimeZone.forID(value.toString()));
    } else {//from   w  w  w. j  a v  a  2  s . c o  m
        throw new IllegalArgumentException("Invalid value " + value);
    }
}

From source file:helper.JodaTimeHelper.java

public String calculateDateTime() {
    DateTime dt = new DateTime();
    dt = dt.withZone(DateTimeZone.forID("-05:00"));
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    return fmt.print(dt);
}