visad.DateTime setFormatPattern is global

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

visad.DateTime setFormatPattern is global

Post by joleenf »

Hi,

I noticed what I believe is a slightly odd behavior of the setFormatPattern method. While a format pattern is set on a visad.DateTime object, it gets applied globally in the jython shell.

Code: Select all


def testload(local=False):
   """
       Returns a small set of addeParms to begin testing a file load.  Additional
       parameters can be added.  This is done to speed testing.
       
       Input:
          local = boolean (default=False) -- determines whether or not a local
                  test dataset will be used.  By default, parameters to match the
                  eastl server are returned.
   """

   centerLocation = {'lat':39.8, 'lon':-103.8}

   if (local):
       visDataSet = getLocalADDEEntry(dataset=dataSet, imageType='Colorado VIS May21')
       parmsS = dict(
           localEntry=visDataSet,
           unit='BRIT',
           band=1,
           mag=(1,1),
           size=(200,200),
           place=CENTER,
           coordinateSystem=LATLON,
           location=(centerLocation['lat'],centerLocation['lon'])
       )
   else:
       parms = dict(
           server='eastl.ssec.wisc.edu',
           dataset='EASTL',
           descriptor='CONUS',
           band=2,
           unit='TEMP',
           mag=(1,1),
           size=(200,200),
           place=CENTER,
           coordinateSystem=LATLON,
           location=(centerLocation['lat'],centerLocation['lon'])
       )
   return parms

addeParam=testload()
data=loadADDEImage(**addeParam)

nominalTime1=data['nominal-time']
print 'Original date/time format from first loadADDEImage is {}'.format(nominalTime1.getFormatPattern())
print 'Original date/time is {}'.format(nominalTime1)

nominalTime1.setFormatPattern('YYYYddd HH:mm:ss z')
print 'Current date/time format from first loadADDEImage is now {}'.format(nominalTime1.getFormatPattern())
print 'Current date/time is {}\n'.format(nominalTime1)


print '****Load a second set of data****'
addeParam['position']=-1
data2=loadADDEImage(**addeParam)
nominalTime2=data2['nominal-time']
print 'Original date/time format from second loadADDEImage is {}'.format(nominalTime2.getFormatPattern())
print 'Second loadADDEImage date/time is {}'.format(nominalTime2)

print '\nIn jython shell, the format pattern is applied globally, overidding anything set in User Preferences rather than being local.\n'
print 'Do you get the same result?\n'
print 'Is there a reason for this, or is this a mistake?'



Could you run some version of the code to test this observation?

Thanks,
Joleen
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: visad.DateTime setFormatPattern is global

Post by joleenf »

Maybe I should just be using formattedString method always and avoiding setFormatPattern?

Code: Select all

tz=data['nominal-time'].getFormatTimeZone()
nominalTime=data['nominal-time'].formattedString('YYYY-MM-dd HH:mm:ss z',tz)



Joleen
User avatar
bobc
Posts: 990
Joined: Mon Nov 15, 2010 5:57 pm

Re: visad.DateTime setFormatPattern is global

Post by bobc »

Hi Joleen -

I'll look into formattedString more, but I'm replicating your results with setFormatPattern. I took a look at the DateTime Class in the VisAD javadocs and it looks like setFormatPattern is actually working as expected:

setFormatPattern

public static void setFormatPattern(String pattern)

Set the format of the output of the toString() method. All DateTime objects created in the JVM once this is set will use the new format so be very careful in your use of this method. The pattern uses the time format syntax of java.text.SimpleDateFormat.

Parameters:
pattern - time format string
See Also:
If you want to use a time zone other than the default,, setFormatTimeZone(java.util.TimeZone)

Notice the bolded text.

Thanks -
Bob Carp
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: visad.DateTime setFormatPattern is global

Post by joleenf »

Hi Bob,

That answers my question. I should be using formattedString instead of setFormatPattern.

Thanks,
Joleen
User avatar
Jon
Posts: 192
Joined: Fri Jan 09, 2009 8:44 pm
Location: Madison, WI

Re: visad.DateTime setFormatPattern is global

Post by Jon »

No matter what the docs say, the API seems questionable--though renaming to "setDefaultFormatPattern" could've helped.

My vote would be to deprecate setFormatPattern/setFormatTimeZone (and their respective getters). We probably can't simply remove them...though a cursory glance through VisAD/IDV/McV turns up only a handful of uses.
User avatar
bobc
Posts: 990
Joined: Mon Nov 15, 2010 5:57 pm

Re: visad.DateTime setFormatPattern is global

Post by bobc »

Thanks for your thoughts, Jon. I agree that the setFormatPattern method's results are unexpected. I wrote up Inquiry 2429 to consider changes to setFormatPattern. I mentioned both of your proposed changes in the Request (change the name or deprecate it). I agree that the getter/setter methods can't be removed since they are used elsewhere in the code and other users may use them in their scripts.

- Bob
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: visad.DateTime setFormatPattern is global

Post by joleenf »

Another related question:

How are the formats of the 'time' and 'day' value formats set in the returned listADDEImages dictionary? I was wondering if the format would remain the same from user to user regardless of how they set their date format in the User Preferences or if these would be determined by that preference? I am just trying to find a best practice when it comes to using dates and manipulating them for filenames, directories, etc. I thought that working with the visad.DateTime object was the answer, but I am now wondering if that is problematic.

Joleen
User avatar
bobc
Posts: 990
Joined: Mon Nov 15, 2010 5:57 pm

Re: visad.DateTime setFormatPattern is global

Post by bobc »

Hi Joleen -

I ran some tests and it looks like the formats of the 'datetime' and 'nominal-time' keys returned from listADDEImages do use the time format preference that is set when McIDAS-V is started up. It looks like the formattedString method you mentioned earlier might be a good way to ensure that the format of the date/time is what you expect.

Thanks -
Bob
Post Reply