forked from trondkr/model2roms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetimeFunctions.py
86 lines (63 loc) · 3.29 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
76
77
78
79
80
81
82
83
84
85
86
from netCDF4 import Dataset, date2num, num2date
import os
from datetime import datetime, timedelta
import calendar
__author__ = 'Trond Kristiansen'
__email__ = '[email protected]'
__created__ = datetime(2015, 8, 11)
__modified__ = datetime(2018, 4, 25)
__version__ = "1.5"
__status__ = "Development, modified on 19.04.2018 by A.Barthel, " \
"refactored 25.04.2018 by Trond Kristiansen"
# Methods for returning list of months and days for the given time-step.
def createlistofmonths(confM2R, currentyear):
if currentyear == confM2R.startdate.year:
IDS = [confM2R.startdate.month + m for m in range(13 - confM2R.startdate.month)]
# months from start month to end of first year
elif currentyear == confM2R.enddate.year:
IDS = [1 + m for m in range(int(confM2R.enddate.month))]
# months from first month to last month of last year
elif confM2R.startdate.year < currentyear < confM2R.enddate.year:
# months from first month to last month of last year
IDS = [1 + m for m in range(12)]
if confM2R.startdate.year == confM2R.enddate.year:
# months from first month to last month of last year
IDS = [confM2R.startdate.month + m for m in range(confM2R.enddate.month - confM2R.startdate.month + 1)]
if confM2R.isclimatology:
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(confM2R, year, month):
days = []
if confM2R.timefrequencyofinputdata == 'day':
daystep = 7
if daystep > 1:
print("WARNING!")
print("----------------------------------------------------------------------")
print("You are only using every {} days of input data! (model2roms.py)".format(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 == confM2R.startdate.month and year == confM2R.startdate.year and confM2R.startdate.day > 1):
days = [i for i in range(confM2R.startdate.day, ndays, daystep)]
# We finish in the last month before last day of month
if (month == confM2R.enddate.month and year == confM2R.enddate.year):
days = [i + 1 for i in range(0, confM2R.enddate.day, daystep)]
# We start and end on different days in the same month
if (confM2R.startdate.month == confM2R.enddate.month and confM2R.startdate.year == confM2R.enddate.year):
days = [i for i in range(confM2R.startdate.day, confM2R.enddate.day, daystep)]
if confM2R.timefrequencyofinputdata == '5days':
for dd in confM2R.timeobject[:]:
dd_date = num2date(dd,units=confM2R.timeobject.units, calendar=confM2R.timeobject.calendar)
if dd_date.year==year and dd_date.month==month:
days.append(dd_date.day)
print(dd_date)
return days
if confM2R.timefrequencyofinputdata == 'month':
days = [15]
return days