-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyearfrac.py
executable file
·63 lines (54 loc) · 2.14 KB
/
yearfrac.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from datetime import datetime as dt
import time
import numpy as np
def yearfrac(date):
"""
% (C) Nick Holschuh - Penn State University - 2015 ([email protected])
% This function takes a datetime object and converts it to a decimal year
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are as follows:
%
% date -- datetime object or list of datetime objects
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The outputs are as follows:
%
% date_output -- date as decimal year
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
"""
##def sinceEpoch(tempdate): # returns seconds since epoch
## return time.mktime(tempdate.timetuple())
##
##if len(date) > 1:
## date_output = []
## for i in date:
## s = sinceEpoch(i)
## year = i.year
## startOfThisYear = dt(year=year, month=1, day=1)
## startOfNextYear = dt(year=year+1, month=1, day=1)
## yearElapsed = s(i) - s(startOfThisYear)
## yearDuration = s(startOfNextYear) - s(startOfThisYear)
## fraction = yearElapsed/yearDuration
## date_output.append(i.year+fraction)
##
##else:
## s = sinceEpoch(date)
## year = date.year
## startOfThisYear = dt(year=year, month=1, day=1)
## startOfNextYear = dt(year=year+1, month=1, day=1)
## yearElapsed = s(date) - s(startOfThisYear)
## yearDuration = s(startOfNextYear) - s(startOfThisYear)
## fraction = yearElapsed/yearDuration
## return date.year + fraction
if len(date) > 1:
date_output = []
for i in date:
years = i.astype('datetime64[Y]').astype(int) + 1970
months = i.astype('datetime64[M]').astype(int) % 12 + 1
days = i - i.astype('datetime64[M]') + 1
days = (days).astype('int')
#print(years,months,days)
date_output.append(float(years)+float(months)/12+float(days)/365.25)
return date_output