Page 1 of 1

Formatting a Date

Posted: Fri Sep 27, 2013 5:15 pm
by joleenf
The class visadDateTime provides a simple way to format a date string. I have chosen to use this class since this is what is returned in the metadata from getADDEImage in McIDAS-V versions 1.4 or later. In McIDAS-V version 1.3, getADDEImage returns a java.util.Date.

1.) Convert a java.util.Date contained in the variable nominalTime to a visad.dateTime string

Code: Select all

 from visad import DateTime
nominalTime = DateTime(nominalTime)


2.) Set a time zone preference

Code: Select all

from java.util import SimpleTimeZone
timeZoneObject = SimpleTimeZone(0,"UTC")


3.) Use the formatting available in the visad.dateTime class

Code: Select all

  theYearJulianDayVersion = nominalTime.formattedString("yyyyDDD HHmmss", timeZoneObject)
  theYMDVersion = nominalTime.formattedString("yyyy-MM-dd HH:mm:ss", timeZoneObject)
  theYMD_TimeZoneVersion= nominalTime.formattedString("yyyy-MM-dd HH:mm:ss z", timeZoneObject)



4.) Put together when obtaining data via getADDEImage

Code: Select all

# import needed java classes
from java.util import SimpleTimeZone
from visad import DateTime

# import a python class
import time

addeParms = dict(
  server = 'adde.ucar.edu',
  dataset = 'GINIEAST',
  descriptor = 'GE1KVIS',
  accounting = ('IDV', '0'),
  size=(480,640),
  coordinateSystem=LATLON,
  place=CENTER,
  location=(39, -93),
  unit='BRIT',
)

metaDataVIS, visDATA = getADDEImage(band=1,**addeParms)


nominalTime = metaDataVIS["nominal-time"]

# set the timezone in the date string to UTC
timeZoneObject = SimpleTimeZone(0,"UTC")

#NOTE:  In McIDAS-V versions 1.3 or earlier, getADDEImage metadata returns the nominal
# and start times as java.util.Date object
# McIDAS-V version 1.4, returns a visad.DateTime object
# account for this difference here.

if isinstance(nominalTime, java.util.Date):
  nominalTime = DateTime(nominalTime)
  time.sleep(3)
elif isinstance(nominalTime, DateTime):
  pass
else:
  myException = 'TypeError: The variable nominalTime contains the value: "' +nominalTime+ '." This is not a java.util.Date or visad.DateTime'
  raise(myException)

theYearJulianDayVersion = nominalTime.formattedString("yyyyDDD HHmmss", timeZoneObject)
theYMDVersion = nominalTime.formattedString("yyyy-MM-dd HH:mm:ss", timeZoneObject)
theYMD_TimeZoneVersion = nominalTime.formattedString("yyyy-MM-dd HH:mm:ss z", timeZoneObject)

print theYearJulianDayVersion
print theYMDVersion
print theYMD_TimeZoneVersion


This is the code above is in this script
visadDateTimeFormattingExample.py
(1.39 KiB) Downloaded 543 times


I adding a "sleep" period after creating the dateTime object because there appeared to be some lag which interfered with proper annotations when trying to display the time on an image.

For formatting information, follow the documentation at
http://docs.oracle.com/javase/7/docs/ap ... ormat.html

This general formula should work within McIDAS-V as long as the object is either a java.util.Date or a visad.dateTime.