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

Thanks for your helpful response, Bob.  Some follow up comments are below

On Tue, Aug 12, 2014 at 7:13 PM, Bob Simons - NOAA Federal <
bob.simons@xxxxxxxx> wrote:

> On 2014-08-12 3:01 PM, James Adams - NOAA Affiliate wrote:
>  I would like to create a method that takes a NetCDF Variable as an
> argument and returns the Variable's start time (or optionally the end
> time).  I can imagine how I'd cook up such a method by getting the
> variable's time dimension, then getting the start (or end) date using the
> time dimension's units, etc., but it'll be several lines of code and I'm
> hoping that instead someone has done this sort of thing before that I can
> reuse, or if not then maybe there's a best practice that I could follow.
>  For example, I'd like to create a couple methods with the following
> signatures (or something similar):
>  public Date getInitialDate(Variable variable);
>  public Date getFinalDate(Variable variable);
>  or more generally:
>  public Date getDate(Variable variable, int timeIndex);
> Can anyone advise?  Thanks in advance for any suggestions.
> Those signatures are odd. Only variables that use a time dimension have a
> start and an end time. Are you going to throw an exception if there is no
> time dimension associated with the variable?
​The signatures I've proposed above are for methods that I would include
only within some code that I have written for our purposes at work, and not
for inclusion within the NetCDF Java API, as perhaps may be assumed.  Sorry
for any confusion there, I should have been more clear about what I was
proposing to do.  In my program I'm reading NetCDF data set files which
typically contain variables with time/lon/lat dimensions, and this is
validated before the proposed methods would be called, i.e. if the data
sets being processed contain variables without a single time dimension then
an exception is thrown, just as you have suggested.

 And if the methods are going to all the trouble of figuring out if there
> is a associated time dimension, then why not have it return two Dates: the
> begin and end dates?
​Actually this is exactly what I'm planning to do.  In my code I flesh out
a POJO which encapsulates relevant information about the data sets I'm
processing, and this initial/final time is information I'd like to find out
once to store for later reference, avoiding the need to repeat the multiple
lines of code necessary to get the same information again.

>  And very few datasets have multiple time dimensions / different time
> dimensions for different variables. Doesn't it almost always make sense to
> just find the time variable, e.g., time[time] and return the first and last
> values as Dates?
​This sounds like a reasonable idea, and just the sort of suggestion I was
fishing for.

> And Dates don't deal with different calendars. And a surprising number of
> datasets have dates in year 0000 and 0001. Don't you want
> ​​
> GregorianCalendar objects instead?

​This is relevant, as the time coordinate variables I'm reading and writing
are using a Gregorian calendar.  I was actually planning to use Joda
DateTime objects within my code.  Is the normal/best practice when handling
time values to use
GregorianCalendar objects?  I see that there's a CalendarDate class in the
NetCDF Java API -- is this an even better class to use?

> Yes, it's several lines of code and how it works depends on whether or not
> the dataset conforms to CF metadata standard (or some other standard), but
> I think that's what you need to do.
​I was afraid of this.  I've been learning Python recently, and so much of
Python just works like magic with so few lines of code -- maybe I'm
starting to get spoiled.  I thought I should hunt around to see if there
were some magic Java-NetCDF methods or libraries with handy conveniences
for things like time that I wasn't aware of yet.

> There is an ACDD standard that is similar:
> see time_coverage_start and time_coverage_end at
> but it is not as widely used as the CF standard.
> Perhaps there is a netcdf-java library call that is more suitable. Someone
> else may know.
> Good luck.
Thanks again for your helpful response.​  Obviously I'm not yet adept at
handling time values with NetCDF-Java (I'm still learning the ropes in many
areas, actually), and I've definetely been confused about this sort of
thing with NetCDF-Java before (see
 But this is how you learn...

>  --James
> _______________________________________________
> netcdf-java mailing listnetcdf-java@xxxxxxxxxxxxxxxx
> For list information or to unsubscribe, visit: 
> --
> Sincerely,
> Bob Simons
> IT Specialist
> Environmental Research Division
> NOAA Southwest Fisheries Science Center
> 1352 Lighthouse Ave
> Pacific Grove, CA 93950-2079
> Phone: (831)658-3205
> Fax: (831)648-8440
> Email: bob.simons@xxxxxxxx
> The contents of this message are mine personally and
> do not necessarily reflect any position of the
> Government or the National Oceanic and Atmospheric
> Administration.
> <>< <>< <>< <>< <>< <>< <>< <>< <>< <><
  • 2014 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: