Translate Day-of-Year Number

QUESTION: The data file I am reading encodes the date (e.g., the date 28 April 2007) as a long integer, like this: 2007118. The first four digits are the year, and the next three digits are the day-of-year number, going from 1 to 365 (or 366, if it is a leap year). In order to calculate a Julian day number for my time data axis, I apparently need to convert this day-of-year nummber into a month and day number. Is there a way to do that in IDL?

ANSWER: The gold standard for anything having to do with Julian numbers or time/date manipulation are the IDL routines in the Johns Hopkins University Applied Physics Lab IDL Library. In this case, you would use a routine named YDN2MD. But if you don't have the JHUAPL library, don't worry. In this case, you can do what you want with good ol' IDL.

James Kuyper points out that a combination of JULDAY and CALDAT would be able to give you the month and day directly:

   IDL> date = 2007118L
   IDL> year = Fix(StrMid(StrTrim(date,2), 0, 4))
   IDL> dayofyear = Fix(StrMid(StrTrim(date,2), 4, 3))
   IDL> CALDAT, JULDAY(1, dayofyear, year), month, day
   IDL> Print, month, day
           4          28

Although, presumably, if you just wanted a Julian number, you wouldn't have to bother doing this in two steps, since JULDAY can do it directly for you. Kuyper points out that this “technique of using January 118th to find the 118th day of the year seems to work with a lot of different date-handling functions on a wide variety of platforms.”

JD Smith has this to add:

If you worry that Kuyper's trick goes against the documented input requirements and so could be broken by future updates, just ask for the Julian date of Jan 1st, then add 117 days:

   IDL> print,julday(1,1,2007)+(118-1)

This doesn't validate your input time string (e.g. 2004366 is valid, 2000366 is not), but it will get the correct answer either way.

Web Coyote's Guide to IDL Programming