There is always a lot of questions
about the
time axis and how to properly understand it and convert between
different time representations and different time units, in order to,
for example, analyze two datasets with different time
representations.
We are providing here the tutorials on the time axis and different ways
of working with it.
In this tutorial you will learn
how to read tje time axis, change time from relative to component
time, or change units of a time axis.,
############################################# # #
Time Concepts: #
#############################################
############################################# # #
Qn : How do I check what times the data is defined for? #
Ans : You can retrieve the time axis using the getTime() method #
and print
out the time axis units and values. #
For
example: #
############################################# f
= cdms.open(TESTDIR + 'ta_1994-1998.nc') ta
= f('ta') time_axis
= ta.getTime() print
'\nTime units are : \n', time_axis.units print
'\nTime axis values are : \n', time_axis[:]
############################################# # #
Qn : How do I see what the start time in the data set is? # #
Ans : The first value in the time axis corresponds to the first time
point. # ############################################## print
'\nFirst time point is ', time_axis[0]
############################################## # #
Qn : What does this number signify? #
Ans : The number signifies a "relative" time i.e "value since
basetime" # #
Qn : How do I know what "calendar time" this corresponds to? #
Ans : The "calendar time" is referred to as "component time" in CDAT. #
To get
the time in "component" time format: # ############################################### import
cdtime print
'\nStart time = ', cdtime.reltime(time_axis[0],
time_axis.units).tocomp()
#
Similarly you can print the last or end time....
print
'End time = ', cdtime.reltime(time_axis[-1],
time_axis.units).tocomp()
############################################### # #
Extra! : You can also just use a function asComponentTime() #
that converts all the time points in the time axis to #
"component time" and then you can just print the start and end times # ############################################### all_component_times
= time_axis.asComponentTime() print
'\nStart time = ', all_component_times[0], print
'End time = ', all_component_times[-1]
############################################### # #
Qn : How do I extract the data based on time axis values? #
Ans : Since you know the first and last time point you are #
interested in, you can specify those in the data extraction stage. # ############################################### ta_slice
= f('ta', time=(0., 15.)) print
'\nTime values in ta_slice = ', ta_slice.getTime()[:]
############################################### # #
Qn : How do I extract a time slice with specific #
start and
end times? Say jan-dec 1996. #
Ans : You can actually specify time = (start, end) in the following
way. # ############################################### ta_1996_only
= f('ta', time=('1996-1-1', '1996-12-1'))
############################################### # #
A more precise alternative would be to create the #
component times for the start and end time points #
of interest and select the variable at #
'time = (start_component_time, end_component_time)' #
This would look like: # ############################################### st
= cdtime.componenttime(1996, 1, 1) en
= cdtime.componenttime(1996, 12, 1) ta_1996_only
= f('ta', time=(st, en))
############################################### # #
Extra! : We can specify "time selectors" just like we specified #
spatial selectors for re-use. The hypothetical case of the time #
period of interest between May 1996 and Feb 1997 would look like: # ############################################## my_time
= cdms.selectors.Selector(time =('1995-5-1', '1997-2-1'))
############################################## # #
Note that we do not need the cdutil.region specification #
because unlike lat and lon dimensions, we do not readjust #
the bounds on time axes. # ############################################## ta_time
= f('ta', my_time) print
'\nThe data ta_time extracted using my_time starts at ', print
cdtime.reltime(ta_time.getTime()[0],
ta_time.getTime().units).tocomp()
----------------------------
#
Now creating an axis ##
First let's create the value vals=Numeric.arange(3,24,6,'d') tim=cdms.createAxis(vals) tim.units='hours
since 2004-6-16' # Make sure it is a time axis
nomater its name tim.designateTime() tim.id='time'
##
or if we want to change the units to something else ##
say some standard units: days since 1900 ##
The following takes care of converting the values and bounds ##
appropriately tim.toRelativeTime('days
since 1900') ##
Now let's apply it to data MV array data.setAxis(0,tim)
Advanced
Tech Tips
Spinning the Earth - learn
how to
make a movie of spinning Earth together with the overplotted cloud
fields
and wind vectors.
We will show you how to create a very cool
presentation of your 3D earth data.
To see the simulation click on the icon
below:
Here is a script to produce the
simulation above.
#!/usr/bin/env
python """ This
exmaple shows how to draw a spininng animated earth (gif) TECHNICS
DEMONSTRATED: ANIMATED GIF, PROJECTIONS MODULE
USED: cdms,vcs,cdutil """
for
i in range(12):
print 'Slice : ',i,'of 12' # Sets the longitude to watch
lon1=(lon1+180)%360-180
print lon1
p.centerlongitude=lon1 ##
retrieve the correct slice
s2=s(time=slice(i,i+1))
u2=u()
v2=v()
x.plot(s2,t,iso,bg=1)
u2.long_name = 'deg C, Air Temp & Wind Speed'
x.plot(u2,v2,t,vc,bg=1)
t.crdate.priority = 1
lon1-=30
a='a'
if i==0: a='r' #
First time create a new gif
x.gif('spinning_earth',merge=a,
geometry='450x400')
x.clear()