[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[THREDDS #AWG-971354]: TDS Netcdf time dimension name changes



Greetings!

Trying to make a collection of GRIB messages look like a netCDF Dataset is a
pretty interesting process. Because each GRIB message is essentially one 
variable
at one level (or layer) at one time means we have to aggregate similar grids
together into multidimensional variables. Part of that process involves creating
the dimensions, such as time. Unfortunately, we do not control the order in 
which
we receive the messages (nor if we receive all messages), so we have to take a
rather simplistic approach in how we create and name things. For something like
time, whatever variable is stored in the first message we read in a given GRIB
file (just a bunch of GRIB messages concatenated together), we will give that a
time dimension called "time". The next message we encounter with a different 
flavor
of time (say, precip accumulation over a time period), we'll call that dimension
time2. As you've seen, with GRIB, there can be many different distinct time 
dimensions, and the naming isn't predictable in the sense that you can hard code
"time" in as the dimension and variable ahead of a priori. However, you can
programmaticly look at the dimensions on a variable to figure out dimension 
names
and access their values through their associated coordinate variables, all 
without
ever using a hardcoded string variable.

For any given dataset, the name of the time dimension might change, but its
position in the listing of dimensions will remain the same. You can use this to
get the times, lats, and lons. For example, let's say we have a Dataset with the
variable "Geopotential_height_isobaric":

# what are the dimension names of the variable "Geopotential_height_isobaric"
print(dataset.variables['Geopotential_height_isobaric'].dimensions)
('time1', 'isobaric4', 'lat', 'lon')

# get the name of the time dimension associated with the 
# "Geopotential_height_isobaric"
dtime = dataset.variables['Geopotential_height_isobaric'].dimensions[0]
# the vaule of dtime is 'time1'

# get the array of time values (values are seconds since some start time, or 
# something like that)
times = dataset.variables[dtime]
# Convert times using the num2date function available through netCDF4
# (gives you python datetime objects)
vtimes = num2date(times[:], times.units)

# get the names of the lat and lon dimensions
dlat = dataset.variables['Geopotential_height_isobaric'].dimensions[2]
dlon = dataset.variables['Geopotential_height_isobaric'].dimensions[3]

# get the lat and lon arrays associated with "Geopotential_height_isobaric"
lat = dataset.variables[dlat][:]
lon = dataset.variables[dlon][:]

Let me know if that help!

Sean

> Hello-
> 
> I have been using python to create weather products based on the data found 
> on thredds.ucar.edu
> 
> I have noticed in some on the model data that the time dimension varies 
> between time, time1, time2, an time3
> 
> Is there any way to predict this? Or programmatically a way to dynamically 
> set this so I can handle the variability gracefully.
> 
> Please help me understands this variability.
> 
> Thanks,


Ticket Details
===================
Ticket ID: AWG-971354
Department: Support THREDDS
Priority: Normal
Status: Open
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.



NOTE: All email exchanges with Unidata User Support are recorded in the Unidata inquiry tracking system and then made publicly available through the web. If you do not want to have your interactions made available in this way, you must let us know in each email you send to us.