date conversions

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

date conversions

Post by joleenf »

Is there anything already coded in McIDAS-V which performs date conversions easily? I am looking for something like DATELIST (from McIDAS-X), which when entered at the command line, the current day would be returned in CCYYDDD and day full_month_name year and YYYY/MM/DD formats. Also, a date could be entered to get the julian date returned or a date offset from the current date.

Joleen
User avatar
barryr
Posts: 213
Joined: Thu Jan 08, 2009 5:42 pm
Contact:

Re: date conversions

Post by barryr »

Hi Joleen,
I'm told that there's no way to list different date formats (like the McIDAS-X command DATELIST). But there is a User Preference that lets you change the date format that appears in McV output (e.g., the layer label when you display an image). It's described here:

http://www.ssec.wisc.edu/mcidas/doc/mcv ... ences.html

Does that help?

Barry
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: date conversions

Post by hproe »

Hi Joleen -

If you don't know. You may override whatever is specified in the Preferences wherever you can use the %timestamp%. E.g. when saving time sequence of frames with View>Capture>Movie... I specify in the Filename Template: %time:yyyyMMddHHmm% in order to get compact filenames while I am using the %timestamp% macro as specified through Preferences (in my case "yyyy-MM-dd HH:mmz") for more readable layer labels.

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

Re: date conversions

Post by joleenf »

Actually, because there is the conversion option in the User Preferences and the conversion is also handled when pulling data from the archive that I thought there might be something already hidden within McIDAS-V that is similar.

Joleen
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: date conversions

Post by tomw »

You can most easily use the Calendar class from Java...here's a little Jython:

>>> from java.util import Calendar
>>> cal = Calendar.getInstance()
>>> cal.set(2011,1,24)
>>> print cal.get(Calendar.DAY_OF_YEAR)
55

Note that months are Jan=0, Feb=1,...
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: date conversions

Post by joleenf »

Great! This is what I was looking for!

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

Re: date conversions

Post by joleenf »

Attached is a version of datelist which replicates the McIDAS-X DATELIST functionality. Please post comments or concerns. Place this in the user McIDAS-V jython library and use in the jython shell. This will function in a similar to the McIDAS command line datelist. It should be fairly easy to use in a script. See documentation for further help.

Thank-you to Jonathan and Tom W. for the advice/help.

This is a note for me as well as new users: the help prints nicely with "print datelist.__doc__" It gets a little jumbled using dumpObj(datelist)

Joleen
Attachments
datelist.txt
(6.37 KiB) Downloaded 449 times
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: date conversions

Post by joleenf »

Small change to the code.

The lines

# Force the user to enter year, jday and day as integers.
year, jday, day=int(year), int(jday), int(day)

should include the "inc" variable as well...

# Force the user to enter year, jday and day as integers.
year, jday, day, inc=int(year), int(jday), int(day), int(inc)
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: date conversions

Post by joleenf »

Another request was made so that when a user enters a julian day which exceeds the number of days in the year, an error is issued. A valid date will still be returned.

In addition, if the user just wants the date printed immediately, I suggest a wrapper which calls datelist and prints the date.

Code: Select all

def pd(year=-1, month=-1, day=-1, jday=-1, inc=0, form=["default"]):
   jd, status=datelist(year=-1, month=-1, day=-1, jday=-1, inc=0, form=["default"])
   print jd


datelist.py
(6.66 KiB) Downloaded 473 times
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: date conversions

Post by joleenf »

Update: "from types import *" line in datelist.py. That line should be removed. It is not necessary to make the type checking work correctly that import actually seems to fail when McV loads the datelist method.

Joleen

datelist.py
(6.64 KiB) Downloaded 489 times
Post Reply