Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract day of year and Julian day from a string date

I have a string "2012.11.07" in python. I need to convert it to date object and then get an integer value of day of year and also Julian day. Is it possible?

like image 792
f.ashouri Avatar asked Dec 18 '12 23:12

f.ashouri


People also ask

How do you convert a string to a Julian date?

Use the DATE function to convert from a seven character string yyyyddd, where yyyy are digits representing a year and ddd are digits between 001and 366 denoting a day of that year.

How do you calculate Julian day of the year?

Multiply the number of non-leap years by 365, and the number of leap years by 366. Add the two totals together for a total number of days in all years. Subtract 10 days to account for the changeover in calendar type from Julian to Gregorian in 1582.

How do you find the Julian Day in Python?

to_julian_date() function convert the given TimeStamp to a Julian Date. The value for 0 Julian date is noon January 1, 4713 BC.

What is the Julian day of the year?

The first two digits are the last numbers of the year and the three digits after the hyphen are the day of that year. So, for instance, a Julian date of 21-001 represents the first day of the year 2021, or January 1, 2021, while a Julian date of 22-165 represents the 165th day of the year 2022 or June 14, 2022.


2 Answers

First, you can convert it to a datetime.datetime object like this:

>>> import datetime >>> fmt = '%Y.%m.%d' >>> s = '2012.11.07' >>> dt = datetime.datetime.strptime(s, fmt) >>> dt datetime.datetime(2012, 11, 7, 0, 0) 

Then you can use the methods on datetime to get what you want… except that datetime doesn't have the function you want directly, so you need to convert to a time tuple

>>> tt = dt.timetuple() >>> tt.tm_yday 312 

The term "Julian day" has a few different meanings. If you're looking for 2012312, you have to do that indirectly, e.g., one of the following.

>>> int('%d%03d' % (tt.tm_year, tt.tm_yday)) 2012312 >>> tt.tm_year * 1000 + tt.tm_yday 2012312 

If you're looking for a different meaning, you should be able to figure it out from here. For example, if you want the "days since 1 Jan 4713 BC" meaning, and you have a formula that requires Gregorian year and day in year, you've got those two values above to plug in. (If you have a formula that takes Gregorian year, month, and day, you don't even need the timetuple step.) If you can't work out where to go from there, ask for further details.

If you don't have a formula—and maybe even if you already do—your best bet is probably to look around PyPI and ActiveState for pre-existing modules. For example, a quick search turned up something called jdcal. I'd never seen it before, but a quick pip install jdcal and a brief skim of the readme, and I was able to do this:

>>> sum(jdcal.gcal2jd(dt.year, dt.month, dt.day)) 2456238.5 

That's the same result that the USN Julian date converter gave me.

If you want integral Julian day, instead of fractional Julian date, you have to decide which direction you want to round—toward 0, toward negative infinity, rounding noon up to the next day, rounding noon toward even days, etc. (Note that Julian date is defined as starting since noon on 1 Jan 4713BC, so half of 7 Nov 2012 is 2456238, the other half is 2456239, and only you know which one of those you want…) For example, to round toward 0:

>>> int(sum(jdcal.gcal2jd(dt.year, dt.month, dt.day))) 2456238 
like image 117
abarnert Avatar answered Oct 29 '22 16:10

abarnert


To get the Julian day, use the datetime.date.toordinal method and add a fixed offset.

The Julian day is the number of days since January 1, 4713 BC at 12:00 in the proleptic Julian calendar, or November 24, 4714 BC at 12:00 in the proleptic Gregorian calendar. Note that each Julian day starts at noon, not midnight.

The toordinal function returns the number of days since December 31, 1 BC at 00:00 in the proleptic Gregorian calendar (in other words, January 1, 1 AD at 00:00 is the start of day 1, not day 0). Note that 1 BC directly precedes 1 AD, there was no year 0 since the number zero wasn't invented until many centuries later.

import datetime  datetime.date(1,1,1).toordinal() # 1 

Simply add 1721424.5 to the result of toordinal to get the Julian day.

Another answer already explained how to parse the string you started with and turn it into a datetime.date object. So you can find the Julian day as follows:

import datetime  my_date = datetime.date(2012,11,7)   # time = 00:00:00 my_date.toordinal() + 1721424.5 # 2456238.5 
like image 33
ghostarbeiter Avatar answered Oct 29 '22 17:10

ghostarbeiter