-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetimeFunctions.py
executable file
·75 lines (57 loc) · 2.85 KB
/
datetimeFunctions.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
64
65
66
67
68
69
70
71
72
73
74
75
from netCDF4 import Dataset, datetime, date2num,num2date
import os
import calendar
__author__ = 'Trond Kristiansen'
__email__ = '[email protected]'
__created__ = datetime(2015, 8, 11)
__modified__ = datetime(2015, 8, 11)
__version__ = "1.5"
__status__ = "Development, modified on 11.08.2015"
# Methods for returning list of months and days for the given time-step.
def createListOfMonths(currentyear,startdate,enddate,isClimatology):
print(currentyear, startdate.year, enddate.year)
if currentyear == startdate.year:
IDS=[startdate.month+m for m in range(13-startdate.month)]
# months from start month to end of first year
elif currentyear == enddate.year:
IDS=[1+m for m in range(int(enddate.month))]
# months from first month to last month of last year
elif startdate.year < currentyear < enddate.year:
# months from first month to last month of last year
IDS = [1+m for m in range(12)]
if startdate.year == enddate.year:
# months from first month to last month of last year
IDS = [startdate.month + m for m in range(enddate.month - startdate.month)]
print("Months for year %s : %s"%(currentyear,IDS))
if isClimatology==True:
IDS = [i+1 for i in range(12)]
if not IDS:
print("Unable to generate IDS for time looping: main.py -> func createIDS")
sys.exit()
return IDS
def createListOfDays(year,month,startdate,enddate,isClimatology,timeFrequencyOfInputData):
days=[]
if timeFrequencyOfInputData == 'day':
dayStep=7
if dayStep > 1:
print("WARNING!")
print("----------------------------------------------------------------------")
print("You are only using every %s days of input data! (model2roms.py)"%(dayStep))
print("----------------------------------------------------------------------")
# Regulary we want all days in each month
ndays = int(calendar.monthrange(year, month)[1])
days = [d+1 for d in range(0,ndays,dayStep)]
# Exceptions:
# We start in the first month after day one
if ( month == startdate.month and year == startdate.year and startdate.day > 1):
days = [i for i in range(startdate.day,ndays,dayStep)]
# We finish in the last month before last day of month
if ( month == enddate.month and year == enddate.year ):
days = [i+1 for i in range(0,enddate.day,dayStep)]
# We start and end on different days in the same month
if ( startdate.month == enddate.month and startdate.year == enddate.year ):
days = [i for i in range(startdate.day,enddate.day,dayStep)]
print("days", days)
if timeFrequencyOfInputData == 'month':
days = [15]
return days