Re: [netcdf-java] Best practice for getting the initial (and/or final) time values of a Variable?

  • To: Java NetCDF <netcdf-java@xxxxxxxxxxxxxxxx>
  • Subject: Re: [netcdf-java] Best practice for getting the initial (and/or final) time values of a Variable?
  • From: James Adams - NOAA Affiliate <james.adams@xxxxxxxx>
  • Date: Wed, 13 Aug 2014 10:41:01 -0400
Thanks for your help, Jon.  I'm reticent to cook third-party JAR
dependencies into my code for a number of reasons if I can avoid it. It's
looking like there's no real best practice for this sort of thing and
everyone just rolls their own, so maybe I should just follow suit?  The
NetCDF-Java API is all over the place, at least based on what I can see in
the Javadoc.  (For example there's a CalendarDateUnit class with no
constructor and flotsam comments from you and Bob Simon included within the
main description section -- is this class useful at all or recommended?)

​For now the below is what I've cooked up​ -- does this look reasonable?
 It seems less than bulletproof (for example how do you best account for
time zone, etc.?) but maybe it's good enough for now, this code isn't going
to be part of a nuclear reactor control system. ;)

            Variable timeCoordinateVariable =
            String timeUnits =
            ArrayInt timeArray = (ArrayInt);
            DateUnit dateUnit = new DateUnit(timeUnits);
            Date initialDate = dateUnit.makeDate(timeArray.getInt(0));
            DateTime initialDateTime = new DateTime(initialDate.getTime(),
            int initialYear = dateTime.getYear();


On Wed, Aug 13, 2014 at 3:02 AM, Jon Blower <j.d.blower@xxxxxxxxxxxxx>

> Hi James,
> The ncWMS libraries contain some high-level calls that might help here.
> The class is the one you want:
> NetcdfDataset nc =“/path/to/my/netcdf/file”);
> GridDatatype grid = CdmUtils.getGridDatatype(nc, “myvar”);
> CoverageMetadata cm = CdmUtils.readCoverageMetadata(grid);
> List<DateTime> dateTimes = cm.getTimeValues();
> This gives a list of all the DateTime objects (these are from the
> joda-time library) in the time axis of the variable “myvar”. They will be
> referenced to a given Chronology (i.e. calendar system), which you can find
> through cm.getChronology().
> The main snag is that your variable has to be fully georeferenced,
> otherwise readCoverageMetadata() won’t work. A common problem here is that
> the vertical axis isn’t always well defined in NetCDF files.
> If you’re interested in this solution you can build a jar containing the
> above code:
> svn checkout svn:// ncwms
> cd ncwms
> ant jar-for-THREDDS
> Hope this helps,
> Jon
> --
> Dr Jon Blower,
> Technical Director, Reading e-Science Centre,
> MELODIES project coordinator,
> School of Mathematical and Physical Sciences,
> University of Reading
> Tel: +44 118 378 5213
> Email: j.d.blower@xxxxxxxxxxxxx
  • 2014 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: